/var/www/html_de/wp-content/plugins/6HHYJBI0P2GXTHB_221285/api/order/functions.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
<?php

require_once __DIR__ '/../database.php';

$map_order["_order_currency"] = "currency";
$map_order["_cart_discount"] = "discount_total";
$map_order["_cart_discount_tax"] = "discount_tax";
$map_order["_order_shipping"] = "shipping_total";
$map_order["_order_shipping_tax"] = "shipping_tax";
$map_order["_order_tax"] = "cart_tax";
$map_order["_order_total"] = "total";
$map_order["_prices_include_tax"] = "prices_include_tax";
$map_order["_customer_user"] = "customer_id";
$map_order["_customer_ip_address"] = "customer_ip_address";
$map_order["_payment_method"] = "payment_method";
$map_order["_payment_method_title"] = "payment_method_title";
$map_order["_date_paid_gmt"] = "date_paid_gmt";
$map_order["_date_completed_gmt"] = "date_completed_gmt";
$map_order["_customer_user_agent"] = "customer_user_agent";
$map_order["_created_via"] = "created_via";

$map_order["_cart_hash"] = "cart_hash";
$map_order["_completed_date"] = "date_completed_gmt";
$map_order["_paid_date"] = "date_paid_gmt";
$map_order["_created_via"] = "created_via";
$map_order["_paypal_transaction_fee"] = 'paypal_fee';
$map_order["_stripe_fee"] = 'stripe_fee';
$map_order["_refund_amount"] = 'refund_amount';

$GLOBALS['map_order'] = $map_order;

$not_map_order = array('_order_version''_billing_address_index''_shipping_address_index''_download_permissions_granted''_recorded_sales''_order_stock_reduced'
'_order_key''_recorded_coupon_usage_counts''_edit_lock''_edit_last''_date_completed''_date_paid');
$GLOBALS['not_map_order'] = $not_map_order;


$map_item_meta['_product_id'] = 'product_id';
$map_item_meta['_variation_id'] = 'variation_id';
$map_item_meta['_qty'] = 'quantity';
$map_item_meta['_tax_class'] = 'tax_class';
$map_item_meta['_line_subtotal'] = 'subtotal';
$map_item_meta['_line_subtotal_tax'] = 'subtotal_tax';
$map_item_meta['_line_total'] = 'total';
$map_item_meta['_line_tax'] = 'total_tax';
$GLOBALS['map_item_meta'] = $not_map_order;


$not_map_item_meta = array('_order_version');
$GLOBALS['not_map_item_meta'] = $not_map_order;

class 
order_functions{
    function 
get_oder($arr_ids$sort_by_date$add_fields null){
        
$table_posts $GLOBALS['table_posts'];
        
$database = new database();

        if(
$add_fields==null)
            
$add_fields '';
        
$add_fields '=,' $add_fields;
        
$add_fields str_replace(' '''$add_fields);
        
$arry_fields explode(','$add_fields);
        
$arr_ids_string implode(','$arr_ids);
        
// die($arr_ids_string);

        
if($arr_ids_string == null || strlen($arr_ids_string) == 0){
            return array();
        }

        
$query "SELECT post.ID, post.post_date_gmt AS date_created_gmt, post.post_modified_gmt AS date_modified_gmt, post.post_parent as parent_id, REPLACE(post.post_status, 'wc-', '') AS `status`
        FROM 
$table_posts AS post
        WHERE ID IN (
$arr_ids_string)";

        if(
$sort_by_date == true)
            
$query .= " ORDER BY post.post_date DESC"// Sắp xếp theo ngày tháng chậm đi 3 lần

        
$query .= ';';
        
// die($query);

        
$orders $database->run_sql_return_array($query);
        if(
$orders != null){
            if(
in_array('meta_data'$arry_fields)){
                
$this->load_meta($database$orders$arr_ids_string);
                
$this->load_refund_item($database$orders$arr_ids_string); // Phai load meta truoc
            
}

            if(
in_array('products'$arry_fields)){
                
$load_category false;
                if(
in_array('category_product'$arry_fields))
                    
$load_category true// sẽ chậm hơn khoàng 10-20%

                
$this->load_product($database$orders$arr_ids_string$load_category);
            }

            if(
in_array('count_item'$arry_fields)){
                
$this->load_count_item($database$orders$arr_ids_string);
            }
        }
        else{
            
$orders = array();
        }
        return 
$orders;
    }

    function 
load_meta(database $database$orders$arr_ids_string){
        global 
$table_post_meta;
        
//Lấy meta
        
$query "SELECT post_id, meta_key, meta_value FROM $table_post_meta WHERE post_id IN ($arr_ids_string);";
        
$metas $database->run_sql_return_array($query);
        if(
$metas != null){

            
$map $GLOBALS['map_order'];
            
$not_map $GLOBALS['not_map_order'];

            foreach(
$orders as $o){
                
$product_id $o->ID;
                
$o->date_paid_gmt null;
                
$o->date_completed_gmt null;
                
$billing = new stdClass();
                
$shipping = new stdClass();
                
$billing->address_2 "";
                
$shipping->address_2 "";

                foreach(
$metas as $m){
                    if(
$m->post_id == $product_id){
                        
$meta_key $m->meta_key;
                        
$meta_value $m->meta_value;

                        if(isset(
$map[$meta_key])){
                            
$o->{$map[$meta_key]} = $meta_value;
                        }else if(!
in_array($meta_key$not_map)){
                            if(
substr($meta_key09) == '_billing_'){
                                
$pros_bill_name str_replace('_billing_'''$meta_key);
                                
$billing->{$pros_bill_name} = $meta_value;
                                
//die($meta_key);
                            
}else if(substr($meta_key010) == '_shipping_'){
                                
$pros_ship_name str_replace('_shipping_'''$meta_key);
                                
$shipping->{$pros_ship_name} = $meta_value;
                            }else{
                                
$o->custom_meta[$meta_key] = $meta_value;
                            }
                        }
                    }
                }
                
$o->billing $billing;
                
$o->shipping $shipping;
            }
        }
    }

    function 
load_product(database $database$orders$arr_ids_string$load_category){
        global 
$table_woo_woocommerce_order_items;
        global 
$table_woo_woocommerce_order_itemmeta;
        global 
$map_item_meta;
        global 
$not_map_item_meta;
        
$table_term_taxonomy $GLOBALS['table_term_taxonomy'];
        global 
$table_terms;
        
$table_term_relationships $GLOBALS['table_term_relationships'];

        
// Lấy danh sách tên và id product
        
$query "SELECT order_item_id, order_item_name, order_id FROM $table_woo_woocommerce_order_items WHERE order_id IN ($arr_ids_string) AND order_item_type = 'line_item';";
        
$products $database->run_sql_return_array($query);

        if(
$products != null){
            foreach(
$orders as $o){
                
$o->products = array();

                foreach(
$products as $p){
                    if(
$p->order_id == $o->ID){
                        
// Tim lai product dang duoc gan du lieu
                        
$product null;
                        if(
$o->products != array()){
                            foreach(
$o->products as $temp){
                                if(
$temp->id == $p->order_item_id){
                                    
$product $temp;
                                    break;
                                }
                            }
                        }   
                        
// Neu khong tim thay thi khoi tao moi
                        
if($product == null){
                            
$product = new stdClass();
                            
$o->products[] = $product;
                        }

                        
$product->id $p->order_item_id;
                        
$product->name $p->order_item_name;
                    }
                }

                
//Load meta của product (lấy từ order meta)
                
$arr_order_item_ids array_column($products'order_item_id');
                
$arr_order_item_ids_string implode(','$arr_order_item_ids); // danh sách order id
                
$query "SELECT order_item_id, meta_key, meta_value FROM $table_woo_woocommerce_order_itemmeta WHERE order_item_id IN ($arr_order_item_ids_string);";
                
$metas $database->run_sql_return_array($query);
                if(
$metas != null){
                    foreach(
$o->products as $product){
                        foreach(
$metas as $m){
                            if(
$m->order_item_id == $product->id){
                                
$meta_key $m->meta_key;
                                
$meta_value $m->meta_value;
                                if(isset(
$map_item_meta[$meta_key])){
                                    
$product->{$map_item_meta[$meta_key]} = $meta_value// load product_id ở đây
                                
}else if(substr($meta_key03) == 'pa_'){
                                    
$attribute = new stdClass();
                                    
$attribute->slug_name str_replace('pa_'''$meta_key);
                                    
$attribute->slug_value $meta_value;
                                    
$product->attribute $attribute;
                                }else if(!
in_array($meta_key$not_map_item_meta)){
                                    
$product->meta_data[$meta_key] = $meta_value;
                                }
                            }
                        }
                    }
                }

                
// Load category của product
                
if($load_category){
                    
$arr_product_ids array_column($o->products'product_id');
                    if(!empty(
$arr_product_ids)) {
                        
$arr_product_ids_string implode(','$arr_product_ids); // danh sách order id

                        
$query "SELECT tr.object_id, t.term_id, t.slug, t.name FROM $table_term_relationships AS tr
                        INNER JOIN 
$table_term_taxonomy AS tx ON tx.term_taxonomy_id = tr.term_taxonomy_id
                        INNER JOIn 
$table_terms AS t on t.term_id = tx.term_id
                        WHERE tr.object_id IN (
$arr_product_ids_string) AND tx.taxonomy = 'product_cat';";
                        
// die($query);
                        
$categories $database->run_sql_return_array($query);
                        foreach (
$o->products as $p) {
                            
$p->categories = array();
                            
$product_id $p->product_id;
                            if (
$categories != null) {
                                foreach (
$categories as $cat) {
                                    if (
$cat->object_id == $product_id) {
                                        
$category = array(
                                            
'id' => $cat->term_id,
                                            
'name' => $cat->name,
                                            
'slug' => $cat->slug
                                        
);
                                        
$p->categories[] = $category;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    function 
load_count_item(database $database$orders$arr_ids_string){
        global 
$table_woo_woocommerce_order_items;

        
$query "SELECT order_id, COUNT(order_id) AS count_item FROM $table_woo_woocommerce_order_items WHERE order_id IN ($arr_ids_string) AND order_item_type = 'line_item' GROUP BY order_id;";
        
$products $database->run_sql_return_array($query);

        if(
$products != null){
            foreach(
$orders as $o){
                if(!isset(
$o->count_item)) $o->count_item 0;
                
                foreach(
$products as $p){
                    if(
$p->order_id == $o->ID){
                        
$o->count_item += $p->count_item;
                    }
                }
            }
        }
    }

    function 
load_refund_item(database $database$orders$arr_ids_string){
        
$table_posts $GLOBALS['table_posts'];
        global 
$table_post_meta;

        
$query "SELECT post.post_parent AS parent_product_id, meta.post_id, meta.meta_key, meta.meta_value FROM $table_post_meta as meta
        INNER JOIN 
$table_posts AS post ON post.ID = meta.post_id
        WHERE meta.post_id IN (SELECT ID FROM 
$table_posts WHERE post_parent IN ($arr_ids_string) AND post_type = 'shop_order_refund');";
        
// die($query);
        
$metas $database->run_sql_return_array($query);
        if(
$metas != null){

            
$map $GLOBALS['map_order'];
            
$not_map $GLOBALS['not_map_order'];

            foreach(
$orders as $o){
                
$order_id $o->ID;
                
$o->refunds = array();

                foreach(
$metas as $m){
                    if(
$m->parent_product_id == $order_id){
                        
//Tim lai object refund
                        
$refund null;
                        foreach(
$o->refunds as $temp){
                            if(
$temp->post_id == $m->post_id){
                                
$refund $temp;
                                break;
                            }
                        }
                        
// Neu khong tim thay thi khoi tao moi
                        
if($refund == null){
                            
$refund = new stdClass();
                            
$refund->post_id $m->post_id;
                            
$o->refunds[] = $refund;
                        }
                        
                        
$meta_key $m->meta_key;
                        
$meta_value $m->meta_value;

                        if(isset(
$map[$meta_key])){
                            
$refund->{$map[$meta_key]} = $meta_value;
                            
// echo($meta_key);
                        
}
                    }
                }
            }
        }
    }
}

$order_function = new order_functions();
$GLOBALS['order_function'] = $order_function;