# -*- coding: utf-8 -*- from openerp.http import request from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, float_round from dateutil.parser import parse from dateutil.relativedelta import relativedelta as rd _MODEL = 'account.invoice' ''' ''' def create_invoice(sale_order_id, currency_id, date_today): sale_order = request.env['sale.order'].browse(sale_order_id) invoice_id = sale_order.action_invoice_create() invoice = request.env[_MODEL].browse(invoice_id) for picking in sale_order.picking_ids: picking.force_assign() picking.action_done() date_due = parse(date_today) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2))) invoice.write({ 'currency_id': currency_id, 'date_invoice': date_today, 'date_due': date_due.strftime(DEFAULT_SERVER_DATE_FORMAT), 'state': 'open' }) return invoice ''' Create move lines ''' def create_invoice_move_lines(invoice_id, paid_amount, date_today): invoice = request.env[_MODEL].browse(invoice_id) context = dict(request.context, lang=invoice.partner_id.lang) invoice_move_lines = invoice._get_analytic_lines() decimal_precision = request.env['decimal.precision'].precision_get('Account') compute_taxes = request.env['account.invoice.tax'].compute(invoice.with_context(context)) invoice.check_tax_lines(compute_taxes) invoice._recompute_tax_amount() invoice_move_lines += request.env['account.invoice.tax'].move_line_get(invoice.id) total, total_currency, invoice_move_lines = invoice.with_context(context).compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines) paid_amount = float_round(paid_amount, precision_digits=decimal_precision) paid_percentage = paid_amount / float_round(total_currency, precision_digits=decimal_precision) distributed_percentage = -(paid_percentage / len(invoice.payment_term.line_ids)) diff_currency = invoice.currency_id != invoice.company_id.currency_id if diff_currency: paid_amount = invoice.currency_id.compute(paid_amount, invoice.company_id.currency_id) payment_lines = [] for line in invoice.payment_term.line_ids: date_due = (parse(date_today) + rd(days=line.days + line.days2)).strftime(DEFAULT_SERVER_DATE_FORMAT) if paid_percentage and paid_percentage < 1.0: payment_lines.append([date_today, paid_percentage]) paid_percentage = paid_amount = 0 if date_due == date_today and line.value_amount: distributed_percentage = -((payment_lines[0][1] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1)) continue if line.value != 'balance': payment_lines.append([date_due, line.value_amount + distributed_percentage]) continue payment_lines.append([date_due, line.value_amount]) for payment_line in payment_lines: current_price = float_round(total * payment_line[1], precision_digits=decimal_precision) if current_price < 0.0: continue paid_amount += current_price # Compute move line price if payment_line[1]: price = current_price else: price = float_round(total - paid_amount, precision_digits=decimal_precision) or total # Compute move line amount in currency if diff_currency: amount_currency = invoice.company_id.currency_id.with_context(context).compute(price, invoice.currency_id) else: amount_currency = False # Create invoice move live value invoice_move_lines.append({ 'type': 'dest', 'name': '/', 'price': price, 'account_id': invoice.account_id.id, 'date_maturity': payment_line[0], 'amount_currency': amount_currency, 'currency_id': diff_currency and invoice.currency_id.id, 'ref': invoice.reference }) payment_lines = [] return invoice_move_lines ''' Number to invoice ''' def number_invoice(invoice_id): request.env[_MODEL].browse(invoice_id).action_number() ''' Close a invoice ''' def close_invoice(invoice_id): invoice = request.env[_MODEL].browse(invoice_id) if invoice.residual == 0: invoice.write({ 'state': 'paid' })