Skip to content

Commit

Permalink
[ADD] purchase stock AVCO tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoParadeda committed Sep 5, 2023
1 parent 9010787 commit 3722abc
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock_lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,115 @@
# Renato Lima <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tools.float_utils import float_is_zero

from .test_l10n_br_purchase_stock import L10nBrPurchaseStockBase


class L10nBrPurchaseStockBase(L10nBrPurchaseStockBase):
def setUp(self):
super().setUp()
self.lp_purchase_po = self.env.ref(
"l10n_br_purchase_stock.lucro_presumido_po_only_products_1"
).copy()
self.picking_ids = False

self.stock_acc_id = self.env["account.account"].search(
[("code", "=", "1.1.3.1.02")], limit=1
)
self.stock_input_acc_id = self.env["account.account"].search(
[("code", "=", "1.1.9.0.01")], limit=1
)
self.stock_output_acc_id = self.env["account.account"].search(
[("code", "=", "1.1.9.0.02")], limit=1
)

# Set ICMS, IPI, PIS, COFINS as non-creditable for Compra para industrialização.
# Note that any purchase using this operation must have it's tax values included
# in the stock cost.
self.fol_ind = self.env.ref("l10n_br_fiscal.fo_compras_compras")
self.fol_ind.non_creditable_tax_definition_ids += self.env.ref(
"l10n_br_fiscal.tax_group_icms"
)
self.fol_ind.non_creditable_tax_definition_ids += self.env.ref(
"l10n_br_fiscal.tax_group_ipi"
)
self.fol_ind.non_creditable_tax_definition_ids += self.env.ref(
"l10n_br_fiscal.tax_group_pis"
)
self.fol_ind.non_creditable_tax_definition_ids += self.env.ref(
"l10n_br_fiscal.tax_group_cofins"
)

def test_purchase_order_lp_stock_price(self):
"""Test stock price compute in purchase order line."""

purchase = self.lp_purchase_po
self._change_user_company(self.company_lucro_presumido)
self._run_purchase_order_onchanges(purchase)

line1 = purchase.order_line[0]
line2 = purchase.order_line[1]

# line 0 -> 90.35
expected_stock_price = (
line1.amount_total
- line1.ipi_value
- line1.icms_value
- line1.pis_value
- line1.cofins_value
) / line1.product_qty
computed_stock_price = line1.stock_price
self.assertEqual(
expected_stock_price, computed_stock_price, "Stock value mispriced"
)

# line 1 -> 120 (price with taxes)
expected_stock_price = line2.amount_total / line2.product_qty
computed_stock_price = line2.stock_price
self.assertEqual(
expected_stock_price, computed_stock_price, "Stock value mispriced"
)

purchase.button_confirm()

# Test if product cost matches stock price. Requires AVCO and auto stock valuation.
self.picking_id = purchase.picking_ids

# product categories
product_id1 = self.po_products.order_line[0].product_id
product_id2 = self.po_products.order_line[1].product_id
categ_id1 = product_id1.categ_id

categ_id1.property_stock_valuation_account_id = self.stock_acc_id
categ_id1.property_stock_account_input_categ_id = self.stock_input_acc_id
categ_id1.property_stock_account_output_categ_id = self.stock_output_acc_id

categ_id1.property_valuation = "real_time"
categ_id1.property_cost_method = "average"

# Validate picking
self.picking_id.action_confirm()
self.picking_id.action_assign()
# Force picking qties and validate
for move in self.picking_id.move_ids_without_package:
move.quantity_done = move.product_uom_qty
self.picking_id.button_validate()

# Validate costs created by auto-valuation
cost1_diff = float_is_zero(
product_id1.standard_price - 90.35, precision_rounding=0.001
)
cost2_diff = float_is_zero(
product_id2.standard_price - 120, precision_rounding=0.001
)
self.assertEqual(
cost1_diff,
True,
"Stock auto-valuation expected a different Unit Cost for PO line 1.",
)
self.assertEqual(
cost2_diff,
True,
"Stock auto-valuation expected a different Unit Cost for PO line 2.",
)

0 comments on commit 3722abc

Please sign in to comment.