This commit is contained in:
gsinghpal
2026-03-14 12:04:20 -04:00
parent fc3c966484
commit e9cf75ee48
75 changed files with 6991 additions and 873 deletions

View File

@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)
from . import serial_scan_wizard

View File

@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
# Copyright 2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)
import re
import logging
from odoo import models, fields, api
_logger = logging.getLogger(__name__)
SKIP_WORDS = frozenset({
'total', 'price', 'quantity', 'subtotal', 'discount', 'amount',
'invoice', 'order', 'product', 'delivery', 'shipping', 'tracking',
'payment', 'customer', 'vendor', 'notes', 'description', 'reference',
'number', 'date', 'name', 'address', 'phone', 'email', 'unit',
'piece', 'each', 'item', 'line', 'from', 'with', 'that', 'this',
'have', 'will', 'been', 'were', 'would', 'could', 'should',
})
SERIAL_PATTERN = re.compile(r'(?<!\w)([\w][\w-]{3,}[\w])(?!\w)')
class FusionSerialScanWizard(models.TransientModel):
_name = 'fusion.serial.scan.wizard'
_description = 'Serial Number Scan Wizard'
picking_id = fields.Many2one(
'stock.picking', string='Transfer', required=True, readonly=True)
line_ids = fields.One2many(
'fusion.serial.scan.line', 'wizard_id', string='Results')
scan_summary = fields.Text(string='Summary', readonly=True)
def _scan(self):
"""Extract serial numbers from SO/invoice text and match against stock.lot."""
self.ensure_one()
pick = self.picking_id
so = pick.sale_id or self.env['sale.order'].search(
[('name', '=', pick.origin)], limit=1)
if not so:
self.scan_summary = 'No linked sale order found.'
return
text_sources = self._collect_text_sources(so)
product_ids = pick.move_ids.mapped('product_id').ids
results = []
seen = set()
for source_label, text in text_sources:
clean = re.sub(r'<[^>]+>', ' ', text or '')
candidates = SERIAL_PATTERN.findall(clean)
for candidate in candidates:
if len(candidate) < 5:
continue
key = candidate.upper()
if key in seen or candidate.lower() in SKIP_WORDS:
continue
seen.add(key)
if not re.search(r'\d', candidate):
continue
lots = self.env['stock.lot'].search([
('name', '=ilike', candidate),
], limit=5)
matched_product = lots.filtered(
lambda l: l.product_id.id in product_ids)
results.append({
'wizard_id': self.id,
'serial_text': candidate,
'source': source_label,
'found_in_system': bool(lots),
'matched_product': bool(matched_product),
'lot_id': matched_product[:1].id if matched_product else (
lots[:1].id if lots else False),
'product_id': (matched_product[:1].product_id.id if matched_product
else (lots[:1].product_id.id if lots else False)),
'lot_product_name': (matched_product[:1].product_id.name if matched_product
else (lots[:1].product_id.name if lots else '')),
})
if results:
self.env['fusion.serial.scan.line'].create(results)
found = sum(1 for r in results if r['found_in_system'])
matched = sum(1 for r in results if r['matched_product'])
self.scan_summary = (
f'Scanned {len(text_sources)} text sources. '
f'Found {len(results)} potential serial numbers: '
f'{found} exist in system, {matched} match products in this transfer.'
)
def _collect_text_sources(self, so):
"""Gather all text from SO lines, notes, and related invoices."""
sources = []
for line in so.order_line:
if line.name:
sources.append((f'SO Line: {line.product_id.name}', line.name))
if so.note:
sources.append(('SO Notes', so.note))
if so.internal_note if hasattr(so, 'internal_note') else False:
sources.append(('SO Internal Note', so.internal_note))
for inv in so.invoice_ids.filtered(lambda m: m.state == 'posted'):
for line in inv.invoice_line_ids:
if line.name:
sources.append((
f'Invoice {inv.name}: {line.product_id.name if line.product_id else ""}',
line.name))
if inv.narration:
sources.append((f'Invoice {inv.name} Notes', inv.narration))
return sources
class FusionSerialScanLine(models.TransientModel):
_name = 'fusion.serial.scan.line'
_description = 'Serial Scan Result Line'
wizard_id = fields.Many2one(
'fusion.serial.scan.wizard', ondelete='cascade', required=True)
serial_text = fields.Char(string='Serial Number', readonly=True)
source = fields.Char(string='Found In', readonly=True)
found_in_system = fields.Boolean(string='Exists in System', readonly=True)
matched_product = fields.Boolean(
string='Matches Transfer Product', readonly=True)
lot_id = fields.Many2one('stock.lot', string='Matched Lot', readonly=True)
product_id = fields.Many2one(
'product.product', string='Lot Product', readonly=True)
lot_product_name = fields.Char(string='Lot Product Name', readonly=True)

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_serial_scan_wizard_form" model="ir.ui.view">
<field name="name">fusion.serial.scan.wizard.form</field>
<field name="model">fusion.serial.scan.wizard</field>
<field name="arch" type="xml">
<form string="Serial Number Scan Results">
<group>
<field name="picking_id" readonly="1"/>
<field name="scan_summary" readonly="1"/>
</group>
<field name="line_ids" readonly="1" nolabel="1">
<list>
<field name="serial_text" string="Serial Number"/>
<field name="source" string="Found In"/>
<field name="found_in_system" widget="boolean"
decoration-success="found_in_system"
decoration-danger="not found_in_system"/>
<field name="matched_product" widget="boolean"
string="Matches Transfer"
decoration-success="matched_product"/>
<field name="lot_id" string="Matched Lot"/>
<field name="lot_product_name" string="Lot Product"/>
</list>
</field>
<footer>
<button string="Close" class="btn-primary" special="cancel"/>
</footer>
</form>
</field>
</record>
</odoo>