Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][14.0][IMP] account_invoice_batches: report intermediary service #426

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions account_invoice_batches/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"l10n_es_facturae",
"account_invoice_report_service",
"sale_order_invoicing_grouping_criteria",
"account_move_service",
Copy link
Member

@eantones eantones Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot make this dependent, we have to allow having batches without move service.

"queue_job",
],
"data": [
Expand Down
5 changes: 5 additions & 0 deletions account_invoice_batches/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ class ResCompany(models.Model):
comodel_name="mail.template",
domain=[("model_id", "=", "account.move")],
)
report_intermediary_service_id = fields.Many2one(
string="Default Report for Intermediary Service Partner",
comodel_name="ir.actions.report",
domain=[("model", "=", "account.move")],
)
4 changes: 4 additions & 0 deletions account_invoice_batches/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class ResConfigSettings(models.TransientModel):
related="company_id.invoice_batch_sending_email_template_id",
readonly=False,
)
report_intermediary_service_id = fields.Many2one(
related="company_id.report_intermediary_service_id",
readonly=False,
)
19 changes: 19 additions & 0 deletions account_invoice_batches/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@
<field name="invoice_batch_sending_email_template_id" />
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane" />
<div class="o_setting_right_pane">
<label
for="report_intermediary_service_id"
string="Default Report for Intermediary Service Partner"
/>
<span
class="fa fa-lg fa-building-o"
title="Values set here are company-specific."
groups="base.group_multi_company"
/>
<div
class="text-muted"
>Default report that will be used to process billing batches for Service Intermediary Clients.
</div>
<field name="report_intermediary_service_id" />
</div>
</div>
</div>
</div>
</field>
Expand Down
57 changes: 52 additions & 5 deletions account_invoice_batches/wizard/account_invoice_batch_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import base64
import io
import logging

from PyPDF2 import PdfFileReader

from odoo import _, fields, models
from odoo.exceptions import UserError

Expand Down Expand Up @@ -152,8 +155,52 @@ def process_invoices(self):
if invoices_pdf:
if not self.company_id.report_service_id:
raise UserError(_("There's no report defined on invoice company"))
report_action = self.company_id.report_service_id.report_action(
invoices_pdf
)
invoices_pdf.write({"is_move_sent": True})
return report_action
report_service = self.company_id.report_service_id
if invoices_pdf.filtered(lambda x: x.partner_id.service_intermediary):
if not self.company_id.report_intermediary_service_id:
raise UserError(
_(
"The report for service intermediary partners is not "
"defined in the configuration settings. Please go to "
"the billing configuration and specify which report "
"will be used."
)
)
report_intermediary = self.company_id.report_intermediary_service_id
pdf_streams = []
for invoice in invoices_pdf:
if invoice.partner_id.service_intermediary:
if not report_intermediary:
raise UserError(
_(
"The report for service intermediary partners is not "
"defined in the configuration settings. Please go to "
"the billing configuration and specify which report "
"will be used."
)
)
report = report_intermediary
else:
report = report_service

pdf_content, _p = report._render_qweb_pdf([invoice.id])
pdf_stream = io.BytesIO(pdf_content)
pdf_streams.append(pdf_stream)

if pdf_streams:
merged_pdf_stream = io.BytesIO()
pdf_merger = PdfFileReader(pdf_content)
for pdf_stream in pdf_streams:
pdf_merger.append(pdf_stream)
pdf_merger.write(merged_pdf_stream)
pdf_merger.close()

return {
"type": "ir.actions.report",
"report_type": "qweb-pdf",
"data": {"pdf_content": merged_pdf_stream.getvalue()},
"report_name": "Merged_Invoices",
"name": _("Merged Invoices"),
}
else:
raise UserError(_("No PDFs were generated for the selected invoices."))
Comment on lines +158 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very messy code, copy pasted, please, try to use functions and reuse code. The original code was very clean with a few branches with little code. Please clean the code

Loading