Skip to content

Commit

Permalink
[IMP] partner_invoicing_mode_weekly: Adapt code to refactor in base m…
Browse files Browse the repository at this point in the history
…odule
  • Loading branch information
rousseldenis authored and acsonefho committed Dec 31, 2024
1 parent 46888dd commit d31e8cf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 119 deletions.
65 changes: 15 additions & 50 deletions partner_invoicing_mode_weekly/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,37 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from datetime import datetime

from odoo import api, models
from odoo.fields import Datetime


class SaleOrder(models.Model):
_inherit = "sale.order"

@api.model
def cron_generate_weekly_invoices(self):
"""Cron called daily to check if weekly invoicing needs to be done."""
company_ids = self._company_weekly_invoicing_today()
company_ids = self._get_companies_weekly_invoicing()
if company_ids:
self.generate_weekly_invoices(company_ids)
self.generate_invoices(
company_ids,
invoicing_mode="weekly",
last_execution_field="invoicing_mode_weekly_last_execution",
)

@api.model
def generate_weekly_invoices(self, companies=None):
"""Generate weekly invoices for customers who require that mode.
Invoices will be generated by other jobs split for different customer
and different payment term.
"""
if not companies:
companies = self.company_id
saleorder_groups = self.read_group(
[
("invoicing_mode", "=", "weekly"),
("invoice_status", "=", "to invoice"),
("company_id", "in", companies.ids),
],
["partner_invoice_id"],
groupby=self._get_groupby_fields_for_weekly_invoicing(),
lazy=False,
)
for saleorder_group in saleorder_groups:
saleorder_ids = self.search(saleorder_group["__domain"]).ids
self.with_delay()._generate_invoices_by_partner(saleorder_ids)
companies.write({"invoicing_mode_weekly_last_execution": datetime.now()})
return saleorder_groups

@api.model
def _get_groupby_fields_for_weekly_invoicing(self):
"""Returns the sale order fields used to group them into jobs."""
return ["partner_invoice_id", "payment_term_id"]

def _generate_invoices_by_partner(self, saleorder_ids, invoicing_mode="weekly"):
"""Generate invoices for a group of sale order belonging to a customer."""
sales = (
self.browse(saleorder_ids)
.exists()
.filtered(lambda r: r.invoice_status == "to invoice")
)
if not sales:
return "No sale order found to invoice ?"
invoices = sales._create_invoices(
grouped=sales[0].partner_invoice_id.one_invoice_per_order,
final=True,
# TODO: Kept former function for backward compatibility. To remove
# in further version.
return self.generate_invoices(
companies,
invoicing_mode="weekly",
last_execution_field="invoicing_mode_weekly_last_execution",
)
for invoice in invoices:
invoice.with_delay()._validate_invoice()
return invoices

@api.model
def _company_weekly_invoicing_today(self):
def _get_companies_weekly_invoicing(self):
"""Get company ids for which today is weekly invoicing day."""
today = datetime.now()
today = Datetime.now()
domain = [
"|",
("invoicing_mode_weekly_last_execution", "<", today),
Expand Down
69 changes: 6 additions & 63 deletions partner_invoicing_mode_weekly/tests/test_invoice_mode_weekly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,20 @@
from odoo.tests.common import TransactionCase
from odoo.tools import relativedelta

from odoo.addons.partner_invoicing_mode.tests.common import CommonPartnerInvoicingMode
from odoo.addons.queue_job.tests.common import trap_jobs


class TestInvoiceModeWeekly(TransactionCase):
class TestInvoiceModeWeekly(CommonPartnerInvoicingMode, TransactionCase):

_invoicing_mode = "weekly"

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.SaleOrder = cls.env["sale.order"]
cls.cron = cls.env.ref(
"partner_invoicing_mode_weekly.ir_cron_generate_weekly_invoice"
)
cls.partner = cls.env.ref("base.res_partner_1")
cls.partner.invoicing_mode = "weekly"
cls.partner2 = cls.env.ref("base.res_partner_2")
cls.partner2.invoicing_mode = "weekly"
cls.product = cls.env.ref("product.product_delivery_01")
cls.pt1 = cls.env["account.payment.term"].create({"name": "Term Two"})
cls.pt2 = cls.env["account.payment.term"].create({"name": "Term One"})
cls.so1 = cls.env["sale.order"].create(
{
"partner_id": cls.partner.id,
"partner_invoice_id": cls.partner.id,
"partner_shipping_id": cls.partner.id,
"payment_term_id": cls.pt1.id,
"order_line": [
(
0,
0,
{
"name": "Line one",
"product_id": cls.product.id,
"product_uom_qty": 4,
"product_uom": cls.product.uom_id.id,
"price_unit": 123,
},
)
],
"pricelist_id": cls.env.ref("product.list0").id,
}
)
# Lets give the saleorder the same partner and payment terms
cls.so2 = cls.env["sale.order"].create(
{
"partner_id": cls.partner.id,
"partner_invoice_id": cls.partner.id,
"partner_shipping_id": cls.partner.id,
"payment_term_id": cls.pt1.id,
"order_line": [
(
0,
0,
{
"name": "Line one",
"product_id": cls.product.id,
"product_uom_qty": 4,
"product_uom": cls.product.uom_id.id,
"price_unit": 123,
},
)
],
"pricelist_id": cls.env.ref("product.list0").id,
}
)
cls.company = cls.so1.company_id

@classmethod
def _confirm_and_deliver(cls, sale_order):
sale_order.action_confirm()
for line in sale_order.order_line:
line.qty_delivered = line.product_uom_qty

def test_saleorder_with_different_mode_term(self):
"""Check multiple sale order one partner diverse terms."""
Expand Down Expand Up @@ -157,7 +100,7 @@ def test_cron(self):
self._confirm_and_deliver(self.so1)
self._confirm_and_deliver(self.so2)
with trap_jobs() as trap:
self.cron.method_direct_trigger()
self.env["sale.order"].cron_generate_weekly_invoices()
trap.assert_jobs_count(2)
trap.assert_enqueued_job(
self.env["sale.order"]._generate_invoices_by_partner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def test_late_invoicing_for_last_week(self):
company.invoicing_mode_weekly_last_execution = "2020-07-02"
self.assertTrue(self.env.company)
with freeze_time("2020-07-03"):
res = self.SaleOrder._company_weekly_invoicing_today()
res = self.SaleOrder._get_companies_weekly_invoicing()
self.assertTrue(res)
company.invoicing_mode_weekly_last_execution = "2020-07-04"
with freeze_time("2020-07-03"):
res = self.SaleOrder._company_weekly_invoicing_today()
res = self.SaleOrder._get_companies_weekly_invoicing()
self.assertFalse(res)

def test_no_invoicing_done_yet(self):
Expand All @@ -38,8 +38,8 @@ def test_no_invoicing_done_yet(self):
company.invoicing_mode_weekly_last_execution = None
self.assertTrue(self.env.company)
with freeze_time("2020-06-07"):
res = self.SaleOrder._company_weekly_invoicing_today()
res = self.SaleOrder._get_companies_weekly_invoicing()
self.assertFalse(res)
with freeze_time("2020-06-08"):
res = self.SaleOrder._company_weekly_invoicing_today()
res = self.SaleOrder._get_companies_weekly_invoicing()
self.assertTrue(res)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<field name="inherit_id" ref="account.res_config_settings_view_form" />
<field name="model">res.config.settings</field>
<field name="arch" type="xml">
<div id="invoicing_settings" position="after">
<h2>Invoicing Weekly</h2>
<div id="standard_invoicing_settings" position="after">
<div
class="row mt16 o_settings_container"
id="weekly_invoicing_settings"
Expand Down

0 comments on commit d31e8cf

Please sign in to comment.