Jump to content

Calculations, Interest, Diff between two days


aarontbarksdale

Recommended Posts

ALRIGHTY GUYS! I would like to first thank you guys for all the assistance that you've given, all the encouragement, etc...I has helped...along with making me look at something a little different. I still don't have it 100% completed, but tinkering around with it I believe that I almost have it. Here's the output then I will explain the two issues:screenshot7.jpgOkay, ISSUE #1: In RED, I need to find the difference between the payment date and the following month's DUE DATE. However, I've not figured out how to "if the above line had a payment" code. ISSUE #2: In Yellow. If the payment amount completely pays the interest, then the following interest amount should only be from the payment date to the next due date (which won't calculate right until I have Issue #1 taken care of.Here's the code thus far:

while($monthStart <= $today){$p = .08;  //Percentageif (empty($monthEnd)){$interval = $monthStart - $monthEnd;} else {$interval = $monthEnd - $monthStart;}if (isset($pmtamt)){$interval = $pmtdate - $monthStart;} else {$interval = $monthEnd - $monthStart;}//If first line of report, there won't be a $monthEndif ($monthEnd <= $row['datestart']) {  $dayslate = 0;} else {$dayslate = ($interval/(24*60*60))*1;}$intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accruedif (empty($newint)) {$newint = 0;}$totint = $intacc + $newint;echo var_dump($pmtamt) ."= 'pmtamt'<br/>";echo var_dump($dayslate) ."= 'dayslate'<br/>";echo var_dump($intacc) ."= 'intacc'<br/>";echo var_dump($totint) ."= 'totint'<br/>";echo var_dump($appint) ."= 'appint'<br/>";echo var_dump($appprinc) ."= 'appprinc'<br/>";echo var_dump($bal) ."= 'bal'<br/>";echo var_dump($amtdue) ."= 'amtdue'<br/>";echo "<br/>"; $payment_found = false;    foreach ($payments as $payment)    {    if (strtotime($payment['pmt_date']) >= $monthStart && strtotime($payment['pmt_date']) < $monthEnd) {          $payment_found = true; //This segment displays the month's due date line if payment is made within that month if (strtotime($payment['pmt_date']) > $monthStart) {echo '<tr><td>'. date("m.d.Y", $monthStart) . '</td>';echo '<td>'. "$";$due = $amtdue + $bal; echo $english_format_number = number_format($row['supportamount'], 2, '.', ',') .'</td>';echo '<td>$0.00</td>'; echo '<td>'. "$";if (empty($newint)) {$newint = 0;}$totint = 0;if(isset($monthEnd)){$totint = $intacc + $newint;} echo $english_format_number = number_format($totint, 2, '.',',') . '</td>';echo '<td>'. "$";//$ai = $pmtamt - $totint;if (isset($pmtamt)){if ($pmtamt > $totint) {$appint = $totint; //Calculates how much of payment is applied to Interest} else {$appint = $totint - $pmtamt;}} else {$appint = 0;}echo $english_format_number = number_format($appint, 2, '.',',') . '</td>';echo '<td>'. "$";if (isset($pmtamt)){if ($pmtamt > $totint) {$appprinc = $pmtamt - $appint;} else {$appprinc = 0; }}echo $english_format_number = number_format($appprinc, 2, '.',',') . '</td>';echo '<td>'. "$";if (isset($bal)){if (isset($pmtamt)){  $bal = ($row['supportamount'] + $bal)- $appprinc;} else {$bal = $row['supportamount'] + $bal;}} else {$bal = $row['supportamount'];}echo $english_format_number = number_format($bal, 2, '.',',') . '</td></td>';echo '<td>'. $dayslate .'</td></tr>';$newint = $totint;} //End Segment//This segment displays the Payment Line$interval = strtotime($payment['pmt_date']) - $monthStart;echo '<tr><td>'. date("m.d.Y", strtotime($payment['pmt_date'])) .'</td>';echo '<td>'. "$";if ($pmtdate >= $monthStart && $pmtdate <= $monthEnd){$due = $bal;} //$due = $amtdue + $bal; echo $english_format_number = number_format($row['supportamount'], 2, '.',',') . '</td>';echo '<td>'. "$". $english_format_number = number_format($payment['pmt_amt'], 2, '.',',') . '</td>'; echo '<td>'. "$";$dayslate = ($interval/(24*60*60));$intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accruedif (empty($newint)) {$newint = 0;}$totint = $intacc + $newint;echo $english_format_number = number_format($totint , 2, '.',',') . ' </td>';echo '<td>'. "$";if ($payment['pmt_amt'] > $totint) {$appint = $totint; //Calculates how much of payment is applied to Interest} else {$appint = $totint - $$payment['pmt_amt'];}echo $english_format_number = number_format($appint , 2, '.',',') . ' </td>';echo '<td>'. "$";if ($payment['pmt_amt'] > $totint) {$appprinc = $payment['pmt_amt'] - $appint;} else {$appprinc = 0; } echo $english_format_number = number_format($appprinc, 2, '.',',') . ' </td>';echo '<td>'. "$";$bal = $bal - $appprinc;echo $english_format_number = number_format($bal, 2, '.',',') . ' </td>';echo '<td>'. $dayslate .'</td></tr>';$newint = $totint;}//End Payment Line Segment  }  if (!$payment_found) //This segment is the Due Date Line.  {echo '<tr><td>'. date("m.d.Y", $monthStart) . ' </td>';echo '<td>'. "$";$due = $amtdue + $bal; echo $english_format_number = number_format($row['supportamount'], 2, '.',',') .' </td>';echo '<td>$0.00 </td>'; echo '<td>'. "$";$intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accruedif (empty($newint)) {$newint = 0;}$totint = $intacc + $newint;echo $english_format_number = number_format($totint, 2, '.',',') . ' </td>';echo '<td>'. "$";if (isset($pmtamt)){if ($pmtamt > $totint) {$appint = $totint; //Calculates how much of payment is applied to Interest} else {$appint = $totint - $pmtamt;}} else {$appint = 0;}echo $english_format_number = number_format($appint, 2, '.',',') . ' </td>';echo '<td>'. "$";if ($pmtamt > $totint) {$appprinc = $pmtamt - $appint;} else {$appprinc = 0; }echo $english_format_number = number_format($appprinc, 2, '.',',') . ' </td>';echo '<td>'. "$";if (isset($bal)){if (isset($pmtamt)){  $bal = ($row['supportamount'] + $bal)- $appprinc;} else {$bal = $row['supportamount'] + $bal;}} else {$bal = $row['supportamount'];}echo $english_format_number = number_format($bal, 2, '.',',') . ' </td>';echo '<td>'. $dayslate .' </td></tr>';$newint = $totint;}$monthStart = strtotime('+1 month', $monthStart);$monthEnd = strtotime('+1 month', $monthStart); $amtdue = $due;}

Edited by aarontbarksdale
Link to comment
Share on other sites

Fixed the Int Accumulated with

 if ($appint = 0){$totint = $intacc + $newint;} else {$totint = $intacc;}

It does correctly calculate, but for the wrong Days Late...now to figure out days late correction... UPDATE: Yep...I am either stuck...or braindead from staring at the code like Neo stared into the Matrix...any help on this ONE LAST ISSUE!!!

Edited by aarontbarksdale
Link to comment
Share on other sites

When I have a problem with a loop, I use a little js script to target the problem...

$str = $problem_var;echo '<script type="text/javascript">';echo 'alert("' . $str . '");';echo '</script>';

... and the problem pops-up without exiting the script!

Edited by niche
Link to comment
Share on other sites

It doesn't say anything...because there's no CODING errors with regard to syntax, etc...the error is there is a piece of code missing. Just like my code fix that essentially zeroed out the Int. Accumulated if the interest was paid....except I just noticed that the second line after a payment it does not add the interest from the line above it. It DOES zero it out...just doesn't carry down.

Link to comment
Share on other sites

OK. So, now you have to find the mistake. Unless you know specifically where it's at, you need the js snippet to find it. Just move the snippet around or use more than one at the same time. You must find the offendning line to proceed.

Link to comment
Share on other sites

Its not a problem with the code...is a calculation problem...its like when you misspell a word...but the word you misspelled is still spelled correctly... if you look at the pic...the day after a payment the days late should be the difference between the end of the month and the pmt date.

Link to comment
Share on other sites

calculation problem is a code problem IMO. Do you know EXACTLY where the problem is?

Link to comment
Share on other sites

By exact, I mean which iteration and which line has the mistake. Generalities won't be much help at this point. The snippet is the only way, I'm aware of, to make that determination in the easiest way possible especially with a script as large as yours.

Edited by niche
Link to comment
Share on other sites

I think the problem is somewhere here...because this is the code segment that occurrs directly AFTER a payment line.

 if (!$payment_found) {echo '<tr><td>'. date("m.d.Y", $monthStart) . ' </td>';echo '<td>'. "$";$due = $amtdue + $bal; echo $english_format_number = number_format($row['supportamount'], 2, '.',',') .' </td>';echo '<td>$0.00 </td>'; echo '<td>'. "$";$intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accruedif (empty($newint)) {$newint = 0;}if ($appint = 0){$totint = $intacc + $newint;} else {$totint = $intacc;}//$totint = $intacc + $newint;echo $english_format_number = number_format($totint, 2, '.',',') . ' </td>';echo '<td>'. "$";if (isset($pmtamt)){if ($pmtamt > $totint) {$appint = $totint; //Calculates how much of payment is applied to Interest} else {$appint = $totint - $pmtamt;}} else {$appint = 0;}echo $english_format_number = number_format($appint, 2, '.',',') . ' </td>';echo '<td>'. "$";if ($pmtamt > $totint) {$appprinc = $pmtamt - $appint;} else {$appprinc = 0; }echo $english_format_number = number_format($appprinc, 2, '.',',') . ' </td>';echo '<td>'. "$";if (isset($bal)){if (isset($pmtamt)){$bal = ($row['supportamount'] + $bal)- $appprinc;} else {$bal = $row['supportamount'] + $bal;}} else {$bal = $row['supportamount'];}echo $english_format_number = number_format($bal, 2, '.',',') . ' </td>';echo '<td>'. $dayslate .' </td></tr>';$newint = $totint;

Link to comment
Share on other sites

Okay...got the Days Late corrected. Added the following code to the END of the Payment "IF" structure:

$dayslateP = ($monthEnd - strtotime($payment['pmt_date']))/(24*60*60);

And I added this to the Regular non-payment related IF structure:

if ($appint > 0){$interval = $dayslateP;$dayslate = $dayslateP;} else {$interval = $dayslate/(60*24*60);[/size][/font][/color]}

WORKS!!! Now to figure out the reason the interest isn't adding correctly...

Edited by aarontbarksdale
Link to comment
Share on other sites

FINISHED!!!! THANK YOU niche AND justsomeguy for all of your help with this! I wish I knew your real names so I can thank you properly! THANK YOU THANK YOU THANK YOU for everything you've done to help me with this. Without your guidance I don't think I could have done it as quickly!

Link to comment
Share on other sites

As far as formatting the code, you should use indenting to help make the structure more obvious. Instead of this:

while($monthStart <= $today){$p = .08;  //Percentageif (empty($monthEnd)){$interval = $monthStart - $monthEnd;} else {$interval = $monthEnd - $monthStart;}if (isset($pmtamt)){$interval = $pmtdate - $monthStart;} else {$interval = $monthEnd - $monthStart;}//If first line of report, there won't be a $monthEndif ($monthEnd <= $row['datestart']) { $dayslate = 0;} else {$dayslate = ($interval/(24*60*60))*1;}$intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accruedif (empty($newint)) {$newint = 0;}$totint = $intacc + $newint;echo var_dump($pmtamt) ."= 'pmtamt'<br/>";echo var_dump($dayslate) ."= 'dayslate'<br/>";echo var_dump($intacc) ."= 'intacc'<br/>";echo var_dump($totint) ."= 'totint'<br/>";echo var_dump($appint) ."= 'appint'<br/>";echo var_dump($appprinc) ."= 'appprinc'<br/>";echo var_dump($bal) ."= 'bal'<br/>";echo var_dump($amtdue) ."= 'amtdue'<br/>";echo "<br/>";$payment_found = false;    foreach ($payments as $payment)    {    if (strtotime($payment['pmt_date']) >= $monthStart && strtotime($payment['pmt_date']) < $monthEnd) {		  $payment_found = true;//This segment displays the month's due date line if payment is made within that monthif (strtotime($payment['pmt_date']) > $monthStart) {echo '<tr><td>'. date("m.d.Y", $monthStart) . '</td>';echo '<td>'. "$";$due = $amtdue + $bal;echo $english_format_number = number_format($row['supportamount'], 2, '.', ',') .'</td>';echo '<td>$0.00</td>';echo '<td>'. "$";if (empty($newint)) {$newint = 0;}$totint = 0;if(isset($monthEnd)){$totint = $intacc + $newint;}echo $english_format_number = number_format($totint, 2, '.',',') . '</td>';echo '<td>'. "$";//$ai = $pmtamt - $totint;if (isset($pmtamt)){if ($pmtamt > $totint) {$appint = $totint; //Calculates how much of payment is applied to Interest} else {$appint = $totint - $pmtamt;}} else {$appint = 0;}echo $english_format_number = number_format($appint, 2, '.',',') . '</td>';

It's easier to read this:

while($monthStart <= $today){  $p = .08;  //Percentage  if (empty($monthEnd)){    $interval = $monthStart - $monthEnd;  } else {    $interval = $monthEnd - $monthStart;  }  if (isset($pmtamt)){    $interval = $pmtdate - $monthStart;  } else {    $interval = $monthEnd - $monthStart;  }  //If first line of report, there won't be a $monthEnd  if ($monthEnd <= $row['datestart']) {     $dayslate = 0;  } else {    $dayslate = ($interval/(24*60*60))*1;  }  $intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accrued  if (empty($newint)) {    $newint = 0;  }  $totint = $intacc + $newint;  echo var_dump($pmtamt) ."= 'pmtamt'<br/>";  echo var_dump($dayslate) ."= 'dayslate'<br/>";  echo var_dump($intacc) ."= 'intacc'<br/>";  echo var_dump($totint) ."= 'totint'<br/>";  echo var_dump($appint) ."= 'appint'<br/>";  echo var_dump($appprinc) ."= 'appprinc'<br/>";  echo var_dump($bal) ."= 'bal'<br/>";  echo var_dump($amtdue) ."= 'amtdue'<br/>";  echo "<br/>";  $payment_found = false;  foreach ($payments as $payment)  {    if (strtotime($payment['pmt_date']) >= $monthStart && strtotime($payment['pmt_date']) < $monthEnd) {	  $payment_found = true;	  //This segment displays the month's due date line if payment is made within that month	  if (strtotime($payment['pmt_date']) > $monthStart) {	    echo '<tr><td>'. date("m.d.Y", $monthStart) . '</td>';	    echo '<td>'. "$";	    $due = $amtdue + $bal;	    echo $english_format_number = number_format($row['supportamount'], 2, '.', ',') .'</td>';	    echo '<td>$0.00</td>';	    echo '<td>'. "$";	    if (empty($newint)) {		  $newint = 0;	    }	    $totint = 0;	    if(isset($monthEnd)){		  $totint = $intacc + $newint;	    }	    echo $english_format_number = number_format($totint, 2, '.',',') . '</td>';	    echo '<td>'. "$";	    //$ai = $pmtamt - $totint;	    if (isset($pmtamt)){		  if ($pmtamt > $totint) {		    $appint = $totint; //Calculates how much of payment is applied to Interest		  } else {		    $appint = $totint - $pmtamt;		  }	    } else {		  $appint = 0;	    }        echo $english_format_number = number_format($appint, 2, '.',',') . '</td>';

The forum may mess with the indenting some, but hopefully you get the idea. It's much easier to see the structure of the code when things are indented.

Link to comment
Share on other sites

There's only a few things in life that feel as good as self-reliance.

Link to comment
Share on other sites

Well...with real life examples...the code messed up royally...will post output later....too tired...but thanks again.

Link to comment
Share on other sites

Let's try this again...here's the code:

 while($monthStart <= $today) {$p = .08;  //Percentage for MISSISSIPPI     if (empty($monthEnd)){          $interval = $monthStart - $monthEnd;     } else {          $interval = $monthEnd - $monthStart;     }     if (isset($pmtamt)){          $interval = $pmtdate - $monthStart;     } else {          $interval = $monthEnd - $monthStart;     }//If first line of report, there won't be a $monthEnd     if ($monthEnd <= $row['datestart']) {           $dayslate = 0;     } else {         $dayslate = ($interval/(24*60*60))*1;     } $intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accrued      if (empty($newint)) {         $newint = 0;     } $totint = $intacc + $newint; $payment_found = false;    foreach ($payments as $payment)    {    if (strtotime($payment['pmt_date']) >= $monthStart && strtotime($payment['pmt_date']) < $monthEnd) {          $payment_found = true;////This segment displays the month's due date line if payment is made within that month //       if (strtotime($payment['pmt_date']) > $monthStart) {          echo '<tr><td>'. date("m.d.Y", $monthStart) . '</td>';          echo '<td>'. "$";          $due = $amtdue + $bal;           echo $english_format_number = number_format($row['supportamount'], 2, '.', ',') .'</td>';         echo '<td>$0.00</td>';          echo '<td>'. "$";         if (empty($newint)) {              $newint = 0;         }         $totint = 0;         if(isset($monthEnd)){              $totint = $intacc + $newint;         }          echo $english_format_number = number_format($totint, 2, '.',',') . '</td>';         echo '<td>'. "$";         if (isset($pmtamt)){                  if ($pmtamt > $totint) {                       $appint = $totint; //Calculates how much of payment is applied to Interest                  } else {                       $appint = $totint - $pmtamt;                  }         } else {              $appint = 0;         }         echo $english_format_number = number_format($appint, 2, '.',',') . '</td>';         echo '<td>'. "$";         if (isset($pmtamt)){              if ($pmtamt > $totint) {                  $appprinc = $pmtamt - $appint;              } else {                  $appprinc = 0;               }         }         echo $english_format_number = number_format($appprinc, 2, '.',',') . '</td>';         echo '<td>'. "$";         if (isset($bal)){              if (isset($pmtamt)){                  $bal = ($row['supportamount'] + $bal)- $appprinc;              } else {                  $bal = $row['supportamount'] + $bal;              }         } else {              $bal = $row['supportamount'];         }         echo $english_format_number = number_format($bal, 2, '.',',') . '</td></tr>';         $newint = $totint;         } //End Segment////This segment displays the Payment Line//          $interval = strtotime($payment['pmt_date']) - $monthStart;         echo '<tr><td>'. date("m.d.Y", strtotime($payment['pmt_date'])) .'</td>';         echo '<td>'. "$";         if ($pmtdate >= $monthStart && $pmtdate <= $monthEnd){              $due = $bal;         }          echo $english_format_number = number_format($row['supportamount'], 2, '.',',') . '</td>';         echo '<td>'. "$". $english_format_number = number_format($payment['pmt_amt'], 2, '.',',') . '</td>';          echo '<td>'. "$";         $dayslate = ($interval/(24*60*60));         $intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accrued         if (empty($newint)) {              $newint = 0;         }         $totint = $intacc + $newint;         echo $english_format_number = number_format($totint , 2, '.',',') . ' </td>';         echo '<td>'. "$";         if ($payment['pmt_amt'] > $totint) {              $appint = $totint; //Calculates how much of payment is applied to Interest         } else {              $appint = $totint - $payment['pmt_amt'];         }         echo $english_format_number = number_format($appint , 2, '.',',') . ' </td>';         echo '<td>'. "$";         if ($payment['pmt_amt'] > $totint) {              $appprinc = $payment['pmt_amt'] - $appint;         } else {              $appprinc = 0;          }          echo $english_format_number = number_format($appprinc, 2, '.',',') . ' </td>';         echo '<td>'. "$";         $bal = $bal - $appprinc;         echo $english_format_number = number_format($bal, 2, '.',',') . ' </td></td>';         $newint = $totint;         $dayslateP = ($monthEnd - strtotime($payment['pmt_date']))/(24*60*60);         }         //End Payment Line Segment     }////This segment is the Due Date Line.//     if (!$payment_found) {          echo '<tr><td>'. date("m.d.Y", $monthStart) . ' </td>';          if ($appint > 0){               $interval = $dayslateP;               $dayslate = $dayslateP;           } else {               $interval = $dayslate/(60*24*60);          }          echo '<td>'. "$";          $due = $amtdue + $bal;           echo $english_format_number = number_format($row['supportamount'], 2, '.',',') .' </td>';          echo '<td>$0.00 </td>';           echo '<td>'. "$";          $intacc = $dayslate * $bal * ($p/365); //Calculates Interest Accrued          if (empty($newint)) {               $newint = 0;          }          if ($appint > 0){               $totint = $intacc;          } else {               $totint = $intacc + $newint;          }          echo $english_format_number = number_format($totint, 2, '.',',') . ' </td>';          echo '<td>'. "$";          if (isset($pmtamt)){               if ($pmtamt > $totint) {                    $appint = $totint; //Calculates how much of payment is applied to Interest               } else {                    $appint = $totint - $pmtamt;               }          } else {               $appint = 0;          }          echo $english_format_number = number_format($appint, 2, '.',',') . ' </td>';          echo '<td>'. "$";          if ($pmtamt > $totint) {               $appprinc = $pmtamt - $appint;          } else {               $appprinc = 0;           }          echo $english_format_number = number_format($appprinc, 2, '.',',') . ' </td>';          echo '<td>'. "$";          if (isset($bal)){               if (isset($pmtamt)){                    $bal = ($row['supportamount'] + $bal)- $appprinc;               } else {                    $bal = $row['supportamount'] + $bal;               }          } else {               $bal = $row['supportamount'];          }          echo $english_format_number = number_format($bal, 2, '.',',') . ' </td></tr>';          $newint = $totint;     }$monthStart = strtotime('+1 month', $monthStart);$monthEnd = strtotime('+1 month', $monthStart);$amtdue = $due;}

Here's part of the output with REAL WORLD Numbers: (I was going to highlight it...but after a few moments, you'll see why the entire image would have been highlighted.screenshot8.jpg

Link to comment
Share on other sites

As you can see, there appears to be a redundancy whenever there are multiple payments within the same month. I know HOW this is happening, but no matter where I place my code, it doesn't rectify the situation. There are TWO "IF" statements within the "Payment = true" IF statement, one displays the Due date IF the payment is made after the next $monthStart (if I leave it out, it doesn't add the next Due date), in the above example, it would have 3.15.2010, but wouldn't have 4.15.2010. then when it loops back around to add the next payment, it's adding another Due Date line...still just not sure how to fix it... ANY feedback will help.

Link to comment
Share on other sites

First, this: if(isset($monthEnd)){ is always going to be true, you're not unsetting monthEnd anywhere. You also have several if statements where you're checking if $pmtamt is set, I'm not sure what you're doing there. What does it mean if $pmtamt is set or not? Does it ever get unset? I don't see you setting it inside the loop anywhere, is it set outside the loop? I'm not sure what the logic is inside the foreach loop. I was thinking you would use that loop to print each payment for the current month, why are you printing 2 rows of data each time through the loop (if the first if statement matches)? If the first row is the payment for that month, what is the second row? As for the part that you commented as the Due Date Line, that is only going to print if there were no payments for the current month. I'm just not sure I follow the logic that you have in that loop, I'm not sure why you have it doing what it's doing. Also, there's no reason to keep setting the $english_format_number variable. Instead of doing this: echo $english_format_number = number_format($row['supportamount'], 2, '.',',') . '</td>'; you can just do this: echo number_format($row['supportamount'], 2, '.',',') . '</td>'; Also, what's the point of this:

	 if (empty($monthEnd)){		  $interval = $monthStart - $monthEnd;	 } else {		  $interval = $monthEnd - $monthStart;	 }	 if (isset($pmtamt)){		  $interval = $pmtdate - $monthStart;	 } else {		  $interval = $monthEnd - $monthStart;     }

If you're always setting $interval in the second if/else, then what's the point of the first if/else? Do you understand what the isset and empty functions check for? You could probably simplify your code a lot if you only have one section that prints the HTML, and just set the various values above. So you could have variables like $date, $amt_due, $amt_paid, $int_acc, etc, which you calculate one of several ways depending on your logic, and then at the end of the loop you have one section that prints the HTML that uses the variables that were previously set.

Link to comment
Share on other sites

Okay...so, this shows how much I DON'T know...1. No, $monthEnd is not unset ANYWHERE, there's no argument there.2. As for all of the isset($pmtamt) checks, that's because I was trying to determine if the previous line was a payment line, if not, the calculations would be different3. The reason for the two lines that it shows in the one loop is because I was trying to make sure that the Date Due line printed in the same month when a payment is due, otherwise it doesn't print at all, doesn't include the amount that is due for that month and the numbers don't add up.4. Thanks for the tip on the number_format5. It first part of the snippet you posted determine's if this is the first month and how to calculate the difference between monthStart and monthEnd...but I see the issue because the very next part changes the $interval. So....crap...aparently I don't understand the isset & empty functions do. That's what I've been trying to do, simplify my code...I'm very n00b with this, and I'm learning as I go...I've pleaded for help, I'm willing to pay, but NO ONE will help me out by showing me what I need to do and explaining to me WHAT and HOW the code corrections work. :-( Any takers???

Link to comment
Share on other sites

OK....So no takers....I will be reevaluating my code...Any suggestions on where I should start? Should I move 100% of the calculations within each if statement to prevent conflicts of variables...and unset variables at the end of each if statement?

Link to comment
Share on other sites

It's usually not necessary to unset variables each time through the loop. If you need to keep track of things like various things about the previous line, I typically just use boolean flags for that kind of thing. That's what the $payment_found variable does, you can use that to keep track of whether the previous line was a payment line. So next time through the loop you can check it (you just have to check it before you reset it back to false), e.g.:

while($monthStart <= $today) {	$previous_payment = $payment_found;	...	$payment_found = false;	foreach ($payments as $payment)	{	  ...

As far as isset and empty, any time you set a variable to any value then isset will always return true. The only way to have isset return false after that is to use the unset function to basically delete the variable. The empty function is a little different, empty will check if a variable is set and also if the value is a non-false value. http://www.php.net/m...ction.empty.php There is a list of values on that page that the variable can be set to in order to be considered empty. All of these would be considered empty: $var = '';$var = false;$var = array();$var = 0; etc. At this point things have changed several times, if you want to start over then start with a description of the data you have in the database, and a description about what you want to show and calculate. Someone might be willing to do it for you, or at least we can start with a roadmap of how to get there from here.

  • Like 1
Link to comment
Share on other sites

Okay...so, I will work on reconfiguring my code, but here's a road map (table.row)1. First line will be first due date, starting with user entered "Start Date" - clients.datestart (Start date will generate a new due date exactly 1 month from it, if it's due on the 10th, then the Due Date will be on the 10th of the following month)2. IF payment is made before next due date, enter line with payments. - payments.pmt_date, payments.pmt_amt a. Determine the interest accrued between Start Date or Due Date and the Payment Date (Payment due on 10th, and payment made on 20th, 9 days of interest will be accrued [interest calculated by (%/365)*Days Late*amt_due]) b.(1) IF payment is GREATER THAN Interest accrued (usually will be) then "Applied to Interest" will equal Interest Accrued. (2) IF payment is LESS THAN interest accrued then Interest Accrued LESS Payment amount. c. IF b(1) is TRUE, then Applied to Principle will equal Payment Amount LESS Interest Accrued3. Any remaining balance will be added into the interest accrued on the following line (whether another payment or another due date)4. If no other payments were made within the same month, then next due date line is automatically generated with interest calculation applied for UNPAID balance from previous line's (due date or payment) remaining balance, if any.5. Code will AUTOMATICALLY stop when $monthStart <= today();Hope that road map is as simple as it appears to me. Below is my db structure (table.row, if just .row then it applies to above table)

---- Table structure for table `clients`-- CREATE TABLE `clients` (  `client_id` int(10) NOT NULL auto_increment,  `userid` varchar(20) NOT NULL,  `cp_firstname` varchar(15) NOT NULL,  `cp_lastname` varchar(20) NOT NULL,  `cp_address` varchar(50) NOT NULL,  `cp_city` varchar(25) NOT NULL,  `cp_state` varchar(2) NOT NULL,  `cp_zip` varchar(5) NOT NULL,  `cp_zip4` varchar(4) NOT NULL,  `cp_homeph` varchar(12) NOT NULL,  `cp_workph` varchar(12) NOT NULL,  `cp_cellph` varchar(12) NOT NULL,  `cp_email` varchar(50) NOT NULL,  `alt_firstname` varchar(15) NOT NULL,  `alt_lastname` varchar(20) NOT NULL,  `alt_homeph` varchar(12) NOT NULL,  `alt_cellph` varchar(12) NOT NULL,  `non_firstname` varchar(15) NOT NULL,  `non_lastname` varchar(20) NOT NULL,  `non_home` varchar(50) NOT NULL,  `non_city` varchar(20) NOT NULL,  `non_state` varchar(2) NOT NULL,  `non_zip` varchar(5) NOT NULL,  `non_zip4` varchar(4) NOT NULL,  `non_homeph` varchar(12) NOT NULL,  `non_workph` varchar(12) NOT NULL,  `non_cellph` varchar(12) NOT NULL,  `non_email` varchar(50) NOT NULL,  `placeofemployment` varchar(50) NOT NULL,  `datestart` date NOT NULL,  `supportamount` varchar(10) NOT NULL,  `judgmentdate` date NOT NULL,  `judge` varchar(50) NOT NULL,  PRIMARY KEY  (`client_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=149 ; ---- Table structure for table `messages` !!!NOT USED YET!!!-- CREATE TABLE `messages` (  `msg_id` varchar(3) NOT NULL,  `userid` varchar(20) NOT NULL,  `message` longtext NOT NULL,  PRIMARY KEY  (`msg_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;  ---- Table structure for table `payments`-- CREATE TABLE `payments` (  `pay_id` int(5) NOT NULL auto_increment,  `client_id` varchar(10) NOT NULL,  `userid` varchar(20) NOT NULL,  `pmt_date` date NOT NULL,  `pmt_amt` varchar(5) NOT NULL,  PRIMARY KEY  (`pay_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=64 ; CREATE TABLE `users` (  `userid` mediumint(10) unsigned NOT NULL auto_increment,  `entityid` varchar(10) character set utf8 collate utf8_bin NOT NULL,  `first_name` varchar(30) NOT NULL,  `last_name` varchar(30) NOT NULL,  `username` varchar(25) NOT NULL,  `password` varchar(25) NOT NULL,  `email` varchar(40) default NULL,  `reg_date` datetime NOT NULL default '0000-00-00 00:00:00',  `superuser` varchar(1) default 'N',  PRIMARY KEY  (`userid`),  UNIQUE KEY `entityid` (`entityid`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; 

Link to comment
Share on other sites

I can quote that if you want, it would probably end up being a few hundred bucks. I would approach it by first creating an array of everything that needs to be printed. So, that starts with getting the payments from the database. Then you would set up a loop where you go through and add each payment due line to the array, since that information doesn't come from the database. So then you have an array that contains all of the rows that need to be displayed in the table (you aren't writing your own rows in the display loop, just printing what's in the array). It wouldn't contain the interest calculations or things at that point though, just payments from the database plus the extra rows you add for due dates. Once you have the array then you sort it by date, and then loop through to print each array item out. When you're looping through to print everything then you would do the interest and balance calculations at that point, as you're going through each row. The point is to create the array with every row that you need to display in the table first, then loop through that and print each item in the array as a row in the table, without dynamically adding new rows as you're going (just doing the calculations). You already have the code to get the payments from the database and set up the array with payments. After that, you loop from start to end dates and for each month you add a payment due line to the array. Then you sort the array (probably using the usort function, to sort by the date field), and start another loop to do the calculation and printing. I like that approach better then looping through the payments and trying to decide in that loop when to output the payment due line. I was confused by what you were doing when I suggested the structure above, I thought that for each month you either wanted to print the payments, or print a default row if there were no payments. In reality you need to print a row for each month, plus any payment information. Putting both the payments and due dates in the same array will also make it easier to print everything in chronological order, instead of needing to determine when to print the due date line.

Link to comment
Share on other sites

Send me a message of what you would charge to do this, because you have flat out lost me. I've got an array already that retrieves everything from the payments table.

$payments = array();while ($row2 = mysql_fetch_assoc($data2)) {     $payments[] = $row2;}... // This is in the main body of the page$payment_found = false;foreach ($payments as $payment) {         if (strtotime($payment['pmt_date']) >= $monthStart && strtotime($payment['pmt_date']) <= $monthEnd) {          $payment_found = true;          echo '<tr><td>'. date("m.d.Y", strtotime($payment['pmt_date'])) .'</td>';          echo '<td>$0.00</td>';          echo '<td>'. "$". number_format($payment['pmt_amt'], 2, '.',',') . '</td>';           echo '<td>'. "$". number_format($intacc , 2, '.',',') . ' </td>';          echo '<td>'. "$". number_format($appint , 2, '.',',') . ' </td>';          echo '<td>'. "$". number_format($appprinc, 2, '.',',') . ' </td>';          echo '<td>'. "$". number_format($bal, 2, '.',',') . ' </td></td>';     }

Is this something that you are talking about...of course it only pulls two pieces of the db information from the 'payments' table...the rest are eventual calculations.

Link to comment
Share on other sites

Right, that's what I was referring to. You're already building an array of payment information. The next step is to add each payment due line to the same array. The goal is to end up with an array that corresponds with your table, so that you have as many items in the array as you have rows in the table. That way you can loop through the array and just print each item (plus calculations) instead of trying to determine if you need to output an extra row. That's what the while loop should be doing, populating that array with payment due items. After that you can use a regular foreach loop to print the table. e.g.:

$payments = array();while ($row2 = mysql_fetch_assoc($data2)) {	 $payments[] = $row2;} while($monthStart <= $today) {  $payments[] = array(    'payment_due' => true,    'amount' => 500,    'pmt_date' => $monthStart  );    $monthStart = strtotime('+1 month', $monthStart);  $monthEnd = strtotime('+1 month', $monthStart);} usort($payments, 'function_to_compare_dates'); foreach ($payments as $item){  if (isset($item['payment_due'])) // this is a payment due line  {    // add to balance due, calculate interest, etc    ...  }  else // this is a payment line  {    // apply payment to interest/principal, etc    ...  }}

Something along those lines. I called the date for the payment due line pmt_date so that it matches what comes from the database, so that the sort function can look for the date in the same place. The end result will be an array of line items sorted in chronological order, and for each line item you either add to the balance due and calculate interest, or apply the payment. I'll send you a PM after work with an estimate of what I think it would take.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...