def calculate_pickup_charge(request, handling_option, pkg, pkg_warehouse): if handling_option == 'drop-at-location': location_id = request.session['dropoff_location_id'] location = LocalDistributorLocation.objects.get(pk=location_id) location_member = location.region.courier if location_member.has_configured_rates: return get_local_freight_from_state(request, pkg.box_weight_higher(), location_id) elif handling_option in ['drop-at-postoffice', 'pick-up-package']: shipping_origin = request.session.get('shipping_origin', "None") shipping_destination = request.session.get('shipping_destination', "None") WHaddress = request.session.get('WHaddress') location_id = WarehouseLocation.objects.get(id=WHaddress) if shipping_origin == 'United States': lb_country = shipping_destination else: lb_country = shipping_origin costcalc = marketingmember_costcalc(request, lb_country) destZipCode = location_id.zip_code pick_up_charge_D = get_pickup_charge(request, pkg, "usps", destZipCode) print 'pick_up_charge_D: ', pick_up_charge_D pick_up_charge_N = float(pick_up_charge_D) * float( costcalc.dollar_exchange_rate) return pick_up_charge_D, pick_up_charge_N
def calculate_last_mile_charges(request, pkg, origin, destination, direction): mm = marketing_member(request) print 'origin -- destination:', origin, destination if origin == 'United States': lb_country = destination elif request.session.has_key('lb_country'): lb_country = request.session.get('lb_country') else: lb_country = origin costcalc = marketingmember_costcalc(request, lb_country) WHaddress = request.session.get('WHaddress') print "wha2:", WHaddress Location = WarehouseLocation.objects.get(id=WHaddress) print "la:", Location distributor = mm.get_route_shipping_distributor(origin, destination, direction) print 'calculate_last_mile_charges | distributor: ', distributor if distributor == None: pick_up_charge_D = pick_up_charge_N = 0.0 return pick_up_charge_D, pick_up_charge_N elif distributor.has_api: print "its here" destZipCode = Location.zip_code print "DZC:", destZipCode pick_up_charge_D = get_pickup_charge(request, pkg, "usps", destZipCode) print "pick_up_charge_D: ", pick_up_charge_D print type(pick_up_charge_D) if type(pick_up_charge_D) != str or type(pick_up_charge_D) != int: pick_up_charge_D = 0.0 else: pass pick_up_charge_N = float(pick_up_charge_D) * float( costcalc.dollar_exchange_rate) # del request.session['WHaddress'] return pick_up_charge_D, pick_up_charge_N else: #return get_local_freight_from_state(request, pkg.box_weight_higher(), distributor.id) # print "pkg: ",pkg # print "got here" print "state of origin:", pkg.delivery_address.state # print "weight:",pkg.box_weight_higher() return get_local_freight_from_state_hd(request, pkg.box_weight_higher(), pkg.delivery_address.state, destination)
def initiate_charge_card(request, **kwargs): if request.method == "POST": markup_percentage, markup_min_charge, is_nigerian_card = get_markup_charge( request) txn_desc = kwargs['txn_desc'] txn_ref = kwargs['txn_ref'] request.session['txn_ref'] = txn_ref actual_amount_D = round(kwargs.get('actual_amount', 0), 2) request.session['actual_amount_D'] = actual_amount_D print 'markup_percentage, markup_min_charge: ', markup_percentage, markup_min_charge markup_charge_D = round( (float(actual_amount_D) * markup_percentage) + markup_min_charge, 2) if is_nigerian_card: max_markup_charge_D = 5 if markup_charge_D > max_markup_charge_D: markup_charge_D = max_markup_charge_D amount_D = round(actual_amount_D + markup_charge_D, 2) cost_calc = marketingmember_costcalc(request, 'Nigeria') amount_N = format(amount_D * float(cost_calc.dollar_exchange_rate), '.2f') amountz = amount_D * float(cost_calc.dollar_exchange_rate) * 100 print "going to paystack to charge card" email = request.user.email url = 'general:verifyPayment' callback_url = request.build_absolute_uri(reverse(url)) print "callback-url", callback_url response = Transaction.initialize(reference=txn_ref, amount=amountz, email=email, callback_url=callback_url) print 'response:', response data = response.get('data') print "data:", data authorization_code = data['access_code'] print "access_code", authorization_code url = data['authorization_url'] print 'url', url return redirect(url)
def dollar_exchange_rate(request): costcalc = marketingmember_costcalc(request) return costcalc.dollar_exchange_rate
def PackageCostCalc(request, shippingWeight, shippingWeight_kg, origin, destination, costcalc, lb_country): print "country chain: ",lb_country print "costcalc: ", costcalc if costcalc is None: costcalc = marketingmember_costcalc(request,lb_country) #weightFactor = costcalc.dim_weight_factor #lb_kg_factor = costcalc.lb_kg_factor exchange_rate = costcalc.dollar_exchange_rate is_promo_on = costcalc.is_promo_on VAT_rate = costcalc.vat_rate / 100.0 INS_rate = costcalc.insurance_rate / 100.0 handling_charge_fee = costcalc.handling_charge_fee Consolidation_Fee = costcalc.consolidation_fee Unit_Strip_My_Package_Fee = costcalc.unit_strip_package_fee Min_Strip_My_Package_Fee = costcalc.strip_package_fee_min shipping_rate_air_D = get_marketing_member_shipping_rate(request, origin, destination, 'air', shippingWeight) shipping_rate_sea_D = get_marketing_member_shipping_rate(request, origin, destination, 'sea', shippingWeight_kg) shipping_rate_express_D = get_marketing_member_shipping_rate(request, origin, destination, 'express', shippingWeight_kg) class DeliveryCostOptions(object): def __init__(self, shipping_weight, shipping_weight_kg): self.shippingWeight = shipping_weight self.shippingWeight_kg = shipping_weight_kg def local_freight_D(self): shipping_option = request.session['handling_option'] if shipping_option == 'send-from-shop': return 0 elif shipping_option == 'drop-at-location': #drop_at_location_id = request.session['drop_at_location_id'] return 0 elif shipping_option == 'drop-at-postoffice': return 0 elif shipping_option == 'pick-up-package': return 0 def air_freight(self): #default value shipping_rate_D = shipping_rate_air_D print 'shipping_rate_air_D: ',shipping_rate_D user_special_rate = has_special_rate_for_route(request, origin, destination, 'air') if user_special_rate[0] == True: shipping_rate_D = user_special_rate[1] print 'user_special_rate|air|shipping_rate_D: ',shipping_rate_D try: return (shipping_rate_D * self.shippingWeight * 100)/100 except: return 0 def sea_freight(self): #default value shipping_rate_D = shipping_rate_sea_D print 'shipping_rate_sea_D: ',shipping_rate_D user_special_rate = has_special_rate_for_route(request, origin, destination, 'sea') if user_special_rate[0] == True: shipping_rate_D = user_special_rate[1] print 'user_special_rate|sea|shipping_rate_D: ',shipping_rate_D try: return (shipping_rate_D * self.shippingWeight * 100)/100 except: return 0 def express_freight(self): #default value shipping_rate_D = shipping_rate_express_D print 'shipping_rate_express_D: ',shipping_rate_D user_special_rate = has_special_rate_for_route(request, origin, destination, 'express') if user_special_rate[0] == True: shipping_rate_D = user_special_rate[1] print 'user_special_rate|air|shipping_rate_D: ',shipping_rate_D try: return (shipping_rate_D * self.shippingWeight * 100)/100 except: return 0 def cost_per_lb_air(self): costPerLbAir = shipping_rate_air_D if costPerLbAir == None: return 0 print 'ca:',costPerLbAir return costPerLbAir def cost_per_lb_sea(self): costPerLbSea = shipping_rate_sea_D if costPerLbSea == None: return 0 print 'cs:',costPerLbSea return costPerLbSea def cost_per_lb_express(self): costPerLbExpress = shipping_rate_express_D if costPerLbExpress == None: return 0 print 'ce:',costPerLbExpress return costPerLbExpress #Creation of an instance with the name 'calc' calc = DeliveryCostOptions(shippingWeight, shippingWeight_kg) # totalSelectBoxWeight, totalSelectBoxWeight_kg, # totalEnterDimensionWeight, totalEnterDimensionWeight_kg, # totalUserShipmentWeight, totalUserShipmentWeight_kg) # shippingWeight = calc.shippingWeight # shippingWeight_kg = calc.shippingWeight_kg # totalSelectBoxWeight = calc.totalSelectBoxWeight # totalSelectBoxWeight_kg = calc.totalSelectBoxWeight_kg # totalEnterDimensionWeight = calc.totalEnterDimensionWeight # totalEnterDimensionWeight_kg = calc.totalEnterDimensionWeight_kg # totalUserShipmentWeight = calc.totalUserShipmentWeight # totalUserShipmentWeight_kg = calc.totalUserShipmentWeight_kg airFreight = calc.air_freight() localFreight_D = calc.local_freight_D() print "Local Freight Cost: ", localFreight_D airFreight_N = math.ceil((airFreight * exchange_rate) * 100)/100 seaFreight = calc.sea_freight() seaFreight_N = math.ceil((seaFreight * exchange_rate) * 100)/100 expressFreight = calc.express_freight() expressFreight_N = math.ceil((expressFreight * exchange_rate) * 100)/100 costPerLbAir = calc.cost_per_lb_air() costPerLbSea = calc.cost_per_lb_sea() costPerLbExpress = calc.cost_per_lb_express() #officePickupOutLag, officePickupOutLag_N, homeDelivInLag, homeDelivInLag_N, homeDelivOutLag, homeDelivOutLag_N = new_fedex_data().localShippingCharges(shippingWeight_kg, exchange_rate) # return shippingWeight, shippingWeight_kg, totalSelectBoxWeight, totalSelectBoxWeight_kg, \ # totalEnterDimensionWeight, totalEnterDimensionWeight_kg, totalUserShipmentWeight , totalUserShipmentWeight_kg, \ # localFreight_D, airFreight, airFreight_N, seaFreight, seaFreight_N, \ # costPerLbAir, costPerLbSea, exchange_rate return localFreight_D, airFreight, airFreight_N, seaFreight, seaFreight_N, \ costPerLbAir, costPerLbSea, costPerLbExpress, exchange_rate, expressFreight, expressFreight_N
def account_standing(request, user): if not request.user.is_authenticated(): return 0, 0 #user = User.objects.select_related('useraccount').get(username = request.user) useraccount = UserAccount.objects.get(user=user) print "useraccount country", useraccount.country #user.get_profile() #user = useraccount.user #user_country = user.useraccount.country #1. Calculate sum of user's approved (bank) and successful (card) payments #added_payments = SokoPay.objects.user_add_jejepay(request.user).aggregate(Sum('amount')) added_payments = SokoPay.objects.user_add_jejepay(user).aggregate( Sum('amount')) if added_payments['amount__sum'] == None: total_added_payments_N = 0 # else: # if not useraccount.country == "USA": # total_added_payments_N = added_payments['amount__sum'] else: total_added_payments_N = added_payments['amount__sum'] print "total added payments", total_added_payments_N #used_payments = SokoPay.objects.jejepay_withdrawals(request.user).aggregate(Sum('amount')) used_payments = SokoPay.objects.sokopay_withdrawals(user).aggregate( Sum('amount')) if used_payments['amount__sum'] == None: total_used_payments_N = 0 # else: # if not useraccount.country == "USA": # total_used_payments_N = used_payments['amount__sum'] else: total_used_payments_N = used_payments['amount__sum'] print "total used payments", total_used_payments_N #user_approved_refunds = SokoPay.objects.user_refund_jejepay(request.user).aggregate(Sum('amount')) user_approved_refunds = SokoPay.objects.user_refund_jejepay( user).aggregate(Sum('amount')) if user_approved_refunds['amount__sum'] == None: total_user_refunds_N = 0 # else: # if not useraccount.country == "USA": # total_user_refunds_N = added_payments['amount__sum'] else: total_user_refunds_N = user_approved_refunds['amount__sum'] print "total user refunds", total_user_refunds_N #Sum of orders and shipments costs #4. Calculate the user's credit standing as #1 - #2 - #3 user_credit_amount_N = total_added_payments_N - total_used_payments_N - total_user_refunds_N if not useraccount.country == "United States": dollar_exchange_rate = marketingmember_costcalc( request, useraccount.country).dollar_exchange_rate user_credit_amount_D = user_credit_amount_N / float( dollar_exchange_rate) useraccount.credit_amount_N = user_credit_amount_N else: user_credit_amount_D = user_credit_amount_N user_credit_amount_N = 0 useraccount.credit_amount_N = 0 print "USER", useraccount.credit_amount_N #return {'user_credit_amount_N': user_credit_amount_N, 'user_credit_amount_D': user_credit_amount_D} #useraccount.credit_amount_N = user_credit_amount_N useraccount.credit_amount_D = user_credit_amount_D useraccount.save() return round(user_credit_amount_N, 2), round(user_credit_amount_D, 2)
def process_api_response(request, xml_response, delivery_speed, action): #print 'xml_response: ',xml_response context = {} context['got_response'] = True context['action'] = action try: root = ET.fromstring(xml_response) except: return HttpResponse("Could not parse XML object") if action == "ShippingEstimate": try: # International shipping context["has_express_speed"] = True context["has_normal_speed"] = True # if delivery_speed in ["Priority Mail", "Online"]: try: context['destination'] = root.findall( ".//Service[@ID='2']/Country")[0].text context['duration'] = root.findall( ".//Service[@ID='2']/SvcCommitments")[0].text context['shipping_charge'] = root.findall( ".//Service[@ID='2']/CommercialPostage")[0].text context['normal_desc'] = root.findall( ".//Service[@ID='2']/SvcDescription")[0].text.split('&')[0] except Exception as e: context['destination'] = root.findall( ".//Service[@ID='1']/Country")[0].text context['duration'] = root.findall( ".//Service[@ID='1']/SvcCommitments")[0].text context['shipping_charge'] = root.findall( ".//Service[@ID='1']/CommercialPostage")[0].text context['express_desc'] = root.findall( ".//Service[@ID='1']/SvcDescription")[0].text.split('&')[0] except Exception as e: try: context['description'] = root.findall( ".//Postage[@CLASSID='1']/MailService")[0].text.split( '&')[0] context['shipping_charge'] = root.findall( ".//Postage[@CLASSID='1']/Rate")[0].text except Exception as e: print 'e3: ', e context['description'] = root.findall( ".//Postage[@CLASSID='3']/MailService")[0].text.split( '&')[0] context['shipping_charge'] = root.findall( ".//Postage[@CLASSID='3']/Rate")[0].text context['domestic_shipping'] = True courier_cost_D = float(context['shipping_charge']) print 'usps_helper|courier_cost_D: ', courier_cost_D request.session['courier_cost_D'] = courier_cost_D higher_weight_lbs = request.session['pkg_weight_lbs'] naira_value = request.session['naira_value'] drop_off_state = request.session['drop_off_state'] drop_off_country = request.session['drop_off_country'] costcalc = marketingmember_costcalc(request) lb_kg_factor = 0.45359 weight_kg = higher_weight_lbs * lb_kg_factor #origin = request.session['shipping_destination'] #destination = request.session['shipping_origin'] origin = 'United States' destination = 'Nigeria' drop_off_id = request.session['drop_off_id'] user_special_rate = has_special_rate_for_route(request, origin, destination, 'air') if user_special_rate[0] == True: shipping_rate_air_D = user_special_rate[1] print 'export|user_special_rate|air|shipping_rate_D: ', shipping_rate_air_D else: shipping_rate_air_D = get_marketing_member_shipping_rate( request, origin, destination, 'air', weight_kg) #local_freight_cost_D, local_freight_cost_N = get_local_freight_from_state(request, dim_weight_lbs, drop_off_state, drop_off_country) local_freight_cost_D, local_freight_cost_N = get_local_freight_from_state( request, higher_weight_lbs, drop_off_id) print 'local_freight_cost_D, local_freight_cost_N : ', local_freight_cost_D, local_freight_cost_N export_cost_calc = ExportPackageCostCalc(request, higher_weight_lbs, None, drop_off_state, naira_value, courier_cost_D, shipping_rate_air_D, local_freight_cost_D, costcalc) service_charge_D, service_charge_N = export_cost_calc.serviceCharge() local_freight_D, local_freight_N = export_cost_calc.localFreight() intl_freight_D, intl_freight_N = export_cost_calc.internationalFreight() #local_intl_freight_cost_D = local_freight_D + intl_freight_D #local_intl_freight_cost_N = local_freight_N + intl_freight_N local_intl_freight_cost_D = local_freight_cost_D + intl_freight_D + service_charge_D local_intl_freight_cost_N = local_freight_cost_N + intl_freight_N + service_charge_N context.update({ 'local_intl_freight_cost_D': local_intl_freight_cost_D, 'local_intl_freight_cost_N': local_intl_freight_cost_N }) return JsonResponse(context)
def CreateShipmentCostCalc(request, shipment_info, country, cost_calc=None): #cost_calc = costcalc_settings() print "country", country country = country '''check if cost_calc is sent for existing package''' if cost_calc == None: cost_calc = marketingmember_costcalc(request, country) weightFactor = cost_calc.dim_weight_factor lb_kg_factor = cost_calc.lb_kg_factor exchange_rate = cost_calc.dollar_exchange_rate #exchange_rate = country_exchange_rate(country, cost_calc) admin_VAT = cost_calc.vat_rate VAT_rate = admin_VAT / 100 admin_INS = cost_calc.insurance_rate INS_rate = admin_INS / 100 #New Pricing Model for Service Charge (January 2014) start sc_dollar_rate_1 = cost_calc.sc_dollar_rate_1 sc_dollar_rate_2 = cost_calc.sc_dollar_rate_2 sc_dollar_rate_3 = cost_calc.sc_dollar_rate_3 service_charge_min_fee = cost_calc.service_charge_min_fee handling_charge_fee = cost_calc.handling_charge_fee #New Pricing Model for Service Charge (January 2014) end Consolidation_Fee = cost_calc.consolidation_fee Unit_Strip_My_Package_Fee = cost_calc.unit_strip_package_fee Min_Strip_My_Package_Fee = cost_calc.strip_package_fee_min customsClearing = cost_calc.customs_clearing_fee pkg_count = shipment_info["pkg_count"] #item_count = get_no_of_items(request) item_count = shipment_info["item_count"] #shippingWeight = shipping_Weight(request) shippingWeight = shipment_info["shippingWeight"] shippingWeight_kg = float(shippingWeight * lb_kg_factor) #cart_value = cartWorth(request) try: cart_value = shipment_info["cart_value"] except: cart_value = 0.0 #cart_value_N = cart_value * dollarNairaRate total_freight_D = shipment_info["total_freight_D"] total_freight_N = total_freight_D / exchange_rate class ShipmentCosts(object): def __init__(self, pkg_count, item_count, shippingWeight, shippingWeight_kg, cart_value, total_freight_D, total_freight_N, VAT_rate, \ INS_rate, sc_dollar_rate_1, sc_dollar_rate_2, sc_dollar_rate_3, Consolidation_Fee, Unit_Strip_My_Package_Fee, \ Min_Strip_My_Package_Fee, customsClearing): self.pkg_count = pkg_count self.item_count = item_count self.shippingWeight = shippingWeight self.shippingWeight_kg = shippingWeight_kg self.cart_value = cart_value self.total_freight_D = float(total_freight_D) self.total_freight_N = float(total_freight_N) self.VAT_rate = VAT_rate self.INS_rate = INS_rate self.sc_dollar_rate_1 = sc_dollar_rate_1 self.sc_dollar_rate_2 = sc_dollar_rate_2 self.sc_dollar_rate_3 = sc_dollar_rate_3 self.Consolidation_Fee = Consolidation_Fee self.Unit_Strip_My_Package_Fee = Unit_Strip_My_Package_Fee self.Min_Strip_My_Package_Fee = Min_Strip_My_Package_Fee self.customs_clearing_fee = customsClearing def package_count(self): return self.pkg_count def item_count(self): return self.item_count def lb_weight(self): return self.shippingWeight def kg_weight(self): return self.shippingWeight_kg def Freight_D(self): return self.total_freight_D def Freight_N(self): return self.total_freight_N #New Pricing Model for Service Charge (January 2014) def ServiceCharge_D(self): SC = handling_charge_fee #equivalentValue = currencyEquivalentOfDollar(exchange_rate, dollarNairaRate, SC) #return SC #return equivalentValue return SC def consolidation_charge(self): #equivalentValue = currencyEquivalentOfDollar(exchange_rate, dollarNairaRate, self.Consolidation_Fee) #return float(equivalentValue) return self.Consolidation_Fee def Insurance(self): ins = float(self.INS_rate) * (float(self.total_freight_D) + float(self.cart_value)) return ins def StripMyPackageFee(self): #equivalentValue = currencyEquivalentOfDollar(exchange_rate, dollarNairaRate, Min_Strip_My_Package_Fee) equivalentValue = Min_Strip_My_Package_Fee if self.item_count < 6: return float(equivalentValue) else: fee = float(item_count * Unit_Strip_My_Package_Fee) if fee < float(equivalentValue): return float(equivalentValue) else: return fee def ValueAddedTax_D(self): vat_d = float( self.VAT_rate * (self.total_freight_D + self.Insurance() + self.ServiceCharge_D() + self.consolidation_charge() + self.StripMyPackageFee())) return vat_d def CustomsClearingFee(self): return float(self.customs_clearing_fee * shippingWeight_kg) paycalc = ShipmentCosts(pkg_count, item_count, shippingWeight, shippingWeight_kg, \ cart_value, total_freight_D, total_freight_N, VAT_rate, INS_rate, \ sc_dollar_rate_1, sc_dollar_rate_2, sc_dollar_rate_3, Consolidation_Fee, \ Unit_Strip_My_Package_Fee, Min_Strip_My_Package_Fee, customsClearing) total_freight_D = paycalc.Freight_D() VAT_D = paycalc.ValueAddedTax_D() #VAT_N = VAT_D * exchange_rate totalServiceCharge_D = paycalc.ServiceCharge_D() #totalServiceCharge_N = totalServiceCharge_D * exchange_rate CON_D = paycalc.consolidation_charge() #CON_N = CON_D * exchange_rate PSDG_D = paycalc.Insurance() #PSDG_N = PSDG_D * exchange_rate SMP_D = paycalc.StripMyPackageFee() #SMP_N = SMP_D * exchange_rate #customsClearing_N = paycalc.CustomsClearingFee() #customsClearing_D = customsClearing_N / exchange_rate freight_VAT_SC_D = total_freight_D + VAT_D + totalServiceCharge_D #freight_VAT_SC_N = freight_VAT_SC_D * exchange_rate #print 'freight_VAT_SC_D: ',freight_VAT_SC_D #print 'total_freight_D, cart_value: ',total_freight_D, cart_value coverage_amount_D = float(total_freight_D) + float(cart_value) #coverage_amount_N = coverage_amount_D * exchange_rate return total_freight_D, VAT_D, totalServiceCharge_D, \ CON_D, PSDG_D, SMP_D, freight_VAT_SC_D, coverage_amount_D, exchange_rate