def getDispatches(self,request):
        params = request.query_params
        if 'toDate' in params and 'fromDate' in params:
            dis = Dispatch.objects.filter(date__range=[getServerDateFromStr(params['fromDate']),getServerDateFromStr(params['toDate'])])
            return dis

        if 'fromDate' in params:
            dis = Dispatch.objects.filter(date__gte=getServerDateFromStr(params['fromDate']))
            return dis

        dis = Dispatch.objects.all()
        return dis
 def addDispatch(self,request):
     params = request.data;
     ds = Dispatch()
     ds.date = getServerDateFromStr(params['date'])
     ds.vanNo = params['vanNo']
     ds.name = params['name']
     ds.remarks = params.get('remarks','')
     ds.save()
     for fnId in params['forwardingNotes']:
         fn = ForwardingNote.objects.get(pk=fnId)
         fn.isDispatched = True
         fn.save()
         ds.forwardingNote.add(fn)
     return ds
    def addForwardingNote(self,request):
        params = request.data;
        fn = ForwardingNote()
        if 'billDates' in params:
            fn.billDates = ",".join(params['billDates'])
        fn.fnDate = getServerDateFromStr(params['fnDate'])
        fn.billNo = params.get('billNo','')
        fn.billValues = params['billValues']
        fn.cases = params['cases']
        fn.transporter_id = params['transporter_id']
        fn.transporterStation = params['transporterStation']
        # if 'transporter_id' in params:
        #     pass
        # else:
        #     transporter = Transporter()
        #     transporter.name = params['transporter']['name']
        #     transporter.city = params['transporterStation']
        #     transporter.org_id = 1
        #     transporter.save()
        #     fn.transporter_id = transporter.id
        if 'customer_id' in params:
            fn.customer_id = params['customer_id']
        else:
            try:
                customer = Customer.objects.get(name__iexact=params['customer']['name'],city__iexact=params['customer']['city'])
            except Customer.DoesNotExist:
                customer = Customer()
                customer.name = params['customer']['name']
                customer.city = params['customer']['city']
                customer.org_id =1
                customer.save()
            fn.customer_id = customer.id


        fn.marka = params['marka']
        if 'companyId' in params:
            fn.company_id = params['companyId']
        fn.transporterStation = params['transporterStation']
        fn.permitNo = params.get('permitNo','')
        fn.commodity = params.get('commodity','')

        fn.save()
        return fn
 def updateDispatch(self,request):
     params = request.data;
     ds = Dispatch.objects.get(pk=params['id'])
     if ds.isLocked:
         raise PermissionDenied()
     ds.date = getServerDateFromStr(params['date'])
     ds.vanNo = params['vanNo']
     ds.name = params['name']
     ds.remarks = params.get('remarks','')
     fns = ds.forwardingNote.all()
     for ofn in fns:
         ofn.isDispatched = False
         ofn.save()
         ds.forwardingNote.remove(ofn)
     for fnId in params['forwardingNotes']:
         oFn = ds.forwardingNote.filter()
         fn = ForwardingNote.objects.get(pk=fnId)
         fn.isDispatched = True
         fn.save()
         ds.forwardingNote.add(fn)
         # timeout(self.lockDispatch,ds,{},10)
     return ds
    def getForwardingNotes(self,request):
        params = request.query_params
        transporterName = ''
        if 'transporterName' in params:
            transporterName =  params['transporterName']
        if 'toDate' in params and 'fromDate' in params:
            fns = ForwardingNote.objects.filter(isDispatched=False,transporter__name__icontains=transporterName,fnDate__range=[getServerDateFromStr(params['fromDate']),getServerDateFromStr(params['toDate'])]).order_by('-id')
            return fns

        if 'fromDate' in params:
            fns = ForwardingNote.objects.filter(isDispatched=False,transporter__name__icontains=transporterName,fnDate__gte=getServerDateFromStr(params['fromDate'])).order_by('-id')
            return fns

        fns = ForwardingNote.objects.filter(isDispatched=False,transporter__name__icontains=transporterName).order_by('-id')
        return fns