You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Valentin CHEMIERE <valentin.chemiere@akretion.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models, fields
  5. from odoo.tools.translate import _
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class StockPicking(models.Model):
  9. _inherit = 'stock.picking'
  10. availability_sent_by_sms = fields.Boolean(default=False)
  11. # TODO use a templating instead
  12. @api.multi
  13. def _prepare_availability_by_sms_notification(self):
  14. self.ensure_one()
  15. gateway = self.env['sms.gateway'].search([
  16. ('default_gateway', '=', True)], limit=1)
  17. return {
  18. 'gateway_id': gateway.id,
  19. 'message': _('Your picking %s is ready to transfer.') % self.name,
  20. 'mobile': self.partner_id.mobile,
  21. 'partner_id': self.partner_id.id,
  22. 'state': 'draft',
  23. 'validity': gateway.validity,
  24. 'classes': gateway.classes,
  25. 'deferred': gateway.deferred,
  26. 'priority': gateway.priority,
  27. 'coding': gateway.coding,
  28. 'nostop': gateway.nostop,
  29. 'company_id': self.company_id.id,
  30. }
  31. @api.model
  32. def _get_send_picking_availability_by_sms_domain(self):
  33. return [
  34. ('state', '=', 'assigned'), # assigned = available
  35. ('availability_sent_by_sms', '=', False),
  36. ('picking_type_id.code', '=', 'outgoing'),
  37. ]
  38. @api.model
  39. def _cron_send_picking_availability_by_sms(self):
  40. domain = self._get_send_picking_availability_by_sms_domain()
  41. pickings = self.search(domain)
  42. total = len(pickings)
  43. for idx, picking in enumerate(pickings):
  44. _logger.debug('Send Sms for picking %s, progress %s/%s', picking,
  45. idx, total)
  46. vals = picking._prepare_availability_by_sms_notification()
  47. if not vals['mobile']:
  48. _logger.warning(
  49. _("SMS issue for picking %s : no mobile phone"
  50. % picking.id))
  51. continue
  52. self.env['sms.sms'].create(vals)
  53. picking.write({'availability_sent_by_sms': True})
  54. picking._cr.commit()