From 5aefb2fe58dfa52a711efacba626b164a452faad Mon Sep 17 00:00:00 2001 From: Yoshi Tashiro Date: Sat, 10 Feb 2024 00:30:17 +0800 Subject: [PATCH] improve code --- .../__manifest__.py | 6 +- .../models/__init__.py | 1 + .../models/carrier_info_mixin.py | 37 +++++++++ .../models/sale_order.py | 75 ++++++++++--------- .../models/stock_picking.py | 32 ++++---- .../views/sale_order_views.xml | 5 +- .../views/stock_picking_views.xml | 4 +- 7 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 stock_outgoing_shipment_report/models/carrier_info_mixin.py diff --git a/stock_outgoing_shipment_report/__manifest__.py b/stock_outgoing_shipment_report/__manifest__.py index 781cb08..3592344 100644 --- a/stock_outgoing_shipment_report/__manifest__.py +++ b/stock_outgoing_shipment_report/__manifest__.py @@ -1,9 +1,9 @@ -# Copyright 2019-2021 Quartile Limited -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# Copyright 2019-2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { "name": "Stock Outgoing Shipment Report", "summary": "", - "version": "12.0.1.1.1", + "version": "12.0.1.2.1", "category": "Stock", "website": "https://www.quartile.co", "author": "Quartile Limited", diff --git a/stock_outgoing_shipment_report/models/__init__.py b/stock_outgoing_shipment_report/models/__init__.py index 3e58fb5..1f3731a 100644 --- a/stock_outgoing_shipment_report/models/__init__.py +++ b/stock_outgoing_shipment_report/models/__init__.py @@ -1,3 +1,4 @@ +from . import carrier_info_mixin from . import delivery_carrier_account from . import delivery_carrier_service from . import delivery_carrier diff --git a/stock_outgoing_shipment_report/models/carrier_info_mixin.py b/stock_outgoing_shipment_report/models/carrier_info_mixin.py new file mode 100644 index 0000000..fd0cb4c --- /dev/null +++ b/stock_outgoing_shipment_report/models/carrier_info_mixin.py @@ -0,0 +1,37 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class CarrierInfoMixin(models.AbstractModel): + """This model should only be inherited by a model with `carrier_id` field.""" + _name = "carrier.info.mixin" + _description = "Carrier Information Mixin" + + shipping_insurance_amt = fields.Float( + "Shipping Insurance Amount", help="For information only." + ) + delivery_carrier_service_id = fields.Many2one( + "delivery.carrier.service", + string="Delivery Service", + domain="[('carrier_id', '=', carrier_id)]", + ) + shipping_use_carrier_acct = fields.Char("Delivery Carrier Account Number") + + @api.multi + def _get_partner_shipping(self): + raise NotImplementedError + + @api.onchange("carrier_id") + def _onchange_carrier_id(self): + self.delivery_carrier_service_id = False + partner = self._get_partner_shipping() + account_ids = partner.delivery_carrier_account_ids + carrier_acct = account_ids.filtered( + lambda l: l.carrier_id == self.carrier_id + )[:1] + if carrier_acct: + self.shipping_use_carrier_acct = carrier_acct.delivery_carrier_account_num + else: + self.shipping_use_carrier_acct = False diff --git a/stock_outgoing_shipment_report/models/sale_order.py b/stock_outgoing_shipment_report/models/sale_order.py index 38ed73c..cfc7244 100644 --- a/stock_outgoing_shipment_report/models/sale_order.py +++ b/stock_outgoing_shipment_report/models/sale_order.py @@ -1,51 +1,54 @@ -# Copyright 2019 Quartile Limited -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# Copyright 2019-2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import api, models class SaleOrder(models.Model): - _inherit = "sale.order" + _name = "sale.order" + _inherit = ["sale.order", "carrier.info.mixin"] - shipping_insurance_amt = fields.Float( - "Shipping Insurance Amount", help="For information only." - ) - delivery_carrier_service_id = fields.Many2one( - "delivery.carrier.service", string="Delivery Service", - ) - shipping_use_carrier_acct = fields.Char(string="Delivery Carrier Account Number",) + @api.multi + def _get_partner_shipping(self): + self.ensure_one() + return self.partner_shipping_id - @api.onchange("carrier_id") - def _onchange_carrier_id(self): - account_ids = self.partner_shipping_id.delivery_carrier_account_ids - if self.carrier_id and account_ids.filtered( - lambda l: l.carrier_id == self.carrier_id - ): - self.shipping_use_carrier_acct = account_ids.filtered( - lambda l: l.carrier_id == self.carrier_id - ).delivery_carrier_account_num - else: - self.shipping_use_carrier_acct = False + @api.onchange("partner_shipping_id") + def _onchange_partner_shipping_id(self): + self._onchange_carrier_id() + return super()._onchange_partner_shipping_id() @api.multi def action_confirm(self): - res = super(SaleOrder, self).action_confirm() + res = super().action_confirm() for order in self: - for pick in order.picking_ids: - pick.write({ - 'carrier_id': order.carrier_id.id, - 'delivery_carrier_service_id': order.delivery_carrier_service_id.id, - 'shipping_use_carrier_acct': order.shipping_use_carrier_acct, - }) + order.picking_ids.write( + { + "carrier_id": order.carrier_id.id, + "delivery_carrier_service_id": order.delivery_carrier_service_id.id, + "shipping_use_carrier_acct": order.shipping_use_carrier_acct, + } + ) return res @api.multi def write(self, vals): - res = super(SaleOrder, self).write(vals) - if "carrier_id" in vals: - for order in self: - pickings = order.mapped("picking_ids").filtered( - lambda p: p.state not in ("done", "cancel") - ) - pickings.update({"carrier_id": vals["carrier_id"]}) + res = super().write(vals) + if not any( + key in vals for key in [ + "carrier_id", "delivery_carrier_service_id", "shipping_use_carrier_acct" + ] + ): + return res + for order in self: + pickings = order.mapped("picking_ids").filtered( + lambda p: p.state not in ("done", "cancel") + ) + pickings.write( + { + "carrier_id": order.carrier_id.id, + "delivery_carrier_service_id": order.delivery_carrier_service_id.id, + "shipping_use_carrier_acct": order.shipping_use_carrier_acct, + } + ) return res diff --git a/stock_outgoing_shipment_report/models/stock_picking.py b/stock_outgoing_shipment_report/models/stock_picking.py index 0404e4e..4b49f05 100644 --- a/stock_outgoing_shipment_report/models/stock_picking.py +++ b/stock_outgoing_shipment_report/models/stock_picking.py @@ -1,16 +1,22 @@ -# Copyright 2019-2021 Quartile Limited -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# Copyright 2019-2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import api, fields, models class StockPicking(models.Model): - _inherit = "stock.picking" + _name = "stock.picking" + _inherit = ["stock.picking", "carrier.info.mixin"] - delivery_carrier_service_id = fields.Many2one( - "delivery.carrier.service", string="Delivery Service", - ) - shipping_use_carrier_acct = fields.Char(string="Delivery Carrier Account Number",) + @api.multi + def _get_partner_shipping(self): + self.ensure_one() + return self.partner_id + + @api.onchange("partner_id") + def onchange_partner_id(self): + self._onchange_carrier_id() + return super().onchange_partner_id() @api.multi def generate_stock_outgoing_shipment_report(self): @@ -21,12 +27,8 @@ def generate_stock_outgoing_shipment_report(self): partner = move.picking_partner_id product = move.product_id vals = {"move_id": move.id} - carrier = picking.carrier_id - vals["carrier_id"] = carrier.id if carrier else False - vals["ship_service_id"] = ( - picking.delivery_carrier_service_id - and picking.delivery_carrier_service_id.id - ) + vals["carrier_id"] = picking.carrier_id.id or False + vals["ship_service_id"] = picking.delivery_carrier_service_id.id or False vals["ship_account"] = picking.shipping_use_carrier_acct vals["ship_date_edit"] = fields.Datetime.context_timestamp( self, picking.scheduled_date @@ -43,9 +45,7 @@ def generate_stock_outgoing_shipment_report(self): vals["ship_to_country"] = partner.country_id and partner.country_id.code vals["ship_to_zip"] = partner.zip vals["ship_to_phone"] = partner.phone - vals["order_notes"] = ( - product.product_tmpl_id.delivery_report_desc or product.name[:40] - ) + vals["order_notes"] = product.delivery_report_desc or product.name[:40] self.env["stock.outgoing.shipment.report"].create(vals) return self.env.ref( "stock_outgoing_shipment_report.action_stock_outgoing_shipment_report" diff --git a/stock_outgoing_shipment_report/views/sale_order_views.xml b/stock_outgoing_shipment_report/views/sale_order_views.xml index 79cf935..b3d1402 100644 --- a/stock_outgoing_shipment_report/views/sale_order_views.xml +++ b/stock_outgoing_shipment_report/views/sale_order_views.xml @@ -9,10 +9,7 @@ - + diff --git a/stock_outgoing_shipment_report/views/stock_picking_views.xml b/stock_outgoing_shipment_report/views/stock_picking_views.xml index a7846e2..e302398 100644 --- a/stock_outgoing_shipment_report/views/stock_picking_views.xml +++ b/stock_outgoing_shipment_report/views/stock_picking_views.xml @@ -6,8 +6,8 @@ - - + +