summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/barcoding_product.py
blob: 204c61287a962177c8659d3156db8d1ae777a032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from odoo import models, api, fields
from odoo.exceptions import AccessError, UserError, ValidationError
from datetime import timedelta, date, datetime
import logging

_logger = logging.getLogger(__name__)

class BarcodingProduct(models.Model):
    _name = "barcoding.product"
    _description = "Barcoding Product"

    barcoding_product_line = fields.One2many('barcoding.product.line', 'barcoding_product_id', string='Barcoding Product Lines', auto_join=True)
    product_id = fields.Many2one('product.product', string="Product", tracking=3)
    quantity = fields.Float(string="Quantity", tracking=3)
    type = fields.Selection([('print', 'Print Barcode'), ('barcoding', 'Add Barcode To Product'), ('barcoding_box', 'Add Barcode Box To Product')], string='Type', default='print')
    barcode = fields.Char(string="Barcode")
    qty_pcs_box = fields.Char(string="Quantity Pcs Box")

    def check_duplicate_barcode(self):
        if self.type in ['barcoding_box', 'barcoding']:
            barcode_product = self.env['product.product'].search([('barcode', '=', self.barcode)])

            if barcode_product:
                raise UserError('Barcode sudah digunakan {}'.format(barcode_product.display_name))
            
            barcode_box = self.env['product.product'].search([('barcode_box', '=', self.barcode)])

            if barcode_box:
                raise UserError('Barcode box sudah digunakan {}'.format(barcode_box.display_name))

    @api.constrains('barcode')
    def _send_barcode_to_product(self):
        for record in self:
            record.check_duplicate_barcode()
            if record.type == 'barcoding_box':
                record.product_id.barcode_box = record.barcode
                record.product_id.qty_pcs_box = record.qty_pcs_box
            else:
                record.product_id.barcode = record.barcode

    @api.onchange('product_id', 'quantity')
    def _onchange_product_or_quantity(self):
        """Update barcoding_product_line based on product_id and quantity"""
        if self.product_id and self.quantity > 0:
            # Clear existing lines
            self.barcoding_product_line = [(5, 0, 0)]

            # Add a new line with the current product and quantity
            self.barcoding_product_line = [(0, 0, {
                'product_id': self.product_id.id,
                'barcoding_product_id': self.id,
            }) for _ in range(int(self.quantity))]
    
    
class BarcodingProductLine(models.Model):
    _name = 'barcoding.product.line'
    _description = 'Barcoding Product Line'
    _order = 'barcoding_product_id, id'

    barcoding_product_id = fields.Many2one('barcoding.product', string='Barcoding Product Ref', required=True, ondelete='cascade', index=True, copy=False)
    product_id = fields.Many2one('product.product', string="Product")
    qr_code_variant = fields.Binary("QR Code Variant", related='product_id.qr_code_variant')