Add lot compute

This commit is contained in:
Oscar 2021-09-25 00:02:37 -05:00
parent 0643296a97
commit 6fc5033b8a
7 changed files with 76 additions and 28 deletions

View File

@ -4,6 +4,8 @@ from datetime import date
from decimal import Decimal
from operator import itemgetter
from sql import Table
import copy
from trytond.modules.company import CompanyReport
from trytond.pool import Pool, PoolMeta
from trytond.wizard import Wizard, StateTransition, StateView, Button, StateReport
@ -62,6 +64,13 @@ class InternalShipment(metaclass=PoolMeta):
super(InternalShipment, cls).wait(shipments)
cls.set_employee(shipments, 'shipped_by')
@classmethod
@ModelView.button_action('stock.wizard_shipment_internal_assign')
def assign_wizard(cls, shipments):
super(InternalShipment, cls).assign_wizard(shipments)
for sh in shipments:
cls.assign_lots(sh)
@classmethod
def done(cls, shipments):
super(InternalShipment, cls).done(shipments)
@ -73,6 +82,58 @@ class InternalShipment(metaclass=PoolMeta):
if employee_id:
cls.write(shipments, {field: employee_id})
@classmethod
def assign_lots(cls, shipment):
"""
Look for the oldest lot or the one closest to the expiration
date and try assign it to shipment moves
"""
Move = Pool().get('stock.move')
Lot = Pool().get('stock.lot')
def _get_lots(product_id):
locations_ids = {'locations': [shipment.from_location.id]}
with Transaction().set_context(locations_ids):
dom = [
('product', '=', product_id),
('active', '=', True),
('quantity', '>', 0),
]
lots = Lot.search(dom, order=[('create_date', 'ASC')])
return lots
for move in shipment.moves:
if move.product.lot_is_required:
lots_list = _get_lots(move.product.id)
balance_qty = move.quantity
print('lots_list... ', lots_list)
for lt in lots_list:
print('q... ', lt.number, lt.quantity)
if lt.quantity >= balance_qty:
move.lot = lt.id
move.save()
break
else:
balance_qty = balance_qty - lt.quantity
move.quantity = lt.quantity
move.lot = lt.id
move.save()
if balance_qty:
print()
move, = Move.create([{
'quantity': balance_qty,
'internal_quantity': balance_qty,
'product': move.product.id,
'lot': lt.id,
'from_location': move.from_location.id,
'to_location': move.to_location.id,
'shipment': str(shipment),
'uom': move.uom.id,
'company': move.company.id,
'state': move.state,
}])
@classmethod
def import_data(cls, fields_names, data):
pool = Pool()

View File

@ -3,7 +3,9 @@
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
<menuitem parent="stock.menu_stock" sequence="200"
name="Reports" id="menu_reports" icon="tryton-folder"/>
<record model="ir.ui.view" id="create_internal_shipment_start_view_form">
<field name="model">stock_co.create_internal_shipment.start</field>
<field name="type">form</field>

View File

@ -32,25 +32,10 @@ class Move(metaclass=PoolMeta):
current_stock = fields.Function(fields.Float('Current Stock',
depends=['product']), 'on_change_with_current_stock')
def next_lot(self, product_id):
"Look for the oldest lot or the one closest to the expiration date."
Lot = Pool().get('stock.lot')
dom = [
('product', '=', product_id),
('active', '=', True),
('quantity', '>', 0),
]
lots = Lot.search(dom, order=[('create_date', 'ASC')])
if lots:
return lots[0].id
@fields.depends('current_stock', 'lot')
@fields.depends('current_stock')
def on_change_product(self, name=None):
super(Move, self).on_change_product()
self.current_stock = self.on_change_with_current_stock()
if self.product:
self.lot = self.next_lot(self.product.id)
@fields.depends('product', 'from_location', 'to_location')
def on_change_with_current_stock(self, name=None):
@ -67,11 +52,11 @@ class Move(metaclass=PoolMeta):
}
with Transaction().set_context(context):
res_dict = self.product._get_quantity(
[self.product],
'quantity',
[location.id],
grouping_filter=([self.product.id],)
)
[self.product],
'quantity',
[location.id],
grouping_filter=([self.product.id],)
)
if res_dict.get(self.product.id):
res += res_dict[self.product.id]
return res

View File

@ -3,12 +3,15 @@
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
<menuitem parent="stock.menu_stock" sequence="200"
name="Reports" id="menu_reports" icon="tryton-folder"/>
<record model="ir.ui.view" id="lot_view_form">
<field name="model">stock.lot</field>
<field name="inherit" ref="stock_lot.lot_view_form"/>
<field name="name">stock_lot_form</field>
</record>
<!-- <record model="ir.ui.view" id="lot_view_tree">
<field name="model">stock.lot</field>
<field name="inherit" ref="stock_lot.lot_view_tree"/>
@ -45,9 +48,6 @@ this repository contains the full copyright notices and license terms. -->
<field name="wiz_name">stock_co.print_move_by_product</field>
</record>
<menuitem parent="stock.menu_stock" sequence="200"
name="Reports" id="menu_reports" icon="tryton-folder"/>
<menuitem parent="menu_reports" sequence="100"
action="wizard_move_by_product" id="menu_move_by_product"/>
@ -110,5 +110,6 @@ this repository contains the full copyright notices and license terms. -->
</record>
<menuitem parent="menu_reports" sequence="140" icon="tryton-print"
action="wizard_print_produts_report" id="print_products_report"/>
</data>
</tryton>

View File

@ -1,5 +1,5 @@
[tryton]
version=6.0.3
version=6.0.5
depends:
product
stock

View File

@ -2,8 +2,7 @@
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<data>
<xpath
expr="/form/field[@name='number']" position="after">
<xpath expr="/form/field[@name='number']" position="after">
<label name="active"/>
<field name="active"/>
</xpath>

Binary file not shown.