aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarf20 <aruizfernandez05@gmail.com>2024-03-24 16:59:57 +0100
committerarf20 <aruizfernandez05@gmail.com>2024-03-24 16:59:57 +0100
commit94b5e9d37cbc5e030f9fbba865b7205b872be365 (patch)
treec9498a3fb7485f14bd550bbdd897d278c3247246
parent8a1f665f1ff9c5084b9c6372d21b49e8a36713b3 (diff)
downloadarfnet2-cstims-94b5e9d37cbc5e030f9fbba865b7205b872be365.tar.gz
arfnet2-cstims-94b5e9d37cbc5e030f9fbba865b7205b872be365.zip
Manage invoices and generate manual invoice
-rw-r--r--makeinvoices.php95
-rw-r--r--manageinvoices.php27
2 files changed, 100 insertions, 22 deletions
diff --git a/makeinvoices.php b/makeinvoices.php
index a12ef5c..55a4a64 100644
--- a/makeinvoices.php
+++ b/makeinvoices.php
@@ -87,23 +87,60 @@ $services = $result->fetch_all(MYSQLI_ASSOC);
$fdom = new DateTime('first day of this month');
$ldom = new DateTime('last day of this month');
-foreach ($clients as $client) {
- $ret = generate_pdf($client, array_filter($dueorders, function($e) { global $client; return $e["client"] == $client["id"]; }));
+// POST actions
+if ($_SERVER["REQUEST_METHOD"] == "POST") {
+ // add entry
+ if (isset($_POST["generate"])) {
+ $ret = generate_pdf(getclientbyid($_POST["client"]), array(getorderbyid($_POST["order"])), $_POST["desc"], $_POST["qty"]);
+
+ $sql = "INSERT INTO invoices (client, `desc`, amount, pdf) VALUES (?, ?, ?, ?)";
+ $stmt = mysqli_prepare($link, $sql);
+ mysqli_stmt_bind_param($stmt, "ssss", $param_client, $param_desc, $param_amount, $param_pdf);
+ $param_client = $_POST["client"];
+ $param_desc = $_POST["desc"];
+ $param_amount = $ret[1];
+ $param_pdf = $ret[0];
+
+ if (!mysqli_stmt_execute($stmt) || (mysqli_stmt_affected_rows($stmt) != 1)) {
+ echo "SQL error.";
+ } else {
+ echo $_POST["client"]." ok ".$ret[1]."\n";
+ header("location: /manageinvoices.php");
+ }
+ }
+}
- $sql = "INSERT INTO invoices (client, `desc`, amount, pdf) VALUES (?, ?, ?, ?)";
- $stmt = mysqli_prepare($link, $sql);
- mysqli_stmt_bind_param($stmt, "ssss", $param_client, $param_desc, $param_amount, $param_pdf);
- $param_client = $client["id"];
- $param_desc = "Monthly invoice";
- $param_amount = $ret[1];
- $param_pdf = $ret[0];
-
- if (!mysqli_stmt_execute($stmt) || (mysqli_stmt_affected_rows($stmt) != 1)) {
- echo "SQL error.";
- } else echo $client["id"]." ok ".$ret[1]."\n";
+if ($_SERVER["REQUEST_METHOD"] == "GET") {
+ foreach ($clients as $client) {
+ $ret = generate_pdf($client, array_filter($dueorders, function($e) { global $client; return $e["client"] == $client["id"]; }));
+
+ $sql = "INSERT INTO invoices (client, `desc`, amount, pdf) VALUES (?, ?, ?, ?)";
+ $stmt = mysqli_prepare($link, $sql);
+ mysqli_stmt_bind_param($stmt, "ssss", $param_client, $param_desc, $param_amount, $param_pdf);
+ $param_client = $client["id"];
+ $param_desc = "Monthly invoice";
+ $param_amount = $ret[1];
+ $param_pdf = $ret[0];
+
+ if (!mysqli_stmt_execute($stmt) || (mysqli_stmt_affected_rows($stmt) != 1)) {
+ echo "SQL error.";
+ } else {
+ echo $client["id"]." ok ".$ret[1]."\n";
+ header("location: /manageinvoices.php");
+ }
+ }
}
+function getorderbyid($id) {
+ global $dueorders;
+ foreach ($dueorders as $order) {
+ if ($order["id"] == $id) {
+ return $order;
+ }
+ }
+}
+
function getservicebyid($id) {
global $services;
foreach ($services as $service) {
@@ -113,8 +150,17 @@ function getservicebyid($id) {
}
}
+function getclientbyid($id) {
+ global $clients;
+ foreach ($clients as $client) {
+ if ($client["id"] == $id) {
+ return $client;
+ }
+ }
+}
+
-function generate_pdf($client, $dueorders) {
+function generate_pdf($client, $dueorders, $desc = null, $manualqty = null) {
global $link, $fdom, $ldom;
// get next invoice id
$sql = "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'invoices'";
@@ -158,7 +204,7 @@ function generate_pdf($client, $dueorders) {
$txt =
"Invoice ID: $nextid\n"
."Invoice date: ".date("l, F j\t\h, Y\n")
- ."Due date: ".date("l, F j\t\h, Y\n");
+ ."Due date: ".date("l, F j\t\h, Y\n\n");
$pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
$pdf->SetFont('helvetica', 'B', 20);
@@ -179,14 +225,21 @@ function generate_pdf($client, $dueorders) {
$price = (float)trim(substr($dueorder["billing"], 0, strpos($dueorder["billing"], "€"))) / (float)(30*24);
$pricestr = number_format($price, 4, '.', '')." €/h";
- $dueorderdate = new DateTime($dueorder["date"]);
- $billingperiodstart = $dueorderdate > $fdom ? $dueorderdate : $fdom;
- $billingperiod = $billingperiodstart->format("d-m-Y")." to ".$ldom->format("d-m-Y");
- $billinginterval = date_diff($billingperiodstart, $ldom);
- $qty = ($billinginterval->d * 24) + $billinginterval->h;
+ if (!isset($manualqty)) {
+ $dueorderdate = new DateTime($dueorder["date"]);
+ $billingperiodstart = $dueorderdate > $fdom ? $dueorderdate : $fdom;
+ $billingperiod = $billingperiodstart->format("d-m-Y")." to ".$ldom->format("d-m-Y");
+ $billinginterval = date_diff($billingperiodstart, $ldom);
+ $qty = ($billinginterval->d * 24) + $billinginterval->h;
+ } else {
+ $billingperiodstart = date("d-m-Y");
+ $billingperiodend = (new DateTime())->add(new DateInterval("PT".$manualqty."H"));
+ $billingperiod = $billingperiodstart." to ".$billingperiodend->format("d-m-Y");
+ $qty = $manualqty;
+ }
+
$amount = $price*$qty;
$subtotal += $amount;
-
$amountstr = number_format($amount, 2, '.', '')." €";
$tdata[] = array($dueorder["id"], $dueorder["name"]." ($billingperiod)", getservicebyid($dueorder["service"])["name"], $pricestr." (".$dueorder["billing"].")", $qty, $amountstr);
}
diff --git a/manageinvoices.php b/manageinvoices.php
index 4e4e9a6..56f2490 100644
--- a/manageinvoices.php
+++ b/manageinvoices.php
@@ -28,6 +28,13 @@ mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$invoices = $result->fetch_all(MYSQLI_ASSOC);
+// Get orders
+$sql = "SELECT id, service, name, client, date, billing, status, comments FROM orders";
+$stmt = mysqli_prepare($link, $sql);
+mysqli_stmt_execute($stmt);
+$result = mysqli_stmt_get_result($stmt);
+$orders = $result->fetch_all(MYSQLI_ASSOC);
+
// GET actions
// delete entry
if (isset($_GET["del"])) {
@@ -128,6 +135,24 @@ function getinvoicebyid($id) {
<h3>Orders</h3>
<?php
+ if (isset($_GET["add"])) {
+ $client_options = $service_options = "";
+ foreach ($clients as $client)
+ $client_options .= "<option value=\"".$client["id"]."\">".$client["username"]."</option>";
+ foreach ($orders as $order)
+ //if ($order["client"] == )
+ $order_options .= "<option value=\"".$order["id"]."\">".$order["name"]."</option>";
+ echo "<div class=\"form\"><h3>Generate invoice</h3><form action=\"/makeinvoices.php\" method=\"post\">\n"
+ ."<label><b>Client</b></label><br><select name=\"client\">".$client_options."</select><br>\n"
+ ."<label><b>Order</b></label><br><select name=\"order\">".$order_options."</select><br>\n"
+ ."<label><b>Description</b></label><br><input type=\"text\" name=\"desc\"><br>\n"
+ ."<label><b>Quantity (hours)</b></label><br><input type=\"text\" name=\"qty\"><br>\n"
+ ."<label><b>Status</b></label><br><select name=\"status\"><option value=\"paid\">paid</option><option value=\"unpaid\">unpaid</option></select><br>\n"
+ ."<input type=\"hidden\" name=\"id\" value=\"".$invoice["id"]."\">"
+ ."<br><input type=\"submit\" name=\"generate\" value=\"Generate\"><a href=\"".$_SERVER['SCRIPT_NAME']."\">cancel</a>"
+ ."</form></div>";
+ }
+
if (isset($_GET["edit"])) {
$invoice = getinvoicebyid($_GET["edit"]);
$client_options = $service_options = "";
@@ -143,7 +168,7 @@ function getinvoicebyid($id) {
}
?>
- <a href="?add">add</a>
+ <a href="?add">manual invoice</a>
<table>
<tr><th>id</th><th>client</th><th>description</th><th>amount</th><th>date</th><th>pdf</th><th>status</th><th>action</th></tr>
<?php