2013-08-13 19:37:11 +02:00
< ? 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-13 19:37:11 +02:00
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
2013-08-13 19:37:11 +02:00
protected $TABLE_NAME = " Invoices " ;
protected $fields = array (
'id' => 'key' ,
2013-08-22 23:14:14 +02:00
'date' => 'date' ,
2013-08-13 19:37:11 +02:00
'buyer' => 'int' ,
2013-09-14 23:21:49 +02:00
'amount' => 'int' ,
2013-08-13 19:37:11 +02:00
'what' => 'text'
);
2013-08-30 20:07:52 +02:00
public function __construct () {
parent :: __construct ();
2013-09-23 23:07:37 +02:00
$this -> users_in = new UsersIn ( 'invoice' );
2013-08-30 20:07:52 +02:00
}
2013-08-26 09:52:04 +02:00
// Getters
// =======
2013-08-13 19:37:11 +02:00
public function getId () {
return $this -> id ;
}
2013-08-30 20:07:52 +02:00
public function getDate ( $format = " d-m-Y H:i " ) {
2013-09-08 18:36:59 +02:00
if ( ! empty ( $this -> date ))
return $this -> date -> format ( $format );
else
return false ;
2013-08-13 19:37:11 +02:00
}
public function getBuyer () {
return $this -> buyer ;
}
2013-09-07 23:56:31 +02:00
public function getUsersIn () {
return $this -> users_in ;
}
2013-08-13 19:37:11 +02:00
public function getAmount () {
2013-09-14 23:21:49 +02:00
return ( float ) $this -> amount / 100 ; // Amount is stored in cents
2013-08-13 19:37:11 +02:00
}
public function getWhat () {
return $this -> what ;
}
2013-08-26 09:52:04 +02:00
// Setters
// =======
2013-08-13 19:37:11 +02:00
public function setId ( $id ) {
2013-09-08 00:10:13 +02:00
$this -> users_in -> setInvoiceId ( $id );
2013-08-13 19:37:11 +02:00
$this -> id = ( int ) $id ;
}
2013-08-30 20:07:52 +02:00
public function setDate ( $minute , $hour , $day , $month , $year ) {
2013-09-25 20:28:09 +02:00
if (( int ) $minute < 10 ) $minute = '0' . ( int ) $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-13 19:37:11 +02:00
}
2013-08-30 20:07:52 +02:00
public function setGuests ( $guests ) {
$this -> guests = $guests ;
}
2013-08-13 19:37:11 +02:00
public function setBuyer ( $buyer ) {
$this -> buyer = ( int ) $buyer ;
}
2013-08-17 18:43:35 +02:00
public function setAmount ( $amount ) {
2013-09-17 00:03:26 +02:00
$amount = str_replace ( ',' , '.' , $amount );
2013-09-14 23:21:49 +02:00
$this -> amount = ( int ) ( $amount * 100 ); // Amount is stored in cents
2013-08-13 19:37:11 +02:00
}
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-09-12 18:44:04 +02:00
// Get the amount to pay by person
// ===============================
2013-09-14 23:21:49 +02:00
public function getAmountPerPerson ( $id ) {
2013-09-23 23:07:37 +02:00
$users_in = $this -> users_in -> get ();
$guests = 0 ;
foreach ( $users_in as $user => $guests_user ) {
$guests += ( int ) $guests_user ;
}
2013-09-14 23:21:49 +02:00
// Amount is stored in cents
2013-11-09 18:00:00 +01:00
return ( isset ( $users_in [( int ) $id ])) ? round ( $this -> amount / 100 / ( count ( $users_in ) + $guests ) * ( 1 + $users_in [( int ) $id ]), 2 ) : 0 ; // Note : $users_in[(int) $id] is the number of guests for user $id
2013-09-12 18:44:04 +02:00
}
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 ;
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' ]);
2013-09-14 23:21:49 +02:00
$this -> amount = ( int ) $data [ 'amount' ];
2013-08-27 15:51:04 +02:00
$this -> setBuyer ( $data [ 'buyer' ]);
2013-09-08 18:36:59 +02:00
$this -> date = DateTime :: createFromFormat ( 'Y-m-d H:i:s' , $data [ 'date' ]);
2013-08-27 15:51:04 +02:00
}
2013-12-20 13:20:10 +01:00
2013-09-08 00:10:13 +02:00
// Override parent load() method
// =============================
2013-12-20 13:20:10 +01:00
public function load ( $fields = NULL , $first_only = false , $key_array = 'id' ) {
2013-09-08 00:10:13 +02:00
$return = parent :: load ( $fields , $first_only ); // Execute parent load
2013-09-08 18:36:59 +02:00
if ( is_array ( $return )) {
2013-09-08 00:10:13 +02:00
foreach ( array_keys ( $return ) as $key ) {
$return [ $key ] -> users_in -> load (); // Load users in for each invoice
}
}
2013-09-08 18:36:59 +02:00
elseif ( is_a ( $return , 'Invoice' )) {
$return -> users_in -> load ();
}
2013-09-08 00:10:13 +02:00
return $return ; // Return the loaded elements
}
2013-09-08 18:36:59 +02:00
// Override parent save() method
2013-09-08 00:10:13 +02:00
// ============================
public function save () {
parent :: save (); // Save invoice element
$this -> users_in -> save (); // Save users in
}
2013-09-08 18:36:59 +02:00
// Override parent delete() method
// ===============================
public function delete () {
parent :: delete (); // Delete invoice element
$this -> users_in -> delete (); // Also delete users in
}
2013-08-13 19:37:11 +02:00
}