Skip to content

Commit

Permalink
[14.0][IMP] purchase_sale_inter_company: the sale order is updated wh…
Browse files Browse the repository at this point in the history
…en the purchase order is modified
  • Loading branch information
chafique-delli committed Jan 24, 2023
1 parent e84dcc0 commit 96c0a54
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
10 changes: 8 additions & 2 deletions purchase_sale_inter_company/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"author": "Odoo SA, Akretion, Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": ["sale", "purchase", "stock", "account_invoice_inter_company"],
"data": ["views/res_config_view.xml"],
"depends": [
"sale",
"purchase",
"stock",
"account_invoice_inter_company",
"base_view_inheritance_extension",
],
"data": ["views/res_config_view.xml", "views/purchase_view.xml"],
}
50 changes: 39 additions & 11 deletions purchase_sale_inter_company/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
# Copyright 2018-2019 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

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


class PurchaseOrder(models.Model):
_inherit = "purchase.order"

intercompany_sale_id = fields.Many2one(
comodel_name="sale.order",
string="Destination Sale Order",
readonly=True,
copy=False,
)

def button_approve(self, force=False):
"""Generate inter company sale order base on conditions."""
res = super().button_approve(force)
Expand Down Expand Up @@ -81,16 +88,28 @@ def _inter_company_create_sale_order(self, dest_company):
"purchase price list currency."
)
)
# if a sale order has already been generated
# delete it and force the same number
force_number = False
if self.intercompany_sale_id and self.intercompany_sale_id.state in [
"draft",
"cancel",
]:
force_number = self.intercompany_sale_id.name
self.intercompany_sale_id.with_context(force_delete=True).unlink()
# create the SO and generate its lines from the PO lines
sale_order_data = self._prepare_sale_order_data(
self.name, company_partner, dest_company, self.dest_address_id
)
if force_number:
sale_order_data["name"] = force_number
sale_order = (
self.env["sale.order"]
.with_user(intercompany_user.id)
.sudo()
.create(sale_order_data)
)
self.write({"intercompany_sale_id": sale_order.id})
for purchase_line in self.order_line:
sale_line_data = self._prepare_sale_order_line_data(
purchase_line, dest_company, sale_order
Expand Down Expand Up @@ -175,14 +194,23 @@ def _prepare_sale_order_line_data(self, purchase_line, dest_company, sale_order)
return new_line._convert_to_write(new_line._cache)

def button_cancel(self):
sale_orders = (
self.env["sale.order"]
.sudo()
.search([("auto_purchase_order_id", "in", self.ids)])
)
for so in sale_orders:
if so.state not in ["draft", "sent", "cancel"]:
raise UserError(_("You can't cancel an order that is %s") % so.state)
sale_orders.action_cancel()
self.write({"partner_ref": False})
if self.intercompany_sale_id:
if self.intercompany_sale_id.state not in ["draft", "sent", "cancel"]:
raise UserError(
_(
"You can not cancel your purchase order %s.\n"
"The sale order %s at your supplier %s that is linked "
"to your purchase order, is in the state %s.\n"
"In order to cancel your purchase order, you must first "
"ask your supplier to cancel his sale order."
)
% (
self.name,
self.intercompany_sale_id.name,
self.partner_id.name,
self.intercompany_sale_id.state,
)
)
self.intercompany_sale_id.sudo().action_cancel()
self.write({"partner_ref": False})
return super().button_cancel()
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,15 @@ def test_po_with_contact_as_partner(self):
self.assertEqual(len(sale), 1)
self.assertEqual(sale.state, "sale")
self.assertEqual(sale.partner_id, self.partner_company_a)

def test_update_po(self):
self.company_b.sale_auto_validation = False
old_sale = self._approve_po()
old_sale_name = old_sale.name
self.purchase_company_a.button_cancel()
self.purchase_company_a.order_line.product_qty = 5.0
self.company_b.sale_auto_validation = True
new_sale = self._approve_po()
self.assertEqual(new_sale.order_line.product_uom_qty, 5.0)
self.assertEqual(new_sale.state, "sale")
self.assertEqual(new_sale.name, old_sale_name)
17 changes: 17 additions & 0 deletions purchase_sale_inter_company/views/purchase_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="purchase_order_form" model="ir.ui.view">
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form" />
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="intercompany_sale_id" invisible="True" />
</field>
<field name="order_line" position="attributes">
<attribute name="attrs" operation="python_dict" key="readonly">
['|', ('state', 'in', ('done', 'cancel')), ('intercompany_sale_id', '!=', False), ('state', '=', 'purchase')]
</attribute>
</field>
</field>
</record>
</odoo>

0 comments on commit 96c0a54

Please sign in to comment.