I deleted a question I posted earlier because I oversimplified the example, and got more flak for problems there, so have decided to post the whole problem here instead.
The Task
Much like this question here...Copying Excel's Circular Reference formula in PHP I am trying to change an Excel Circular reference into PHP.
Most of my frustration is not knowing the correct terms to google this for myself.
The Situation
I have converted all logic in the following spreadsheet into PHP below...
<?php
$vat = '1.2';
$mileage = 10;
$price = 6250.00;
$contract_term = 24;
$deposit_months = 6;
$otr_price = 9409.00;
$discount = 0;
$lom_a = 36.08;
$lom_b = 5.14;
$vehicletype = "Car";
$first_reg_fee = 55.00;
$emission_price = 30.00;
$fees = 21.03;
$margin = 50.00;
if (!empty($discount)) {
$discounted_otr_percent = (int)$discount/100;
$discounted_otr_amount = $discounted_otr_percent * $otr_price;
$discounted_otr = $otr_price-$discounted_otr_amount;
} else {
$discounted_otr = $otr_price;
}
$totaltobefunded = ($discounted_otr - $first_reg_fee - 0 /* Emissions */) / $vat;
print '<hr><p>Total to be Funded: <strong>' . $totaltobefunded . '</strong></p>';
$vatbal = $discounted_otr - $first_reg_fee - 0 /* Emissions */ - $totaltobefunded;
print '<p>VAT: <strong>' . $vatbal . '</strong></p>';
if($vehicletype == "Car") {
$balloon = $price / $vat * 0.9;
} else {
$balloon = $price * 0.9;
}
print '<p>CAP 90% price: <strong>' . $balloon . '</strong></p>';
$totalamountborrowed = $totaltobefunded - $balloon;
print '<p>Total Amount Borrowed: <strong>' . $totalamountborrowed . '</strong></p>';
$fundingrental = $totalamountborrowed * $lom_a / 1000;
print '<p>Funding Rental: <strong>' . $fundingrental . '</strong></p>';
$balloonrental = $balloon * $lom_b / 1000;
print '<p>Funding Rental: <strong>' . $balloonrental . '</strong></p>';
$monthlyfinancerental = $fundingrental + $balloonrental;
print '<p>Monthly Finance Rental: <strong>' . $monthlyfinancerental . '</strong></p>';
$monthlyrentalwithfees = $monthlyfinancerental + $fees;
print '<p>Monthly Finance Rental With Fees: <strong>' . $monthlyrentalwithfees . '</strong></p>';
$totalmargin = $margin * $contract_term;
print '<p>Total Margin: <strong>' . $totalmargin . '</strong></p>';
$totalrentalandfees = $monthlyrentalwithfees * ($contract_term + $deposit_months -1);
print '<p>Total Rental and Fees: <strong>' . $totalrentalandfees . '</strong></p>';
$totalcontractcost = $totalmargin + $totalrentalandfees;
print '<p>Total Contract Cost: <strong>' . $totalcontractcost . '</strong></p>';
$actualmonthlyrental = $totalcontractcost / ($contract_term + $deposit_months -1);
print '<p>Actual Monthly Rental: <strong>' . $actualmonthlyrental . '</strong></p>';
$deposit = $actualmonthlyrental * $deposit_months;
print '<p>Deposit: <strong>' . $deposit . '</strong></p>';
In the PHP code above, I have hard coded all of the variables for the time being just to get this working. In the real life example, these will change for each vehicle.
A Circular Reference exists between N19 (Total to be funded) , N34 (Actual Monthly Rental) and N36 (Deposit).
So at some point before, during, or after the FOR statement, there needs to be...
$totaltobefunded = $totaltobefunded - $deposit;
What I have tried
I have tried using the PHP for from the linked question on my example, but it just keeps taking away the deposit from the Total to be funded, and I am left with minus numbers.
In the example in the link, it has the following results...
1410
1211.19
1239.22221
1235.26966839
1235.826976757
1235.7483962773
1235.7594761249
1235.7579138664
1235.7581341448
1235.7581030856
1235.7581074649
1235.7581068474
This refines the number to the best possible outcome, and this is what I am trying to achieve. Just like my Excel document.
At the moment, my PHP prints out....
Total to be Funded: 7795
VAT: 1559
CAP 90% price: 4687.5
Total Amount Borrowed: 3107.5
Funding Rental: 112.1186
Funding Rental: 24.09375
Monthly Finance Rental: 136.21235
Monthly Finance Rental With Fees: 157.24235
Total Margin: 1200
Total Rental and Fees: 4560.02815
Total Contract Cost: 5760.02815
Actual Monthly Rental: 198.62166034483
Deposit: 1191.729962069
What I need help with
Using the PHP code above (which I have over simplified for this question btw), where should I put my "for" statement in order to correctly loop through the 3 numbers in the circular reference to get the result Im after?
As you can see in the Excel Screenshot, the target numbers are...
Total to be Funded: 6815.32
VAT: 2538.68
CAP 90% price: 4687.5
Total Amount Borrowed: 2127.82
Funding Rental: 76.7717456
Funding Rental: 24.09375
Monthly Finance Rental: 100.8654956
Monthly Finance Rental With Fees: 121.8954956
Total Margin: 1200
Total Rental and Fees: 3534.9693724
Total Contract Cost: 4734.9693724
Actual Monthly Rental: 163.27480594483
Deposit: 979.64883566897
I hope this makes sense to someone and they can assist. I'll answer any questions as best as I can.
The solution in PHP is as follows....
<?php
$vat = '1.2';
$mileage = 10;
$price = 6250.00;
$contract_term = 24;
$deposit_months = 6;
$otr_price = 9409.00;
$discount = 0;
$lom_a = 36.08;
$lom_b = 5.14;
$vehicletype = "Car";
$first_reg_fee = 55.00;
$emission_price = 30.00;
$fees = 21.03;
$margin = 50.00;
$deposit = 0;
if (!empty($discount)) {
$discounted_otr_percent = (int)$discount/100;
$discounted_otr_amount = $discounted_otr_percent * $otr_price;
$discounted_otr = $otr_price-$discounted_otr_amount;
} else {
$discounted_otr = $otr_price;
}
$max_iter = 100;
$max_change = 0.001;
$prev_target_val = PHP_INT_MAX;
$totaltobefunded = 0;
$crt_iter = 0;
while(($crt_iter < $max_iter) && (abs($prev_target_val - $totaltobefunded) > $max_change)) {
$crt_iter++;
$prev_target_val = $totaltobefunded;
$totaltobefunded = ($discounted_otr - $first_reg_fee - 0 /* Emissions */) / $vat - $deposit;
$vatbal = $discounted_otr - $first_reg_fee - 0 /* Emissions */ - $totaltobefunded;
if($vehicletype == "Car") {
$balloon = $price / $vat * 0.9;
} else {
$balloon = $price * 0.9;
}
$totalamountborrowed = $totaltobefunded - $balloon;
$fundingrental = $totalamountborrowed * $lom_a / 1000;
$balloonrental = $balloon * $lom_b / 1000;
$monthlyfinancerental = $fundingrental + $balloonrental;
$monthlyrentalwithfees = $monthlyfinancerental + $fees;
$totalmargin = $margin * $contract_term;
$totalrentalandfees = $monthlyrentalwithfees * ($contract_term + $deposit_months -1);
$totalcontractcost = $totalmargin + $totalrentalandfees;
$actualmonthlyrental = $totalcontractcost / ($contract_term + $deposit_months -1);
$deposit = $actualmonthlyrental * $deposit_months;
}
print '<p>Final Number: <strong>' . $actualmonthlyrental . '</strong></p>';
?>
Related
I'm trying to apply a discount to my basket for each pair of 2 items that cost £1.50 each but I can't seem to get the hang of it. I draw the results from two SQL queries/tables of 'discount' and 'basket' Here's the following I've tried:
$discount_count[] = 0;
$i = 0;
while ($row2 = mysql_fetch_array($data_set2)) {
if ($row['product'] == $row2['product']) {
foreach ($row2 as $current_row) {
$actual_price = $row['product_cost'] * $row2['quantity'];
$discount_total = round(($actual_price - ($actual_price * ($row2['discount_percent'] / 100))), 2);
$total_price = $discount_total / $row2['quantity'] * $row['quantity'];
$product_total = $row['product_cost'] * $row['quantity'];
if ($i % 2 && $total_price * $row2['quantity'] > $row['product_cost'] * $row2['quantity']){
echo "<br /><br />actual price per 2: £" . $actual_price;
echo "<br />discount total: £" . $discount_total;
echo "<br />actual minus discount: £";
echo $actual_price - $discount_total;
echo "<br />total price: £" . $total_price;
echo "<br />product cost: £" . $product_total;
$discount_count[] = $actual_price - $discount_total;
}
$i++;
}
}
}
For example :
$monthly_bill = 700
$paid_amount = 2100
Now I want to use a php loop which will print me 700, 700 and 400 to each line. How can I do this ?
I am doing following code but not working :
<?php
$paid_amount = 1800;
$monthly_bill = 700;
$counting_month = round($paid_amount / $monthly_bill);
for ($x=1; $x<=$counting_month; $x++) {
echo $insert = $paid_amount - $monthly_bill;
$paid_amount = $insert;
echo '<br/>';
}
Don't use for loop. Those run a fixed number of times, and your loop would keep running until well after the money runs out.
Use a do instead, and have its termination clause be when the money runs out:
$remaining = 1800;
$monthly = 700;
do {
echo $monthly;
$remaining -= $monthly;
if ($remaining < $monthly) {
$monthly = $remaining;
}
} while ($remaining > 0);
<?php
$amount = 1800;
$monthlybill = 700;
for ($i=0; $i<$amount/$monthlybill; $i++) {
echo "paid for " . ($i+1) . " month =" . $monthlybill . "<br>\n";
$amount -= $monthlybill;
}
if ($amount>0) {
echo "paid for " . ($i+1) . " month =" . $amount;
}
I have two sets of data
1. Purchased orders received before the need by date.
2. Purchase orders received after the need by date.
Currently we are using inoERP pie chart to show two graphs side by side to compare.
Now, I want to combine the two a single over-lapping SVG chart.i.e Left side showing all data from x <=-3 and right side x >=3 .Any data between -3 & +3 will be in the overlapping period.
I dropped a mail to the their team but got the reply that they wont do any free customization of features not available in Oracle / SAP. So, I would like to know if there is any way to achieve this.
I looked into the code of the product and it's using below code
private function _draw_pie() {
if ((empty($this->_data)) || (!is_array($this->_data))) {
return;
}
$x_cordinate = $this->_left_margin;
$y_cordinate = $this->_top_margin;
$this->_prepare_pie();
$title_yaxis = 10;
if ($this->_chart_width > $this->_chart_height) {
$radius = ($this->_chart_height / 2);
} else {
$radius = ($this->_chart_width / 2);
}
$title_xaxis = $this->_left_margin + $radius;
$cx = $x_cordinate + $radius;
$cy = $y_cordinate + $radius;
$sum_of_elements = array_sum($this->_data);
if ($sum_of_elements <= 0) {
echo "<br> Wrong data selection. No value in value field";
return;
}
$legend_yaxis = $this->_top_margin;
$legend_xaxis = $cx + $radius * 1.2;
$chart = "<div class='chart_name'>" . $this->_chart_name . '</div>';
if ($this->_chart_type != 'donut') {
$pie_chart_height = 2.5 * $radius;
$chart .= "<svg class='chart pie' width=\"$this->_chart_width_orginal\" height=\"{$pie_chart_height}\">";
} else {
$chart .= "<svg class='chart pie' width=\"$this->_chart_width_orginal\" height=\"$this->_chart_height_orginal\">";
}
$chart .= '<text x="' . $title_xaxis . '" y="' . $title_yaxis . '" class="chart_xaxis_title" >' . $this->_x_axis_text . ' </text>';
$last_x = $radius;
$last_y = 0;
$cum_val = $cum_segment = 0;
$count = 0;
$large_arc = 0;
$chart .= "<g transform=\"translate(0,0)\">";
foreach ($this->_data as $key => $value) {
$cum_val += $value;
$div_class = 'pie_element value cluster_class_' . $count;
if ((($value / $sum_of_elements) * 360) > 180) {
$large_arc = 1;
} else {
$large_arc = 0;
}
$cum_segment += ($value / $sum_of_elements) * 360;
$last_y_m = -($last_y);
$next_x = $radius * cos(deg2rad($cum_segment));
$next_y = $radius * sin(deg2rad($cum_segment));
$end_point_x = ($next_x - $last_x);
$end_point_y = -($next_y - $last_y);
$chart .= "<path id=\"pie_path_$count\" d=\"M$cx,$cy
l$last_x, $last_y_m
a$radius,$radius 0 $large_arc,0 $end_point_x,$end_point_y z \"
style=\"stroke:#660000; \" class=\"$div_class\"/>";
$last_x = $next_x;
$last_y = $next_y;
$count++;
}
$chart .= '</g>';
$key_count = 0;
foreach ($this->_data as $key => $value) {
$cluster_class = 'pie_element cluster_class_' . $key_count;
$legend_y_axis_rect = $legend_yaxis - 10;
$chart .= "<rect x=\"$legend_xaxis\" y=\"$legend_y_axis_rect\" width=\"10\" height=\"10\" class=\" $key $cluster_class legend \"/>";
$legend_xaxis += 15;
$chart .= '<text x="' . $legend_xaxis . '" y="' . $legend_yaxis . '" class="chart_pie_value " >' . round($value / $sum_of_elements * 100, 1) . '% </text>';
$legend_xaxis += 45;
if (strlen($key) > 15) {
$legend_yaxis_os = 0;
$legend_xaxis_os = 0;
$key_a = explode(' ', $key);
foreach ($key_a as $nl_k => $new_line) {
$chart .= '<text x="' . $legend_xaxis . '" y="' . $legend_yaxis . '" class="chart_legend_title" >' . $new_line . ' </text>';
$legend_yaxis += 20;
$legend_xaxis -= 60;
}
$legend_xaxis += 60;
} else {
$chart .= '<text x="' . $legend_xaxis . '" y="' . $legend_yaxis . '" class="chart_legend_title" >' . $key . ' </text>';
$legend_yaxis += 40;
$legend_xaxis -= 60;
}
$key_count++;
}
$chart .= '<text x="' . $legend_xaxis . '" y="' . $legend_yaxis . '" class="chart_pie_value" > (Total : ' . $sum_of_elements . ') </text>';
if ($this->_chart_type != 'donut') {
$chart .= '</svg>';
}
return $chart;
}
I think it might help you.
the keyword is VENN DIAGRAMS.
install.packages('VennDiagram')
library(VennDiagram)
grid.newpage()
draw.pairwise.venn(22, 20, 11, category = c("Dog", "Cat"), lty = rep("blank",
2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(0,
0), cat.dist = rep(0.025, 2), scaled = FALSE)
Venn diagram here
I have an idea how you can do it.
First find out the value of overlapping as a percentage of radius. Ex. if you are using radius of two circles as x then the value of the overlapping distance should be m*x (you have to calculate m dynamically by dividing total value to the overlapping value)
next draw the second circle by changing the center of the circle by that amoun
I'm trying to do an activity where I need do put a number in cm, and the program has to return it with km, m, and cm.
An example: 270004 cm = 2km, 700m, 4cm // 100cm = 0km, 1m, 0cm.
I have done this code, but, sometimes I get negative numbers, and sometimes I get strange numbers.
Maybe is why I use the PHP_ROUND_HALF_UP??
What can I do to solve it?
//I have an HTML form to introduce the number value
$num = 123456789;
$km = $num/1000;
$km = round($km, 0, PHP_ROUND_HALF_UP);
echo "km: ".$km."<br>";
$subtraction = ($km * 1000) - $num;
$m = $subtraction / 100;
$m = round($m, 0, PHP_ROUND_HALF_UP);
echo "m: ".$m."<br>";
$subtraction2 = ($m * 100) - $subtraction;
$cm = $subtraction2;
echo "cm: ".$cm."<br>";
Honestly, your code seems to be a bit over complicated :P Ever heard of modulo division? Comes in handy in here!
<?php
$x = 123456789323; // centimeters
$km = floor($x / 100000); // amount of "full" kilometers
$rkm = $x % 100000; // rest
$m = floor($rkm / 100); // amount of "full" meters"
$cm = $rkm % 100; // rest
echo $x . ' cm = ' . $km . ' kilometers ' . $m . ' meters and ' . $cm . 'centimeters' . PHP_EOL;
$cm = 234459891345;
echo sprintf('km: %02d m: %02d cm: %02d', ($cm/100000),($cm/100%100), $cm%100);
I making one counter, I am using PHP and almost everything was going fine but i found one small bug in it.
My first two step on counter is going fine that is $bid and $stax.
My last result $pay should be : 8138 + 814 + 448 = 9400 but it is giving me $9,399
My Output is:
Value $8,138 bid $814 tax $448 You Pay $9,399
Value $8,952 bid $895 tax $492 You Pay $10,339
Value $9,847 bid $985 tax $542 You Pay $11,373
Here is my php
<?php
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = $v / 10; // output : $814
$ftax = $v + $bid;
$stax = $ftax / 20; // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = $v * 1.1;
} while ($i <= 2);
?>
Thanks in advance :)
Change your echo line to not drop decimals:
echo "Value $" . $v . " bid $" . $bid . " tax $" . $stax . " You Pay $" . $pay . "<br />";
Value $8138 bid $813.8 tax $447.59 You Pay $9399.39Value $8951.8 bid $895.18 tax $492.349 You Pay $10339.329Value $9846.98 bid $984.698 tax $541.5839 You Pay $11373.2619
So 8138 + 813.8 + 447.59 = 9399.39, which rounds down to 9399.
On the other hand, if you round immediately, 813.8 rounds up to 814 and 447.59 rounds up to 448, but you've just added 0.61 to your calculation before even starting, which obviously results in a higher number (9400).
This is why math and science teachers tell you not to round until the very end, since each time you do it, your answer gets less accurate.
if you dont want to use decimals, round off all division calculations by using round(). Also, multiplications with decimals. See it in action here
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = round($v / 10); // output : $814
$ftax = $v + $bid;
$stax = round($ftax / 20); // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = round($v * 1.1);
Lack of casting/decimals be ye problem matey. har har har