如何在 Odoo 16 中创建条形码

让我们讨论如何在 Odoo 16 中创建条形码。众所周知,条形码是机器可读的数据的视觉表示。它使用不同宽度和间距的平行线图案表示数字或字母数据。条形码扫描仪读取并分析条形码中编码的信息,然后由计算机处理。
让我们考虑两种情况, 

1.以生成产品EAN13标准条码为例。 
 

导入数学

从 odoo 导入 api、模型


class ProductAutoBarcode(models.Model): 

    _inherit = 'product.product' 

    @api.model 

    def create(self, vals): 

        """创建新产品时生成条形码""" 

        res = super(ProductAutoBarcode, self).create(vals) 

        ean = generate_ean(str(res.id)) 

        res.barcode = '21' + ean[2:] 

        return res 


def ean_checksum(eancode): 

    """返回长度为 13 的 ean 字符串的校验和,如果

    字符串长度错误则返回 -1""" 

    if len(eancode) != 13: 

        return -1 

    oddsum = 0 

    evensum = 0 

    eanvalue = eancode 

    reversevalue = eanvalue[::-1] 

    finalean = reversevalue[1:] 

    for i in range(len(finalean)): 

        if i % 2 == 0: 

            oddsum += int(finalean[i]) 

        else: 

            evensum += int(finalean[i]) 

    total = (oddsum * 3) + evensum 

    check = int(10 - math.ceil(total % 10.0)) % 10 

    return check 


def check_ean(eancode): 

    """如果 eancode 是有效的 ean13 字符串,则返回 True,否则返回 null"""

    如果不是 eancode:

        返回 True

    如果 len(eancode) != 13:

        返回 False 

    try: 

        int(eancode) 

    except:

        返回 False 

    return ean_checksum(eancode) 


def generate_ean(ean): 

    """从无效的 ean13 创建并返回有效的 ean13"""

    如果不是 ean:

        返回 "0000000000000" 

    product_identifier = '00000000000' + ean 

    ean = product_identifier[-11:] 

    check_number = check_ean(ean + '00') 

    return f'{ean}0{check_number}' 


class ProductTemplateAutoBarcode(models.Model): 

    _inherit = 'product.template' 

    @api.model 

    def create(self, vals_list): 

        """创建新产品时生成条形码编号""" 

        templates = super(ProductTemplateAutoBarcode, self).create(vals_list) 

        ean = generate_ean(str(templates.id)) 

        templates.barcode = '22' + ean[2:] 

        return templates
在这里,我们将为产品生成条形码。EAN-13 是欧洲最广泛认可的条形码,用于超市和其他零售企业的基本产品识别。它是美国 UPC-A 条形码的欧洲版本。
有关更多信息,您可以参考以下应用程序链接https://apps.odoo.com/apps/modules/16.0/product_barcode/

2. 接下来,我们可以检查如何为序列生成条形码并将其显示在 Odoo 中的报告中。

打印.py文件:
def generate_barcode(self):  

   data = {  

       'response':self.name,  

   }  

   return self.env.ref('module_name.action_generate_barcode')。report_action(self,data = data)

打印按钮 XML:
 

<header> 

            <button name="generate_barcode" string="生成条形码" type="object"/> 

</header>

打印barcode_action.xml:
 

<?xml version="1.0" encoding="utf-8"?> 

 <odoo> 

       <record id="action_generate_barcode" model="ir.actions.report"> 

            <field name="name"> 条形码</field> 

            <field name="model">module.name</field> 

            <field name="report_type">qweb-pdf</field> 

            <field name="report_name">module_name.print_barcode</field> 

            <field name="report_file">module_name.print_barcode</field> 

      </record> 

</odoo>

打印barcode_template.xml:
 

<? xml 版本 ="1.0" 编码 ="UTF-8" ?>  

<odoo>  

<template id ="print_barcode_custom" >  

    <t t-call ="web.basic_layout" >  

        <div class ="page" >  

            <div class = "col-md-6" >  

                <img class ="barcode" 

                      t-att-src ="'/report/barcode/?barcode_type=%s & value=%s & width=%s & height=% s & humanreadable=1 & quiet=0' % ('Code128',response, 205, 67)" 

                      alt ="条形码" />  

            </div>  

        </div>  

    </t>  

</template>  

</ odoo>
要了解有关管理 Odoo 16 更多信息,请参阅我博客的其他内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的蜗牛..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值