purchase_order.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, api, fields
  3. from pytz import timezone
  4. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  5. from datetime import datetime,timedelta
  6. DATE_FORMAT = '%Y-%m-%d'
  7. class PurchaseOrder(models.Model):
  8. _inherit = 'purchase.order'
  9. _name = 'purchase.order'
  10. invoice_number = fields.Char('Numero de Factura')
  11. contado = fields.Boolean('Contado')
  12. credito = fields.Boolean('Crédito')
  13. _defaults = {
  14. 'contado': True
  15. }
  16. @api.one
  17. @api.onchange('credito')
  18. def cambiar_estado_credito(self):
  19. self.contado = not self.credito
  20. @api.one
  21. @api.onchange('contado')
  22. def cambiar_estado_contado(self):
  23. self.credito = not self.contado
  24. ''' Timezone '''
  25. def get_timezone(self):
  26. return timezone(self._context.get('tz') or self.env.user.tz)
  27. def _convert_str_to_datetime(self, date):
  28. return datetime.strptime(date,DEFAULT_SERVER_DATETIME_FORMAT)
  29. @api.multi
  30. def purchase_process_now(self):
  31. """
  32. Confirms order and creates and validates invoice, confirms pickings.
  33. """
  34. for purchase in self:
  35. self.update_state()
  36. inv_id = purchase.action_invoice_create()
  37. if inv_id:
  38. inv = self.env['account.invoice'].browse(inv_id)
  39. # import web_pdb; web_pdb.set_trace()
  40. date = self._convert_str_to_datetime(purchase.date_order)
  41. date = date.strftime(DATE_FORMAT)
  42. inv.write({
  43. 'date_invoice':date,
  44. 'supplier_invoice_number':self.invoice_number,
  45. 'contado':self.contado,
  46. 'credito':self.credito,
  47. })
  48. inv.signal_workflow('invoice_open')
  49. purchase.action_picking_create()
  50. for picking in purchase.picking_ids:
  51. picking.force_assign()
  52. picking.action_done()
  53. @api.multi
  54. def update_state(self):
  55. for order in self:
  56. order.write({'state': 'approved'})
  57. return True
  58. class PurchaseOrderLine(models.Model):
  59. _inherit = 'purchase.order.line'
  60. _name = 'purchase.order.line'
  61. amount_total = fields.Float(compute='_calculate_total_purchase_line',string='Total',default=0.0)
  62. @api.depends('discount','price_unit','product_qty')
  63. def _calculate_total_purchase_line(self):
  64. for line in self:
  65. line.amount_total = line.product_qty * (line.price_unit - ((line.price_unit * (line.discount or 0.0))/100.0))
  66. class AccountInvoice(models.Model):
  67. _inherit = 'account.invoice'
  68. _name = 'account.invoice'
  69. contado = fields.Boolean('Contado')
  70. credito = fields.Boolean('Crédito')
  71. _defaults = {
  72. 'contado': True
  73. }
  74. @api.one
  75. @api.onchange('credito')
  76. def cambiar_estado_credito(self):
  77. self.contado = not self.credito
  78. @api.one
  79. @api.onchange('contado')
  80. def cambiar_estado_contado(self):
  81. self.credito = not self.contado