nota_remision.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 = "Sales Order Reference Note"
  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='Reference/Description', index=True, readonly=True)
  76. origin = fields.Char(string='Source Document', help="Reference of the document that produced this note.", readonly=True)
  77. #partner data
  78. partner_id = fields.Many2one('res.partner', string='Partner', 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='Company')
  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', 'Empresa logística', select=True)
  91. logistic_ruc = fields.Char(string='R.U.C./C.I. Empresa 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', 'Cancelled'),('draft', 'Draft'),('progress', 'Progress'),('done', 'Done')],
  96. 'Status', required=True, readonly=True, copy=False,
  97. help='* The \'Draft\' status is set when the related note order in draft status. \
  98. \n* The \'Progress\' status is set when the related note order is in progress. \
  99. \n* The \'Done\' status is set when the note order line has been picked. \
  100. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  101. #Transfer details
  102. initial_transfer_date = fields.Datetime('Initial Transfer Date')
  103. finish_transfer_date = fields.Datetime('Finish Transfer Date')
  104. #Vehicle and Logistic details
  105. vehicle_name = fields.Char('Vehicle')
  106. vehicle_plate = fields.Char('Nro. de la Chapa')
  107. #Driver details
  108. driver_id = fields.Many2one('res.partner','Chofer')
  109. driver_ruc = fields.Char(string='R.U.C./C.I. Chofer',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('Sale:')
  114. is_purchase = fields.Boolean('Purchase:')
  115. is_export = fields.Boolean('Export:')
  116. is_import= fields.Boolean('Import:')
  117. is_consignment = fields.Boolean('Consignment:')
  118. is_return = fields.Boolean('Return:')
  119. is_intertal_transfer = fields.Boolean('Internal Transfer between stores:')
  120. is_transformation_transfer = fields.Boolean('Transferencia de transformación:')
  121. is_repair_transfer = fields.Boolean('Repair Transfer:')
  122. is_movil_transfer = fields.Boolean('Movil Transfer:')
  123. is_exhibition = fields.Boolean('Exhibition/Demonstration:')
  124. is_fair = fields.Boolean('Fair Participation:')
  125. another_transfer = fields.Text('Another Transfer motive')
  126. sale_voucher = fields.Char('Sale Voucher')
  127. nota_line = fields.One2many('sale.nota.remision.line', 'nota_remision_id', 'Note Lines', readonly=True, states={'draft': [('readonly', False)]}, copy=True)
  128. amount_total = fields.Float('Amount Total',readonly=True)
  129. _defaults = {
  130. 'state': 'draft',
  131. 'is_consignment': True,
  132. 'name': lambda obj, cr, uid, context: '/',
  133. }
  134. class sale_nota_remision_line(models.Model):
  135. _name = 'sale.nota.remision.line'
  136. _description = "Sales Order Reference Note Line"
  137. @api.one
  138. @api.model
  139. def _amount_line(self):
  140. for line in self.ids:
  141. line_obj = self.env['sale.nota.remision.line'].search([('id','=',line)])
  142. price = line_obj.price_unit*(1 - (line_obj.discount or 0.0) / 100.0)
  143. self.price_subtotal = price*line_obj.product_uom_qty
  144. #fields
  145. nota_remision_id = fields.Many2one('sale.nota.remision', 'Note Reference', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]})
  146. name = fields.Text('Description', required=True, readonly=True, states={'draft': [('readonly', False)]})
  147. product_id = fields.Many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict')
  148. product_uom_qty = fields.Float('Quantity', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]})
  149. price_unit = fields.Float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]})
  150. discount = fields.Float('Discount (%)', digits_compute= dp.get_precision('Discount'), readonly=True, states={'draft': [('readonly', False)]})
  151. price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')
  152. # price_subtotal = fields.Float(compute='_amount_line', string='Subtotal', digits_compute= dp.get_precision('Account'))
  153. state = fields.Selection(
  154. [('cancel', 'Cancelled'),('draft', 'Draft'),('progress', 'Progress'),('done', 'Done')],
  155. 'Status', required=True, readonly=True, copy=False,
  156. help='* The \'Draft\' status is set when the related note order in draft status. \
  157. \n* The \'Progress\' status is set when the related note order is in progress. \
  158. \n* The \'Done\' status is set when the note order line has been picked. \
  159. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  160. _order = 'nota_remision_id desc, id'
  161. _defaults = {
  162. 'state': 'draft',
  163. }
  164. class sale_order(models.Model):
  165. _name = 'sale.order'
  166. _inherit = 'sale.order'
  167. @api.multi
  168. def _note_reference_exists(self):
  169. print self.note_reference_ids
  170. if self.note_reference_ids:
  171. self.note_reference_exists=True
  172. else:
  173. self.note_reference_exists=False
  174. note_reference_exists = fields.Boolean(string="Note exists", compute='_note_reference_exists', store="True")
  175. note_reference_ids = fields.Many2one('sale.nota.remision', 'Reference Note')
  176. @api.multi
  177. def action_button_view_note(self):
  178. # print 'Ver Nota'
  179. return {
  180. 'type': 'ir.actions.act_window',
  181. 'res_model': 'sale.nota.remision',
  182. 'view_type': 'form',
  183. 'view_mode': 'form',
  184. 'target': 'current',
  185. 'res_id':self.note_reference_ids.id,
  186. }
  187. @api.multi
  188. def action_button_create_note(self):
  189. # print "Crear Nota de Remision"
  190. # print self.partner_id.id
  191. # print self
  192. nsr = self.env['sale.nota.remision'].search([('origin','=',self.name)])
  193. if not nsr:
  194. valores = {'partner_id':self.partner_id.id,
  195. 'origin':self.name,
  196. 'initial_transfer_date':self.date_order,
  197. 'user_id':self.user_id.id,
  198. 'amount_total':self.amount_total,
  199. 'partner_company':self.partner_id.company_id.id,
  200. }
  201. #crear la nota de remision
  202. # print 'Nota creada'
  203. nr = self.env['sale.nota.remision'].create(valores)
  204. # print nr
  205. if nr:
  206. self.note_reference_ids=nr
  207. self._note_reference_exists()
  208. #copiar las lineas del pedido
  209. for line in self.order_line:
  210. # print line
  211. # print line.product_id
  212. line_order = self.env['sale.order.line'].search([('id','=',line.id)])
  213. if line_order:
  214. valores = {'nota_remision_id':nr[0].id,
  215. 'product_id':line_order[0].product_id.id,
  216. 'name':line_order[0].name,
  217. 'product_uom_qty':line_order[0].product_uom_qty,
  218. 'price_unit':line_order[0].price_unit,
  219. 'discount':line_order[0].discount,
  220. }
  221. nrl = self.env['sale.nota.remision.line'].create(valores)
  222. # else:
  223. # print 'La Nota ya existe'
  224. return True
  225. _defaults = {
  226. 'note_reference_exists':False,
  227. }