Skip to content

Commit

Permalink
[ADD] stock price and non-creditable taxes
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoParadeda committed Sep 5, 2023
1 parent 7d84928 commit 9010787
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
24 changes: 24 additions & 0 deletions l10n_br_fiscal/models/document_fiscal_line_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,3 +857,27 @@ def _operation_domain(self):
comodel_name="l10n_br_fiscal.cnae",
string="CNAE Code",
)

stock_price = fields.Float(string="Stock Price", compute="_compute_stock_price")

@api.depends("amount_total", "fiscal_tax_ids")
def _compute_stock_price(self):
for record in self:
if not hasattr(record, "product_uom_qty") or not record.product_uom_qty:
record.stock_price = 0
continue

non_creditable_taxes = (
record.fiscal_operation_line_id.non_creditable_tax_definition_ids
)
price = record.amount_total

for tax in record.fiscal_tax_ids:
if hasattr(record, "%s_tax_id" % (tax.tax_domain,)):
tax_id = getattr(record, "%s_tax_id" % (tax.tax_domain,))
if tax_id.tax_group_id not in non_creditable_taxes:
if not hasattr(record, "%s_value" % (tax.tax_domain)):
continue
price -= getattr(record, "%s_value" % (tax.tax_domain))

record.stock_price = price / record.product_uom_qty
5 changes: 5 additions & 0 deletions l10n_br_fiscal/models/operation_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class OperationLine(models.Model):

date_end = fields.Datetime(string="End Date")

non_creditable_tax_definition_ids = fields.Many2many(
comodel_name="l10n_br_fiscal.tax.group",
string="Non-creditable Tax Groups",
)

_sql_constraints = [
(
"fiscal_operation_name_uniq",
Expand Down
5 changes: 5 additions & 0 deletions l10n_br_fiscal/views/document_fiscal_line_mixin_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@
<field name="manual_additional_data" />
<field name="additional_data" readonly="1" />
</group>
<group>
<h3>Stock Unit Cost <span class="text-danger">
<field name="stock_price" /></span>
</h3>
</group>
</page>
</notebook>
</form>
Expand Down
7 changes: 7 additions & 0 deletions l10n_br_fiscal/views/operation_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
nolabel="1"
context="{'tree_view_ref': 'l10n_br_fiscal.tax_definition_tree','form_view_ref': 'l10n_br_fiscal.tax_definition_form', 'default_fiscal_operation_line_id': id}"
/>
<label for="non_creditable_tax_definition_ids" />
<field
nolabel="1"
name="non_creditable_tax_definition_ids"
attrs="{'readonly': [('state', '!=', 'draft')]}"
widget="many2many_tags"
/>
</page>
<page name="extra_info" string="Extra Info">
<group>
Expand Down
1 change: 1 addition & 0 deletions l10n_br_purchase_stock/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from . import stock_rule
from . import res_config_settings
from . import stock_move
from . import account_move
23 changes: 23 additions & 0 deletions l10n_br_purchase_stock/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2023 - Diego Paradeda - KMEE

from odoo import api, models


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

@api.model
def _get_default_tax_account(self, repartition_line):
"""This is required to remap the tax line onto the same account as
the origin line, e.g. ICMS -> Estoque."""
account = super(AccountMoveLine, self)._get_default_tax_account(
repartition_line
)

if (
repartition_line.tax_id.tax_group_id.fiscal_tax_group_id
in self.fiscal_operation_line_id.non_creditable_tax_definition_ids
):
return self.account_id

return account
13 changes: 13 additions & 0 deletions l10n_br_purchase_stock/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
class StockMove(models.Model):
_inherit = "stock.move"

def _get_price_unit(self):
res = super()._get_price_unit()

po_line = self.purchase_line_id.sudo()
if (
po_line
and self.product_id == po_line.product_id
and not self.fiscal_operation_id.fiscal_operation_type == "out"
):
res = po_line.stock_price

return res

def _get_price_unit_invoice(self, inv_type, partner, qty=1):
result = super()._get_price_unit_invoice(inv_type, partner, qty)
# Caso tenha Purchase Line já vem desagrupado aqui devido ao KEY
Expand Down
1 change: 1 addition & 0 deletions l10n_br_sale_stock/tests/test_sale_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def test_picking_sale_order_product_and_service(self):
"display_name",
"state",
"create_date",
"stock_price",
]

common_fields = list(set(acl_fields) & set(sol_fields) - set(skipped_fields))
Expand Down

0 comments on commit 9010787

Please sign in to comment.