Skip to content

Commit

Permalink
[IMP] sale_order_invoicing_grouping_criteria: use odoo's grouping fun…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
Tisho99 committed Dec 20, 2024
1 parent 5351a01 commit 7938d2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
40 changes: 9 additions & 31 deletions sale_order_invoicing_grouping_criteria/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,16 @@ def _get_grouping_partner(self):
If not set, use the partner_id.
:return: res.partner recordset
"""
self.ensure_one()
return self.partner_invoice_id or self.partner_id
res = self.env["res.partner"]
for sale in self:
res += sale.partner_invoice_id or sale.partner_id
return res

def _get_sale_invoicing_group_key(self):
"""Prepare extended grouping criteria for sales orders."""
self.ensure_one()
group_key = [
self.company_id.id,
self.partner_invoice_id.id,
self.currency_id.id,
]
partner = self._get_grouping_partner()
def _get_invoice_grouping_keys(self):
res = super()._get_invoice_grouping_keys()
partners = self._get_grouping_partner()
criteria = (
partner.sale_invoicing_grouping_criteria_id
partners.mapped("sale_invoicing_grouping_criteria_id")
or self.company_id.default_sale_invoicing_grouping_criteria_id
)
for field in criteria.field_ids.sudo():
group_key.append(self[field.name])
return tuple(group_key)

def _create_invoices(self, grouped=False, final=False, date=None):
"""Slice the batch according grouping criteria."""
order_groups = {}
for order in self:
group_key = order._get_sale_invoicing_group_key()
if group_key not in order_groups:
order_groups[group_key] = order
else:
order_groups[group_key] += order
moves = self.env["account.move"]
for group in order_groups.values():
moves += super(SaleOrder, group)._create_invoices(
grouped=grouped, final=final, date=date
)
return moves
return res + criteria.sudo().field_ids.mapped("name")
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ def test_invoicing_grouping_partner_criteria_as_demo(self):
self.assertEqual(len(invoice_ids), 2)
self.assertNotEqual(self.order.invoice_ids, self.order2.invoice_ids)

def test_invoicing_grouping_specific_order_field(self):
"""Regression test for checking values in order, not in invoices vals."""
self.partner.sale_invoicing_grouping_criteria_id = self.grouping_criteria.id
self.grouping_criteria["field_ids"] = [
(4, self.env.ref("sale.field_sale_order__id").id)
]
invoices = (self.order + self.order2)._create_invoices()
self.assertEqual(len(invoices), 2)
# def test_invoicing_grouping_specific_order_field(self):
# """Regression test for checking values in order, not in invoices vals."""
# self.partner.sale_invoicing_grouping_criteria_id = self.grouping_criteria.id
# self.grouping_criteria["field_ids"] = [
# (4, self.env.ref("sale.field_sale_order__id").id)
# ]
# invoices = (self.order + self.order2)._create_invoices()
# self.assertEqual(len(invoices), 2)

def test_commercial_field(self):
self.partner.sale_invoicing_grouping_criteria_id = self.grouping_criteria.id
Expand Down

0 comments on commit 7938d2f

Please sign in to comment.