Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
yostashiro committed Feb 9, 2024
1 parent 23df894 commit 5aefb2f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 61 deletions.
6 changes: 3 additions & 3 deletions stock_outgoing_shipment_report/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions stock_outgoing_shipment_report/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import carrier_info_mixin
from . import delivery_carrier_account
from . import delivery_carrier_service
from . import delivery_carrier
Expand Down
37 changes: 37 additions & 0 deletions stock_outgoing_shipment_report/models/carrier_info_mixin.py
Original file line number Diff line number Diff line change
@@ -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
75 changes: 39 additions & 36 deletions stock_outgoing_shipment_report/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 16 additions & 16 deletions stock_outgoing_shipment_report/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -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"
Expand Down
5 changes: 1 addition & 4 deletions stock_outgoing_shipment_report/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
<attribute name="attrs" />
</xpath>
<xpath expr="//div[@name='carrier_selection']" position="after">
<field
name="delivery_carrier_service_id"
domain="[('carrier_id', '=', carrier_id)]"
/>
<field name="delivery_carrier_service_id" />
<field name="shipping_use_carrier_acct" />
<field name="shipping_insurance_amt" />
</xpath>
Expand Down
4 changes: 2 additions & 2 deletions stock_outgoing_shipment_report/views/stock_picking_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='carrier_id']" position="after">
<field name="delivery_carrier_service_id"/>
<field name="shipping_use_carrier_acct"/>
<field name="delivery_carrier_service_id" attrs="{'readonly':[('state','in',('done','cancel'))]}" />
<field name="shipping_use_carrier_acct" attrs="{'readonly':[('state','in',('done','cancel'))]}" />
</xpath>
</field>
</record>
Expand Down

0 comments on commit 5aefb2f

Please sign in to comment.