nota_remision.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # -*- encoding: utf-8 -*-
  2. #################################################################################
  3. # #
  4. # product_features for OpenERP #
  5. # Copyright (C) 2009 NetAndCo (<http://www.netandco.net>). #
  6. # Authors, Mathieu Lemercier, mathieu@netandco.net, #
  7. # Franck Bret, franck@netandco.net #
  8. # Copyright (C) 2011 Akretion Benoît Guillot <benoit.guillot@akretion.com> #
  9. # #
  10. # This program is free software: you can redistribute it and/or modify #
  11. # it under the terms of the GNU Affero General Public License as #
  12. # published by the Free Software Foundation, either version 3 of the #
  13. # License, or (at your option) any later version. #
  14. # #
  15. # This program is distributed in the hope that it will be useful, #
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  18. # GNU Affero General Public License for more details. #
  19. # #
  20. # You should have received a copy of the GNU Affero General Public License #
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>. #
  22. # #
  23. #################################################################################
  24. from openerp import models, fields, api, _
  25. import openerp.addons.decimal_precision as dp
  26. class sale_nota_remision(models.Model):
  27. _name = "sale.nota.remision"
  28. _description = "Nota de referencia de orden de venta"
  29. def validar(self):
  30. print 'Confirmar'
  31. self.state = 'progress'
  32. return True
  33. #data fields for partner
  34. @api.multi
  35. @api.depends('partner_id')
  36. def _partner_data(self):
  37. self.partner_ruc = self.partner_id.ruc
  38. self.partner_phone = self.partner_id.phone
  39. self.partner_mobile = self.partner_id.mobile
  40. self.partner_company = self.partner_id.company_id.id
  41. #data fields for salesman
  42. @api.multi
  43. @api.depends('user_id')
  44. def _user_data(self):
  45. self.user_ruc = self.user_id.ruc
  46. self.user_phone = self.user_id.phone
  47. self.user_mobile = self.user_id.mobile
  48. #data fields for vehicle
  49. # @api.multi
  50. # @api.depends('vehicle_id')
  51. # def _vehicle_data(self):
  52. # self.vehicle_plate = self.vehicle_id.license_plate
  53. # self.driver_id = self.vehicle_id.driver_id
  54. #data fields for driver
  55. @api.multi
  56. @api.depends('driver_id')
  57. def _driver_data(self):
  58. self.driver_ruc = self.driver_id.ruc
  59. self.driver_phone = self.driver_id.phone
  60. self.driver_mobile = self.driver_id.mobile
  61. #data fields for logistic
  62. @api.multi
  63. @api.depends('logistic_company_id')
  64. def _logistic_data(self):
  65. self.logistic_ruc = self.logistic_company_id.ruc
  66. self.logistic_phone = self.logistic_company_id.phone
  67. self.logistic_mobile = self.logistic_company_id.mobile
  68. @api.model
  69. def create(self, vals):
  70. nota_id = super(sale_nota_remision,self).create(vals)
  71. # vals['name']=self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
  72. nota_id.name=self.pool.get('ir.sequence').get(self.env.cr, self.env.uid, 'sale.nota.remision') or '/'
  73. return nota_id
  74. # fields
  75. name = fields.Char(string='Referencia/Descripción', index=True, readonly=True)
  76. origin = fields.Char(string='Documento Origen ', help="Referencia del documento que produjo esta nota.", readonly=True)
  77. #partner data
  78. partner_id = fields.Many2one('res.partner', string='Cliente', required=True)
  79. partner_ruc = fields.Char(string='R.U.C./C.I.',compute='_partner_data')
  80. partner_phone = fields.Char(string='Teléfono',compute='_partner_data')
  81. partner_mobile = fields.Char(string='Telef. Móvil',compute='_partner_data')
  82. #company address
  83. partner_company = fields.Many2one('res.company', string='Empresa')
  84. #commercial data
  85. user_id = fields.Many2one('res.users', 'Vendedor', select=True)
  86. user_ruc = fields.Char(string='R.U.C./C.I. Vendedor',compute='_user_data')
  87. user_phone = fields.Char(string='Teléfono',compute='_user_data')
  88. user_mobile = fields.Char(string='Telef. Móvil',compute='_user_data')
  89. #logistic data
  90. logistic_company_id = fields.Many2one('res.partner', 'Compañia Logística', select=True)
  91. logistic_ruc = fields.Char(string='R.U.C./C.I. Compañia Logística',compute='_logistic_data')
  92. logistic_phone = fields.Char(string='Teléfono',compute='_logistic_data')
  93. logistic_mobile = fields.Char(string='Telef. Móvil',compute='_logistic_data')
  94. state = fields.Selection(
  95. [('cancel', 'Cancelado'),('draft', 'Borrador'),('progress', 'En progreso'),('done', 'Realizado')],
  96. 'Status', required=True, readonly=True, copy=False,
  97. help='* El estado de \'Borrador\' se establece cuando la nota relacionada se ordena en estado de borrador. \
  98. \n* El estado de \'Progreso\' se establece cuando el orden de las notas relacionadas está en curso. \
  99. \n* El estado de \'Realizado\' se establece cuando se selecciona la línea de orden de nota.\
  100. \n* El estado \'Cancelado\' se establece cuando un usuario cancela el pedido de notas relacionado')
  101. #Transfer details
  102. initial_transfer_date = fields.Datetime('Fecha de transferencia inicial')
  103. finish_transfer_date = fields.Datetime('Fecha de transferencia finalizada')
  104. #Vehicle and Logistic details
  105. vehicle_name = fields.Char('Vehículo')
  106. vehicle_plate = fields.Char('Nro. de la Chapa')
  107. #Driver details
  108. driver_id = fields.Many2one('res.partner','Chófer')
  109. driver_ruc = fields.Char(string='R.U.C./C.I. Chófer',compute='_driver_data')
  110. driver_phone = fields.Char(string='Teléfono',compute='_driver_data')
  111. driver_mobile = fields.Char(string='Telef. Móvil',compute='_driver_data')
  112. #Transfer motives
  113. is_sale = fields.Boolean('Venta:')
  114. is_purchase = fields.Boolean('Compra:')
  115. is_export = fields.Boolean('Exportación:')
  116. is_import= fields.Boolean('Importación:')
  117. is_consignment = fields.Boolean('Consignación:')
  118. is_return = fields.Boolean('Devolución:')
  119. is_intertal_transfer = fields.Boolean('Transferencia interna entre almacenes:')
  120. is_transformation_transfer = fields.Boolean('Transferencia de transformación:')
  121. is_repair_transfer = fields.Boolean('Transferencia de reparación:')
  122. is_movil_transfer = fields.Boolean('Transferencia Movil:')
  123. is_exhibition = fields.Boolean('Exhibición/Demostración:')
  124. is_fair = fields.Boolean('Participación justa:')
  125. another_transfer = fields.Text('Otro motivo de transferencia')
  126. sale_voucher = fields.Char('Recibo de venta')
  127. obs_remision = fields.Text('Observación')
  128. nota_line = fields.One2many('sale.nota.remision.line', 'nota_remision_id', 'Líneas de nota de remisión', readonly=True, states={'draft': [('readonly', False)]}, copy=True)
  129. amount_total = fields.Float('Monto Total',readonly=True)
  130. _defaults = {
  131. 'state': 'draft',
  132. 'is_consignment': True,
  133. 'name': lambda obj, cr, uid, context: '/',
  134. }
  135. class sale_nota_remision_line(models.Model):
  136. _name = 'sale.nota.remision.line'
  137. _description = "Referencia de orden de venta"
  138. @api.one
  139. @api.model
  140. def _amount_line(self):
  141. for line in self.ids:
  142. line_obj = self.env['sale.nota.remision.line'].search([('id','=',line)])
  143. price = line_obj.price_unit*(1 - (line_obj.discount or 0.0) / 100.0)
  144. self.price_subtotal = price*line_obj.product_uom_qty
  145. #fields
  146. nota_remision_id = fields.Many2one('sale.nota.remision', 'Nota Referencia', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]})
  147. name = fields.Text('Descripción', required=True, readonly=True, states={'draft': [('readonly', False)]})
  148. product_id = fields.Many2one('product.product', 'Producto', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict')
  149. product_uom_qty = fields.Float('Cantidad', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]})
  150. price_unit = fields.Float('Precio Unit.', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]})
  151. discount = fields.Float('Descuento (%)', digits_compute= dp.get_precision('Discount'), readonly=True, states={'draft': [('readonly', False)]})
  152. price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')
  153. # price_subtotal = fields.Float(compute='_amount_line', string='Subtotal', digits_compute= dp.get_precision('Account'))
  154. state = fields.Selection(
  155. [('cancel', 'Cancelado'),('draft', 'Borrador'),('progress', 'En progreso'),('done', 'Realizado')],
  156. 'Status', required=True, readonly=True, copy=False,
  157. help='* The \'Draft\' status is set when the related note order in draft status. \
  158. \n* The \'Progress\' status is set when the related note order is in progress. \
  159. \n* The \'Done\' status is set when the note order line has been picked. \
  160. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  161. _order = 'nota_remision_id desc, id'
  162. _defaults = {
  163. 'state': 'draft',
  164. }
  165. class sale_order(models.Model):
  166. _name = 'sale.order'
  167. _inherit = 'sale.order'
  168. @api.multi
  169. def _note_reference_exists(self):
  170. print self.note_reference_ids
  171. if self.note_reference_ids:
  172. self.note_reference_exists=True
  173. else:
  174. self.note_reference_exists=False
  175. note_reference_exists = fields.Boolean(string="Nota existe", compute='_note_reference_exists', store="True")
  176. note_reference_ids = fields.Many2one('sale.nota.remision', 'Referencia Nota')
  177. @api.multi
  178. def action_button_view_note(self):
  179. # print 'Ver Nota'
  180. return {
  181. 'type': 'ir.actions.act_window',
  182. 'res_model': 'sale.nota.remision',
  183. 'view_type': 'form',
  184. 'view_mode': 'form',
  185. 'target': 'current',
  186. 'res_id':self.note_reference_ids.id,
  187. }
  188. @api.multi
  189. def action_button_create_note(self):
  190. # print "Crear Nota de Remision"
  191. # print self.partner_id.id
  192. # print self
  193. nsr = self.env['sale.nota.remision'].search([('origin','=',self.name)])
  194. if not nsr:
  195. valores = {'partner_id':self.partner_id.id,
  196. 'origin':self.name,
  197. 'initial_transfer_date':self.date_order,
  198. 'user_id':self.user_id.id,
  199. 'amount_total':self.amount_total,
  200. 'partner_company':self.partner_id.company_id.id,
  201. }
  202. #crear la nota de remision
  203. # print 'Nota creada'
  204. nr = self.env['sale.nota.remision'].create(valores)
  205. # print nr
  206. if nr:
  207. self.note_reference_ids=nr
  208. self._note_reference_exists()
  209. #copiar las lineas del pedido
  210. for line in self.order_line:
  211. # print line
  212. # print line.product_id
  213. line_order = self.env['sale.order.line'].search([('id','=',line.id)])
  214. if line_order:
  215. valores = {'nota_remision_id':nr[0].id,
  216. 'product_id':line_order[0].product_id.id,
  217. 'name':line_order[0].name,
  218. 'product_uom_qty':line_order[0].product_uom_qty,
  219. 'price_unit':line_order[0].price_unit,
  220. 'discount':line_order[0].discount,
  221. }
  222. nrl = self.env['sale.nota.remision.line'].create(valores)
  223. # else:
  224. # print 'La Nota ya existe'
  225. return True
  226. _defaults = {
  227. 'note_reference_exists':False,
  228. }