sale_crm.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion (http://www.akretion.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. # @author Alexis de Lattre <alexis.delattre@akretion.com>
  5. from openerp import models, fields, api, _, workflow
  6. from openerp.exceptions import Warning as UserError
  7. class SaleOrder(models.Model):
  8. _inherit = 'sale.order'
  9. lead_id = fields.Many2one(
  10. 'crm.lead', string='Opportunity')
  11. @api.multi
  12. def action_button_confirm(self):
  13. res = super(SaleOrder, self).action_button_confirm()
  14. won_stage = self.env.ref('crm.stage_lead6')
  15. for order in self:
  16. if order.lead_id:
  17. order.lead_id.stage_id = won_stage
  18. order.lead_id.message_post(_(
  19. "Stage automatically updated to <i>Won</i> upon "
  20. "confirmation of the quotation <b>%s</b>" % order.name))
  21. return res
  22. class CrmLead(models.Model):
  23. _inherit = 'crm.lead'
  24. sale_ids = fields.One2many(
  25. 'sale.order', 'lead_id', string='Quotations', readonly=True)
  26. @api.multi
  27. def view_sale_orders(self):
  28. self.ensure_one()
  29. if self.sale_ids:
  30. action = {
  31. 'name': _('Quotations'),
  32. 'type': 'ir.actions.act_window',
  33. 'res_model': 'sale.order',
  34. 'target': 'current',
  35. 'context':
  36. "{'default_partner_id': %s, 'default_lead_id': %s}" % (
  37. self.partner_id.id or False, self[0].id),
  38. }
  39. if len(self.sale_ids) == 1:
  40. action.update({
  41. 'view_mode': 'form,tree,calendar,graph',
  42. 'res_id': self.sale_ids[0].id,
  43. })
  44. else:
  45. action.update({
  46. 'view_mode': 'tree,form,calendar,graph',
  47. 'domain': "[('id', 'in', %s)]" % self.sale_ids.ids,
  48. })
  49. return action
  50. else:
  51. raise UserError(_(
  52. 'There are no quotations linked to this opportunity'))
  53. @api.model
  54. def create(self, vals):
  55. if vals is None:
  56. vals = {}
  57. if self._context.get('usability_default_stage_xmlid'):
  58. stage = self.env.ref(self._context['usability_default_stage_xmlid'])
  59. vals['stage_id'] = stage.id
  60. return super(CrmLead, self).create(vals)
  61. @api.multi
  62. def case_mark_lost(self):
  63. """When opportunity is marked as lost, cancel the related quotations
  64. I don't inherit the write but the button, because it leaves a waty to
  65. mask lead as lost and not cancel the quotations
  66. """
  67. res = super(CrmLead, self).case_mark_lost()
  68. sales = self.env['sale.order'].search([
  69. ('lead_id', 'in', self.ids),
  70. ('state', 'in', ('draft', 'sent'))])
  71. for so in sales:
  72. workflow.trg_validate(
  73. self._uid, 'sale.order', so.id, 'cancel', self._cr)
  74. so.message_post(_(
  75. 'The related opportunity has been marked as lost, '
  76. 'therefore this quotation has been automatically cancelled.'))
  77. return res