/var/www/hkosl.com/littleark/webadmin/_cron_job.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
<?php
    
//this is a cron job function list
    
require_once 'configure.php';

    
set_time_limit(60);

    
//auto complete customer order and order item status from SHIPPED to COMPLETED

    
$today = new DateTime();

    
$check_shipped_date $today->modify('-' $master_settings["MAXIMUM_IDLE_DAY_FROM_SHIPPED_TO_COMPLETED"] . ' day');

    
//filter 1
    
$order_item_info OrderItem::whereDeleted(0)->whereRaw("status = ? and temp = ? and shipped_date <= ?", array("SHIPPED"0$check_shipped_date))->get();
    
//debug_log(111, $order_item_info);

    
if (!empty($order_item_info)) {
        foreach (
$order_item_info as $row) {
            
$supplier_info                                   get_supplier($row["supplier_id"], "ACTIVE");
            
$supplier_max_idle_day_from_shipped_to_completed $supplier_info["max_idle_day_from_shipped_to_completed"];
            if (empty(
$supplier_max_idle_day_from_shipped_to_completed) || $supplier_max_idle_day_from_shipped_to_completed $master_settings["MAXIMUM_IDLE_DAY_FROM_SHIPPED_TO_COMPLETED"])
                
$supplier_max_idle_day_from_shipped_to_completed $master_settings["MAXIMUM_IDLE_DAY_FROM_SHIPPED_TO_COMPLETED"];

            
$start_date  = new DateTime();
            
$since_start $start_date->diff(new DateTime($row["shipped_date"]));

            if (
$since_start->days >= $supplier_max_idle_day_from_shipped_to_completed) {
                
//auto complete this item
                
$order_item_id $row["id"];
                
$customer_id   $row["customer_id"];
                
$order_id      $row["order_id"];
                
//copy from order item completed
                
$sql        "update order_item set `status` = ? where id = ? and customer_id = ?";
                
$parameters = array("COMPLETED"$order_item_id$customer_id);
                
bind_pdo($sql$parameters);

                
$order_item_info get_order_item($customer_idnull0$order_id$order_item_id);
                
$order_item_info $order_item_info[0];

                
//give points (base on order item discounted price) to customer account
                
$sql        "insert into customer_point (customer_id, points, status, table_name, table_refid, createdate) values (?,?,?,?,?,?)";
                
$parameters = array($customer_idfloor($order_item_info["discounted_price"]), "APPROVED""order_item"$order_item_iddate("Y-m-d H:i:s"));
                
bind_pdo($sql$parameters);

                
$sql        "update customer set points=points+? where id = ?";
                
$parameters = array(floor($order_item_info["discounted_price"]), $customer_id);
                
bind_pdo($sql$parameters);

                
//update supplier order
                //get order supplier list
                
$order_supplier_list get_order_supplier_list(0$order_id);
                foreach (
$order_supplier_list as $supplier_id) {
                    
$sql        "select count(*) as order_item_num from order_item order_item where order_id = ? and temp = ? and deleted = ? and supplier_id = ?
            UNION ALL
            select count(*) as order_item_num from order_item order_item where order_id = ? and temp = ? and deleted = ? and `status` = ? and supplier_id = ?"
;
                    
$parameters = array($order_id00$supplier_id$order_id00"COMPLETED"$supplier_id);
                    
$result     bind_pdo($sql$parameters"selectall");

                    
//if all receive product number equal to supplier order product number
                    
if ($result[0]["order_item_num"] == $result[1]["order_item_num"]) {
                        
//update order status
                        
$sql        "update `supplier_order` set `status` = ? where order_id = ? and supplier_id = ? and temp = ? and deleted = ?";
                        
$parameters = array("COMPLETED"$order_id$supplier_id00);
                        
bind_pdo($sql$parameters);
                    }
                }

                
//update order
                
$sql        "select count(*) as supplier_order_num from supplier_order where order_id = ? and temp = ? and deleted = ?
            UNION ALL
            select count(*) as supplier_order_num from supplier_order where order_id = ? and temp = ? and deleted = ? and status = ?"
;
                
$parameters = array($order_id00$order_id00"COMPLETED");
                
$result     bind_pdo($sql$parameters"selectall");

                
//if all receive product number equal to supplier order product number
                
if ($result[0]["supplier_order_num"] == $result[1]["supplier_order_num"]) {
                    
//update order status
                    
$sql        "update `order` set `status` = ? where id = ? and temp = ? and deleted = ?";
                    
$parameters = array("COMPLETED"$order_id00);
                    
bind_pdo($sql$parameters);
                }
            }
        }
    }