I had the situation a few years back where we were running a custom developed job system (PHP with mySQL) and wanted to import the invoices from this database into the account software we were running which was MYOB.
I thought this would be as simple as exporting the data to a CSV file and importing, however MYOB was a little precious about how it required the file to be formatted and it took a bunch of research to work out the best way to get this done.
This example code works via the Import / Export Assistant in MYOB, which has a bunch of options available, such as importing customers etc.
The below example is based on inserting invoices already created, but you could adapt it to suit your exact needs, such as website orders or any other custom type of database you may have in place.
First off we will setup PHP to output as a CSV file and give it a file name.
<? // set php to output as CSV file type header('Content-type: application/ms-excel'); header("Pragma: no-cache"); header("Expires: 0"); // set your file name, this one has the current date header("Content-Disposition: attachment; filename=invoices-".date("d-m-Y").".csv");
Next, we are going to define some settings like tax and days due that will be used on all rows. We will also pull in our data source, for the purposes of this demonstration to keep everything in a single, easy to understand file, I have used an array, but you may replace this with your mySQL database connection and SQL query.
// here we set some of the tax and days due settings $taxcode = 'GST'; $taxamount = 0.1; // 10% for GST $daysdue = 5; $myob_account_code = 41100; // sample data source, replace this with your database connection etc $sampledata = array( array( "customer_id" => 12345, "customer_name" => "Test Company Name", "customer_address" => "123 Henry Street", "customer_suburb" => "Sydney", "customer_postcode" => "2000", "order_description" => "Web Design Services", "order_price" => 2000, "order_number" => 23232, "order_date" => "2017-10-10" ), array( "customer_id" => 12345, "customer_name" => "Test Company Name", "customer_address" => "123 Henry Street", "customer_suburb" => "Sydney", "customer_postcode" => "2000", "order_description" => "Printing Business Cards", "order_price" => 375.50, "order_number" => 23233, "order_date" => "2017-10-16" ), array( "customer_id" => 98989, "customer_name" => "The Other Company", "customer_address" => "44 Ross Street", "customer_suburb" => "Melbounre", "customer_postcode" => "3000", "order_description" => "Graphic Design Services", "order_price" => 1000, "order_number" => 23234, "order_date" => "2017-10-14" ) );
Lastly, we will actually output the header rows and loop through our data source to display in the format MYOB requires.
// write the header row with MYOB order and names, yes the tabs are required echo "Co./Last Name,Addr 1 - Line 1, - Line 2, - Line 3,Invoice #,Date,Customer PO,Description,Account #,Amount,Journal Memo,Tax Code,Inc-Tax Amount,Terms - Payment is Due, - Discount Days, - Balance Due Days,Card ID\r\n"; // loop through the data foreach ($sampledata as $i => $row) { // output each record echo ''.$row['customer_name'].','.$row['customer_name'].',"'.$row['customer_address'].'","'.$row['customer_suburb'] . ' ' . $row['customer_postcode'].'",'.$row['order_number'].','.date('d/m/Y', strtotime($row['order_date'])).',,'.$row['order_description'].','.$myob_account_code.','.number_format($row['order_price'], 2, '.', '').',"Sale; '.$row['order_description'].'",'.$taxcode.','.number_format(($row['order_price']*$taxamount), 2, '.', '').',5,1,'.$daysdue.','.$row['customer_id']; //create empty line before next record, not sure why MYOB requires this echo "\r\n"; echo "\r\n"; } ?>
So that’s it, I hope this might save someone a little time and allow you to build on this example to suit your needs.
Some other things to note: