sale_order.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 SaleOrder(models.Model):
  8. _inherit = 'sale.order'
  9. _name = 'sale.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 order_process_now(self):
  31. """
  32. Confirms order and creates and validates invoice, confirms pickings.
  33. """
  34. for sale in self:
  35. sale.action_button_confirm()
  36. inv_id = sale.action_invoice_create()
  37. if inv_id:
  38. inv = self.env['account.invoice'].browse(inv_id)
  39. date = self._convert_str_to_datetime(sale.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. self.update_state()
  48. inv.signal_workflow('invoice_open')
  49. for picking in sale.picking_ids:
  50. picking.force_assign()
  51. picking.action_done()
  52. @api.multi
  53. def update_state(self):
  54. for order in self:
  55. order.write({'state': 'done'})
  56. return True
  57. class account_invoice(models.Model):
  58. _inherit = 'account.invoice'
  59. _name = 'account.invoice'
  60. contado = fields.Boolean('Contado')
  61. credito = fields.Boolean('Crédito')
  62. _defaults = {
  63. 'contado': True
  64. }
  65. @api.one
  66. @api.onchange('credito')
  67. def cambiar_estado_credito(self):
  68. self.contado = not self.credito
  69. @api.one
  70. @api.onchange('contado')
  71. def cambiar_estado_contado(self):
  72. self.credito = not self.contado
  73. class SaleOrderLine(models.Model):
  74. _inherit = 'sale.order.line'
  75. _name = 'sale.order.line'
  76. amount_total = fields.Float(compute='_calculate_total_sale_line',string='Total',default=0.0)
  77. standard_price = fields.Float(compute='_standard_price',string='Precio de costo',default=0.0)
  78. discount_id = fields.Many2one(
  79. 'sale.purchase.discount',
  80. string='Desc. Costo',
  81. ondelete='restrict'
  82. )
  83. sale_percentage_id = fields.Many2one(
  84. 'sale.purchase.discount',
  85. string='% Venta',
  86. ondelete='restrict'
  87. )
  88. @api.depends('price_unit','product_uom_qty')
  89. def _calculate_total_sale_line(self):
  90. for line in self:
  91. line.amount_total = line.product_uom_qty * line.price_unit
  92. @api.depends('product_id')
  93. def _standard_price(self):
  94. for line in self:
  95. line.standard_price = line.product_id.standard_price
  96. @api.onchange('discount_id','sale_percentage_id')
  97. def _calculate_price_unit(self):
  98. for line in self:
  99. if line.discount_id and not line.sale_percentage_id:
  100. price_unit = line.standard_price - ((line.standard_price * (line.discount_id.value or 0.0))/100)
  101. line.price_unit = price_unit + ((price_unit * (line.interest_amount or 0.0))/100)
  102. if line.sale_percentage_id and not line.discount_id:
  103. price_unit = line.standard_price + ((line.standard_price * (line.sale_percentage_id.value or 0.0))/100)
  104. line.price_unit = price_unit + ((price_unit * (line.interest_amount or 0.0))/100)
  105. if line.discount_id and line.sale_percentage_id:
  106. price_unit = (line.standard_price - ((line.standard_price * (line.discount_id.value or 0.0))/100)) + (((line.standard_price - ((line.standard_price * (line.discount_id.value or 0.0))/100)) * (line.sale_percentage_id.value or 0.0))/100)
  107. line.price_unit = price_unit + ((price_unit * (line.interest_amount or 0.0))/100)