/
ajax_data.php
2331 lines (1851 loc) · 96.3 KB
/
ajax_data.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Product Details for purchase
if(isset($_GET['page']) and $_GET['page'] == "productDetails") {
$product_id = "";
if(isset($_GET["product_id"])) {
$selectProductId = easySelectA(array(
"table" => "products",
"fields" => "product_id",
"where" => array(
"product_id" => $_GET["product_id"],
" or product_code" => $_GET["product_id"],
)
));
if($selectProductId !== false) {
$product_id = $selectProductId["data"][0]["product_id"];
}
}
// If no product id
if(empty($product_id)) {
die("Sorry no product found!");
}
//$this_product = product_type($product_id);
$select_product = easySelectA(array(
"table" => "products as product",
"fields" => "product.product_id, concat(product_name, ' ', if(product_group is null, '', left(product_group, 3))) as product_name, product_type, 0 as product_discount,
round( COALESCE(purchase_price, product_purchase_price), 2) as product_purchase_price,
round( COALESCE(sale_price, product_sale_price), 2) as product_sale_price,
product_unit, has_expiry_date, if(stock_in is null, 0, round(stock_in, 2)) as stock_in,
if(total_sold is null, 0, round(total_sold, 2)) as total_sold_count, round(product_alert_qnt, 2) as product_alert_qnt",
"join" => array(
"left join (select
vp_id,
sum(base_stock_in/base_qty) as stock_in
from product_base_stock
where warehouse = '{$_SESSION['wid']}'
group by vp_id
) as pbs on pbs.vp_id = product.product_id",
"left join (
SELECT
stock_product_id,
sum(stock_item_qty) as total_sold
FROM {$table_prefix}product_stock
WHERE is_trash = 0 and stock_type = 'sale' and stock_warehouse_id = '{$_SESSION['wid']}'
) as sold on sold.stock_product_id = product.product_id",
// Because Of we have different price based on shop
"left join (SELECT
product_id,
purchase_price,
sale_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price on product_price.product_id = product.product_id"
),
"where" => array(
"product.is_trash = 0 and product.product_id" => $product_id
)
));
// If there is no product againts given ID
if(!$select_product) {
die("Sorry no product found!");
}
// This variable will be kept all data for returning
$returnData = array();
$product = $select_product["data"][0];
// Use for checking product type
$product_is = function($type) {
return $type === $GLOBALS["product"]["product_type"];
};
if( $product_is("Variable")) {
$select_variation = easySelectA(array(
"table" => "products as parent_product",
"fields" => "product_meta.product_id as id,
(case when meta_type = 'Variation' then 'V'
when meta_type = 'Default-Variation' then 'DV'
END) as t,
meta_key as mk, meta_value as mv",
"join" => array(
"left join {$table_prefix}products as child_products on parent_product.product_id = child_products.product_parent_id",
"inner join ( select
product_id,
meta_type,
meta_key,
meta_value
from {$table_prefix}product_meta
) as product_meta on product_meta.product_id = parent_product.product_id or product_meta.product_id = child_products.product_id"
),
"where" => array(
"child_products.is_trash = 0 and parent_product.product_id" => $product_id
),
"groupby" => "product_meta.product_id, meta_type, meta_key, meta_value"
))["data"];
// Store the product data
array_push($returnData, array(
"pid" => $product["product_id"], // pid = Product id
"pn" => $product["product_name"], // pn = product Name
"pv" => $select_variation
));
} else if( $product_is("Grouped") ) {
$selectGroupedProducts = easySelectA(array(
"table" => "bg_product_items as bg_product_items",
"fields" => "bg_item_product_id, concat(product_name, ' ', if(product_group is null, '', left(product_group, 3))) as product_name, product_type,
round( COALESCE(purchase_price, product_purchase_price) , 2) as product_purchase_price,
product_unit, bg_product_qnt, 0 as product_discount, if(stock_in is null, 0, round(stock_in, 2)) as stock_in,
if(total_sold is null, 0, round(total_sold, 2)) as total_sold_count, round(product_alert_qnt, 2) as product_alert_qnt",
"join" => array(
"left join {$table_prefix}products as products on products.product_id = bg_item_product_id",
"left join (SELECT
product_id,
purchase_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price on product_price.product_id = bg_item_product_id",
"left join (
SELECT
stock_product_id,
sum(stock_item_qty) as total_sold
FROM {$table_prefix}product_stock
WHERE is_trash = 0 and stock_type = 'sale' and stock_warehouse_id = '{$_SESSION['wid']}'
) as sold on sold.stock_product_id = bg_item_product_id",
"left join (select
vp_id,
sum(base_stock_in/base_qty) as stock_in
from product_base_stock
where warehouse = '{$_SESSION['wid']}'
group by vp_id
) as pbs on pbs.vp_id = bg_item_product_id",
),
"where" => array(
"is_raw_materials = 0 and bg_product_id" => $product_id
)
));
if($selectGroupedProducts !== false) {
foreach($selectGroupedProducts["data"] as $pkey => $pvalue) {
// Store the product data
array_push($returnData,
array(
"pid" => $pvalue["bg_item_product_id"], // pid = Product id
"pn" => $pvalue["product_name"], // pn = product Name
"pd" => $pvalue["product_discount"], // pd = product Discount
"iq" => $pvalue["bg_product_qnt"], // iq = Item Quantity
"pu" => $pvalue["product_unit"], // iq = Item Quantity
"alertq" => $product["product_alert_qnt"], // aq = alert quantity
"soldq" => $product["total_sold_count"], // soldq = stock quantity
"stockq" => $product["stock_in"], // stockq = stock quantity
"pp" => $pvalue["product_purchase_price"], // sp = purchase price
)
);
}
}
} else {
// For normal product
array_push($returnData, array(
"pid" => $product["product_id"], // pid = Product id
"pn" => $product["product_name"], // pn = product Name
"pd" => $product["product_discount"], // pd = product Discount
"sp" => $product["product_sale_price"], // sp = sale price
"pp" => $product["product_purchase_price"], // sp = purchase price
"pu" => $product["product_unit"], // pu = Product Unit
"alertq" => $product["product_alert_qnt"], // aq = alert quantity
"soldq" => $product["total_sold_count"], // soldq = stock quantity
"stockq" => $product["stock_in"], // stockq = stock quantity
"hed" => $product["has_expiry_date"] // hed = Has Expiry Date
));
}
echo json_encode($returnData);
}
// Product details for POS
if(isset($_GET['page']) and $_GET['page'] == "productDetailsForPos") {
$product_id = "";
if(isset($_GET["product_id"])) {
$selectProductId = easySelectA(array(
"table" => "products",
"fields" => "product_id",
"where" => array(
"product_id" => $_GET["product_id"],
" or product_code" => $_GET["product_id"],
)
));
if($selectProductId !== false) {
$product_id = $selectProductId["data"][0]["product_id"];
}
}
// If no product id
if(empty($product_id)) {
$returnData = array (
"error" => true,
"msg" => "Product out of stock or not found."
);
echo json_encode($returnData);
exit();
}
$warehouse_id = isset($_GET["warehouse_id"]) ? (int)safe_input($_GET["warehouse_id"]) : "";
$customer_id = isset($_GET["cid"]) ? (int)safe_input($_GET["cid"]) : "";
$product_qnt = (isset($_GET["pqnt"]) and !empty($_GET["pqnt"])) ? safe_entities($_GET["pqnt"]) : get_options("defaultSaleQnt");
$packet = ( isset($_GET["packet"]) and !empty($_GET["packet"]) ) ? safe_entities($_GET["packet"]) : 0;
$customerType = "consumer";
$selectCustomerType = easySelectA(array(
"table" => "customers",
"fields" => "customer_type",
"where" => array(
"customer_id" => $customer_id
)
));
if($selectCustomerType !== false) {
$customerType = strtolower($selectCustomerType["data"][0]["customer_type"]);
}
// Settings
$allowToAddStockOutProduct = get_options("allowToAddStockOutProductInPOS");
$allowToSaleStockOutProduct = get_options("allowToSaleStockOutProductInPOS");
$returnData = array();
$select_product = easySelectA(array(
"table" => "products as product",
"fields" => "product.product_id as product_id, concat(product_name, ' ', if(product_group is null, '', left(product_group, 3))) as product_name, product_generic,
if(stock_in is null, 0, round(stock_in, 2) ) as stock_in, product_type, product_{$customerType}_discount as product_discount,
round( COALESCE(product_price.purchase_price, product_purchase_price), 2) as product_purchase_price,
round( COALESCE(product_price.sale_price, product_sale_price), 2) as product_sale_price,
product_packet_qnt, product_unit, maintain_stock, has_expiry_date, has_batch",
"join" => array(
"left join ( select
vp_id,
if(batch_id is not null, 1, 0) as has_batch, /** Checking the product if set has expriy date and there also have batch entry*/
warehouse,
sum(base_stock_in/base_qty) as stock_in
FROM product_base_stock
where vp_id= '{$product_id}' and ( batch_id is null or date(batch_expiry_date) > curdate() ) and warehouse = '{$warehouse_id}'
group by vp_id
) as stock on stock.vp_id = product.product_id",
// Because Of we have different price based on shop
"left join (SELECT
product_id,
purchase_price,
sale_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price on product_price.product_id = product.product_id"
),
"where" => array(
"product.is_trash = 0 and product.product_id" => $product_id
)
));
// If there is no product againts given ID
if(!$select_product) {
$returnData = array (
"error" => true,
"msg" => "Product out of stock or not found."
);
echo json_encode($returnData);
exit();
}
// This variable will be kept all data for returning
$returnData = array();
$product = $select_product["data"][0];
// Use for checking product type
$product_is = function($type) {
return $type === $GLOBALS["product"]["product_type"];
};
if( $product_is("Variable")) {
$select_variation = easySelectA(array(
"table" => "products as parent_product",
"fields" => "product_meta.product_id as id,
(case when meta_type = 'Variation' then 'V'
when meta_type = 'Default-Variation' then 'DV'
END) as t,
meta_key as mk, meta_value as mv",
"join" => array(
"left join {$table_prefix}products as child_products on parent_product.product_id = child_products.product_parent_id",
"inner join ( select
product_id,
meta_type,
meta_key,
meta_value
from {$table_prefix}product_meta
) as product_meta on product_meta.product_id = parent_product.product_id or product_meta.product_id = child_products.product_id"
),
"where" => array(
"child_products.is_trash = 0 and parent_product.product_id" => $product_id
),
"groupby" => "product_meta.product_id, meta_type, meta_key, meta_value"
))["data"];
// Store the product data
array_push($returnData, array(
"pid" => $product["product_id"], // pid = Product id
"pn" => $product["product_name"], // pn = product Name
"pv" => $select_variation
));
} else {
// Select products, which have sub/bundle products and check it if there have enough quantity in stock
$subProductsStockCheck = easySelectA(array(
"table" => "bg_product_items",
"fields" => "bg_item_product_id, product_name, product_{$customerType}_discount as product_discount,
bg_item_product_id,
round( COALESCE(product_price.purchase_price, sub_product.product_purchase_price), 2) as product_purchase_price,
round( COALESCE(product_price.sale_price, sub_product.product_sale_price), 2) as product_sale_price,
sub_product.product_generic as product_generic,
sub_product.product_unit as product_unit,
sub_product.product_packet_qnt as product_packet_qnt,
sub_product.has_expiry_date as has_expiry_date,
sub_product.maintain_stock as maintain_stock,
if(stock_in is null, 0, round(stock_in, 2)) as stock_in,
round(bg_product_qnt, 2) as bg_product_qnt,
has_batch
",
"join" => array(
"left join {$table_prefix}products as sub_product on sub_product.product_id = bg_item_product_id",
"left join ( select
vp_id,
if(batch_id is not null, 1, 0) as has_batch, /** Checking the product if set has expriy date and there also have batch entry*/
sum(base_stock_in/base_qty) as stock_in
FROM product_base_stock
where ( batch_id is null or date(batch_expiry_date) > curdate() ) and warehouse = '{$warehouse_id}'
group by vp_id
) as stock on stock.vp_id = bg_item_product_id",
// Because Of we have different price based on shop
"left join (SELECT
product_id,
purchase_price,
sale_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price on product_price.product_id = sub_product.product_id"
),
"where" => array(
"is_raw_materials = 0 and bg_product_id = '{$product_id}'"
)
));
// default false value for stock out product in bundle/ sub product
$subProductIsStockOut = false;
// Checking sub product stock
if($subProductsStockCheck !== false) {
// Set stock quantity to null by default
$lowerQuantity = null;
foreach($subProductsStockCheck["data"] as $subKey => $subProduct) {
// Calculate stock quantity
$stockQnt = $subProduct["stock_in"] / $subProduct["bg_product_qnt"];
// Check there at least one stock out product in this bundle product
$subProductIsStockOut = $subProductIsStockOut ?: $product_qnt > $stockQnt;
// Check if the $product_qnt/ Sale quantity is below of stock quantity
// And return the lower quantity with error msg
if( $subProduct["maintain_stock"] == 1 and $product_qnt > $stockQnt and ( !$allowToAddStockOutProduct and !$allowToSaleStockOutProduct ) and ( $lowerQuantity === null or $lowerQuantity > $stockQnt )) {
$product_unit = $subProduct["product_unit"] !== null ? $subProduct["product_unit"] . "(s)" : "";
$returnData = array (
"error" => true,
"stq" => $stockQnt,
"msg" => "The Bundle/ Grouped item ({$subProduct["product_name"]}) only left {$subProduct["stock_in"]}/{$subProduct["bg_product_qnt"]} = {$stockQnt} {$product_unit} in stock. But it is defined {$product_qnt} {$product_unit}. Quantity must be bellow or equal to product stock in."
);
$lowerQuantity = $stockQnt;
} else if( $product_is("Grouped") ) {
// If there is no error, add the grouped product in $returnData
// Store the Grouped product data
array_push($returnData, array(
"pid" => $subProduct["bg_item_product_id"], // pid = Product id
"pn" => $subProduct["product_name"], // pn = product Name
"pd" => $subProduct["product_discount"], // pd = product Discount
"stq" => $stockQnt, // stq = Stock Quantity
"iq" => $subProduct["bg_product_qnt"], // item quantity
"so" => ( $subProduct["maintain_stock"] == 1 and ( $product_qnt > $stockQnt or $subProductIsStockOut ) and !$allowToSaleStockOutProduct) ? 1 : 0, // Stock Out
"sp" => $subProduct["product_sale_price"], // sp = sale price
"pp" => $subProduct["product_purchase_price"], // pp = purchase price
"gn" => $subProduct["product_generic"], // gn = generic name
"pu" => $subProduct["product_unit"], // pu = Product Unit
"pq" => $subProduct["product_packet_qnt"], // pu = Product Unit
"hed" => ($subProduct["has_expiry_date"] and $subProduct["has_batch"]) ? 1 : 0, // hed = Has Expiry Date
));
}
}
}
// Check there are any error in grouped/ bundle product checking
// and If the product is not grouped product
if( !isset($returnData["error"]) and $product_is("Grouped") === false ) {
if( $product["maintain_stock"] == 1 and $product["stock_in"] == 0 and ( !$allowToAddStockOutProduct and !$allowToSaleStockOutProduct ) ) { // Check if there enough stock for normal product
$returnData = array (
"error" => true,
"msg" => "Product out of stock in the selected warehouse."
);
} else if( $product["maintain_stock"] == 1 and $product_qnt > $product["stock_in"] and ( !$allowToAddStockOutProduct and !$allowToSaleStockOutProduct ) ) {
$product_unit = $product["product_unit"] !== null ? $product["product_unit"] . "(s)" : "";
$returnData = array (
"error" => true,
"msg" => "Only {$product["stock_in"]} {$product_unit} left in stock. You entered total {$product_qnt} {$product_unit}. Quantity must be bellow or equal to product stock in.",
"stq" => $product["stock_in"],
"sp" => $product["product_sale_price"],
"pp" => $product["product_purchase_price"]
);
} else {
// Store the product data
// Normal and Bundle Product
array_push($returnData, array(
"pid" => $product["product_id"], // pid = Product id
"pn" => $product["product_name"], // pn = product Name
"pd" => $product["product_discount"], // pd = product Discount
"stq" => $product["stock_in"], // stq = Stock Quantity
"iq" => $product_qnt, // item quantity
"so" => ( $product["maintain_stock"] == 1 and ( $product_qnt > $product["stock_in"] or $subProductIsStockOut ) and !$allowToSaleStockOutProduct) ? 1 : 0, // SO = stock out
"sp" => $product["product_sale_price"], // sp = sale price
"pp" => $product["product_purchase_price"], // pp = purchase price
"gn" => $product["product_generic"], // gn = generic name
"pu" => $product["product_unit"], // pu = Product Unit
"pq" => $product["product_packet_qnt"], // pu = Product Unit
"hed" => ($product["has_expiry_date"] and $product["has_batch"]) ? 1 : 0, // hed = Has Expiry Date
));
}
}
}
echo json_encode($returnData);
}
// Product for return
if(isset($_GET['page']) and $_GET['page'] == "productDetailsForReturn") {
$product_id = "";
if(isset($_GET["product_id"])) {
$selectProductId = easySelectA(array(
"table" => "products",
"fields" => "product_id",
"where" => array(
"product_id" => $_GET["product_id"],
" or product_code" => $_GET["product_id"],
)
));
if($selectProductId !== false) {
$product_id = $selectProductId["data"][0]["product_id"];
}
}
// If no product id
if(empty($product_id)) {
die("Sorry no product found!");
}
$customer_id = isset($_GET["customer_id"]) ? safe_input($_GET["customer_id"]) : "";
$customerType = "consumer";
$selectCustomerType = easySelectA(array(
"table" => "customers",
"fields" => "customer_type",
"where" => array(
"customer_id" => $customer_id
)
));
if($selectCustomerType !== false) {
$customerType = strtolower($selectCustomerType["data"][0]["customer_type"]);
}
$select_product = easySelectA(array(
"table" => "products",
"fields" => "product_id, product_name, product_type, product_{$customerType}_discount as product_discount,
round( COALESCE(purchase_price, product_purchase_price), 2) as product_purchase_price,
round( COALESCE(sale_price, product_sale_price), 2) as product_sale_price,
product_unit, has_expiry_date",
"join" => array(
// Because Of we have different price based on shop
"left join (SELECT
product_id,
purchase_price,
sale_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price using(product_id) "
),
"where" => array(
"is_trash = 0 and product_id" => $product_id
)
));
// If there is no product againts given ID
if(!$select_product) {
die("Sorry no product found!");
}
// This variable will be kept all data for returning
$returnData = array();
$product = $select_product["data"][0];
// Use for checking product type
$product_is = function($type) {
return $type === $GLOBALS["product"]["product_type"];
};
if( $product_is("Variable")) {
$select_variation = easySelectA(array(
"table" => "products as parent_product",
"fields" => "product_meta.product_id as id,
(case when meta_type = 'Variation' then 'V'
when meta_type = 'Default-Variation' then 'DV'
END) as t,
meta_key as mk, meta_value as mv",
"join" => array(
"left join {$table_prefix}products as child_products on parent_product.product_id = child_products.product_parent_id",
"inner join ( select
product_id,
meta_type,
meta_key,
meta_value
from {$table_prefix}product_meta
) as product_meta on product_meta.product_id = parent_product.product_id or product_meta.product_id = child_products.product_id"
),
"where" => array(
"parent_product.product_id" => $product_id
),
"groupby" => "product_meta.product_id, meta_type, meta_key, meta_value"
))["data"];
// Store the product data
array_push($returnData, array(
"pid" => $product["product_id"], // pid = Product id
"pn" => $product["product_name"], // pn = product Name
"pv" => $select_variation
));
} else {
// For normal product
$normal_product = easySelectA(array(
"table" => "products as products",
"fields" => "
product_id, product_name, product_{$customerType}_discount as product_discount, product_unit, has_expiry_date,
round( COALESCE(sale_price, product_sale_price), 2) as product_sale_price,
if(sale_item_quantity is null, 0, round(sale_item_quantity, 2)) as purchasedQnt,
if(returns_products_quantity is null, 0, round(returns_products_quantity, 2)) as returnedQnt
",
"join" => array(
"left join (
select
stock_product_id,
sum(case when stock_type = 'sale' then stock_item_qty end ) as sale_item_quantity,
sum(case when stock_type = 'sale-return' then stock_item_qty end ) as returns_products_quantity
from {$table_prefix}product_stock as product_stock
left join {$table_prefix}sales on stock_sales_id = sales_id
where stock_product_id = {$product_id} and sales_customer_id = '{$customer_id}' and product_stock.is_trash = 0
group by stock_product_id
) as stock on stock_product_id = product_id",
"left join (SELECT
product_id,
sale_price
FROM {$table_prefix}product_price
WHERE shop_id = '{$_SESSION['sid']}'
) as product_price using(product_id) "
),
"where" => array(
"product_id" => $product_id
)
))["data"][0];
array_push($returnData, array(
"pid" => $normal_product["product_id"], // pid = Product id
"pn" => $normal_product["product_name"], // pn = product Name
"pd" => $normal_product["product_discount"], // pd = product Discount
"prq" => $normal_product["purchasedQnt"], // prq = Purchased Quantity
"rtq" => $normal_product["returnedQnt"], // rtq = Returned Quantity
"sp" => $normal_product["product_sale_price"], // sp = sale price
"pu" => $normal_product["product_unit"], // pu = product_unit
"hed" => $normal_product["has_expiry_date"]
));
}
echo json_encode($returnData);
}
// Product List
if(isset($_GET['page']) and $_GET['page'] == "productList") {
$productCategoryFilter = ( isset($_GET["catId"]) and !empty($_GET["catId"]) ) ? $_GET["catId"] : "";
$productBrandFilter = ( isset($_GET["brand"]) and !empty($_GET["brand"]) ) ? $_GET["brand"] : "";
$productGenericFilter = ( isset($_GET["generic"]) and !empty($_GET["generic"]) ) ? $_GET["generic"] : "";
$productAuthorFilter = ( isset($_GET["author"]) and !empty($_GET["author"]) ) ? $_GET["author"] : "";
// If there are any edition to filter, we do not need product_type != 'Child' filter
$productEditionFilter = ( isset($_GET["edition"]) and !empty($_GET["edition"]) ) ? " product_edition = '". safe_input($_GET["edition"]) ."' " : " product_type != 'Child' ";
$oderBy = array();
if( !empty(get_options("defaultProductOrder")) ) {
$oderBy = array(
get_options("defaultProductOrder") => empty(get_options("defaultProductOrderBy")) ? "ASC" : get_options("defaultProductOrderBy")
);
}
$ProductList = easySelectA(array(
"table" => "products",
"fields" => "product_id as id, product_name as name, length(product_photo) as v",
"where" => array (
"is_trash = 0 AND {$productEditionFilter}",
" AND product_category_id" => $productCategoryFilter,
" AND product_brand_id" => $productBrandFilter,
" AND product_generic" => $productGenericFilter
),
"join" => array(
"left join ( select
stock_product_id,
if(stock_item_qty is null, 0, sum(stock_item_qty)) as totalSoldQnt
from {$table_prefix}product_stock
where is_trash = 0 and stock_type = 'sale'
group by stock_product_id
) as sale_items on stock_product_id = product_id"
),
"orderby" => $oderBy,
"limit" => array (
"start" => 0,
"length" => empty(get_options("maxProductDisplay")) ? 1500 : get_options("maxProductDisplay")
)
));
if($ProductList) {
echo json_encode($ProductList["data"]);
} else {
echo "null";
}
}
// Product Unit Details
if(isset($_GET['page']) and $_GET['page'] == "productUnitDetails") {
$unitDetails = easySelectA(
array(
"table" => "product_unit_variants",
// "fields" => "puv_default as d, puv_name as n, puv_product_id as pid, round(puv_purchase_price, 2) as pp, round(puv_sale_price, 2) as sp",
"fields" => "round(puv_purchase_price, 2) as pp, round(puv_sale_price, 2) as sp",
"where" => array(
"puv_product_id" => $_GET["product_id"],
" and puv_name" => $_GET["unit"]
)
)
)["data"][0];
echo json_encode($unitDetails);
}
// Return shop income in the specific date
if(isset($_GET['page']) and $_GET['page'] == "getShopIncome") {
$todaySale = easySelect(
"sales",
"(sum(sales_grand_total) - sum(sales_due)) as chash_in",
array(),
array (
"is_trash = 0 and sales_delivery_date" => $_POST["incomeDate"],
" AND sales_shop_id" => $_POST["shopId"]
)
);
echo $todaySale["data"][0]["chash_in"];
}
// Employee Salary data
if(isset($_GET['page']) and $_GET['page'] == "getEmpSalaryData") {
$emp_id = safe_input($_POST["empId"]);
$employeeSalaryData = easySelectD(
"SELECT
emp_id, emp_firstname, emp_lastname, emp_positions, round(emp_payable_salary, 2) as emp_payable_salary, round(emp_payable_overtime, 2) as emp_payable_overtime, round(emp_payable_bonus, 2) as emp_payable_bonus,
loan_amount, loan_installment_amount, loan_id,
if(loan_installment_paid_amount is null, 0, loan_installment_paid_amount) as loan_installment_paid_amount
from {$table_prefix}employees
left join (select
loan_id, loan_borrower, loan_amount, loan_installment_amount
from {$table_prefix}loan where is_trash = 0 group by loan_id
) as loan on emp_id = loan_borrower
left join (select
loan_ids, sum(loan_installment_paying_amount) as loan_installment_paid_amount
from {$table_prefix}loan_installment where is_trash = 0 group by loan_ids
) as loan_installment on loan_id = loan_ids
where emp_id = '{$emp_id}'
order by loan_id
desc limit 1"
)["data"][0];
echo json_encode($employeeSalaryData);
}
// Customer Payment Info
if(isset($_GET['page']) and $_GET['page'] == "getCustomerPaymentInfo") {
$customer_id = safe_input($_POST["customerId"]);
$customerPaymentsData = easySelectD(
"SELECT customer_id, round(customer_opening_balance, 2) as customer_opening_balance,
if(sales_grand_total is null, 0, round(sales_grand_total, 2)) as sales_grand_total,
if(sales_shipping is null, 0, round(sales_shipping, 2)) as sales_shipping,
if(sales_due is null, 0, round(sales_due, 2)) as sales_due,
if(returns_grand_total is null, 0, round(returns_grand_total, 2)) as returns_grand_total,
if(received_payments_amount is null, 0, round(received_payments_amount, 2)) as total_received_payments,
if(payments_return_amount is null, 0, round(payments_return_amount, 2)) as total_payment_return,
if(received_payments_bonus is null, 0, round(received_payments_bonus, 2)) as total_given_bonus
from {$table_prefix}customers
left join ( select
sales_customer_id,
sum(sales_grand_total) as sales_grand_total,
sum(sales_shipping) as sales_shipping,
sum(sales_due) as sales_due
from {$table_prefix}sales where is_return = 0 and is_trash = 0 and sales_status = 'Delivered' group by sales_customer_id
) as sales on customer_id = sales.sales_customer_id
left join ( select
sales_customer_id,
sum(sales_grand_total) as returns_grand_total
from {$table_prefix}sales where is_return = 1 and is_trash = 0 and sales_status = 'Delivered' group by sales_customer_id
) as product_returns on customer_id = product_returns.sales_customer_id
left join ( select
received_payments_from,
sum(received_payments_amount) as received_payments_amount,
sum(received_payments_bonus) as received_payments_bonus
from {$table_prefix}received_payments where is_trash = 0 group by received_payments_from
) as received_payments on customer_id = received_payments_from
left join ( select
payments_return_customer_id,
sum(payments_return_amount) as payments_return_amount
from {$table_prefix}payments_return where is_trash = 0 and payments_return_type = 'Outgoing' group by payments_return_customer_id
) as payment_return on customer_id = payments_return_customer_id
where customer_id = '{$customer_id}'"
)["data"][0];
echo json_encode($customerPaymentsData);
}
// Customer Statement Info
if(isset($_GET['page']) and $_GET['page'] == "getCustomerStatementInfo") {
$customer_id = safe_input($_POST["customerId"]);
$dateRange = explode(" - ", safe_input($_POST["dateRange"]));
$customerPaymentsData = easySelectD(
"select customer_id, customer_name, customer_address, customer_district, district_name,
(
if(sales_total_amount is null, 0, sales_total_amount) +
if(wastage_sale_grand_total is null, 0, wastage_sale_grand_total)
) as net_purchased,
if(sales_shipping is null, 0, sales_shipping) as total_shipping,
(
if(sales_product_discount is null, 0, sales_product_discount) +
if(sales_discount is null, 0, sales_discount)
) as total_purchased_discount,
if(received_payments_amount is null, 0, received_payments_amount) as received_payments_amount,
if(advance_payments_amount is null, 0, advance_payments_amount) as advance_payments_amount,
if(sales_payments_amount is null, 0, sales_payments_amount) as sales_payments_amount,
if(received_payments_bonus is null, 0, received_payments_bonus) as total_given_bonus,
if(returns_grand_total is null, 0, returns_grand_total) as total_product_returns,
if(discounts_amount is null, 0, discounts_amount) as special_discounts_amount,
if(payments_return_amount is null, 0, payments_return_amount) as payments_return_amount
from {$table_prefix}customers
left join {$table_prefix}districts on district_id = customer_district
left join ( select
sales_customer_id,
sum(case when is_return = 0 then sales_total_amount end) as sales_total_amount,
sum(case when is_return = 0 then sales_shipping end) as sales_shipping,
sum(case when is_return = 0 then sales_product_discount end) as sales_product_discount,
sum(case when is_return = 0 then sales_discount end) as sales_discount,
sum(case when is_return = 1 then sales_grand_total end) as returns_grand_total
from {$table_prefix}sales where is_trash = 0 and sales_status = 'Delivered' and sales_delivery_date between '{$dateRange[0]}' and '{$dateRange[1]}' group by sales_customer_id
) as sales on customer_id = sales_customer_id
left join ( select
wastage_sale_customer,
sum(wastage_sale_grand_total) as wastage_sale_grand_total
from {$table_prefix}wastage_sale where is_trash = 0 and wastage_sale_date between '{$dateRange[0]}' and '{$dateRange[1]}' group by wastage_sale_customer
) as wastage_sale on customer_id = wastage_sale_customer
left join ( select
received_payments_from,
sum(case when received_payments_type = 'Received Payments' then received_payments_amount end) as received_payments_amount,
sum(case when received_payments_type = 'Advance Collection' then received_payments_amount end) as advance_payments_amount,
sum(case when received_payments_type = 'Sales Payments' or received_payments_type = 'Wastage Sales Payments' then received_payments_amount end) as sales_payments_amount,
sum(received_payments_bonus) as received_payments_bonus
from {$table_prefix}received_payments
where is_trash = 0 and received_payments_type != 'Discounts' and date(received_payments_datetime) between '{$dateRange[0]}' and '{$dateRange[1]}'
group by received_payments_from
) as received_payments on customer_id = received_payments.received_payments_from
left join ( select
received_payments_from,
sum(received_payments_amount) as discounts_amount
from {$table_prefix}received_payments
where is_trash = 0 and received_payments_type = 'Discounts' and date(received_payments_datetime) between '{$dateRange[0]}' and '{$dateRange[1]}'
group by received_payments_from
) as given_discounts on customer_id = given_discounts.received_payments_from
left join (select
payments_return_customer_id,
sum(payments_return_amount) as payments_return_amount
from {$table_prefix}payments_return
where is_trash = 0 and payments_return_type = 'Outgoing' and date(payments_return_date) between '{$dateRange[0]}' and '{$dateRange[1]}'
group by payments_return_customer_id
) as payment_return on customer_id = payments_return_customer_id
where customer_id = '{$customer_id}'"
)["data"][0];
$customerPaymentsData["previous_balance"] = easySelectD("
SELECT
(
if(customer_opening_balance is null, 0, customer_opening_balance) +
if(total_returned_before_filtered_date is null, 0, total_returned_before_filtered_date) +
if(total_payment_before_filtered_date is null, 0, total_payment_before_filtered_date)
) - (
if(total_purchased_before_filtered_date is null, 0, total_purchased_before_filtered_date) +
if(total_wastage_purched_before_filtered_date is null, 0, total_wastage_purched_before_filtered_date) +
if(total_payment_return_before_filtered_date is null, 0, total_payment_return_before_filtered_date)
) as previous_balance
FROM {$table_prefix}customers as customers
left join ( select
sales_customer_id,
sum(case when is_return = 0 then sales_grand_total end) as total_purchased_before_filtered_date,
sum(case when is_return = 1 then sales_due end) as total_returned_before_filtered_date
from {$table_prefix}sales where is_trash = 0 and sales_status = 'Delivered' and sales_delivery_date < '{$dateRange[0]}' group by sales_customer_id
) as sales on sales_customer_id = customer_id
left join ( select
wastage_sale_customer,
sum(wastage_sale_grand_total) as total_wastage_purched_before_filtered_date
from {$table_prefix}wastage_sale where is_trash = 0 and wastage_sale_date < '{$dateRange[0]}' group by wastage_sale_customer
) as wastage_sale on wastage_sale_customer = customer_id
left join ( select
received_payments_from,
sum(received_payments_amount) + sum(received_payments_bonus) as total_payment_before_filtered_date
from {$table_prefix}received_payments where is_trash = 0 and date(received_payments_datetime) < '{$dateRange[0]}' group by received_payments_from
) as payments on received_payments_from = customer_id
left join (select
payments_return_customer_id,
sum(payments_return_amount) as total_payment_return_before_filtered_date
from {$table_prefix}payments_return
where is_trash = 0 and payments_return_type = 'Outgoing' and date(payments_return_date) < '{$dateRange[0]}'
group by payments_return_customer_id
) as payment_return on customer_id = payments_return_customer_id
where customer_id = '{$customer_id}'
")["data"][0]["previous_balance"];
echo json_encode($customerPaymentsData);
}
// Get Employee Loan Data
if(isset($_GET['page']) and $_GET['page'] == "getEmpLoanLoanData") {
$emp_id = safe_input($_POST["empId"]);
$month = safe_input($_POST["month"]);
$year = safe_input($_POST["year"]);
$getLoanDetails = easySelectD(
"select
loan_id, loan_amount, loan_installment_amount,
if(thisMonthInstallmentPayingStatus is null, 0, 1) as thisMonthInstallmentPayingStatus,
if(loan_paid_amount is null, 0, loan_paid_amount) as loan_paid_amount
from {$table_prefix}loan as loan
left join (select
loan_ids,
sum(loan_installment_paying_amount) as loan_paid_amount
from {$table_prefix}loan_installment where is_trash = 0 group by loan_ids
) as totalPaidAmount on loan_id = totalPaidAmount.loan_ids
left join (select
loan_ids,
1 as thisMonthInstallmentPayingStatus
from {$table_prefix}loan_installment where is_trash = 0 and MONTH(loan_installment_date) = '{$month}' and year(loan_installment_date) = '{$year}' group by loan_ids
) as thisMonthStatus on loan_id = thisMonthStatus.loan_ids
where loan.is_trash = 0 and loan_borrower = '{$emp_id}' and loan_installment_starting_from <= '{$year}-{$month}-01'
and ( loan_paid_amount is null or loan_paid_amount < loan_amount)"
// loan_paid_amount can be NULL on left join if there is no recrods, for that the is null check.
// We can also use HAVING clause without using is null check. But it will raise a error with full_group_by mode.
);
$selectSalary = easySelectA(array(
"table" => "employees",
"fields" => "round(emp_salary, 2) as emp_salary",
"where" => array("emp_id = '{$emp_id}'")
))["data"][0]["emp_salary"];
if(!isset($getLoanDetails["data"])) {
$return = array (
"totalInstallmentAmount" => 0,
"totalLoan" => 0,
"totalLoanPaid" => 0,
"salary" => $selectSalary
);
echo json_encode($return);
return;
}
$loanInstallmentAmountInCurrentMonth = 0;
$totalLoanAmount = 0;
$totalLoanPaidAmount = 0;
foreach($getLoanDetails["data"] as $key => $value) {
// Check if the loan installment already not paid
// then add the installment amount
if($value["thisMonthInstallmentPayingStatus"] != 1) {