nota_remision.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_dir = self.partner_id.street
  40. self.partner_mobile = self.partner_id.mobile
  41. self.partner_company = self.partner_id.company_id.id
  42. #data fields for salesman
  43. @api.multi
  44. @api.depends('user_id')
  45. def _user_data(self):
  46. self.user_ruc = self.user_id.ruc
  47. self.user_phone = self.user_id.phone
  48. self.user_mobile = self.user_id.mobile
  49. #data fields for vehicle
  50. # @api.multi
  51. # @api.depends('vehicle_id')
  52. # def _vehicle_data(self):
  53. # self.vehicle_plate = self.vehicle_id.license_plate
  54. # self.driver_id = self.vehicle_id.driver_id
  55. #data fields for driver
  56. @api.multi
  57. @api.depends('driver_id')
  58. def _driver_data(self):
  59. self.driver_ruc = self.driver_id.ruc
  60. self.driver_phone = self.driver_id.phone
  61. self.driver_mobile = self.driver_id.mobile
  62. #data fields for logistic
  63. @api.multi
  64. @api.depends('logistic_company_id')
  65. def _logistic_data(self):
  66. self.logistic_ruc = self.logistic_company_id.ruc
  67. self.logistic_phone = self.logistic_company_id.phone
  68. self.logistic_mobile = self.logistic_company_id.mobile
  69. @api.model
  70. def create(self, vals):
  71. nota_id = super(sale_nota_remision,self).create(vals)
  72. # vals['name']=self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
  73. nota_id.name=self.pool.get('ir.sequence').get(self.env.cr, self.env.uid, 'sale.nota.remision') or '/'
  74. return nota_id
  75. # fields
  76. name = fields.Char(string='Reference/Description', index=True, readonly=True)
  77. origin = fields.Char(string='Source Document', help="Reference of the document that produced this note.", readonly=True)
  78. #partner data
  79. partner_id = fields.Many2one('res.partner', string='Partner', required=True)
  80. partner_ruc = fields.Char(string='R.U.C./C.I.',compute='_partner_data')
  81. partner_dir = fields.Char(string='Dirección',compute='_partner_data')
  82. partner_phone = fields.Char(string='Teléfono',compute='_partner_data')
  83. partner_mobile = fields.Char(string='Telef. Móvil',compute='_partner_data')
  84. #company address
  85. partner_company = fields.Many2one('res.company', string='Company')
  86. #commercial data
  87. user_id = fields.Many2one('res.users', 'Vendedor', select=True)
  88. user_ruc = fields.Char(string='R.U.C./C.I. Vendedor',compute='_user_data')
  89. user_phone = fields.Char(string='Teléfono',compute='_user_data')
  90. user_mobile = fields.Char(string='Telef. Móvil',compute='_user_data')
  91. #logistic data
  92. logistic_company_id = fields.Many2one('res.partner', 'Empresa logística', select=True)
  93. logistic_ruc = fields.Char(string='R.U.C./C.I. Empresa logística',compute='_logistic_data')
  94. logistic_phone = fields.Char(string='Teléfono',compute='_logistic_data')
  95. logistic_mobile = fields.Char(string='Telef. Móvil',compute='_logistic_data')
  96. state = fields.Selection(
  97. [('cancel', 'Cancelled'),('draft', 'Draft'),('progress', 'Progress'),('done', 'Done')],
  98. 'Status', required=True, readonly=True, copy=False,
  99. help='* The \'Draft\' status is set when the related note order in draft status. \
  100. \n* The \'Progress\' status is set when the related note order is in progress. \
  101. \n* The \'Done\' status is set when the note order line has been picked. \
  102. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  103. #Transfer details
  104. initial_transfer_date = fields.Datetime('Initial Transfer Date')
  105. finish_transfer_date = fields.Datetime('Finish Transfer Date')
  106. #Vehicle and Logistic details
  107. vehicle_name = fields.Char('Vehicle')
  108. vehicle_plate = fields.Char('Nro. de la Chapa')
  109. #Driver details
  110. driver_id = fields.Many2one('res.partner','Chofer')
  111. driver_ruc = fields.Char(string='R.U.C./C.I. Chofer',compute='_driver_data')
  112. driver_phone = fields.Char(string='Teléfono',compute='_driver_data')
  113. driver_mobile = fields.Char(string='Telef. Móvil',compute='_driver_data')
  114. #Transfer motives
  115. is_sale = fields.Boolean('Sale:')
  116. is_purchase = fields.Boolean('Purchase:')
  117. is_export = fields.Boolean('Export:')
  118. is_import= fields.Boolean('Import:')
  119. is_consignment = fields.Boolean('Consignment:')
  120. is_return = fields.Boolean('Return:')
  121. is_intertal_transfer = fields.Boolean('Internal Transfer between stores:')
  122. is_transformation_transfer = fields.Boolean('Transformation Transfer:')
  123. is_repair_transfer = fields.Boolean('Repair Transfer:')
  124. is_movil_transfer = fields.Boolean('Movil Transfer:')
  125. is_exhibition = fields.Boolean('Exhibition/Demonstration:')
  126. is_fair = fields.Boolean('Fair Participation:')
  127. another_transfer = fields.Text('Another Transfer motive')
  128. sale_voucher = fields.Char('Sale Voucher')
  129. nota_line = fields.One2many('sale.nota.remision.line', 'nota_remision_id', 'Note Lines', readonly=True, states={'draft': [('readonly', False)]}, copy=True)
  130. amount_total = fields.Float('Amount Total',readonly=True)
  131. _defaults = {
  132. 'state': 'draft',
  133. 'is_sale': True,
  134. 'name': lambda obj, cr, uid, context: '/',
  135. }
  136. class sale_nota_remision_line(models.Model):
  137. _name = 'sale.nota.remision.line'
  138. _description = "Sales Order Reference Note Line"
  139. @api.one
  140. @api.model
  141. def _amount_line(self):
  142. for line in self.ids:
  143. line_obj = self.env['sale.nota.remision.line'].search([('id','=',line)])
  144. price = line_obj.price_unit*(1 - (line_obj.discount or 0.0) / 100.0)
  145. self.price_subtotal = price*line_obj.product_uom_qty
  146. #fields
  147. nota_remision_id = fields.Many2one('sale.nota.remision', 'Note Reference', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]})
  148. name = fields.Text('Description', readonly=True, states={'draft': [('readonly', False)]})
  149. product_id = fields.Many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict')
  150. product_uom_qty = fields.Float('Quantity', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]})
  151. price_unit = fields.Float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]})
  152. discount = fields.Float('Discount (%)', digits_compute= dp.get_precision('Discount'), readonly=True, states={'draft': [('readonly', False)]})
  153. price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')
  154. # price_subtotal = fields.Float(compute='_amount_line', string='Subtotal', digits_compute= dp.get_precision('Account'))
  155. state = fields.Selection(
  156. [('cancel', 'Cancelled'),('draft', 'Draft'),('progress', 'Progress'),('done', 'Done')],
  157. 'Status', required=True, readonly=True, copy=False,
  158. help='* The \'Draft\' status is set when the related note order in draft status. \
  159. \n* The \'Progress\' status is set when the related note order is in progress. \
  160. \n* The \'Done\' status is set when the note order line has been picked. \
  161. \n* The \'Cancelled\' status is set when a user cancel the note order related.')
  162. _order = 'nota_remision_id desc, id'
  163. _defaults = {
  164. 'state': 'draft',
  165. }
  166. class sale_order(models.Model):
  167. _name = 'sale.order'
  168. _inherit = 'sale.order'
  169. @api.multi
  170. def _note_reference_exists(self):
  171. print self.note_reference_ids
  172. if self.note_reference_ids:
  173. self.note_reference_exists=True
  174. else:
  175. self.note_reference_exists=False
  176. note_reference_exists = fields.Boolean(string="Note exists", compute='_note_reference_exists', store="True")
  177. note_reference_ids = fields.Many2one('sale.nota.remision', 'Reference Note')
  178. @api.multi
  179. def action_button_view_note(self):
  180. # print 'Ver Nota'
  181. return {
  182. 'type': 'ir.actions.act_window',
  183. 'res_model': 'sale.nota.remision',
  184. 'view_type': 'form',
  185. 'view_mode': 'form',
  186. 'target': 'current',
  187. 'res_id':self.note_reference_ids.id,
  188. }
  189. @api.multi
  190. def action_button_create_note(self):
  191. # print "Crear Nota de Remision"
  192. # print self.partner_id.id
  193. # print self
  194. nsr = self.env['sale.nota.remision'].search([('origin','=',self.name)])
  195. if not nsr:
  196. valores = {'partner_id':self.partner_id.id,
  197. 'partner_street':self.partner_id.street,
  198. 'origin':self.name,
  199. 'initial_transfer_date':self.date_order,
  200. 'finish_transfer_date':self.date_order,
  201. 'user_id':self.user_id.id,
  202. 'amount_total':self.amount_total,
  203. 'partner_company':self.partner_id.company_id.id,
  204. }
  205. #crear la nota de remision
  206. # print 'Nota creada'
  207. nr = self.env['sale.nota.remision'].create(valores)
  208. # print nr
  209. if nr:
  210. self.note_reference_ids=nr
  211. self._note_reference_exists()
  212. #copiar las lineas del pedido
  213. for line in self.order_line:
  214. line_order = self.env['sale.order.line'].search([('id','=',line.id)])
  215. if line_order:
  216. valores = {'nota_remision_id':nr[0].id,
  217. 'product_id':line_order[0].product_id.id,
  218. 'name':line_order[0].name,
  219. 'product_uom_qty':line_order[0].product_uom_qty,
  220. 'price_unit':line_order[0].price_unit,
  221. 'discount':line_order[0].discount,
  222. }
  223. nrl = self.env['sale.nota.remision.line'].create(valores)
  224. # else:
  225. # print 'La Nota ya existe'
  226. return True
  227. _defaults = {
  228. 'note_reference_exists':False,
  229. }