def test_calculate_comission(self): seller = self.get_seller() plan = seller.plan DIFFERENCE = Decimal(100.0) min_value = plan.min_value lower_amount = min_value - DIFFERENCE lower_comission = lower_amount * plan.lower_percentage sale_lower_comission = Sale.calculate_comission( plan=plan, amount=lower_amount ) self.assertEquals( lower_comission, sale_lower_comission ) upper_amount = min_value upper_comission = upper_amount * plan.upper_percentage sale_upper_comission = Sale.calculate_comission( plan=plan, amount=upper_amount ) self.assertEquals( upper_comission, sale_upper_comission )
def should_notify_user(seller: Seller, amount: Decimal): """ Checks if user should or should not be notified about under performing sales, according to specified rules. :param seller: Seller :param amount: Decimal :return: boolean """ MAX_LAST_SALES = 5 PERCENTAGE = percent(10) comission_value = Sale.calculate_comission(plan=seller.plan, amount=amount) weight_sales = get_seller_last_sales(seller=seller, max_last_sales=MAX_LAST_SALES) seller_has_sales = len(weight_sales) if not seller_has_sales: return False comissions_weighted_average = comission_weight_avg(sales=weight_sales) percentage_cut = sale_percentage( sales_comission_value_avg=comissions_weighted_average, percentage=PERCENTAGE) comissions_weighted_average_minus_percentage_cut = ( comissions_weighted_average - percentage_cut) lower_comission_value = (comission_value < comissions_weighted_average_minus_percentage_cut) return True if lower_comission_value else False
def test_sales_weight_avg(self): seller = self.get_seller() CONSTANT_AMOUNT = Decimal(500.0) PERCENTAGE = Sale.calculate_comission(plan=seller.plan, amount=CONSTANT_AMOUNT) MAX_LAST_SALES = 5 self.generate_const_sales(seller=seller, year=2019, amount=CONSTANT_AMOUNT) last_sales = get_seller_last_sales(seller=seller, max_last_sales=MAX_LAST_SALES) sale_weighted_avg = comission_weight_avg(sales=last_sales) self.assertEquals(sale_weighted_avg, PERCENTAGE)
def create(self, validated_data): """Metodo para guardar en venta.""" products = validated_data.pop("products") total_amount = self.get_total_amount(products) validated_data["total_amount"] = total_amount instance = Sale(**validated_data) instance.save() sale_detail = {} client = validated_data["client"] max_fee_product = 0 for product in products: plan_acquired = {} for prx in range(product["quantity"]): # verificamos si el producto es plan de consultass if product["product_type"].id == 1: sale_detail["pin_code"] = generate_pin_code() sale_detail["description"] = product["product_type"].description sale_detail["price"] = float(product["plan_id"].price) sale_detail["is_billable"] = product["is_billable"] # comparo si es promocional o no if product["is_billable"]: sale_detail["discount"] = 0.0 plan_acquired["is_active"] = False plan_acquired["available_queries"] = 0 plan_acquired["queries_to_pay"] = product["plan_id"].query_quantity plan_acquired["status"] = 1 is_chosen = False else: # es plan promocional plan_acquired["available_queries"] = product["plan_id"].query_quantity plan_acquired["queries_to_pay"] = 0 validated_data["is_fee"] = False plan_acquired["activation_date"] = date.today() plan_acquired["status"] = date.today() plan_acquired["expiration_date"] = get_date_by_time(product["plan_id"].validity_months) # se activa automaticamente por ser promocional plan_acquired["is_active"] = True plan_acquired["status"] = 4 plan_promotionals = SellerNonBillablePlans.objects.get( seller=validated_data["seller"], number_month=hoy.month, number_year=hoy.year) plan_promotionals.quantity = plan_promotionals.quantity - 1 plan_promotionals.save() if not has_chosen_plan(validated_data["client"]): is_chosen = True else: is_chosen = False sale_detail["discount"] = float(product["plan_id"].price) sale_detail["product_type"] = product["product_type"] sale_detail["sale"] = instance # creamos la instancia de detalle instance_sale = SaleDetail.objects.create(**sale_detail) # Calculamos la mayor cantidad de vigencias para el descuento validity_months = product["plan_id"].validity_months if validity_months > max_fee_product: max_fee_product = validity_months # llenamos data del plan adquirido plan_acquired["validity_months"] = validity_months plan_acquired["query_quantity"] = product["plan_id"].query_quantity plan_acquired["available_requeries"] = 10 # harcoded. CAMBIAR plan_acquired["maximum_response_time"] = 24 # harcoded.CAMBIAR plan_acquired["plan_name"] = product["plan_id"].name plan_acquired["query_plans"] = product["plan_id"] plan_acquired["sale_detail"] = instance_sale ins_plan = QueryPlansAcquired.objects.create(**plan_acquired) QueryPlansClient.objects.create( acquired_plan=ins_plan, status=1, is_chosen=is_chosen, client=validated_data["client"]) if is_chosen: if 'test' not in sys.argv: ser = QueryPlansAcquiredSerializer(ins_plan) new_ser = ser.data.copy() new_ser["is_chosen"] = is_chosen pyrebase.chosen_plan(client.id, new_ser) # Crear cuotas if validated_data["is_fee"]: n_fees = max_fee_product else: n_fees = 1 # solo si no es promocional if product["is_billable"]: for i in range(1, n_fees+1): if n_fees == 1: # Si la venta es de una sola cuota fee_amount = float(instance.total_amount) else: # Calcular el monto a pagar fee_amount = 0 for product in products: price = float(product["plan_id"].price) validity_months = int(product["plan_id"].validity_months) quantity = int(product["quantity"]) # Se Toman en cuenta solo planes con cuotas if validity_months >= i: fee_amount = (price/validity_months)*quantity + fee_amount pay_day = date.today() + relativedelta(months=i-1) # Hardcoded cambiar la cantidad de dias sale_id = instance MonthlyFee.objects.create(fee_amount=fee_amount, fee_order_number=i, status=1, sale=sale_id, pay_before=pay_day, fee_quantity=n_fees) return instance
def post(self, request, format=None): body = json.loads(request.body) sale = Sale() sale.name = body['name'] sale.address1 = body['address1'] sale.address2 = body['address2'] sale.city = body['city'] sale.state = body['state'] sale.zipcode = body['zipcode'] sale.total = body['total'] sale.items = body['items'] sale.payment_intent = stripe.PaymentIntent.create( amount=int(sale.total * 100), currency='usd', ) sale.save() return Response({ 'sale_id': sale.id, # must have called sale.save() or id will be empty 'client_secret': sale.payment_intent['client_secret'], })
def post(self, request, format=None): body = json.loads(request.body) sale = Sale() sale.name = body['name'] sale.address1 = body['address1'] sale.address2 = body['address2'] sale.city = body['city'] sale.state = body['state'] sale.zipcode = body['zipcode'] sale.total = body['total'] sale.items = body['items'] sale.payment_intent = stripe.PaymentIntent.create( amount=int(sale.total * 100), # $10.00 is 1000s currency='usd', ) sale.save() return Response({ # pylint: disable=maybe-no-member 'sale_id': sale.id, 'client_secret': sale.payment_intent['client_secret'], })
def post(self, request, format=None): body = json.loads( request.body ) # import this library at top..dont need to install tho body = body['values'] sale = Sale() # import from models.py at top of this file # set the field values sale.name = body['name'] sale.address1 = body['address1'] sale.address2 = body['address2'] sale.city = body['city'] sale.state = body['state'] sale.zipcode = body['zipcode'] sale.total = body['total'] sale.items = body['items'] sale.payment_intent = stripe.PaymentIntent.create( amount=int(int(sale.total) * 100), currency='usd', ) sale.save() return Response({ 'sale_id': sale.id, 'client_secret': sale.payment_intent['client_secret'], })
def post(self, request, format=None): body = json.loads(request.body) sale = Sale() sale.name = body["name"] sale.address1 = body["address1"] sale.address2 = body["address2"] sale.city = body["city"] sale.state = body["state"] sale.zipcode = body["zipcode"] sale.total = body["total"] sale.items = body["items"] sale.payment_intent = stripe.PaymentIntent.create( amount=int(sale.total) * 100, currency='usd') sale.save() return Response({ 'sale_id': sale.id, 'client_secret': sale.payment_intent['client_secret'] })