I'm trying to import a csv file with wp all import set specific functions to set the sale price and the offer price.
Now I would need to call the function that forms the regular price, inside the function of the offer price, to work on the result and maybe apply a percentage discount.
<?php
$GLOBALS['$commissione_paypal_lm'] = 4;
$GLOBALS['$spese_spedizione_moto_lm'] = 10;
$GLOBALS['$spese_spedizione_autocarro_lm'] = 10;
$GLOBALS['$spese_spedizione_varie_lm'] = 7;
$GLOBALS['$pfu_moto_lm'] = 1.50;
$GLOBALS['$pfu_autocarro_lm'] = 4.90;
$GLOBALS['$pfu_varie_lm'] = 2.90;
// First function for regular price,i take variables from csv and global variables
function prezzo_finale( $price = null, $pfu = null, $diametro = null ) {
if ( !empty( $price ) ) {
// strip any extra characters from price
$price = preg_replace("/[^0-9,.]/", "", $price);
$pfu = preg_replace("/[^0-9,.]/", "", $pfu);
//price ommas and dots fix
$price = str_replace(",",".",str_replace(".","",$price));
$pfu = str_replace(",",".",str_replace(".","",$pfu));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
$prezzo_finale = $price;
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else if ($percent > 0){
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}
// perform calculations
return $prezzo_finale;
}
}
function prezzo_finale_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null ) {
if ( !empty( $fifo_ponderato ) ) {
// strip any extra characters from price
$fifo_ponderato = preg_replace("/[^0-9,.]/", "", $fifo_ponderato);
//price ommas and dots fix
$fifo_ponderato = str_replace(",",".",str_replace(".","",$fifo_ponderato));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
// moto = 10; vettura 8; isole extra valutare; paypal 3%;
$prezzo_finale_lm = ($fifo_ponderato);
if ($settori_codice === 'MOTO' || $settori_codice === 'SCOOTER' || $settori_codice === 'CICLOMOTORI'){
$spese_spedizione = $GLOBALS['$spese_spedizione_moto_lm'];
$pfu = $GLOBALS['$pfu_moto_lm'];
}else if ($settori_codice === 'AUTOCARRO'){
$spese_spedizione = $GLOBALS['$spese_spedizione_autocarro_lm'];
$pfu = $GLOBALS['$pfu_autocarro_lm'];
}else{
$spese_spedizione = $GLOBALS['$spese_spedizione_varie_lm'];
$pfu = $GLOBALS['$pfu_varie_lm'];
}
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else if ($percent > 0){
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}
// perform calculations
return $prezzo_finale_lm_paypal;
}
}
// Second function for offer price,i would take regular price and work with
function prezzo_offerta_lm() {
if (prezzo_finale_lm()){
return prezzo_finale_lm();
// and for example apply discount as global variable
}else{
}
// perform calculations
}
?>
This is shortcode i use for regular price:
[prezzo_finale_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
Thank you all for any advice.
<?php
// First function for regular price,i take $price variable from imported csv column
function regular_price( $price = null) {
if ( !empty( $price ) ) {
// final price
$prezzo_finale = $price;
// perform calculations as example add markup or paypal commission
return $prezzo_finale;
}
}
// Second function for offer price,where i need to get returned value from first function
function offer_price($price = null)) {
if ( !empty( $price ) ) {
$offer_price = regular_price(); // i need to return here value from first function to perform discount calculation
// perform calculations
return $offer_price;
}
}
?>
Related
I have installed a PHP script into the functions.php file and now I need to call it from the price field for import have no clue on which code to use? Below is the function.PHP code\
I am new to this so I am learning have checked 20 allimport help posts but can not connect the dots Thank you in advance XMark
function price_increment( $price ) {
if($price <= 20){
$adjustment = .15;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 50){
$adjustment = .12;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 100){
$adjustment = .07;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else {
$adjustment = .05;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
}
}
I'm trying to calculate EMI as per the attached screenshot. but there is a minor difference in my calculation. what I have done so far as below:
function interest($investment,$year,$rate=15,$n=1){
global $total_result, $total_interest, $totalamount;
$accumulated=0;
if ($year > 1){
$accumulated=interest($investment,$year-1,$rate,$n);
}
$accumulated += $investment;
$rateC = $rate / 100;
$result = $rateC / 12 * pow(1 + ($rateC) / 12, ($year * 12)) / (pow(1 + ($rateC) / 12, ($year * 12)) - 1) * $accumulated;
$result = 85.60;
for ($i=0; $i < 12; $i++) {
// echo round($accumulated,2).'<br>';
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
$innntrest = ($accumulated - $investment) / 12;
$i_result = $result - $innntrest;
$accumulated = $investment - $i_result;
$investment = $accumulated;
echo '<br>***'.round($result,2).'***'.round($i_result,2).'***'.round($innntrest,2).'<br>';
$total_result = $total_result + round($result,2);
$total_interest = $total_interest + $i_result;
$totalamount = $totalamount + $innntrest;
}
return $accumulated;
}
Where $investment=1000(loan amount), $year=1 (loan term), $rate=5%(interest rate), $n=4 (compound)
So I want exact output as an attached screenshot, so can anyone help me with this code to find out the formula? Thanks in advance!
After a few days of research, I got the solution as below:
function interest($investment,$year,$rate=15,$n=1,$payment_frequency=4){
$accumulated=0;
$accumulated += $investment;
$rateC = $rate / 100;
$total_i = 0;
if($payment_frequency == 1) {
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
} else {
$rate = pow(1 + ($rateC/$n),($n*($year/($year*$payment_frequency)))) - 1;
$result = ($rate*$accumulated)/(1 - pow((1 + $rate),-($year*$payment_frequency)));
for ($i=0; $i < ($year*$payment_frequency); $i++) {
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
$innntrest = ($accumulated - $investment) * 100;
$i_result = $result - $innntrest;
$accumulated = $investment - $i_result;
$investment = $accumulated;
$total_i += $innntrest;
}
return $total_i;
}
}
I am trying to create a loop in PHP where for each extra item in a shopping cart, the shipping gets a ascending 10% discount.
This is the result I want:
Quantity = 1: Shipping = 30;
Quantity = 2: Shipping = 30 + (30 x 0.5);
Quantity = 3: Shipping = 45 + (30 x 0.4);
Quantity = 4: Shipping = 57 + (30 x 0.3);
Quantity = 5: Shipping = 69 + (30 x 0.2);
Quantity = 6: Shipping = 78 + (30 x 0.1);
Quantity = 7: Shipping = 81 + (30 x 0.1);
Quantity = 8: Shipping = 84 + (30 x 0.1);
etc.
I don't know where to place my $quantity variable now.
My PHP:
<?php
$quantity = 10;
$shipping = 30;
for( $discount = 0.5; $discount >= 0.1; $discount - 0.1 ) {
$shipping = $shipping + ( $shipping * $discount );
}
echo $shipping;
?>
you should iterate the quantity value, not the discount.
$shippingDefault = 30;
$discount = 0.5;
for ($i = 2 ; $i <= $quantity; $i++) {
if ($discount < 0.1) {
$discount = 0.1;
}
$shipping += ( $shippingDefault * $discount );
$discount -= 0.1;
}
This is my scenario:
if units are <= 100 then charge Rs 1 rupee/unit
if units are > 100 && <= 200 then charge Rs 2 rupee/unit
if units are > 200 && <= 300 then charge Rs 3 rupee/unit
if units are > 300then charge Rs 7 rupee/unit
if($units > 100){
$bill = 100 * 1;
$remaining_units= $units -100;
if($remaining_units > 100 ){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 2);
if($remaining_units > 100 || $remaining_units < 100){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 3);
if($remaining_units > 100 || $remaining_units < 100){
$bill = $bill + (100* 7);
}
}
}
}
echo $bill;
Use this function this function will work for you:
echo calculate_bill(210);
function calculate_bill($units, $multipler = 1, $oldBill=0){
if($units > 100){
$remainingUnits = $units -100;
$oldBill = (100 * $multipler) + $oldBill;
}else{
$a = ($units * $multipler) + ($oldBill);
return $a;
}
/*first level complete*/
if($multipler == 1){
$multipler = 2;
}elseif($multipler == 2){
$multipler = 3;
}elseif($multipler >= 3){
$multipler = 7;
}
return calculate_bill($remainingUnits,$multipler, $oldBill);
}
Ya its working fine now
if($units < 100){
$bill = $units*1;
}elseif($units > 100 && $units <=200){
$temp = 100*1;
$remaining_units = $units - 100;
$bill = $temp + ($remaining_units *2);
}elseif($units > 200 && $units <=300){
$temp = (100*1)+ (100*2);
$remaining_units = $units - 200;
$bill = $temp + ($remaining_units *3);
}else{
$temp = (100*1)+ (100*2)+ (100*3);
$remaining_units = $units - 300;
$bill = $temp + ($remaining_units *7);
}
echo $bill;
I have a database table of UK postcodes that has 4 fields:
postcode,
east,
north,
pqi
Example values: ST1 6BQ, 388605, 349057,10
The primary key is postcodes and east and north are bothint(11)` fields.
Does anybody have an efficient MySQL query that will return the nearest 5 postcodes to a given post?
I have seen many examples using long and lat, but not northing and easting.
Convert your northings/eastings to lat/long, remembering that The OS grid is based on OSGB36 rather than WGS84.
I use the following class:
<?php
namespace OSGB36;
use \Geodetic\Datum;
class Converter
{
private $_osRef;
private $_fromDatum;
private $_toDatum;
public function __construct()
{
$this->_osRef = new OSRef();
$this->_fromDatum = new Datum(Datum::OSGB36);
$this->_toDatum = new Datum(Datum::WGS84);
}
/**
* Converts easting/northing into lat/long
*
* #param integer $eastings
* #param integer $northings
* #return \Geodetic\LatLong
*/
public function calculateLatLong($eastings, $northings)
{
$this->_osRef->setNorthings($northings)
->setEastings($eastings);
$OSGB36LatLong = $this->_osRef->toLatLong(
$this->_fromDatum->getReferenceEllipsoid()
);
$ecef = $OSGB36LatLong->toECEF($this->_fromDatum);
$ecef->toWGS84($this->_fromDatum);
$WGS84LatLong = $ecef->toLatLong($this->_toDatum);
return $WGS84LatLong;
}
}
and
<?php
namespace OSGB36;
use \Geodetic\LatLong\CoordinateValues;
class OSRef
{
private $_northings;
private $_eastings;
public function __construct($northings = NULL, $eastings = NULL)
{
$this->_northings = $northings;
$this->_eastings = $eastings;
}
public function setNorthings($northings)
{
$this->_northings = $northings;
return $this;
}
public function setEastings($eastings)
{
$this->_eastings = $eastings;
return $this;
}
private function _sinSquared($x) {
return sin($x) * sin($x);
}
private function _tanSquared($x) {
return tan($x) * tan($x);
}
private function _secant($x) {
return 1.0 / cos($x);
}
private function _cosecant($x) {
return 1.0 / sin($x);
}
private function _cotangent($x) {
return 1.0 / tan($x);
}
public function toLatLong(\Geodetic\ReferenceEllipsoid $airy1830) {
$OSGB_F0 = 0.9996012717; // Central Meridan Scale factor
$N0 = -100000.0; // True origin Northing
$E0 = 400000.0; // True origin Easting
$phi0 = deg2rad(49.0); // True origin Latitude
$lambda0 = deg2rad(-2.0); // True origin Longitude
$semiMajorAxis = $airy1830->getSemiMajorAxis();
$semiMinorAxis = $airy1830->getSemiMinorAxis();
$eSquared = $airy1830->getFirstEccentricitySquared();
$easting = $this->_eastings - $E0;
$northing = $this->_northings - $N0;
$n = ($semiMajorAxis - $semiMinorAxis) / ($semiMajorAxis + $semiMinorAxis);
$M = 0.0;
$phiPrime = ($northing / ($semiMajorAxis * $OSGB_F0)) + $phi0;
do {
$M = ($semiMinorAxis * $OSGB_F0) *
(((1 + $n + ((5.0 / 4.0) * $n * $n) + ((5.0 / 4.0) * $n * $n * $n)) *
($phiPrime - $phi0)) -
(((3 * $n) + (3 * $n * $n) + ((21.0 / 8.0) * $n * $n * $n)) *
sin($phiPrime - $phi0) *
cos($phiPrime + $phi0)) +
((((15.0 / 8.0) * $n * $n) + ((15.0 / 8.0) * $n * $n * $n)) *
sin(2.0 * ($phiPrime - $phi0)) *
cos(2.0 * ($phiPrime + $phi0))) -
(((35.0 / 24.0) * $n * $n * $n) *
sin(3.0 * ($phiPrime - $phi0)) *
cos(3.0 * ($phiPrime + $phi0))));
$phiPrime += ($northing - $M) / ($semiMajorAxis * $OSGB_F0);
} while (($northing - $M) >= 0.001);
$v = $semiMajorAxis * $OSGB_F0 * pow(1.0 - $eSquared * $this->_sinSquared($phiPrime), -0.5);
$rho = $semiMajorAxis * $OSGB_F0 * (1.0 - $eSquared) *
pow(1.0 - $eSquared * $this->_sinSquared($phiPrime), -1.5);
$etaSquared = ($v / $rho) - 1.0;
$VII = tan($phiPrime) / (2 * $rho * $v);
$VIII = (tan($phiPrime) / (24.0 * $rho * pow($v, 3.0))) *
(5.0 + (3.0 * $this->_tanSquared($phiPrime)) + $etaSquared - (9.0 * $this->_tanSquared($phiPrime) * $etaSquared));
$IX = (tan($phiPrime) / (720.0 * $rho * pow($v, 5.0))) *
(61.0 + (90.0 * $this->_tanSquared($phiPrime)) + (45.0 * $this->_tanSquared($phiPrime) * $this->_tanSquared($phiPrime)));
$X = $this->_secant($phiPrime) / $v;
$XI = ($this->_secant($phiPrime) / (6.0 * $v * $v * $v)) * (($v / $rho) + (2 * $this->_tanSquared($phiPrime)));
$XII = ($this->_secant($phiPrime) / (120.0 * pow($v, 5.0))) *
(5.0 + (28.0 * $this->_tanSquared($phiPrime)) + (24.0 * $this->_tanSquared($phiPrime) * $this->_tanSquared($phiPrime)));
$XIIA = ($this->_secant($phiPrime) / (5040.0 * pow($v, 7.0))) *
(61.0 + (662.0 * $this->_tanSquared($phiPrime)) + (1320.0 * $this->_tanSquared($phiPrime) * $this->_tanSquared($phiPrime)) +
(720.0 * $this->_tanSquared($phiPrime) * $this->_tanSquared($phiPrime) * $this->_tanSquared($phiPrime)));
$phi = $phiPrime - ($VII * pow($easting, 2.0)) + ($VIII * pow($easting, 4.0)) - ($IX * pow($easting, 6.0));
$lambda = $lambda0 +
($X * $easting) - ($XI * pow($easting, 3.0)) + ($XII * pow($easting, 5.0)) - ($XIIA * pow($easting, 7.0));
$latLongCoordinates = new CoordinateValues(
$phi,
$lambda,
\Geodetic\Angle::RADIANS,
0.0,
\Geodetic\Distance::METRES
);
return new \Geodetic\LatLong($latLongCoordinates);
}
function toGridRef()
{
$hundredkmE = floor($this->_eastings / 100000);
$hundredkmN = floor($this->_northings / 100000);
$firstLetter = "";
if ($hundredkmN < 5) {
if ($hundredkmE < 5) {
$firstLetter = "S";
} else {
$firstLetter = "T";
}
} else if ($hundredkmN < 10) {
if ($hundredkmE < 5) {
$firstLetter = "N";
} else {
$firstLetter = "O";
}
} else {
$firstLetter = "H";
}
$secondLetter = "";
$index = 65 + ((4 - ($hundredkmN % 5)) * 5) + ($hundredkmE % 5);
$ti = $index;
if ($index >= 73)
{
$index++;
}
$secondLetter = chr($index);
$e = round(($this->_eastings - (100000 * $hundredkmE)) / 100);
$n = round(($this->_northings - (100000 * $hundredkmN)) / 100);
return sprintf("%s%s%03d%03d", $firstLetter, $secondLetter, $e, $n);
}
public static function createOSRefFromGridRef($gridRef)
{
$char1 = substr($gridRef, 0, 1);
$char2 = substr($gridRef, 1, 1);
$east = substr($gridRef, 2, 3) * 100;
$north = substr($gridRef, 5, 3) * 100;
if ($char1 == 'H') {
$north += 1000000;
} else if ($char1 == 'N') {
$north += 500000;
} else if ($char1 == 'O') {
$north += 500000;
$east += 500000;
} else if ($char1 == 'T') {
$east += 500000;
}
$char2ord = ord($char2);
if ($char2ord > 73)
{
$char2ord--; // Adjust for no I
}
$nx = (($char2ord - 65) % 5) * 100000;
$ny = (4 - floor(($char2ord - 65) / 5)) * 100000;
return new OSRef($north + $ny, $east + $nx);
}
}
combined with my Geodetic library.
It's pretty quick chundering through the entirety of CodePoint open to give me a database of postcode details including WGS84 lat/long coordinates for use with OpenStreetMap.