def list(self, request):
        medicine = Medicine.objects.all()
        serializer = MedicineSerliazer(medicine,
                                       many=True,
                                       context={"request": request})

        medicine_data = serializer.data
        newmedicinelist = []

        #Adding Extra Key for Medicine Details in Medicine
        for medicine in medicine_data:
            #Accessing All the Medicine Details of Current Medicine ID
            medicine_details = MedicalDetails.objects.filter(
                medicine_id=medicine["id"])
            medicine_details_serializers = MedicalDetailsSerializerSimple(
                medicine_details, many=True)
            medicine["medicine_details"] = medicine_details_serializers.data
            newmedicinelist.append(medicine)

        response_dict = {
            "error": False,
            "message": "All Medicine List Data",
            "data": newmedicinelist
        }
        return Response(response_dict)
示例#2
0
    def retrieve(self,request,pk=None):
        queryset=Medicine.objects.all()
        medicine=get_object_or_404(queryset,pk=pk)
        serializer=MedicineSerliazer(medicine,context={"request":request})

        serializer_data=serializer.data
        # Accessing All the Medicine Details of Current Medicine ID
        medicine_details = MedicalDetails.objects.filter(medicine_id=serializer_data["id"])
        medicine_details_serializers = MedicalDetailsSerializerSimple(medicine_details, many=True)
        serializer_data["medicine_details"] = medicine_details_serializers.data

        return Response({"error":False,"message":"Single Data Fetch","data":serializer_data})
    def update(self, request, pk=None):
        queryset = Medicine.objects.all()
        medicine = get_object_or_404(queryset, pk=pk)
        serializer = MedicineSerliazer(medicine,
                                       data=request.data,
                                       context={"request": request})
        serializer.is_valid()
        serializer.save()
        #print(request.data["medicine_details"])
        for salt_detail in request.data["medicine_details"]:
            if salt_detail["id"] == 0:
                #For Insert New Salt Details
                del salt_detail["id"]
                salt_detail["medicine_id"] = serializer.data["id"]
                serializer2 = MedicalDetailsSerializer(
                    data=salt_detail, context={"request": request})
                serializer2.is_valid()
                serializer2.save()
            else:
                #For Update Salt Details
                queryset2 = MedicalDetails.objects.all()
                medicine_salt = get_object_or_404(queryset2,
                                                  pk=salt_detail["id"])
                del salt_detail["id"]
                serializer3 = MedicalDetailsSerializer(
                    medicine_salt,
                    data=salt_detail,
                    context={"request": request})
                serializer3.is_valid()
                serializer3.save()
                print("UPDATE")

        return Response({"error": False, "message": "Data Has Been Updated"})
示例#4
0
    def create(self,request):
        try:
            serializer=MedicineSerliazer(data=request.data,context={"request":request})
            serializer.is_valid(raise_exception=True)
            serializer.save()

            medicine_id=serializer.data['id']
            #Access The Serializer Id Which JUSt SAVE in OUR DATABASE TABLE
            #print(medicine_id)

            #Adding and Saving Id into Medicine Details Table
            medicine_details_list=[]
            for medicine_detail in request.data["medicine_details"]:
                print(medicine_detail)
                #Adding medicine id which will work for medicine details serializer
                medicine_detail["medicine_id"]=medicine_id
                medicine_details_list.append(medicine_detail)
                print(medicine_detail)

            serializer2=MedicalDetailsSerializer(data=medicine_details_list,many=True,context={"request":request})
            serializer2.is_valid()
            serializer2.save()

            dict_response={"error":False,"message":"Medicine Data Save Successfully"}
        except:
            dict_response={"error":True,"message":"Error During Saving Medicine Data"}
        return Response(dict_response)
    def list(self, request):
        customer_request = CustomerRequest.objects.all()
        customer_request_serializer = CustomerRequestSerializer(
            customer_request, many=True, context={"request": request})

        bill_count = Bill.objects.all()
        bill_count_serializer = BillSerializer(bill_count,
                                               many=True,
                                               context={"request": request})

        medicine_count = Medicine.objects.all()
        medicine_count_serializer = MedicineSerliazer(
            medicine_count, many=True, context={"request": request})

        company_count = Company.objects.all()
        company_count_serializer = CompanySerliazer(
            company_count, many=True, context={"request": request})

        employee_count = Employee.objects.all()
        employee_count_serializer = EmployeeSerializer(
            employee_count, many=True, context={"request": request})

        bill_details = BillDetails.objects.all()
        profit_amt = 0
        sell_amt = 0
        buy_amt = 0
        for bill in bill_details:
            buy_amt = float(buy_amt + float(bill.medicine_id.buy_price)) * int(
                bill.qty)
            sell_amt = float(sell_amt +
                             float(bill.medicine_id.sell_price)) * int(
                                 bill.qty)

        profit_amt = sell_amt - buy_amt

        customer_request_pending = CustomerRequest.objects.filter(status=False)
        customer_request_pending_serializer = CustomerRequestSerializer(
            customer_request_pending, many=True, context={"request": request})

        customer_request_completed = CustomerRequest.objects.filter(
            status=True)
        customer_request_completed_serializer = CustomerRequestSerializer(
            customer_request_completed,
            many=True,
            context={"request": request})

        current_date = datetime.today().strftime("%Y-%m-%d")
        current_date1 = datetime.today()
        current_date_7days = current_date1 + timedelta(days=7)
        current_date_7days = current_date_7days.strftime("%Y-%m-%d")
        bill_details_today = BillDetails.objects.filter(
            added_on__date=current_date)
        profit_amt_today = 0
        sell_amt_today = 0
        buy_amt_today = 0
        for bill in bill_details_today:
            buy_amt_today = float(buy_amt_today +
                                  float(bill.medicine_id.buy_price)) * int(
                                      bill.qty)
            sell_amt_today = float(sell_amt_today +
                                   float(bill.medicine_id.sell_price)) * int(
                                       bill.qty)

        profit_amt_today = sell_amt_today - buy_amt_today

        medicine_expire = Medicine.objects.filter(
            expire_date__range=[current_date, current_date_7days])
        medicine_expire_serializer = MedicineSerliazer(
            medicine_expire, many=True, context={"request": request})

        bill_dates = BillDetails.objects.order_by().values(
            "added_on__date").distinct()
        profit_chart_list = []
        sell_chart_list = []
        buy_chart_list = []
        for billdate in bill_dates:
            access_date = billdate["added_on__date"]

            bill_data = BillDetails.objects.filter(added_on__date=access_date)
            profit_amt_inner = 0
            sell_amt_inner = 0
            buy_amt_inner = 0

            for billsingle in bill_data:
                buy_amt_inner = float(buy_amt_inner + float(
                    billsingle.medicine_id.buy_price)) * int(billsingle.qty)
                sell_amt_inner = float(sell_amt_inner + float(
                    billsingle.medicine_id.sell_price)) * int(billsingle.qty)

            profit_amt_inner = sell_amt_inner - buy_amt_inner

            profit_chart_list.append({
                "date": access_date,
                "amt": profit_amt_inner
            })
            sell_chart_list.append({
                "date": access_date,
                "amt": sell_amt_inner
            })
            buy_chart_list.append({"date": access_date, "amt": buy_amt_inner})

        dict_respone = {
            "error": False,
            "message": "Home Page Data",
            "customer_request": len(customer_request_serializer.data),
            "bill_count": len(bill_count_serializer.data),
            "medicine_count": len(medicine_count_serializer.data),
            "company_count": len(company_count_serializer.data),
            "employee_count": len(employee_count_serializer.data),
            "sell_total": sell_amt,
            "buy_total": buy_amt,
            "profit_total": profit_amt,
            "request_pending": len(customer_request_pending_serializer.data),
            "request_completed":
            len(customer_request_completed_serializer.data),
            "profit_amt_today": profit_amt_today,
            "sell_amt_today": sell_amt_today,
            "medicine_expire_serializer_data":
            len(medicine_expire_serializer.data),
            "sell_chart": sell_chart_list,
            "buy_chart": buy_chart_list,
            "profit_chart": profit_chart_list
        }
        return Response(dict_respone)