/
response.php
338 lines (314 loc) · 10.9 KB
/
response.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
require './Smsgh/Api.php';
if(!isset($_REQUEST['cmd'])){
echo '{"result":0,message:"unknown command"}';
exit();
}
$cmd=$_REQUEST['cmd'];
switch($cmd)
{
case 1: //allows owner to add product to inventory
add();
break;
case 2: //view all products
viewAll();
break;
case 3: //update a product
update();
break;
case 4: //get details of a product
getProduct();
break;
case 5: //sends sms to get stock level
getStock();
break;
case 6: //generate random id for transaction
generate_random_password(5);
break;
case 7: //add sale to db
addSale();
break;
case 8: //all transaction to db
addTransaction();
break;
case 9: //get all purchases with a given transaction id
displaySale();
break;
case 10: //set the total value of a transaction
setTotal();
break;
case 11: //display all transactions
allTransaction();
break;
case 12:
deleteSale();
break;
case 13:
editSale();
break;
default:
echo '{"result":0,"message":"unknown command"}';
break;
}
function add(){
$pid = $_REQUEST["pid"];
$pname = $_REQUEST["pname"];
$price = $_REQUEST["price"];
$qty = $_REQUEST["qty"];
include("product.php");
$obj=new product();
if($obj->addProduct($pid, $pname,$price,$qty)) {
echo '{"result":1}';
}
else {
echo "{'result':0}";
}
}
function update(){
$pid = $_REQUEST["pid"];
$pname = $_REQUEST["pname"];
$price = $_REQUEST["price"];
$qty = $_REQUEST["qty"];
include("product.php");
$obj=new product();
if($obj->editProduct($pid, $pname,$price,$qty)) {
echo '{"result":1}';
}
else {
echo "{'result':0}";
}
}
function getProduct(){
$pid = $_REQUEST["pid"];
include("product.php");
$obj=new product();
if(!$obj->displayProduct($pid)) {
echo '{"result":0}';
}
else {
$row=$obj->fetch();
echo '{"result":1,"product":[';
echo json_encode($row);
echo "]}";
}
}
function addSale(){
$sid = $_REQUEST["sid"];
$pname = $_REQUEST["pname"];
$price = $_REQUEST["price"];
$qty = $_REQUEST["qty"];
$pid = $_REQUEST["pid"];
include_once("sales.php");
include_once("product.php");
$obj=new sales();
$obj2=new product();
if($obj->addSales($sid, $pname,$price,$qty)) {
if($obj2->decrementQty($pid, $qty)){
echo '{"result":1}'; }
}
else {
echo "{'result':0}";
}
}
function addTransaction(){
$s = $_REQUEST["sid"];
$p = $_REQUEST["pnum"];
$d = $_REQUEST["date"];
$t = $_REQUEST["time"];
$tt = $_REQUEST["total"];
include("trans.php");
$obj=new trans();
if($obj->addTrans($s,$d,$t,$p,$tt)) {
if($obj->checkTotal($s)){
$discount = random_password(8);
$row=$obj->fetch();
$msg = "Your Discount Code is: ".$discount;
$pnum = $row['pnumber'];
}
echo '{"result":1}';
sms($msg,$pnum);
}
else {
echo '{"result":0}';
}
}
function displaySale(){
$p = $_REQUEST["sid"];
include("sales.php");
$obj=new sales();
if(!$obj->displaySale($p)) {
echo '{"result":0}';
}
else {
$row=$obj->fetch();
echo '{"result":1,"sale":[';
while($row){
echo json_encode($row); //convert the result array to json object
$row=$obj->fetch();
if($row){
echo ","; //if there are more rows, add comma
}
}
echo "]}";
}
}
function setTotal(){
$p = $_REQUEST["sid"];
$t = $_REQUEST["total"];
include("trans.php");
$obj=new trans();
if(!$obj->setTotal($p, $t)) {
echo '{"result":0}';
}
// $obj->setTotal($p, $t);
$discount = random_password(8);
if(!$obj->checkTotal($p)){
//return error
echo '{"result":0,"message": "did not work."}';
return;
}
$row=$obj->fetch();
$msg = "Your Discount Code is: ".$discount;
$pnum = $row['pnumber'];
sms($msg,$pnum);
echo '{"result":1}';
}
function generate_random_password($length = 10) {
$alphabets = range('A','Z');
$numbers = range('0','9');
$additional_characters = array('_','.');
$final_array = array_merge($alphabets,$numbers,$additional_characters);
$password = '';
while($length--) {
$key = array_rand($final_array);
$password .= $final_array[$key];
}
echo '{"result":1,"pass":"' .$password . '"}';
// return $password;
}
function random_password($length = 10) {
$alphabets = range('A','Z');
$numbers = range('0','9');
$additional_characters = array('_','.');
$final_array = array_merge($alphabets,$numbers,$additional_characters);
$password = '';
while($length--) {
$key = array_rand($final_array);
$password .= $final_array[$key];
}
return $password;
}
function sms($msg, $num){
//Here we assume the user is using the combination of his clientId and clientSecret as credentials
$auth = new BasicAuth("igkoydll", "dngiqlfo");
// instance of ApiHost
$apiHost = new ApiHost($auth);
$enableConsoleLog = FALSE;
$messagingApi = new MessagingApi($apiHost, $enableConsoleLog);
try {
$messageResponse = $messagingApi->sendQuickMessage("MyStore", $num, $msg);
if ($messageResponse instanceof MessageResponse) {
//echo
$messageResponse->getStatus();
} elseif ($messageResponse instanceof HttpResponse) {
//echo "\nServer Response Status : " .
$messageResponse->getStatus();
}
} catch (Exception $ex) {
//echo
$ex->getTraceAsString();
}
}
function viewAll(){
include("product.php");
$obj = new product();
if(!$obj->displayAll()){
//return error
echo '{"result":0,"message": "search did not work."}';
return;
}
//at this point the search has been successful.
//generate the JSON message to echo to the browser
$row=$obj->fetch();
echo '{"result":1,"products":['; //start of json object
while($row){
echo json_encode($row); //convert the result array to json object
$row=$obj->fetch();
if($row){
echo ","; //if there are more rows, add comma
}
}
echo "]}";
}
function allTransaction(){
$d = $_REQUEST["date"];
include("trans.php");
$obj = new trans();
if(!$obj->transDay($d)) {
echo '{"result":0,"message": "search did not work."}';
return;
}
$row=$obj->fetch();
echo '{"result":1,"trans":['; //start of json object
while($row){
echo json_encode($row); //convert the result array to json object
$row=$obj->fetch();
if($row){
echo ","; //if there are more rows, add comma
}
}
echo "]}";
}
function getStock(){
$message = $_REQUEST["message"];
$mess = explode(" ", $message);
$pnum = $_REQUEST["pnum"];
include("product.php");
$obj = new product();
if($obj->displayProduct($mess[0])){
$row = $obj->fetch();
$msg = $row['pname']+"<br>" + $row['price']+"<br>"+$row['qty'];
echo $msg;
// sms($msg,$pnum);
}
else{
echo '{"result":0}';
}
}
function setDiscount(){
$s=$_REQUEST['sid'];
$t=$_REQUEST['total'];
include("trans.php");
$obj = new trans();
if(!$obj->checkTotal($s, $t)){
echo '{"result":0,"message": "did not work."}';
return;
}
$row=$obj->fetch();
echo '{"result":1,"products":[';
echo json_encode($row);
echo "]}";
}
function deleteSale(){
$p = $_REQUEST["sid"];
include("sales.php");
$obj=new sales();
if(!$obj->deleteSale($p)) {
echo '{"result":0}';
}
else {
echo '{"result":1}';
}
}
function editSale(){
$p = $_REQUEST["sid"];
include("sales.php");
$obj=new sales();
if(!$obj->editSale($p)) {
echo '{"result":0}';
}
else {
echo '{"result":1}';
}
}
?>