-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshoplib.php
More file actions
157 lines (133 loc) · 4.79 KB
/
shoplib.php
File metadata and controls
157 lines (133 loc) · 4.79 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
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
<?php
function dbconnect()
{
$con = mysql_connect("localhost", "yatish", "null");
mysql_select_db("3002local", $con);
if(!$con)
{
die("Connection Failed!");
}
else{
return $con;
}
}
function dbclose($con)
{
mysql_close($con);
}
//call to encrpt
function encrypt($sData, $sKey='54h6vhcg4c4gl'){
$sResult = '';
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) + ord($sKeyChar));
$sResult .= $sChar;
}
return encode_base64($sResult);
}
//call to decrypt. Use same sKey for encrypt and decrypt
function decrypt($sData, $sKey='54h6vhcg4c4gl'){
$sResult = '';
$sData = decode_base64($sData);
for($i=0;$i<strlen($sData);$i++){
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) - ord($sKeyChar));
$sResult .= $sChar;
}
return $sResult;
}
function encode_base64($sData){
$sBase64 = base64_encode($sData);
return strtr($sBase64, '+/', '-_');
}
function decode_base64($sData){
$sBase64 = strtr($sData, '-_', '+/');
return base64_decode($sBase64);
}
function createJson()
{
$con = dbconnect();
$posts = array();
$response = array();
$query = mysql_query("SELECT barcode FROM product");
while($row = mysql_fetch_array($query))
{
$barcode = $row['barcode'];
$qtyquery = mysql_query("SELECT SUM( unitsold ) FROM transactiondetails td, transaction t
WHERE t.transactionid = td.transactionid
AND DATE >= DATE_ADD( CURDATE( ) , INTERVAL -7 DAY )
AND DATE <= CURDATE( )
AND type = 'sale'
AND barcode =$barcode;");
$qtyreq = mysql_fetch_array($qtyquery);
$qtyreq = intval($qtyreq[0]);
$qtyreq = intval($qtyreq/7);
$holdingquery = mysql_query("SELECT stocklevel, active FROM product WHERE barcode = $barcode");
$holdreq = mysql_fetch_array($holdingquery);
$holdingreq = intval($holdreq[0]);
$status = intval($holdreq[1]);
$qtyreq = $qtyreq - $holdingreq;
$salesquery = mysql_query("SELECT SUM( unitsold ) FROM transactiondetails td, transaction t
WHERE t.transactionid = td.transactionid
AND DATE = CURDATE( )
AND type = 'sale'
AND barcode =$barcode;");
$salesreq = mysql_fetch_array($salesquery);
$salesreq = intval($salesreq[0]);
$writeoffquery = mysql_query("SELECT SUM( unitsold ) FROM transactiondetails td, transaction t
WHERE t.transactionid = td.transactionid
AND DATE = CURDATE( )
AND type = 'write off'
AND barcode =$barcode;");
$writeoffreq = mysql_fetch_array($writeoffquery);
$writeoffreq = intval($writeoffreq[0]);
if($qtyreq <= 0) {
$qtyreq = 0;
}
if($status == 1) {
$enc_key = "AXCDSCFDSSXCVSS";
$posts[] = array('barcode'=>$barcode, 'quantity'=>encrypt($qtyreq), 'sales'=>encrypt($salesreq), 'write-off'=>encrypt($writeoffreq));
}
}
$revenueQuery = mysql_query("SELECT SUM(t.price) FROM transaction t
WHERE t.date = CURDATE()");
$totalrevenue = mysql_fetch_array($revenueQuery);
$totalrevenue = $totalrevenue[0];
$response['products'] = $posts;
$response['total'] = encrypt($totalrevenue);
//name file by shop id from settings file
$filename = '1.json';
$fp = fopen($filename, 'w');
fwrite($fp, json_encode($response));
fclose($fp);
dbclose($con);
return $filename;
}
function postFileToUrl($filename)
{
$target_url = 'http://cg3002-20-z.comp.nus.edu.sg/api/upload.php';
//This needs to be the full path to the file you want to send.
//$file_name_with_full_path = 'C/shop/'.$filename;
// password should match the one on regional
$password = "helloworld";
//md5 hash it
$key = md5($password);
$post = array('id' => '1', 'key' => $key, 'file_contents'=>'@C:\Users\yatishby\Desktop\Semester 5\CG3002\Project\retailGUI\1.json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
$err = curl_errno($ch);
curl_close ($ch);
echo "Sent!!";
//echo $result;
echo $err;
}
function sendData()
{
postFileToUrl(createJson());
}
?>