From 8d53924988272d6da2e66b6dd19bc3e9f32d38f5 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 11 Jan 2012 19:59:20 +1100 Subject: [PATCH] Added Bulk Payments --- application/classes/config.php | 4 +- application/classes/model/account.php | 2 +- application/classes/orm.php | 2 +- .../classes/controller/admin/payment.php | 34 ++++++ .../payment/classes/payment/bulk/ezypay.php | 104 ++++++++++++++++++ .../views/payment/admin/addbulk/ezypay.php | 13 +++ .../payment/admin/addbulk/ezypay/body.php | 7 ++ .../payment/admin/addbulk/ezypay/foot.php | 6 + .../payment/admin/addbulk/ezypay/head.php | 8 ++ 9 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 modules/payment/classes/payment/bulk/ezypay.php create mode 100644 modules/payment/views/payment/admin/addbulk/ezypay.php create mode 100644 modules/payment/views/payment/admin/addbulk/ezypay/body.php create mode 100644 modules/payment/views/payment/admin/addbulk/ezypay/foot.php create mode 100644 modules/payment/views/payment/admin/addbulk/ezypay/head.php diff --git a/application/classes/config.php b/application/classes/config.php index bd787ca0..39b9c4df 100644 --- a/application/classes/config.php +++ b/application/classes/config.php @@ -59,8 +59,8 @@ class Config extends lnApp_Config { return Company::name(); } - public static function siteid() { - return Config::instance()->loadsite()->so->id; + public static function siteid($format=FALSE) { + return $format ? sprintf('%02s',Config::instance()->loadsite()->so->id) : Config::instance()->loadsite()->so->id; } public static function sitemode() { diff --git a/application/classes/model/account.php b/application/classes/model/account.php index 1b5e4565..be59abe0 100644 --- a/application/classes/model/account.php +++ b/application/classes/model/account.php @@ -45,7 +45,7 @@ class Model_Account extends Model_Auth_UserDefault { } public function accnum() { - return sprintf('%02s-%04s',Config::siteid(),$this->id); + return sprintf('%s-%04s',Config::siteid(TRUE),$this->id); } public function title($name) { diff --git a/application/classes/orm.php b/application/classes/orm.php index 5db74939..4d644008 100644 --- a/application/classes/orm.php +++ b/application/classes/orm.php @@ -67,7 +67,7 @@ class ORM extends Kohana_ORM { $value = $this->__get($column); // If some of our fields need to be formated for display purposes. - if ($this->_loaded AND ! $this->_formated AND $this->_display_filters) + if (! $this->_formated AND $this->_display_filters) $this->_format(); if (isset($this->_object_formated[$column])) diff --git a/modules/payment/classes/controller/admin/payment.php b/modules/payment/classes/controller/admin/payment.php index 4472ab32..655740f1 100644 --- a/modules/payment/classes/controller/admin/payment.php +++ b/modules/payment/classes/controller/admin/payment.php @@ -13,6 +13,7 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin { protected $secure_actions = array( 'add'=>TRUE, + 'addbulk'=>TRUE, 'list'=>TRUE, 'view'=>TRUE, 'autocomplete'=>FALSE, @@ -203,5 +204,38 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin { 'body'=>$this->add_view($id,$output), )); } + + public function action_addbulk() { + $supported = array( + 'ezypay'=>'Ezypay', + ); + + $output = ''; + + if ($_POST AND isset($_POST['payer'])) { + $c = sprintf('payment_bulk_%s',$_POST['payer']); + $o = new $c(); + + if (! $_FILES) { + $output .= $o->form(); + + } else { + + $output .= $o->process(); + } + + // We dont know what sort of payment type yet + } else { + $output .= Form::open(); + $output .= Form::select('payer',$supported); + $output .= Form::submit('submit','submit',array('class'=>'form_button')); + $output .= Form::close(); + } + + Block::add(array( + 'title'=>_('Bulk Payments Received'), + 'body'=>$output, + )); + } } ?> diff --git a/modules/payment/classes/payment/bulk/ezypay.php b/modules/payment/classes/payment/bulk/ezypay.php new file mode 100644 index 00000000..9123f5e0 --- /dev/null +++ b/modules/payment/classes/payment/bulk/ezypay.php @@ -0,0 +1,104 @@ +'multipart/form-data')); + $return .= Form::hidden('payer',$_POST['payer']); + $return .= View::factory('payment/admin/addbulk/ezypay'); + $return .= Form::submit('submit','submit',array('class'=>'form_button')); + $return .= Form::close(); + + return $return; + } + + public function process() { + $payments = array(); + + // Process payment + $file = file_get_contents($_FILES['payment']['tmp_name']); + $file = explode("\r\n",$file); + + $i = 0; + foreach ($file as $line) { + // Line 0 is our header + if ($i++ == 0 OR ! trim($line)) + continue; + + // Trim our whitespace on the end of the line. + $line = preg_replace("/\s+$/",'',$line); + $array = explode("\t",$line); + + // Field 4 has our account reference + if (preg_match('/^'.Config::siteid(TRUE).'-/',$array[4]) AND $array[10] == 'Cleared') { + $aid = preg_replace('/^'.Config::siteid(TRUE).'-/','',$array[4]); + + $po = ORM::factory('payment'); + $po->account_id = $aid; + $po->total_amt = $array[7]; + $po->notes = $array[2].':'.$array[3]; + $po->date_payment = strtotime(str_replace('/','-',$array[8])); + + $payments[$array[3]] = $po; + } + } + + $file = file_get_contents($_FILES['transaction']['tmp_name']); + $file = explode("\r\n",$file); + + $i = 0; + foreach ($file as $line) { + // Line 0 is our header + if ($i++ == 0 OR ! trim($line)) + continue; + + // Trim our whitespace on the end of the line. + $line = preg_replace("/\s+$/",'',$line); + $array = explode("\t",$line); + + // Our commission fees + // @todo This should be in a config file + if (in_array($array[9],array(1,15))) + $payments[$array[3]]->fees_amt = (float)$array[7]; + + // @todo Hack - since the reports dont show how the payment was made. + // @todo Put this in a config file, in the mean time. + if ($array[7] == 1.05) + $payments[$array[3]]->checkout_plugin_id = 2; + else + $payments[$array[3]]->checkout_plugin_id = 4; + } + + $return = ''; + $return .= View::Factory('payment/admin/addbulk/ezypay/head'); + + $total = $fees = 0; + foreach ($payments as $po) { + $po->save(); + + $total += $po->total_amt; + $fees += $po->fees_amt; + + $return .= View::Factory('payment/admin/addbulk/ezypay/body') + ->set('o',$po); + } + + $return .= View::Factory('payment/admin/addbulk/ezypay/foot') + ->set('total',$total) + ->set('fees',$fees);; + + return $return; + } +} +?> diff --git a/modules/payment/views/payment/admin/addbulk/ezypay.php b/modules/payment/views/payment/admin/addbulk/ezypay.php new file mode 100644 index 00000000..ac570294 --- /dev/null +++ b/modules/payment/views/payment/admin/addbulk/ezypay.php @@ -0,0 +1,13 @@ + + + + + + + + + + + + +
Ezypay Payment
Payment File (BillDetails)
Transaction File (AddItems)
diff --git a/modules/payment/views/payment/admin/addbulk/ezypay/body.php b/modules/payment/views/payment/admin/addbulk/ezypay/body.php new file mode 100644 index 00000000..932a9650 --- /dev/null +++ b/modules/payment/views/payment/admin/addbulk/ezypay/body.php @@ -0,0 +1,7 @@ + + display('id'); ?> + display('date_payment'); ?> + display('total_amt'); ?> + display('fees_amt'); ?> + display('notes'); ?> + diff --git a/modules/payment/views/payment/admin/addbulk/ezypay/foot.php b/modules/payment/views/payment/admin/addbulk/ezypay/foot.php new file mode 100644 index 00000000..2c77b4bb --- /dev/null +++ b/modules/payment/views/payment/admin/addbulk/ezypay/foot.php @@ -0,0 +1,6 @@ + +   + + + + diff --git a/modules/payment/views/payment/admin/addbulk/ezypay/head.php b/modules/payment/views/payment/admin/addbulk/ezypay/head.php new file mode 100644 index 00000000..53a4ee6d --- /dev/null +++ b/modules/payment/views/payment/admin/addbulk/ezypay/head.php @@ -0,0 +1,8 @@ + + + + + + + +
Payment IDDate PaymentAmountFeesTransaction ID