Skip to content

Commit c39c7f0

Browse files
Version 2.3 get-data & transaction-add bug fix
1 parent 5a47d14 commit c39c7f0

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

functions/data/get-data.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,64 @@ function customer_info($id){
4343
return $results[0] ?? '';
4444
}
4545

46+
function total_price($id){
47+
global $db;
48+
$sql = "SELECT SUM(CASE
49+
WHEN r.type = 'day' THEN co.priceDay
50+
WHEN r.type = 'night' THEN co.priceNight
51+
ELSE 0
52+
END) AS total,
53+
c.fullname
54+
FROM transactions t
55+
JOIN rentals r ON t.id = r.transact_id
56+
JOIN customers c ON t.customer_id = c.id
57+
JOIN cottages co ON r.cottage_id = co.id
58+
WHERE t.user_id = :id AND t.status = 'Pending';";
59+
$statement = $db->prepare($sql);
60+
$statement->bindParam(':id', $id);
61+
$statement->execute();
62+
$result = $statement->fetch();
63+
return number_format($result['total'], 2);
64+
}
65+
66+
function total_cottage($id){
67+
global $db;
68+
$sql = "SELECT COUNT(r.id) AS total,
69+
c.fullname
70+
FROM transactions t
71+
JOIN rentals r ON t.id = r.transact_id
72+
JOIN customers c ON t.customer_id = c.id
73+
JOIN cottages co ON r.cottage_id = co.id
74+
WHERE t.user_id = :id AND t.status = 'Pending';";
75+
$statement = $db->prepare($sql);
76+
$statement->bindParam(':id', $id);
77+
$statement->execute();
78+
$result = $statement->fetch();
79+
return $result['total'];
80+
}
81+
82+
function get_sales($period = 'monthly') {
83+
global $db;
84+
85+
$sql = "SELECT SUM(CASE
86+
WHEN r.type = 'day' THEN co.priceDay
87+
WHEN r.type = 'night' THEN co.priceNight
88+
ELSE 0
89+
END) AS total
90+
FROM transactions t
91+
JOIN rentals r ON t.id = r.transact_id
92+
JOIN cottages co ON r.cottage_id = co.id
93+
WHERE t.status = 'Pending'";
94+
95+
if ($period === 'monthly') {
96+
$sql .= " AND DATE_FORMAT(t.created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m')";
97+
} elseif ($period === 'annual') {
98+
$sql .= " AND DATE_FORMAT(t.created_at, '%Y') = DATE_FORMAT(CURRENT_DATE, '%Y')";
99+
}
100+
101+
$statement = $db->prepare($sql);
102+
$statement->execute();
103+
$result = $statement->fetch();
104+
105+
return number_format($result['total'], 2);
106+
}

functions/transaction-add.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
$stmt->bindParam(':user_id', $user_id);
2020
$stmt->execute();
2121
$transaction = $stmt->fetch(PDO::FETCH_ASSOC);
22-
22+
23+
if (!$transaction) {
24+
header('Location: ../rent.php?type=error&message=Please create a transaction first');
25+
exit();
26+
}
27+
2328
$transaction_id = $transaction['id'];
2429
$customer_id = $transaction['customer_id'];
2530

rent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<div class="row align-items-center no-gutters">
5454
<div class="col me-2">
5555
<div class="text-uppercase text-primary fw-bold text-xs mb-1"><span>TOTAL Price</span></div>
56-
<div class="text-dark fw-bold h5 mb-0"><span>&lt;price&gt;</span></div>
56+
<div class="text-dark fw-bold h5 mb-0"><span><?php echo total_price($_SESSION['id'])?? '0'; ?></span></div>
5757
</div>
5858
<div class="col-auto"><i class="fas fa-credit-card fa-2x text-gray-300"></i></div>
5959
</div>
@@ -66,7 +66,7 @@
6666
<div class="row align-items-center no-gutters">
6767
<div class="col me-2">
6868
<div class="text-uppercase text-primary fw-bold text-xs mb-1"><span>TOTAL COTTAGE</span></div>
69-
<div class="text-dark fw-bold h5 mb-0"><span>&lt;total&gt;</span></div>
69+
<div class="text-dark fw-bold h5 mb-0"><span><?php echo total_cottage($_SESSION['id'])?? '0'; ?></span></div>
7070
</div>
7171
<div class="col-auto"><i class="fas fa-warehouse fa-2x text-gray-300"></i></div>
7272
</div>

0 commit comments

Comments
 (0)