def dashboard(request): validate_access(request, 'ac') # Getting the expenses all = Entries.objects.all() expenses = all.filter(type=False) incomes = all.filter(type=True) # Summing all the prices in a category exp_tally = sum_and_order(expenses) inc_tally = sum_and_order(incomes) print(exp_tally) print(inc_tally) # Summing the income and expenses income, expense = 0, 0 for i in all: if i.type: income += i.price else: expense += i.price netprofit = income - expense return render(request, 'accounts/dashboard.html', context={"exp_tally": exp_tally, "inc_tally": inc_tally, "income": income, "expense": expense, "netprofit": netprofit})
def save_prescription(request): # Getting the user from the request validate_access(request, 'd') # Getting the medicines and the related case case = Cases.objects.get(id=int(request.GET['case_id'])) medicines = json.loads(request.GET["medicines"]) # Printing the value for debubbing purposes print(case, medicines) # Making the medicine instances and relating to case for med in medicines: item = Stock.objects.get(id=int(med)) medicine = Medicine(prescription=case.prescription, item=item, quantity=int(medicines[med])) medicine.save() # Updating prescription state case.prescription.status = 'p' case.prescription.save() # returning success message return HttpResponse("success")
def add_prescription(request): # Getting the user from the request validate_access(request, 'd') # Getting the case from case id case = Cases.objects.get(id=int(request.GET["id"])) # Rendering the html file return render(request, 'doctors/add-prescription.html', context={"case": case})
def view_history(request): validate_access(request, 'd') # Getting the cases from the request object p_id = int(request.GET['id']) patient = Patient.objects.get(id=p_id) cases = patient.cases_set.filter(status='d') return render(request, 'doctors/case-history.html', context={ 'patient': patient, 'cases': cases })
def shop(request): # Checking if user is allowed visit the site validate_access(request, 'p') # Getting the list of stocks items = Stock.objects.filter(deleted=False) items = list(items) for item in items: # Removing out of stock items if item.quantity < 1: items.remove(item) return render(request, "pharma/shop.html", context={'items': items})
def bill_info(request): # Getting the orders user = validate_access(request, 'p') orders = user.staff.order_set.all() print(orders) print(request.GET) # Redirecting back to home if there are no orders if not orders: return redirect("pharma:shop") # Checking if the form data is recieved if request.method == "POST": form = BillForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] phone = form.cleaned_data['phone'] # Redirecting user to prescription page response = redirect('pharma:bill') response['Location'] += f'?name={name}&phone={phone}' return response else: form = BillForm() return render(request, "pharma/bill_info.html", context={'form': form})
def place(request): user = validate_access(request, 'p') ID = request.GET['product_id'] qty = int(request.GET['product_amount']) item = Stock.objects.filter(id=ID)[0] orders = user.staff.order_set.all() # Checking if item already in cart in_cart = False for order in orders: if order.item.id == item.id: in_cart = True # If the item is already in cart if in_cart: order = user.staff.order_set.filter(item__id=item.id)[0] order.quantity += qty # Creating a new order if not already there else: order = Order(user=user.staff, item=item, quantity=qty) # Updating the item stock and saving changes item.quantity -= qty order.save() item.save() return HttpResponse("success")
def bill(request): # Getting the orders user = validate_access(request, 'p') orders = user.staff.order_set.all() print(orders) print(request.GET) # Redirecting back to home if there are no orders if not orders: return redirect("pharma:shop") # Generating the bill objects bill = utils.generate_bill(name=request.GET["name"], contact_num=request.GET["phone"], date=timezone.now(), orders=orders) # Clearing the cart for order in orders: order.delete() # getting all the bill units to send in context bill_units = bill.billunit_set.all() print(bill_units, bill) total = utils.get_bill_total(bill_units) return render(request, 'pharma/bill.html', context={ 'bill': bill, 'bill_unit': bill_units, 'total': total })
def remove_stock(request): # making sure the user is a pharmacist validate_access(request, "p") # Deleting the product if product_id is sent if 'product_id' in request.GET: print(f"deleting {request.GET['product_id']}") id = int(request.GET['product_id']) item = Stock.objects.get(id=id) item.deleted = True item.save() # loading the list of stocks items = Stock.objects.filter(deleted=False) return render(request, 'pharma/remove_stock.html', context={'items': items})
def add_stock(request): validate_access(request, 'p') if request.method == "POST": form = StockForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] price = form.cleaned_data['price'] quantity = form.cleaned_data['quantity'] stock = Stock.objects.create(name=name, desc="med", price=price, quantity=quantity) stock.save() return redirect("pharma:shop") return render(request, 'pharma/add_stock.html')
def entries(request): validate_access(request, 'ac') # Handling the request to save entries if 'entries' in request.GET: data = json.loads(request.GET['entries']) for ent in data.values(): t = ent[3] == 'Income' entry = Entries(date=ent[0], desc=ent[1], cat=ent[2], type=t, price=int(ent[4])) entry.save() return HttpResponse('success') # getting all the entries and the unique categories entries = Entries.objects.all() cats = [] for cat in entries.values('cat').distinct(): cats.append(cat['cat']) return render(request, 'accounts/entries.html', context={"entries": entries, "cats": cats})
def cart(request): user = validate_access(request, 'p') orders = user.staff.order_set.all() total = utils.get_total(orders) return render(request, "pharma/cart.html", { 'cart': orders, 'total': total })
def case_archive(request): user = validate_access(request, 'd') # Getting the cases under the doctor cases = user.staff.doctor.cases_set.filter(status='d') patients = {} # Adding patients as the keys and the done cases as values for case in cases: if case.patient not in patients: patients[case.patient] = case.patient.cases_set.filter(status='d')
def order_prescription(request): validate_access(request, 'p') prescriptions = Prescription.objects.filter(status='p') # placing order if request has id if 'id' in request.GET: pres = prescriptions.get(id=int(request.GET['id'])) # returning a redirect if medicine is not available issue_stock = {} for med in pres.medicine_set.all(): if med.quantity > med.item.quantity or med.item.deleted: issue_stock[med.item.id] = med.item.name # if There are missing stock or out of stock medicines if issue_stock: return HttpResponse(json.dumps(issue_stock), content_type='application/json', status=418) # Generating the bill if the objects are in stock bill = utils.generate_bill(name=pres.case.patient.name, contact_num=pres.case.patient.phone, date=timezone.now(), orders=pres.medicine_set.all()) # Updating the prescription status and saving changes pres.bill = bill pres.status = 'd' pres.save() # giving a redirect to view the bill return HttpResponse(json.dumps({'id': bill.id}), content_type='application/json') return render(request, 'pharma/orders.html', context={'prescriptions': prescriptions})
def prescription(request): # Getting the user from the request user = validate_access(request, 'd') # Getting the list of cases cases = Cases.objects.filter(doctor=user.staff.doctor) prescriptions = [] for case in cases: # checking if prescription exists and the state is created if hasattr(case, 'prescription'): if case.prescription.status == 'c': prescriptions.append(case.prescription)
def edit_prescription(request): user = validate_access(request, 'p') pres = Prescription.objects.get(id=int(request.GET["id"])) # if the list was edited if 'save' in request.GET: medicine_ids = json.loads(request.GET['medicines']) items = Stock.objects.filter(deleted=False) epres = EditedPrescription(user=user.staff, pres=pres) epres.save() for med_id in medicine_ids: item = items.get(id=med_id) med = EPMedicine(prescription=epres, item=item, quantity=int(medicine_ids[med_id])) med.save() print(med, 'saved') bill = utils.generate_bill(name=pres.case.patient.name, contact_num=pres.case.patient.phone, date=timezone.now(), orders=epres.medicines.all()) for med in epres.medicines.all(): print(med) pres.bill = bill pres.status = 'd' pres.save() return HttpResponse(json.dumps({'bill_id': bill.id}), content_type='application/json') # getting valid medicines from stock all_medicines = [] for medicine in Stock.objects.filter(deleted=False): if medicine.quantity > 0: all_medicines.append(medicine) medicines = json.loads(request.GET['medicines']) return render(request, 'pharma/edit_prescription.html', context={ 'prescription': pres, 'missing_stock': medicines, 'all_medicines': all_medicines })
def appointment(request): # Getting the user from the request user = validate_access(request, 'd') # Getting the list of patients who are to be attended d = date.today() cases = user.staff.doctor.cases_set.filter(status="t", appointed_date__date=d) # Checking if a patient diagnosis is being sent if request.method == "POST": form_data = CaseProcessing(request.POST) if form_data.is_valid(): case_id = form_data.cleaned_data['id'] # Making sure the form is not tamperd with and the case belongs to the doctor case = None for c in cases: if c.id == case_id: case = c # if the case belongs to the doctor and is to be handled if case: # Saving the case data and updating the case state case.desc = form_data.cleaned_data['desc'] case.status = 'd' case.save() # If the case has a prescription then making one if form_data.cleaned_data["has_pres"]: pres = Prescription(case=case) pres.save() # Redirecting user to prescription page response = redirect('doctor:add_prescription') response['Location'] += f'?id={case.id}' return response # Redirecting user back to appointments page return redirect("doctor:dashboard")
def add_one(request): user = validate_access(request, "p") # Getting the product id from the request ID = int(request.GET['product_id']) # Getting the respective items and orders item = Stock.objects.get(id=ID) order = user.staff.order_set.get(item__id=item.id) # Changing the quantity accordingly order.quantity += 1 item.quantity -= 1 # Saving the changes order.save() item.save() return render(request, "pharma/cart.html", {'cart': cart})
def remove(request): user = validate_access(request, 'p') # Getting the item id and quantity to reduce from request ID = int(request.GET['product_id']) reduce = int(request.GET['remove_amount']) # Getting the respective items and objects from the table item = Stock.objects.filter(id=ID)[0] order = user.staff.order_set.filter(item__id=item.id)[0] # Checking if the quantity in order will be 0 if order.quantity == reduce: order.delete() else: order.quantity -= reduce order.save() # Updating item quantity and saving changes item.quantity += reduce item.save() return render(request, "pharma/cart.html", {'cart': cart})
def bill_archive(request): validate_access(request, 'p') bills = Bill.objects.all() return render(request, 'pharma/bill_archive.html', context={"bills": bills})