197 lines
6.7 KiB
Python
197 lines
6.7 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
|
|
from trytond.exceptions import UserError
|
|
from itertools import groupby
|
|
from trytond.transaction import Transaction
|
|
|
|
|
|
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),},
|
|
depends=['product_equipment'])
|
|
|
|
equipment_serial = fields.Function(fields.Char('Serial',
|
|
states={'readonly': True,
|
|
'invisible': If(~Eval('product_equipment'), True)},
|
|
depends=['product_equipment']),
|
|
'get_equipment_serial')
|
|
product_equipment = fields.Function(fields.Boolean("It Equipment"),'get_product_equipment')
|
|
|
|
@fields.depends('product')
|
|
def get_product_equipment(self, product):
|
|
if self.product.equipment:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
@fields.depends('equipment')
|
|
def get_equipment_serial(self, equipment):
|
|
if self.equipment:
|
|
return self.equipment.serial
|
|
else:
|
|
return None
|
|
|
|
@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
|
|
|
|
|
|
@fields.depends(methods=['get_equipment_serial'])
|
|
def on_change_equipment(self):
|
|
if self.equipment:
|
|
self.product = self.equipment.product.id
|
|
self.equipment_serial = self.get_equipment_serial(self.equipment)
|
|
else:
|
|
self.equipment_serial = None
|
|
|
|
|
|
|
|
class ShipmentOut(metaclass=PoolMeta):
|
|
"Customer Shipment"
|
|
__name__ = 'stock.shipment.out'
|
|
|
|
|
|
def get_outgoing_moves(self, name):
|
|
moves = []
|
|
if self.state == 'done':
|
|
for move in self.inventory_moves:
|
|
moves.append(move.id)
|
|
return moves
|
|
else:
|
|
for move in self.moves:
|
|
if move.from_location == self.warehouse_output:
|
|
moves.append(move.id)
|
|
return moves
|
|
|
|
@classmethod
|
|
def view_attributes(cls):
|
|
return super(ShipmentOut, cls).view_attributes() + [
|
|
('//page[@name="inventory_moves"]', 'states', {
|
|
'invisible': False,
|
|
}),]
|
|
|
|
|
|
@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')
|
|
|
|
Equipments = pool.get('optical_equipment.equipment')
|
|
|
|
for shipment in shipments:
|
|
for move in shipment.inventory_moves:
|
|
count = 0
|
|
if move.equipment:
|
|
#raise UserError(str((move.origin)))
|
|
equipment = move.equipment
|
|
Id = equipment.id
|
|
equipment = Equipments.search(['id', '=',Id])[0]
|
|
equipment.propietary = shipment.customer.id
|
|
equipment.propietary_address= shipment.delivery_address.id
|
|
equipment.state="uncontrated"
|
|
equipment.shipment_destination = shipment
|
|
equipment.sale_destination = shipment.outgoing_moves[count].origin
|
|
equipment.maintenance_frequency = "6" if shipment.customer.client_type == "ips" else "12"
|
|
count+=1
|
|
equipment.save()
|
|
else:
|
|
count+=1
|
|
|
|
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
|
|
|
|
class ShipmentInternal(metaclass=PoolMeta):
|
|
'Shipment Interncal'
|
|
__name__ = 'stock.shipment.internal'
|
|
|
|
@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')
|
|
|
|
for shipment in shipments:
|
|
for move in shipment.moves:
|
|
if move.equipment:
|
|
move.equipment.location = shipment.to_location
|
|
move.equipment.save()
|
|
|
|
Move.do([m for s in shipments for m in s.incoming_moves])
|
|
cls.write([s for s in shipments if not s.effective_date], {
|
|
|
|
'effective_date': Date.today(),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|