[ADD] invoice merging using module account_invoice_merge

Merged Sebastian Kennedy requested to merge 8.0-invoice_merge into 8.0-stage
Compare and
2 files
+ 88
4
Preferences
File browser
Compare changes
+ 87
3
@@ -7,9 +7,9 @@
##############################################################################
from openerp import _, api, exceptions, fields, models
from openerp.exceptions import except_orm
from openerp.addons.decimal_precision import decimal_precision as dp
from openerp import api, fields, models, _ # , exceptions
from openerp.exceptions import ValidationError # except_orm
# from openerp.addons.decimal_precision import decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, float_compare
import logging
@@ -69,6 +69,90 @@ class AccountInvoice(models.Model):
romaneos = self.env['ranch.purchase.romaneo'].browse(res)
return romaneos
def do_merge(self, keep_references=True, date_invoice=False):
ranch_invoices = False
ranch_invoices = [invoice.purchase_data_id != False
for invoice in self]
# If any invoice is from ranch, all of them should be
if any(ranch_invoices) and not all(ranch_invoices):
raise ValidationError(
_('All invoices should be from Ranch'))
if all(ranch_invoices):
ranch_invoices = True
context = self._context.copy()
context['ranch_invoices'] = ranch_invoices
invoices_info, invoice_lines_info = super(
AccountInvoice, self.with_context(context)).\
do_merge(keep_references=keep_references,
date_invoice=date_invoice)
final_model = self.env['ranch.purchase.data.final.line']
summ_model = self.env['ranch.purchase.romaneo.summary.line']
for il_old, il_new in invoice_lines_info.items()[0][1].items():
final_line = final_model.search(
[('invoice_lines', 'in', il_old)])
summ_line = summ_model.search(
[('invoice_lines', 'in', il_old)])
if final_line:
final_line.write({
'invoice_lines': [
(3, il_old, _), (4, il_new, _)]
})
if summ_line:
summ_line.write({
'invoice_lines': [
(3, il_old, _), (4, il_new, _)]
})
# Write payment_system_term_ids to new invoice
invoice.payment_system_term_ids.write({'invoice_id': invoices_info.keys()[0]})
return invoices_info, invoice_lines_info
@api.model
def _get_first_invoice_fields(self, invoice):
"""Method from account_invoice_merge module"""
res = super(AccountInvoice, self).\
_get_first_invoice_fields(invoice)
ranch_invoices = self._context.get('ranch_invoices')
if not ranch_invoices:
return True
if ranch_invoices:
res['purchase_data_id'] = invoice.purchase_data_id.id
res['date_cutoff'] = invoice.date_cutoff
res['date_due'] = invoice.date_due
res['is_lsp'] = invoice.is_lsp
res['ranch_date'] = invoice.ranch_date
res['renspa'] = invoice.renspa
res['dte'] = invoice.dte
res['payment_term_days'] = invoice.payment_term_days
res['payment_system_id'] = invoice.payment_system_id.id
# TODO: move to another module related to l10n_ar
res['denomination_id'] = invoice.denomination_id.id
return res
@api.model
def _get_invoice_line_key_cols(self):
res = super(AccountInvoice, self)._get_invoice_line_key_cols()
if self._context.get('ranch_invoices'):
res.append('id')
return res
@api.multi
def unlink(self):
for inv in self: