/var/www/enzatesting.onesolution.hk/ajax_get_cus_items_exportexcel.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
<?php
header
('Content-Type: application/json');
include 
"./inc/configure.php";

$custcode $_GET['custcode'];

$sql_clause '1=1'// Default condition that is always true
$sql_param = array();


$sql_clause .= ' AND (ord_main.custcode=:customer)';
$sql_param[':customer'] = $custcode;

if (!empty(
$_GET['launchdate_start'])) {
    
$sql_clause .= ' AND ord_main.launch_date>=:launchdate_start';
    
$sql_param[':launchdate_start'] = $_GET['launchdate_start'];
}

if (!empty(
$_GET['launchdate_end'])) {
    
$sql_clause .= ' AND ord_main.launch_date<=:launchdate_end';
    
$sql_param[':launchdate_end'] = $_GET['launchdate_end'];
}

if (!empty(
$_GET['itemtype'])) {
    if (
$_GET['itemtype'] != 'product') {
        
$sql_clause .= ' AND ord_dtl_rm.bomcategory=:cat';
        
$sql_param[':cat'] = $_GET['itemtype'];
    }
}

if (!empty(
$_GET['itemno'])) {
    if (!empty(
$_GET['itemtype']) && $_GET['itemtype'] == 'product') {
        
$sql_clause .= ' AND ord_dtl.itemno=:itemno';
    } else {
        
$sql_clause .= ' AND ord_dtl_rm.itemno=:itemno';
    }
    
$sql_param[':itemno'] = $_GET['itemno'];
}




$sql_products_items "
SELECT 
    inv_product.name_en,
    ord_dtl.itemno, 
    MIN(ord_main.custcode) as custcode,
    SUM(ord_dtl.qty) AS total_qty,
    SUM(ord_dtl.unitcost) AS total_unitcost,
    SUM(ord_dtl.unitfactcost) AS total_unitfactcost,
    SUM(ord_dtl.unitprice) AS total_unitprice
FROM 
    ord_dtl
INNER JOIN 
    inv_product ON inv_product.itemno = ord_dtl.itemno
INNER JOIN 
    ord_main ON ord_main.refid = ord_dtl.main_refid
WHERE 
    
$sql_clause
GROUP BY 
    ord_dtl.itemno, inv_product.name_en
ORDER BY 
    ord_dtl.itemno ASC
"
;





$sth_product_items $dbh->prepare($sql_products_items);

$q_product_items $sth_product_items->execute($sql_param);


$sql_other_items "
SELECT 
CASE
    WHEN accessory.itemno IS NOT NULL THEN accessory.name_en
    WHEN stone.itemno IS NOT NULL THEN stone.name_en
    WHEN diamond.itemno IS NOT NULL THEN diamond.name_en
    WHEN material.itemno IS NOT NULL THEN material.name_en
END as name_en,
ord_dtl_rm.itemno, 
SUM(ord_dtl_rm.qty) AS total_qty,
SUM(ord_dtl_rm.unitcost) AS total_unitcost,
SUM(ord_dtl_rm.unitfactcost) AS total_unitfactcost,
SUM(ord_dtl_rm.unitprice) AS total_unitprice
FROM 
ord_dtl_rm
LEFT JOIN 
inv_accessory AS accessory ON accessory.itemno = ord_dtl_rm.itemno
LEFT JOIN 
inv_stone AS stone ON stone.itemno = ord_dtl_rm.itemno
LEFT JOIN 
inv_diamond AS diamond ON diamond.itemno = ord_dtl_rm.itemno
LEFT JOIN 
inv_material AS material ON material.itemno = ord_dtl_rm.itemno
INNER JOIN 
ord_main ON ord_main.refid = ord_dtl_rm.main_refid
WHERE 
$sql_clause
GROUP BY 
ord_dtl_rm.itemno, 
CASE
    WHEN accessory.itemno IS NOT NULL THEN accessory.name_en
    WHEN stone.itemno IS NOT NULL THEN stone.name_en
    WHEN diamond.itemno IS NOT NULL THEN diamond.name_en
    WHEN material.itemno IS NOT NULL THEN material.name_en
END
ORDER BY 
ord_dtl_rm.itemno ASC
"
;

$sth_other_items $dbh->prepare($sql_other_items);


$q_other_items $sth_other_items->execute($sql_param);


$data = array();

while (
$row_product_items $sth_product_items->fetch()) {
    
$data[] = array(
        
'name_en' => $row_product_items['name_en'] . $custcode",
        
'itemno' => $row_product_items['itemno'],
        
'total_qty' => $row_product_items['total_qty'],
        
'total_unitcost' => $row_product_items['total_unitcost'],
        
'total_unitfactcost' => $row_product_items['total_unitfactcost'],
        
'total_unitprice' => $row_product_items['total_unitprice']
    );
}

while (
$row_other_items $sth_other_items->fetch()) {
    
$data[] = array(
        
'name_en' => $row_other_items['name_en'],
        
'itemno' => $row_other_items['itemno'],
        
'total_qty' => $row_other_items['total_qty'],
        
'total_unitcost' => $row_other_items['total_unitcost'],
        
'total_unitfactcost' => $row_other_items['total_unitfactcost'],
        
'total_unitprice' => $row_other_items['total_unitprice']
    );
}


echo 
json_encode($data);