Search 1.9 billion lines of Odoo code on GitHub

demo_sale_scan_barcode

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>&#39;depends&#39;: [......, &#39;barcodes&#39;], ...... ```</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 = &quot;sale.order&quot; _inherit = [&quot;sale.order&quot;, &quot;barcodes.barcode</em>events_mixin&quot;]</p> <pre><code>_barcode_scanned = fields.Char(string=&#39;Barcode&#39;, help=&quot;Here you can provide the barcode for the product&quot;) @api.multi def on_barcode_scanned(self, barcode): product_obj = self.env[&#39;product.product&#39;].search([(&#39;barcode&#39;, &#39;=&#39;, barcode)], limit=1) val = { &#39;product_id&#39;: product_obj, &#39;product_uom_qty&#39;: 1, &#39;price_unit&#39;: 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): &quot;&quot;&quot; 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. &quot;&quot;&quot;</p> <pre><code>_name = &#39;barcodes.barcode_events_mixin&#39; _description = &#39;Barcode Event Mixin&#39; _barcode_scanned = fields.Char(&quot;Barcode Scanned&quot;, help=&quot;Value of the last barcode scanned.&quot;, store=False) @api.onchange(&#39;_barcode_scanned&#39;) def _on_barcode_scanned(self): barcode = self._barcode_scanned if barcode: self._barcode_scanned = &quot;&quot; return self.on_barcode_scanned(barcode) def on_barcode_scanned(self, barcode): raise NotImplementedError(&quot;In order to use barcodes.barcode_events_mixin, method on_barcode_scanned must be implemented&quot;) </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 &lt;?xml version=&quot;1.0&quot;?&gt; &lt;odoo&gt; &lt;record id=&quot;view_order_form_scan_barcode&quot; model=&quot;ir.ui.view&quot;&gt; &lt;field name=&quot;name&quot;&gt;sale.order.form.scan.barcode&lt;/field&gt; &lt;field name=&quot;model&quot;&gt;sale.order&lt;/field&gt; &lt;field name=&quot;inherit_id&quot; ref=&quot;sale.view_order_form&quot;/&gt; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt; &lt;field name=&quot;partner_id&quot; position=&quot;after&quot;&gt; &lt;!-- invisible=&quot;1&quot; --&gt; &lt;field name=&quot;_barcode_scanned&quot; widget=&quot;barcode_handler&quot;/&gt; &lt;/field&gt; &lt;/field&gt; &lt;/record&gt; &lt;/odoo&gt; </code></p> <p>這邊主要是加上 <code>&lt;field name=&quot;_barcode_scanned&quot; widget=&quot;barcode_handler&quot;/&gt;</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>