Skip to content

Commit

Permalink
Merge l10n_th_account_advance_clearing to l10n_th_account_expense_tax
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Oct 31, 2021
1 parent 7b98f24 commit a3cd6b3
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 5 deletions.
1 change: 1 addition & 0 deletions l10n_th_account_tax/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ access_account_move_tax_invoice_portal,account.move.tax.invoice portal,model_acc
access_account_move_tax_invoice_manager,account.move.tax.invoice manager,model_account_move_tax_invoice,account.group_account_manager,1,0,0,0
access_account_move_tax_invoice_user,account.move.tax.invoice user,model_account_move_tax_invoice,base.group_user,1,1,1,0
access_account_withholding_tax,account.withholding.tax,model_account_withholding_tax,account.group_account_invoice,1,1,1,1
access_account_withholding_tax_user,account.withholding.tax.user,model_account_withholding_tax,base.group_user,1,0,0,0
access_personal_income_tax,access_personal_income_tax,model_personal_income_tax,account.group_account_user,1,1,1,1
access_personal_income_tax_user,access_personal_income_tax_user,model_personal_income_tax,,1,0,0,0
access_personal_income_tax_rate,access_personal_income_tax_rate,model_personal_income_tax_rate,account.group_account_user,1,1,1,1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
<field name="has_wht" invisible="1" />
<button
name="action_create_withholding_tax_entry"
string="Create Withholding JV"
string="Create Withholding Tax JV"
type="object"
groups="account.group_account_user"
attrs="{'invisible': ['|', '|', '|', ('state', '!=', 'done'), ('has_wht', '=', False), ('wht_move_id', '!=', False), ('advance_sheet_id', '=', False)]}"
attrs="{'invisible': ['|', ('state', 'not in', ('done', 'post')), ('wht_move_id', '!=', False)]}"
confirm="This action will create a new Withholding Tax JV to adjust this expense eheet. Are you sure to continue?"
/>
</button>
<field name="account_move_id" position="after">
Expand Down
1 change: 1 addition & 0 deletions l10n_th_account_tax_expense/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from . import account_move
from . import hr_expense
from . import hr_expense_sheet
33 changes: 31 additions & 2 deletions l10n_th_account_tax_expense/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,38 @@ class AccountMove(models.Model):
_inherit = "account.move"

def _post(self, soft=True):
res = super()._post(soft)
self._assign_tax_invoice()
self._reconcile_withholding_tax_entry()
return res

def _reconcile_withholding_tax_entry(self):
""" Re-Reconciliation, ensure the wht_move is taken into account """
for move in self:
clearing = self.env["hr.expense.sheet"].search(
[("wht_move_id", "=", move.id)]
)
if not clearing:
continue
clearing.ensure_one()
move_lines = clearing.account_move_id.line_ids
accounts = move_lines.mapped("account_id").filtered("reconcile")
# Find clearing and advance moves to unreconcile first
r_lines = clearing.account_move_id.line_ids.filtered(
lambda l: l.account_id.id in accounts.ids
)
md_lines = r_lines.mapped("matched_debit_ids.debit_move_id")
mc_lines = r_lines.mapped("matched_credit_ids.credit_move_id")
# Removes reconcile
(r_lines + md_lines + mc_lines).remove_move_reconcile()
# Re-reconcile again this time with the wht_tax JV
wht_lines = move.line_ids.filtered(
lambda l: l.account_id.id in accounts.ids
)
(r_lines + wht_lines + md_lines + mc_lines).reconcile()

def _assign_tax_invoice(self):
""" Use Bill Reference and Date from Expense Line as Tax Invoice """
# Expense Taxes
for move in self:
for tax_invoice in move.tax_invoice_ids.filtered(
lambda l: l.tax_line_id.type_tax_use == "purchase"
Expand All @@ -25,4 +55,3 @@ def _post(self, soft=True):
bill_partner = tax_invoice.move_line_id.expense_id.bill_partner_id
if bill_partner:
tax_invoice.write({"partner_id": bill_partner.id})
return super()._post(soft)
96 changes: 96 additions & 0 deletions l10n_th_account_tax_expense/models/hr_expense_sheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
from odoo import _, fields, models
from odoo.exceptions import UserError


class HrExpenseSheet(models.Model):
_inherit = "hr.expense.sheet"

wht_move_id = fields.Many2one(
comodel_name="account.move",
string="Withholding Tax JV",
ondelete="set null",
copy=False,
readonly=True,
help="For case clear advance, a JV can be created to record withholding tax",
)
has_wht = fields.Boolean(
compute="_compute_has_wht",
)

def _compute_has_wht(self):
for rec in self:
rec.has_wht = len(rec.expense_line_ids.mapped("wht_tax_id")) > 0

def action_create_withholding_tax_entry(self):
"""From expense sheet with WHT lines, this action
helps create new JV with default withholding entries"""
active_id = self.env.context.get("active_id")
sheet = self.env["hr.expense.sheet"].browse(active_id)
sheet.ensure_one()
# Validation
if sheet.state not in ("done", "post"):
raise UserError(_("Only posted or paid expense report can create JV"))
if sheet.wht_move_id:
raise UserError(_("Already created withholding tax JV"))
# Window action
action = self.env.ref("account.action_move_journal_line")
result = action.sudo().read()[0]
view = self.env.ref("account.view_move_form", False)
result["views"] = [(view and view.id or False, "form")]
# Create wht JV
move_vals = sheet._prepare_withholding_tax_entry()
move = self.env["account.move"].create(move_vals)
sheet.wht_move_id = move
result["res_id"] = move.id
return result

def _prepare_withholding_tax_entry(self):
self.ensure_one()
# Prepare Dr. Advance, Cr. WHT lines
line_vals_list = []
move_lines = self.account_move_id.mapped("line_ids")
# Cr. WHT Lines
wht_move_lines = move_lines.filtered("wht_tax_id")
currency = self.env.company.currency_id
deduction_list, amount_deduct = wht_move_lines._prepare_deduction_list(
currency=currency
)
for deduction in deduction_list:
line_vals_list.append(
{
"name": deduction["name"],
"amount_currency": -deduction["amount"],
"currency_id": currency.id,
"debit": 0.0,
"credit": deduction["amount"],
"partner_id": deduction["partner_id"],
"account_id": deduction["account_id"],
"wht_tax_id": deduction["wht_tax_id"],
"tax_base_amount": deduction["wht_amount_base"],
}
)
accounts = move_lines.mapped("account_id").filtered("reconcile")
# Dr. Reconcilable Account (i.e., AP, Advance)
partner_id = self.employee_id.sudo().address_home_id.commercial_partner_id.id
for account in accounts:
line_vals_list.append(
{
"name": account.name,
"amount_currency": amount_deduct,
"currency_id": currency.id,
"debit": amount_deduct, # Sum of all credit
"credit": 0.0,
"partner_id": partner_id,
"account_id": account.id,
}
)
amount_deduct = 0.0
# Create JV
move_vals = {
"move_type": "entry",
"ref": self.account_move_id.display_name,
"line_ids": [(0, 0, line_vals) for line_vals in line_vals_list],
}
return move_vals
4 changes: 3 additions & 1 deletion l10n_th_account_tax_expense/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
This module make sure that Expense will also comply with Tax Invoice concept of l10n_th_tax.
This module make sure that Expense will also comply with Tax Invoice concept of l10n_th_account_tax.

In case expense has VAT,

* Expense's Bill Reference and Date will be used as Tax Invoice Number and Tax Invoice Date
* Without Bill Reference, Post Journal Entry is not allowed

This module also provide addition step to create withholding tax journal entry.
18 changes: 18 additions & 0 deletions l10n_th_account_tax_expense/views/hr_expense_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<field name="model">hr.expense.sheet</field>
<field name="inherit_id" ref="hr_expense.view_hr_expense_sheet_form" />
<field name="arch" type="xml">
<field name="account_move_id" position="after">
<field
name="wht_move_id"
attrs="{'invisible': [('wht_move_id', '=', False)]}"
/>
</field>
<xpath
expr="//field[@name='expense_line_ids']/tree/field[@name='tax_ids']"
position="after"
Expand Down Expand Up @@ -64,4 +70,16 @@
</xpath>
</field>
</record>

<record id="action_create_withholding_entry" model="ir.actions.server">
<field name="name">Create Withholding Tax JV</field>
<field name="model_id" ref="hr_expense.model_hr_expense_sheet" />
<field name="binding_model_id" ref="hr_expense.model_hr_expense_sheet" />
<field name="binding_view_types">form</field>
<field name="state">code</field>
<field name="code">
action = env['hr.expense.sheet'].action_create_withholding_tax_entry()
</field>
</record>

</odoo>

0 comments on commit a3cd6b3

Please sign in to comment.