2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is for processing Ezypay payments.
|
|
|
|
*
|
|
|
|
* @package Payment
|
|
|
|
* @category Helpers
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
|
|
|
class Payment_Bulk_Ezypay {
|
2013-06-19 09:26:07 +00:00
|
|
|
private function file($name) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
// Process file
|
|
|
|
$file = preg_split("/[\r]?[\n]+/",file_get_contents($_FILES[$name]['tmp_name']));
|
|
|
|
|
|
|
|
$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_push($result,explode("\t",$line));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The input form to capture the required files
|
|
|
|
*/
|
2013-10-10 02:44:53 +00:00
|
|
|
public function form() {
|
|
|
|
$result = '';
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
$result .= Form::open(NULL,array('enctype'=>'multipart/form-data','class'=>'form-horizontal'));
|
2013-10-10 02:44:53 +00:00
|
|
|
$result .= View::factory('payment/admin/addbulk/ezypay');
|
|
|
|
$result .= Form::close();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
/**
|
|
|
|
* Process the uploaded files
|
|
|
|
*/
|
2013-10-10 02:44:53 +00:00
|
|
|
public function process() {
|
|
|
|
// Process payment
|
2013-06-19 09:26:07 +00:00
|
|
|
$payments = array();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
foreach ($this->file('payment') as $line => $array) {
|
2013-10-10 02:44:53 +00:00
|
|
|
// Field 4 has our account reference
|
|
|
|
if (preg_match('/^'.Company::instance()->site(TRUE).'-/',$array[4]) AND $array[10] == 'Cleared') {
|
|
|
|
$aid = preg_replace('/^'.Company::instance()->site(TRUE).'-/','',$array[4]);
|
|
|
|
|
|
|
|
$po = ORM::factory('Payment');
|
|
|
|
$po->account_id = $aid;
|
|
|
|
$po->total_amt = $array[7];
|
2013-06-19 09:26:07 +00:00
|
|
|
$po->checkout_data = array('transid'=>$array[2].':'.$array[3]);
|
2013-10-10 02:44:53 +00:00
|
|
|
$po->date_payment = strtotime(str_replace('/','-',$array[8]));
|
|
|
|
|
2014-03-04 05:17:01 +00:00
|
|
|
$sbo = $po->account->service_billing->where('checkout_data','=',$array[3])->find();
|
2013-06-19 09:26:07 +00:00
|
|
|
if (! $sbo->loaded())
|
|
|
|
throw HTTP_Exception::factory(501,'No Service Billing Data for :aid (:pd)?',array(':aid'=>$aid,':pd'=>$array[3]));
|
|
|
|
|
|
|
|
$po->checkout_id = $sbo->checkout_id;
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
$payments[$array[3]] = $po;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
foreach ($this->file('transaction') as $line => $array) {
|
2013-10-10 02:44:53 +00:00
|
|
|
// If we dont have a payment item for this fee, we'll continue.
|
|
|
|
if (! isset($payments[$array[3]]))
|
|
|
|
continue;
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
// Our commission fees, which appear has Type 1 or 15
|
2013-10-10 02:44:53 +00:00
|
|
|
if (in_array($array[9],array(1,15)))
|
|
|
|
$payments[$array[3]]->fees_amt = (float)$array[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
$total = $fees = 0;
|
|
|
|
foreach ($payments as $po) {
|
|
|
|
$po->save();
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
if (! $po->saved())
|
|
|
|
throw HTTP_Exception::factory(501,'Failed to Save Payment for :aid (:pd)?',array(':aid'=>$po->account_id,':pd'=>$po->checkout_data['transid']));
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
$total += $po->total_amt;
|
|
|
|
$fees += $po->fees_amt;
|
|
|
|
}
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
$output = Table::factory()
|
|
|
|
->data($payments)
|
|
|
|
->columns(array(
|
|
|
|
'id'=>'ID',
|
|
|
|
'date_payment'=>'Date',
|
|
|
|
'checkout->display("name")'=>'Method',
|
|
|
|
'total_amt'=>'Amount',
|
|
|
|
'fees_amt'=>'Fees',
|
|
|
|
'account->accnum()'=>'Cust ID',
|
|
|
|
'account->name()'=>'Customer',
|
|
|
|
))
|
|
|
|
->prepend(array(
|
2013-06-05 03:42:55 +00:00
|
|
|
'id'=>array('url'=>URL::link('admin','payment/edit/')),
|
2013-06-19 09:26:07 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return View::factory('payment/admin/addbulk/ezypay_processed')
|
|
|
|
->set('table',$output)
|
|
|
|
->set('fee',$fees)
|
|
|
|
->set('total',$total);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|