This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/export/classes/export/quicken.php
2011-05-03 09:49:04 +10:00

199 lines
6.2 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities for Quickbooks.
*
* @package OSB
* @subpackage Export/Quicken
* @category Controllers/Admin
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Export_Quicken extends Export {
public function export() {
if (! empty($_POST['payment_id']) AND count($_POST['payment_id'])) {
$qo = new Quicken;
foreach ($_POST['payment_id'] as $pid) {
$mpo = ORM::factory('payment',$pid);
if ($mpo->loaded()) {
$invoice_ids = array();
foreach ($mpo->payment_item->find_all() as $pio) {
// If our invoice ID is not blank, then the payment was applied to an invoice
if ($pio->invoice->id) {
// Record our invoice IDs for the summary
array_push($invoice_ids,$pio->invoice->id);
$qio = new Quicken_Invoice;
$qio->TRNSID = sprintf('%06s',$pio->invoice->id);
$qio->DATE = date('m/d/Y',$pio->invoice->date_orig);
$qio->MEMO = 'Import from OSB';
$qio->CLEAR = 'N';
$qio->TOPRINT = 'N';
$qio->PAID = 'N';
$qio->ADDR1 = $mpo->account->address1;
$qio->ADDR2 = $mpo->account->address2;
$qio->ADDR3 = sprintf('%s, %s %s',$mpo->account->city,$mpo->account->state,$mpo->account->zip);
// @todo - should be configurable
$qio->TERMS = '7 Days';
// @todo - should be configurable
$qio->INVTITLE = 'Graytech Hosting Invoice';
// @todo - should be configurable
$qio->INVMEMO = 'Thank you for using Graytech Hosting';
$qio->DOCNUM = sprintf('%06s',$pio->invoice->id);
$qio->DUEDATE = date('m/d/Y',$pio->invoice->due_date);
$qio->AMOUNT = sprintf('%3.2f',$pio->invoice->total_amt);
if ($mpo->account->company)
$qio->NAME = $mpo->account->company;
else
$qio->NAME = sprintf('%s %s',$mpo->account->last_name,$mpo->account->first_name);
// Other Quicken fields not used.
#$qio->CLASS = '';
#$qio->SHIPVIA = '';
#$qio->SHIPDATE = '';
#$qio->OTHER1 = '';
#$qio->REP = '';
#$qio->FOB = '';
#$qio->PONUM = '';
#$qio->SADDR1 = '';
#$qio->SADDR2 = '';
#$qio->SADDR3 = '';
#$qio->SADDR4 = '';
#$qio->SADDR5 = '';
// Add the items to the invoice
foreach ($pio->invoice->invoice_item->find_all() as $iio) {
$qto = new Quicken_InvoiceItem;
if ($iio->date_start OR $iio->date_stop)
$daterange = sprintf('%s-%s',date('d-m-Y',$iio->date_start),date('d-m-Y',$iio->date_stop));
elseif ($iio->product_attr && preg_match('/^a/',$iio->product_attr)) {
echo 'Uncaptured';die();
} elseif ($iio->product_attr && preg_match('/^s/',$iio->product_attr))
$daterange = preg_replace("/\r?\n/",' ',unserialize($iio->product_attr));
else
$daterange = '';
if (! $iio->product_id && preg_match('/^DOMAIN/',$iio->sku)) {
$qto->ACCNT = 'Internet:Domain Name';
$qto->INVITEM = sprintf('Domain:%s',
($iio->domain_tld) ? strtoupper($iio->domain_tld) : 'Unknown');
$qto->MEMO = sprintf('Domain: %s.%s (%s)',
strtoupper($iio->domain_name),strtoupper($iio->domain_tld),$daterange);
} elseif ($iio->product_id) {
$module = ORM::factory('module',array('name'=>'product'));
$export = ORM::factory('export','module')
->where('plugin_name','=',$this->plugin)
->and_where('module_id','=',$module->id)
->and_where('item_id','=',$iio->product_id)
->find();
if ($export->loaded()) {
$map_data = unserialize($export->map_data);
$qto->ACCNT = $map_data['account'];
$qto->INVITEM = $map_data['item'];
} else {
$qto->ACCNT = 'Other Income';
$qto->INVITEM = 'Product:Unknown';
}
$qto->MEMO = sprintf('%s (%s)',
$iio->product->product_translate->find()->name,$daterange);
} else {
$qto->ACCNT = 'Other Income';
$qto->INVITEM = 'Unknown';
$qto->MEMO = sprintf('%s (%s)',
$iio->product->product_translate->find()->name,$daterange);
}
$qto->CLEAR = 'N';
$qto->QNTY = -1;
if ($pio->invoice->tax_amt) {
$qto->TAXABLE = 'Y';
# @todo, get this from OSB
$qto->TAXCODE = 'GST';
$qto->TAXRATE = sprintf('%3.2f%%','0.10');
$qto->TAXAMOUNT = sprintf('%3.2f',$iio->tax_amt*-1);
} else {
$qto->TAXAMOUNT = 0;
}
$qto->PRICE = sprintf('%3.2f',$iio->total_amt-$iio->tax_amt);
$qto->AMOUNT = sprintf('%3.2f',($iio->total_amt-$iio->tax_amt)*-1);
$qio->addInvoiceItem($qto);
}
$qo->addInvoice($qio);
}
}
$qpo = new Quicken_Payment;
$qpo->AMOUNT = sprintf('%3.2f',$mpo->total_amt);
$qpo->TRNSID = sprintf('P%06s',$mpo->id);
$qpo->DATE = date('m/d/Y',$mpo->date_payment);
// @todo this should be from a function - when no invoice is paid we cant use $qio
if ($mpo->account->company)
$qpo->NAME = $mpo->account->company;
else
$qpo->NAME = sprintf('%s %s',$mpo->account->last_name,$mpo->account->first_name);
$qpo->CLEAR = 'N';
$qpo->MEMO = sprintf('Payment for invoice(s) %s (%s)',implode(':',$invoice_ids),$mpo->checkout->name);
// @todo Accounts/Payment should be configurable
switch ($mpo->checkout->checkout_plugin) {
// @todo this is direct debit
case 'MANUAL':
$qpo->PAYMETH = 'DirectDebit';
$qpo->ACCNT = 'Ezypay';
break;
case 'REMIT_CHECK':
$qpo->PAYMETH = 'Cheque';
$qpo->ACCNT = 'Undeposited Funds';
break;
case 'REMIT_BANK_WIRE':
$qpo->PAYMETH = 'DirectCredit';
$qpo->ACCNT = 'Bendigo Bank';
break;
case 'PAYPAL':
$qpo->PAYMETH = 'Paypal';
$qpo->ACCNT = 'Paypal';
break;
default:
$qpo->PAYMETH = 'TBA';
$qpo->ACCNT = 'Undeposited Funds';
}
$qio->addPayment($qpo);
}
}
}
if (! empty($qo))
$this->request->response = $qo->export();
$this->request->send_file(TRUE,'quicken-import.iif');
}
}
?>