Skip to content

Commit

Permalink
[IMP] custom_mrp_import_bizerba: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
oihane committed Jun 27, 2024
1 parent 936507a commit afa50b4
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 135 deletions.
18 changes: 10 additions & 8 deletions custom_mrp_import_bizerba/data/product_qty_decimal_precision.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<odoo>
<data noupdate="1">
<record forcecreate="True" id="decimal_bizerba_product_qty" model="decimal.precision">
<field name="name">Bizerba Product Qty Decimal Precision</field>
<field name="digits">15</field>
</record>
</data>
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<record
forcecreate="True"
id="decimal_bizerba_product_qty"
model="decimal.precision"
>
<field name="name">Bizerba Product Qty Decimal Precision</field>
<field name="digits">15</field>
</record>
</odoo>

55 changes: 25 additions & 30 deletions custom_mrp_import_bizerba/models/bizerba_import_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class BizerbaImportLine(models.Model):
_description = "Wizard lines to import Bizerba lines"

production_id = fields.Many2one(
string="Production",
comodel_name="mrp.production"
string="Production",
comodel_name="mrp.production",
)
action = fields.Selection(
string="Action",
Expand Down Expand Up @@ -80,51 +80,46 @@ def _action_validate(self):
action = "nothing"
if state != "error":
action = "create"
update_values.update({
"line_uom_id": uom and uom.id,
"line_product_id": product and product.id,
"log_info": "\n".join(log_infos),
"state": state,
"action": action,
})
update_values.update(
{
"line_uom_id": uom and uom.id,
"line_product_id": product and product.id,
"log_info": "\n".join(log_infos),
"state": state,
"action": action,
}
)
return update_values

def _action_process(self):
update_values = super()._action_process()
if self.action == "create":
log_info = ""
move_line = self.production_id.move_line_ids.filtered(
lambda c: c.product_id == self.line_product_id
)
lambda c: c.product_id == self.line_product_id
)
if move_line:
same_product_lines = (
self.production_id.import_line_ids.filtered(
lambda c: c.line_product_id == self.line_product_id
)
same_product_lines = self.production_id.import_line_ids.filtered(
lambda c: c.line_product_id == self.line_product_id
)
move_line[:1].write(
{
"container": len(same_product_lines),
"qty_done": sum(same_product_lines.mapped("line_product_qty")),
"product_uom_id": self.line_uom_id.id,
}
)
{
"container": len(same_product_lines),
"qty_done": sum(same_product_lines.mapped("line_product_qty")),
"product_uom_id": self.line_uom_id.id,
}
)
for line in same_product_lines:
line.write({
"action": "nothing",
"state": "done"
})
line.write({"action": "nothing", "state": "done"})
move_line[:1].onchange_container()
move_line[:1].onchange_unit()
else:
log_info = _("Error: There is no entry line with this product.")
state = "error" if log_info else "done"
action = "nothing"
update_values.update({
"log_info": log_info,
"action": action,
"state": state
})
update_values.update(
{"log_info": log_info, "action": action, "state": state}
)
return update_values

def _check_product(self):
Expand Down
80 changes: 44 additions & 36 deletions custom_mrp_import_bizerba/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# Copyright 2022 Berezi Amubieta - AvanzOSC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime, timedelta

import pymssql
import pytz

from odoo import _, api, fields, models
from odoo.addons.base_import_wizard.models.base_import import IMPORT_STATUS
from odoo.exceptions import ValidationError
from datetime import datetime, timedelta, time
import pytz
import pymssql

from odoo.addons.base_import_wizard.models.base_import import IMPORT_STATUS


class MrpProduction(models.Model):
_name = "mrp.production"
_inherit = ["mrp.production", "base.import", "mail.thread",
"mail.activity.mixin"]
_inherit = ["mrp.production", "base.import", "mail.thread", "mail.activity.mixin"]

name = fields.Char(
compute=False
)
data = fields.Binary(
required=False
)
name = fields.Char(compute=False)
data = fields.Binary(required=False)
import_line_ids = fields.One2many(
comodel_name="bizerba.import.line",
inverse_name="production_id"
inverse_name="production_id",
)
import_state = fields.Selection(
selection=IMPORT_STATUS,
Expand Down Expand Up @@ -53,25 +51,26 @@ def action_conect_with_bizerba(self):
self.ensure_one()
self.import_line_ids.unlink()
self.action_confirm()
if not self.clasified_date or not (
self.clasified_time_start) and not (
self.clasified_time_stop):
raise ValidationError(_(
"This classification does not have a start/end " +
"time or date."))
if (
not self.clasified_date
or not self.clasified_time_start
and not self.clasified_time_stop
):
raise ValidationError(
_("This classification does not have a start/end " + "time or date.")
)
if not self.lot_producing_id:
raise ValidationError(_(
"This classification does not have the lot."))
raise ValidationError(_("This classification does not have the lot."))
date = self.clasified_date
time_start = self.clasified_time_start
if time_start == 0:
date = date - timedelta(days=1)
time_start = "{0:02.0f}:{1:02.0f}".format(*divmod(time_start * 60, 60))
time_start = "{:02.0f}:{:02.0f}".format(*divmod(time_start * 60, 60))
time_start = datetime.strptime(time_start, "%H:%M")
time_start = time_start - timedelta(minutes=1)
time_start = time_start.time()
time_stop = self.clasified_time_stop
time_stop = "{0:02.0f}:{1:02.0f}".format(*divmod(time_stop * 60, 60))
time_stop = "{:02.0f}:{:02.0f}".format(*divmod(time_stop * 60, 60))
time_stop = datetime.strptime(time_stop, "%H:%M").time()
start_date = "{} {}".format(date, time_start)
start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")
Expand All @@ -81,12 +80,22 @@ def action_conect_with_bizerba(self):
stop_date = datetime.strptime(stop_date, "%Y-%m-%d %H:%M:%S")
try:
conn = pymssql.connect(
server='192.168.201.3\SQLEXPRESS',
user='integracion',
password='ew#211218',
database='BCT2DB')
server=r"192.168.201.3\SQLEXPRESS",
user="integracion",
password="ew#211218",
database="BCT2DB",
)
cursor = conn.cursor()
query = "SELECT * FROM [Pesada Individual] as p WHERE LEFT(p.GT1F,4)+'-'+SUBSTRING(p.GT1F,5,2)+'-'+SUBSTRING(p.GT1F,7,2)+' '+SUBSTRING(p.GT1F,10,2)+':'+RIGHT(p.GT1F,2)+':00' BETWEEN '" +str(start_date)+"' AND '"+str(stop_date)+"'"
query = (
"SELECT * FROM [Pesada Individual] as p "
"WHERE LEFT(p.GT1F,4)+'-'+SUBSTRING(p.GT1F,5,2)+'-'"
"+SUBSTRING(p.GT1F,7,2)+' '+SUBSTRING(p.GT1F,10,2)+"
"':'+RIGHT(p.GT1F,2)+':00' BETWEEN '"
+ str(start_date)
+ "' AND '"
+ str(stop_date)
+ "'"
)
cursor.execute(query)
row = cursor.fetchone()
while row:
Expand All @@ -97,16 +106,16 @@ def action_conect_with_bizerba(self):
line_product_qty = line_product_qty.split(";")
line_uom = line_product_qty[0]
line_product_qty = float(line_product_qty[2]) * pow(
10, float(line_product_qty[1]))
10, float(line_product_qty[1])
)
line_chicken_code = row[12]
line_date = row[13]
line_date = datetime.strptime(line_date, "%Y%m%d-%H%M")
log_info = ""
timezone = pytz.timezone(self._context.get('tz') or 'UTC')
line_date = timezone.localize(
line_date).astimezone(pytz.UTC)
timezone = pytz.timezone(self._context.get("tz") or "UTC")
line_date = timezone.localize(line_date).astimezone(pytz.UTC)
line_date = line_date.replace(tzinfo=None)
line_data = ({
line_data = {
"import_id": self.id,
"production_id": self.id,
"line_lot": line_lot,
Expand All @@ -116,7 +125,7 @@ def action_conect_with_bizerba(self):
"line_chicken_code": line_chicken_code,
"line_date": line_date,
"log_info": log_info,
})
}
self.import_line_ids = [(0, 0, line_data)]
row = cursor.fetchone()
conn.close()
Expand All @@ -125,5 +134,4 @@ def action_conect_with_bizerba(self):
if self.import_line_ids:
self.action_validate()
except Exception:
raise ValidationError(_(
"The connection could not be established."))
raise ValidationError(_("The connection could not be established."))
100 changes: 68 additions & 32 deletions custom_mrp_import_bizerba/views/bizerba_import_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@
<field name="model">bizerba.import.line</field>
<field name="arch" type="xml">
<search>
<field name="line_product_id"/>
<field name="line_product_code"/>
<field name="line_chicken_code"/>
<separator/>
<filter string="Errored" name="error" domain="[('state', '=', 'error')]"/>
<separator/>
<filter string="To Create" name="2create" domain="[('action', '=', 'create')]"/>
<field name="line_product_id" />
<field name="line_product_code" />
<field name="line_chicken_code" />
<separator />
<filter
string="Errored"
name="error"
domain="[('state', '=', 'error')]"
/>
<separator />
<filter
string="To Create"
name="2create"
domain="[('action', '=', 'create')]"
/>
<group expand="0" name="group_by" string="Group By">
<filter name="group_product" string="Product" context="{'group_by': 'line_product_id'}"/>
<filter name="group_state" string="State" context="{'group_by': 'state'}"/>
<filter
name="group_product"
string="Product"
context="{'group_by': 'line_product_id'}"
/>
<filter
name="group_state"
string="State"
context="{'group_by': 'state'}"
/>
</group>
</search>
</field>
Expand All @@ -24,24 +40,39 @@
<field name="arch" type="xml">
<form>
<header>
<button name="action_validate" string="Validate" type="object" class="oe_read_only" states="2validate,error"/>
<button name="action_process" string="Process" type="object" class="oe_read_only" states="pass"/>
<field name="state" widget="statusbar"/>
<button
name="action_validate"
string="Validate"
type="object"
class="oe_read_only"
states="2validate,error"
/>
<button
name="action_process"
string="Process"
type="object"
class="oe_read_only"
states="pass"
/>
<field name="state" widget="statusbar" />
</header>
<sheet>
<group>
<group>
<field name="line_product_code"/>
<field name="line_product_qty"/>
<field name="line_chicken_code"/>
<field name="line_date"/>
<field name="line_lot"/>
<field name="line_product_code" />
<field name="line_product_qty" />
<field name="line_chicken_code" />
<field name="line_date" />
<field name="line_lot" />
</group>
<group>
<field name="line_product_id" options="{'no_create': True, 'no_open': True}"/>
<field
name="line_product_id"
options="{'no_create': True, 'no_open': True}"
/>
</group>
</group>
<field name="log_info"/>
<field name="log_info" />
</sheet>
</form>
</field>
Expand All @@ -50,19 +81,24 @@
<record id="bizerba_import_line_view_tree" model="ir.ui.view">
<field name="model">bizerba.import.line</field>
<field name="arch" type="xml">
<tree decoration-danger="state == 'error'" decoration-muted="state == 'done'" decoration-it="log_info" editable="top">
<field name="action" readonly="1"/>
<field name="production_id" invisible="1"/>
<field name="line_product_code" readonly="1"/>
<field name="line_product_qty" readonly="1"/>
<field name="line_uom" readonly="1"/>
<field name="line_uom_id" optional="show" readonly="1"/>
<field name="line_chicken_code" readonly="1"/>
<field name="line_lot" readonly="1"/>
<field name="line_date" readonly="1"/>
<field name="line_product_id" optional="show" readonly="1"/>
<field name="state" readonly="1"/>
<field name="log_info" optional="show" readonly="1"/>
<tree
decoration-danger="state == 'error'"
decoration-muted="state == 'done'"
decoration-it="log_info"
editable="top"
>
<field name="action" readonly="1" />
<field name="production_id" invisible="1" />
<field name="line_product_code" readonly="1" />
<field name="line_product_qty" readonly="1" />
<field name="line_uom" readonly="1" />
<field name="line_uom_id" optional="show" readonly="1" />
<field name="line_chicken_code" readonly="1" />
<field name="line_lot" readonly="1" />
<field name="line_date" readonly="1" />
<field name="line_product_id" optional="show" readonly="1" />
<field name="state" readonly="1" />
<field name="log_info" optional="show" readonly="1" />
</tree>
</field>
</record>
Expand Down
Loading

0 comments on commit afa50b4

Please sign in to comment.