<commit_before># -*- coding: utf-8 -*-
from openerp.osv import osv
from openerp.tools.translate import _
from openerp import models, fields, api


class GroupedInvoiceWizard(osv.osv_memory):
    _name = "sale_line.grouped_invoice_wizard"

    # TODO: don't use old api
    def make_grouped_invoice(self, cr, uid, ids, context=None):
        sales_order_line_obj = self.pool.get('sale.order.line')
        order_obj = self.pool.get('sale.order')
        invoice = self.pool.get('account.invoice')

        invoices = {}
        inv_lines = []
        orders = []
        created_ids = []

        for line in sales_order_line_obj.browse(cr, uid, context.get('active_ids', []), context=context):
            if (not line.invoiced) and (line.state not in ('draft', 'cancel')):
                inv_line_id = sales_order_line_obj.invoice_line_create(cr, uid, [line.id])
                for lid in inv_line_id:
                    inv_lines.append(lid)
                    orders.append(line.order_id.id)
                    invoices.setdefault(line.order_id.partner_invoice_id.id or line.order_id.partner_id.id, []).append((line.order_id, [lid]))

        partner_currency = {}
        for o in order_obj.browse(cr, uid, orders, context=context):
            currency_id = o.pricelist_id.currency_id.id
            if (o.partner_id.id in partner_currency) and (partner_currency[o.partner_id.id] != currency_id):
                raise osv.except_osv(
                    _('Error!'),
                    _('You cannot group sales having different currencies for the same partner.'))

            partner_currency[o.partner_id.id] = currency_id

        for val in invoices.values():
            res = order_obj._make_invoice(cr, uid, val[0][0], reduce(lambda x, y: x + y, [l for o, l in val], []), context=context)
            invoice_ref = ''
            origin_ref = ''
            for o, l in val:
                invoice_ref += (o.client_order_ref or o.name) + '|'
                origin_ref += (o.origin or o.name) + '|'
                order_obj.write(cr, uid, [o.id], {'state': 'progress'})
                cr.execute('insert into sale_order_invoice_rel (order_id,invoice_id) values (%s,%s)', (o.id, res))
                order_obj.invalidate_cache(cr, uid, ['invoice_ids'], [o.id], context=context)
            # remove last '|' in invoice_ref
            if len(invoice_ref) >= 1:
                invoice_ref = invoice_ref[:-1]
            if len(origin_ref) >= 1:
                origin_ref = origin_ref[:-1]
            invoice.write(cr, uid, [res], {'origin': origin_ref, 'name': invoice_ref})
            created_ids.append(res)<commit_msg>[FIX] inoice_sale_order_line_group: there was not possible to create invoice from several lines from one order<commit_after># -*- coding: utf-8 -*-
from openerp.osv import osv
from openerp.tools.translate import _
from openerp import models, fields, api


class GroupedInvoiceWizard(osv.osv_memory):
    _name = "sale_line.grouped_invoice_wizard"

    # TODO: don't use old api
    def make_grouped_invoice(self, cr, uid, ids, context=None):
        sales_order_line_obj = self.pool.get('sale.order.line')
        order_obj = self.pool.get('sale.order')
        invoice = self.pool.get('account.invoice')

        invoices = {}
        inv_lines = []
        orders = []
        created_ids = []

        for line in sales_order_line_obj.browse(cr, uid, context.get('active_ids', []), context=context):
            if (not line.invoiced) and (line.state not in ('draft', 'cancel')):
                inv_line_id = sales_order_line_obj.invoice_line_create(cr, uid, [line.id])
                for lid in inv_line_id:
                    inv_lines.append(lid)
                    orders.append(line.order_id.id)
                    invoices.setdefault(line.order_id.partner_invoice_id.id or line.order_id.partner_id.id, []).append((line.order_id, [lid]))

        partner_currency = {}
        for o in order_obj.browse(cr, uid, orders, context=context):
            currency_id = o.pricelist_id.currency_id.id
            if (o.partner_id.id in partner_currency) and (partner_currency[o.partner_id.id] != currency_id):
                raise osv.except_osv(
                    _('Error!'),
                    _('You cannot group sales having different currencies for the same partner.'))

            partner_currency[o.partner_id.id] = currency_id

        for val in invoices.values():
            res = order_obj._make_invoice(cr, uid, val[0][0], reduce(lambda x, y: x + y, [l for o, l in val], []), context=context)
            invoice_ref = ''
            origin_ref = ''
            for o in set([o for o, l in val]):
                invoice_ref += (o.client_order_ref or o.name) + '|'
                origin_ref += (o.origin or o.name) + '|'
                order_obj.write(cr, uid, [o.id], {'state': 'progress'})
            cr.execute('insert into sale_order_invoice_rel (order_id,invoice_id) values (%s,%s)', (o.id, res))
            order_obj.invalidate_cache(cr, uid, ['invoice_ids'], [o.id], context=context)
            # remove last '|' in invoice_ref
            if len(invoice_ref) >= 1:
                invoice_ref = invoice_ref[:-1]
            if len(origin_ref) >= 1:
                origin_ref = origin_ref[:-1]
            invoice.write(cr, uid, [res], {'origin': origin_ref, 'name': invoice_ref})
            created_ids.append(res)
{'fraction_non_alphanumeric': 0.1268283651175708, 'fraction_numerical': 0.0025921125717459728, 'mean_word_length': 2.303975535168196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1692429', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1705, 'n_words': 379}