sale_order_barcode.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 OpenSynergy Indonesia
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api, _
  5. import openerp.addons.decimal_precision as dp
  6. from openerp.exceptions import Warning as UserError
  7. class SaleOrderBarcode(models.TransientModel):
  8. _name = 'sale.order.barcode'
  9. _description = 'Sale Order Barcode Wizard'
  10. product_barcode = fields.Char(
  11. string='Product Barcode (EAN13)',
  12. help="This field is designed to be filled with a barcode reader")
  13. product_id = fields.Many2one(
  14. comodel_name='product.product',
  15. string='Product',
  16. required=True)
  17. product_name = fields.Many2one(
  18. comodel_name='product.product',
  19. string='Product',
  20. related='product_id')
  21. product_qty = fields.Float(
  22. string='Quantity',
  23. digits=dp.get_precision('Product Unit of Measure'),
  24. default=1.0)
  25. product_uom_id = fields.Many2one(
  26. comodel_name='product.uom',
  27. string='Unit of Measure',
  28. required=True)
  29. type = fields.Selection(
  30. [
  31. ('insert', 'Insert'),
  32. ('update', 'Update'),
  33. ],
  34. string='Type',
  35. required=True,
  36. default=lambda self: self._context.get('type', 'insert')
  37. )
  38. @api.multi
  39. def _prepare_domain(self):
  40. self.ensure_one()
  41. domain = [
  42. ('ean13', 'like', self.product_barcode),
  43. ('sale_ok', '=', True)
  44. ]
  45. return domain
  46. @api.multi
  47. def _check_uom(self, lines):
  48. self.ensure_one()
  49. new_uom = self.product_uom_id.id
  50. old_uom = lines.product_uom.id
  51. if new_uom != old_uom:
  52. return False
  53. else:
  54. return True
  55. @api.multi
  56. def _convert_uom(self, lines, product_qty):
  57. self.ensure_one()
  58. obj_product = self.env['product.uom']
  59. from_uom = self.product_uom_id.id
  60. to_uom = lines.product_uom.id
  61. return obj_product._compute_qty(
  62. from_uom_id=from_uom,
  63. qty=product_qty,
  64. to_uom_id=to_uom)
  65. @api.onchange('product_barcode')
  66. def product_barcode_onchange(self):
  67. obj_product = self.env['product.product']
  68. if self.product_barcode:
  69. domain = self._prepare_domain()
  70. products = obj_product.search(domain)
  71. if len(products) == 1:
  72. self.product_id = products[0]
  73. elif len(products) > 1:
  74. return {'warning': {
  75. 'title': _('Error'),
  76. 'message': _(
  77. 'Several products have been found '
  78. 'with this code as EAN13 or Internal Reference:\n %s'
  79. '\nYou should select the right product manually.'
  80. ) % '\n'.join([
  81. product.name_get()[0][1] for product in products
  82. ])}}
  83. else:
  84. return {'warning': {
  85. 'title': _('Error'),
  86. 'message': _(
  87. 'No product found with this code as '
  88. 'EAN13 or product is not for sale. You should select '
  89. 'the right product manually.')}}
  90. @api.onchange('product_id')
  91. def product_id_onchange(self):
  92. if self.product_id:
  93. self.product_uom_id = self.product_id.uom_id.id
  94. # self.create_sale_order_line()
  95. @api.multi
  96. def create_sale_order_line(self):
  97. self.ensure_one()
  98. obj_sale_order_line = self.env['sale.order.line']
  99. active_id = self._context['active_id']
  100. lines = obj_sale_order_line.search([
  101. ('order_id', '=', active_id),
  102. ('product_id', '=', self.product_id.id),
  103. ])
  104. if len(lines) == 1:
  105. check_uom = self._check_uom(lines)
  106. if check_uom is True:
  107. product_qty = self.product_qty
  108. else:
  109. product_qty = self._convert_uom(
  110. lines,
  111. self.product_qty
  112. )
  113. if self.type == 'insert':
  114. lines.write({
  115. 'product_uom_qty': lines.product_uom_qty + product_qty
  116. })
  117. elif self.type == 'update':
  118. lines.write({
  119. 'product_uom_qty': product_qty
  120. })
  121. elif len(lines) > 1:
  122. raise UserError(_("More than 1 line found"))
  123. elif len(lines) == 0:
  124. obj_sale_order_line.create({
  125. 'order_id': active_id,
  126. 'product_id': self.product_id.id,
  127. 'product_uom': self.product_uom_id.id,
  128. 'product_uom_qty': self.product_qty
  129. })
  130. @api.multi
  131. def save(self):
  132. self.ensure_one()
  133. self.create_sale_order_line()
  134. action = {
  135. 'name': _('Sale Order Barcode Interface'),
  136. 'type': 'ir.actions.act_window',
  137. 'res_model': 'sale.order.barcode',
  138. 'view_mode': 'form',
  139. 'nodestroy': True,
  140. 'target': 'new',
  141. 'context': self._context,
  142. }
  143. return action