| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models, _ |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrder(models.Model): |
| 8 |
_inherit = 'sale.order' |
| 9 |
|
| 10 |
purchase_order_count = fields.Integer( |
| 11 |
"Number of Purchase Order Generated", |
| 12 |
compute='_compute_purchase_order_count', |
| 13 |
groups='purchase.group_purchase_user') |
| 14 |
|
| 15 |
@api.depends('order_line.purchase_line_ids.order_id') |
| 16 |
def _compute_purchase_order_count(self): |
| 17 |
for order in self: |
| 18 |
order.purchase_order_count = len(order._get_purchase_orders()) |
| 19 |
|
| 20 |
def _action_confirm(self): |
| 21 |
result = super(SaleOrder, self)._action_confirm() |
| 22 |
for order in self: |
| 23 |
order.order_line.sudo()._purchase_service_generation() |
| 24 |
return result |
| 25 |
|
| 26 |
def _action_cancel(self): |
| 27 |
result = super()._action_cancel() |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
self.sudo()._activity_cancel_on_purchase() |
| 32 |
return result |
| 33 |
|
| 34 |
def action_view_purchase_orders(self): |
| 35 |
self.ensure_one() |
| 36 |
purchase_order_ids = self._get_purchase_orders().ids |
| 37 |
action = { |
| 38 |
'res_model': 'purchase.order', |
| 39 |
'type': 'ir.actions.act_window', |
| 40 |
} |
| 41 |
if len(purchase_order_ids) == 1: |
| 42 |
action.update({ |
| 43 |
'view_mode': 'form', |
| 44 |
'res_id': purchase_order_ids[0], |
| 45 |
}) |
| 46 |
else: |
| 47 |
action.update({ |
| 48 |
'name': _("Purchase Order generated from %s", self.name), |
| 49 |
'domain': [('id', 'in', purchase_order_ids)], |
| 50 |
'view_mode': 'list,form', |
| 51 |
}) |
| 52 |
return action |
| 53 |
|
| 54 |
def _get_purchase_orders(self): |
| 55 |
return self.order_line.purchase_line_ids.order_id |
| 56 |
|
| 57 |
def _activity_cancel_on_purchase(self): |
| 58 |
""" If some SO are cancelled, we need to put an activity on their generated purchase. If sale lines of |
| 59 |
different sale orders impact different purchase, we only want one activity to be attached. |
| 60 |
""" |
| 61 |
purchase_to_notify_map = {} |
| 62 |
|
| 63 |
purchase_order_lines = self.env['purchase.order.line'].search([ |
| 64 |
('sale_line_id', 'in', self.mapped('order_line').ids), |
| 65 |
('state', '!=', 'cancel'), |
| 66 |
('product_id.service_to_purchase', '=', True), |
| 67 |
]) |
| 68 |
for purchase_line in purchase_order_lines: |
| 69 |
purchase_to_notify_map.setdefault(purchase_line.order_id, self.env['sale.order.line']) |
| 70 |
purchase_to_notify_map[purchase_line.order_id] |= purchase_line.sale_line_id |
| 71 |
|
| 72 |
for purchase_order, sale_order_lines in purchase_to_notify_map.items(): |
| 73 |
purchase_order._activity_schedule_with_view('mail.mail_activity_data_warning', |
| 74 |
user_id=purchase_order.user_id.id or self.env.uid, |
| 75 |
views_or_xmlid='sale_purchase.exception_purchase_on_sale_cancellation', |
| 76 |
render_context={ |
| 77 |
'sale_orders': sale_order_lines.mapped('order_id'), |
| 78 |
'sale_order_lines': sale_order_lines, |
| 79 |
}) |