Skip to content

Commit

Permalink
[16.0] Add Avatax retail price
Browse files Browse the repository at this point in the history
  • Loading branch information
atchuthan authored and ChrisOForgeFlow committed Dec 22, 2023
1 parent 08a2f9d commit 1a9ab77
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 2 deletions.
2 changes: 1 addition & 1 deletion account_avatax_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Avalara Avatax Certified Connector
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9f82d88370f901cad0b42bfe17b17b40970e49fe2b0e36430c84a0aee9202a67
!! source digest: sha256:eb0092e675d6d627be98b46de656c619eb9791bae8159d524f3888d1aef971a8
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"views/account_move_view.xml",
"views/account_tax_view.xml",
"views/account_fiscal_position_view.xml",
"views/retail_group.xml",
],
"demo": ["demo/avatax_demo.xml"],
"images": ["static/description/avatax_icon.png"],
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from . import account_fiscal_position
from . import account_tax
from . import res_company
from . import retail_group
from . import avatax_rest_api
83 changes: 83 additions & 0 deletions account_avatax_oca/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ def _avatax_compute_tax(self, commit=False):
)
rate = round(tax_calculation * 100, 4)
tax = Tax.get_avalara_tax(rate, doc_type)
# Added retail delivery fee
fixed_tax_amount = tax_result_line["tax"]
retail_delivery_fee_tax = line.retail_delivery_fee_id.tax_ids
retail_delivery_fee_tax_match = retail_delivery_fee_tax.filtered(
lambda t: t.amount == fixed_tax_amount
)
# setting Retail Delivery Fee from avatax salestax
if retail_delivery_fee_tax_match:
tax = retail_delivery_fee_tax_match
# tax amount doesn't match means creating copy record with new amount
elif retail_delivery_fee_tax:
retail_delivery_fee_tax = retail_delivery_fee_tax[0]
vals = {
"amount": fixed_tax_amount,
"name": f"{retail_delivery_fee_tax.name} - {fixed_tax_amount}",
}
tax = retail_delivery_fee_tax.sudo().copy(default=vals)
if tax and tax not in line.tax_ids:
line_taxes = line.tax_ids.filtered(lambda x: not x.is_avatax)
taxes_to_set.append((index, line_taxes | tax))
Expand All @@ -269,6 +286,68 @@ def _avatax_compute_tax(self, commit=False):

return tax_result

def add_retail_product(self):
invoice_line = self.env["account.move.line"].sudo()
avatax_config = self.company_id.get_avatax_config_company()
if avatax_config:
retail_group = avatax_config.retail_group_ids.filtered(
lambda r: r.country_id.code == self.tax_address_id.country_id.code
and r.state_id.code == self.tax_address_id.state_id.code
)
if retail_group:
retail_group = retail_group[0]
invoice_line = self.invoice_line_ids.filtered(
lambda l: l.retail_delivery_fee
and l.product_id == retail_group.product_id
)
if not invoice_line:
invoice_line = invoice_line.with_context(
default_move_type=self.move_type,
journal_id=self.journal_id.id,
default_partner_id=self.commercial_partner_id.id,
default_currency_id=self.currency_id.id,
)
temp_invoice_line = invoice_line.new(
{
"product_id": retail_group.product_id.id,
"price_unit": retail_group.amount,
"retail_delivery_fee_id": retail_group.id,
"retail_delivery_fee": True,
"move_id": self.id,
}
)
for method in temp_invoice_line._onchange_methods.get(
"product_id", ()
):
method(temp_invoice_line)
vals = temp_invoice_line._convert_to_write(temp_invoice_line._cache)
vals["price_unit"] = retail_group.amount
lines_before_create = self.invoice_line_ids
self.write({"invoice_line_ids": [(0, 0, vals)]})
invoice_line = self.invoice_line_ids - lines_before_create
else:
invoice_line_to_edit = invoice_line.filtered(
lambda o: o.price_unit != retail_group.amount
)
if invoice_line_to_edit:
self.write(
{
"invoice_line_ids": [
(
1,
invoice_line_to_edit.id,
{"price_unit": retail_group.amount},
)
]
}
)
invoice_to_unlink = (
self.invoice_line_ids.filtered(lambda o: o.retail_delivery_fee)
- invoice_line
)
if invoice_to_unlink:
self.write({"invoice_line_ids": [(2, invoice_to_unlink[0].id)]})

# Same as v13
def avatax_compute_taxes(self, commit=False):
"""
Expand All @@ -281,6 +360,8 @@ def avatax_compute_taxes(self, commit=False):
and invoice.fiscal_position_id.is_avatax
and (invoice.state == "draft" or commit)
):
if not commit:
invoice.add_retail_product()
invoice._avatax_compute_tax(commit=commit)
return True

Expand Down Expand Up @@ -425,6 +506,8 @@ def create(self, vals_list):
class AccountMoveLine(models.Model):
_inherit = "account.move.line"

retail_delivery_fee = fields.Boolean()
retail_delivery_fee_id = fields.Many2one("retail.group")
avatax_amt_line = fields.Float(string="AvaTax Line", copy=False)

def _get_avatax_amount(self, qty=None):
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/models/account_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def _get_avalara_tax_domain(self, tax_rate, doc_type):
"=",
self.env.company.id,
),
("amount_type", "=", "percent"),
]

@api.model
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/models/avalara_salestax.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def _get_avatax_supported_countries(self):
help="Uncheck the this field to show exemption fields on SO/Invoice form view. "
"Also, it will show Tax based on shipping address button",
)
retail_group_ids = fields.Many2many("retail.group", string="Retail Delivery Fee")
# TODO: add option to Display Prices with Tax Included
# Enabled the tax inclusive flag in the GetTax Request.

Expand Down
27 changes: 27 additions & 0 deletions account_avatax_oca/models/retail_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from odoo import fields, models


class RetailGroupState(models.Model):
_name = "retail.group"
_description = "Retail Delivery Fee"

country_id = fields.Many2one(
"res.country",
required=True,
)
state_id = fields.Many2one(
"res.country.state",
required=True,
)
amount = fields.Float()
product_id = fields.Many2one(
"product.product",
string="Product",
required=True,
)
tax_ids = fields.Many2many(
"account.tax",
string="Taxes",
domain=[("amount_type", "=", "fixed")],
required=True,
)
2 changes: 2 additions & 0 deletions account_avatax_oca/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ access_exemption_code_employee,exemption.code.employee,model_exemption_code,base
access_avalara_salestax_ping,avalara_salestax_ping,model_avalara_salestax_ping,account.group_account_manager,1,1,1,1
access_avalara_salestax_address_validate,avalara_salestax_address_validate,model_avalara_salestax_address_validate,account.group_account_manager,1,1,1,1
access_avalara_salestax_getcompany,avalara_salestax_getcompany,model_avalara_salestax_getcompany,account.group_account_manager,1,1,1,1
access_retail_group,retail_group,model_retail_group,base.group_user,1,1,1,1
access_retail_group_for_all_groups,retail_group,model_retail_group,,1,0,0,0
2 changes: 1 addition & 1 deletion account_avatax_oca/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Avalara Avatax Certified Connector</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9f82d88370f901cad0b42bfe17b17b40970e49fe2b0e36430c84a0aee9202a67
!! source digest: sha256:eb0092e675d6d627be98b46de656c619eb9791bae8159d524f3888d1aef971a8
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/account-fiscal-rule/tree/16.0/account_avatax_oca"><img alt="OCA/account-fiscal-rule" src="https://img.shields.io/badge/github-OCA%2Faccount--fiscal--rule-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-fiscal-rule-16-0/account-fiscal-rule-16-0-account_avatax_oca"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/account-fiscal-rule&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/sales-tax-badge/"><img alt="Sales Tax Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/SalesTax.png" style="width: 250px;" /></a> <a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/refunds-credit-memos-badge/"><img alt="Refunds Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/Refunds.png" style="width: 250px;" /></a> <a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/address-validation-badge/"><img alt="Address Validation Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/AddressValidation.png" style="width: 250px;" /></a></p>
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/views/avalara_salestax_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<group>
<field name="request_timeout" />
<field name="country_ids" widget="many2many_tags" />
<field name="retail_group_ids" />
</group>
</page>
</notebook>
Expand Down
36 changes: 36 additions & 0 deletions account_avatax_oca/views/retail_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<odoo>
<record id="retail_group_tree_view" model="ir.ui.view">
<field name="name">retail.group.tree</field>
<field name="model">retail.group</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree>
<field name="country_id" />
<field name="state_id" />
<field name="product_id" />
<field name="amount" />
<field name="tax_ids" widget="many2many_tags" />
</tree>
</field>
</record>
<record id="retail_group_form_view" model="ir.ui.view">
<field name="name">retail.group.form</field>
<field name="model">retail.group</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form>
<group>
<field name="country_id" />
<field name="state_id" />
<field name="product_id" />
<field name="amount" />
<field
name="tax_ids"
widget="many2many_tags"
context="{'default_amount_type': 'fixed'}"
/>
</group>
</form>
</field>
</record>
</odoo>
Loading

0 comments on commit 1a9ab77

Please sign in to comment.