Skip to content

Commit

Permalink
[IMP] account_payment_term_surcharge: add improvement in cron_recurri…
Browse files Browse the repository at this point in the history
…ng_surcharges_invoices

closes #546

X-original-commit: 92cd505
Signed-off-by: Filoquin adhoc <[email protected]>
Signed-off-by: rov-adhoc <[email protected]>
  • Loading branch information
rov-adhoc committed Aug 30, 2024
1 parent 873c4aa commit 94a80cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion account_payment_term_surcharge/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
{
'name': 'Surcharges on payment terms',
'version': "17.0.1.0.0",
'version': "17.0.1.1.0",
'category': 'Accounting',
'sequence': 14,
'summary': 'Allow to add surcharges for invoices on payment terms',
Expand All @@ -33,6 +33,7 @@
'data': [
'views/account_payment_term_view.xml',
'views/account_payment_term_surcharge_view.xml',
'views/account_move_views.xml',
'wizard/res_config_settings_views.xml',
'security/ir.model.access.csv',
'data/ir_cron_data.xml'
Expand Down
27 changes: 25 additions & 2 deletions account_payment_term_surcharge/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,48 @@ class AccountMove(models.Model):

next_surcharge_date = fields.Date(compute='_compute_next_surcharge', store=True)
next_surcharge_percent = fields.Float(compute='_compute_next_surcharge', store=True)
avoid_surcharge_invoice = fields.Boolean()

def _cron_recurring_surcharges_invoices(self, batch_size=60):
current_date = fields.Date.context_today(self)
domain = [
('next_surcharge_date', '<=', current_date),
('state', '=', 'posted'),
('payment_state', 'in', ['not_paid', 'partial'])]
('payment_state', 'in', ['not_paid', 'partial']),
('avoid_surcharge_invoice', '=', False)]
_logger.info('Running Surcharges Invoices Cron Job, pendientes por procesar %s facturas' % self.search_count(domain))
to_create = self.search(domain)
to_create[:batch_size].create_surcharges_invoices()
if len(to_create) > batch_size:
self.env.ref('account_payment_term_surcharge.cron_recurring_surcharges_invoices')._trigger()

def create_surcharges_invoices(self):
invoice_with_errors = []
for rec in self:
_logger.info(
'Creating Surcharges Invoices (id: %s, company: %s)', rec.id,
rec.company_id.name)
rec.create_surcharge_invoice(rec.next_surcharge_date, rec.next_surcharge_percent)
try:
rec.create_surcharge_invoice(rec.next_surcharge_date, rec.next_surcharge_percent)
rec.env.cr.commit()
except:
invoice_with_errors.append(rec.id)
rec.avoid_surcharge_invoice = True
_logger.error("Something went wrong creating the surcharge invoice from the invoice id:{}".format(rec.id))

message_body = _("Something went wrong creating the surcharge invoice from this invoice. Please take a look on it.")
partner_ids = rec.message_partner_ids.filtered(lambda x: not x.partner_share)
rec.message_post(
body=message_body,
partner_ids=partner_ids,
subtype_xmlid='mail.mt_note'
)
rec.env.cr.commit()
continue
if invoice_with_errors:
error_message = _("We couldn't run surcharges cron job in the following invoice: %s.") % invoice_with_errors
raise UserError(error_message)


def create_surcharge_invoice(self, surcharge_date, surcharge_percent):
self.ensure_one()
Expand Down
15 changes: 15 additions & 0 deletions account_payment_term_surcharge/views/account_move_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="view_move_form" model="ir.ui.view">
<field name="name">account.move.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<field name="to_check" position="after">
<field name="avoid_surcharge_invoice"/>
</field>
</field>
</record>
</data>
</odoo>

0 comments on commit 94a80cf

Please sign in to comment.