Author: |
My Company |
License: |
no license |
Branch: |
14.0 |
Repository: |
twtrubiks/odoo-demo-addons-tutorial |
Dependencies: |
barcodes,
sale,
and
sale_management |
Languages: |
Markdown (92, 67.6%),
Python (31, 22.8%),
and
XML (13, 9.6%) |
Other branches: |
master |
<h1>odoo 實作 scan barcode</h1>
<p>建議觀看影片, 因為 scanner 會看的更清楚:smile:</p>
<p><a href="https://youtu.be/o2THTpLmUec">Youtube Tutorial - odoo - 實作 scan barcode</a></p>
<p>建議在閱讀這篇文章之前, 請先確保了解看過以下的文章 (因為都有連貫的關係)</p>
<p><a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_odoo_tutorial">odoo 手把手建立第一個 addons</a></p>
<p>odoo 搭配 scanner,</p>
<p>主要介紹 odoo 中如何實作 scan barcode.</p>
<h2>說明</h2>
<p>首先是 <a href="__manifest__.py"><strong>manifest</strong>.py</a> 的部份, 記得要加上 <code>barcodes</code></p>
<p>```python
......</p>
<p>'depends': [......, 'barcodes'],
......
```</p>
<p>接著看 <a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_sale_scan_barcode/models/models.py">models/models.py</a></p>
<p>```python
......</p>
<p>class SaleOrderBarcodes(models.Model):
<em>name = "sale.order"
_inherit = ["sale.order", "barcodes.barcode</em>events_mixin"]</p>
<pre><code>_barcode_scanned = fields.Char(string='Barcode', help="Here you can provide the barcode for the product")
@api.multi
def on_barcode_scanned(self, barcode):
product_obj = self.env['product.product'].search([('barcode', '=', barcode)], limit=1)
val = {
'product_id': product_obj,
'product_uom_qty': 1,
'price_unit': product_obj.lst_price
}
self.order_line = [(0, 0, val)]
</code></pre>
<p>```</p>
<p><code>_inherit</code> 必須繼承 <code>barcodes.barcode_events_mixin</code>,</p>
<p>然後要定義 <code>_barcode_scanned</code>,</p>
<p>資料庫中是不會有 <code>_barcode_scanned</code> 的欄位, 因為 BarcodeEventsMixin 是 AbstractModel,</p>
<p>如果不了解甚麼是 AbstractModel, 請參考 <a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_abstractmodel_tutorial">odoo 手把手教學 - AbstractModel</a>.</p>
<p>也需要實作 <code>on_barcode_scanned</code>, 當 scanner 掃描到 barcode 時, 就會去觸發這個 method.</p>
<p>實作的邏輯很簡單,</p>
<p>就是使用所掃描到的 barcode 去 <code>product.product</code> 中尋找對應的產品,</p>
<p>如果有, 就自動增加到 sale order line 中.</p>
<p>如果不知道如何增加 One2many M2X record,</p>
<p>可參考 <a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_expense_tutorial_v1#odoo-%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E5%AD%B8---%E4%BD%BF%E7%94%A8-python-%E5%A2%9E%E5%8A%A0%E5%8F%96%E4%BB%A3-one2many-m2x-record---part8">odoo 手把手教學 - 使用 python 增加取代 One2many M2X record</a></p>
<p><code>barcode_events_mixin.py</code> 可參考原始碼中的 <code>addons/barcodes/models/barcode_events_mixin.py</code></p>
<p>``<code>python
......
class BarcodeEventsMixin(models.AbstractModel):
""" Mixin class for objects reacting when a barcode is scanned in their form views
which contains</code><field name="_barcode_scanned" widget="barcode_handler"/>`.
Models using this mixin must implement the method on<em>barcode</em>scanned. It works
like an onchange and receives the scanned barcode in parameter.
"""</p>
<pre><code>_name = 'barcodes.barcode_events_mixin'
_description = 'Barcode Event Mixin'
_barcode_scanned = fields.Char("Barcode Scanned", help="Value of the last barcode scanned.", store=False)
@api.onchange('_barcode_scanned')
def _on_barcode_scanned(self):
barcode = self._barcode_scanned
if barcode:
self._barcode_scanned = ""
return self.on_barcode_scanned(barcode)
def on_barcode_scanned(self, barcode):
raise NotImplementedError("In order to use barcodes.barcode_events_mixin, method on_barcode_scanned must be implemented")
</code></pre>
<p>```</p>
<p>views 的部份可參考 <a href="https://github.com/twtrubiks/demo_config_settings/tree/master/demo_sale_scan_barcode/views/view.xml">views/view.xml</a></p>
<p><code>xml
<?xml version="1.0"?>
<odoo>
<record id="view_order_form_scan_barcode" model="ir.ui.view">
<field name="name">sale.order.form.scan.barcode</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<!-- invisible="1" -->
<field name="_barcode_scanned" widget="barcode_handler"/>
</field>
</field>
</record>
</odoo>
</code></p>
<p>這邊主要是加上 <code><field name="_barcode_scanned" widget="barcode_handler"/></code>,</p>
<p>也可以選擇將它隱藏起來, 是不影響工作的.</p>
<p>將 addons 裝起來之後, 先到 Product Variants 設定 barcode,</p>
<p><img src="https://i.imgur.com/m9o8vHY.png" alt="alt tag"></p>
<p>範例 barcode</p>
<p><img src="https://i.imgur.com/0S5Bsu9.png" alt="alt tag"></p>
<p>記住一定要進入編輯 (Edit) 狀態, 否則會出現錯誤:exclamation::exclamation:</p>
<p><img src="https://i.imgur.com/cNzb2VJ.png" alt="alt tag"></p>
<p>也請記得要 focus 在當下的 odoo 畫面 (否則系統會抓不到):exclamation::exclamation:</p>
<p>scanner 掃到條碼後, 就會將對應的產品帶入 sale order line 中.</p>
<p>這邊建議觀看影片, 會比較清楚:smile:</p>
<p>scanner 其實也是將 barcode 或是 qrcode 內的資料讀取出來而已</p>
<p><img src="https://i.imgur.com/K457P5w.png" alt="alt tag"></p>