Esempio n. 1
0
def register_user(request):
    if request.method == 'POST':
        name = request.POST['name']

        phone = request.POST['phone']
        address = request.POST['address']
        age = request.POST['age']
        gender = request.POST['gender']
        total_disease = request.POST['total_disease']
        total_medicine = request.POST['total_medicine']
        # total_price = request.POST['price']
        adhar = request.POST['adhar']

        print(total_disease)
        print(total_medicine)

        patient = Patient()
        patient.name = name

        patient.phone = phone
        patient.age = age
        patient.sex = gender
        patient.address = address
        # patient.total_bill = total_price
        patient.aadhar_id = adhar
        patient.save()

        i = 1
        j = 1

        while i <= int(total_disease):
            disease_name = request.POST['disease' + str(i)]
            disease_obj = get_object_or_404(Disease, name=disease_name)
            disease_allot = DiseaseAllot()
            disease_allot.patient = patient
            disease_allot.disease = disease_obj
            disease_allot.name = disease_name
            disease_allot.save()
            i += 1

        while j <= int(total_medicine):
            medicine_name = request.POST['medicine' + str(j)]
            medicine_qty = request.POST['quantity' + str(j)]

            medicine_obj = get_object_or_404(Medicine, name=medicine_name)

            medicine_allot = MedicineAllot()
            medicine_allot.name = medicine_name
            medicine_allot.patient = patient

            medicine_allot.medicine = medicine_obj

            total_qty = int(medicine_obj.total_quantity) - int(medicine_qty)

            if total_qty > 0:
                medicine_obj.total_quantity = total_qty
                medicine_allot.quantity = medicine_qty
                # medicine_allot.price = int(medicine_obj.tablet_price) * int(medicine_qty)
                messages.success(
                    request,
                    str(total_qty) + " tablets of " + str(medicine_obj.name) +
                    " left in stock")

            elif total_qty == 0:
                medicine_obj.total_quantity = total_qty
                medicine_allot.quantity = medicine_qty
                # medicine_allot.price = int(medicine_obj.tablet_price) * int(medicine_qty)
                medicine_obj.available = False
                messages.success(request,
                                 medicine_obj.name + " is out of stock")

            else:
                medicine_obj.total_quantity = 0
                medicine_obj.available = False
                medicine_allot.quantity = int(medicine_obj.total_quantity)
                # medicine_allot.price = int(medicine_obj.tablet_price) * int(medicine_obj.total_quantity)
                messages.success(request,
                                 medicine_obj.name + " is out of stock")

            medicine_obj.save()
            medicine_allot.save()
            j += 1

        messages.success(request, "User Registered Successfully.")
        return HttpResponseRedirect('/patient/add_patient')

    else:
        return render(request, 'patient/add_patient.html', {})