From 55a27be83cf65fcfce142b97f007360f443b7734 Mon Sep 17 00:00:00 2001 From: Ahmed Aly Date: Thu, 14 Dec 2017 16:18:16 +0100 Subject: [PATCH] Feedback 1216: Flag - Sammelrechnung Kundenstamm --- .../dp_custom/models/res_partner.py | 1 + ext/custom-addons/dp_custom/models/sale.py | 59 ++++++++++++++++++- .../dp_custom/views/res_partner_views.xml | 4 ++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/ext/custom-addons/dp_custom/models/res_partner.py b/ext/custom-addons/dp_custom/models/res_partner.py index d0544165..b99ecebc 100644 --- a/ext/custom-addons/dp_custom/models/res_partner.py +++ b/ext/custom-addons/dp_custom/models/res_partner.py @@ -56,6 +56,7 @@ class Partner(models.Model): active = fields.Boolean(track_visibility='onchange') portal_export_pending = fields.Boolean(string='Portal Export ausständig') date_vat_check = fields.Date(string='Datum der letzten UID-Prüfung') + collective_bill = fields.Boolean(string='Sammelrechnung') _sql_constraints = [ ('ref_uniq', 'unique(ref)', 'Die Interne Referenz muss eindeutig sein'), diff --git a/ext/custom-addons/dp_custom/models/sale.py b/ext/custom-addons/dp_custom/models/sale.py index 8532fd13..1f7ad61f 100644 --- a/ext/custom-addons/dp_custom/models/sale.py +++ b/ext/custom-addons/dp_custom/models/sale.py @@ -21,7 +21,8 @@ from datetime import datetime from odoo import api, fields, models, _ -from odoo.exceptions import ValidationError +from odoo.tools import float_is_zero +from odoo.exceptions import ValidationError, UserError from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT @@ -207,6 +208,62 @@ class SaleOrder(models.Model): return res + @api.multi + def action_invoice_create(self, grouped=False, final=False): + inv_obj = self.env['account.invoice'] + precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') + invoices = {} + references = {} + for order in self: + collective_bill = order.partner_id.collective_bill and "x" or order.id + group_key = order.id if grouped else (collective_bill, order.partner_invoice_id.id, order.currency_id.id) + for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0): + if float_is_zero(line.qty_to_invoice, precision_digits=precision): + continue + if group_key not in invoices: + inv_data = order._prepare_invoice() + invoice = inv_obj.create(inv_data) + references[invoice] = order + invoices[group_key] = invoice + elif group_key in invoices: + vals = {} + if order.name not in invoices[group_key].origin.split(', '): + vals['origin'] = invoices[group_key].origin + ', ' + order.name + if order.client_order_ref and order.client_order_ref not in invoices[group_key].name.split( + ', ') and order.client_order_ref != invoices[group_key].name: + vals['name'] = invoices[group_key].name + ', ' + order.client_order_ref + invoices[group_key].write(vals) + if line.qty_to_invoice > 0: + line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice) + elif line.qty_to_invoice < 0 and final: + line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice) + + if references.get(invoices.get(group_key)): + if order not in references[invoices[group_key]]: + references[invoice] = references[invoice] | order + + if not invoices: + raise UserError(_('There is no invoicable line.')) + + for invoice in invoices.values(): + if not invoice.invoice_line_ids: + raise UserError(_('There is no invoicable line.')) + # If invoice is negative, do a refund invoice instead + if invoice.amount_untaxed < 0: + invoice.type = 'out_refund' + for line in invoice.invoice_line_ids: + line.quantity = -line.quantity + # Use additional field helper function (for account extensions) + for line in invoice.invoice_line_ids: + line._set_additional_fields(invoice) + # Necessary to force computation of taxes. In account_invoice, they are triggered + # by onchanges, which are not triggered when doing a create. + invoice.compute_taxes() + invoice.message_post_with_view('mail.message_origin_link', + values={'self': invoice, 'origin': references[invoice]}, + subtype_id=self.env.ref('mail.mt_note').id) + return [inv.id for inv in invoices.values()] + class SaleOrderLine(models.Model): _inherit = 'sale.order.line' diff --git a/ext/custom-addons/dp_custom/views/res_partner_views.xml b/ext/custom-addons/dp_custom/views/res_partner_views.xml index c16384f3..617e6ca9 100644 --- a/ext/custom-addons/dp_custom/views/res_partner_views.xml +++ b/ext/custom-addons/dp_custom/views/res_partner_views.xml @@ -54,6 +54,10 @@ + + + +