Skip to content

Commit

Permalink
adj
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Nov 12, 2024
1 parent 01dcadf commit 7f0ce68
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 89 deletions.
181 changes: 106 additions & 75 deletions l10n_jp_summary_invoice/models/account_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,79 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class AccountBilling(models.Model):
_inherit = 'account.billing'

summary_invoice_amount_tax = fields.Monetary(
compute="_compute_amount_tax",
store=True,
)
invoice_tax_sum = fields.Monetary(
string="Invoices Tax Sum",
compute="_compute_amount_tax",
)
tax_difference = fields.Monetary(
string="Tax Difference",
compute="_compute_tax_difference",
store=True,
)
tax_adjustment_entry_id = fields.Many2one("account.move")
tax_entry_journal_id = fields.Many2one("account.journal", help="This journal will be used for tax adjustment journal entry.")

@api.depends('billing_line_ids.move_id')
def _compute_amount_tax(self):
for billing in self:
tax_sum = sum(line.move_id.amount_tax for line in billing.billing_line_ids if line.move_id)
billing.invoice_tax_sum = tax_sum
grouped_lines = {}
for line in billing.billing_line_ids:
invoice = line.move_id
for inv_line in invoice.invoice_line_ids:
for tax in inv_line.tax_ids:
if tax not in grouped_lines:
grouped_lines[tax] = inv_line.price_subtotal
continue
def _get_tax_summary_from_invoices(self):
"""Get the actual tax amounts per tax based on the invoice lines associated with billing lines.
Returns a dictionary where each key is a tax and the value is the total tax amount.
"""
self.ensure_one()
tax_summary = {}
for line in self.billing_line_ids:
invoice = line.move_id
for tax_line in invoice.line_ids.filtered(lambda l: l.tax_line_id):
tax = tax_line.tax_line_id
tax_summary[tax] = tax_summary.get(tax, 0) + tax_line.credit
return tax_summary

def _group_invoice_lines_by_tax(self):
"""Group invoice lines by tax, summing the subtotals for each tax type.
Returns a dictionary with tax as keys and subtotal as values.
"""
self.ensure_one()
grouped_lines = {}
for line in self.billing_line_ids:
invoice = line.move_id
for inv_line in invoice.invoice_line_ids:
for tax in inv_line.tax_ids:
if tax not in grouped_lines:
grouped_lines[tax] = inv_line.price_subtotal
else:
grouped_lines[tax] += inv_line.price_subtotal
billing.summary_invoice_amount_tax = sum(
tax.compute_all(subtotal, billing.currency_id)['taxes'][0]['amount']
for tax, subtotal in grouped_lines.items()
)
return grouped_lines

def _get_calculated_tax_summary(self):
"""Calculate the tax amounts per tax based on the subtotal of each invoice line.
Returns a dictionary where each key is a tax and the value is the expected tax amount.
"""
self.ensure_one()
calculated_tax_summary = {}
tax_grouped_lines = self._group_invoice_lines_by_tax()
for tax, subtotal in tax_grouped_lines.items():
# Compute tax amount based on the subtotal and tax rates
tax_amount = tax.compute_all(subtotal, self.currency_id)['taxes'][0]['amount']
calculated_tax_summary[tax] = tax_amount
return calculated_tax_summary

@api.depends('summary_invoice_amount_tax', 'invoice_tax_sum')
def _compute_tax_difference(self):
for billing in self:
billing.tax_difference = billing.summary_invoice_amount_tax - billing.invoice_tax_sum
def _get_tax_differences(self, tax_summary, calculated_tax_summary):
"""Compare actual tax amounts from invoices with the expected tax amounts,
and return the differences for each tax.
"""
self.ensure_one()
tax_differences = {}
for tax, amount in tax_summary.items():
summary_tax_amount = calculated_tax_summary.get(tax, 0)
tax_diff = summary_tax_amount - amount
if tax_diff != 0:
tax_differences[tax] = tax_diff
return tax_differences

def _assign_tax_tags_to_entry(self, adjustment_entry, tax_account, tax_differences):
"""Assign the appropriate tax tags to the adjustment journal entry.
"""
for line in adjustment_entry.line_ids.filtered(lambda l: l.account_id == tax_account and l.tax_ids):
for tax, _ in tax_differences.items():
if f"Tax Adjustment for {tax.name}" in line.name:
line.tax_tag_ids = [(6, 0, tax.invoice_repartition_line_ids[1].tag_ids.ids)]

def validate_billing(self):
super().validate_billing()
invoice = self.billing_line_ids[0].move_id if self.billing_line_ids else None
receivable_line = invoice.line_ids.filtered(lambda line: line.account_id.account_type == 'asset_receivable')
receivable_account = receivable_line[0].account_id if receivable_line else None
tax_line = invoice.line_ids.filtered(lambda line: line.tax_line_id and line.account_id.account_type == 'liability_current')
tax_account = tax_line[0].account_id if tax_line else None
tag_ids = self.env['account.account.tag'].search([('is_use_in_tax_adjustment', '=', True)])
if not tag_ids:
raise UserError(_("There are no tax grids for the adjustment tax."))
tax_adjustment_tax = self.env['account.tax'].search([('name', '=', 'Adjustment')], limit=1)
if not tax_adjustment_tax:
tax_adjustment_tax = self.env['account.tax'].create({
Expand All @@ -66,39 +83,53 @@ def validate_billing(self):
'type_tax_use': 'sale',
'price_include': True,
})
tax_adjustment_tax.invoice_repartition_line_ids[0].tag_ids = [(6, 0, tag_ids.ids)]
tax_adjustment_tax.refund_repartition_line_ids[0].tag_ids = [(6, 0, tag_ids.ids)]
for rec in self:
if rec.tax_difference != 0:
adjustment_entry_vals = {
'journal_id': self.tax_entry_journal_id.id,
'date': rec.date,
'line_ids': [
(0, 0, {
'account_id': tax_account.id,
'debit': 0.0,
'credit': rec.tax_difference,
'tax_ids': [(6, 0, tax_adjustment_tax.ids)],
'name': 'Tax Adjustment',
}),
(0, 0, {
'account_id': receivable_account.id,
'debit': rec.tax_difference,
'credit': 0.0,
'name': 'Tax Adjustment',
})
]
}
adjustment_entry = self.env['account.move'].create(adjustment_entry_vals)
adjustment_entry.line_ids.filtered(
lambda line: not (
(line.account_id == tax_account and line.tax_ids) or
(line.account_id == receivable_account)
)
).with_context(dynamic_unlink=True).unlink()
adjustment_entry.action_post()
rec.tax_adjustment_entry_id = adjustment_entry.id

invoice = self.billing_line_ids[0].move_id if self.billing_line_ids else None
receivable_line = invoice.line_ids.filtered(lambda line: line.account_id.account_type == 'asset_receivable')
receivable_account = receivable_line[0].account_id if receivable_line else None
tax_line = invoice.line_ids.filtered(lambda line: line.tax_line_id and line.account_id.account_type == 'liability_current')
tax_account = tax_line[0].account_id
tax_differences = rec._get_tax_differences(
rec._get_tax_summary_from_invoices(),
rec._get_calculated_tax_summary()
)
if not tax_differences:
continue
journal = rec.tax_entry_journal_id
adjustment_entry_vals = {
'journal_id': journal.id,
'date': rec.date,
'line_ids': [],
}
credit = 0.0
debit = 0.0
for tax, difference in tax_differences.items():
if difference != 0:
adjustment_entry_vals['line_ids'].append((0, 0, {
'account_id': tax_account.id,
'credit': abs(difference) if difference > 0 else 0,
'debit': abs(difference) if difference < 0 else 0,
'name': f"Tax Adjustment for {tax.name}",
'tax_ids': [(6, 0, tax_adjustment_tax.ids)],
}))
debit += abs(difference) if difference > 0 else 0
credit += abs(difference) if difference < 0 else 0
adjustment_entry_vals['line_ids'].append((0, 0, {
'account_id': receivable_account.id,
'debit': debit,
'credit': credit,
'name': 'Tax Adjustment'
}))
rec.tax_adjustment_entry_id = self.env['account.move'].create(adjustment_entry_vals)
rec._assign_tax_tags_to_entry(rec.tax_adjustment_entry_id, tax_account, tax_differences)
rec.tax_adjustment_entry_id.line_ids.filtered(
lambda line: not (
(line.account_id == tax_account and line.tax_ids) or
(line.account_id == receivable_account)
)
).with_context(dynamic_unlink=True).unlink()
rec.tax_adjustment_entry_id.action_post()

def action_cancel(self):
super().action_cancel()
for rec in self:
Expand Down
60 changes: 59 additions & 1 deletion l10n_jp_summary_invoice/reports/report_summary_invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<th>
<span>Tax</span>
</th>
<th class="text-right">
<th>
<span>Total</span>
</th>
</tr>
Expand All @@ -59,6 +59,64 @@
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered o_main_table">
<thead>
<tr>
<th>
<span>Date / Invoice No.</span>
</th>
<th>
<span>Product</span>
</th>
<th>
<span>Quantity</span>
</th>
<th>
<span>Uom</span>
</th>
<th>
<span>Price</span>
</th>
<th>
<span>Tax</span>
</th>
<th>
<span>Subtotal</span>
</th>
<th>
<span>Sale Order No.</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span>100.00</span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
<td>
<span></span>
</td>
</tr>
</tbody>
</table>
</div>
</t>
</template>
Expand Down
15 changes: 2 additions & 13 deletions l10n_jp_summary_invoice/views/account_billing_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@
<field name="inherit_id" ref="account_billing.view_account_billing_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='currency_id']" position="after" >
<field name="tax_entry_journal_id" />
</xpath>
<xpath expr="//notebook" position="inside">
<page string="Tax Adjustment Information" attrs="{'invisible': [('bill_type', '!=', 'out_invoice')]}">
<group string="Summary" class="oe_mandatory">
<group>
<field name="summary_invoice_amount_tax" readonly="1" />
<field name="invoice_tax_sum" readonly="1" />
<field name="tax_difference" readonly="1" />
<field name="tax_adjustment_entry_id" readonly="1"/>
</group>
</group>
</page>
<field name="tax_entry_journal_id" attrs="{'invisible': [('bill_type', '!=', 'out_invoice')]}"/>
<field name="tax_adjustment_entry_id" readonly="1" attrs="{'invisible': [('bill_type', '!=', 'out_invoice')]}"/>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 7f0ce68

Please sign in to comment.