- Posts: 42
- Thank you received: 2
Hikashop Orders to Xero Invoices - My Working PHP Code
11 years 5 months ago - 11 years 5 months ago #199472
by GSW00d
Hikashop Orders to Xero Invoices - My Working PHP Code was created by GSW00d
This is not strictly a bug, but rather intended for anyone wanting to use my code as a template from which to work. I've edited the backend order->export view.
I have some logic that is strictly for my own needs relating to courier waybills, currency, custom products, international shipping method names, admin fees etc. Please adapt to your own needs.
If any PHP programmers have any tips for better/efficient/elegant factoring, please let me know as I am still new to coding.
Regards,
Bruce Atkinson
I have some logic that is strictly for my own needs relating to courier waybills, currency, custom products, international shipping method names, admin fees etc. Please adapt to your own needs.
If any PHP programmers have any tips for better/efficient/elegant factoring, please let me know as I am still new to coding.
Regards,
Bruce Atkinson
Code:
<?php
/**
* @package HikaShop for Joomla!
* @version 2.3.5
* @author hikashop.com
* @copyright (C) 2010-2015 HIKARI SOFTWARE. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die('Restricted access');
?><?php
while (ob_get_level() > 1)
ob_end_clean();
$config =& hikashop_config();
$format = $config->get('export_format', 'csv');
$separator = $config->get('csv_separator', ';');
$force_quote = $config->get('csv_force_quote', 1);
$export = hikashop_get('helper.spreadsheet');
date_default_timezone_set('Africa/Johannesburg');
$export->init($format, 'Hikashop Order Export ' . date('Y-m-d H:i'), $separator, $force_quote);
if (!empty($this->orders)) {
//
// HEADER ROW
//
$headerRow = array();
$o = reset($this->orders);
$o = null;
// Add dynamic keys
$headerRow[] = 'InventoryItemCode';
$headerRow[] = 'Description';
$headerRow[] = 'Quantity';
$headerRow[] = 'UnitAmount';
$headerRow[] = 'AccountCode';
$headerRow[] = 'TaxType';
// Add static keys
$headerRow[] = 'ContactName';
$headerRow[] = 'EmailAddress';
$headerRow[] = 'POAddressLine1';
$headerRow[] = 'POAddressLine2';
$headerRow[] = 'POAddressLine3';
$headerRow[] = 'POAddressLine4';
$headerRow[] = 'POCity';
$headerRow[] = 'POPostalCode';
$headerRow[] = 'POCountry';
$headerRow[] = 'InvoiceNumber';
$headerRow[] = 'Reference';
$headerRow[] = 'InvoiceDate';
$headerRow[] = 'DueDate';
$headerRow[] = 'Currency';
//
// WRITE ROW
//
$export->writeLine($headerRow);
//
// PROCESS ORDERS
//
function compileContentLine(&$static, &$variable) {
// set dynamic key values
$contentRow[InventoryItemCode] = $variable[InventoryItemCode];
$contentRow[Description] = $variable[Description];
$contentRow[Quantity] = $variable[Quantity];
$contentRow[UnitAmount] = $variable[UnitAmount];
$contentRow[AccountCode] = $variable[AccountCode];
$contentRow[TaxType] = $variable[TaxType];
// set static key values
$contentRow[ContactName] = $static[ContactName];
$contentRow[EmailAddress] = $static[EmailAddress];
$contentRow[POAddressLine1] = $static[POAddressLine1];
$contentRow[POAddressLine2] = $static[POAddressLine2];
$contentRow[POAddressLine3] = $static[POAddressLine3];
$contentRow[POAddressLine4] = $static[POAddressLine4];
$contentRow[POCity] = $static[POCity];
$contentRow[POPostalCode] = $static[POPostalCode];
$contentRow[POCountry] = $static[POCountry];
$contentRow[InvoiceNumber] = $static[InvoiceNumber];
$contentRow[Reference] = $static[Reference];
$contentRow[InvoiceDate] = $static[InvoiceDate];
$contentRow[DueDate] = $static[DueDate];
$contentRow[Currency] = $static[Currency];
return $contentRow;
}
function checkIfInternational($ShippingName) {
if (strpos($ShippingName, 'International Courier') !== false) {
$variable[AccountCode] = 260;
$variable[TaxType] = 'Tax Exempt';
} else {
$variable[AccountCode] = 200;
$variable[TaxType] = 'Tax on Sales';
}
return $variable;
}
foreach ($this->orders as $orderRow) {
// skip if order is "processed"
if ($orderRow->order_status === 'processed') {
continue;
} else {
//
// STATIC VALUES
//
$static[ContactName] = $orderRow->billing_address_firstname . ' ' . $orderRow->billing_address_lastname;
$static[EmailAddress] = $orderRow->user_email;
$static[POAddressLine1] = $orderRow->billing_address_street;
$static[POAddressLine2] = $orderRow->billing_address_state . ', ' . $orderRow->billing_address_post_code . ', ' . $orderRow->billing_address_country;
$static[POAddressLine3] = ' ~ SHIP TO: ' . strtoupper($orderRow->shipping_address_firstname) . ' ' . strtoupper($orderRow->shipping_address_lastname) . ', ' . $orderRow->shipping_address_telephone . ($orderRow->shipping_address_telephone2 == NULL ? '' : ' OR ' . $orderRow->shipping_address_telephone2);
$static[POAddressLine4] = strtoupper($orderRow->shipping_address_street);
$static[POCity] = $orderRow->shipping_address_state;
$static[POPostalCode] = $orderRow->shipping_address_post_code;
$static[POCountry] = $orderRow->shipping_address_country;
$static[InvoiceNumber] = 'OIK' . $orderRow->order_number;
// prepare order weight for reference
$totalProducts = count($orderRow->products);
$totalWeight = 0;
for ($i = 1; $i <= $totalProducts; $i++) {
$orderProduct = $orderRow->products[$i - 1];
$hikaProductClass = hikashop_get('class.product'); // get hikashop product class
$hikaProduct = $hikaProductClass->get($orderProduct->product_id);
$totalWeight += $hikaProduct->product_weight * $orderProduct->order_product_quantity;
}
// prepare shipping class object and courier service required for reference
$hikaShippingClass = hikashop_get('class.shipping');
$orderShipping = $hikaShippingClass->get($orderRow->order_shipping_id);
if (strpos($orderShipping->shipping_name, 'Courier (L') !== false) {
if ($totalWeight < 1) {
$serviceRequired = '| Service: LOF | ';
} else {
$serviceRequired = '| Service: LOX | ';
}
} elseif (strpos($orderShipping->shipping_name, 'International Courier') !== false) {
$serviceRequired = '| Aramex waybill no. 00000000000 | ';
} else {
if ($totalWeight < 2) {
$serviceRequired = '| Service: OVN | ';
} elseif ($totalWeight < 6) {
$serviceRequired = '| Service: AIR | ';
} elseif ($totalWeight >= 6) {
$serviceRequired = '| Service: ECO | ';
} else {
$serviceRequired = '| ';
}
}
$orderNotes = trim($orderRow->notes);
$orderNotes = ($orderNotes !== '') ? ' ' . $orderNotes : $orderNotes;
$static[Reference] = $serviceRequired . 'Mass: ' . round($totalWeight, 2) . 'KG | Pieces: 1' . ' | Reference: order:0' . $orderRow->order_id . ' - ' . $orderRow->user_email . ' | Parcel Description: Educational Text books | Special Instructions: Please phone receiver before delivering.' . $orderNotes . ' |';
// prepare order date format
if (!empty($orderRow->order_created)) {
$order_created_date = hikashop_getDate($orderRow->order_created, '%Y-%m-%d'); // Assign Hikashop getDate function result to variable
$order_created_date = new DateTime($order_created_date); // Change variable to DateTime object
$order_created_date = $order_created_date->format('d M Y'); // Change output format of datetime object to 01 Mon YYYY
$orderRow->order_created = $order_created_date; // Assign this date to $orderRow array
}
if (!empty($orderRow->order_modified)) {
$order_modified_date = hikashop_getDate($orderRow->order_modified, '%Y-%m-%d'); // Assign Hikashop getDate function result to variable
$order_modified_date = new DateTime($order_modified_date); // Change variable to DateTime object
$order_modified_date = $order_modified_date->format('d M Y'); // Change output format of datetime object to 01 Mon YYYY
$orderRow->order_modified = $order_modified_date; // Assign this date to $orderRow array
}
$static[InvoiceDate] = $orderRow->order_created;
$static[DueDate] = $orderRow->order_modified;
$static[Currency] = 'ZAR';
//
// PRODUCT ROWS
//
$TaxType = checkIfInternational($orderShipping->shipping_name);
for ($i = 1; $i <= $totalProducts; $i++) {
// prepare dynamic key values
$orderProduct = $orderRow->products[$i - 1];
$variable[InventoryItemCode] = $orderProduct->order_product_code;
$variable[Description] = $orderProduct->order_product_name;
$variable[Quantity] = $orderProduct->order_product_quantity;
$variable[UnitAmount] = $orderProduct->order_product_price + $orderProduct->order_product_tax;
// Check if children at risk
if ($orderProduct->order_product_code === 'R10_donation_to_children_at_risk') {
$variable[AccountCode] = 250;
$variable[TaxType] = 'Tax Exempt';
} else {
$variable[AccountCode] = $TaxType[AccountCode];
$variable[TaxType] = $TaxType[TaxType];
}
$export->writeLine(compileContentLine($static, $variable));
}
//
// SHIPPING ROW
//
// prepare dynamic key values
$variable[InventoryItemCode] = 'Shipping - ' . $orderShipping->shipping_name;
$variable[Description] = $orderShipping->shipping_name;
$variable[Quantity] = 1;
$variable[UnitAmount] = $orderRow->order_shipping_price;
// Check if international
$variable[AccountCode] = $TaxType[AccountCode];
$variable[TaxType] = $TaxType[TaxType];
$export->writeLine(compileContentLine($static, $variable));
//
// ADMIN FEE ROW
//
// prepare dynamic key values
$variable[InventoryItemCode] = 'ADMIN-FEE';
$variable[Description] = 'Admin fee';
$variable[Quantity] = 1;
$variable[UnitAmount] = $orderRow->order_payment_price;
// Check if international
$variable[AccountCode] = $TaxType[AccountCode];
$variable[TaxType] = $TaxType[TaxType];
$export->writeLine(compileContentLine($static, $variable));
}
}
}
$export->send();
exit;
Last edit: 11 years 5 months ago by GSW00d.
The following user(s) said Thank You: Jerome
Please Log in or Create an account to join the conversation.
7 years 7 months ago - 7 years 7 months ago #303400
by twhcreations
Replied by twhcreations on topic Hikashop Orders to Xero Invoices - My Working PHP Code
Bruce,
Did you get this to work and export then import a bunch of invoices into Xero?
Did you get this to work and export then import a bunch of invoices into Xero?
Last edit: 7 years 7 months ago by twhcreations.
Please Log in or Create an account to join the conversation.
Time to create page: 0.172 seconds