|
| 1 | +# Copyright 2024 Camptocamp SA |
| 2 | +# Copyright 2024 Michael Tietz (MT Software) <mtietz@mt-software.de> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) |
| 4 | + |
| 5 | +from odoo import SUPERUSER_ID, _, api, fields, models |
| 6 | +from odoo.exceptions import UserError |
| 7 | + |
| 8 | + |
| 9 | +class SameDeliveryLabelGenerated(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class PurchaseOrder(models.Model): |
| 14 | + _inherit = "purchase.order" |
| 15 | + |
| 16 | + vendor_label_carrier_id = fields.Many2one( |
| 17 | + "delivery.carrier", |
| 18 | + "Vendor Label Carrier", |
| 19 | + domain=[("purchase_label_picking_type", "!=", False)], |
| 20 | + ) |
| 21 | + delivery_label_picking_id = fields.Many2one( |
| 22 | + "stock.picking", |
| 23 | + "Delivery Label Picking", |
| 24 | + store=True, |
| 25 | + readonly=True, |
| 26 | + ) |
| 27 | + |
| 28 | + @api.model |
| 29 | + def _states_to_generate_delivery_label(self): |
| 30 | + """Labels will be (re)generated only if the PO is in one of these states.""" |
| 31 | + return ["draft", "sent"] |
| 32 | + |
| 33 | + @api.model |
| 34 | + def _mail_templates_to_not_attach_labels(self): |
| 35 | + return self.env.ref("purchase.email_template_edi_purchase_reminder").ids |
| 36 | + |
| 37 | + def action_rfq_send(self): |
| 38 | + self.ensure_one() |
| 39 | + self._generate_purchase_delivery_label() |
| 40 | + return super().action_rfq_send() |
| 41 | + |
| 42 | + def button_cancel(self): |
| 43 | + self._cancel_purchase_delivery_label_picking() |
| 44 | + return super().button_cancel() |
| 45 | + |
| 46 | + def _is_valid_for_vendor_labels(self): |
| 47 | + self.ensure_one() |
| 48 | + if self.state not in self._states_to_generate_delivery_label(): |
| 49 | + return False |
| 50 | + if not self.dest_address_id: |
| 51 | + return False |
| 52 | + if not any( |
| 53 | + product.type in ["product", "consu"] |
| 54 | + for product in self.order_line.product_id |
| 55 | + ): |
| 56 | + return False |
| 57 | + if not self.vendor_label_carrier_id.purchase_label_picking_type: |
| 58 | + return False |
| 59 | + return True |
| 60 | + |
| 61 | + def _are_delivery_label_equivalent(self, pick_1, pick_2): |
| 62 | + if len(pick_1.move_lines) != len(pick_2.move_lines): |
| 63 | + return False |
| 64 | + for move_old, move_new in zip(pick_1.move_lines, pick_2.move_lines): |
| 65 | + if ( |
| 66 | + move_old.product_id != move_new.product_id |
| 67 | + or move_old.product_uom_qty != move_new.product_uom_qty |
| 68 | + ): |
| 69 | + return False |
| 70 | + return True |
| 71 | + |
| 72 | + def _generate_purchase_delivery_label(self): |
| 73 | + """Create a transfer to generate the carrier labels.""" |
| 74 | + self.ensure_one() |
| 75 | + if not self._is_valid_for_vendor_labels(): |
| 76 | + return |
| 77 | + if not self.partner_id.property_stock_supplier.id: |
| 78 | + raise UserError( |
| 79 | + _( |
| 80 | + "You must set a Vendor Location for this partner %s", |
| 81 | + self.partner_id.name, |
| 82 | + ) |
| 83 | + ) |
| 84 | + carrier = self.vendor_label_carrier_id |
| 85 | + order = self.with_company(self.company_id) |
| 86 | + new_label_picking = None |
| 87 | + try: |
| 88 | + with self.env.cr.savepoint(): |
| 89 | + # Using a savepoint for generating the transfer for the label |
| 90 | + # and keep the new one only if it is different |
| 91 | + new_label_picking = order._create_purchase_delivery_label_picking( |
| 92 | + carrier |
| 93 | + ) |
| 94 | + if self._are_delivery_label_equivalent( |
| 95 | + new_label_picking, order.delivery_label_picking_id |
| 96 | + ): |
| 97 | + new_label_picking = None |
| 98 | + raise SameDeliveryLabelGenerated |
| 99 | + except SameDeliveryLabelGenerated: |
| 100 | + pass |
| 101 | + if not new_label_picking: |
| 102 | + return |
| 103 | + if order.delivery_label_picking_id: |
| 104 | + order._cancel_purchase_delivery_label_picking() |
| 105 | + order.delivery_label_picking_id = new_label_picking |
| 106 | + new_label_picking.message_post_with_view( |
| 107 | + "mail.message_origin_link", |
| 108 | + values={"self": new_label_picking, "origin": self}, |
| 109 | + subtype_id=self.env.ref("mail.mt_note").id, |
| 110 | + ) |
| 111 | + |
| 112 | + def _create_purchase_delivery_label_picking(self, carrier): |
| 113 | + self.ensure_one() |
| 114 | + values = self._get_purchase_delivery_label_picking_value(carrier) |
| 115 | + picking = self.env["stock.picking"].with_user(SUPERUSER_ID).create(values) |
| 116 | + moves = self.order_line._create_stock_moves(picking) |
| 117 | + moves.location_id = picking.location_id |
| 118 | + moves.location_dest_id = picking.location_dest_id |
| 119 | + # Remove the link on the sale and purchase |
| 120 | + # To not impact the delivered quantity on them |
| 121 | + picking.sale_id = False |
| 122 | + moves.sale_line_id = False |
| 123 | + moves.purchase_line_id = False |
| 124 | + picking.action_assign() |
| 125 | + for move in picking.move_lines: |
| 126 | + move.quantity_done = move.product_uom_qty |
| 127 | + picking._action_done() |
| 128 | + return picking |
| 129 | + |
| 130 | + def _cancel_purchase_delivery_label_picking(self): |
| 131 | + delivery_label_pickings = self.delivery_label_picking_id |
| 132 | + for picking in delivery_label_pickings: |
| 133 | + picking.cancel_shipment() |
| 134 | + # Using wirte to by pass internal checks, not a problem |
| 135 | + # because this is a fake move (Vendor to Vendor) |
| 136 | + delivery_label_pickings.move_line_ids.write({"state": "cancel"}) |
| 137 | + delivery_label_pickings.move_lines.write({"state": "cancel"}) |
| 138 | + delivery_label_pickings.write({"state": "cancel"}) |
| 139 | + |
| 140 | + def _get_purchase_delivery_label_picking_value(self, carrier): |
| 141 | + return { |
| 142 | + "picking_type_id": carrier.purchase_label_picking_type.id, |
| 143 | + "partner_id": self.dest_address_id.id, |
| 144 | + "user_id": False, |
| 145 | + "date": fields.Datetime.now(), |
| 146 | + "origin": " - ".join( |
| 147 | + [origin for origin in [self.origin, self.name] if origin] |
| 148 | + ), |
| 149 | + "location_dest_id": self.partner_id.property_stock_supplier.id, |
| 150 | + "location_id": self.partner_id.property_stock_supplier.id, |
| 151 | + "company_id": self.company_id.id, |
| 152 | + "carrier_id": carrier.id, |
| 153 | + "delivery_label_purchase_id": self.id, |
| 154 | + } |
0 commit comments