Skip to content

Commit

Permalink
[IMP] purchase_operating_unit: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-nstuder authored and BT-aleonard committed May 11, 2023
1 parent aeb13ef commit 78c8444
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 221 deletions.
10 changes: 4 additions & 6 deletions purchase_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
{
"name": "Operating Unit in Purchase Orders",
"summary": "Adds the concecpt of operating unit (OU) in purchase order "
"management",
"management",
"version": "12.0.1.0.0",
"author": "Eficent, "
"Serpent Consulting Services Pvt. Ltd.,"
"Odoo Community Association (OCA)",
"Serpent Consulting Services Pvt. Ltd.,"
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/operating-unit",
"category": "Purchase Management",
"depends": ["stock_operating_unit", "purchase_stock"],
Expand All @@ -20,8 +20,6 @@
"views/purchase_order_view.xml",
"views/purchase_order_line_view.xml",
],
"demo": [
"demo/purchase_order_demo.xml",
],
"demo": ["demo/purchase_order_demo.xml"],
"installable": True,
}
24 changes: 8 additions & 16 deletions purchase_operating_unit/demo/purchase_order_demo.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--© 2015-17 Eficent Business and IT Consulting Services S.L.-->
<!-- - Jordi Ballester Alomar-->
<!--© 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya-->
<!--License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).-->
<odoo>

<record id="purchase.purchase_order_1" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_2" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_3" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_4" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_5" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_6" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

<record id="purchase.purchase_order_7" model="purchase.order">
<field name="operating_unit_id" ref="operating_unit.main_operating_unit"/>
<field name="operating_unit_id" ref="operating_unit.main_operating_unit" />
</record>

</odoo>
31 changes: 18 additions & 13 deletions purchase_operating_unit/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


class AccountInvoice(models.Model):
_inherit = 'account.invoice'
_inherit = "account.invoice"

# Load all unsold PO lines
@api.onchange('purchase_id')
@api.onchange("purchase_id")
def purchase_order_change(self):
"""
Override to add Operating Unit from Purchase Order to Invoice.
Expand All @@ -19,27 +19,32 @@ def purchase_order_change(self):
self.operating_unit_id = self.purchase_id.operating_unit_id.id
return super(AccountInvoice, self).purchase_order_change()

@api.onchange('operating_unit_id')
@api.onchange("operating_unit_id")
def _onchange_allowed_purchase_ids(self):
'''
"""
Show only the purchase orders that have the same operating unit
'''
"""
result = super(AccountInvoice, self)._onchange_allowed_purchase_ids()

result['domain']['purchase_id'] += [('operating_unit_id', '=',
self.operating_unit_id.id)]
result["domain"]["purchase_id"] += [
("operating_unit_id", "=", self.operating_unit_id.id)
]
return result


class AccountInvoiceLines(models.Model):
_inherit = 'account.invoice.line'
_inherit = "account.invoice.line"

@api.constrains('operating_unit_id', 'purchase_line_id')
@api.constrains("operating_unit_id", "purchase_line_id")
def _check_invoice_ou(self):
for line in self:
if (line.purchase_line_id and line.operating_unit_id !=
line.purchase_line_id.operating_unit_id):
if (
line.purchase_line_id
and line.operating_unit_id != line.purchase_line_id.operating_unit_id
):
raise exceptions.ValidationError(
_('The operating unit of the purchase order must '
'be the same as in the associated invoices.')
_(
"The operating unit of the purchase order must "
"be the same as in the associated invoices."
)
)
116 changes: 68 additions & 48 deletions purchase_operating_unit/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,130 @@
# © 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.exceptions import ValidationError, UserError
from odoo.exceptions import UserError, ValidationError


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

@api.model
def _default_picking_type(self):
res = super(PurchaseOrder, self)._default_picking_type()
type_obj = self.env['stock.picking.type']
operating_unit = self.env['res.users'].operating_unit_default_get(
self.env.uid
type_obj = self.env["stock.picking.type"]
operating_unit = self.env["res.users"].operating_unit_default_get(self.env.uid)
types = type_obj.search(
[
("code", "=", "incoming"),
("warehouse_id.operating_unit_id", "=", operating_unit.id),
]
)
types = type_obj.search([('code', '=', 'incoming'),
('warehouse_id.operating_unit_id', '=',
operating_unit.id)])
if types:
res = types[:1].id
return res

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

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

requesting_operating_unit_id = fields.Many2one(
comodel_name='operating.unit',
string='Requesting Operating Unit',
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))
default=lambda self: (
self.env["res.users"].operating_unit_default_get(self.env.uid)
),
)

picking_type_id = fields.Many2one(
comodel_name='stock.picking.type',
string='Deliver To',
comodel_name="stock.picking.type",
string="Deliver To",
help="This will determine picking type of incoming shipment",
required=True,
states={'confirmed': [('readonly', True)],
'approved': [('readonly', True)],
'done': [('readonly', True)]},
states={
"confirmed": [("readonly", True)],
"approved": [("readonly", True)],
"done": [("readonly", True)],
},
default=lambda self: self._default_picking_type(),
)

@api.constrains('operating_unit_id', 'picking_type_id')
@api.constrains("operating_unit_id", "picking_type_id")
def _check_warehouse_operating_unit(self):
for record in self:
picking_type = record.picking_type_id
if not record.picking_type_id:
continue
warehouse = picking_type.warehouse_id
if (picking_type.warehouse_id and
picking_type.warehouse_id.operating_unit_id and
record.operating_unit_id and
warehouse.operating_unit_id != record.operating_unit_id):
if (
picking_type.warehouse_id
and picking_type.warehouse_id.operating_unit_id
and record.operating_unit_id
and warehouse.operating_unit_id != record.operating_unit_id
):
raise ValidationError(
_('Configuration error. The Quotation / Purchase Order '
'and the Warehouse of picking type must belong to the '
'same Operating Unit.')
_(
"Configuration error. The Quotation / Purchase Order "
"and the Warehouse of picking type must belong to the "
"same Operating Unit."
)
)

@api.constrains('operating_unit_id', 'company_id')
@api.constrains("operating_unit_id", "company_id")
def _check_company_operating_unit(self):
for record in self:
if (record.company_id and record.operating_unit_id and
record.company_id != record.operating_unit_id.company_id):
if (
record.company_id
and record.operating_unit_id
and record.company_id != record.operating_unit_id.company_id
):
raise ValidationError(
_('Configuration error. The Company in the Purchase Order '
'and in the Operating Unit must be the same.')
_(
"Configuration error. The Company in the Purchase Order "
"and in the Operating Unit must be the same."
)
)

@api.onchange('operating_unit_id')
@api.onchange("operating_unit_id")
def _onchange_operating_unit_id(self):
type_obj = self.env['stock.picking.type']
type_obj = self.env["stock.picking.type"]
if self.operating_unit_id:
types = type_obj.search([('code', '=', 'incoming'),
('warehouse_id.operating_unit_id', '=',
self.operating_unit_id.id)])
types = type_obj.search(
[
("code", "=", "incoming"),
("warehouse_id.operating_unit_id", "=", self.operating_unit_id.id),
]
)
if types:
self.picking_type_id = types[:1]
else:
raise UserError(
_("No Warehouse found with the Operating Unit indicated "
"in the Purchase Order")
_(
"No Warehouse found with the Operating Unit indicated "
"in the Purchase Order"
)
)

@api.model
def _prepare_picking(self):
picking_vals = super(PurchaseOrder, self)._prepare_picking()
picking_vals['operating_unit_id'] = self.operating_unit_id.id
picking_vals["operating_unit_id"] = self.operating_unit_id.id
return picking_vals


class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
_inherit = "purchase.order.line"

operating_unit_id = fields.Many2one(related='order_id.operating_unit_id',
string='Operating Unit')
operating_unit_id = fields.Many2one(
related="order_id.operating_unit_id", string="Operating Unit"
)
37 changes: 19 additions & 18 deletions purchase_operating_unit/security/purchase_security.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--© 2015-17 Eficent Business and IT Consulting Services S.L.-->
<!-- - Jordi Ballester Alomar-->
<!--© 2015-17 Serpent Consulting Services Pvt. Ltd. - Sudhir Arya-->
<!--License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).-->
<odoo noupdate="1">

<record id="ir_rule_purchase_order_allowed_operating_units" model="ir.rule">
<field name="model_id" ref="purchase.model_purchase_order"/>
<field name="domain_force">['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.ids)]</field>
<field name="model_id" ref="purchase.model_purchase_order" />
<field
name="domain_force"
>['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.ids)]</field>
<field name="name">PO's from allowed operating units</field>
<field name="global" eval="True"/>
<field eval="0" name="perm_unlink"/>
<field eval="0" name="perm_write"/>
<field eval="1" name="perm_read"/>
<field eval="0" name="perm_create"/>
<field name="global" eval="True" />
<field eval="0" name="perm_unlink" />
<field eval="0" name="perm_write" />
<field eval="1" name="perm_read" />
<field eval="0" name="perm_create" />
</record>

<record id="ir_rule_purchase_order_line_allowed_operating_units" model="ir.rule">
<field name="model_id" ref="purchase.model_purchase_order_line"/>
<field name="domain_force">['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.ids)]</field>
<field name="model_id" ref="purchase.model_purchase_order_line" />
<field
name="domain_force"
>['|',('operating_unit_id','=',False),('operating_unit_id','in', user.operating_unit_ids.ids)]</field>
<field name="name">PO lines from allowed operating units</field>
<field name="global" eval="True"/>
<field eval="0" name="perm_unlink"/>
<field eval="0" name="perm_write"/>
<field eval="1" name="perm_read"/>
<field eval="0" name="perm_create"/>
<field name="global" eval="True" />
<field eval="0" name="perm_unlink" />
<field eval="0" name="perm_write" />
<field eval="1" name="perm_read" />
<field eval="0" name="perm_create" />
</record>

</odoo>
Loading

0 comments on commit 78c8444

Please sign in to comment.