purchase_order.py 2.9 KB

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