| 1 |
from odoo import models, fields, _ |
| 2 |
from odoo.exceptions import UserError |
| 3 |
|
| 4 |
|
| 5 |
class PosMakeInvoice(models.TransientModel): |
| 6 |
_name = 'pos.make.invoice' |
| 7 |
_description = 'Multiple order invoice creation' |
| 8 |
|
| 9 |
consolidated_billing = fields.Boolean( |
| 10 |
string="Consolidated Billing", default=True, |
| 11 |
help="Create one invoice for all orders related to same customer and same invoicing address" |
| 12 |
) |
| 13 |
count = fields.Integer(string="Order Count", compute='_compute_order_count') |
| 14 |
|
| 15 |
def _compute_order_count(self): |
| 16 |
for wizard in self: |
| 17 |
wizard.count = len(self.env.context.get('active_ids', [])) |
| 18 |
|
| 19 |
def action_create_invoices(self): |
| 20 |
self.ensure_one() |
| 21 |
selected_orders = self.env['pos.order'].browse(self.env.context.get('active_ids')).filtered(lambda o: o.invoice_status == 'to_invoice' and o.state != 'draft' and o.state != 'cancel') |
| 22 |
if not selected_orders: |
| 23 |
raise UserError(_("No valid orders were selected. No new invoices could be generated")) |
| 24 |
|
| 25 |
is_single_order = len(selected_orders) == 1 |
| 26 |
|
| 27 |
invalid_refund_orders = selected_orders.filtered(lambda o: o.refunded_order_id.account_move) |
| 28 |
if (not is_single_order) and invalid_refund_orders: |
| 29 |
# Normally it can't be encountered because when paying a refund order, |
| 30 |
# if the original order is invoiced, the refund order is required by the UI to be invoiced. |
| 31 |
order_names = "\n".join([f"{o.name} ({o.pos_reference})" for o in invalid_refund_orders]) |
| 32 |
raise UserError(_("The following refund orders can't be part of a consolidated invoice because they refunded invoiced orders. Each refund order should be handled separately.\n\n%s", order_names)) |
| 33 |
|
| 34 |
invoices = self.env['account.move'] |
| 35 |
|
| 36 |
if not self.consolidated_billing or len(selected_orders) == 1: |
| 37 |
for order in selected_orders: |
| 38 |
invoices |= order._generate_pos_order_invoice() |
| 39 |
else: |
| 40 |
configs = selected_orders.config_id |
| 41 |
partners = selected_orders.partner_id |
| 42 |
some_order_has_no_partner = any(not o.partner_id for o in selected_orders) |
| 43 |
if len(configs) == 1 and len(partners) == 1 and some_order_has_no_partner: |
| 44 |
# When all the orders belong to one config and there is only one customer but some orders have no partner, |
| 45 |
# we can proceed but we ask the user if we proceed by setting that one customer to all the orders. |
| 46 |
return { |
| 47 |
'name': _('Warning'), |
| 48 |
'view_mode': 'form', |
| 49 |
'view_id': self.env.ref('point_of_sale.view_confirm_action_wizard').id, |
| 50 |
'res_model': 'pos.confirmation.wizard', |
| 51 |
'target': 'new', |
| 52 |
'type': 'ir.actions.act_window', |
| 53 |
'context': {'orders': selected_orders.ids, 'dialog_size': 'medium'}, |
| 54 |
} |
| 55 |
|
| 56 |
grouped_orders = [] |
| 57 |
for config, config_orders in selected_orders.grouped('config_id').items(): |
| 58 |
for partner, partner_orders in config_orders.grouped('partner_id').items(): |
| 59 |
if not partner: |
| 60 |
raise UserError(_("Kindly ensure that each order contains a customer.")) |
| 61 |
|
| 62 |
for user, user_orders in partner_orders.grouped('user_id').items(): |
| 63 |
for fiscal_position, fiscal_position_orders in user_orders.grouped('fiscal_position_id').items(): |
| 64 |
grouped_orders.append(((config, partner, user, fiscal_position), fiscal_position_orders)) |
| 65 |
|
| 66 |
for _key, orders in grouped_orders: |
| 67 |
invoices |= orders._generate_pos_order_invoice() |
| 68 |
|
| 69 |
if invoices: |
| 70 |
return selected_orders.action_view_invoice() |