This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/payment/classes/controller/admin/payment.php

207 lines
5.6 KiB
PHP
Raw Normal View History

2011-12-20 05:46:10 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides payment capabilities.
*
* @package OSB
* @subpackage Payment
* @category Controllers/Admin
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Admin_Payment extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'add'=>TRUE,
'list'=>TRUE,
'view'=>TRUE,
2011-12-20 05:46:10 +00:00
'autocomplete'=>FALSE,
'autoitemlist'=>FALSE,
);
public function action_autocomplete() {
// We are only available via an ajax call.
if (! Request::current()->is_ajax())
die();
$return = array();
if (isset($_REQUEST['term']) AND trim($_REQUEST['term'])) {
$return += ORM::factory('account')->list_autocomplete($_REQUEST['term']);
$return += ORM::factory('invoice')->list_autocomplete($_REQUEST['term'],'account_id');
}
$this->auto_render = FALSE;
$this->response->headers('Content-Type','application/json');
$this->response->body(json_encode(array_values($return)));
}
public function action_autoitemlist() {
// We are only available via an ajax call.
if (! Request::current()->is_ajax() OR ! isset($_REQUEST['key']) OR ! trim($_REQUEST['key']))
die();
$output = View::factory($this->viewpath().'/head');
2011-12-20 05:46:10 +00:00
$this->auto_render = FALSE;
$i = 0;
if (isset($_REQUEST['pid']))
foreach (ORM::factory('payment_item')->where('payment_id','=',$_REQUEST['pid'])->find_all() as $pio)
$output .= View::factory($this->viewpath().'/body')
2011-12-20 05:46:10 +00:00
->set('trc',$i++%2 ? 'odd' : 'even')
->set('pio',$pio)
->set('io',$pio->invoice);
foreach (ORM::factory('account',$_REQUEST['key'])->invoices_due() as $io)
$output .= View::factory($this->viewpath().'/body')
2011-12-20 05:46:10 +00:00
->set('trc',$i++%2 ? 'odd' : 'even')
->set('io',$io);
// @todo Need the JS to add up the payment allocation before submission
$output .= View::factory($this->viewpath().'/foot')
2011-12-20 05:46:10 +00:00
->set('trc',$i++%2 ? 'odd' : 'even');
$this->response->body($output);
}
/**
* Show a list of invoices
*/
public function action_list() {
Block::add(array(
'title'=>_('Customer Payments'),
'body'=>Table::display(
ORM::factory('payment')->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>'admin/payment/view/'),
2011-12-20 05:46:10 +00:00
'date_payment'=>array('label'=>'Date'),
'account->accnum()'=>array('class'=>'right'),
'account->name()'=>array('label'=>'Account'),
'checkout->display("name")'=>array('label'=>'Method'),
'total_amt'=>array('label'=>'Total','class'=>'right'),
'balance(TRUE)'=>array('label'=>'Balance','class'=>'right'),
'invoicelist()'=>array('label'=>'Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>'admin/payment/view',
2011-12-20 05:46:10 +00:00
)),
));
}
private function add_view($id=NULL,$output='') {
2011-12-20 05:46:10 +00:00
$po = ORM::factory('payment',$id);
if ($_POST) {
if (isset($_POST['payment_item']) AND count($_POST['payment_item'])) {
foreach ($_POST['payment_item'] as $k=>$v) {
$pio = $po->add_item();
$pio->invoice_id = $k;
$pio->alloc_amt = $v;
}
}
if ($po->changed()) {
// Entry updated
if (! $po->values($_POST)->check() OR ! $po->save())
throw new Kohana_Exception('Unable to save payment');
else
SystemMessage::add(array(
'title'=>'Payment Recorded',
'type'=>'info',
'body'=>'Payment successfully recorded.',
2011-12-20 05:46:10 +00:00
));
}
}
$output .= Form::open();
$output .= View::factory('payment/admin/add_view')
2011-12-20 05:46:10 +00:00
->set('po',$po);;
$output .= Form::submit('submit','submit',array('class'=>'form_button'));
$output .= Form::close();
Style::add(array(
'type'=>'stdin',
'data'=>'.ui-autocomplete-loading { background: white url("'.URL::site('media/img/ui-anim_basic_16x16.gif').'") right center no-repeat; }'
));
Style::add(array(
'type'=>'file',
'data'=>'js/jquery.ui/css/smoothness/jquery-ui-1.8.16.custom.css',
));
Script::add(array(
'type'=>'file',
'data'=>'js/jquery-ui-1.8.16.custom.min.js',
));
Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() {
$("input[name=date_payment]").datepicker({
dateFormat: "@",
showOtherMonths: true,
selectOtherMonths: true,
showButtonPanel: true,
beforeShow: function(data) {
if (data.value)
data.value = data.value*1000;
},
onClose: function(data) {
$("input[name=date_payment]").val(data/1000);
}
});
$("input[name=account_id]").autocomplete({
source: "'.URL::site('admin/payment/autocomplete').'",
minLength: 2,
change: function(event,ui) {
// Send the request and update sub category dropdown
$.ajax({
type: "GET",
data: "key="+$(this).val(),
dataType: "html",
cache: false,
url: "'.URL::site('admin/payment/autoitemlist').'",
timeout: 2000,
error: function(x) {
alert("Failed to submit");
},
2011-12-20 05:46:10 +00:00
success: function(data) {
$("div[id=items]").replaceWith(data);
}
});
2011-12-20 05:46:10 +00:00
}
});
});'
));
if ($po->loaded()) {
Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() {
$("div[id=items]").load("'.URL::site('admin/payment/autoitemlist').'", {key: "'.$po->account_id.'", pid: "'.$po->id.'" });
});'
));
}
return $output;
}
public function action_add() {
2011-12-20 05:46:10 +00:00
Block::add(array(
'title'=>_('Add Payments Received'),
'body'=>$this->add_view(),
));
}
public function action_view() {
list($id,$output) = Table::page(__METHOD__);
Block::add(array(
'title'=>sprintf('%s: %s',_('View Payments Received'),$id),
'body'=>$this->add_view($id,$output),
2011-12-20 05:46:10 +00:00
));
}
}
?>