Overhauled export, and other minor updates

This commit is contained in:
Deon George 2013-05-14 23:53:04 +10:00
parent 81cb759667
commit c56742d127
33 changed files with 690 additions and 652 deletions

View File

@ -65,7 +65,7 @@ class Config extends Kohana_Config {
}
public static function date($date) {
return date(Company::instance()->date_format(),($date ? $date : time()));
return is_null($date) ? NULL : date(Company::instance()->date_format(),$date);
}
/**

View File

@ -10,6 +10,9 @@
* @license http://dev.osbill.net/license.html
*/
class Model_Module_Method_Token extends ORM_OSB {
// This module doesnt keep track of column updates automatically
protected $_updated_column = FALSE;
// Relationships
protected $_belongs_to = array(
'account'=>array(),
@ -19,9 +22,6 @@ class Model_Module_Method_Token extends ORM_OSB {
'record_id'=>array(),
);
// This module doesnt keep track of column updates automatically
protected $_update_column = FALSE;
public function method(array $modmeth) {
list($module,$method) = $modmeth;

View File

@ -115,6 +115,10 @@ abstract class ORM extends Kohana_ORM {
return $value;
}
public function display_filters(array $filters) {
$this->_display_filters = Arr::merge($this->_display_filters,$filters);
}
/**
* Function help to find records that are active
*/

View File

@ -171,6 +171,10 @@ abstract class ORM_OSB extends ORM {
return array_key_exists($key,$this->$column) ? $this->{$column}[$key] : NULL;
}
final public function module() {
return ORM::factory(Kohana::classname($this->name));
}
final public function mid() {
return ORM::factory('Module',array('name'=>$this->_table_name));
}

View File

@ -1,91 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is class renders responses and forms based on the values from a module.
*
* @package OSB
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class StaticList_Module extends StaticList {
protected static $record = array();
protected function _table() {
}
/**
* Display a static name for a value
*/
public static function display($id) {
// Override our argument list as defined in parent
list($table,$key,$skey,$value) = func_get_args();
$db = DB::select($key)->from($table)->where($skey,'=',$value)->execute();
if ($db->count() !== 1)
return sprintf('No Value (%s)',$value);
else
return $db->get($key);
}
/**
* This function is to return the cached value of the current active record
* This is so that a follow up call to get an attribute of a value retrieved
* can reuse the active record values.
* This gets over a limitation where the query to form() to get a default
* no longer exists (or is invalid) and you want other attributes of the
* remaining active record, which may not be the default record.
*/
public static function record($table,$attribute,$skey,$value) {
if (empty(static::$record[$table]))
return static::display($table,$attribute,$skey,$value);
else
return static::$record[$table][$attribute];
}
/**
* Renders form input
*/
public static function form($name,$default='',$addblank=FALSE) {
// Override our argument list as defined in parent
list($name,$table,$default,$key,$value,$where,$addblank,$attributes) = func_get_args();
$db = DB::select()->from($table);
foreach ($where as $k=>$v) {
list ($op,$v) = explode(':',$v);
$db->where($k,$op,$v);
}
$db = $db->execute();
// If we only have one record, dont make a select list
if ($db->count() == 1) {
static::$record[$table] = $db->as_array();
static::$record[$table] = array_shift(static::$record[$table]);
return Form::hidden($name,$db->get($key)).$db->get($value);
}
// Else we return a select list
$x = array();
if ($addblank)
$x[] = '';
foreach ($db as $record) {
$x[$record[$key]] = $record[$value];
// Save our static record, in case we reference this item again.
if ($record[$key] == $default)
static::$record[$table] = $record;
}
return Form::select($name,$x,$default,$attributes);
}
public static function get($value) {
return static::factory()->_get($value);
}
}
?>

View File

@ -1,5 +0,0 @@
<h1>Oops!</h1>
<h2>403 Not Found or Not Authorised?</h2>
<div class="error-details">
Sorry, either the item doesnt exist, or you are not authorised to see it.
</div>

View File

@ -1,5 +0,0 @@
<h1>Oops!</h1>
<h2>404 Not Found?</h2>
<div class="error-details">
Sorry, an error has occured, requested page not found?
</div>

View File

@ -1,6 +0,0 @@
<h1>Oops!</h1>
<h2>501 Bother, something went wrong.</h2>
<?php echo $message; ?>
<div class="error-details">
If this keeps happening, please let us know.
</div>

View File

@ -57,7 +57,7 @@
<tr>
<td>Country</td>
<!-- @todo - our default currency should be defined in a config -->
<td><?php echo StaticList_Module::form('country_id','country',61,'id','name',array());?></td>
<td><?php echo Form::select('country_id',ORM::factory('Country')->list_select(),$o->country_id,array('label'=>'Country','required')); ?></td>
<!--
{if $VAR.account_country_id != ''}
{$list->menu('no','account_country_id','country','name',$VAR.account_country_id,'form_field" onchange="taxIdsDisplay(this.value)',true)}

View File

@ -26,7 +26,7 @@
</tr>
<tr>
<td class="head">Country</td>
<td><?php echo StaticList_Module::form('country_id','country',61,'id','name',array(),NULL,array('class'=>'form_button'));?></td>
<td><?php echo Form::select('country_id',ORM::factory('Country')->list_select(),$o->country_id,array('label'=>'Country','required')); ?></td>
</tr>
<tr>
<td class="spacer" colspan="2">&nbsp;</td>

View File

@ -9,51 +9,123 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Admin_Export extends Controller_TemplateDefault_Admin {
protected $control_title = 'Export';
class Controller_Admin_Export extends Controller_Export {
protected $secure_actions = array(
'add'=>TRUE,
'edit'=>TRUE,
'index'=>TRUE,
'module'=>TRUE,
);
/**
* Add Export Maping items
*/
public function action_add() {
$eo = ORM::factory('Export');
$output = '';
if (empty($_POST['export_module_id']) OR empty($_POST['module_id']))
HTTP::redirect(URL::link('admin','export/index'));
if ($_POST AND $eo->values($_POST)->check()) {
$eo->module_id = ORM::factory('Module',array('name'=>'product'))->id; // @todo This probably should be in the form.
$eo->plugin_name = 'quicken'; // @todo This should be in the form.
// Entry updated
if (! $eo->save())
throw new Kohana_Exception('Unable to save data :post',array(':post'=>serialize($_POST)));
$edo = ORM::factory('Export_DataMap');
SystemMessage::add(array(
'title'=>_('Record add'),
'type'=>'info',
'body'=>_('Export Map entry added.')
));
if ($_POST AND isset($_POST['item_id']) AND $edo->values($_POST)->check()) {
$edo->status = 1;
if (! $edo->save())
throw HTTP_Exception::factory(501,'Unable to save data :post',array(':post'=>serialize($_POST)));
SystemMessage::factory()
->title('Record added')
->type('success')
->body(_('Export DataMap record has been added.'));
}
$output .= Form::open();
$output .= View::factory($this->viewpath())
->set('eo',$eo);
$output .= '<div>'.Form::submit('submit',_('Add'),array('class'=>'form_button')).'</div>';
$output .= Form::close();
Block::add(array(
'title'=>_('Add Export Map'),
'body'=>$output,
));
Block::factory()
->title('Add Export Item Map')
->title_icon('icon-plus-sign')
->type('form-horizontal')
->body(View::factory('export/admin/add')
->set('o',$edo)
->set('emo',ORM::factory('Export_Module',$_POST['export_module_id']))
->set('module',ORM::factory('Module',$_POST['module_id'])->module()));
}
/**
* Edit Export Maping items
* Select our primary export target
*/
public function action_edit() {
public function action_index() {
$output = '';
if ($_POST and isset($_POST['eid'])) {
$select = array();
foreach (ORM::factory('Export_Module')->where('export_id','=',$_POST['eid'])->find_all() as $emo)
$select[$emo->id] = $emo->module->name;
$output .= Form::open(URL::link('admin','export/add'));
$output .= Form::select('export_module_id',$select);
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
// @todo This shouldnt be hard coded.
$output .= Form::hidden('module_id',ORM::factory('Product')->mid());
} else {
$output .= Form::open();
$output .= Form::select('eid',ORM::factory('Export')->list_select());
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
}
$output .= Form::close();
Block::factory()
->title('Select Export Target')
->title_icon('icon-share')
->body($output);
}
/**
* Update the export module settings
*/
public function action_module() {
if ($_POST AND isset($_POST['export_module_id'])) {
$emo = ORM::factory('Export_Module',$_POST['export_module_id']);
if ($emo->loaded()) {
$emo->values($_POST);
$emo->save();
if ($emo->saved())
SystemMessage::factory()
->title('Record updated')
->type('success')
->body(_('Export Module record has been updated.'));
}
}
if ($x = $this->request->param('id')) {
$emo = ORM::factory('Export_Module',$x);
if ($emo->loaded())
Block::factory()
->title(sprintf('Export Module: %s for %s',$emo->module->display('name'),$emo->export->display('name')))
->title_icon('icon-wrench')
->type('form-horizontal')
->body(View::factory('export/module/admin/edit')->set('o',$emo));
} else {
Block::factory()
->title('Export Module Update')
->title_icon('icon-th-list')
->body(Table::factory()
->data(ORM::factory('Export_Module')->find_all())
->jssort(TRUE)
->columns(array(
'id'=>'ID',
'export->name'=>'Name',
'module->name'=>'Module'
))
->prepend(array(
'id'=>array('url'=>URL::link('admin','export/module/')),
))
);
}
}
}
?>

View File

@ -1,87 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities.
*
* @package Export
* @category Controllers/Affiliate
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Affiliate_Export extends Controller_TemplateDefault_Affiliate {
protected $control_title = 'Export';
protected $secure_actions = array(
'index'=>TRUE,
'export'=>TRUE,
);
/**
* Export plugins must define an export action.
*/
public function action_export() {
if (empty($_POST['plugin']))
$this->request->redirect('affiliate/export/index');
$sc = Kohana::classname('Export_'.$_POST['plugin']);
if (! class_exists($sc))
throw new Kohana_Exception('Export Class doesnt exist for :plugin',array(':plugin'=>$_POST['plugin']));
else
$export = new $sc;
// @todo: Need to limit this to affiliate acounts
$export->export();
}
/**
* This is the main call to export, providing a list of items to export and
* setting up the page to call the export plugin when submitted.
*/
public function action_index() {
// @todo this should come from a file list
$TBRexportplugins = array('quicken'=>'Export to Quicken');
// @todo: Need to limit this to affiliate acounts
$p = ORM::factory('Payment');
if ($p->find_all()->count()) {
Block::add(array(
'title'=>_('Payments to Export'),
'body'=>Table::display(
$p->find_all(),
25,
array(
'id'=>array('label'=>'ID'),
'date_payment'=>array('label'=>'Date'),
'checkout->display("name")'=>array('label'=>'Method'),
'account->accnum()'=>array('label'=>'Acc Num'),
'account->name()'=>array('label'=>'Account'),
'total_amt'=>array('label'=>'Total','class'=>'right'),
'balance(TRUE)'=>array('label'=>'Balance','class'=>'right'),
'invoicelist()'=>array('label'=>'Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>'affiliate/export/export',
'hidden'=>array(
Form::hidden('plugin','quicken'),
),
'button'=>array(
Form::submit('submit',_('Export'),array('class'=>'form_button')),
),
)),
));
# Nothing to export
} else {
SystemMessage::add(array(
'title'=>_('No payments to export'),
'type'=>'info',
'body'=>sprintf(_('There are no payments within the last %s days (since %s) to show.'),
$daysago,date(Kohana::$config->load('osb')->date_format,$daysago*86400+time())),
));
}
}
}
?>

View File

@ -0,0 +1,104 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities.
*
* @package Export
* @category Controllers/Reseller
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Reseller_Export extends Controller_Export {
protected $secure_actions = array(
'export'=>TRUE,
'index'=>TRUE,
'list'=>TRUE,
);
/**
* Export plugins must define an export action.
*/
public function action_export() {
$this->auto_render = FALSE;
if (empty($_POST['export_module_id']) OR ! ($emo = ORM::factory('Export_Module',$_POST['export_module_id'])) OR ! $emo->loaded())
HTTP::redirect(URL::link('reseller','export/index'));
$emo->export->plugin($emo)->export($this->response);
}
/**
* Select our primary export target
*/
public function action_index() {
$output = '';
$output .= Form::open(URL::link('reseller','export/list'));
$output .= Form::select('eid',ORM::factory('Export')->list_select());
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary'));
$output .= Form::close();
Block::factory()
->title('Select Export Target')
->title_icon('icon-share')
->body($output);
}
/**
* This is the main call to export, providing a list of items to export and
* setting up the page to call the export plugin when submitted.
*/
public function action_list() {
if (empty($_POST['eid']))
HTTP::redirect(URL::link('reseller','export/index'));
$eo = ORM::factory('Export',$_POST['eid']);
if (! $eo->loaded())
HTTP::redirect(URL::link('reseller','export/index'));
$c = 0;
$output = '<div class="tabbable span11">';
$output .= '<ul class="nav nav-tabs">';
// @todo To limit to Reseller accounts only
foreach ($eo->export_module->find_all() as $emo)
$output .= sprintf('<li class="%s"><a href="#tab%s" data-toggle="tab">%s</a></li>',$c++ ? '' : 'active',$emo->id,ucfirst($emo->module->module()->object_name()));
$output .= '</ul>';
$c = 0;
$output .= '<div class="tab-content">';
foreach ($eo->export_module->find_all() as $emo) {
$output .= sprintf('<div class="tab-pane %s" id="tab%s">',$c++ ? '' : 'active',$emo->id);
$output .= Table::factory()
->data($emo->list_export())
->jssort($emo->id)
->columns(Arr::merge(array(
'id'=>'ID',
'account->name(TRUE)'=>'Account',
'date_orig'=>'Date',
'status(TRUE)'=>'Active',
'total(TRUE)'=>'Total',
'exported'=>'Exported',
),$emo->display ? $emo->display : array()))
->select(URL::link('reseller','export/export'),$emo->id,array('export_module_id'=>$emo->id))
->prepend(array(
'id'=>array('checkbox'=>'id[]'),
))->filters(array(
'exported'=>array(array('Config::date',array(':value'))),
));
$output .= '</div>';
}
$output .= '</div>';
Block::factory()
->title('Export Transactions')
->title_icon('icon-share')
->body($output);
}
}
?>

View File

@ -1,21 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities for OSB.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Export {
// Name of export plugin.
protected $plugin;
public function __construct() {
$this->plugin = preg_replace('/^'.get_parent_class($this).'_/','',get_class($this));
$this->response = Response::factory();
}
}
?>

View File

@ -0,0 +1,19 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class enables common Export functions for Plugins
*
* @package Export
* @category Plugins
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
abstract class Export_Plugin {
protected $emo; // Our Export Object
public function __construct(Model_Export_Module $emo) {
$this->emo = $emo;
}
}
?>

View File

@ -0,0 +1,288 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities for Quickbooks.
*
* @package Export
* @category Plugins
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Export_Plugin_Quicken extends Export_Plugin {
public function export(Response $response) {
$output = '';
if (! isset($_POST['id']) OR ! $_POST['id'] OR ! $this->emo->loaded())
HTTP::redirect(URL::link('reseller','export/index'));
$o = ORM::factory('Module',$this->emo->module_id);
if (! $o->loaded())
HTTP::redirect(URL::link('reseller','export/index'));
switch ($o->name) {
case 'invoice': $output .= $this->invoices($_POST['id']);
break;
case 'payment': $output .= $this->payments($_POST['id']);
break;
default:
throw HTTP_Exception::factory(501,'Dont know how to export :name',array(':name'=>$o->name));
}
$response->body($output);
$response->send_file(TRUE,'quicken-import.iif',array('mime_type'=>'text/plain'));
}
/**
* Export an invoice
*/
private function invoice(Model_Invoice $io) {
$defaults = array(
'ACCNT'=>'Accounts Receivable',
'TRNSTYPE'=>'TAX_INVOICE',
'INVMEMO'=>'Thank you for using '.Company::instance()->name(),
'CLEAR'=>'N',
'TOPRINT'=>'N',
'PAID'=>'N',
'MEMO'=>'Import from OSB',
'INVTITLE'=>Company::instance()->name().' Invoice',
);
$invoice = $items = array();
$invoice['TRNSID'] = sprintf('%06s',$io->id);
$invoice['DATE'] = date('m/d/Y',$io->date_orig);
$invoice['ADDR1'] = $io->account->address1;
$invoice['ADDR2'] = $io->account->address2;
$invoice['ADDR3'] = sprintf('%s, %s %s',$io->account->city,$io->account->state,$io->account->zip);
// @todo - should be configurable
# $invoice['TERMS'] = '7 Days';
$invoice['DOCNUM'] = sprintf('%06s',$io->id);
$invoice['DUEDATE'] = date('m/d/Y',$io->due_date);
$invoice['AMOUNT'] = sprintf('%3.2f',$io->total());
$invoice['NAME'] = $io->account->company ? $io->account->company : sprintf('%s %s',$io->account->last_name,$io->account->first_name);
// Other Quicken fields not used.
#$invoice['CLASS'] = '';
#$invoice['SHIPVIA'] = '';
#$invoice['SHIPDATE'] = '';
#$invoice['OTHER1'] = '';
#$invoice['REP'] = '';
#$invoice['FOB'] = '';
#$invoice['PONUM'] = '';
#$invoice['SADDR1'] = '';
#$invoice['SADDR2'] = '';
#$invoice['SADDR3'] = '';
#$invoice['SADDR4'] = '';
#$invoice['SADDR5'] = '';
$c = 0;
// Add the items to the invoice
foreach ($io->invoice_item->find_all() as $iio) {
// Skip any zero amount items not relating to a service
if ($iio->total() == 0 and ! $iio->service_id)
continue;
// Get the mapping item for account purposes
if ($iio->product_id) {
$edo = ORM::factory('Export_DataMap')
->where('module_id','=',$iio->product->mid())
->and_where('item_id','=',$iio->product_id)
->find();
if ($edo->loaded()) {
$items[$c]['ACCNT'] = $edo->map_data['account'];
$items[$c]['INVITEM'] = $edo->map_data['item'];
} else {
throw HTTP_Exception::factory(501,'Missing product map data for :product (:id)',array(':product'=>$iio->product->title(),':id'=>$iio->product_id));
}
$items[$c]['MEMO'] = sprintf('%s (%s)',$iio->product->title(),$iio->period());
// Non product item
} else {
$items[$c]['ACCNT'] = 'Other Income';
$items[$c]['INVITEM'] = 'Unknown';
$items[$c]['MEMO'] = $iio->period();
}
$items[$c]['CLEAR'] = 'N';
$items[$c]['QNTY'] = -1;
if ($iio->tax_items()) {
// @todo, need to figure out how multiple tax items are handled
if (count($iio->tax_items()) > 1)
throw HTTP_Exception(501,'Export cant handle multiple tax items yet');
foreach ($iio->tax_items() as $tid => $amount) {
$to = ORM::factory('Tax',$tid);
$items[$c]['TAXABLE'] = 'Y';
$items[$c]['TAXCODE'] = $to->description;
$items[$c]['TAXRATE'] = sprintf('%3.2f%%',$to->rate);
$items[$c]['TAXAMOUNT'] = sprintf('%3.2f',$amount*-1);
}
} else {
$items[$c]['TAXAMOUNT'] = 0;
}
// @todo This rounding should be a system config.
$items[$c]['PRICE'] = sprintf('%3.2f',round($iio->subtotal()-$iio->discount(),2));
$items[$c]['AMOUNT'] = sprintf('%3.2f',round($iio->subtotal()-$iio->discount(),2)*-1);
$c++;
}
// Add credits as a other item
if ($io->total_credits()) {
$items[$c]['ACCNT'] = 'Other Income';
$items[$c]['INVITEM'] = 'Product:Unknown';
$items[$c]['CLEAR'] = 'N';
$items[$c]['QNTY'] = 1;
$items[$c]['MEMO'] = 'Credit Item';
$items[$c]['TAXAMOUNT'] = 0;
$items[$c]['PRICE'] = sprintf('%3.2f',round(($io->total_credits()-$io->tax())*-1,2));
$items[$c]['AMOUNT'] = sprintf('%3.2f',round(($io->total_credits()-$io->tax()),2));
}
return $this->output(Arr::merge($defaults,$invoice),$items);
}
/**
* Export selected invoices
*/
private function invoices(array $ids) {
$output = '';
foreach ($ids as $id)
$output .= $this->invoice(ORM::factory('Invoice',$id));
// If all went OK, update our export status
$this->update($ids);
return $output;
}
/**
* Return exported data for download
*/
private function output(array $trans,array $items) {
$output = '';
$output .= "!TRNS\t";
$output .= implode("\t",array_keys($trans))."\n";
$output .= "TRNS\t";
$output .= implode("\t",array_values($trans))."\n";
$spl = 0;
foreach ($items as $detail) {
if (! $spl) {
$output .= "!SPL\tSPLID\t";
$output .= implode("\t",array_keys($detail))."\n";
}
$output .= sprintf("SPL\t%s\t%s\n",$spl++,implode("\t",array_values($detail)));
}
$output .= "ENDTRNS\n";
return $output;
}
/**
* Export a payment
*/
private function payment(Model_Payment $po) {
$defaults = array(
'CLEAR'=>'N',
'TRNSTYPE'=>'PAYMENT',
);
$payment = $items = array();
$payment['AMOUNT'] = sprintf('%3.2f',$po->total_amt);
$payment['TRNSID'] = sprintf('P%06s',$po->id);
$payment['DATE'] = date('m/d/Y',$po->date_payment);
$payment['NAME'] = $po->account->company ? $po->account->company : sprintf('%s %s',$po->account->last_name,$po->account->first_name);
$payment['MEMO'] = sprintf('Payment for invoice(s) %s (%s)',implode(':',$po->invoices()),$po->checkout->name);
// @todo Accounts/Payment should be configurable
switch ($po->checkout->plugin) {
// @todo this is direct debit
case 'DD_EZYPAY':
$payment['PAYMETH'] = 'DirectDebit';
$payment['ACCNT'] = 'Ezypay';
break;
case 'REMIT_CHEQUE':
$payment['PAYMETH'] = 'Cheque';
$payment['ACCNT'] = 'Undeposited Funds';
break;
case 'REMIT_BANK_WIRE':
$payment['PAYMETH'] = 'DirectCredit';
$payment['ACCNT'] = 'Bendigo Bank';
break;
case 'PAYPAL_CART':
$payment['PAYMETH'] = 'Paypal';
$payment['ACCNT'] = 'Paypal';
break;
default:
$payment['PAYMETH'] = 'TBA';
$payment['ACCNT'] = 'Undeposited Funds';
}
$items[0]['TRANSTYPE'] = 'PAYMENT';
$items[0]['CLEAR'] = 'N';
$items[0]['ACCNT'] = 'Accounts Receivable';
$items[0]['AMOUNT'] = $po->total();
return $this->output(Arr::merge($defaults,$payment),$items);
}
/**
* Export selected invoices
*/
private function payments(array $ids) {
$output = '';
foreach ($ids as $id)
$output .= $this->payment(ORM::factory('Payment',$id));
// If all went OK, update our export status
$this->update($ids);
return $output;
}
private function update(array $ids) {
foreach ($ids as $id) {
// Check if we have exported this item already
$eio = ORM::factory('Export_Item')
->where('item_id','=',$id)
->and_where('export_module_id','=',$this->emo->id)
->find();
if (! $eio->loaded()) {
$eio->item_id = $id;
$eio->export_module_id = $this->emo->id;
}
$eio->save();
}
}
}
?>

View File

@ -1,217 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB exporting capabilities for Quickbooks.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 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'])) {
$qo = new Quicken;
foreach ($_POST['id'] as $pid) {
$po = ORM::factory('Payment',$pid);
if ($po->loaded()) {
$invoice_ids = array();
foreach ($po->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 = $po->account->address1;
$qio->ADDR2 = $po->account->address2;
$qio->ADDR3 = sprintf('%s, %s %s',$po->account->city,$po->account->state,$po->account->zip);
// @todo - should be configurable
$qio->TERMS = '7 Days';
// @todo - should be configurable
$qio->INVTITLE = Company::instance()->name().' Invoice';
// @todo - should be configurable
$qio->INVMEMO = 'Thank you for using '.Company::instance()->name();
$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());
if ($po->account->company)
$qio->NAME = $po->account->company;
else
$qio->NAME = sprintf('%s %s',$po->account->last_name,$po->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->period())
$daterange = $iio->period();
// @todo This should go.
elseif ($iio->product_attr && preg_match('/^a/',$iio->product_attr)) {
echo 'Uncaptured';die();
// @todo This should go.
} 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) {
$mo = ORM::factory('Module',array('name'=>'product'));
$eo = ORM::factory('Export')
->where('plugin_name','=',strtolower($this->plugin))
->and_where('module_id','=',$mo->id)
->and_where('item_id','=',$iio->product_id)
->find();
if ($eo->loaded()) {
$qto->ACCNT = $eo->map_data['account'];
$qto->INVITEM = $eo->map_data['item'];
} else {
throw new Kohana_Exception('Missing product map data for :product (:id)',
array(':product'=>$iio->product->title(),':id'=>$iio->product_id));
$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()) {
$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);
} 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);
$qio->addInvoiceItem($qto);
}
// 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');
$tax = round($pio->invoice->total_credits()/11,2);
$qto->TAXAMOUNT = sprintf('%3.2f',$tax);
} else {
$qto->TAXAMOUNT = 0;
}
$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));
$qio->addInvoiceItem($qto);
$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);
// @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;
else
$qpo->NAME = sprintf('%s %s',$po->account->last_name,$po->account->first_name);
$qpo->CLEAR = 'N';
$qpo->MEMO = sprintf('Payment for invoice(s) %s (%s)',implode(':',$invoice_ids),$po->checkout->name);
// @todo Accounts/Payment should be configurable
switch ($po->checkout->plugin) {
// @todo this is direct debit
case 'DD_EZYPAY':
$qpo->PAYMETH = 'DirectDebit';
$qpo->ACCNT = 'Ezypay';
break;
case 'REMIT_CHEQUE':
$qpo->PAYMETH = 'Cheque';
$qpo->ACCNT = 'Undeposited Funds';
break;
case 'REMIT_BANK_WIRE':
$qpo->PAYMETH = 'DirectCredit';
$qpo->ACCNT = 'Bendigo Bank';
break;
case 'PAYPAL_CART':
$qpo->PAYMETH = 'Paypal';
$qpo->ACCNT = 'Paypal';
break;
default:
$qpo->PAYMETH = 'TBA';
$qpo->ACCNT = 'Undeposited Funds';
}
if (isset($qio))
$qio->addPayment($qpo);
}
}
}
if (! empty($qo))
$this->response->body($qo->export());
$this->response->send_file(TRUE,'quicken-import.iif',array('mime_type'=>'text/plain'));
}
}
?>

View File

@ -1,31 +1,34 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting by rending the exportable items.
* This class supports OSB exporting.
*
* @package Export
* @category Helpers
* @category Model
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Export extends ORM_OSB {
protected $_serialize_column = array(
'map_data',
// Relationships
protected $_has_many = array(
'export_module' => array('far_key'=>'id','xforeign_key'=>'x'),
);
public function list_itemsnoexport() {
$result = array();
protected $_form = array('id'=>'id','value'=>'name');
$mo = ORM::factory('Module',array('name'=>'product'));
$p = ORM::factory('Product')
->order_by('id');
/**
* Return the object of the product plugin
*/
public function plugin(Model_Export_Module $emo,$type='') {
$c = Kohana::classname('Export_Plugin_'.$this->plugin);
foreach ($p->find_all() as $po)
if (! ORM::factory('Export')->where('module_id','=',$mo->id)->where('item_id','=',$po->id)->find()->loaded())
$result[$po->id] = $po;
if (! $this->plugin OR ! class_exists($c))
return NULL;
return $result;
$o = new $c($emo);
return $type ? $o->$type : $o;
}
}
?>

View File

@ -0,0 +1,36 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting.
*
* @package Export
* @category Model
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Export_DataMap extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'export_module' => array(),
'module' => array(),
);
public function list_itemsnoexport(Model $o,$emoid,$desc='title()') {
$result = array();
$o->select(array($this->table_name().'.id','edm'))
->join($this->table_name(),'LEFT OUTER')
->on($this->table_name().'.site_id','=',$o->table_name().'.site_id') // @todo This should be automatic
->on($this->table_name().'.item_id','=',$o->table_name().'.id')
->on('export_module_id','=',$emoid)
->where($o->table_name().'.status','=',TRUE)
->having('edm','=',NULL);
foreach ($o->find_all() as $object)
$result[$object->id] = $object->resolve($desc);
return $result;
}
}
?>

View File

@ -1,17 +1,18 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Quicken exporting capabilities.
* This class supports OSB exporting.
*
* @package Export
* @category Helpers
* @category Model
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Quicken_Payment extends Quicken {
protected $defaults = array(
'TRNSTYPE'=>'PAYMENT'
class Model_Export_Item extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'export_module' => array(),
);
}
?>

View File

@ -0,0 +1,42 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting.
*
* @package Export
* @category Model
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Export_Module extends ORM_OSB {
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
// Relationships
protected $_belongs_to = array(
'export' => array(),
'module' => array(),
);
protected $_has_many = array(
'export_item' => array('far_key'=>'id'),
);
protected $_nullifempty = array(
'display',
);
public function list_export() {
$o = $this->module->module();
return $o
->select(array($this->export_item->table_name().'.date_orig','exported'))
->join($this->export_item->table_name(),'LEFT OUTER')
->on($this->export_item->table_name().'.site_id','=',$o->table_name().'.site_id') // @todo This should be automatic
->on($this->export_item->table_name().'.item_id','=',$o->table_name().'.id')
->on('export_module_id','=',$this->id)
->where($o->table_name().'.date_orig','>=',time()-86400*90)
->find_all();
}
}
?>

View File

@ -1,35 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is the abstract class for all export capabilities.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
abstract class OSBExport {
private $_data = array();
protected $_items = array();
protected $_payments = array();
abstract public function export();
public function __get($key) {
return $this->_data[$key];
}
public function __set($key,$value) {
$this->_data[$key] = $value;
}
protected function keys() {
return array_keys($this->_data);
}
protected function vals() {
return array_values($this->_data);
}
}
?>

View File

@ -1,69 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Quicken exporting capabilities.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 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) {
if (! $payitem->AMOUNT)
continue;
$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;
}
}
?>

View File

@ -1,26 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Quicken exporting capabilities.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Quicken_Invoice extends Quicken {
protected $defaults = array(
'ACCNT'=>'Accounts Receivable',
'TRNSTYPE'=>'TAX_INVOICE'
);
public function addInvoiceItem(Quicken_InvoiceItem $item) {
array_push($this->_items,$item);
}
public function addPayment(Quicken_Payment $item) {
array_push($this->_payments,$item);
}
}
?>

View File

@ -1,14 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Quicken exporting capabilities.
*
* @package Export
* @category Helpers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Quicken_InvoiceItem extends Quicken {
}
?>

View File

@ -1,18 +1,23 @@
<table>
<tr>
<td>Product</td>
<td><?php echo Form::select('item_id',$eo->list_itemsnoexport()); ?></td>
</tr>
<tr>
<td>Export</td>
<td><?php echo Form::input('plugin_name','quicken',array('disabled'=>'disabled')); ?></td>
</tr>
<tr>
<td>Account ID</td>
<td><?php echo Form::input('map_data[account]','Internet:ADSL Supply'); ?></td>
</tr>
<tr>
<td>Item ID</td>
<td><?php echo Form::input('map_data[item]','ADSL:0256/064'); ?></td>
</tr>
</table>
<div class="row">
<div class="span10 offset1">
<fieldset>
<legend>Add Export Data Map</legend>
<p>Available <?php $o->module->table_name(); ?> Columns to display</p>
<?php echo Form::select('item_id',$o->list_itemsnoexport($module,$emo->id),NULL,array('label'=>'Product')); ?>
<?php echo Form::input('map_data[account]','Internet:ADSL Supply',array('label'=>'Accounting Code','placeholder'=>'Account Code')); ?>
<?php echo Form::input('map_data[item]','ADSL:ADSL2',array('label'=>'Inventory Code','placeholder'=>'Inventory Code')); ?>
<?php echo Form::hidden('export_module_id',$emo->id); ?>
<?php echo Form::hidden('module_id',$module->mid()); ?>
<div class="row">
<div class="offset2">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="button" class="btn">Cancel</button>
</div>
</div>
</fieldset>
</div> <!-- /span10 -->
</div> <!-- /row -->

View File

@ -0,0 +1,20 @@
<div class="row">
<div class="span10 offset1">
<fieldset>
<legend>Export Module Update</legend>
<p>Available <?php $o->module->table_name(); ?> Columns to display</p>
<?php echo Table::factory()
->data($o->module->module()->table_columns())
->jssort(FALSE)
->columns(array(
'column_name'=>'Name',
))
->select(URL::link('reseller','export/export'),$o->id,array('export_module_id'=>$o->id))
->prepend(array(
'column_name'=>array('input'=>array('key'=>'display[__VALUE__]','values'=>$o->display)),
)); ?>
</fieldset>
</div> <!-- /span8 -->
</div> <!-- /row -->

View File

@ -84,6 +84,19 @@ class Model_Invoice_Item extends ORM_OSB {
return Currency::round($result);
}
public function tax_items() {
$result = array();
foreach ($this->invoice_item_tax->find_all() as $iit) {
if (! isset($result[$iit->tax_id]))
$result[$iit->tax_id] = 0;
$result[$iit->tax_id] += $iit->amount;
}
return $result;
}
// This total of this item before discounts and taxes
public function subtotal() {
return Currency::round($this->price_base*$this->quantity);

@ -1 +1 @@
Subproject commit b65ddab2d0499e00e2b0e4caebc2178433b25c5b
Subproject commit 41e777bd9d85fdc9d32a3aa22f916d0d94530048

View File

@ -25,6 +25,9 @@ class Model_Payment extends ORM_OSB {
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'date_payment'=>array(
array('Config::date',array(':value')),
),

View File

@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Task_Listactive extends Task {
class Task_Task_Listactive extends Minion_Task {
protected function _execute(array $params) {
$header = "%2s %30s %21s %21s %40s\n";

View File

@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Task_Run extends Task {
class Task_Task_Run extends Minion_Task {
protected function _execute(array $params) {
if ($params['id']) {
$to = ORM::factory('Task',$params['id']);