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

[10.0] Merge shopinvader invoice + invoice download #407

Merged
Merged
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
10 changes: 6 additions & 4 deletions shopinvader/services/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def download(self, _id, **params):
invoice = self._get(_id)
headers, content = self._get_binary_content(invoice)
if not content:
raise MissingError(_("No image found for partner %s") % _id)
raise MissingError(_("No content found for invoice %s") % _id)
response = request.make_response(content, headers)
response.status_code = 200
return response
Expand Down Expand Up @@ -89,7 +89,7 @@ def _get_base_search_domain(self):
("typology", "=", "sale"),
]
# invoice_ids on sale.order is a computed field...
# to avoid to duplicate the logic, we search for the sale oders
# to avoid to duplicate the logic, we search for the sale orders
# and check if the invoice_id is into the list of sale.invoice_ids
sales = self.env["sale.order"].search(so_domain)
invoice_ids = sales.mapped("invoice_ids").ids
Expand All @@ -107,13 +107,15 @@ def _get_binary_content(self, invoice):
invoice_report_def = invoice.invoice_print()
report_name = invoice_report_def["report_name"]
report_type = invoice_report_def["report_type"]
content, format = self.env["ir.actions.report.xml"].render_report(
content, file_format = self.env["ir.actions.report.xml"].render_report(
res_ids=invoice.ids,
name=report_name,
data={"report_type": report_type},
)
report = self._get_report(report_name, report_type)
filename = self._get_binary_content_filename(invoice, report, format)
filename = self._get_binary_content_filename(
invoice, report, file_format
)
mimetype = mimetypes.guess_type(filename)
if mimetype:
mimetype = mimetype[0]
Expand Down
2 changes: 1 addition & 1 deletion shopinvader/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
from . import test_customer
from . import test_shopinvader_partner
from . import test_shopinvader_variant_seo_title
from . import test_shopinvader_partner_binding
from . import test_res_partner
from . import test_invoice
from . import test_shopinvader_partner_binding
22 changes: 1 addition & 21 deletions shopinvader_invoice/services/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@


class InvoiceService(Component):
_inherit = "shopinvader.abstract.mail.service"
_name = "shopinvader.invoice.service"
_usage = "invoice"
_expose_model = "account.invoice"
_inherit = "shopinvader.invoice.service"

def search(self, **params):
"""
Expand Down Expand Up @@ -110,20 +107,3 @@ def _to_json(self, invoices):
for invoice in invoices:
res.append(self._to_json_invoice(invoice))
return res

def _get_base_search_domain(self):
"""
Get every account.invoice (customer invoices or refunds)
related to current user.
If the current user is the anonymous one, it'll return an invalid
domain (to have 0 invoice as result)
:return:
"""
if self.shopinvader_backend.anonymous_partner_id == self.partner:
return [(0, "=", 1)]
return [
("partner_id", "=", self.partner.id),
("type", "in", ("out_invoice", "out_refund")),
("state", "in", ["open", "paid"]),
("shopinvader_backend_id", "=", self.shopinvader_backend.id),
]
128 changes: 56 additions & 72 deletions shopinvader_invoice/tests/test_invoice_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ class TestInvoiceService(CommonCase):
def setUp(self, *args, **kwargs):
super(TestInvoiceService, self).setUp(*args, **kwargs)
self.invoice_obj = self.env["account.invoice"]
self.partner = self.env.ref("base.res_partner_2").copy()
self.journal_obj = self.env["account.journal"]
self.register_payments_obj = self.env["account.register.payments"]
self.sale = self.env.ref("shopinvader.sale_order_2")
self.partner = self.env.ref("shopinvader.partner_1")
self.product = self.env.ref("product.product_product_4")
self.bank_journal_euro = self.journal_obj.create(
{"name": "Bank", "type": "bank", "code": "BNK67"}
)
self.payment_method_manual_in = self.env.ref(
"account.account_payment_method_manual_in"
)
self.precision = 2
with self.work_on_services(partner=self.partner) as work:
self.service = work.component(usage="invoice")
Expand Down Expand Up @@ -44,13 +53,7 @@ def _check_data_content(self, data, invoices):
:return: bool
"""
# To have them into correct order
invoices = invoices.search(
[
("id", "in", invoices.ids),
# Invoice must be paid to be in data
("state", "in", ["open", "paid"]),
]
)
invoices = invoices.search(self.service._get_base_search_domain())
self.assertEquals(len(data), len(invoices))
for current_data, invoice in zip(data, invoices):
state_label = self._get_selection_label(invoice, "state")
Expand All @@ -74,65 +77,49 @@ def _check_data_content(self, data, invoices):
self.assertEquals(current_data.get("amount_due"), invoice.residual)
return True

def _create_invoice(
self, partner=False, inv_type="out_invoice", validate=False
):
def _confirm_and_invoice_sale(self, sale, payment=True):
"""
Create a new invoice
:param partner: res.partner
:param inv_type: str
:param validate: bool
:return: stock.invoice recordset
Confirm the given SO and create an invoice.
Can also make the payment if payment parameter is True
:param sale: sale.order recordset
:param payment: bool
:return: account.invoice
"""
partner = partner or self.partner
account = self.product.categ_id.property_account_expense_categ_id
values = {
"partner_id": partner.id,
"partner_shipping_id": partner.id,
"shopinvader_backend_id": self.backend.id,
"date_invoice": fields.Date.today(),
"type": inv_type,
"invoice_line_ids": [
(
0,
False,
{
"product_id": self.product.id,
"quantity": 10,
"price_unit": 1250,
"account_id": account.id,
"name": self.product.display_name,
},
)
],
}
invoice = self.invoice_obj.create(values)
if validate:
invoice.action_invoice_open()
sale.action_confirm()
for line in sale.order_line:
line.write({"qty_delivered": line.product_uom_qty})
invoice_id = sale.action_invoice_create()
invoice = self.env["account.invoice"].browse(invoice_id)
invoice.action_invoice_open()
invoice.action_move_create()
if payment:
self._make_payment(invoice)
return invoice

def test_get_invoice_anonymous(self):
def _make_payment(self, invoice, journal=False, amount=False):
"""
Test the get on guest mode (using anonymous user).
It should not return any result, even if the anonymous user has some
invoices
:return:
Make payment for given invoice
:param invoice: account.invoice recordset
:param amount: float
:return: bool
"""
# Check first without invoice related to the anonymous user
result = self.service_guest.dispatch("search")
data = result.get("data", [])
self.assertFalse(data)
# Then create a invoice related to the anonymous user
invoice = self._create_invoice(
partner=self.backend.anonymous_partner_id, validate=True
ctx = {"active_model": invoice._name, "active_ids": invoice.ids}
wizard_obj = self.register_payments_obj.with_context(**ctx)
register_payments = wizard_obj.create(
{
"payment_date": fields.Date.today(),
"journal_id": self.bank_journal_euro.id,
"payment_method_id": self.payment_method_manual_in.id,
}
)
self.assertEquals(
invoice.partner_id, self.backend.anonymous_partner_id
)
result = self.service_guest.dispatch("search")
data = result.get("data", [])
self.assertFalse(data)
return
values = {}
if journal:
values.update({"journal_id": journal.id})
if amount:
values.update({"amount": amount})
if values:
register_payments.write(values)
register_payments.create_payment()

def test_get_invoice_logged(self):
"""
Expand All @@ -146,12 +133,12 @@ def test_get_invoice_logged(self):
data = result.get("data", [])
self.assertFalse(data)
# Then create a invoice related to partner
invoice = self._create_invoice(partner=self.service.partner)
invoice = self._confirm_and_invoice_sale(self.sale, payment=False)
self.assertEquals(invoice.partner_id, self.service.partner)
result = self.service.dispatch("search")
data = result.get("data", [])
self._check_data_content(data, invoice)
invoice.action_invoice_open()
self._make_payment(invoice)
result = self.service.dispatch("search")
data = result.get("data", [])
self._check_data_content(data, invoice)
Expand All @@ -164,16 +151,13 @@ def test_get_multi_invoice(self):
But to the second, he should have one.
:return:
"""
invoice1 = self._create_invoice(
partner=self.service.partner, validate=True
)
invoice2 = self._create_invoice(
partner=self.service.partner, validate=True
)
invoice3 = self._create_invoice(
partner=self.service.partner, validate=True
)
invoice4 = self._create_invoice(partner=self.service.partner)
sale2 = self.sale.copy()
sale3 = self.sale.copy()
sale4 = self.sale.copy()
invoice1 = self._confirm_and_invoice_sale(self.sale)
invoice2 = self._confirm_and_invoice_sale(sale2)
invoice3 = self._confirm_and_invoice_sale(sale3)
invoice4 = self._confirm_and_invoice_sale(sale4)
invoices = invoice1 | invoice2 | invoice3 | invoice4
self.assertEquals(invoice1.partner_id, self.service.partner)
self.assertEquals(invoice2.partner_id, self.service.partner)
Expand Down