Auto generate invoices from csv files with PHP
Tax season again, and i had to submit all invoices to file taxes. I always wanted a way to automatically generate invoices from the CSV file, i had which details like sales and billing activity from 2checkout.
I never liked buying accounting software specifically for this and i believe anything can be done with PHP magic code, which is why i coded myself to accomplish whatever i wanted..
All i had to do is just parse the CSV file using fgetcsv() and echo the data on the format of the invoice. It was pretty easy and i was accomplish this within 2 hrs. You just need to put this php file and the xls file on same folder in xampp or wamp and this script will neatly print invoices for all sales activity.
If you are using 2CO, just download the sales and billing activity, separated by semicolon (;). Note that this script can only parse xls file, separated by semi-colon. if you want to work for comma (,) just tweak the below code. Within this code you can uncomment lines, which is not needed. I have left as it as just to play around.
Here is the code…
<?php
/* CSV First Row Array Data Format
Array ( [0] => Sale [1] => Total [2] => Date Placed
[3] => Date Shipped [4] => Name [5] => Address
[6] => City [7] => State [8] => Postal Code
[9] => Country [10] => Phone [11] => Email [12] => Ship To
[13] => Ship Address [14] => Ship City [15] => Ship State
[16] => Ship Postal Code [17] => Ship Country [18] => Date Paid
[19] => Refund [20] => Refund Date [21] => Chargeback
[22] => Chargeback Date [23] => Vendor Product ID [24] => Product Name
*/
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000,'\r\n')) !== FALSE) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
// use comma if you have data separated by commas in explode
$data = explode(';',$data[$c]);
//echo $data[5];
?>
<table width="90%" height="225" border="1">
<tr>
<td colspan="2" align="center"><strong>I N V O I C E</strong></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="399"><p><strong>ABC LLC</strong><br />
West End Blvd #2999<br />
Chicago, IL 98829<br />
United States<br />
</p>
<p>Company Registration No: 29204929423 <br />
VAT: EX239402349</p>
<p>Website: http://www.example.com<br />
Email: email@example.com
<br />
</p></td>
<td width="346"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><p>Client / Customer: </p>
<p> <?php echo $data[4]; ?> <br />
<?php echo $data[5]; ?><br>
<?php echo $data[6]; ?> <?php echo $data[8]; ?><br>
<?php echo $data[7]; ?><br>
<?php echo $data[9]; ?><br>
Tel: <?php echo $data[10]; ?>
<br />
</p></td>
<td><p>Invoice no: <?php echo $data[0]; ?> </p>
<p>Date: <?php echo $data[2]; ?> </p></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><table width="100%" border="1">
<tr>
<th bgcolor="#caeffc" scope="col">Item</th>
<th bgcolor="#caeffc" scope="col">Description</th>
<th bgcolor="#caeffc" scope="col">Qty</th>
<th bgcolor="#caeffc" scope="col">Price</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>#<?php echo $data[23]; ?></td>
<td><?php echo $data[24]; ?></td>
<td>1</td>
<td><?php echo $data[1]; ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="center">Thank you for the payment.</td>
<td align="right">Total Amount Paid: USD <?php echo $data[1] ?></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
<p></p>
<?php
//print_r($data);
}
}
fclose($handle);
}
?>
Similar Posts:
- How to import text data in mysql with spaces?
- How to use LOAD DATA INFILE to import fixed width data in MySQL
- Tutorial: How to write a WordPress Plugin?
- Fix -> MySQL LOAD DATA Infile – Access Denied using password YES
- How to insert HTML/BBcode tags to WordPress Comments Form
- Facebook like button not working!
- MySQL delete is very slow and takes long time!
- Credit card expiring with Paypal?
- Implementing Secure File Upload in PHP
- How to filter & escape data from Injection attacks in PHP!



May 11, 2012
Thanks Man. I was able to customize it for my needs. I am also amazed how on did u think it will be useful to someone else!!!