Skip to content

Commit

Permalink
Merge PR #766 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Feb 5, 2025
2 parents bc97b08 + 8829e90 commit 173e4d8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
29 changes: 25 additions & 4 deletions account_invoice_inter_company/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import logging

from odoo import _, api, fields, models
Expand Down Expand Up @@ -55,7 +56,7 @@ def action_post(self):
# do not consider invoices that have already been auto-generated,
# nor the invoices that were already validated in the past
dest_company = src_invoice._find_company_from_invoice_partner()
if not dest_company or src_invoice.auto_generated:
if not dest_company:
continue
# If one of the involved companies have the intercompany setting disabled, skip
if (
Expand All @@ -68,9 +69,12 @@ def action_post(self):
src_invoice = src_invoice.with_user(intercompany_user).sudo()
else:
src_invoice = src_invoice.sudo()
src_invoice.with_company(dest_company.id).with_context(
skip_check_amount_difference=True
)._inter_company_create_invoice(dest_company)
if not src_invoice.auto_generated:
src_invoice.with_company(dest_company.id).with_context(
skip_check_amount_difference=True
)._inter_company_create_invoice(dest_company)
if src_invoice.is_sale_document():
src_invoice._attach_original_pdf_report()
# set invoice ref on supplier invoice when the customer invoice is validated
# (case where the source invoice was the supplier one)
for invoice in self.filtered(
Expand All @@ -79,6 +83,23 @@ def action_post(self):
invoice.sudo()._set_intercompany_supplier_invoice_ref()
return res

def _attach_original_pdf_report(self):
supplier_invoice = self.auto_invoice_id
if not supplier_invoice:
supplier_invoice = self.search([("auto_invoice_id", "=", self.id)], limit=1)
report = self.env.ref("account.account_invoices").with_company(self.company_id)
pdf = report._render_qweb_pdf(report.report_name, [self.id])[0]
self.env["ir.attachment"].create(
{
"name": self.name + ".pdf",
"type": "binary",
"datas": base64.b64encode(pdf),
"res_model": "account.move",
"res_id": supplier_invoice.id,
"mimetype": "application/pdf",
}
)

def _check_intercompany_product(self, dest_company):
self.ensure_one()
if dest_company.company_share_product:
Expand Down
42 changes: 42 additions & 0 deletions account_invoice_inter_company/tests/test_inter_company_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,48 @@ def test_confirm_invoice_with_native_product_rule_and_unshared_product(self):
with self.assertRaises(UserError):
self._confirm_invoice_with_product()

def test_purchase_attachement_out_invoice(self):
# Sale Invoice PDF appears as attachment in the purchase invoice form.
# From a Sale Invoice.
self.invoice_company_a.action_post()
invoice_company_b = self.account_move_obj.with_user(
self.user_company_b.id
).search([("auto_invoice_id", "=", self.invoice_company_a.id)])
invoice_b_pdf = self.env["ir.attachment"].search(
[("res_model", "=", "account.move"), ("res_id", "=", invoice_company_b.id)]
)
self.assertEqual(len(invoice_b_pdf), 1)
self.assertEqual(invoice_b_pdf.name, self.invoice_company_a.name + ".pdf")

def test_purchase_attachement_in_invoice(self):
# Sale Invoice PDF appears as attachment in the purchase invoice form.
# From a Purchase Invoice.
bill_company_a = Form(
self.account_move_obj.with_company(self.company_a.id).with_context(
default_move_type="in_invoice",
)
)
bill_company_a.partner_id = self.partner_company_b
bill_company_a.invoice_date = bill_company_a.date
with bill_company_a.invoice_line_ids.new() as line_form:
line_form.product_id = self.product_consultant_multi_company
line_form.quantity = 1
line_form.product_uom_id = self.env.ref("uom.product_uom_hour")
line_form.price_unit = 450.0
bill_company_a = bill_company_a.save()
bill_company_a.action_post()

invoice_company_b = self.account_move_obj.with_user(
self.user_company_b.id
).search([("auto_invoice_id", "=", bill_company_a.id)])
bill_a_pdf = self.env["ir.attachment"].search(
[("res_model", "=", "account.move"), ("res_id", "=", bill_company_a.id)]
)
self.assertEqual(len(bill_a_pdf), 1)
self.assertEqual(bill_a_pdf.name, invoice_company_b.name + ".pdf")
invoice_company_b.button_cancel()
invoice_company_b.action_post()

def _confirm_invoice_with_product(self):
# Confirm the invoice of company A
self.invoice_company_a.with_user(self.user_company_a.id).action_post()
Expand Down

0 comments on commit 173e4d8

Please sign in to comment.