# -*- coding: utf-8 -*- from openerp.http import request as r from openerp.tools import DEFAULT_SERVER_DATE_FORMAT from dateutil.parser import parse from dateutil.relativedelta import relativedelta as rd def create_invoice(origin, supplier_id, cart_items, currency_id, payment_term_id=None, supplier_invoice_number=None, user_id=None): partner = r.env['res.partner'].browse(supplier_id) journal = r.env['account.journal'].search([('type', 'in', ['purchase']), ('company_id', '=', partner.company_id.id)]) values = { 'name': '/', 'origin': origin, 'partner_id': partner.id, 'invoice_line': [[0, False, { 'name': line.get('name'), 'product_id': int(line.get('id')), 'quantity': float(line.get('quantity')), 'price_unit': float(line.get('price')) }] for line in cart_items], 'account_id': partner.property_account_payable.id, 'journal_id': journal.id, 'currency_id': currency_id, 'payment_term': payment_term_id, 'supplier_invoice_number': supplier_invoice_number, 'user_id': user_id, 'type': 'in_invoice' } return r.env['account.invoice'].create(values) def prepare_invoice(invoice_ids, currency_id, date_today): assert len(invoice_ids) == 1 invoice = r.env['account.invoice'].browse(invoice_ids) 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' }) def validate_invoice(invoice_ids, type=None): assert len(invoice_ids) == 1 invoice = r.env['account.invoice'].browse(invoice_ids) invoice.action_number() invoice.invoice_validate() if type == 'expense': name = 'GASTO' + invoice.name[invoice.name.index('/'):] invoice.write({ 'number': name, 'internal_number': name }) def close_invoice(invoice_ids): assert len(invoice_ids) == 1 invoice = r.env['account.invoice'].browse(invoice_ids) if invoice.residual == 0: invoice.write({ 'state': 'paid' })