def check_commission(request): if request.method == "POST": serializer = CheckCommissionSerializer(data=request.data) if serializer.is_valid(): cc = Sales() return Response(cc.check_commission(request.data["sellers_id"], request.data["amount"]), status=status.HTTP_200_OK) return Response({"message": "Bad request. Please check syntax and try again"}, status=status.HTTP_400_BAD_REQUEST)
def setUp(self): Commission_plan.objects.create(lower_percentage=2.5, upper_percentage=10.5, min_value=5000.00) Commission_plan.objects.create(lower_percentage=1.5, upper_percentage=5, min_value=4500) Sellers.objects.create(name="Ricardo Almeida", address="Rua abc, 213", phone="11932455678", age=35, email="*****@*****.**", cpf="65478932102", plan=Commission_plan.objects.get(id=1)) Sellers.objects.create(name="José Vendedor", address="Rua Rasa, 01", phone="5547993548264", age=42, email="*****@*****.**", cpf="456123987", plan=Commission_plan.objects.get(id=2)) Sales.objects.create(sellers_id=Sellers.objects.get(id=1), amount=1238.00, month=1, commission=round( Sales.calc_commission(self, 1, 1238.00), 2)) Sales.objects.create(sellers_id=Sellers.objects.get(id=2), amount=10950.00, month=1, commission=round( Sales.calc_commission(self, 2, 10950.00), 2))
def month_commission(request): if request.method == "POST": try: calc = Sales() calculated_amount = calc.calc_commission( request.data["sellers_id"], request.data["amount"]) except: return Response({"message": "Bad request. Please check syntax and try again"}, status=status.HTTP_400_BAD_REQUEST) request.data["commission"] = calculated_amount serializer = SalesSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({"id": serializer.data["id"], "commission": serializer.data["commission"], }, status=status.HTTP_201_CREATED) return Response({"message": "Bad request. Please check syntax and try again"}, status=status.HTTP_400_BAD_REQUEST)
def vendedores(request, month): if request.method == "GET": rs = Sales() return Response(rs.return_sellers(month))
def test_check_commission(self): self.assertEqual(Sales.check_commission(self, 1, 1325.00), {"seller_notified": False}) self.assertEqual(Sales.check_commission(self, 2, 3200.00), {"seller_notified": False})
def test_return_sellers(self): self.assertEqual(len(Sales.return_sellers(self, 1)), 2) self.assertEqual(Sales.return_sellers(self, 2), 404)
def test_calc_commission(self): self.assertEqual(Sales.calc_commission(self, 1, 1238.00), round(Decimal(30.95), 2)) self.assertEqual(Sales.calc_commission(self, 2, 10950.00), round(Decimal(547.50), 2))