aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--classes/constants.php2
-rw-r--r--classes/offer.php93
-rw-r--r--classes/payment.php142
-rw-r--r--include/home.php632
-rw-r--r--include/offers-edit.php7
-rw-r--r--install/index.php11
-rw-r--r--install/upgrade.php22
8 files changed, 568 insertions, 342 deletions
diff --git a/README.md b/README.md
index d5b44f1..263ffea 100644
--- a/README.md
+++ b/README.md
@@ -171,6 +171,7 @@ are listed by name and removal time. This way, you never really lose your file.
### 0.4 (Jul 26, 2016)
+0.4.2 Moved `offer.payment_received` to a separate table `payments`.
0.4.1 Some users are administrator and can create and delete users.
0.4 User authentication mechanism.
diff --git a/classes/constants.php b/classes/constants.php
index d7c7b7c..663d603 100644
--- a/classes/constants.php
+++ b/classes/constants.php
@@ -80,5 +80,5 @@ class constants {
const password_cost = 10;
/** @const version Version of BusinessAdmin. Don't change this yourself! */
- const version = '0.4.1';
+ const version = '0.4.2';
}
diff --git a/classes/offer.php b/classes/offer.php
index 0eb2fa1..56a8878 100644
--- a/classes/offer.php
+++ b/classes/offer.php
@@ -34,9 +34,8 @@ class offer {
* @var int $invoice_date A UNIX timestamp of the invoice date
* @var bool $accepted Whether the offer is accepted or not
* @var null|int $invoice_fileId If an invoice has been generated, an the id of the file
- * @var null|int $payment_received A UNIX timestamp of the date the payment has been received
*/
- protected $pdo, $id, $contactId, $start_date, $end_date, $invoice_date, $accepted, $invoice_fileId, $payment_received;
+ protected $pdo, $id, $contactId, $start_date, $end_date, $invoice_date, $accepted, $invoice_fileId;
const SUBTOTAL = 1;
const VAT = 2;
@@ -70,7 +69,6 @@ class offer {
$this->invoice_date = strtotime($offer['invoice_date']);
$this->accepted = (bool) $offer['accepted'];
$this->invoice_fileId = $offer['invoice_fileId'];
- $this->payment_received = ($offer['payment_received'] == null) ? null : strtotime($offer['payment_received']);
}
//------------------------------------------------------------------------------
@@ -147,7 +145,7 @@ class offer {
/**
* Get all discount ids for this offer
*
- * @see offer::getDiscounts() This funtion returns instances of the discount class instead of just the ids
+ * @see offer::getDiscounts() This funtion returns instances of the discount class instead of just the ids
*
* @throws PDOException Is something went wrong with the database
*
@@ -165,11 +163,11 @@ class offer {
/**
* Get all discounts for this offer
*
- * @see offer::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
+ * @see offer::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
*
* @throws PDOException If something went wrong with the database
*
- * @return discount[] An array indexed by id of instances of the discount class
+ * @return discount[] An array indexed by id of instances of the discount class
*/
public function getDiscounts() {
$ids = $this->getDiscountIds();
@@ -181,6 +179,42 @@ class offer {
}
/**
+ * Get the payment id for this offer
+ *
+ * @see offer::getPayment() This funtion returns an instance of the payment class instead of just the id
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int|null The id, or null if no payment exists
+ */
+ public function getPaymentId() {
+ $ids = array();
+ $payments = $this->pdo->query("SELECT `id` FROM `".constants::db_prefix."payment` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($payments as $payment) {
+ return $payment['id'];
+ }
+ return null;
+ }
+
+ /**
+ * Get the payment for this offer
+ *
+ * @see offer::getPaymentId() This function returns just the id of the payment, and not an instance of the payment class
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return payment|null The payment, or null if it does not exist
+ */
+ public function getPayment() {
+ $id = $this->getPaymentId();
+ if (is_null($id)) {
+ return null;
+ } else {
+ return new payment($this->pdo, $id);
+ }
+ }
+
+ /**
* Get the start date of the assignment
*
* @return int The start date as a UNIX timestamp
@@ -273,26 +307,11 @@ class offer {
* @return int|null The date as a UNIX timestamp, or null if it wasn't received yet
*/
public function getPaymentReceived() {
- return $this->payment_received;
- }
-
- /**
- * Set the payment received date of the assignment
- *
- * @param int $payment_received The new date the payment has been received as a UNIX timestamp
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setPaymentReceived($payment_received) {
- $stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."offer` SET `payment_received`=? WHERE `id`=?");
- $stmt->execute(array(date('Y-m-d', $payment_received), $this->id));
- if ($stmt->rowCount() == 1) {
- $this->payment_received = $payment_received;
- return true;
+ $payment = $this->getPayment();
+ if (is_null($payment)) {
+ return null;
} else {
- return false;
+ return $payment->getDate();
}
}
@@ -486,7 +505,7 @@ class offer {
* @throws PDOException If something went wrong with the database
* @throws Exception If there was a problem with the input
*
- * @return discount A new instance of the discount class containing the new discount
+ * @return discount A new instance of the discount class containing the new discount
*/
public function createDiscount($title, $description, $value, $vat) {
$stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."discount` (`offerId`,`title`,`description`,`value`,`VAT_percentage`) VALUES (?,?,?,?,?)");
@@ -506,6 +525,28 @@ class offer {
}
/**
+ * Add a payment for this order
+ *
+ * @param string $date Optional: the date for the payment
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If there was a problem with the input
+ *
+ * @return payment A new instance of the payment class containing the new payment
+ */
+ public function createPayment($date=null) {
+ $date = is_null($date) ? time() : $date;
+ $stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."payment` (`offerId`,`date`) VALUES (?,?)");
+ $stmt->execute([$this->id, date('Y-m-d H:i:s', $date)]);
+ if ($stmt->rowCount() == 1) {
+ return new payment($this->pdo, $this->pdo->lastInsertId());
+ } else {
+ $error = $stmt->errorInfo();
+ throw new Exception($error[2]);
+ }
+ }
+
+ /**
* Generate a PDF invoice
*
* @throws PDOException If something went wrong with the database
diff --git a/classes/payment.php b/classes/payment.php
new file mode 100644
index 0000000..9d4782c
--- /dev/null
+++ b/classes/payment.php
@@ -0,0 +1,142 @@
+<?php
+/**
+ * Provides the payment class, an interface to the payment table in the database
+ *
+ * @author Camil Staps
+ *
+ * BusinessAdmin: administrative software for small companies
+ * Copyright (C) 2015 Camil Staps (ViviSoft)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * An interface to the payment table in the database
+ */
+class payment {
+ /**
+ * @var pdo $pdo The PDO class for database communication
+ * @var int $id The id of the payment
+ * @var int $offerId The id of the offer this payment is linked to
+ * @var int $date A unix timestamp describing the date of the payment
+ */
+ protected $pdo, $offerId, $id, $date;
+
+ /**
+ * Create a new instance
+ *
+ * @param PDO $pdo The PDO class, to access the database
+ * @param int $id The id of the payment to fetch
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If the payment could not be found
+ */
+ public function __construct($pdo, $id) {
+ $this->pdo = $pdo;
+
+ $stmt = $this->pdo->prepare("SELECT * FROM `".constants::db_prefix."payment` WHERE `id`=?");
+ $stmt->execute(array($id));
+ if ($stmt->rowCount() == 0) {
+ throw new Exception("The payment with id '$id' could not be found.");
+ }
+ $payment = $stmt->fetch(PDO::FETCH_ASSOC);
+
+ $this->id = $payment['id'];
+ $this->offerId = $payment['offerId'];
+ $this->date = strtotime($payment['date']);
+ }
+
+ //------------------------------------------------------------------------------
+ // Getters and setters
+ //------------------------------------------------------------------------------
+
+ /**
+ * Get the ID of the payment
+ *
+ * @return int The ID
+ */
+ public function getId() {
+ return $this->id;
+ }
+
+ /**
+ * Get the ID of the offer that this payment is linked to
+ *
+ * @return int The ID
+ */
+ public function getOfferId() {
+ return $this->offerId;
+ }
+
+ /**
+ * Get the offer that this payment is linked to
+ *
+ * @return offer The offer
+ */
+ public function getOffer() {
+ return new offer($this->pdo, $this->offerId);
+ }
+
+ /**
+ * Get the date of the payment
+ *
+ * @return int A unix timestamp describing the date of the payment
+ */
+ public function getDate() {
+ return $this->date;
+ }
+
+ /**
+ * Set the date of the payment
+ *
+ * @param int $date The new date for the payment
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return bool True on success, false on failure
+ */
+ public function setDate($date) {
+ $stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."payment` SET `date`=? WHERE `id`=?");
+ $stmt->execute([date('Y-m-d H:i:s', $date), $this->id]);
+ if ($stmt->rowCount() == 1) {
+ $this->date = $date;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ //------------------------------------------------------------------------------
+ // Other functions
+ //------------------------------------------------------------------------------
+
+ /**
+ * Remove this payment from the database
+ *
+ * If this doesn't succeed (i.e. false is returned), that means the payment was removed manually or by another instance of this class
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return bool True on success, false on failure
+ */
+ public function delete() {
+ $stmt = $this->pdo->prepare("DELETE FROM `".constants::db_prefix."payment` WHERE `id`=?");
+ $stmt->execute(array($this->id));
+ if ($stmt->rowCount() != 1) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+}
diff --git a/include/home.php b/include/home.php
index cb624fe..c9d0cf3 100644
--- a/include/home.php
+++ b/include/home.php
@@ -2,17 +2,17 @@
/**
* BusinessAdmin: administrative software for small companies
* Copyright (C) 2015 Camil Staps (ViviSoft)
- *
+ *
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -24,317 +24,323 @@ require('./header.php');
<div id="wrapper">
- <?php require('nav.php'); ?>
-
- <div id="page-wrapper">
- <div class="row">
- <div class="col-lg-12">
- <h1 class="page-header">Welcome</h1>
- </div>
- <!-- /.col-lg-12 -->
- </div>
- <!-- /.row -->
- <div class="row">
- <div class="col-lg-3 col-md-3 col-sm-6">
- <?php
- $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted` = 0")));
- ?>
- <div class="panel panel-<?=($count==0 ? 'primary' : 'yellow')?>">
- <div class="panel-heading">
- <div class="row">
- <div class="col-xs-3">
- <i class="fa fa-briefcase fa-5x"></i>
- </div>
- <div class="col-xs-9 text-right">
- <div class="huge"><?=$count?></div>
- <!--<div>Unaccepted offers</div>-->
- </div>
- </div>
- </div>
- <a href="<?=constants::url_external?>offers">
- <div class="panel-footer">
- <span class="pull-left">Unaccepted offers</span>
- <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
- <div class="clearfix"></div>
- </div>
- </a>
- </div>
- </div>
- <div class="col-lg-3 col-md-3 col-sm-6">
- <?php
- $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= CURDATE()")));
- ?>
- <div class="panel panel-<?=($count==0 ? 'primary' : ($count < 3) ? 'green' : ($count < 5 ? 'yellow' : 'red'))?>">
- <div class="panel-heading">
- <div class="row">
- <div class="col-xs-3">
- <i class="fa fa-tasks fa-5x"></i>
- </div>
- <div class="col-xs-9 text-right">
- <div class="huge"><?=$count?></div>
- <!--<div>Active offers</div>-->
- </div>
- </div>
- </div>
- <a href="#panel-active-offers">
- <div class="panel-footer">
- <span class="pull-left">Active offers</span>
- <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
- <div class="clearfix"></div>
- </div>
- </a>
- </div>
- </div>
- <div class="col-lg-3 col-md-3 col-sm-6">
- <?php
- $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted`=1", "`end_date` <= CURDATE()", "`invoice_date` IS NULL OR `invoice_date`='1970-01-01' OR `invoice_date`>CURDATE()")));
- ?>
- <div class="panel panel-<?=($count==0 ? 'primary' : ($count < 3) ? 'green' : ($count < 5 ? 'yellow' : 'red'))?>">
- <div class="panel-heading">
- <div class="row">
- <div class="col-xs-3">
- <i class="fa fa-clock-o fa-5x"></i>
- </div>
- <div class="col-xs-9 text-right">
- <div class="huge"><?=$count?></div>
- <!--<div>Active offers</div>-->
- </div>
- </div>
- </div>
- <a href="#panel-finished-offers">
- <div class="panel-footer">
- <span class="pull-left" title="Offers that have been finished but for which no invoice has been sent yet">Finished offers</span>
- <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
- <div class="clearfix"></div>
- </div>
- </a>
- </div>
- </div>
- <div class="col-lg-3 col-md-3 col-sm-6">
- <?php
- $count = count(BusinessAdmin::getOfferIds($_pdo, array("`invoice_date` > '1970-01-01'", "`invoice_date`<=CURDATE()", "`payment_received` <= '1970-01-01' OR `payment_received` IS NULL")));
- ?>
- <div class="panel panel-<?=($count==0 ? 'primary' : 'yellow')?>">
- <div class="panel-heading">
- <div class="row">
- <div class="col-xs-3">
- <i class="fa fa-circle-o-notch fa-5x"></i>
- </div>
- <div class="col-xs-9 text-right">
- <div class="huge"><?=$count?></div>
- <!--<div>Open invoices</div>-->
- </div>
- </div>
- </div>
- <a href="#panel-open-invoices">
- <div class="panel-footer">
- <span class="pull-left">Open invoices</span>
- <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
- <div class="clearfix"></div>
- </div>
- </a>
- </div>
- </div>
- </div>
- <!-- /.row -->
- <div class="row">
- <div class="col-lg-12">
- <div class="panel panel-default" id="panel-active-offers">
- <div class="panel-heading">
- <i class="fa fa-tasks fa-fw"></i> Currently active offers
- </div>
- <!-- /.panel-heading -->
- <div class="panel-body">
- <?php
- $offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= CURDATE()"));
- $list = array();
- foreach ($offers as $offer) {
- $start = BusinessAdmin::formatDate($offer->getStartDate(), false);
- $end = BusinessAdmin::formatDate($offer->getEndDate(), false);
- $since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->getStartDate();
- $total = $offer->getEndDate() - $offer->getStartDate();
- $percentage = ($total == 0) ? 100 : round($since / $total * 100);
+ <?php require('nav.php'); ?>
- // We want to sort on percentage (DESC) and secondly end date (ASC) so start date (DESC)
- $base = str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->getStartDate();
- for ($i = 0; isset($list["$base-$i"]); $i++);
- $list["$base-$i"] = array(
- 'start' => $start,
- 'end' => $end,
- 'id' => $offer->getId(),
- 'contactClientName' => $offer->getContact()->getClient()->getName(),
- 'percentage' => $percentage,
- 'price_excl' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL),
- 'price_incl' => constants::invoice_valuta . $offer->calculate(offer::TOTAL)
- );
- }
- krsort($list, SORT_STRING);
- foreach ($list as $item) {
- echo "<p>#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT)<span class='pull-right text-muted'>{$item['percentage']}% complete</span></p>
- <div class='progress progress-striped active' style='clear:both;'>
- <div class='progress-bar progress-bar-".($item['percentage'] < 60 ? 'info' : ($item['percentage'] < 80 ? 'warning' : 'danger'))."' style='width:{$item['percentage']}%;' aria-valuemax='100' aria-valuemin='0' aria-valuenow='{$item['percentage']}' role='progressbar'></div>
- </div>";
- }
- if (count($list) == 0) {
- echo "There are no currently active offers.";
- }
- ?>
- </div>
- <!-- /.panel-body -->
- </div>
- <!-- /.panel -->
- </div>
- <div class="col-lg-6 col-md-6">
- <div class="panel panel-default" id="panel-finished-offers">
- <div class="panel-heading" title="Offers that have been finished but for which no invoice has been sent yet">
- <i class="fa fa-clock-o fa-fw"></i> Finished offers
- </div>
- <!-- /.panel-heading -->
- <div class="panel-body table-responsive">
- <table class="table table-bordered table-striped">
- <thead>
- <tr>
- <th>#</th>
- <th>Contact</th>
- <th>Offer ended</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`end_date` <= CURDATE()", "`invoice_date` IS NULL OR `invoice_date`='1970-01-01' OR `invoice_date`>CURDATE()"));
- foreach ($offers as $offer) {
- echo "<tr>";
- echo "<td>{$offer->getId()}</td>";
- echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
- echo "<td>".BusinessAdmin::formatDate($offer->getEndDate(), false)."</td>";
- echo "</tr>";
- }
- if (count($offers) == 0) {
- echo "<tr><td colspan='3'>There are no offers that need an invoice.</td></tr>";
- }
- ?>
- </tbody>
- </table>
- </div>
- <!-- /.panel-body -->
- </div>
- <!-- /.panel -->
- </div>
- <div class="col-lg-6 col-md-6">
- <div class="panel panel-default" id="panel-open-invoices">
- <div class="panel-heading">
- <i class="fa fa-circle-o-notch fa-fw"></i> Currently open invoices
- </div>
- <div class="panel-body table-responsive">
- <table class="table table-bordered table-striped">
- <thead>
- <tr>
- <th>#</th>
- <th>Contact</th>
- <th>Invoice sent</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $offers = BusinessAdmin::getOffers($_pdo, array("`invoice_date` > '1970-01-01'", "`invoice_date`<=CURDATE()", "`payment_received` <= '1970-01-01' OR `payment_received` IS NULL"));
- if (count($offers) == 0) {
- echo "<tr><td colspan='3'>There are no currently open invoices.</td></tr>";
- } else {
- foreach ($offers as $offer) {
- echo "<tr>";
- echo "<td>{$offer->getId()}</td>";
- echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
- echo "<td>".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)."</td>";
- echo "</tr>";
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- <!-- /.panel-body -->
- </div>
- <!-- /.panel -->
- </div>
- </div>
- <!-- /.row -->
- <div class="row">
- <div class="col-lg-12">
- <div class="panel panel-default" id="panel-timeline">
- <div class="panel-heading">
- <i class="fa fa-clock-o fa-fw"></i> Timeline
- </div>
- <!-- /.panel-heading -->
- <div class="panel-body">
- <ul class="timeline">
- <?php
- $offers = BusinessAdmin::getOffers($_pdo, array('`accepted`=1'));
- $list = array();
- $sort_list = array();
- foreach ($offers as $offer) {
- $temp = array(
- 'id' => $offer->getId(),
- 'contact' => $offer->getContact()->getName(),
- 'assignments' => '',
- 'assignments_header' => ''
- );
- foreach ($offer->getAssignments() as $assignment) {
- $temp['assignments'] .= "<b>{$assignment->getTitle()}</b><br/><span class='smaller'>(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getDescription()}</p>";
- $temp['assignments_header'] .= "<b>{$assignment->getTitle()}</b><br/><span class='smaller'>(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)</span><br/>";
- }
- $list[] = array_merge($temp, array('type' => 'start', 'time' => $offer->getStartDate(), 'description' => 'Offer started'));
- $sort_list[] = $offer->getStartDate() . $offer->getId() . 0;
- $list[] = array_merge($temp, array('type' => 'end', 'time' => $offer->getEndDate(), 'description' => 'Offer ended'));
- $sort_list[] = $offer->getEndDate() . $offer->getId() . 1;
- if ($offer->getInvoiceDate() > 0) {
- $list[] = array_merge($temp, array('type' => 'invoice', 'time' => $offer->getInvoiceDate(), 'description' => 'Invoice sent'));
- $sort_list[] = $offer->getInvoiceDate() . $offer->getId() . 2;
- if ($offer->getPaymentReceived() > 0) {
- $list[] = array_merge($temp, array('type' => 'payment_received', 'time' => $offer->getPaymentReceived(), 'description' => 'Payment received'));
- $sort_list[] = $offer->getPaymentReceived() . $offer->getId() . 3;
- }
- }
- }
- array_multisort($sort_list, SORT_DESC, $list);
- foreach ($list as $item) {
- if ($item['time'] > time()) {
- continue;
- }
- echo "<li" . ($item['type'] != 'start' ? ' class="timeline-inverted"' : '') . ">";
- switch ($item['type']) {
- case 'start': echo "<div class='timeline-badge info' title='{$item['description']}'><i class='fa fa-circle-o-notch'></i></div>"; break;
- case 'end': echo "<div class='timeline-badge primary' title='{$item['description']}'><i class='fa fa-circle-o'></i></div>"; break;
- case 'invoice': echo "<div class='timeline-badge warning' title='{$item['description']}'><i class='fa fa-check-circle-o'></i></div>"; break;
- case 'payment_received': echo "<div class='timeline-badge success' title='{$item['description']}'><i class='fa fa-".constants::fa_valuta."'></i></div>"; break;
- }
- echo "<div class='timeline-panel'>";
- echo "<div class='timeline-heading'><h4 class='timeline-title'>#{$item['id']} to {$item['contact']}: {$item['description']}</h4><p><small class='text-muted'><i class='fa fa-clock-o fa-fw'></i> ".BusinessAdmin::formatDate($item['time'],false,true,true)."</small></p></div>";
- switch ($item['type']) {
- case 'start': echo "<div class='timeline-body'>{$item['assignments']}</div>"; break;
- default: echo "<div class='timeline-body'>{$item['assignments_header']}</div>";
- }
- echo "</div>";
- echo "</li>";
- }
- if (count($list) == 0) {
- echo '<li class="timeline-inverted">
- <div class="timeline-badge info"><i class="fa fa-check-circle-o"></i></div>
- <div class="timeline-panel">
- <div class="timeline-heading"><h4 class="timeline-title">Welcome to BusinessAdmin!</div>
- <div class="timeline-body">When you start adding projects, a timeline will appear here.</div>
- </div>
- </li>';
- }
- ?>
- </ul>
- </div>
- <!-- /.panel-body -->
- </div>
- <!-- /.panel -->
- </div>
- </div>
- </div>
- <!-- /#page-wrapper -->
+ <div id="page-wrapper">
+ <div class="row">
+ <div class="col-lg-12">
+ <h1 class="page-header">Welcome</h1>
+ </div>
+ <!-- /.col-lg-12 -->
+ </div>
+ <!-- /.row -->
+ <div class="row">
+ <div class="col-lg-3 col-md-3 col-sm-6">
+ <?php
+ $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted` = 0")));
+ ?>
+ <div class="panel panel-<?=($count==0 ? 'primary' : 'yellow')?>">
+ <div class="panel-heading">
+ <div class="row">
+ <div class="col-xs-3">
+ <i class="fa fa-briefcase fa-5x"></i>
+ </div>
+ <div class="col-xs-9 text-right">
+ <div class="huge"><?=$count?></div>
+ <!--<div>Unaccepted offers</div>-->
+ </div>
+ </div>
+ </div>
+ <a href="<?=constants::url_external?>offers">
+ <div class="panel-footer">
+ <span class="pull-left">Unaccepted offers</span>
+ <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
+ <div class="clearfix"></div>
+ </div>
+ </a>
+ </div>
+ </div>
+ <div class="col-lg-3 col-md-3 col-sm-6">
+ <?php
+ $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= CURDATE()")));
+ ?>
+ <div class="panel panel-<?=($count==0 ? 'primary' : ($count < 3) ? 'green' : ($count < 5 ? 'yellow' : 'red'))?>">
+ <div class="panel-heading">
+ <div class="row">
+ <div class="col-xs-3">
+ <i class="fa fa-tasks fa-5x"></i>
+ </div>
+ <div class="col-xs-9 text-right">
+ <div class="huge"><?=$count?></div>
+ <!--<div>Active offers</div>-->
+ </div>
+ </div>
+ </div>
+ <a href="#panel-active-offers">
+ <div class="panel-footer">
+ <span class="pull-left">Active offers</span>
+ <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
+ <div class="clearfix"></div>
+ </div>
+ </a>
+ </div>
+ </div>
+ <div class="col-lg-3 col-md-3 col-sm-6">
+ <?php
+ $count = count(BusinessAdmin::getOfferIds($_pdo, array("`accepted`=1", "`end_date` <= CURDATE()", "`invoice_date` IS NULL OR `invoice_date`='1970-01-01' OR `invoice_date`>CURDATE()")));
+ ?>
+ <div class="panel panel-<?=($count==0 ? 'primary' : ($count < 3) ? 'green' : ($count < 5 ? 'yellow' : 'red'))?>">
+ <div class="panel-heading">
+ <div class="row">
+ <div class="col-xs-3">
+ <i class="fa fa-clock-o fa-5x"></i>
+ </div>
+ <div class="col-xs-9 text-right">
+ <div class="huge"><?=$count?></div>
+ <!--<div>Active offers</div>-->
+ </div>
+ </div>
+ </div>
+ <a href="#panel-finished-offers">
+ <div class="panel-footer">
+ <span class="pull-left" title="Offers that have been finished but for which no invoice has been sent yet">Finished offers</span>
+ <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
+ <div class="clearfix"></div>
+ </div>
+ </a>
+ </div>
+ </div>
+ <div class="col-lg-3 col-md-3 col-sm-6">
+ <?php
+ $count = count(BusinessAdmin::getOfferIds($_pdo, array(
+ "`invoice_date` > '1970-01-01'",
+ "`invoice_date`<=CURDATE()",
+ "NOT EXISTS (SELECT * FROM `".constants::db_prefix."payment` WHERE `offerId`=`".constants::db_prefix."offer`.`id` AND `date` != TIMESTAMP(0))")));
+ ?>
+ <div class="panel panel-<?=($count==0 ? 'primary' : 'yellow')?>">
+ <div class="panel-heading">
+ <div class="row">
+ <div class="col-xs-3">
+ <i class="fa fa-circle-o-notch fa-5x"></i>
+ </div>
+ <div class="col-xs-9 text-right">
+ <div class="huge"><?=$count?></div>
+ <!--<div>Open invoices</div>-->
+ </div>
+ </div>
+ </div>
+ <a href="#panel-open-invoices">
+ <div class="panel-footer">
+ <span class="pull-left">Open invoices</span>
+ <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
+ <div class="clearfix"></div>
+ </div>
+ </a>
+ </div>
+ </div>
+ </div>
+ <!-- /.row -->
+ <div class="row">
+ <div class="col-lg-12">
+ <div class="panel panel-default" id="panel-active-offers">
+ <div class="panel-heading">
+ <i class="fa fa-tasks fa-fw"></i> Currently active offers
+ </div>
+ <!-- /.panel-heading -->
+ <div class="panel-body">
+ <?php
+ $offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= CURDATE()"));
+ $list = array();
+ foreach ($offers as $offer) {
+ $start = BusinessAdmin::formatDate($offer->getStartDate(), false);
+ $end = BusinessAdmin::formatDate($offer->getEndDate(), false);
+ $since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->getStartDate();
+ $total = $offer->getEndDate() - $offer->getStartDate();
+ $percentage = ($total == 0) ? 100 : round($since / $total * 100);
+
+ // We want to sort on percentage (DESC) and secondly end date (ASC) so start date (DESC)
+ $base = str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->getStartDate();
+ for ($i = 0; isset($list["$base-$i"]); $i++);
+ $list["$base-$i"] = array(
+ 'start' => $start,
+ 'end' => $end,
+ 'id' => $offer->getId(),
+ 'contactClientName' => $offer->getContact()->getClient()->getName(),
+ 'percentage' => $percentage,
+ 'price_excl' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL),
+ 'price_incl' => constants::invoice_valuta . $offer->calculate(offer::TOTAL)
+ );
+ }
+ krsort($list, SORT_STRING);
+ foreach ($list as $item) {
+ echo "<p>#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT)<span class='pull-right text-muted'>{$item['percentage']}% complete</span></p>
+ <div class='progress progress-striped active' style='clear:both;'>
+ <div class='progress-bar progress-bar-".($item['percentage'] < 60 ? 'info' : ($item['percentage'] < 80 ? 'warning' : 'danger'))."' style='width:{$item['percentage']}%;' aria-valuemax='100' aria-valuemin='0' aria-valuenow='{$item['percentage']}' role='progressbar'></div>
+ </div>";
+ }
+ if (count($list) == 0) {
+ echo "There are no currently active offers.";
+ }
+ ?>
+ </div>
+ <!-- /.panel-body -->
+ </div>
+ <!-- /.panel -->
+ </div>
+ <div class="col-lg-6 col-md-6">
+ <div class="panel panel-default" id="panel-finished-offers">
+ <div class="panel-heading" title="Offers that have been finished but for which no invoice has been sent yet">
+ <i class="fa fa-clock-o fa-fw"></i> Finished offers
+ </div>
+ <!-- /.panel-heading -->
+ <div class="panel-body table-responsive">
+ <table class="table table-bordered table-striped">
+ <thead>
+ <tr>
+ <th>#</th>
+ <th>Contact</th>
+ <th>Offer ended</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php
+ $offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`end_date` <= CURDATE()", "`invoice_date` IS NULL OR `invoice_date`='1970-01-01' OR `invoice_date`>CURDATE()"));
+ foreach ($offers as $offer) {
+ echo "<tr>";
+ echo "<td>{$offer->getId()}</td>";
+ echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
+ echo "<td>".BusinessAdmin::formatDate($offer->getEndDate(), false)."</td>";
+ echo "</tr>";
+ }
+ if (count($offers) == 0) {
+ echo "<tr><td colspan='3'>There are no offers that need an invoice.</td></tr>";
+ }
+ ?>
+ </tbody>
+ </table>
+ </div>
+ <!-- /.panel-body -->
+ </div>
+ <!-- /.panel -->
+ </div>
+ <div class="col-lg-6 col-md-6">
+ <div class="panel panel-default" id="panel-open-invoices">
+ <div class="panel-heading">
+ <i class="fa fa-circle-o-notch fa-fw"></i> Currently open invoices
+ </div>
+ <div class="panel-body table-responsive">
+ <table class="table table-bordered table-striped">
+ <thead>
+ <tr>
+ <th>#</th>
+ <th>Contact</th>
+ <th>Invoice sent</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php
+ $offers = BusinessAdmin::getOffers($_pdo, array(
+ "`invoice_date` > '1970-01-01'",
+ "`invoice_date`<=CURDATE()",
+ "NOT EXISTS (SELECT * FROM `".constants::db_prefix."payment` WHERE `offerId`=`".constants::db_prefix."offer`.`id` AND `date` != TIMESTAMP(0))"));
+ if (count($offers) == 0) {
+ echo "<tr><td colspan='3'>There are no currently open invoices.</td></tr>";
+ } else {
+ foreach ($offers as $offer) {
+ echo "<tr>";
+ echo "<td>{$offer->getId()}</td>";
+ echo "<td>{$offer->getContact()->getClient()->getName()}</td>";
+ echo "<td>".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)."</td>";
+ echo "</tr>";
+ }
+ }
+ ?>
+ </tbody>
+ </table>
+ </div>
+ <!-- /.panel-body -->
+ </div>
+ <!-- /.panel -->
+ </div>
+ </div>
+ <!-- /.row -->
+ <div class="row">
+ <div class="col-lg-12">
+ <div class="panel panel-default" id="panel-timeline">
+ <div class="panel-heading">
+ <i class="fa fa-clock-o fa-fw"></i> Timeline
+ </div>
+ <!-- /.panel-heading -->
+ <div class="panel-body">
+ <ul class="timeline">
+ <?php
+ $offers = BusinessAdmin::getOffers($_pdo, array('`accepted`=1'));
+ $list = array();
+ $sort_list = array();
+ foreach ($offers as $offer) {
+ $temp = array(
+ 'id' => $offer->getId(),
+ 'contact' => $offer->getContact()->getName(),
+ 'assignments' => '',
+ 'assignments_header' => ''
+ );
+ foreach ($offer->getAssignments() as $assignment) {
+ $temp['assignments'] .= "<b>{$assignment->getTitle()}</b><br/><span class='smaller'>(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getDescription()}</p>";
+ $temp['assignments_header'] .= "<b>{$assignment->getTitle()}</b><br/><span class='smaller'>(".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)</span><br/>";
+ }
+ $list[] = array_merge($temp, array('type' => 'start', 'time' => $offer->getStartDate(), 'description' => 'Offer started'));
+ $sort_list[] = $offer->getStartDate() . $offer->getId() . 0;
+ $list[] = array_merge($temp, array('type' => 'end', 'time' => $offer->getEndDate(), 'description' => 'Offer ended'));
+ $sort_list[] = $offer->getEndDate() . $offer->getId() . 1;
+ if ($offer->getInvoiceDate() > 0) {
+ $list[] = array_merge($temp, array('type' => 'invoice', 'time' => $offer->getInvoiceDate(), 'description' => 'Invoice sent'));
+ $sort_list[] = $offer->getInvoiceDate() . $offer->getId() . 2;
+ if ($offer->getPaymentReceived() > 0) {
+ $list[] = array_merge($temp, array('type' => 'payment_received', 'time' => $offer->getPaymentReceived(), 'description' => 'Payment received'));
+ $sort_list[] = $offer->getPaymentReceived() . $offer->getId() . 3;
+ }
+ }
+ }
+ array_multisort($sort_list, SORT_DESC, $list);
+ foreach ($list as $item) {
+ if ($item['time'] > time()) {
+ continue;
+ }
+ echo "<li" . ($item['type'] != 'start' ? ' class="timeline-inverted"' : '') . ">";
+ switch ($item['type']) {
+ case 'start': echo "<div class='timeline-badge info' title='{$item['description']}'><i class='fa fa-circle-o-notch'></i></div>"; break;
+ case 'end': echo "<div class='timeline-badge primary' title='{$item['description']}'><i class='fa fa-circle-o'></i></div>"; break;
+ case 'invoice': echo "<div class='timeline-badge warning' title='{$item['description']}'><i class='fa fa-check-circle-o'></i></div>"; break;
+ case 'payment_received': echo "<div class='timeline-badge success' title='{$item['description']}'><i class='fa fa-".constants::fa_valuta."'></i></div>"; break;
+ }
+ echo "<div class='timeline-panel'>";
+ echo "<div class='timeline-heading'><h4 class='timeline-title'>#{$item['id']} to {$item['contact']}: {$item['description']}</h4><p><small class='text-muted'><i class='fa fa-clock-o fa-fw'></i> ".BusinessAdmin::formatDate($item['time'],false,true,true)."</small></p></div>";
+ switch ($item['type']) {
+ case 'start': echo "<div class='timeline-body'>{$item['assignments']}</div>"; break;
+ default: echo "<div class='timeline-body'>{$item['assignments_header']}</div>";
+ }
+ echo "</div>";
+ echo "</li>";
+ }
+ if (count($list) == 0) {
+ echo '<li class="timeline-inverted">
+ <div class="timeline-badge info"><i class="fa fa-check-circle-o"></i></div>
+ <div class="timeline-panel">
+ <div class="timeline-heading"><h4 class="timeline-title">Welcome to BusinessAdmin!</div>
+ <div class="timeline-body">When you start adding projects, a timeline will appear here.</div>
+ </div>
+ </li>';
+ }
+ ?>
+ </ul>
+ </div>
+ <!-- /.panel-body -->
+ </div>
+ <!-- /.panel -->
+ </div>
+ </div>
+ </div>
+ <!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
diff --git a/include/offers-edit.php b/include/offers-edit.php
index 95de9b3..5ed95c4 100644
--- a/include/offers-edit.php
+++ b/include/offers-edit.php
@@ -38,7 +38,12 @@ try {
$response->success = $offer->setInvoiceDate(strtotime($_REQUEST['value']));
break;
case 'payment_received':
- $response->success = $offer->setPaymentReceived(strtotime($_REQUEST['value']));
+ $payment = $offer->getPayment();
+ if (is_null($payment)) {
+ $response->success = $offer->createPayment(strtotime($_REQUEST['value']));
+ } else {
+ $response->success = $payment->setDate(strtotime($_REQUEST['value']));
+ }
break;
default:
$response->http_response_code(404);
diff --git a/install/index.php b/install/index.php
index 213eec7..f565696 100644
--- a/install/index.php
+++ b/install/index.php
@@ -82,13 +82,19 @@ if (isset($_GET['create_tables'])) {
`invoice_date` date NOT NULL,
`accepted` tinyint(1) unsigned NOT NULL DEFAULT '0',
`invoice_fileId` smallint(5) unsigned DEFAULT NULL,
- `payment_received` date DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `invoice_fileId` (`invoice_fileId`),
KEY `contactId` (`contactId`),
KEY `contactId_2` (`contactId`),
KEY `invoice_fileId_2` (`invoice_fileId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1");
+
+ $_pdo->query("CREATE TABLE IF NOT EXISTS `payment` (
+ `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
+ `offerId` smallint(5) unsigned NOT NULL,
+ `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (`id`)
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1;");
$_pdo->query("CREATE TABLE IF NOT EXISTS `".constants::db_prefix."user` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
@@ -110,6 +116,9 @@ if (isset($_GET['create_tables'])) {
ADD CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`invoice_fileId`) REFERENCES `".constants::db_prefix."file` (`id`),
ADD CONSTRAINT `offer_ibfk_2` FOREIGN KEY (`contactId`) REFERENCES `".constants::db_prefix."contact` (`id`)");
+ $_pdo->query("ALTER TABLE `payment`
+ ADD CONSTRAINT `payment_ibfk_1` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`);");
+
echo "Succeeded creating the database tables.";
} catch (PDOException $e) {
diff --git a/install/upgrade.php b/install/upgrade.php
index e145ba7..78f5dff 100644
--- a/install/upgrade.php
+++ b/install/upgrade.php
@@ -86,6 +86,28 @@ if (isset($_GET['upgrade'])) {
}
}
+ if (lower_version($_GET['upgrade'], '0.4.2')) {
+ try {
+ $_pdo->query("CREATE TABLE IF NOT EXISTS `".constants::db_prefix."payment` (
+ `id` smallint(5) unsigned NOT NULL,
+ `offerId` smallint(5) unsigned NOT NULL,
+ `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
+ ) ENGINE=InnoDB DEFAULT CHARSET=latin1;");
+
+ $offers = $_pdo->query("SELECT `id`,`payment_received` FROM `".constants::db_prefix."offer` WHERE `payment_received` IS NOT NULL");
+ $offers = $offers->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($offers as $offer) {
+ $received = $offer['payment_received'];
+ $offer = new offer($_pdo, $offer['id']);
+ $offer->createPayment($received);
+ }
+
+ $_pdo->query("ALTER TABLE `".constants::db_prefix."offer` DROP `payment_received`;");
+ } catch (PDOException $e) {
+ echo "Altering the database structure failed with a PDOException ({$e->getCode()}): {$e->getMessage()}<br/>" . $e->getTraceAsString();
+ }
+ }
+
echo "<br/>All done.";
}
?>