Skip to content

Commit

Permalink
[FIX] l10n_it_delivery_note: carrier syncing
Browse files Browse the repository at this point in the history
- Set the delivery note carrier as a 'delivery carrier' instead of a
  partner
- Sync the carrier between the picking and the delivery note
- All the pickings carriers inside a DN must be the same
  • Loading branch information
renda-dev committed Jan 11, 2024
1 parent 3b88d0a commit eb0c9d9
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 14 deletions.
2 changes: 1 addition & 1 deletion l10n_it_delivery_note/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ITA - Documento di trasporto
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:48fb8a0d085dce618e7696ef8bdb30aa56d61f48d3db5f895cf477ddf0d3505c
!! source digest: sha256:03411c18bec6b352c5e29adf11136169bc5b144b8e7e55898358671fb8c9d0cf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
2 changes: 1 addition & 1 deletion l10n_it_delivery_note/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Marco Calcagni, Gianmarco Conte, Link IT Europe Srl, "
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/l10n-italy",
"version": "14.0.2.5.0",
"version": "14.0.2.6.0",
"category": "Localization/Italy",
"license": "AGPL-3",
"maintainers": ["MarcoCalcagni", "aleuffre", "renda-dev"],
Expand Down
4 changes: 2 additions & 2 deletions l10n_it_delivery_note/cli/migrate_l10n_it_ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging

from odoo import _
from odoo import _, fields
from odoo.exceptions import UserError, ValidationError

from .core import EasyCommand
Expand Down Expand Up @@ -287,7 +287,7 @@ def vals_getter(record):
"partner_shipping_id": record.partner_shipping_id.id,
"type_id": self._document_types[record.ddt_type_id].id,
"date": record.date,
"carrier_id": record.carrier_id.id,
"carrier_id": fields.first(record.picking_ids).carrier_id.id,
"delivery_method_id": record.partner_id.property_delivery_carrier_id.id,
"transport_datetime": record.date_done,
"packages": record.parcels,
Expand Down
11 changes: 11 additions & 0 deletions l10n_it_delivery_note/migrations/14.0.2.6.0/post-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
# Sync every picking's carrier to their delivery_note
picking_ids = env["stock.picking"].search(
[("delivery_note_id", "!=", False), ("carrier_id", "!=", False)]
)
for picking in picking_ids:
picking.delivery_note_id.carrier_id = picking.carrier_id
75 changes: 75 additions & 0 deletions l10n_it_delivery_note/migrations/14.0.2.6.0/pre-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from collections import defaultdict

from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
# Get every carrier that was a 'res.partner' and create a 'delivery.carrier'
env.cr.execute(
"""SELECT id, carrier_id
FROM stock_delivery_note
WHERE carrier_id IS NOT NULL"""
)
res = env.cr.dictfetchall()

# Group by carrier_id to not create duplicates
new_res = defaultdict(list)
for i in res:
new_res[i.get("carrier_id")].append(i.get("id"))

for (carrier_id, delivery_note_ids) in new_res.items():
partner_carrier_id = env["res.partner"].browse(carrier_id)

carrier_product = env["product.product"].create(
{
"name": partner_carrier_id.name,
"type": "service",
"sale_ok": False,
"purchase_ok": False,
"list_price": 0.0,
"invoice_policy": "order",
}
)

delivery_carrier_id = env["delivery.carrier"].create(
{
"name": partner_carrier_id.name,
"company_id": partner_carrier_id.company_id.id,
"country_ids": [(4, partner_carrier_id.country_id.id)]
if partner_carrier_id.country_id
else [],
"state_ids": [(4, partner_carrier_id.state_id.id)]
if partner_carrier_id.state_id
else [],
"zip_from": partner_carrier_id.zip,
"zip_to": partner_carrier_id.zip,
"product_id": carrier_product.id,
}
)

# If it's only one add a duplicate to
# delivery note ids otherwise it would
# be casted to tuple as '(id,)' and that will raise an error
if len(delivery_note_ids) == 1:
delivery_note_ids.append(delivery_note_ids[0])

# Update carrier in 'stock.picking'
openupgrade.logged_query(
env.cr,
"""UPDATE stock_picking
SET carrier_id={}
WHERE delivery_note_id IN {}""".format(
delivery_carrier_id.id, tuple(delivery_note_ids)
),
)

# Remove carrier from delivery notes
openupgrade.logged_query(
env.cr,
"""UPDATE stock_delivery_note
SET carrier_id=NULL
WHERE id IN {}""".format(
tuple(delivery_note_ids)
),
)
9 changes: 9 additions & 0 deletions l10n_it_delivery_note/mixins/picking_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def _check_pickings_partners(self, pickings):
_("You need to select pickings with all the same recipient.")
)

@api.model
def _check_pickings_carriers(self, pickings):
carrier_ids = pickings.mapped("carrier_id")
if len(carrier_ids) > 1:
raise ValidationError(

Check warning on line 81 in l10n_it_delivery_note/mixins/picking_checker.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_delivery_note/mixins/picking_checker.py#L81

Added line #L81 was not covered by tests
_("You need to select pickings with all the same carriers.")
)

@api.model
def _check_pickings_src_locations(self, pickings):
src_locations = pickings.mapped("location_id")
Expand Down Expand Up @@ -130,5 +138,6 @@ def check_compliance(self, pickings):
self._check_pickings_state(pickings)
self._check_pickings_types(pickings)
self._check_pickings_partners(pickings)
self._check_pickings_carriers(pickings)
self._check_pickings_src_locations(pickings)
self._check_pickings_dest_locations(pickings)
1 change: 0 additions & 1 deletion l10n_it_delivery_note/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ class ResPartner(models.Model):
default_transport_method_id = fields.Many2one(
"stock.picking.transport.method", string="Method of transport"
)
is_carrier = fields.Boolean("Is Carrier")
2 changes: 1 addition & 1 deletion l10n_it_delivery_note/models/stock_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _domain_weight_uom(self):
)

carrier_id = fields.Many2one(
"res.partner",
"delivery.carrier",
string="Carrier",
states=DONE_READONLY_STATE,
tracking=True,
Expand Down
3 changes: 2 additions & 1 deletion l10n_it_delivery_note/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class StockPicking(models.Model):
)

delivery_note_carrier_id = fields.Many2one(
"res.partner", string="DN Carrier", related="delivery_note_id.carrier_id"
"delivery.carrier", string="DN Carrier", related="delivery_note_id.carrier_id"
)
delivery_method_id = fields.Many2one(
"delivery.carrier", related="delivery_note_id.delivery_method_id"
Expand Down Expand Up @@ -346,6 +346,7 @@ def _create_delivery_note(self):
"partner_shipping_id": partners[1].id,
"type_id": type_id.id,
"date": self.date_done,
"carrier_id": self.carrier_id.id,
"delivery_method_id": self.partner_id.property_delivery_carrier_id.id,
"transport_condition_id": (
self.sale_id.default_transport_condition_id.id
Expand Down
3 changes: 2 additions & 1 deletion l10n_it_delivery_note/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">ITA - Documento di trasporto</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:48fb8a0d085dce618e7696ef8bdb30aa56d61f48d3db5f895cf477ddf0d3505c
!! source digest: sha256:03411c18bec6b352c5e29adf11136169bc5b144b8e7e55898358671fb8c9d0cf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-italy/tree/14.0/l10n_it_delivery_note"><img alt="OCA/l10n-italy" src="https://img.shields.io/badge/github-OCA%2Fl10n--italy-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-italy-14-0/l10n-italy-14-0-l10n_it_delivery_note"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-italy&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><strong>English</strong></p>
Expand Down
1 change: 0 additions & 1 deletion l10n_it_delivery_note/views/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<field name="arch" type="xml">
<xpath expr="//page[@name='sales_purchases']/group" position="inside">
<group string="Shipping information">
<field name="is_carrier" />
<field name="default_goods_appearance_id" widget="selection" />
<field name="default_transport_condition_id" widget="selection" />
<field name="default_transport_reason_id" widget="selection" />
Expand Down
5 changes: 1 addition & 4 deletions l10n_it_delivery_note/views/stock_delivery_note.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@
</group>
<group>
<group>
<field
name="carrier_id"
domain="[('is_carrier','=', True)]"
/>
<field name="carrier_id" />
<field name="delivery_method_id" />
</group>
<group>
Expand Down
1 change: 0 additions & 1 deletion l10n_it_delivery_note/views/stock_picking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
<field
name="delivery_note_carrier_id"
attrs="{'readonly': [('delivery_note_readonly', '=', True)]}"
domain="[('is_carrier','=', True)]"
string="Carrier"
/>
<field
Expand Down
1 change: 1 addition & 0 deletions l10n_it_delivery_note/wizard/delivery_note_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _get_validation_errors(self, pickings):
(self._check_pickings_state, False),
(self._check_pickings_types, False),
(self._check_pickings_partners, False),
(self._check_pickings_carriers, False),
(self._check_pickings_src_locations, False),
(self._check_pickings_dest_locations, False),
(self._check_delivery_notes, False),
Expand Down
1 change: 1 addition & 0 deletions l10n_it_delivery_note/wizard/delivery_note_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def confirm(self):
"partner_shipping_id": self.partner_shipping_id.id,
"type_id": self.type_id.id,
"date": self.date,
"carrier_id": self.selected_picking_ids.carrier_id.id,
"delivery_method_id": self.partner_id.property_delivery_carrier_id.id,
"transport_condition_id": sale_order_id
and sale_order_id.default_transport_condition_id.id
Expand Down

0 comments on commit eb0c9d9

Please sign in to comment.