Skip to content

Commit

Permalink
[IMP] stock_account_valuation_discrepancy_adjust: add an option to cr…
Browse files Browse the repository at this point in the history
…eate single journal entry
  • Loading branch information
AaronHForgeFlow committed Nov 11, 2024
1 parent 63f9129 commit 0208a15
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2021 ForgeFlow S.L.
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare


class WizardStockDiscrepancyAdjustment(models.TransientModel):
Expand All @@ -12,6 +13,7 @@ class WizardStockDiscrepancyAdjustment(models.TransientModel):
inverse_name="wizard_id",
string="Selected Products",
)
single_journal_entry = fields.Boolean()

def _get_default_stock_journal(self):
return self.env["account.journal"].search(

Check warning on line 19 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L19

Added line #L19 was not covered by tests
Expand Down Expand Up @@ -74,25 +76,45 @@ def action_create_adjustment(self):
move_model = self.env["account.move"]
product_model = self.env["product.product"]
moves_created = move_model.browse()
move_data = {
"journal_id": self.journal_id.id,
"date": self.to_date,
"ref": _("Adjust for Stock Valuation Discrepancy"),
"line_ids": [],
}

if self.product_selection_ids:
products_with_discrepancy = product_model.with_context(
to_date=self.to_date
).browse(self.product_selection_ids.mapped("product_id").ids)

for product in products_with_discrepancy:
move_data = {
"journal_id": self.journal_id.id,
"date": self.to_date,
"ref": _("Adjust for Stock Valuation Discrepancy"),
}
valuation_account = product.product_tmpl_id._get_product_accounts()[
"stock_valuation"
]
if not valuation_account:
raise UserError(

Check warning on line 96 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L96

Added line #L96 was not covered by tests
_("Product %s doesn't " "have stock valuation account assigned")
% (product.display_name)
_("Product %s doesn't have stock valuation account assigned")
% product.display_name
)
# do not create move if no discrepancy
if (
float_compare(
product.qty_at_date,
product.account_qty_at_date,
precision_digits=product.uom_id.rounding,
)
move_data["line_ids"] = [
== 0
and float_compare(
product.stock_value,
product.account_value,
precision_digits=product.uom_id.rounding,
)
== 0
):
continue

Check warning on line 115 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L115

Added line #L115 was not covered by tests
# Create debit and credit line data for this product
line_debit_credit = [
(
0,
0,
Expand Down Expand Up @@ -126,12 +148,31 @@ def action_create_adjustment(self):
},
),
]

# If single_journal_entry is True, append line items to move_data
if self.single_journal_entry:
move_data["line_ids"].extend(line_debit_credit)

Check warning on line 154 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L154

Added line #L154 was not covered by tests
else:
# Create individual move for each product
move = move_model.create(
{
**move_data,
"line_ids": line_debit_credit,
}
)
move.action_post()
moves_created |= move

# If single_journal_entry is True, create one move with all lines
if self.single_journal_entry:
move = move_model.create(move_data)
move.action_post()
moves_created |= move

Check warning on line 170 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L168-L170

Added lines #L168 - L170 were not covered by tests

action = self.env.ref("account.action_move_journal_line").read()[0]
action["domain"] = [("id", "in", moves_created.ids)]
return action

return {"type": "ir.actions.act_window_close"}

Check warning on line 176 in stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py

View check run for this annotation

Codecov / codecov/patch

stock_account_valuation_discrepancy_adjust/wizards/wizard_stock_discrepancy_adjustment.py#L176

Added line #L176 was not covered by tests


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
name="decrease_account_id"
attrs="{'invisible': [('product_selection_ids', '=', [])], 'required': [('product_selection_ids', '!=', [])]}"
/>
<field name="single_journal_entry" />
</group>
<notebook colspan="4">
<page string="Products with discrepancy">
Expand Down

0 comments on commit 0208a15

Please sign in to comment.