forked from OCA/l10n-italy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UPD] l10n_it_delivery_note_base: aggiornata PR OCA#4293
- Loading branch information
Showing
7 changed files
with
282 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
# Copyright (c) 2019, Link IT Europe Srl | ||
# @author: Matteo Bilotta <[email protected]> | ||
from itertools import groupby | ||
|
||
from odoo import api, fields, models | ||
from odoo.exceptions import AccessError, UserError | ||
from odoo.fields import Command | ||
|
||
from .stock_delivery_note import DOMAIN_DELIVERY_NOTE_STATES, DOMAIN_INVOICE_STATUSES | ||
|
||
|
@@ -121,13 +124,14 @@ def _generate_delivery_note_lines(self, invoice_ids): | |
invoices = self.env["account.move"].browse(invoice_ids) | ||
invoices.update_delivery_note_lines() | ||
|
||
def _create_invoices(self, grouped=False, final=False, date=None): | ||
invoice_ids = super()._create_invoices(grouped=grouped, final=final, date=date) | ||
# def _create_invoices(self, grouped=False, final=False, date=None): | ||
# invoice_ids = super()._create_invoices(grouped=grouped, | ||
# final=final, date=date) | ||
|
||
self._assign_delivery_notes_invoices(invoice_ids.ids) | ||
self._generate_delivery_note_lines(invoice_ids.ids) | ||
# self._assign_delivery_notes_invoices(invoice_ids.ids) | ||
# self._generate_delivery_note_lines(invoice_ids.ids) | ||
|
||
return invoice_ids | ||
# return invoice_ids | ||
|
||
def goto_delivery_notes(self, **kwargs): | ||
delivery_notes = self.mapped("delivery_note_ids") | ||
|
@@ -154,3 +158,138 @@ def goto_delivery_notes(self, **kwargs): | |
action = {"type": "ir.actions.act_window_close"} | ||
|
||
return action | ||
|
||
def _create_invoices(self, grouped=False, final=False, date=None): # noqa: C901 | ||
if ( | ||
not self.env["ir.config_parameter"] | ||
.sudo() | ||
.get_param("l10n_it_delivery_note.delivery_note_group_by_quantity") | ||
): | ||
invoice_ids = super()._create_invoices(grouped, final, date) | ||
self._assign_delivery_notes_invoices(invoice_ids.ids) | ||
self._generate_delivery_note_lines(invoice_ids.ids) | ||
return invoice_ids | ||
if not self.env["account.move"].check_access_rights("create", False): | ||
try: | ||
self.check_access_rights("write") | ||
self.check_access_rule("write") | ||
except AccessError: | ||
return self.env["account.move"] | ||
|
||
# 1) Create invoices. | ||
invoice_vals_list = [] | ||
invoice_item_sequence = ( | ||
10 # Incremental sequencing to keep the lines order on the invoice. | ||
) | ||
for order in self: | ||
order = order.with_company(order.company_id).with_context( | ||
lang=order.partner_invoice_id.lang | ||
) | ||
|
||
invoice_vals = order._prepare_invoice() | ||
invoiceable_lines = order._get_invoiceable_lines(final) | ||
|
||
# if not any(not line.display_type for line in invoiceable_lines): | ||
# continue | ||
|
||
invoice_line_vals = [] | ||
down_payment_section_added = False | ||
for line in invoiceable_lines.mapped("delivery_note_line_ids"): | ||
if not down_payment_section_added and line.sale_line_id.is_downpayment: | ||
# Create a dedicated section for the down payments | ||
# (put at the end of the invoiceable_lines) | ||
invoice_line_vals.append( | ||
Command.create( | ||
order._prepare_down_payment_section_line( | ||
sequence=invoice_item_sequence | ||
) | ||
), | ||
) | ||
down_payment_section_added = True | ||
invoice_item_sequence += 10 | ||
invoice_line_vals.append( | ||
Command.create( | ||
line._prepare_ddt_invoice_line(sequence=invoice_item_sequence) | ||
), | ||
) | ||
invoice_item_sequence += 10 | ||
|
||
invoice_vals["invoice_line_ids"] += invoice_line_vals | ||
invoice_vals_list.append(invoice_vals) | ||
|
||
if not invoice_vals_list and self._context.get( | ||
"raise_if_nothing_to_invoice", True | ||
): | ||
raise UserError(self._nothing_to_invoice_error_message()) | ||
|
||
# 2) Manage 'grouped' parameter: group by (partner_id, currency_id). | ||
if not grouped: | ||
new_invoice_vals_list = [] | ||
invoice_grouping_keys = self._get_invoice_grouping_keys() | ||
invoice_vals_list = sorted( | ||
invoice_vals_list, | ||
key=lambda x: [ | ||
x.get(grouping_key) for grouping_key in invoice_grouping_keys | ||
], | ||
) | ||
for _grouping_keys, invoices in groupby( | ||
invoice_vals_list, | ||
key=lambda x: [ | ||
x.get(grouping_key) for grouping_key in invoice_grouping_keys | ||
], | ||
): | ||
origins = set() | ||
payment_refs = set() | ||
refs = set() | ||
ref_invoice_vals = None | ||
for invoice_vals in invoices: | ||
if not ref_invoice_vals: | ||
ref_invoice_vals = invoice_vals | ||
else: | ||
ref_invoice_vals["invoice_line_ids"] += invoice_vals[ | ||
"invoice_line_ids" | ||
] | ||
origins.add(invoice_vals["invoice_origin"]) | ||
payment_refs.add(invoice_vals["payment_reference"]) | ||
refs.add(invoice_vals["ref"]) | ||
ref_invoice_vals.update( | ||
{ | ||
"ref": ", ".join(refs)[:2000], | ||
"invoice_origin": ", ".join(origins), | ||
"payment_reference": len(payment_refs) == 1 | ||
and payment_refs.pop() | ||
or False, | ||
} | ||
) | ||
new_invoice_vals_list.append(ref_invoice_vals) | ||
invoice_vals_list = new_invoice_vals_list | ||
if len(invoice_vals_list) < len(self): | ||
SaleOrderLine = self.env["sale.order.line"] | ||
for invoice in invoice_vals_list: | ||
sequence = 1 | ||
for line in invoice["invoice_line_ids"]: | ||
line[2]["sequence"] = SaleOrderLine._get_invoice_line_sequence( | ||
new=sequence, old=line[2]["sequence"] | ||
) | ||
sequence += 1 | ||
|
||
moves = ( | ||
self.env["account.move"] | ||
.sudo() | ||
.with_context(default_move_type="out_invoice") | ||
.create(invoice_vals_list) | ||
) | ||
if final: | ||
moves.sudo().filtered( | ||
lambda m: m.amount_total < 0 | ||
).action_switch_invoice_into_refund_credit_note() | ||
for move in moves: | ||
move.message_post_with_view( | ||
"mail.message_origin_link", | ||
values={"self": move, "origin": move.line_ids.sale_line_ids.order_id}, | ||
subtype_id=self.env["ir.model.data"]._xmlid_to_res_id("mail.mt_note"), | ||
) | ||
# return moves | ||
self._assign_delivery_notes_invoices(moves.ids) | ||
self._generate_delivery_note_lines(moves.ids) | ||
return moves |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.