sale_order.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 sale_order(models.Model):
  9. _inherit = 'sale.order'
  10. contado = fields.Boolean('Contado')
  11. credito = fields.Boolean('Crédito')
  12. _defaults = {
  13. 'contado': True,
  14. 'order_policy':'prepaid'
  15. }
  16. @api.one
  17. @api.onchange('contado')
  18. def cambiar_estado_contado(self):
  19. self.credito = not self.contado
  20. if self.contado == True:
  21. self.payment_term = False
  22. self.order_policy = 'prepaid'
  23. @api.one
  24. @api.onchange('credito')
  25. def cambiar_estado_credito(self):
  26. self.contado = not self.credito
  27. if self.credito == True:
  28. self.order_policy = 'manual'
  29. def _convert_str_to_datetime(self, date):
  30. return datetime.strptime(date,DEFAULT_SERVER_DATETIME_FORMAT)
  31. @api.model
  32. def action_invoice_create(self):
  33. for sale in self:
  34. inv_id = super(sale_order, self).action_invoice_create()
  35. if inv_id:
  36. inv = self.env['account.invoice'].browse(inv_id)
  37. date = self._convert_str_to_datetime(sale.date_order)
  38. date = date.strftime(DATE_FORMAT)
  39. inv.write({
  40. 'date_invoice':date,
  41. 'contado':self.contado,
  42. 'credito':self.credito
  43. })
  44. self.update_state()
  45. @api.multi
  46. def update_state(self):
  47. for order in self:
  48. order.write({'state': 'done'})
  49. return True