Exemplo n.º 1
0
    def change_lsp(self, request, **kwargs):
        data = kwargs.get('data',{})
        agent = kwargs.get('agent')
        o_log = kwargs.get('order_log')
        
        lsp = data.get('lsp')
        tracking_number = data.get('tracking_number')
        if not lsp:
            return
        
        from orders.models import OrderLog
        if not o_log:
            o_log = OrderLog(order=self.order, profile=agent, action='shipment')
            shipment_log = ShipmentLog(shipment=self, action='update',
                delivery_number=self.delivery_number)

        self.lsp = lsp
        self.tracking_number = tracking_number
        self.save()
        shipment_log.lsp = lsp
        shipment_log.tracking_number = tracking_number
        
        o_log.save()
        shipment_log.order_log = o_log
        shipment_log.save()
        return
Exemplo n.º 2
0
    def move_shipment_state(self, request, **kwargs):
        new_state = kwargs.get('new_state')
        agent = kwargs.get('agent')
        shipment_item = kwargs.get('shipment_item')
        o_log = kwargs.get('order_log')

        data = kwargs.get('data',{})
        order = self.order
        
        from orders.models import OrderLog, OrderItemLog
        if not o_log:
            shipment_logs = ShipmentLog.objects.select_related('order_log').filter(
                shipment=self, action='status', status=new_state).order_by('-id')[:1]
            if shipment_logs:
                shipment_log = shipment_logs[0]
                o_log = shipment_log.order_log
            else:
                o_log = OrderLog(order=order, profile=agent, action='shipment')
                shipment_log = ShipmentLog(shipment=self, action='status', status=new_state,
                    delivery_number=self.delivery_number)
        
        valid_states = []
        if not self.status:
            valid_states = ['delivery created']
        if self.status == 'delivery created':
            valid_states = ['invoiced','deleted']
        elif self.status == 'invoiced':
            valid_states = ['shipped','deleted','delivered','returned','undeliverable']
        elif self.status == 'shipped':
            valid_states = ['delivered','returned','undeliverable']
        elif self.status == 'undeliverable':
            valid_states = ['delivered','returned','undeliverable']
        
        if (new_state != self.status) and (new_state not in valid_states):
            raise self.InvalidOperation
        
        self.status = new_state
        shipment_log.status = new_state
        if new_state == 'delivery created':
            delivery_number = data.get('delivery_number')
            dc = data.get('dc')
            tracking_number = data.get('tracking_number')
            lsp = data.get('lsp')
            if not (delivery_number and dc):
                raise self.InsufficientData
            self.delivery_number = delivery_number
            self.dc = dc
            shipment_log.delivery_number = delivery_number
            shipment_log.dc = dc
            if tracking_number:
                self.tracking_number = tracking_number
                shipment_log.tracking_number = tracking_number
            if lsp:
                self.lsp = lsp
                shipment_log.lsp = lsp
            self.save()
            
            if shipment_item:
                if shipment_item.quantity == shipment_item.order_item.qty:
                    item_status = 'delivery created'
                elif shipment_item.quantity < shipment_item.order_item.qty:
                    item_status = 'partial stock'
                else:
                    raise Exception('shipment item quantity (%s) greater than order item quantity (%s)' %
                    (shipment_item.quantity, shipment_item.order_item.qty))
                
                if not o_log.id:
                    o_log.save()
                shipment_log.order_log = o_log
                shipment_log.save()
                if shipment_item.sap_order_item.status not in ['cancelled', item_status]:
                    shipment_item.sap_order_item.move_state(request, new_state=item_status, order_log=o_log)
        
        elif new_state == 'invoiced':
            invoice_number = data.get('invoice_number')
            invoiced_on = data.get('invoiced_on')
            tracking_number = data.get('tracking_number')
            lsp = data.get('lsp')
            if not (invoice_number and invoiced_on):
                raise self.InsufficientData
            self.invoice_number = invoice_number
            self.invoiced_on = invoiced_on
            shipment_log.invoice_number = invoice_number
            if (not self.tracking_number) and tracking_number:
                self.tracking_number = tracking_number
                shipment_log.tracking_number = tracking_number
            if (not self.lsp) and lsp:
                self.lsp = lsp
                shipment_log.lsp = lsp
            self.save()
            if order.support_state != 'invoiced' and order.all_items_invoiced(request):
                order.support_state = 'invoiced'
                order.save()
                o_log.status = 'invoiced'
            o_log.save()
            shipment_log.order_log = o_log
            shipment_log.save()
        
        elif new_state == 'deleted':
            self.save()
            if order.support_state != 'confirmed':
                order.support_state = 'confirmed'
                order.save()
                o_log.status = 'confirmed'

            o_log.save()
            shipment_log.order_log = o_log
            shipment_log.save()

            shipment_items = self.shipment_items.select_related('sap_order_item').all()
            for shipment_item in shipment_items:
                shipment_item.sap_order_item.status = 'awaiting delivery creation'
                shipment_item.sap_order_item.save()
                OrderItemLog.objects.create(order_item=shipment_item.order_item, order_log=o_log,
                    action='status', status='awaiting delivery creation')

        else:
            tracking_number = data.get('tracking_number')
            lsp = data.get('lsp')
            pickedup_on = data.get('pickedup_on')
            delivered_on = data.get('delivered_on')
            if not (tracking_number and lsp):
                raise self.InsufficientData
            self.tracking_number = tracking_number
            self.lsp = lsp
            self.pickedup_on = pickedup_on
            shipment_log.tracking_number = tracking_number
            shipment_log.lsp = lsp
            if new_state == 'delivered':
                self.delivered_on = delivered_on or datetime.now()
            elif new_state == 'returned':
                self.returned_on = datetime.now()
            self.save()
            if new_state == 'shipped' and order.all_items_shipped(request):
                order.support_state = 'shipped'
                order.save()
                o_log.status = 'shipped'
                try:
                    # can remove once we are confident about emails
                    order.notify_shipped_order(request, self)
                except:
                    pass
            elif new_state == 'delivered' and order.all_items_delivered(request):
                order.support_state = 'delivered'
                order.save()
                o_log.status = 'delivered'
                if order.payment_mode == 'cod':
                    pas = order.get_payments(request, filter=dict(status='pending realization',
                        payment_mode='cod', amount=order.payable_amount)).order_by('-id')
                    i = 0
                    for pa in pas:
                        if i == 0:
                            pa.move_payment_state(request, new_state='paid', order_log=o_log)
                        else:
                            pa.status = 'rejected'
                            pa.save()
                        i += 1
            elif new_state == 'returned' and order.all_items_returned(request):
                order.support_state = 'returned'
                order.save()
                o_log.status = 'returned'
            elif new_state == 'undeliverable' and order.all_items_undeliverable(request):
                order.support_state = 'undeliverable'
                order.save()
                o_log.status = 'undeliverable'
            o_log.save()
            shipment_log.order_log = o_log
            shipment_log.save()
        return o_log