Skip to content

Commit

Permalink
[MIG] purchase_operating_unit: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidderen-nsi committed May 29, 2024
1 parent 89b1ddd commit 6f4850f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 69 deletions.
2 changes: 1 addition & 1 deletion purchase_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "Operating Unit in Purchase Orders",
"summary": "Adds the concecpt of operating unit (OU) in purchase order "
"management",
"version": "15.0.1.2.0",
"version": "17.0.1.0.0",
"author": "ForgeFlow, "
"Serpent Consulting Services Pvt. Ltd.,"
"Odoo Community Association (OCA)",
Expand Down
15 changes: 1 addition & 14 deletions purchase_operating_unit/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
# - Jordi Ballester Alomar
# Copyright 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import _, api, fields, models
from odoo import _, api, models
from odoo.exceptions import ValidationError


class AccountMove(models.Model):
_inherit = "account.move"

purchase_ou_domain = fields.Many2many(
comodel_name="purchase.order", compute="_compute_purchase_ou_domain"
)

# Load all unsold PO lines
@api.onchange("purchase_vendor_bill_id", "purchase_id")
def _onchange_purchase_auto_complete(self):
Expand All @@ -27,15 +23,6 @@ def _onchange_purchase_auto_complete(self):
self.operating_unit_id = purchase_id.operating_unit_id.id
return super()._onchange_purchase_auto_complete()

@api.depends("operating_unit_id")
def _compute_purchase_ou_domain(self):
for rec in self:
rec.purchase_ou_domain = (
self.env["purchase.order"]
.sudo()
.search([("operating_unit_id", "=", rec.operating_unit_id.id)])
)


class AccountMoveLine(models.Model):
_inherit = "account.move.line"
Expand Down
12 changes: 2 additions & 10 deletions purchase_operating_unit/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@
class PurchaseOrder(models.Model):
_inherit = "purchase.order"

READONLY_STATES = {
"purchase": [("readonly", True)],
"done": [("readonly", True)],
"cancel": [("readonly", True)],
}

operating_unit_id = fields.Many2one(
comodel_name="operating.unit",
string="Operating Unit",
states=READONLY_STATES,
default=lambda self: (
self.env["res.users"].operating_unit_default_get(self.env.uid)
self.env["res.users"]._get_default_operating_unit(self.env.uid)
),
)

requesting_operating_unit_id = fields.Many2one(
comodel_name="operating.unit",
string="Requesting Operating Unit",
states=READONLY_STATES,
default=lambda self: (
self.env["res.users"].operating_unit_default_get(self.env.uid)
self.env["res.users"]._get_default_operating_unit(self.env.uid)
),
)

Expand Down
83 changes: 43 additions & 40 deletions purchase_operating_unit/tests/test_purchase_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,56 @@


class TestPurchaseOperatingUnit(common.TransactionCase):
def setUp(self):
super().setUp()
self.ResUsers = self.env["res.users"]
self.PurchaseOrder = self.env["purchase.order"]
self.AccountInvoice = self.env["account.move"]
self.AccountAccount = self.env["account.account"]
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.ResUsers = cls.env["res.users"]
cls.PurchaseOrder = cls.env["purchase.order"]
cls.AccountInvoice = cls.env["account.move"]
cls.AccountAccount = cls.env["account.account"]
# company
self.company = self.env.ref("base.main_company")
cls.company = cls.env.ref("base.main_company")
# groups
self.group_purchase_user = self.env.ref("purchase.group_purchase_user")
cls.group_purchase_user = cls.env.ref("purchase.group_purchase_user")
# Main Operating Unit
self.ou1 = self.env.ref("operating_unit.main_operating_unit")
cls.ou1 = cls.env.ref("operating_unit.main_operating_unit")
# B2B Operating Unit
self.b2b = self.env.ref("operating_unit.b2b_operating_unit")
cls.b2b = cls.env.ref("operating_unit.b2b_operating_unit")
# Partner
self.partner1 = self.env.ref("base.res_partner_1")
cls.partner1 = cls.env.ref("base.res_partner_1")
# Products
self.product1 = self.env.ref("product.product_product_7")
self.product2 = self.env.ref("product.product_product_9")
self.product3 = self.env.ref("product.product_product_11")
cls.product1 = cls.env.ref("product.product_product_7")
cls.product2 = cls.env.ref("product.product_product_9")
cls.product3 = cls.env.ref("product.product_product_11")
# Account
payable_acc_type = self.env.ref("account.data_account_type_payable").id
self.account = self.AccountAccount.search(
[("user_type_id", "=", payable_acc_type)], limit=1
cls.account = cls.AccountAccount.search(
[("account_type", "=", "liability_payable")], limit=1
)
# Create users
self.user1_id = self._create_user(
cls.user1_id = cls._create_user(
"user_1",
[self.group_purchase_user],
self.company,
[self.ou1],
[cls.group_purchase_user],
cls.company,
[cls.ou1],
)
self.user2_id = self._create_user(
cls.user2_id = cls._create_user(
"user_2",
[self.group_purchase_user],
self.company,
[self.b2b],
[cls.group_purchase_user],
cls.company,
[cls.b2b],
)
self.purchase1 = self._create_purchase(
self.user1_id,
[(self.product1, 1000), (self.product2, 500), (self.product3, 800)],
cls.purchase1 = cls._create_purchase(
cls.user1_id,
[(cls.product1, 1000), (cls.product2, 500), (cls.product3, 800)],
)
self.purchase1.with_user(self.user1_id).button_confirm()
self.invoice = self._create_invoice(self.purchase1, self.partner1, self.account)
cls.purchase1.with_user(cls.user1_id).button_confirm()
cls.invoice = cls._create_invoice(cls.purchase1, cls.partner1, cls.account)

def _create_user(self, login, groups, company, operating_units):
@classmethod
def _create_user(cls, login, groups, company, operating_units):
"""Create a user."""
group_ids = [group.id for group in groups]
user = self.ResUsers.with_context(**{"no_reset_password": True}).create(
user = cls.ResUsers.with_context(**{"no_reset_password": True}).create(
{
"name": "Chicago Purchase User",
"login": login,
Expand All @@ -72,7 +73,8 @@ def _create_user(self, login, groups, company, operating_units):
)
return user.id

def _create_purchase(self, user_id, line_products):
@classmethod
def _create_purchase(cls, user_id, line_products):
"""Create a purchase order.
``line_products`` is a list of tuple [(product, qty)]
"""
Expand All @@ -87,18 +89,19 @@ def _create_purchase(self, user_id, line_products):
"date_planned": time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
}
lines.append((0, 0, line_values))
purchase = self.PurchaseOrder.with_user(user_id).create(
purchase = cls.PurchaseOrder.with_user(user_id).create(
{
"operating_unit_id": self.ou1.id,
"requesting_operating_unit_id": self.ou1.id,
"partner_id": self.partner1.id,
"operating_unit_id": cls.ou1.id,
"requesting_operating_unit_id": cls.ou1.id,
"partner_id": cls.partner1.id,
"order_line": lines,
"company_id": self.company.id,
"company_id": cls.company.id,
}
)
return purchase

def _create_invoice(self, purchase, partner, account):
@classmethod
def _create_invoice(cls, purchase, partner, account):
"""Create a vendor invoice for the purchase order."""
invoice_vals = {
"purchase_id": purchase.id,
Expand All @@ -111,7 +114,7 @@ def _create_invoice(self, purchase, partner, account):
"active_model": "purchase.order",
}
res = (
self.env["account.move"]
cls.env["account.move"]
.with_context(**purchase_context)
.create(invoice_vals)
)
Expand Down
7 changes: 3 additions & 4 deletions purchase_operating_unit/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
<field name="inherit_id" ref="purchase.view_move_form_inherit_purchase" />
<field name="arch" type="xml">
<xpath expr="//field[@name='purchase_id']" position="attributes">
<attribute name="domain">[('id', 'in', purchase_ou_domain)]</attribute>
</xpath>
<xpath expr="//field[@name='purchase_id']" position="after">
<field name="purchase_ou_domain" invisible="1" />
<attribute
name="domain"
>['|',('operating_unit_id', '=', operating_unit_id),('operating_unit_id', '=', False)]</attribute>
</xpath>
</field>
</record>
Expand Down
2 changes: 2 additions & 0 deletions purchase_operating_unit/views/purchase_order_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
<field
name="requesting_operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
readonly="state in ['cancel', 'done', 'purchase']"
/>
<field
name="operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
readonly="state in ['cancel', 'done', 'purchase']"
/>
</field>
<field name="order_line" position="attributes">
Expand Down

0 comments on commit 6f4850f

Please sign in to comment.