-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_class.php
368 lines (342 loc) · 10.8 KB
/
admin_class.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
session_start();
ini_set('display_errors', 1);
Class Action {
private $db;
public function __construct() {
ob_start();
include 'db_connect.php';
$this->db = $conn;
}
function __destruct() {
$this->db->close();
ob_end_flush();
}
function login(){
extract($_POST);
$qry = $this->db->query("SELECT * FROM users where username = '".$username."' and password = '".$password."' ");
if($qry->num_rows > 0){
foreach ($qry->fetch_array() as $key => $value) {
if($key != 'passwors' && !is_numeric($key))
$_SESSION['login_'.$key] = $value;
}
return 1;
}else{
return 3;
}
}
function login2(){
extract($_POST);
$qry = $this->db->query("SELECT * FROM user_info where email = '".$email."' and password = '".md5($password)."' ");
if($qry->num_rows > 0){
foreach ($qry->fetch_array() as $key => $value) {
if($key != 'passwors' && !is_numeric($key))
$_SESSION['login_'.$key] = $value;
}
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$this->db->query("UPDATE cart set user_id = '".$_SESSION['login_user_id']."' where client_ip ='$ip' ");
return 1;
}else{
return 3;
}
}
function logout(){
session_destroy();
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
header("location:login.php");
}
function logout2(){
session_destroy();
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
header("location:../index.php");
}
function save_user(){
extract($_POST);
$data = " name = '$name' ";
$data .= ", username = '$username' ";
$data .= ", password = '$password' ";
$data .= ", type = '$type' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO users set ".$data);
}else{
$save = $this->db->query("UPDATE users set ".$data." where id = ".$id);
}
if($save){
return 1;
}
}
function signup(){
extract($_POST);
$data = " first_name = '$first_name' ";
$data .= ", last_name = '$last_name' ";
$data .= ", mobile = '$mobile' ";
$data .= ", address = '$address' ";
$data .= ", email = '$email' ";
$data .= ", password = '".md5($password)."' ";
$chk = $this->db->query("SELECT * FROM user_info where email = '$email' ")->num_rows;
if($chk > 0){
return 2;
exit;
}
$save = $this->db->query("INSERT INTO user_info set ".$data);
if($save){
$login = $this->login2();
return 1;
}
}
function save_settings(){
extract($_POST);
$data = " name = '$name' ";
$data .= ", email = '$email' ";
$data .= ", contact = '$contact' ";
$data .= ", about_content = '".htmlentities(str_replace("'","’",$about))."' ";
if($_FILES['img']['tmp_name'] != ''){
$fname = strtotime(date('y-m-d H:i')).'_'.$_FILES['img']['name'];
$move = move_uploaded_file($_FILES['img']['tmp_name'],'../assets/img/'. $fname);
$data .= ", cover_img = '$fname' ";
}
// echo "INSERT INTO system_settings set ".$data;
$chk = $this->db->query("SELECT * FROM system_settings");
if($chk->num_rows > 0){
$save = $this->db->query("UPDATE system_settings set ".$data." where id =".$chk->fetch_array()['id']);
}else{
$save = $this->db->query("INSERT INTO system_settings set ".$data);
}
if($save){
$query = $this->db->query("SELECT * FROM system_settings limit 1")->fetch_array();
foreach ($query as $key => $value) {
if(!is_numeric($key))
$_SESSION['setting_'.$key] = $value;
}
return 1;
}
}
function save_category(){
extract($_POST);
$data = " name = '$name' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO category_list set ".$data);
}else{
$save = $this->db->query("UPDATE category_list set ".$data." where id=".$id);
}
if($save)
return 1;
}
function delete_category(){
extract($_POST);
$delete = $this->db->query("DELETE FROM category_list where id = ".$id);
if($delete)
return 1;
}
function save_supplier(){
extract($_POST);
$data = " supplier_name = '$name' ";
$data .= ", contact = '$contact' ";
$data .= ", address = '$address' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO supplier_list set ".$data);
}else{
$save = $this->db->query("UPDATE supplier_list set ".$data." where id=".$id);
}
if($save)
return 1;
}
function delete_supplier(){
extract($_POST);
$delete = $this->db->query("DELETE FROM supplier_list where id = ".$id);
if($delete)
return 1;
}
function save_product(){
extract($_POST);
$data = " name = '$name' ";
$data .= ", product_id = '$product_id' ";
$data .= ", category_id = '$category_id' ";
$data .= ", mfg_date = '$mfg_date' ";
$data .= ", exp_date = '$exp_date' ";
$data .= ", description = '$description' ";
$data .= ", price = '$price' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO product_list set ".$data);
}else{
$save = $this->db->query("UPDATE product_list set ".$data." where id=".$id);
}
if($save)
return 1;
}
function delete_product(){
extract($_POST);
$delete = $this->db->query("DELETE FROM product_list where id = ".$id);
if($delete)
return 1;
}
function delete_user(){
extract($_POST);
$delete = $this->db->query("DELETE FROM users where id = ".$id);
if($delete)
return 1;
}
function save_receiving(){
extract($_POST);
$data = " supplier_id = '$supplier_id' ";
$data .= ", total_amount = '$tamount' ";
if(empty($id)){
$ref_no = sprintf("%'.08d\n", $ref_no);
$i = 1;
while($i == 1){
$chk = $this->db->query("SELECT * FROM receiving_list where ref_no ='$ref_no'")->num_rows;
if($chk > 0){
$ref_no = mt_rand(1,99999999);
$ref_no = sprintf("%'.08d\n", $ref_no);
}else{
$i=0;
}
}
$data .= ", ref_no = '$ref_no' ";
$save = $this->db->query("INSERT INTO receiving_list set ".$data);
$id =$this->db->insert_id;
foreach($product_id as $k => $v){
$data = " form_id = '$id' ";
$data .= ", product_id = '$product_id[$k]' ";
$data .= ", qty = '$qty[$k]' ";
$data .= ", type = '1' ";
$data .= ", stock_from = 'receiving' ";
$details = json_encode(array('price'=>$price[$k],'qty'=>$qty[$k]));
$data .= ", other_details = '$details' ";
$data .= ", remarks = 'Stock from Receiving-".$ref_no."' ";
$save2[]= $this->db->query("INSERT INTO inventory set ".$data);
}
if(isset($save2)){
return 1;
}
}else{
$save = $this->db->query("UPDATE receiving_list set ".$data." where id =".$id);
$ids = implode(",",$inv_id);
$this->db->query("DELETE FROM inventory where type = 1 and form_id ='$id' and id NOT IN (".$ids.") ");
foreach($product_id as $k => $v){
$data = " form_id = '$id' ";
$data .= ", product_id = '$product_id[$k]' ";
$data .= ", qty = '$qty[$k]' ";
$data .= ", type = '1' ";
$data .= ", stock_from = 'receiving' ";
$details = json_encode(array('price'=>$price[$k],'qty'=>$qty[$k]));
$data .= ", other_details = '$details' ";
$data .= ", remarks = 'Stock from Receiving-".$ref_no."' ";
if(!empty($inv_id[$k])){
$save2[]= $this->db->query("UPDATE inventory set ".$data." where id=".$inv_id[$k]);
}else{
$save2[]= $this->db->query("INSERT INTO inventory set ".$data);
}
}
if(isset($save2)){
return 1;
}
}
}
function delete_receiving(){
extract($_POST);
$del1 = $this->db->query("DELETE FROM receiving_list where id = $id ");
$del2 = $this->db->query("DELETE FROM inventory where type = 1 and form_id = $id ");
if($del1 && $del2)
return 1;
}
function save_customer(){
extract($_POST);
$data = " name = '$name' ";
$data .= ", contact = '$contact' ";
$data .= ", address = '$address' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO customer_list set ".$data);
}else{
$save = $this->db->query("UPDATE customer_list set ".$data." where id=".$id);
}
if($save)
return 1;
}
function delete_customer(){
extract($_POST);
$delete = $this->db->query("DELETE FROM customer_list where id = ".$id);
if($delete)
return 1;
}
function chk_prod_availability(){
extract($_POST);
$price = $this->db->query("SELECT * FROM product_list where id = ".$id)->fetch_assoc()['price'];
$inn = $this->db->query("SELECT sum(qty) as inn FROM inventory where type = 1 and product_id = ".$id);
$inn = $inn && $inn->num_rows > 0 ? $inn->fetch_array()['inn'] : 0;
$out = $this->db->query("SELECT sum(qty) as `out` FROM inventory where type = 2 and product_id = ".$id);
$out = $out && $out->num_rows > 0 ? $out->fetch_array()['out'] : 0;
$available = $inn - $out;
return json_encode(array('available'=>$available,'price'=>$price));
}
function save_sales(){
extract($_POST);
$data = " customer_id = '$customer_id' ";
$data .= ", total_amount = '$tamount' ";
$data .= ", amount_discount = '$amount_discount' ";
$data .= ", discount_amount = '$totalamount' ";
if(empty($id)){
$ref_no = sprintf("%'.08d\n", $ref_no);
$i = 1;
while($i == 1){
$chk = $this->db->query("SELECT * FROM sales_list where ref_no ='$ref_no'")->num_rows;
if($chk > 0){
$ref_no = mt_rand(1,99999999);
$ref_no = sprintf("%'.08d\n", $ref_no);
}else{
$i=0;
}
}
$data .= ", ref_no = '$ref_no' ";
$save = $this->db->query("INSERT INTO sales_list set ".$data);
$id =$this->db->insert_id;
foreach($product_id as $k => $v){
$data = " form_id = '$id' ";
$data .= ", product_id = '$product_id[$k]' ";
$data .= ", qty = '$qty[$k]' ";
$data .= ", type = '2' ";
$data .= ", stock_from = 'Sales' ";
$details = json_encode(array('price'=>$price[$k],'qty'=>$qty[$k]));
$data .= ", other_details = '$details' ";
$data .= ", remarks = 'Stock out from Sales-".$ref_no."' ";
$save2[]= $this->db->query("INSERT INTO inventory set ".$data);
}
if(isset($save2)){
return $id;
}
}else{
$save = $this->db->query("UPDATE sales_list set ".$data." where id=".$id);
$ids = implode(",",$inv_id);
$this->db->query("DELETE FROM inventory where type = 1 and form_id ='$id' and id NOT IN (".$ids.") ");
foreach($product_id as $k => $v){
$data = " form_id = '$id' ";
$data .= ", product_id = '$product_id[$k]' ";
$data .= ", qty = '$qty[$k]' ";
$data .= ", type = '2' ";
$data .= ", stock_from = 'Sales' ";
$details = json_encode(array('price'=>$price[$k],'qty'=>$qty[$k]));
$data .= ", other_details = '$details' ";
$data .= ", remarks = 'Stock out from Sales-".$ref_no."' ";
if(!empty($inv_id[$k])){
$save2[]= $this->db->query("UPDATE inventory set ".$data." where id=".$inv_id[$k]);
}else{
$save2[]= $this->db->query("INSERT INTO inventory set ".$data);
}
}
if(isset($save2)){
return $id;
}
}
}
function delete_sales(){
extract($_POST);
$del1 = $this->db->query("DELETE FROM sales_list where id = $id ");
$del2 = $this->db->query("DELETE FROM inventory where type = 2 and form_id = $id ");
if($del1 && $del2)
return 1;
}
}