Started to work on Payback system

This commit is contained in:
Phyks 2013-09-10 23:07:39 +02:00
parent d4a5da6297
commit 978b518f78
3 changed files with 127 additions and 3 deletions

106
inc/Paybacks.class.php Normal file
View File

@ -0,0 +1,106 @@
<?php
require_once('data/config.php');
require_once('Storage.class.php');
class Payback extends Storage {
protected $id = 0, $date, $invoice_id, $amount, $from, $to;
protected $TABLE_NAME = "Paybacks";
protected $fields = array(
'id'=>'key',
'date'=>'date',
'invoice_id'=>'int',
'amount'=>'float',
'from_user'=>'int',
'to_user'=>'int'
);
public function __construct() {
parent::__construct();
}
// Getters
// =======
public function getId() {
return (int) $this->id;
}
public function getDate($format = 'd-m-Y H:i') {
if(!empty($this->date))
return $this->date->format($format);
else
return false;
}
public function getInvoice() {
return (int) $this->invoice_id;
}
public function getAmount() {
return (float) $this->amount;
}
public function getFrom() {
return (int) $this->from;
}
public function getTo() {
return (int) $this->to;
}
// Setters
// =======
public function setId($id) {
$this->id = (int) $id;
}
public function setDate($minute, $hour, $day, $month, $year) {
if((int) $minute < 10) $minute = '0'.$minute;
$this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute);
}
public function setInvoice($invoice_id) {
$this->invoice_id = (int) $invoice_id;
}
public function setAmount($amount) {
$this->amount = (float) $amount;
}
public function setFrom($from) {
$this->from = (int) $from;
}
public function setTo($to) {
$this->to = (int) $to;
}
// Restores object from array
// ==========================
public function sessionRestore($data, $serialized = false) {
if($serialized)
$data = unserialize($data);
$this->setId($data['id']);
$this->setInvoice($data['invoice_id']);
$this->setAmount($data['amount']);
$this->setFrom($data['from']);
$this->setTo($data['to']);
$this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']);
}
// Maps htmlspecialchars on the class before display
// =================================================
public function secureDisplay() {
$this->id = (int) $this->id;
$this->invoice_id = (int) $this->invoice_id;
$this->amount = (float) $this->amount;
$this->from = (int) $this->from;
$this->to = (int) $this->to;
}
}

View File

@ -21,6 +21,7 @@
require_once('data/config.php'); require_once('data/config.php');
require_once('inc/User.class.php'); require_once('inc/User.class.php');
require_once('inc/Invoices.class.php'); require_once('inc/Invoices.class.php');
require_once('inc/Paybacks.class.php');
require_once('inc/rain.tpl.class.php'); require_once('inc/rain.tpl.class.php');
require_once('inc/functions.php'); require_once('inc/functions.php');
require_once('inc/Ban.inc.php'); require_once('inc/Ban.inc.php');
@ -474,8 +475,9 @@
break; break;
default: default:
$use_cache = false;
// Display cached page in priority // Display cached page in priority
if($cache = $tpl->cache('index', $expire_time = 600, $cache_id = $current_user->getLogin())) { if($use_cache && $cache = $tpl->cache('index', $expire_time = 600, $cache_id = $current_user->getLogin())) {
echo $cache; echo $cache;
} }
else { else {
@ -485,8 +487,17 @@
$invoices_list = new Invoice(); $invoices_list = new Invoice();
$invoices_list = $invoices_list->load(); $invoices_list = $invoices_list->load();
if($invoices_list === false) $invoices_list = array();
$paybacks = array();
foreach($invoices_list as $invoice) {
$paybacks[$invoice->getId()] = new Payback();
$paybacks[$invoice->getId()] = $paybacks[$invoice->getId()]->load(array('invoice_id'=>$invoice->getId()));
}
$tpl->assign('users', secureDisplay($users_list)); $tpl->assign('users', secureDisplay($users_list));
$tpl->assign('invoices', secureDisplay($invoices_list)); $tpl->assign('invoices', secureDisplay($invoices_list));
$tpl->assign('paybacks', secureDisplay($paybacks));
// Cache the page (1 month to make it almost permanent and only regenerate it upon new invoice) // Cache the page (1 month to make it almost permanent and only regenerate it upon new invoice)
$tpl->cache('index', 108000, $current_user->getLogin()); $tpl->cache('index', 108000, $current_user->getLogin());

View File

@ -35,7 +35,7 @@
//Create table "Invoices" //Create table "Invoices"
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date INT(11), users_in VARCHAR(255), buyer INT(11), amount FLOAT, what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, users_in VARCHAR(255), buyer INT(11), amount FLOAT, what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
$count_invoices = $db->query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "'.$mysql_db.'" AND table_name = "'.$mysql_prefix.'"Invoices'); $count_invoices = $db->query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "'.$mysql_db.'" AND table_name = "'.$mysql_prefix.'"Invoices');
$count_invoices = $count_users->fetch(); $count_invoices = $count_users->fetch();
@ -53,7 +53,14 @@
$warning .= 'Table '.$mysql_prefix.'Users_in_invoices already exists. Not doing anything on this table. Please check manually that this table is correct.<br/>'; $warning .= 'Table '.$mysql_prefix.'Users_in_invoices already exists. Not doing anything on this table. Please check manually that this table is correct.<br/>';
} }
//Create table "Payback" - TODO //Create table "Paybacks"
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Paybacks (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, invoice_id INT(11), KEY invoice_id (invoice_id), amount FLOAT, from_user INT(11), to_user INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
$count_paybacks = $db->query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "'.$mysql_db.'" AND table_name = "'.$mysql_prefix.'"Paybacks');
$count_paybacks = $count_paybacks->fetch();
if($count_paybacks[0] > 0) {
$warning .= 'Table '.$mysql_prefix.'Paybacks already exists. Not doing anything on this table. Please check manually that this table is correct.<br/>';
}
} catch (PDOException $e) { } catch (PDOException $e) {
$error = 'Unable to connect to database and create database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.'; $error = 'Unable to connect to database and create database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.';
} }