<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides Quicken exporting capabilities.
 *
 * @package    OSB
 * @subpackage Export
 * @category   Classes/Quicken
 * @author     Deon George
 * @copyright  (c) 2010 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Quicken extends OSBExport {
	protected $defaults = array();

	protected function keys() {
		return array_merge(array_keys($this->defaults),parent::keys());
	}

	protected function vals() {
		return array_merge(array_values($this->defaults),parent::vals());
	}

	public function addInvoice(Quicken_Invoice $item) {
		array_push($this->_items,$item);
	}

	public function export() {
		$export = '';

		foreach ($this->_items as $inv) {
			# Invoices
			$export .= "!TRNS\t";
			$export .= implode("\t",$inv->keys())."\n";
			$export .= "TRNS\t";
			$export .= implode("\t",$inv->vals())."\n";

			# Invoice Items
			$spl = 0;
			foreach ($inv->_items as $invitem) {
				if (! $spl) {
					$export .= "!SPL\tSPLID\t";
					$export .= implode("\t",$invitem->keys())."\n";
				}

				$export .= sprintf("SPL\t%s\t%s",$spl++,implode("\t",$invitem->vals()))."\n";
			}

			$export .= "ENDTRNS\n";

			# Payments
			foreach ($inv->_payments as $payitem) {
				$export .= "!TRNS\t";
				$export .= implode("\t",$payitem->keys())."\n";
				$export .= "TRNS\t";
				$export .= implode("\t",$payitem->vals())."\n";

				$export .= sprintf("!SPL\t%s\t%s\t%s\t%s\t%s\t",'SPLID','TRANSTYPE','CLEAR','ACCNT','AMOUNT')."\n";
				$export .= sprintf("SPL\t%s\t%s\t%s\t%s\t%s\t",0,'PAYMENT','N','Accounts Receivable',$payitem->AMOUNT)."\n";
				$export .= "ENDTRNS\n";
			}
		}

		return $export;
	}
}
?>