nota_remision.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_street = self.partner_id.street
  41. self.partner_company = self.partner_id.company_id.id
  42. self.partner_city = self.partner_id.city
  43. self.partner_state = self.partner_id.state_id.name
  44. #data fields for salesman
  45. @api.multi
  46. @api.depends('user_id')
  47. def _user_data(self):
  48. self.user_ruc = self.user_id.ruc
  49. self.user_phone = self.user_id.phone
  50. self.user_mobile = self.user_id.mobile
  51. #data fields for vehicle
  52. # @api.multi
  53. # @api.depends('vehicle_id')
  54. # def _vehicle_data(self):
  55. # self.vehicle_plate = self.vehicle_id.license_plate
  56. # self.driver_id = self.vehicle_id.driver_id
  57. #data fields for driver
  58. @api.multi
  59. @api.depends('driver_id')
  60. def _driver_data(self):
  61. self.driver_ruc = self.driver_id.ruc
  62. self.driver_phone = self.driver_id.phone
  63. self.driver_mobile = self.driver_id.mobile
  64. #data fields for logistic
  65. @api.multi
  66. @api.depends('logistic_company_id')
  67. def _logistic_data(self):
  68. self.logistic_ruc = self.logistic_company_id.ruc
  69. self.logistic_phone = self.logistic_company_id.phone
  70. self.logistic_mobile = self.logistic_company_id.mobile
  71. @api.model
  72. def create(self, vals):
  73. nota_id = super(sale_nota_remision,self).create(vals)
  74. # vals['name']=self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
  75. nota_id.name=self.pool.get('ir.sequence').get(self.env.cr, self.env.uid, 'sale.nota.remision') or '/'
  76. return nota_id
  77. # fields
  78. name = fields.Char(string='Referencia/Descripción', index=True, readonly=True)
  79. origin = fields.Char(string='Documento Origen ', help="Referencia del documento que produjo esta nota.", readonly=True)
  80. #partner data
  81. partner_id = fields.Many2one('res.partner', string='Cliente', required=True)
  82. partner_ruc = fields.Char(string='R.U.C./C.I.',compute='_partner_data')
  83. partner_phone = fields.Char(string='Teléfono',compute='_partner_data')
  84. partner_mobile = fields.Char(string='Telef. Móvil',compute='_partner_data')
  85. partner_street = fields.Char(string='Dirección',compute='_partner_data')
  86. partner_city = fields.Char(string='Ciudad',compute='_partner_data')
  87. partner_state = fields.Char(string='Departamento',compute='_partner_data')
  88. #company address
  89. partner_company = fields.Many2one('res.company', string='Empresa')
  90. #commercial data
  91. user_id = fields.Many2one('res.users', 'Vendedor', select=True)
  92. user_ruc = fields.Char(string='R.U.C./C.I. Vendedor',compute='_user_data')
  93. user_phone = fields.Char(string='Teléfono',compute='_user_data')
  94. user_mobile = fields.Char(string='Telef. Móvil',compute='_user_data')
  95. #logistic data
  96. logistic_company_id = fields.Many2one('res.partner', 'Compañia Logística', select=True)
  97. logistic_ruc = fields.Char(string='R.U.C./C.I. Compañia Logística',compute='_logistic_data')
  98. logistic_phone = fields.Char(string='Teléfono',compute='_logistic_data')
  99. logistic_mobile = fields.Char(string='Telef. Móvil',compute='_logistic_data')
  100. state = fields.Selection(
  101. [('cancel', 'Cancelado'),('draft', 'Borrador'),('progress', 'En progreso'),('done', 'Realizado')],
  102. 'Status', required=True, readonly=True, copy=False,
  103. help='* El estado de \'Borrador\' se establece cuando la nota relacionada se ordena en estado de borrador. \
  104. \n* El estado de \'Progreso\' se establece cuando el orden de las notas relacionadas está en curso. \
  105. \n* El estado de \'Realizado\' se establece cuando se selecciona la línea de orden de nota.\
  106. \n* El estado \'Cancelado\' se establece cuando un usuario cancela el pedido de notas relacionado')
  107. #Transfer details
  108. initial_transfer_date = fields.Datetime('Fecha de transferencia inicial')
  109. finish_transfer_date = fields.Datetime('Fecha de transferencia finalizada')
  110. #Vehicle and Logistic details
  111. vehicle_name = fields.Char('Vehículo')
  112. vehicle_plate = fields.Char('Nro. de la Chapa')
  113. #Driver details
  114. driver_id = fields.Many2one('res.partner','Chófer')
  115. driver_ruc = fields.Char(string='R.U.C./C.I. Chófer',compute='_driver_data')
  116. driver_phone = fields.Char(string='Teléfono',compute='_driver_data')
  117. driver_mobile = fields.Char(string='Telef. Móvil',compute='_driver_data')
  118. #Transfer motives
  119. is_sale = fields.Boolean('Venta:')
  120. is_purchase = fields.Boolean('Compra:')
  121. is_export = fields.Boolean('Exportación:')
  122. is_import= fields.Boolean('Importación:')
  123. is_consignment = fields.Boolean('Consignación:')
  124. is_return = fields.Boolean('Devolución:')
  125. is_intertal_transfer = fields.Boolean('Transferencia interna entre almacenes:')
  126. is_transformation_transfer = fields.Boolean('Transferencia de transformación:')
  127. is_repair_transfer = fields.Boolean('Transferencia de reparación:')
  128. is_movil_transfer = fields.Boolean('Transferencia Movil:')
  129. is_exhibition = fields.Boolean('Exhibición/Demostración:')
  130. is_fair = fields.Boolean('Participación justa:')
  131. another_transfer = fields.Text('Otro motivo de transferencia')
  132. sale_voucher = fields.Char('Recibo de venta')
  133. obs_remision = fields.Text('Observación')
  134. 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)
  135. amount_total = fields.Float('Monto Total',readonly=True)
  136. _defaults = {
  137. 'state': 'draft',
  138. 'is_consignment': True,
  139. 'name': lambda obj, cr, uid, context: '/',
  140. }
  141. class sale_nota_remision_line(models.Model):
  142. _name = 'sale.nota.remision.line'
  143. _description = "Referencia de orden de venta"
  144. @api.one
  145. @api.model
  146. def _amount_line(self):
  147. for line in self.ids:
  148. line_obj = self.env['sale.nota.remision.line'].search([('id','=',line)])
  149. price = line_obj.price_unit*(1 - (line_obj.discount or 0.0) / 100.0)
  150. self.price_subtotal = price*line_obj.product_uom_qty
  151. #fields
  152. nota_remision_id = fields.Many2one('sale.nota.remision', 'Nota Referencia', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]})
  153. name = fields.Text('Descripción', required=True, readonly=True, states={'draft': [('readonly', False)]})
  154. product_id = fields.Many2one('product.product', 'Producto', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict')
  155. product_uom_qty = fields.Float('Cantidad', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]})
  156. price_unit = fields.Float('Precio Unit.', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]})
  157. discount = fields.Float('Descuento (%)', digits_compute= dp.get_precision('Discount'), readonly=True, states={'draft': [('readonly', False)]})
  158. price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')
  159. # price_subtotal = fields.Float(compute='_amount_line', string='Subtotal', digits_compute= dp.get_precision('Account'))
  160. state = fields.Selection(
  161. [('cancel', 'Cancelado'),('draft', 'Borrador'),('progress', 'En progreso'),('done', 'Realizado')],
  162. 'Status', required=True, readonly=True, copy=False,
  163. help='* The \'Draft\' status is set when the related note order in draft status. \
  164. \n* The \'Progress\' status is set when the related note order is in progress. \
  165. \n* The \'Done\' status is set when the note order line has been picked. \
  166. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  167. _order = 'nota_remision_id desc, id'
  168. _defaults = {
  169. 'state': 'draft',
  170. }
  171. class sale_order(models.Model):
  172. _name = 'sale.order'
  173. _inherit = 'sale.order'
  174. @api.multi
  175. def _note_reference_exists(self):
  176. print self.note_reference_ids
  177. if self.note_reference_ids:
  178. self.note_reference_exists=True
  179. else:
  180. self.note_reference_exists=False
  181. note_reference_exists = fields.Boolean(string="Nota existe", compute='_note_reference_exists', store="True")
  182. note_reference_ids = fields.Many2one('sale.nota.remision', 'Referencia Nota')
  183. @api.multi
  184. def action_button_view_note(self):
  185. # print 'Ver Nota'
  186. return {
  187. 'type': 'ir.actions.act_window',
  188. 'res_model': 'sale.nota.remision',
  189. 'view_type': 'form',
  190. 'view_mode': 'form',
  191. 'target': 'current',
  192. 'res_id':self.note_reference_ids.id,
  193. }
  194. @api.multi
  195. def action_button_create_note(self):
  196. # print "Crear Nota de Remision"
  197. # print self.partner_id.id
  198. # print self
  199. nsr = self.env['sale.nota.remision'].search([('origin','=',self.name)])
  200. if not nsr:
  201. valores = {'partner_id':self.partner_id.id,
  202. 'origin':self.name,
  203. 'initial_transfer_date':self.date_order,
  204. 'user_id':self.user_id.id,
  205. 'amount_total':self.amount_total,
  206. 'partner_company':self.partner_id.company_id.id,
  207. }
  208. #crear la nota de remision
  209. # print 'Nota creada'
  210. nr = self.env['sale.nota.remision'].create(valores)
  211. # print nr
  212. if nr:
  213. self.note_reference_ids=nr
  214. self._note_reference_exists()
  215. #copiar las lineas del pedido
  216. for line in self.order_line:
  217. # print line
  218. # print line.product_id
  219. line_order = self.env['sale.order.line'].search([('id','=',line.id)])
  220. if line_order:
  221. valores = {'nota_remision_id':nr[0].id,
  222. 'product_id':line_order[0].product_id.id,
  223. 'name':line_order[0].name,
  224. 'product_uom_qty':line_order[0].product_uom_qty,
  225. 'price_unit':line_order[0].price_unit,
  226. 'discount':line_order[0].discount,
  227. }
  228. nrl = self.env['sale.nota.remision.line'].create(valores)
  229. # else:
  230. # print 'La Nota ya existe'
  231. return True
  232. _defaults = {
  233. 'note_reference_exists':False,
  234. }