sale_order_barcode.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_qty = fields.Float(
  18. string='Quantity',
  19. digits=dp.get_precision('Product Unit of Measure'),
  20. default=1.0)
  21. product_uom_id = fields.Many2one(
  22. comodel_name='product.uom',
  23. string='Unit of Measure',
  24. required=True)
  25. type = fields.Selection(
  26. [
  27. ('insert', 'Insert'),
  28. ('update', 'Update'),
  29. ],
  30. string='Type',
  31. required=True,
  32. default=lambda self: self._context.get('type', 'insert')
  33. )
  34. @api.multi
  35. def _prepare_domain(self):
  36. self.ensure_one()
  37. domain = [
  38. ('ean13', '=', self.product_barcode),
  39. ('sale_ok', '=', True)
  40. ]
  41. return domain
  42. @api.multi
  43. def _check_uom(self, lines):
  44. self.ensure_one()
  45. new_uom = self.product_uom_id.id
  46. old_uom = lines.product_uom.id
  47. if new_uom != old_uom:
  48. return False
  49. else:
  50. return True
  51. @api.multi
  52. def _convert_uom(self, lines, product_qty):
  53. self.ensure_one()
  54. obj_product = self.env['product.uom']
  55. from_uom = self.product_uom_id.id
  56. to_uom = lines.product_uom.id
  57. return obj_product._compute_qty(
  58. from_uom_id=from_uom,
  59. qty=product_qty,
  60. to_uom_id=to_uom)
  61. @api.onchange('product_barcode')
  62. def product_barcode_onchange(self):
  63. obj_product = self.env['product.product']
  64. if self.product_barcode:
  65. domain = self._prepare_domain()
  66. products = obj_product.search(domain)
  67. if len(products) == 1:
  68. self.product_id = products[0]
  69. elif len(products) > 1:
  70. return {'warning': {
  71. 'title': _('Error'),
  72. 'message': _(
  73. 'Several products have been found '
  74. 'with this code as EAN13 or Internal Reference:\n %s'
  75. '\nYou should select the right product manually.'
  76. ) % '\n'.join([
  77. product.name_get()[0][1] for product in products
  78. ])}}
  79. else:
  80. return {'warning': {
  81. 'title': _('Error'),
  82. 'message': _(
  83. 'No product found with this code as '
  84. 'EAN13 or product is not for sale. You should select '
  85. 'the right product manually.')}}
  86. @api.onchange('product_id')
  87. def product_id_onchange(self):
  88. if self.product_id:
  89. self.product_uom_id = self.product_id.uom_id.id
  90. @api.multi
  91. def create_sale_order_line(self):
  92. self.ensure_one()
  93. obj_sale_order_line = self.env['sale.order.line']
  94. active_id = self._context['active_id']
  95. lines = obj_sale_order_line.search([
  96. ('order_id', '=', active_id),
  97. ('product_id', '=', self.product_id.id),
  98. ])
  99. if len(lines) == 1:
  100. check_uom = self._check_uom(lines)
  101. if check_uom is True:
  102. product_qty = self.product_qty
  103. else:
  104. product_qty = self._convert_uom(
  105. lines,
  106. self.product_qty
  107. )
  108. if self.type == 'insert':
  109. lines.write({
  110. 'product_uom_qty': lines.product_uom_qty + product_qty
  111. })
  112. elif self.type == 'update':
  113. lines.write({
  114. 'product_uom_qty': product_qty
  115. })
  116. elif len(lines) > 1:
  117. raise UserError(_("More than 1 line found"))
  118. elif len(lines) == 0:
  119. obj_sale_order_line.create({
  120. 'order_id': active_id,
  121. 'product_id': self.product_id.id,
  122. 'product_uom': self.product_uom_id.id,
  123. 'product_uom_qty': self.product_qty
  124. })
  125. @api.multi
  126. def save(self):
  127. self.ensure_one()
  128. self.create_sale_order_line()
  129. action = {
  130. 'name': _('Sale Order Barcode Interface'),
  131. 'type': 'ir.actions.act_window',
  132. 'res_model': 'sale.order.barcode',
  133. 'view_mode': 'form',
  134. 'nodestroy': True,
  135. 'target': 'new',
  136. 'context': self._context,
  137. }
  138. return action