diff --git a/TODO b/TODO index 69db01c..0e9e95c 100755 --- a/TODO +++ b/TODO @@ -6,6 +6,7 @@ * cf TODO in files * French template * Favicon +* Dépenses du dernier mois Manage paybacks : ================= diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index b7fe77e..78e1b1d 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -13,7 +13,7 @@ 'id'=>'key', 'date'=>'date', 'buyer'=>'int', - 'amount'=>'float', + 'amount'=>'int', 'what'=>'text' ); @@ -44,7 +44,7 @@ } public function getAmount() { - return $this->amount; + return (float) $this->amount / 100; // Amount is stored in cents } public function getWhat() { @@ -73,7 +73,7 @@ } public function setAmount ($amount) { - $this->amount = (float) $amount; + $this->amount = (int) ($amount * 100); // Amount is stored in cents } public function setWhat($what) { @@ -87,8 +87,12 @@ // Get the amount to pay by person // =============================== - public function getAmountPerPerson() { - return round($this->amount / count($this->users_in->get()), 2); + public function getAmountPerPerson($id) { + $guests = $this->users_in->get(); + $guests = $guests[(int) $id]; + + // Amount is stored in cents + return round($this->amount / 100 / (count($this->users_in->get()) + $guests), 2); } // Maps htmlspecialchars on the class before display @@ -111,7 +115,7 @@ $this->setId($data['id']); $this->setWhat($data['what']); - $this->setAmount($data['amount']); + $this->amount = (int) $data['amount']; $this->setBuyer($data['buyer']); $this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']); diff --git a/inc/Paybacks.class.php b/inc/Paybacks.class.php index 7c4ebdd..aaf3cfc 100644 --- a/inc/Paybacks.class.php +++ b/inc/Paybacks.class.php @@ -9,7 +9,7 @@ 'id'=>'key', 'date'=>'date', 'invoice_id'=>'int', - 'amount'=>'float', + 'amount'=>'int', 'from_user'=>'int', 'to_user'=>'int' ); @@ -37,7 +37,7 @@ } public function getAmount() { - return (float) $this->amount; + return (float) $this->amount / 100; // Amount is stored in cents } public function getFrom() { @@ -66,7 +66,7 @@ } public function setAmount($amount) { - $this->amount = (float) $amount; + $this->amount = (int) ($amount * 100); // Amount is stored in cents } public function setFrom($from) { @@ -86,7 +86,7 @@ $this->setId($data['id']); $this->setInvoice($data['invoice_id']); - $this->setAmount($data['amount']); + $this->amount = (int) $data['amount']; $this->setFrom($data['from_user']); $this->setTo($data['to_user']); diff --git a/index.php b/index.php index dccebba..3a79bc1 100644 --- a/index.php +++ b/index.php @@ -262,7 +262,7 @@ $user->save(); $_SESSION['current_user'] = $user->sessionStore(); - header('location: index.php'.$get_redir); + header('location: index.php?do=password&'.$get_redir); exit(); break; @@ -272,6 +272,44 @@ $user->setId($_GET['user_id']); $user->delete(); + // Update concerned invoices + $invoices = new Invoice(); + $invoices = $invoices->load(); + if($invoices !== FALSE) { + foreach($invoices as $invoice) { + if($invoice->getBuyer() == $_GET['user_id']) { + $invoice->setBuyer(0); + $invoice->save(); + } + if($invoice->getUsersIn()->inUsersIn($_GET['user_id'])) { + $users_in = $invoice->getUsersIn()->get(); + $users_in[0] = $users_in[$_GET['user_id']]; + unset($users_in[$_GET['user_id']]); + + $invoice->setUsersIn($users_in); + $invoice->save(); + } + } + } + + // Update paybacks + $paybacks = new Payback(); + $paybacks = $paybacks->load(array('from_user'=>(int) $_GET['user_id'])); + if($paybacks !== FALSE) { + foreach($paybacks as $payback) { + $payback->setFrom(0); + $payback->save(); + } + } + $paybacks = $paybacks->load(array('to_user'=>(int) $_GET['user_id'])); + if($paybacks !== FALSE) { + foreach($paybacks as $payback) { + $payback->setTo(0); + $payback->save(); + } + } + + // Clear the cache array_map("unlink", glob(raintpl::$cache_dir."*.rtpl.php")); @@ -489,10 +527,16 @@ $payback = new Payback(); } } + else { + $payback = $payback->load(array('invoice_id'=>(int) $_GET['invoice_id'], 'to_user'=>(int) $_GET['to'], 'from_user'=>(int) $_GET['from']), true); + + if($payback == false) + $payback = new Payback(); + } $payback->setDate(date('i'), date('G'), date('j'), date('n'), date('Y')); $payback->setInvoice($_GET['invoice_id']); - $payback->setAmount($invoice->getAmount()); + $payback->setAmount($invoice->getAmountPerPerson($_GET['from'])); $payback->setFrom($_GET['from']); $payback->setTo($_GET['to']); @@ -525,8 +569,10 @@ $paybacks = $paybacks->load(array('to_user'=>(int) $_GET['to'], 'from_user'=> (int) $_GET['from'], 'invoice_id'=> (int) $_GET['invoice_id'])); - foreach($paybacks as $payback) { - $payback->delete(); + if($paybacks == false) { + foreach($paybacks as $payback) { + $payback->delete(); + } } // Clear the cache @@ -535,6 +581,7 @@ array_map("unlink", $tmp_files); } + exit(); header('location: index.php'); exit(); } @@ -597,7 +644,7 @@ if($invoices_list_balances !== false) { foreach($invoices_list_balances as $invoice) { if($invoice->getUsersIn()->inUsersIn($user1->getId())) { - $balances[$user1->getId()][$user2->getId()] += $invoice->getAmountPerPerson(); + $balances[$user1->getId()][$user2->getId()] += $invoice->getAmountPerPerson($user1->getId()); } } } @@ -608,7 +655,7 @@ if($invoices_list_balances !== false) { foreach($invoices_list_balances as $invoice) { if($invoice->getUsersIn()->inUsersIn($user2->getId())) { - $balances[$user1->getId()][$user2->getId()] -= $invoice->getAmountPerPerson(); + $balances[$user1->getId()][$user2->getId()] -= $invoice->getAmountPerPerson($user2->getId()); } } } diff --git a/install.php b/install.php index 20f711c..774e5c4 100644 --- a/install.php +++ b/install.php @@ -28,13 +28,13 @@ $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(255), display_name VARCHAR(255), password VARCHAR(130), admin TINYINT(1), json_token VARCHAR(32)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); //Create table "Invoices" - $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, 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, buyer INT(11), amount INT(11), what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); //Create table "Users_in_invoices" $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users_in_invoices (invoice_id INT(11) NOT NULL, KEY invoice_id (invoice_id), user_id INT(11), KEY user_id (user_id), guests INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); //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'); + $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 INT(11), from_user INT(11), to_user INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); } catch (PDOException $e) { $error = 'Unable to connect to database and create database, check your credentials and config.
Error message : '.$e->getMessage().'.'; } diff --git a/tpl/default_en/edit_users.html b/tpl/default_en/edit_users.html index 2f95f02..5b11168 100644 --- a/tpl/default_en/edit_users.html +++ b/tpl/default_en/edit_users.html @@ -58,7 +58,7 @@ {elseif condition="$view == 'password'"}

Edit your password

-

+

Toggle visible

Toggle visible

diff --git a/tpl/default_en/index.html b/tpl/default_en/index.html index 28ac5bc..919ba76 100755 --- a/tpl/default_en/index.html +++ b/tpl/default_en/index.html @@ -56,16 +56,16 @@ ({$value2} guest) {/if} - - {if condition="$paybacks[$value1->getId()] === false"} + {if condition="$paybacks[$value1->getId()] === false || !in_array($key2, array_keys($paybacks[$value1->getId()]))"} {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"} {/if} - Remains {$value1->getAmountPerPerson()} {$currency} + Remains {$value1->getAmountPerPerson($key2)} {$currency} {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"} {/if} {else} - {if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson()"} + {if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson($key2)"} {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"} {/if} @@ -77,7 +77,7 @@ {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"} {/if} - Remains {$value1->getAmountPerPerson() - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency} + Remains {$value1->getAmountPerPerson($key2) - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency} {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"} {/if}