-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-myorders.php
More file actions
94 lines (86 loc) · 2.72 KB
/
Copy pathworker-myorders.php
File metadata and controls
94 lines (86 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require 'user.php';
CheckIfWorkerLoggedIn();
function MyOrderNotShippedYet(){
require 'worker-common.php';
// Find all orders that I own that are not yet shipped
$sql = 'SELECT * FROM orders WHERE workerID =:id AND shipped = 0';
commonWorkerSearch($sql,1);
}
function SendOrder(){
// Mark order as shipped
require 'db.php';
try{
$db->beginTransaction();
// Get all orderlines
$db->exec('LOCK TABLES orderdetail WRITE');
$sql = 'select lineID,productID,qty from orderdetail where orderID=:id';
$sthLine = $db->prepare ($sql);
$sthLine->bindParam (':id', $_POST['order']);
$sthLine->execute ();
while($row = $sthLine->fetch()){
// Get old amount
$db->exec('LOCK TABLES products WRITE');
$sql = 'select onStock from products where id=:id';
$tmpProd = $db->prepare ($sql);
$tmpProd->bindParam (':id', $row['productID']);
$tmpProd->execute ();
$tmpRow = $tmpProd->fetch();
// If not enough in stock, then rollback
if($tmpRow['onStock'] < $row['qty']){
$db->rollBack();
throw new Exception('Not enough in stock');
}
// Update amount of each product in stock
$db->exec('LOCK TABLES products WRITE');
$sql = 'UPDATE products set onStock=:qtyLeft where id=:productID';
$updateSTH = $db->prepare ($sql);
$updateSTH->bindParam (':productID', $row['productID']);
$left = intval($tmpRow['onStock']) - intval($row['qty']);
$updateSTH->bindParam (':qtyLeft',$left);
$updateSTH->execute ();
// Mark orderline as sent
$sql = 'Update orderdetail set sendt=1 where lineID=:lineID';
$tmpSTH = $db->prepare ($sql);
$tmpSTH->bindParam (':lineID', $row['lineID']);
$db->exec('LOCK TABLES orderdetail WRITE');
$tmpSTH->execute ();
}
// Mark order as shipped
$db->exec('LOCK TABLES orders WRITE');
$sql = 'UPDATE orders set shipped=1 where id=:orderID';
$sthOrder = $db->prepare ($sql);
$sthOrder->bindParam (':orderID', $_POST['order']);
$sthOrder->execute();
$rowsChanged = $sthOrder->rowCount();
if($rowsChanged === 0){
$db->rollBack();
throw new Exception('Order not found');
}
$db->commit();
header( 'Location: worker-myorders.php' );
}
catch(Exception $e){
return $e->getMessage();
$db->rollBack();
$db->query ('UNLOCK TABLES');
throw new Exception('Something whent wrong, Not enough in stock');
}
}
?>
<?php
include 'Geeksforsaletop.php';
?>
<div id="content">
<?php include 'worker-buttons.php'; ?>
<?php
echo "<p>My orders not shipped";
echo "</br>";
if (isset ($_POST['order'])) {
echo SendOrder();
}
MyOrderNotShippedYet();
?>
</div>
</BODY>
</HTML>