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), 'readonly': (Eval('state').in_(['cancelled', 'done'])),}, 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' service_maintenance_initial = fields.Boolean('Maintenance Initial', states={'readonly': True}) @classmethod def __setup__(cls): super(ShipmentOut, cls).__setup__() cls._buttons.update({ 'maintenance_initial': {'invisible': If(Eval('service_maintenance_initial') == True, True)}}) 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 def maintenance_initial(cls, shipments): pool = Pool() MaintenanceService = pool.get('optical_equipment_maintenance.service') Maintenance = pool.get('optical_equipment.maintenance') SaleLine = pool.get('sale.line') Equipments = pool.get('optical_equipment.equipment') for shipment in shipments: for move in shipment.inventory_moves: count = 0 if move.equipment: 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.propietarys += (shipment.customer,) equipment.maintenance_frequency = "6" if shipment.customer.client_type == "ips" else "12" count+=1 equipment.save() else: count+=1 for shipment in shipments: sale_origin = shipment.outgoing_moves[0].origin.sale.id saleLine = SaleLine( type='line', quantity=1, unit_price=0, sale=sale_origin) saleLine.save() maintenanceService = MaintenanceService( sale_date=shipment.outgoing_moves[0].origin.sale.sale_date, sale_origin=saleLine, maintenance_type='initial', propietary=shipment.customer.id, propietary_address=shipment.delivery_address.id) maintenanceService.save() serial = False for move in shipment.inventory_moves: if move.product_equipment and move.equipment: serial = True else: serial = False if serial == True: for move in shipment.inventory_moves: if move.product_equipment and move.equipment: maintenance = Maintenance( service_maintenance=maintenanceService.id, maintenance_type='initial', propietary=shipment.customer.id, propietary_address=shipment.delivery_address.id, equipment=move.equipment.id) maintenance.save() shipment.service_maintenance_initial = True else: raise UserError(str('Por favor Primero debe Asignar un serial a todos los Equipos.')) 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(),})