Skip to content

Commit

Permalink
temp rebasing PR 168 (6b0ab72)
Browse files Browse the repository at this point in the history
  • Loading branch information
roboadhoc committed Nov 24, 2023
2 parents 12b61bf + 6b0ab72 commit c5ce719
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 98 deletions.
1 change: 1 addition & 0 deletions account_invoice_tax/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_invoice_tax,access_account_invoice_tax,account_invoice_tax.model_account_invoice_tax,account.group_account_invoice,1,1,1,0
access_account_invoice_tax_line,access_account_invoice_tax_line,account_invoice_tax.model_account_invoice_tax_line,account.group_account_invoice,1,1,1,1
2 changes: 1 addition & 1 deletion account_invoice_tax/static/src/xml/tax_totals.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<t t-out="props.taxGroup.formatted_tax_group_amount"/>
</span>
</span>
</xpath>
</xpath>
</t>
</templates>
3 changes: 1 addition & 2 deletions account_invoice_tax/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

<div colspan="2" class="oe_right oe_edit_only" attrs="{'invisible': ['|', ('state', '!=', 'draft'), ('move_type', 'not in', ('in_invoice', 'in_refund', 'in_receipt'))]}">
<strong>TAX: </strong>
<button name="%(account_invoice_tax.action_view_account_invoice_tax)d" type="action" string="Add/update" title="Add/update fixed Tax" class="oe_link" context="{'move_type': move_type, 'type_operation': 'add'}"/>
<button name="%(account_invoice_tax.action_view_account_invoice_tax)d" type="action" string="Remove" title="Remove Tax" class="oe_link" context="{'move_type': move_type, 'type_operation': 'remove'}"/>
<button name="%(account_invoice_tax.action_view_account_invoice_tax)d" type="action" string="Add/update" title="Add/update fixed Tax" class="oe_link" context="{'move_type': move_type}"/>
</div>
</field>
</field>
Expand Down
151 changes: 67 additions & 84 deletions account_invoice_tax/wizards/account_invoice_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,44 @@ class AccountInvoiceTax(models.TransientModel):
_description = 'Account Invoice Tax'

move_id = fields.Many2one('account.move', required=True)
type_operation = fields.Selection([('add', 'Add Tax'), ('remove', 'Remove Tax')])
tax_id = fields.Many2one('account.tax', required=True)
amount = fields.Float()
new_tax = fields.Boolean()
company_id = fields.Many2one(related='move_id.company_id')
tax_line_ids = fields.One2many('account.invoice.tax_line', 'invoice_tax_id')

@api.model
def default_get(self, fields):
res = super().default_get(fields)
move_ids = self.env['account.move'].browse(self.env.context['active_ids']) if self.env.context.get(
'active_model') == 'account.move' else self.env['account.move']
res['move_id'] = move_ids[0].id if move_ids else False
res['type_operation'] = self.env.context.get('type_operation', 'add')
return res
lines = []
for line in move_ids[0].line_ids.filtered(lambda x: x.tax_line_id):
lines.append(Command.create({'tax_id': line.tax_line_id.id ,'amount': line.amount_currency, 'new_tax':False}))
res['tax_line_ids'] = lines

@api.onchange('move_id')
def onchange_move_id(self):
taxes = self.env['account.tax'].search([]) if self.type_operation == 'add' else self.move_id.mapped(
'invoice_line_ids.tax_ids')
leaf = [('id', 'in', taxes.ids), ('type_tax_use', '=', 'purchase') ,('company_id', '=', self.move_id.company_id.id)]
if self.env.context.get('group_id'):
leaf += [('tax_group_id', '=', self.env.context.get('group_id'))]
return {'domain': {'tax_id': leaf}}

@api.onchange('tax_id')
def onchange_tax_id(self):
tax_line = self.move_id.line_ids.filtered(lambda x: x.tax_line_id and x.tax_line_id.id == self.tax_id.id)
if tax_line:
self.amount = abs(tax_line.amount_currency)
self.new_tax = False
else:
self.new_tax = True

def _get_amount_updated_values(self):
debit = credit = 0
if self.move_id.move_type == "in_invoice":
if self.amount > 0:
debit = self.amount
elif self.amount < 0:
credit = -self.amount
else: # For refund
if self.amount > 0:
credit = self.amount
elif self.amount < 0:
debit = -self.amount

# If multi currency enable
move_currency = self.move_id.currency_id
company_currency = self.move_id.company_currency_id
if move_currency and move_currency != company_currency:
return {'amount_currency': self.amount if debit else -self.amount}

return {'debit': debit, 'credit': credit, 'balance': self.amount if debit else -self.amount}

def add_tax_and_new(self):
self.add_tax()
return {'type': 'ir.actions.act_window',
'name': _('Edit tax lines'),
'res_model': self._name,
'target': 'new',
'view_mode': 'form',
'context': self._context,
}

def add_tax(self):
""" Add the given taxes to all the invoice line of the current invoice """
return res

def action_update_tax(self):
move = self.move_id
fixed_taxes_bu = {
line.tax_line_id: {
'amount_currency': line.amount_currency,
'debit': line.debit,
'credit': line.credit,
} for line in move.line_ids.filtered(lambda x: x.tax_repartition_line_id.tax_id.amount_type == 'fixed')}
if self.new_tax:
# al crear la linea de impuesto no queda balanceado porque no recalcula las lineas AP/AR
# por eso pasamos check_move_validity
container = {'records': move}
with move.with_context(check_move_validity=False)._check_balanced(container):
with move._sync_dynamic_lines(container):
move.invoice_line_ids.filtered(lambda x: x.display_type == 'product').write({'tax_ids': [Command.link(self.tax_id.id)]})
} for line in self.move_id.line_ids.filtered(lambda x: x.tax_repartition_line_id.tax_id.amount_type == 'fixed')}

active_tax = self.tax_line_ids.mapped('tax_id')
origin_tax = self.move_id.line_ids.filtered(lambda x: x.tax_line_id).mapped('tax_repartition_line_id.tax_id')
to_remove_tax = origin_tax - active_tax
to_add_tax = active_tax - origin_tax
container = {'records':move, 'self':move}

# change tax list
with move.with_context(check_move_validity=False)._check_balanced(container):
with move._sync_dynamic_lines(container):
if to_remove_tax:
move.invoice_line_ids.filtered(lambda x: x.display_type == 'product').write({'tax_ids': [Command.unlink(tax_id.id) for tax_id in to_remove_tax]})
if to_add_tax:
move.invoice_line_ids.filtered(lambda x: x.display_type == 'product').write({'tax_ids': [Command.link(tax_id.id) for tax_id in to_add_tax]})

# set amount in the new created tax line. En este momento si queda balanceado y se ajusta la linea AP/AR
container = {'records': move}
Expand All @@ -97,25 +55,50 @@ def add_tax(self):
for tax_line in move.line_ids.filtered(
lambda x: x.tax_repartition_line_id.tax_id in fixed_taxes_bu and x.tax_repartition_line_id.tax_id.amount_type == 'fixed'):
tax_line.write(fixed_taxes_bu.get(tax_line.tax_line_id))
for tax_line_id in self.tax_line_ids:
# seteamos valor al impuesto segun lo que puso en el wizard
line_with_tax = move.line_ids.filtered(lambda x: x.tax_line_id == tax_line_id.tax_id)
line_with_tax.write(tax_line_id._get_amount_updated_values())

# seteamos valor al impuesto segun lo que puso en el wizard
line_with_tax = move.line_ids.filtered(lambda x: x.tax_line_id == self.tax_id)
line_with_tax.write(self._get_amount_updated_values())

def remove_tax(self):
""" Remove the given taxes to all the invoice line of the current invoice """
move_id = self.move_id.with_context(check_move_validity=False)
fixed_taxes_bu = {
line.tax_line_id: {
'amount_currency': line.amount_currency,
'debit': line.debit,
'credit': line.credit,
} for line in move_id.line_ids.filtered(lambda x: x.tax_repartition_line_id.tax_id.amount_type == 'fixed')}
container = {'records': move_id, 'self': move_id}
with move_id._check_balanced(container):
with move_id._sync_dynamic_lines(container):
move_id.invoice_line_ids.filtered(lambda x: x.display_type == 'product').write({'tax_ids': [Command.unlink(self.tax_id.id)]})
for tax_line in move_id.line_ids.filtered(
lambda x: x.tax_repartition_line_id.tax_id in fixed_taxes_bu and x.tax_repartition_line_id.tax_id.amount_type == 'fixed'):
tax_line.write(fixed_taxes_bu.get(tax_line.tax_line_id))
def add_tax_and_new(self):
self.add_tax()
return {'type': 'ir.actions.act_window',
'name': _('Edit tax lines'),
'res_model': self._name,
'target': 'new',
'view_mode': 'form',
'context': self._context,
}


class AccountInvoiceTax(models.TransientModel):

_name = 'account.invoice.tax_line'
_description = 'Account Invoice Tax line'

invoice_tax_id = fields.Many2one('account.invoice.tax')
tax_id = fields.Many2one('account.tax', required=True)
amount = fields.Float()
new_tax = fields.Boolean(default=True)

def _get_amount_updated_values(self):
debit = credit = 0
if self.invoice_tax_id.move_id.move_type == "in_invoice":
if self.amount > 0:
debit = self.amount
elif self.amount < 0:
credit = -self.amount
else: # For refund
if self.amount > 0:
credit = self.amount
elif self.amount < 0:
debit = -self.amount

# If multi currency enable
move_currency = self.invoice_tax_id.move_id.currency_id
company_currency = self.invoice_tax_id.move_id.company_currency_id
if move_currency and move_currency != company_currency:
return {'amount_currency': self.amount if debit else -self.amount}

return {'debit': debit, 'credit': credit, 'balance': self.amount if debit else -self.amount}
20 changes: 9 additions & 11 deletions account_invoice_tax/wizards/account_invoice_tax_view.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>

<record id="view_account_invoice_tax" model="ir.ui.view">
<field name="name">account.invoce.tax.form</field>
<field name="model">account.invoice.tax</field>
<field name="arch" type="xml">
<form>
<h1>
<field name="type_operation" nolabel="1" colspan="2" readonly="1"/>
</h1>
<group>
<field name="company_id" invisible="1"/>
<field name="move_id" invisible="1"/>
<field name="tax_id" options="{'no_create': True, 'no_edit': True}"/>
<field name="amount" attrs="{'invisible': [('type_operation', '=', 'remove')], 'required': [('type_operation', '=', 'add')]}"/>
<field colspan="2" nolabel="1" name="tax_line_ids">
<tree decoration-info="new_tax == True" editable="bottom">
<field name="tax_id" domain="[('type_tax_use', '=', 'purchase'), ('company_id', '=', parent.company_id)]" options="{'no_create': True, 'no_edit': True}"/>
<field name="amount"/>
<field name="new_tax" invisible="True"/>
</tree>
</field>
</group>
<footer>
<button string='Add' name="add_tax" type="object" class="btn-primary" attrs="{'invisible': [('type_operation', '=', 'remove')]}"/>
<button string='Add and New' name="add_tax_and_new" type="object" class="btn-primary" attrs="{'invisible': [('type_operation', '=', 'remove')]}"/>
<button string='Remove' name="remove_tax" type="object" class="btn-primary" attrs="{'invisible': [('type_operation', '=', 'add')]}"/>
<button string='Update' name="action_update_tax" type="object" class="btn-primary" />
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_view_account_invoice_tax" model="ir.actions.act_window">
<field name="name">Edit tax lines</field>
<field name="res_model">account.invoice.tax</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

</odoo>

0 comments on commit c5ce719

Please sign in to comment.