purchase_order.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. from pytz import timezone
  5. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  6. from datetime import datetime,timedelta
  7. DATE_FORMAT = '%Y-%m-%d'
  8. class purchase_order(models.Model):
  9. _inherit = 'purchase.order'
  10. contado = fields.Boolean('Contado')
  11. credito = fields.Boolean('Crédito')
  12. _defaults = {
  13. 'contado': True
  14. }
  15. @api.one
  16. @api.onchange('contado')
  17. def cambiar_estado_contado(self):
  18. self.credito = not self.contado
  19. if self.contado == True:
  20. self.payment_term = False
  21. @api.one
  22. @api.onchange('credito')
  23. def cambiar_estado_credito(self):
  24. self.contado = not self.credito
  25. def _convert_str_to_datetime(self, date):
  26. return datetime.strptime(date,DEFAULT_SERVER_DATETIME_FORMAT)
  27. @api.model
  28. def action_invoice_create(self):
  29. for purchase in self:
  30. inv_id = super(purchase_order, self).action_invoice_create()
  31. if inv_id:
  32. inv = self.env['account.invoice'].browse(inv_id)
  33. date = self._convert_str_to_datetime(purchase.date_order)
  34. date = date.strftime(DATE_FORMAT)
  35. inv.write({
  36. 'date_invoice':date,
  37. 'contado':self.contado,
  38. 'credito':self.credito
  39. })
  40. self.update_state()
  41. @api.multi
  42. def update_state(self):
  43. for order in self:
  44. order.write({'state': 'done'})
  45. return True