mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[IMP] stock_request_analytic: black, isort, prettier
This commit is contained in:
committed by
Denis Roussel
parent
8ecc0f5054
commit
23d243ff42
@@ -1,2 +1 @@
|
||||
|
||||
from . import models
|
||||
|
||||
@@ -7,13 +7,9 @@
|
||||
"version": "12.0.1.0.1",
|
||||
"license": "LGPL-3",
|
||||
"website": "https://github.com/stock-logistics-warehouse",
|
||||
"author": "Eficent, "
|
||||
"Odoo Community Association (OCA)",
|
||||
"author": "Eficent, " "Odoo Community Association (OCA)",
|
||||
"category": "Analytic",
|
||||
"depends": [
|
||||
"stock_request",
|
||||
"stock_analytic",
|
||||
],
|
||||
"depends": ["stock_request", "stock_analytic",],
|
||||
"data": [
|
||||
"security/ir.model.access.csv",
|
||||
"views/stock_request_views.xml",
|
||||
|
||||
@@ -5,8 +5,11 @@ from odoo import fields, models
|
||||
|
||||
|
||||
class AccountAnalyticAccount(models.Model):
|
||||
_inherit = 'account.analytic.account'
|
||||
_inherit = "account.analytic.account"
|
||||
|
||||
stock_request_ids = fields.One2many(
|
||||
comodel_name='stock.request', inverse_name='analytic_account_id',
|
||||
string='Stock Requests', copy=False)
|
||||
comodel_name="stock.request",
|
||||
inverse_name="analytic_account_id",
|
||||
string="Stock Requests",
|
||||
copy=False,
|
||||
)
|
||||
|
||||
@@ -9,15 +9,22 @@ class StockRequest(models.Model):
|
||||
_inherit = "stock.request"
|
||||
|
||||
analytic_account_id = fields.Many2one(
|
||||
'account.analytic.account', string='Analytic Account')
|
||||
analytic_tag_ids = fields.Many2many(
|
||||
'account.analytic.tag', string='Analytic Tags')
|
||||
"account.analytic.account", string="Analytic Account"
|
||||
)
|
||||
analytic_tag_ids = fields.Many2many("account.analytic.tag", string="Analytic Tags")
|
||||
|
||||
@api.constrains('analytic_account_id')
|
||||
@api.constrains("analytic_account_id")
|
||||
def _check_analytic_company_constrains(self):
|
||||
if any(r.company_id and r.analytic_account_id and
|
||||
r.analytic_account_id.company_id != r.company_id for r in self):
|
||||
if any(
|
||||
r.company_id
|
||||
and r.analytic_account_id
|
||||
and r.analytic_account_id.company_id != r.company_id
|
||||
for r in self
|
||||
):
|
||||
raise ValidationError(
|
||||
_('You cannot link a analytic account '
|
||||
'to a stock request that belongs to '
|
||||
'another company.'))
|
||||
_(
|
||||
"You cannot link a analytic account "
|
||||
"to a stock request that belongs to "
|
||||
"another company."
|
||||
)
|
||||
)
|
||||
|
||||
@@ -5,45 +5,43 @@ from odoo import api, fields, models
|
||||
|
||||
|
||||
class StockRequestOrder(models.Model):
|
||||
_inherit = 'stock.request.order'
|
||||
_inherit = "stock.request.order"
|
||||
|
||||
analytic_count = fields.Integer(
|
||||
compute='_compute_analytic_ids',
|
||||
readonly=True,
|
||||
)
|
||||
analytic_count = fields.Integer(compute="_compute_analytic_ids", readonly=True,)
|
||||
analytic_account_ids = fields.One2many(
|
||||
comodel_name='account.analytic.account',
|
||||
compute='_compute_analytic_ids',
|
||||
string='Analytic Accounts',
|
||||
comodel_name="account.analytic.account",
|
||||
compute="_compute_analytic_ids",
|
||||
string="Analytic Accounts",
|
||||
readonly=True,
|
||||
)
|
||||
analytic_tag_ids = fields.One2many(
|
||||
comodel_name='account.analytic.tag',
|
||||
compute='_compute_analytic_ids',
|
||||
string='Analytic Tags',
|
||||
comodel_name="account.analytic.tag",
|
||||
compute="_compute_analytic_ids",
|
||||
string="Analytic Tags",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
@api.depends('stock_request_ids')
|
||||
@api.depends("stock_request_ids")
|
||||
def _compute_analytic_ids(self):
|
||||
for req in self.sudo():
|
||||
req.analytic_account_ids = req.stock_request_ids.mapped(
|
||||
'analytic_account_id')
|
||||
req.analytic_tag_ids = req.stock_request_ids.mapped(
|
||||
'analytic_tag_ids')
|
||||
"analytic_account_id"
|
||||
)
|
||||
req.analytic_tag_ids = req.stock_request_ids.mapped("analytic_tag_ids")
|
||||
req.analytic_count = len(req.analytic_account_ids)
|
||||
|
||||
@api.multi
|
||||
def action_view_analytic(self):
|
||||
action = self.env.ref(
|
||||
'analytic.action_account_analytic_account_form').read()[0]
|
||||
analytics = self.mapped('analytic_account_ids')
|
||||
action = self.env.ref("analytic.action_account_analytic_account_form").read()[0]
|
||||
analytics = self.mapped("analytic_account_ids")
|
||||
if len(analytics) > 1:
|
||||
action['domain'] = [('id', 'in', analytics.ids)]
|
||||
action["domain"] = [("id", "in", analytics.ids)]
|
||||
elif analytics:
|
||||
action['views'] = [
|
||||
(self.env.ref(
|
||||
'analytic.action_account_analytic_account_form').id,
|
||||
'form')]
|
||||
action['res_id'] = analytics.id
|
||||
action["views"] = [
|
||||
(
|
||||
self.env.ref("analytic.action_account_analytic_account_form").id,
|
||||
"form",
|
||||
)
|
||||
]
|
||||
action["res_id"] = analytics.id
|
||||
return action
|
||||
|
||||
@@ -7,15 +7,29 @@ from odoo import models
|
||||
class ProcurementRule(models.Model):
|
||||
_inherit = "stock.rule"
|
||||
|
||||
def _get_stock_move_values(self, product_id, product_qty, product_uom,
|
||||
location_id, name, origin, values, group_id):
|
||||
def _get_stock_move_values(
|
||||
self,
|
||||
product_id,
|
||||
product_qty,
|
||||
product_uom,
|
||||
location_id,
|
||||
name,
|
||||
origin,
|
||||
values,
|
||||
group_id,
|
||||
):
|
||||
res = super(ProcurementRule, self)._get_stock_move_values(
|
||||
product_id, product_qty, product_uom, location_id, name, origin,
|
||||
values, group_id)
|
||||
if values.get('stock_request_id'):
|
||||
stock_request = self.env['stock.request'].browse(
|
||||
values['stock_request_id']
|
||||
product_id,
|
||||
product_qty,
|
||||
product_uom,
|
||||
location_id,
|
||||
name,
|
||||
origin,
|
||||
values,
|
||||
group_id,
|
||||
)
|
||||
if values.get("stock_request_id"):
|
||||
stock_request = self.env["stock.request"].browse(values["stock_request_id"])
|
||||
analytic_account = stock_request.analytic_account_id
|
||||
analytic_tags = stock_request.analytic_tag_ids
|
||||
res.update(
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
|
||||
from . import test_stock_request_analytic
|
||||
|
||||
@@ -1,86 +1,98 @@
|
||||
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
|
||||
|
||||
from odoo.addons.stock_request.tests import test_stock_request
|
||||
from odoo import fields
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
from odoo.addons.stock_request.tests import test_stock_request
|
||||
|
||||
|
||||
class TestStockRequestAnalytic(test_stock_request.TestStockRequest):
|
||||
def setUp(self):
|
||||
super(TestStockRequestAnalytic, self).setUp()
|
||||
self.analytic_model = self.env['account.analytic.account']
|
||||
self.analytic = self.analytic_model.create({'name': 'Pizza'})
|
||||
self.analytic2 = self.analytic_model.create({
|
||||
'name': 'Pizza',
|
||||
'company_id': self.company_2.id,
|
||||
})
|
||||
self.demand_loc = self.env['stock.location'].create({
|
||||
'name': 'demand_loc',
|
||||
'location_id': self.warehouse.lot_stock_id.id,
|
||||
'usage': 'internal',
|
||||
})
|
||||
self.demand_route = self.env['stock.location.route'].create({
|
||||
'name': 'Transfer',
|
||||
'product_categ_selectable': False,
|
||||
'product_selectable': True,
|
||||
'company_id': self.main_company.id,
|
||||
'sequence': 10,
|
||||
})
|
||||
self.pizza = self._create_product('PZ', 'Pizza', False)
|
||||
self.demand_rule = self.env['stock.rule'].create({
|
||||
'name': 'Transfer',
|
||||
'route_id': self.demand_route.id,
|
||||
'location_src_id': self.warehouse.lot_stock_id.id,
|
||||
'location_id': self.demand_loc.id,
|
||||
'action': 'pull',
|
||||
'picking_type_id': self.warehouse.int_type_id.id,
|
||||
'procure_method': 'make_to_stock',
|
||||
'warehouse_id': self.warehouse.id,
|
||||
'company_id': self.main_company.id,
|
||||
'propagate': 'False',
|
||||
})
|
||||
self.analytic_model = self.env["account.analytic.account"]
|
||||
self.analytic = self.analytic_model.create({"name": "Pizza"})
|
||||
self.analytic2 = self.analytic_model.create(
|
||||
{"name": "Pizza", "company_id": self.company_2.id,}
|
||||
)
|
||||
self.demand_loc = self.env["stock.location"].create(
|
||||
{
|
||||
"name": "demand_loc",
|
||||
"location_id": self.warehouse.lot_stock_id.id,
|
||||
"usage": "internal",
|
||||
}
|
||||
)
|
||||
self.demand_route = self.env["stock.location.route"].create(
|
||||
{
|
||||
"name": "Transfer",
|
||||
"product_categ_selectable": False,
|
||||
"product_selectable": True,
|
||||
"company_id": self.main_company.id,
|
||||
"sequence": 10,
|
||||
}
|
||||
)
|
||||
self.pizza = self._create_product("PZ", "Pizza", False)
|
||||
self.demand_rule = self.env["stock.rule"].create(
|
||||
{
|
||||
"name": "Transfer",
|
||||
"route_id": self.demand_route.id,
|
||||
"location_src_id": self.warehouse.lot_stock_id.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"action": "pull",
|
||||
"picking_type_id": self.warehouse.int_type_id.id,
|
||||
"procure_method": "make_to_stock",
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"company_id": self.main_company.id,
|
||||
"propagate": "False",
|
||||
}
|
||||
)
|
||||
self.pizza.route_ids = [(6, 0, self.demand_route.ids)]
|
||||
|
||||
def prepare_order_request_analytic(self, aa, company, analytic_tags=None):
|
||||
expected_date = fields.Datetime.now()
|
||||
analytic_tags = analytic_tags or self.env["account.analytic.tag"]
|
||||
vals = {
|
||||
'company_id': company.id,
|
||||
'warehouse_id': self.warehouse.id,
|
||||
'location_id': self.demand_loc.id,
|
||||
'expected_date': expected_date,
|
||||
'stock_request_ids': [(0, 0, {
|
||||
'product_id': self.pizza.id,
|
||||
'product_uom_id': self.pizza.uom_id.id,
|
||||
'product_uom_qty': 5.0,
|
||||
'analytic_account_id': aa.id,
|
||||
'analytic_tag_ids': [(4, tag.id) for tag in analytic_tags],
|
||||
'company_id': company.id,
|
||||
'warehouse_id': self.warehouse.id,
|
||||
'location_id': self.demand_loc.id,
|
||||
'expected_date': expected_date,
|
||||
})]
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
"stock_request_ids": [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"product_id": self.pizza.id,
|
||||
"product_uom_id": self.pizza.uom_id.id,
|
||||
"product_uom_qty": 5.0,
|
||||
"analytic_account_id": aa.id,
|
||||
"analytic_tag_ids": [(4, tag.id) for tag in analytic_tags],
|
||||
"company_id": company.id,
|
||||
"warehouse_id": self.warehouse.id,
|
||||
"location_id": self.demand_loc.id,
|
||||
"expected_date": expected_date,
|
||||
},
|
||||
)
|
||||
],
|
||||
}
|
||||
return vals
|
||||
|
||||
def test_stock_analytic(self):
|
||||
analytic_tag = self.env.ref('analytic.tag_contract')
|
||||
analytic_tag = self.env.ref("analytic.tag_contract")
|
||||
vals = self.prepare_order_request_analytic(
|
||||
self.analytic, self.main_company, analytic_tags=analytic_tag)
|
||||
order = self.env['stock.request.order'].create(vals)
|
||||
self.analytic, self.main_company, analytic_tags=analytic_tag
|
||||
)
|
||||
order = self.env["stock.request.order"].create(vals)
|
||||
req = order.stock_request_ids
|
||||
order.action_confirm()
|
||||
self.assertEqual(
|
||||
req.move_ids.mapped('analytic_account_id'), self.analytic)
|
||||
self.assertEqual(
|
||||
req.move_ids.mapped('analytic_tag_ids'), analytic_tag)
|
||||
self.assertEqual(req.move_ids.mapped("analytic_account_id"), self.analytic)
|
||||
self.assertEqual(req.move_ids.mapped("analytic_tag_ids"), analytic_tag)
|
||||
self.assertEqual(order.analytic_count, 1)
|
||||
action = order.action_view_analytic()
|
||||
self.assertTrue(action['res_id'], self.analytic.id)
|
||||
self.assertTrue(action["res_id"], self.analytic.id)
|
||||
|
||||
def test_company(self):
|
||||
with self.assertRaises(ValidationError):
|
||||
vals = self.prepare_order_request_analytic(
|
||||
self.analytic2, self.main_company)
|
||||
self.env['stock.request.order'].create(vals)
|
||||
self.analytic2, self.main_company
|
||||
)
|
||||
self.env["stock.request.order"].create(vals)
|
||||
|
||||
@@ -16,5 +16,4 @@
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</odoo>
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
<!-- Copyright 2017 Eficent
|
||||
License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
||||
<odoo>
|
||||
|
||||
<record id="stock_request_order_form" model="ir.ui.view">
|
||||
<field name="name">stock.request.order.form - stock_request_analytic</field>
|
||||
<field name="model">stock.request.order</field>
|
||||
@@ -11,20 +10,30 @@
|
||||
<div name="button_box" position="inside">
|
||||
<field name="analytic_account_ids" invisible="1" />
|
||||
<field name="analytic_tag_ids" />
|
||||
<button type="object"
|
||||
<button
|
||||
type="object"
|
||||
name="action_view_analytic"
|
||||
class="oe_stat_button"
|
||||
icon="fa-list"
|
||||
attrs="{'invisible': [('analytic_count', '=', 0)]}"
|
||||
groups="analytic.group_analytic_accounting">
|
||||
<field name="analytic_count" widget="statinfo"
|
||||
string="Analytic Accounts"/>
|
||||
groups="analytic.group_analytic_accounting"
|
||||
>
|
||||
<field
|
||||
name="analytic_count"
|
||||
widget="statinfo"
|
||||
string="Analytic Accounts"
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
<xpath expr="//field[@name='stock_request_ids']/tree//field[@name='route_id']" position="after">
|
||||
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
|
||||
<xpath
|
||||
expr="//field[@name='stock_request_ids']/tree//field[@name='route_id']"
|
||||
position="after"
|
||||
>
|
||||
<field
|
||||
name="analytic_account_id"
|
||||
groups="analytic.group_analytic_accounting"
|
||||
/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</odoo>
|
||||
|
||||
@@ -2,17 +2,21 @@
|
||||
<!-- Copyright 2017 Eficent
|
||||
License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
||||
<odoo>
|
||||
|
||||
<record id="view_stock_request_form" model="ir.ui.view">
|
||||
<field name="name">stock.request.form</field>
|
||||
<field name="model">stock.request</field>
|
||||
<field name="inherit_id" ref="stock_request.view_stock_request_form" />
|
||||
<field name="arch" type="xml">
|
||||
<field name="procurement_group_id" position="after">
|
||||
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
|
||||
<field name="analytic_tag_ids" groups="analytic.group_analytic_accounting"/>
|
||||
<field
|
||||
name="analytic_account_id"
|
||||
groups="analytic.group_analytic_accounting"
|
||||
/>
|
||||
<field
|
||||
name="analytic_tag_ids"
|
||||
groups="analytic.group_analytic_accounting"
|
||||
/>
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</odoo>
|
||||
|
||||
Reference in New Issue
Block a user