bouffeatulm/inc/Invoices.class.php

133 lines
3.9 KiB
PHP
Raw Normal View History

<?php
require_once('data/config.php');
require_once('Storage.class.php');
2013-09-07 23:56:31 +02:00
require_once('UsersIn.class.php');
2013-08-17 18:43:35 +02:00
class Invoice extends Storage {
2013-09-07 23:56:31 +02:00
protected $id = 0, $date, $users_in, $buyer, $amount, $what;
2013-08-30 20:07:52 +02:00
// date is a DateTime object
// buyer is a User object
2013-09-07 23:56:31 +02:00
// users_in is a UsersIn objects
protected $TABLE_NAME = "Invoices";
protected $fields = array(
'id'=>'key',
2013-08-22 23:14:14 +02:00
'date'=>'date',
'buyer'=>'int',
'amount'=>'float',
'what'=>'text'
);
2013-08-30 20:07:52 +02:00
public function __construct() {
parent::__construct();
2013-09-07 23:56:31 +02:00
$users_in = new UsersIn();
2013-08-30 20:07:52 +02:00
}
2013-08-26 09:52:04 +02:00
// Getters
// =======
public function getId() {
return $this->id;
}
2013-08-30 20:07:52 +02:00
public function getDate($format = "d-m-Y H:i") {
return $this->date->format($format);
}
public function getBuyer() {
return $this->buyer;
}
2013-09-07 23:56:31 +02:00
public function getUsersIn() {
return $this->users_in;
}
public function getAmount() {
return $this->amount;
}
public function getWhat() {
return $this->what;
}
2013-08-26 09:52:04 +02:00
// Setters
// =======
public function setId($id) {
$this->users_in->setInvoiceId($id);
$this->id = (int) $id;
}
2013-08-30 20:07:52 +02:00
public function setDate($minute, $hour, $day, $month, $year) {
if((int) $minute < 10) $minute = '0'.$minute;
2013-08-22 23:14:14 +02:00
2013-08-30 20:07:52 +02:00
$this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute);
}
2013-08-30 20:07:52 +02:00
public function setGuests($guests) {
$this->guests = $guests;
}
public function setBuyer($buyer) {
$this->buyer = (int) $buyer;
}
2013-08-17 18:43:35 +02:00
public function setAmount ($amount) {
$this->amount = (float) $amount;
}
public function setWhat($what) {
$this->what = $what;
}
2013-09-07 23:56:31 +02:00
public function setUsersIn($users_in) {
// Note : users_in in param is an array with users in listed and guests for each user
$this->users_in->set($users_in);
}
2013-08-26 21:21:52 +02:00
// Maps htmlspecialchars on the class before display
// =================================================
public function secureDisplay() {
$this->id = (int) $this->id;
$this->what = htmlspecialchars($this->what);
$this->amount = (float) $this->amount;
$this->buyer = (int) $this->buyer;
$this->date = htmlspecialchars($this->date);
return $this;
}
2013-08-27 15:51:04 +02:00
// Restores object from array
// ==========================
public function sessionRestore($data, $serialized = false) {
if($serialized) {
$data = unserialize($data);
}
$this->setId($data['id']);
$this->setWhat($data['what']);
$this->setAmount($data['amount']);
$this->setBuyer($data['buyer']);
$this->setDate($data['date']);
}
// Override parent load() method
// =============================
public function load($fields = NULL, $first_only = false) {
$return = parent::load($fields, $first_only); // Execute parent load
if($return !== false) {
foreach(array_keys($return) as $key) {
$return[$key]->users_in->load(); // Load users in for each invoice
}
}
return $return; // Return the loaded elements
}
// Overrid parent save() method
// ============================
public function save() {
parent::save(); // Save invoice element
$this->users_in->save(); // Save users in
}
}