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

219 lines
6.8 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?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['id']) AND count($_POST['id'])) {
2010-11-29 22:41:08 +00:00
$qo = new Quicken;
foreach ($_POST['id'] as $pid) {
2012-11-09 23:13:57 +00:00
$po = ORM::factory('Payment',$pid);
2010-11-29 22:41:08 +00:00
if ($po->loaded()) {
2010-11-29 22:41:08 +00:00
$invoice_ids = array();
foreach ($po->payment_item->find_all() as $pio) {
2010-11-29 22:41:08 +00:00
// 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 = $po->account->address1;
$qio->ADDR2 = $po->account->address2;
$qio->ADDR3 = sprintf('%s, %s %s',$po->account->city,$po->account->state,$po->account->zip);
2010-11-29 22:41:08 +00:00
// @todo - should be configurable
$qio->TERMS = '7 Days';
// @todo - should be configurable
$qio->INVTITLE = Company::instance()->name().' Invoice';
2010-11-29 22:41:08 +00:00
// @todo - should be configurable
$qio->INVMEMO = 'Thank you for using '.Company::instance()->name();
2010-11-29 22:41:08 +00:00
$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());
2010-11-29 22:41:08 +00:00
if ($po->account->company)
$qio->NAME = $po->account->company;
2010-11-29 22:41:08 +00:00
else
$qio->NAME = sprintf('%s %s',$po->account->last_name,$po->account->first_name);
2010-11-29 22:41:08 +00:00
// 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->period())
$daterange = $iio->period();
2010-11-29 22:41:08 +00:00
// @todo This should go.
2010-11-29 22:41:08 +00:00
elseif ($iio->product_attr && preg_match('/^a/',$iio->product_attr)) {
echo 'Uncaptured';die();
// @todo This should go.
2010-11-29 22:41:08 +00:00
} 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) {
2012-11-09 23:13:57 +00:00
$mo = ORM::factory('Module',array('name'=>'product'));
$eo = ORM::factory('Export')
->where('plugin_name','=',strtolower($this->plugin))
->and_where('module_id','=',$mo->id)
2010-11-29 22:41:08 +00:00
->and_where('item_id','=',$iio->product_id)
->find();
if ($eo->loaded()) {
2012-08-01 12:34:59 +00:00
$qto->ACCNT = $eo->map_data['account'];
$qto->INVITEM = $eo->map_data['item'];
2010-11-29 22:41:08 +00:00
} else {
throw new Kohana_Exception('Missing product map data for :product (:id)',
array(':product'=>$iio->product->name(),':id'=>$iio->product_id));
2010-11-29 22:41:08 +00:00
$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)',
2012-08-01 12:34:59 +00:00
$iio->product->product_translate->find()->name,$daterange);
2010-11-29 22:41:08 +00:00
}
$qto->CLEAR = 'N';
$qto->QNTY = -1;
if ($pio->invoice->tax()) {
2010-11-29 22:41:08 +00:00
$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()*-1);
2010-11-29 22:41:08 +00:00
} else {
$qto->TAXAMOUNT = 0;
}
// @todo This rounding should be a system config.
$qto->PRICE = sprintf('%3.2f',round($iio->subtotal()-$iio->discount(),2));
$qto->AMOUNT = sprintf('%3.2f',round($iio->subtotal()-$iio->discount(),2)*-1);
2010-11-29 22:41:08 +00:00
$qio->addInvoiceItem($qto);
}
2012-08-01 12:34:59 +00:00
// Add credits as a other item
$qto = new Quicken_InvoiceItem;
$qto->ACCNT = 'Other Income';
$qto->INVITEM = 'Product:Unknown';
$qto->CLEAR = 'N';
$qto->QNTY = 1;
$qto->MEMO = 'Credit Item';
if ($pio->invoice->tax()) {
$qto->TAXABLE = 'Y';
# @todo, get this from OSB
$qto->TAXCODE = 'GST';
$qto->TAXRATE = sprintf('%3.2f%%','0.10');
2013-02-26 03:19:32 +00:00
$tax = round($pio->invoice->total_credits()/11,2);
2012-08-01 12:34:59 +00:00
$qto->TAXAMOUNT = sprintf('%3.2f',$tax);
} else {
$qto->TAXAMOUNT = 0;
}
2013-02-26 03:19:32 +00:00
$qto->PRICE = sprintf('%3.2f',round(($pio->invoice->total_credits()-$tax)*-1,2));
$qto->AMOUNT = sprintf('%3.2f',round(($pio->invoice->total_credits()-$tax),2));
2012-08-01 12:34:59 +00:00
$qio->addInvoiceItem($qto);
2010-11-29 22:41:08 +00:00
$qo->addInvoice($qio);
}
}
$qpo = new Quicken_Payment;
$qpo->AMOUNT = sprintf('%3.2f',$po->total_amt);
$qpo->TRNSID = sprintf('P%06s',$po->id);
$qpo->DATE = date('m/d/Y',$po->date_payment);
2010-11-29 22:41:08 +00:00
// @todo this should be from a function - when no invoice is paid we cant use $qio
if ($po->account->company)
$qpo->NAME = $po->account->company;
2010-11-29 22:41:08 +00:00
else
$qpo->NAME = sprintf('%s %s',$po->account->last_name,$po->account->first_name);
2010-11-29 22:41:08 +00:00
$qpo->CLEAR = 'N';
$qpo->MEMO = sprintf('Payment for invoice(s) %s (%s)',implode(':',$invoice_ids),$po->checkout->name);
2010-11-29 22:41:08 +00:00
// @todo Accounts/Payment should be configurable
switch ($po->checkout->checkout_plugin) {
2010-11-29 22:41:08 +00:00
// @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';
}
if (isset($qio))
$qio->addPayment($qpo);
2010-11-29 22:41:08 +00:00
}
}
}
if (! empty($qo))
$this->response->body($qo->export());
2010-11-29 22:41:08 +00:00
$this->response->send_file(TRUE,'quicken-import.iif',array('mime_type'=>'text/plain'));
2010-11-29 22:41:08 +00:00
}
}
?>