account_invoice.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  4. from dateutil.parser import parse
  5. from dateutil.relativedelta import relativedelta as rd
  6. def create_invoice(origin, supplier_id, cart_items, currency_id, payment_term_id=None, supplier_invoice_number=None, user_id=None):
  7. partner = r.env['res.partner'].browse(supplier_id)
  8. journal = r.env['account.journal'].search([('type', 'in', ['purchase']), ('company_id', '=', partner.company_id.id)])
  9. values = {
  10. 'name': '/',
  11. 'origin': origin,
  12. 'partner_id': partner.id,
  13. 'invoice_line': [[0, False, {
  14. 'name': line.get('name'),
  15. 'product_id': int(line.get('id')),
  16. 'quantity': float(line.get('quantity')),
  17. 'price_unit': float(line.get('price'))
  18. }] for line in cart_items],
  19. 'account_id': partner.property_account_payable.id,
  20. 'journal_id': journal.id,
  21. 'currency_id': currency_id,
  22. 'payment_term': payment_term_id,
  23. 'supplier_invoice_number': supplier_invoice_number,
  24. 'user_id': user_id,
  25. 'type': 'in_invoice'
  26. }
  27. return r.env['account.invoice'].create(values)
  28. def prepare_invoice(invoice_ids, currency_id, date_today):
  29. assert len(invoice_ids) == 1
  30. invoice = r.env['account.invoice'].browse(invoice_ids)
  31. date_due = parse(date_today) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
  32. invoice.write({
  33. 'currency_id': currency_id,
  34. 'date_invoice': date_today,
  35. 'date_due': date_due.strftime(DEFAULT_SERVER_DATE_FORMAT),
  36. 'state': 'open'
  37. })
  38. def validate_invoice(invoice_ids, type=None):
  39. assert len(invoice_ids) == 1
  40. invoice = r.env['account.invoice'].browse(invoice_ids)
  41. invoice.action_number()
  42. invoice.invoice_validate()
  43. if type == 'expense':
  44. name = 'GASTO' + invoice.name[invoice.name.index('/'):]
  45. invoice.write({
  46. 'number': name,
  47. 'internal_number': name
  48. })
  49. def close_invoice(invoice_ids):
  50. assert len(invoice_ids) == 1
  51. invoice = r.env['account.invoice'].browse(invoice_ids)
  52. if invoice.residual == 0:
  53. invoice.write({
  54. 'state': 'paid'
  55. })