Skip to content

Commit

Permalink
[14.0][IMP] custom_mrp_descarga: Add reader onchange custom.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berezi committed Dec 9, 2024
1 parent 8faf006 commit 94072a9
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
3 changes: 3 additions & 0 deletions custom_mrp_descarga/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"stock_move_in_out_qty",
"mrp_production_date",
"stock_inventory_import_cost",
"product_expiry",
"stock_move_line_product_lot_reader",
"product_multi_company_usability",
],
"data": [
"data/quartering_product.xml",
Expand Down
10 changes: 7 additions & 3 deletions custom_mrp_descarga/models/stock_inventory_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ def onchange_cost(self):
if entry_line and sum(entry_line.mapped("qty_done")) != 0:
dev_line_amount = sum(dev_line.mapped("amount")) or 0
dev_line_qty = sum(dev_line.mapped("qty_done")) or 0
self.cost = (sum(entry_line.mapped("amount")) - dev_line_amount) / (
sum(entry_line.mapped("qty_done")) - dev_line_qty
)
if sum(entry_line.mapped("qty_done")) - dev_line_qty != 0:
cost = (sum(entry_line.mapped("amount")) - dev_line_amount) / (
sum(entry_line.mapped("qty_done")) - dev_line_qty
)
else:
cost = 0
self.cost = cost
elif self.product_id:
entry_line = self.inventory_id.batch_id.move_line_ids.filtered(
lambda c: c.product_id == self.product_id
Expand Down
112 changes: 112 additions & 0 deletions custom_mrp_descarga/models/stock_move_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2022 Berezi Amubieta - AvanzOSC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime

import pytz

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
Expand Down Expand Up @@ -52,6 +55,115 @@ def _compute_performance(self):
performance = (line.qty_done * 100) / line.production_id.consume_qty
line.performance = performance

@api.onchange("reader")
def onchange_reader(self):
if self.reader:
if (
len(self.reader) == 44
and self.reader[16] == "1"
and self.reader[17] == "5"
):
self.reader_44_15(reader=self.reader)
elif (
len(self.reader) == 46
and self.reader[16] == "3"
and self.reader[17] == "1"
):
self.reader_46_31(reader=self.reader)
else:
message = _("Unidentified barcode format : %(reader)s") % {
"reader": self.reader,
}
raise ValidationError(message)

def reader_44_15(self, reader):
self.ensure_one()
if reader and len(reader) == 44 and reader[16] == "1" and reader[17] == "5":
product_code = reader[2:16]
product_domain = [("default_code", "=", product_code)]
product = (
self.env["product.product"]
.search(product_domain)
.filtered(
lambda c: c.company_id == self.company_id
or self.company_id in c.company_ids
)
)
if not product:
message = _(
"Product not found, reader information for product code: %(reader)s"
) % {
"reader": product_code,
}
raise ValidationError(message)
self.product_id = product.id
expiration_date = reader[18:24]
timezone = pytz.timezone(self._context.get("tz") or "UTC")
expiration_date = datetime.strptime(expiration_date, "%y%m%d")
expiration_date = timezone.localize(expiration_date).astimezone(pytz.UTC)
expiration_date = expiration_date.replace(tzinfo=None)
qty_done = reader[28:31]
qty_done_decimal = reader[31:34]
qty_done = qty_done + "." + qty_done_decimal
qty_done = float(qty_done)
lot_name = reader[36:44]
lot_domain = [
("name", "=", lot_name),
("company_id", "=", self.company_id.id),
("product_id", "=", product.id),
]
lot = self.env["stock.production.lot"].search(lot_domain)
if not lot:
lot = self.env["stock.production.lot"].action_create_lot(
product, lot_name, self.company_id
)
self.write(
{
"expiration_date": expiration_date,
"qty_done": qty_done,
"lot_id": lot.id,
}
)

def reader_46_31(self, reader):
self.ensure_one()
if reader and len(reader) == 46 and reader[16] == "3" and reader[17] == "1":
product_code = reader[2:16]
product_domain = [("default_code", "=", product_code)]
product = (
self.env["product.product"]
.search(product_domain)
.filtered(
lambda c: c.company_id == self.company_id
or self.company_id in c.company_ids
)
)
if not product:
message = _(
"Product not found, reader information for product code: %(reader)s"
) % {
"reader": product_code,
}
raise ValidationError(message)
self.product_id = product.id
qty_done = reader[20:23]
qty_done_decimal = reader[23:26]
qty_done = qty_done + "." + qty_done_decimal
container = int(reader[28:36])
lot_name = reader[38:46]
lot_domain = [
("name", "=", lot_name),
("company_id", "=", self.company_id.id),
("product_id", "=", product.id),
]
lot = self.env["stock.production.lot"].search(lot_domain)
if not lot:
lot = self.env["stock.production.lot"].action_create_lot(
product, lot_name, self.company_id
)
vals = {"container": container, "qty_done": qty_done, "lot_id": lot.id}
self.write(vals)

@api.onchange("unit")
def onchange_unit(self):
super(StockMoveLine, self).onchange_unit()
Expand Down
6 changes: 6 additions & 0 deletions custom_mrp_descarga/views/stock_move_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
</tree>
<field name="product_id" position="before">
<field name="sequence" string=" " />
<field
name="reader"
attrs="{'readonly': ('state', 'in', ('done', 'cancel'))}"
optional="show"
/>
<field name="expiration_date" optional="hide" force_save="1" />
</field>
<field name="location_dest_id" position="after">
<field
Expand Down

0 comments on commit 94072a9

Please sign in to comment.