Skip to content

Commit

Permalink
[WIP] account_payment_term_surcharge:
Browse files Browse the repository at this point in the history
cambios requeridos, se agrega campo payment_term_surcharge_invoice_auto_post
en company para auto post
  • Loading branch information
ica-adhoc committed Jan 27, 2023
1 parent e97de9c commit ff08700
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions account_payment_term_surcharge/__manifest__.py
Expand Up @@ -18,11 +18,11 @@
#
##############################################################################
{
'name': 'Term Surcharge',
'name': 'Surcharges on payment terms',
'version': "13.0.1.0.0",
'category': 'Accounting',
'sequence': 14,
'summary': 'Calculate and generate term surcharge for payments',
'summary': 'Allow to add surcharges for invoices on payment terms',
'author': 'ADHOC SA',
'website': 'www.adhoc.com.ar',
'license': 'AGPL-3',
Expand Down
29 changes: 20 additions & 9 deletions account_payment_term_surcharge/models/account_move.py
Expand Up @@ -4,15 +4,17 @@
import logging
_logger = logging.getLogger(__name__)


class AccountMove(models.Model):
_inherit = 'account.move'

def _get_payment_term_surcharges(self):
result = []
if self.invoice_payment_term_id:
for surcharge in self.invoice_payment_term_id.surcharge_ids:
result.append((surcharge.surcharge, surcharge._calculate_date(self.invoice_date)))
result.sort(key=lambda x: x[1])
result.append({'date': surcharge._calculate_date(self.invoice_date), 'surcharge': surcharge.surcharge})
# result.append((surcharge.surcharge, surcharge._calculate_date(self.invoice_date)))
result.sort(key=lambda x: x['date'])
return result

def _cron_recurring_surcharges_invoices(self):
Expand All @@ -34,21 +36,30 @@ def create_surcharges_invoices(self):
current_date = fields.Date.today()
surcharges = rec._get_payment_term_surcharges()
for surcharge in surcharges:
# surcharge -> (rate (%), date)
if surcharge[1] == current_date and current_date not in rec.debit_note_ids.mapped('invoice_date'):
if surcharge.get('date') == current_date and current_date not in rec.debit_note_ids.mapped('invoice_date'):
# si tiene un surcharge el dia de hoy, se evalua que no tenga notas de debito
# con fecha de hoy, en caso de que tenga, se corre el create_invoice
self.create_invoice(surcharge[0], surcharge[1])
rec.create_invoice(surcharge)


def create_invoice(self, surcharge, to_date):
def create_invoice(self, surcharge):
self.ensure_one()
product = self.company_id.payment_term_surcharge_product_id
debt = self.amount_residual
surcharge_percent = surcharge.get('surcharge')
to_date = surcharge.get('date')

move_vals = self._prepare_surcharge_invoice(product, debt, to_date, surcharge_percent)

move_vals = self._prepare_surcharge_invoice(product, debt, to_date, surcharge)
move = self.with_context(internal_type='debit_note').create(move_vals)

self.with_context(internal_type='debit_note').create(move_vals)
if self.company_id.payment_term_surcharge_invoice_auto_post:
try:
move.action_post()
except Exception as e:
_logger.error(
"Something went wrong creating "
"interests invoice: {}".format(e))

def prepare_info(self, to_date, debt, surcharge):
self.ensure_one()
Expand Down Expand Up @@ -89,7 +100,7 @@ def _prepare_surcharge_invoice(self, product, debt, to_date, surcharge):
'invoice_line_ids': [(0, 0, {
"product_id": product.id,
"quantity": 1.0,
"price_unit": ((surcharge / 100) + 1) * debt,
"price_unit": (surcharge / 100) * debt,
"partner_id": partner.id,
"name": product.name + '.\n' + comment,
# "analytic_account_id": self.analytic_account_id.id,
Expand Down
4 changes: 4 additions & 0 deletions account_payment_term_surcharge/models/res_company.py
Expand Up @@ -7,3 +7,7 @@ class ResCompany(models.Model):
'product.product',
'Surcharge Product',
)

payment_term_surcharge_invoice_auto_post = fields.Boolean(
default=False
)
5 changes: 5 additions & 0 deletions account_payment_term_surcharge/wizard/res_config_settings.py
Expand Up @@ -9,3 +9,8 @@ class ResConfigSettings(models.TransientModel):
related='company_id.payment_term_surcharge_product_id',
string="Producto por defecto para los recargos", readonly=False
)

payment_term_surcharge_invoice_auto_post = fields.Boolean(
related='company_id.payment_term_surcharge_invoice_auto_post',
string= 'Validad automaticamente las facturas', readonly=False
)
Expand Up @@ -19,6 +19,17 @@
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="payment_term_surcharge_invoice_auto_post"/>
</div>
<div class="o_setting_right_pane">
<label for="payment_term_surcharge_invoice_auto_post" string="Validar automaticamente facturas de recargo"/>
<div class="text-muted">
Validar automaticamente facturas de recargo
</div>
</div>
</div>
</div>
</field>
</record>
Expand Down

0 comments on commit ff08700

Please sign in to comment.