Improved ADSL Billing Review

This commit is contained in:
Deon George
2013-10-08 13:34:56 +11:00
parent ab3735914b
commit 638d123739
15 changed files with 367 additions and 248 deletions

View File

@@ -13,7 +13,6 @@ class Controller_Admin_Service extends Controller_Service {
protected $secure_actions = array(
'ajaxjson_traffic'=>TRUE,
'adslstat'=>TRUE,
'listadslbilling'=>TRUE,
'listexpiring'=>TRUE,
'listdomainservicesbysupplier'=>TRUE,
'listdomainservicesbydnshost'=>TRUE,
@@ -221,194 +220,6 @@ class Controller_Admin_Service extends Controller_Service {
));
}
/**
* Reconcile billing for an ADSL supplier
*
* @todo this should really be in a different class, since adsl wont be part of the main app
*/
public function action_listadslbilling() {
$id = $this->request->param('id');
$aso = ORM::factory('ADSL_Supplier',$id);
// Process upload
// @todo This should be separated out by supplier in case each supplier has a different format
if ($_FILES) {
$files = Validation::factory($_FILES)
->rule('csv','Upload::valid')
->rule('csv','Upload::not_empty')
->rule('csv','Upload::type',array(':value',array('csv')))
->rule('csv','Upload::size',array(':value','10M'));
if ($files->check())
foreach ($files->data() as $file)
$csv = $this->process($file);
}
// @todo add a display if there are no items
$i = $j = 0;
$total = 0;
$summary = '';
$output = View::factory($this->viewpath().'/head');
$output .= '<table class="box-left">';
foreach ($aso->services(TRUE) as $so) {
$po = $so->plugin()->product();
// Reset our uploaded data
$uploaded = array();
$uploaded['excess'] = empty($csv[$so->plugin()->service_number]['excess']) ? 0 : $csv[$so->plugin()->service_number]['excess'];
// If our uploaded file has some cost data.
if (! empty($csv[$so->plugin()->service_number])) {
$uploaded['amount'] =
(empty($csv[$so->plugin()->service_number]['cost']) ? 0 : $csv[$so->plugin()->service_number]['cost']) +
(empty($csv[$so->plugin()->service_number]['credit']) ? 0 : $csv[$so->plugin()->service_number]['credit']);
// Record the the exception if the cost is not expected
if (round($po->adsl_supplier_plan->base_cost+$po->adsl_supplier_plan->tax(),2) != $uploaded['amount']) {
$summary .= View::factory($this->viewpath().'/summary')
->set('service',$so)
->set('plan',$po)
->set('planoverride',$so->plugin()->provided_adsl_plan_id ? TRUE : FALSE)
->set('amount',$uploaded['amount'])
->set('i',$j++%2);
$uploaded['checked'] = '';
} else {
$uploaded['checked'] = 'checked="checked"';
}
unset($csv[$so->plugin()->service_number]);
} else {
$uploaded['checked'] = '';
$uploaded['amount'] = 0;
}
$total += $uploaded['amount'];
$output .= View::factory($this->viewpath().'/body')
->set('service',$so)
->set('plan',$po)
->set('planoverride',$so->plugin()->provided_adsl_plan_id ? TRUE : FALSE)
->set('checked',$uploaded['checked'])
->set('amount',$uploaded['amount'])
->set('excess',$uploaded['excess'])
->set('adsl',$so->plugin())
->set('i',$i++%2);
}
$output .= View::factory($this->viewpath().'/foot')
->set('total',$total);
$output .= '</table>';
// Summary Report of remaining CSV items.
if (! empty($csv))
foreach ($csv as $service => $item) {
$summary .= View::factory($this->viewpath().'/summary_exception')
->set('service',$service)
->set('item',$item)
->set('i',$j++%2);
}
$output .= Form::open(NULL,array('enctype'=>'multipart/form-data'));
$output .= '<div>';
$output .= Form::file('csv');
$output .= Form::submit('submit','upload',array('class'=>'form_button'));
$output .= '</div>';
$output .= Form::close();
Block::add(array(
'title'=>_('ADSL Services'),
'body'=>$output,
));
if ($summary)
Block::add(array(
'title'=>_('Exception Charges'),
'body'=>'<table class="box-left">'.$summary.'</table>',
));
Style::add(array(
'type'=>'file',
'data'=>'css/list.css',
));
}
private function process(array $file) {
$data = file_get_contents($file['tmp_name']);
if (! $data)
return;
$start = $end = FALSE;
$result = array();
foreach (preg_split("/\n/",$data) as $line) {
// Items start after "Item ID"
if (! $start && preg_match('/^Item ID,/',$line)) {
$start = true;
continue;
// Items end after "Subtotal"
} elseif ($start && ! $end && preg_match('/^Subtotal:,/',$line)) {
$end = true;
continue;
// If we havent started or not ended, continue
} elseif (! $start || $end) {
continue;
}
$record = explode(',',$line);
// 1 = Item ID (ignore)
// 2 = Reference ID (ignore - its not useful for us)
// 3 = Category (always appears blank)
// 4 = Item Description (has our service number, rental and excess charges description)
// 0nnnnnnnnn - Monthly Internet Charge On Plan XXXXX For billing period (dd/mm/yyyy - dd/mm/yyyy) (7 FIELDED LINES)
// 0nnnnnnnnn - Excess usage charges for March 2013 (8 FIELDED LINES)
// 5 = Quantity
// Always 1 for Plan Fees
// 0nnnnnnnnn@graytech.net.au Excess Usage y GB (for excess charges)
// 6 = Unit Price
// Always 1 for Excess Usage (probably quantity)
// 7 = Total Price
// Unit price for Excess Usage
// 8 = Total Price for Excess Usage
if (! count($record) >= 7)
throw Kohana_Exception('Format of CSV file changed? (:record)',array(':record'=>$record));
if (preg_match('/Monthly Internet Charge On Plan /',$record[3])) {
list($service,$description) = explode(':',(preg_replace('/([0-9]+)\s+-\s+(.*)$/',"$1:$2",$record[3])));
$result[$service]['cost'] = str_replace('$','',$record[6]);
} elseif (preg_match('/VISP Credit/',$record[3])) {
list($service,$description) = explode(':',(preg_replace('/([0-9]+)\s+-\s+(.*)$/',"$1:$2",$record[3])));
$result[$service]['credit'] = str_replace('$','',$record[6]);
} elseif (preg_match('/Excess usage charges for /',$record[3])) {
list($service,$description) = explode(':',(preg_replace('/([0-9]+)\s+-\s+(.*)$/',"$1:$2",$record[3])));
$result[$service]['excess'] = str_replace('$','',$record[7]);
// Ignore Payment For Invoice lines
} elseif (preg_match('/Payment For Invoice:/',$record[3])) {
} else {
try {
list($service,$description) = explode(':',(preg_replace('/([0-9]+)\s+-\s+(.*)$/',"$1:$2",$record[3])));
$result[$service]['info'] = $line;
} catch (Exception $e) {
$result['000']['info'] = $line;
}
}
}
return $result;
}
/**
* List services that need to be invoiced.
*/