120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
from trytond.model import fields, ModelSQL, ModelView, Workflow, dualmethod
|
|
from trytond.modules.company.model import employee_field, set_employee
|
|
from trytond.pool import Pool, PoolMeta
|
|
from trytond.pyson import Eval, If
|
|
|
|
|
|
class Move(metaclass=PoolMeta):
|
|
"Stock Move"
|
|
__name__ = "stock.move"
|
|
|
|
equipment = fields.Many2One('optical_equipment.equipment', "Equipment",
|
|
domain=[('state', '=', 'registred'),
|
|
('product','=', Eval('product'))
|
|
],
|
|
states={'invisible': If(~Eval('product_equipment'), True)},)
|
|
|
|
equipment_serial = fields.Char('Serial',states={'readonly': True})
|
|
|
|
|
|
@fields.depends('product', 'equipment', 'uom')
|
|
def on_change_product(self):
|
|
if self.product:
|
|
if (not self.uom
|
|
or self.uom.category != self.product.default_uom.category):
|
|
self.uom = self.product.default_uom
|
|
|
|
|
|
|
|
def on_change_equipment(self):
|
|
if self.equipment:
|
|
self.product = self.equipment.product.id
|
|
self.equipment_serial = self.equipment.serial
|
|
|
|
else:
|
|
self.equipment_serial = None
|
|
|
|
|
|
|
|
class ShipmentOut(metaclass=PoolMeta):
|
|
"Customer Shipment"
|
|
__name__ = 'stock.shipment.out'
|
|
|
|
|
|
@classmethod
|
|
@ModelView.button
|
|
@Workflow.transition('done')
|
|
@set_employee('done_by')
|
|
def done(cls, shipments):
|
|
pool = Pool()
|
|
Move = pool.get('stock.move')
|
|
Date = pool.get('ir.date')
|
|
|
|
|
|
pool = Pool()
|
|
Equipments = pool.get('optical_equipment.equipment')
|
|
|
|
for shipment in shipments:
|
|
for move in shipment.moves:
|
|
if move.product.equipment:
|
|
equipment=move.serial
|
|
equipment.propietary=shipment.customer.id
|
|
equipment.propietary_address= shipment.delivery_address.id
|
|
equipment.state="uncontrated"
|
|
equipment.shipment_destination = move
|
|
equipment.maintenance_frequency = "6" if sale.party.client_type == "ips" else "12"
|
|
equipment.save()
|
|
|
|
Move.delete([
|
|
m for s in shipments for m in s.outgoing_moves
|
|
if m.state == 'staging'])
|
|
|
|
Move.do([m for s in shipments for m in s.outgoing_moves])
|
|
for company, c_shipments in groupby(
|
|
shipments, key=lambda s: s.company):
|
|
with Transaction().set_context(company=company.id):
|
|
|
|
today = Date.today()
|
|
|
|
cls.write([s for s in c_shipments if not s.effective_date], {
|
|
'effective_date': today,
|
|
})
|
|
|
|
def _get_inventory_move(self, move):
|
|
'Return inventory move for the outgoing move if necessary'
|
|
pool = Pool()
|
|
Move = pool.get('stock.move')
|
|
Uom = pool.get('product.uom')
|
|
quantity = move.quantity
|
|
|
|
for inventory_move in self.inventory_moves:
|
|
if (inventory_move.origin == move
|
|
and inventory_move.state != 'cancelled'):
|
|
quantity -= Uom.compute_qty(
|
|
inventory_move.uom, inventory_move.quantity, move.uom)
|
|
quantity = move.uom.round(quantity)
|
|
|
|
if quantity <= 0:
|
|
return
|
|
|
|
inventory_move = Move(
|
|
from_location=self.warehouse_storage,
|
|
to_location=move.from_location,
|
|
product=move.product,
|
|
uom=move.uom,
|
|
quantity=quantity,
|
|
shipment=self,
|
|
planned_date=move.planned_date,
|
|
company=move.company,
|
|
origin=move,
|
|
state='staging' if move.state == 'staging' else 'draft',
|
|
)
|
|
|
|
if inventory_move.on_change_with_unit_price_required():
|
|
|
|
inventory_move.unit_price = move.unit_price
|
|
|
|
inventory_move.currency = move.currency
|
|
|
|
return inventory_move
|