def test_delete_purchase(self): """ for admin operations we need to invoice to be updated when a purchase is deleted. """ p1 = Purchase(customer=self.customer, article=self.articles[0], quantity=2, price=self.articles[0].price) p1.save() p2 = Purchase(customer=self.customer, article=self.articles[1], quantity=2, price=self.articles[1].price) p2.save() # create invoice with p1 and ps invoice = create_invoice(self.customer, self.invoice_datetime, [p1, p2]) self.assertEquals( Decimal( f'{2 * self.articles[0].price + 2 * self.articles[1].price:.2f}' ), invoice.amount) # delete p1 p1.delete() self.assertEquals(Decimal(f'{2 * self.articles[1].price:.2f}'), invoice.amount)
def cart(request): id=request.session['logid'] p=Purchase.objects.all().filter(adm_no=id) product=Product.objects.all() i=Purchase() for i in p: a=0 pl=Plist.objects.all().filter(purch_id=i.purch_id) return render(request, 'cart/cart.html',{'k': i , 'pl' : pl, 'product' : product })
def bill1(request): pk = request.POST.get("prod", "") id = request.session['logid'] uAll = User.objects.all().filter(adm_no=id) for u in uAll: a = 0 pre = Product.objects.all().filter(product_id=pk) for pr in pre: a = 0 p = Purchase.objects.all() j = a pu = Purchase() for pu in p: if (pu.adm_no == u and pu.responded == 0): a = 1 if (pu.responded != 0): a = 0 j = pu.total + pr.price if (a == 0): pu = Purchase(adm_no=u, purchase_date=timezone.now(), total=pr.price, remarks='', response='', responded=0, placed=64) j = 0 pu.save() pl = Plist(purch_id=pu, product_id=pr, price=pr.price, quantity=1) pl.save() pl = Plist.objects.all().filter(purch_id=pu.purch_id) for pls in pl: j = j + pls.price pu.total = j pu.placed = 64 pu.save() return redirect('/bill')
def player_buy_game(request, game_name): user = request.user game = Game.objects.get(game_name=game_name) player_own_game = Purchase.objects.filter(user=user, result=True) if (len(player_own_game.filter(game=game)) > 0): return render(request, "buy_game.html", { 'msg': 'You already owned the game ' + game_name, 'owned': True }) pid = str(uuid.uuid1().hex) amount = game.price checksum_str = "pid={}&sid={}&amount={}&token={}".format( pid, "plr", amount, "c12ccb024b3d72922f9b85575e76154d") m = md5(checksum_str.encode("ascii")) current_site = get_current_site(request) checksum = m.hexdigest() print(checksum) post_data = { "pid": pid, "amount": amount, "sid": 'plr', "success_url": "http://" + current_site.domain + "/player/success", "cancel_url": "http://" + current_site.domain + "/player/store", "error_url": "http://" + current_site.domain + "/player/store", "checksum": checksum, "owned": False } print(post_data) # Add a unfinished purchase first p = Purchase(game=game, user=user, pid=pid, amount=amount, checksum=checksum, result=False) p.save() return render(request, "buy_game.html", post_data)
def test_create_invoice(self): # create 2 purchases p1 = Purchase(customer=self.customer, article=self.articles[0], quantity=2, price=self.articles[0].price) p1.save() p2 = Purchase(customer=self.customer, article=self.articles[1], quantity=1, price=self.articles[1].price) p2.save() invoice = create_invoice(self.customer, self.invoice_datetime, [p1, p2]) self.assertEquals(self.customer, invoice.customer) self.assertEquals(2, len(invoice.purchases.all())) expected_amount = Decimal( f'{2 * self.articles[0].price + self.articles[1].price:.2f}') self.assertEquals(expected_amount, invoice.amount)
def test_generate_invoice_pdf(self): total_amount = 1.20 + 2 * 4.5 # 1x item0, 2x item1 ip1 = Purchase(article=self.articles[0], quantity=1, price=self.articles[0].price, customer=self.customer) ip1.save() ip2 = Purchase(article=self.articles[1], quantity=2, price=self.articles[1].price, customer=self.customer) ip2.save() invoice = create_invoice(self.customer, self.invoice_datetime, [ip1, ip2]) # test pdf rendering # outstream = BytesIO() with open('test_invoice.pdf', 'wb') as outstream: renderer = InvoicePdfRenderer() renderer.render(invoice, outstream)
def test_validate_purchase(self): """ add purchase to an invoice make sure that customer and price are set automatically by validation logic (clean()) """ # create empty invoice invoice = create_invoice(self.customer, self.invoice_datetime, []) # add purchase without specifying price or customer p1 = Purchase( invoice=invoice, article=self.articles[0], quantity=2, ) p1.clean() self.assertEquals(self.customer, p1.customer) self.assertEquals(self.articles[0].price, p1.price) p1.save() self.assertEquals(Decimal(f'{2 * p1.price:0.2f}'), invoice.amount)
def test_modify_invoice(self): """ add an additional item to an invoice. this should change the invoice amount. """ # create 2 item purchases p1 = Purchase(customer=self.customer, article=self.articles[0], quantity=2, price=self.articles[0].price) p1.save() p2 = Purchase(customer=self.customer, article=self.articles[1], quantity=2, price=self.articles[1].price) p2.save() # create invoice with p1 invoice = create_invoice(self.customer, self.invoice_datetime, [p1]) self.assertEquals(Decimal(f'{2 * self.articles[0].price}'), invoice.amount) # add purchase p2 p2.invoice = invoice p2.save() invoice.save() self.assertEquals( Decimal( f'{2 * self.articles[0].price + 2 * self.articles[1].price:.2f}' ), invoice.amount)