Jump to content

[SOLVED] monthly billing not working


Ninjakreborn

Recommended Posts

Ok, here is the code.

		<h1>System Check</h1>
		<p>A full system check is run here on the following things:</p>
		<ul>
		<li>Monthly Billing</li>
		<li>Late Fees Calculations</li>
		<li>Sending out monthly reminders (for members wanting reminders about billers</li>
		</ul>
		<p>The system will run through all it's necessary procedures and provide a full
		report at the end of the entire process.</p>

<?php
# CHECK LAST SYSTEM DATE
// check to see the last day the system check was ran, we only want to allow it to run
// ONE TIME per day.
// However it has to atleast run ONE time per day.
$currentdate = date("m/d/Y");
$selectsysdate = "SELECT * FROM systemcheck WHERE lastcheck = '$currentdate';";
$querysysdate = mysql_query($selectsysdate);
if ($rowsys = mysql_fetch_array($querysysdate)) {
die("System check can only be ran once per day.");
}
# BILL USERS MONTHLY

// Here is where the aggravations start to happen.
// I am going to try to do this in an organized and structured manner so we can 
// hopefully make additions and changes as needed
// FIRST we need to get all the information about the user's so we can work with.  
// We don't need to concern ourselves with any payments they made, or anything like 
// that, because they are handled throughout there respective areas.
$select = "SELECT * FROM cy_members;"; // get users
$query = mysql_query($select); // query users
$billerror = 0; // to report number of successfully billed accounts.
$billok    = 0; // to report the number of failed account billings.
while ($row = mysql_fetch_array($query)) { // get the data to work with
// get all the dates and proper variables prepared so we can start working with them
// don't forget we have timestamps in the database, so if the timestamp is needed
// during these calculations I can go ahead and use the date straight from the
// database to have the timestamp.
	$nextbillingdate = date("m/d/Y", $row['system_nextbillingdate']);
	$lastbillingdate = date("m/d/Y", $row['system_lastbillingdate']);
	$currentowedamount = $row['system_currentdueamount'];
	$currentdate = "06/02/2007"; //date("m/d/Y");
	$currentdate_s = strtotime($currentdate);
	// first we need to perform some simple calculations to find out if there 
	// billing date has arrived or not
	// basically if the current date is the same day as, or after the next billing 
	// date, then it's time to do some calculations
	if ($currentdate_s == $row['nextbillingdate']) {
		// this is the amount they are now going to owe, because 19.95 is added onto there
		// owed amount every month. 
		$newamount = $currentowedamount + "19.95";
		// the last billed date becomes today because they were just billed.
		$newlastbillingdate = $currentdate_s; 
		// the next billing date is 1 month from today (the date they were billed).
		$newnextbillingdate = mktime(date("h", $currentdate_s), date("i", $currentdate_s), date("s", $currentdate_s), date("m", $currentdate_s)+1, date("d", $currentdate_s), date("y", $currentdate_s));
		// so far we have successfully charged them there monthly charges of 19.99, we are
		// also going to hang onto that currentowedamount because that is the amount 
		// they owe before the 15 days are up.
		// Now this will permanently take care of all of these issues with billing monthly
		// the next thign to do is database all the information so we can run the proper
		// calculations for there late fee's if they are applicable.
		$update = "UPDATE cy_members SET system_currentdueamount = '$newamount', system_nextbillingdate = '$newnextbillingdate', system_lastbillingdate = '$newlastbillingdate', system_leftover = '$currentowedamount';"; 
		if (mysql_query($update)) {
			$billok++;
			$billsuccess = $billok . " accounts where billed successfully.";

		}else {
			$billerror++;
			$billfailure = "There was a problem with billing " . $billerror . " members this month.";
		}
	}
	unset($update);
	unset($newnextbillingdate);
	unset($newamount);
	unset($newlastbillingdate);
	unset($currentdate);
	unset($currentowedamount);
	unset($lastbillingdate);
	unset($nextbillingdate);
}
# CALCULATE LATE FEES



# SEND OUT MONTH REMINDERS ON ADDED BILLERS TO MEMBERS


# Progress Report
echo "FULL PROGRESS REPORT";
echo "<hr />";
echo "<h1>Monthly Billing Report</h1>";
echo $billsuccess . "<br />";
echo $billfailure . "<br />";
echo "<hr />";
echo "<h1>Late Fee Calculation Report</h1>";
echo $latefeesuccess . "<br />";
echo $latefeefailure . "<br />";
echo "<hr />";
echo "<h1>Monthly Reminders Reports</h1>";
echo $remindersuccess . "<br />";
echo $reminderfailure . "<br />";
$systemdate = date("m/d/Y");
$insert = "INSERT INTO systemcheck (lastcheck) VALUES ('$systemdate');";
mysql_query($insert);

?>

THis code is going to take me forever, right now I thought I had the recurring billing (monthly) working.  I noticed some issues, so I put in something that only allows the script to run once per day.  This will remove all issues.

I basically have to accomplish 3 parts to this script.

1. Bill users monthly based on there billing period.

2. Calculate late fee’s for overdue balances and.

3. Send out monthly reminders when users request monthly reminders for one of there billers.

Number 1 I thought I had done, and number 3 will be easy.

All of these are things that will only need to be checked on once a day, I am definitely going to need some guidance for number 2 when I get there, but for now I am on number 1.

How do I fix number 1.

I thought I had it working, but it's not.

Any advice.

The way I know it's not working is when I had the other thing setup it seemed to work but I couldn't test it.

Right now I changed is as I was suggested to == instead, and it's not running the tests at all.  Now if it's on the same day or not it's not running the billing cycle.

Link to comment
Share on other sites

Ok, I want to go ahead and ask here before I even start.

I got the monthly thing working

<?

session_start();
header("Cache-control: private"); //IE 6 Fix

if(!$_SESSION['cyadmin']) {
header('Location: ../index.php');
exit;
}		

include('../common/config.php');
include('../common/admin_header.php');
include('../common/functions.php');

?>
		<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
		<h1>System Check</h1>
		<p>A full system check is run here on the following things:</p>
		<ul>
		<li>Monthly Billing</li>
		<li>Late Fees Calculations</li>
		<li>Sending out monthly reminders (for members wanting reminders about billers</li>
		</ul>
		<p>The system will run through all it's necessary procedures and provide a full
		report at the end of the entire process.</p>

<?php
# CHECK LAST SYSTEM DATE
// check to see the last day the system check was ran, we only want to allow it to run
// ONE TIME per day.
// However it has to atleast run ONE time per day.
$currentdate = date("m/d/Y");
$selectsysdate = "SELECT * FROM systemcheck WHERE lastcheck = '$currentdate';";
$querysysdate = mysql_query($selectsysdate);
if ($rowsys = mysql_fetch_array($querysysdate)) {
die("System check can only be ran once per day.");
}
# BILL USERS MONTHLY

// Here is where the aggravations start to happen.
// I am going to try to do this in an organized and structured manner so we can 
// hopefully make additions and changes as needed
// FIRST we need to get all the information about the user's so we can work with.  
// We don't need to concern ourselves with any payments they made, or anything like 
// that, because they are handled throughout there respective areas.
$select = "SELECT * FROM cy_members;"; // get users
$query = mysql_query($select); // query users
$billerror = 0; // to report number of successfully billed accounts.
$billok    = 0; // to report the number of failed account billings.
while ($row = mysql_fetch_array($query)) { // get the data to work with
// get all the dates and proper variables prepared so we can start working with them
// don't forget we have timestamps in the database, so if the timestamp is needed
// during these calculations I can go ahead and use the date straight from the
// database to have the timestamp.
	$nextbillingdate = date("m/d/Y", $row['system_nextbillingdate']);
	$lastbillingdate = date("m/d/Y", $row['system_lastbillingdate']);
	$currentowedamount = $row['system_currentdueamount'];
	$currentdate = "06/02/2007";//date("m/d/Y"); //"06/02/2007"; 
	$currentdate_s = strtotime($currentdate);
	// first we need to perform some simple calculations to find out if there 
	// billing date has arrived or not
	// basically if the current date is the same day as, or after the next billing 
	// date, then it's time to do some calculations
	if ($currentdate == $nextbillingdate) {
		// this is the amount they are now going to owe, because 19.95 is added onto there
		// owed amount every month. 
		$newamount = $currentowedamount + "19.95";
		// the last billed date becomes today because they were just billed.
		$newlastbillingdate = $currentdate_s; 
		// the next billing date is 1 month from today (the date they were billed).
		$newnextbillingdate = mktime(date("h", $currentdate_s), date("i", $currentdate_s), date("s", $currentdate_s), date("m", $currentdate_s)+1, date("d", $currentdate_s), date("y", $currentdate_s));
		// so far we have successfully charged them there monthly charges of 19.99, we are
		// also going to hang onto that currentowedamount because that is the amount 
		// they owe before the 15 days are up.
		// Now this will permanently take care of all of these issues with billing monthly
		// the next thign to do is database all the information so we can run the proper
		// calculations for there late fee's if they are applicable.
		$update = "UPDATE cy_members SET system_currentdueamount = '$newamount', system_nextbillingdate = '$newnextbillingdate', system_lastbillingdate = '$newlastbillingdate', system_leftover = '$currentowedamount';"; 
		if (mysql_query($update)) {
			$billok++;
			$billsuccess = $billok . " accounts where billed successfully.";

		}else {
			$billerror++;
			$billfailure = "There was a problem with billing " . $billerror . " members this month.";
		}
	}
	unset($nextbillingdate);
	unset($lastbillingdate);
	unset($currentdate);
	unset($currentdate_s);
}
# CALCULATE LATE FEES



# SEND OUT MONTH REMINDERS ON ADDED BILLERS TO MEMBERS


# Progress Report
echo "FULL PROGRESS REPORT";
echo "<hr />";
echo "<h1>Monthly Billing Report</h1>";
echo $billsuccess . "<br />";
echo $billfailure . "<br />";
echo "<hr />";
echo "<h1>Late Fee Calculation Report</h1>";
echo $latefeesuccess . "<br />";
echo $latefeefailure . "<br />";
echo "<hr />";
echo "<h1>Monthly Reminders Reports</h1>";
echo $remindersuccess . "<br />";
echo $reminderfailure . "<br />";
$systemdate = date("m/d/Y");
$insert = "INSERT INTO systemcheck (lastcheck) VALUES ('$systemdate');";
mysql_query($insert);

?>

Ok, this works fine, (bills them monthly).

The next step (I have 3 steps I have to accomplish), the next one I need some serious feedback on.

Right now when they are billed they are billed 19.95, whatever was there before is added into leftover (so I can remember how much it was).

Basically is needs to keep up with day's that they are late.

Ok, let me give a rough example.

01-01-2007 - they sign up for an account, and are billed 19.95 for there first payment.  A transaction is also put in for 19.95 (they are already aware of this). (it clears automatically or whatever.

01-15-2007 - if they haven't paid there first month they are added an additional 10.00 onto there owed amount.

02-01-2007 - The bill for this month is added onto the total amount they owe. (19.95)

02-15-2007 - I need to check if they have paid there dues for this month, if they haven't then an additional 10.00 is added as a late fee onto there current amount.

03-01-2007 - They are billed an additional 19.95 (and it's added to the amount they owe.)

03-15-2007 - Need to check if they paid this month's bills (if not) then they are added an additional 10.00 to there current owed amount.

It continues in this same cycle

* The monthly billing cycle is always based on the month they signed up for the account.

 

Ok now that you know this, I have the monthly billing itself done.  Now I just need to check to see if they have paid there dues 15 days after there last billed date, if they did then fine, if not then I need to add a 10.00 fee, but all the while every month, I need to keep up with it??

Any advice on how i can plan this out before tackling it later today will be greatly appreciated.

Link to comment
Share on other sites

I'm really interested in this code as I was ABOUT to start coding a billing system myself, but only part of your code is here, not sure what's in your functions. on a breif scan of it, how are you running it once a day? manually or auto?

 

your table structure isn't here, so I can't really see much. my thinking is that you'd want the members table. then the billing rate, then a sepeate bill tracking table, listing what they've paid, how much, and when the payment was made, this makes the calculations much easier and a snap.

is this how you are setup now? or are you using more or less tables?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.