/var/www/onesupportdemo.onesolution.hk/contract/add.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
<?php
    session_start
();
//checking here
    
$schedulecall 0;
    if (!empty(
$_POST["schedule_call_3hrs"])) {
        
$schedulecall++;
    }
    if (!empty(
$_POST["schedule_call_half_day"])) {
        
$schedulecall++;
    }
    if (!empty(
$_POST["schedule_call_full_day"])) {
        
$schedulecall++;
    }

    if(
$schedulecall 1){  //only one type of schedule call is allowed
        
echo "<script>alert('Only one option is allowed in Schedule Call!');history.back();</script>";
        exit;
    }

    if(
strtotime($_POST["contract_from"]) > strtotime($_POST["contract_to"])){
        echo 
"<script>alert('Contract From Date should before the Contract To Date!');history.back();</script>";
        exit;
    }


    require_once(
__DIR__ '/../checkuser.php');
//-----------------------------------------------------------------------------
// Check permission
//-----------------------------------------------------------------------------
//if (!Util::isAdmin()) {
//    redirectAndExit('index.php?message=No permission!');
//}

//-----------------------------------------------------------------------------
// Save if POST method
//-----------------------------------------------------------------------------
    
if (isPost()) {
        
$sql        "SELECT column_name FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = ?";
        
$parameters = array('sup_contract');
        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }
        
$columns $sth->fetchAll();

        foreach (
$columns as $column) {
            
$column $column['column_name'];
            if (isset(
$_POST[$column])) {
                
$contract[$column] = $_POST[$column];
            }
        }

        
$sql        "SELECT * FROM v_cm_customer_support V_CM_CUSTOMER_SUPPORT WHERE cust_id = ?";
        
$parameters = array($contract['customer_id']);
        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }
        
$customer $sth->fetch(PDO::FETCH_ASSOC);

        
//$contract['email1'] = $customer['email'];

        // Append record time
        
$now      date("Y-m-d H:i:s");
        
$contract array_merge($contract, array(
            
'createdate' => $now,
            
'createby'   => $_SESSION['webadmin']['id'],
            
'lastupdate' => $now,
            
'lastupby'   => $_SESSION['webadmin']['id'],
        ));

        
// Create contract
        
$columns    = array();
        
$values     = array();
        
$parameters = array();
        foreach (
$contract as $column => $value) {

            
/*if($column == "date_from"){
                $column = "contract_from";
            }

            if($column == "date_to"){
                $column = "contract_to";
            }*/

            
$columns[]    = $column;
            
$parameters[] = !strlen($value) ? null $value;
            
$values[]     = '?';
        }


        
$_contract_from $_POST["date_from"];
        
$_contract_to   $_POST["date_to"];
        
$_customer_id $_POST["customer_id"];

        
//check if that customer has exist contract
        
$sql "SELECT * FROM sup_contract WHERE contract_from <= ? and ? <= contract_to and customer_id = ? and status = ?";

        
$parameters4 = array($_contract_to$_contract_from$_customer_id"1");
        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters4)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }
        
$contract_customer $sth->fetch(PDO::FETCH_ASSOC);

        if(!empty(
$contract_customer)){ //that customer has opened contract
            
$_SESSION["exist_contract_id"] = $contract_customer['contract_id'];
            
redirectAndExit('index.php?message=Unsuccess! This customer already has a opened contract!');

        }

        
//contract does not exist
        
$sql "INSERT sup_contract (" implode(', '$columns) . ") VALUES (" implode(', '$values) . ")";

        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute($parameters)) {
            throw new 
Exception("sql execute statement failure: $sql");
        }
        
$contract['contract_id'] = $dbh->lastInsertId();

        
$_SESSION["remark_contract_id"] = $contract['contract_id'];

        
$data = array(
            
'contract_id' => $contract['contract_id'],
        );


        
//update the previous record with correct time

        
if (empty($_POST["location"])) {
            if (!empty(
$customer["billaddress"])) {
                
$location $customer["billaddress"];
            } else {
                
$location $customer["shipaddress"];
            }
        } else {
            
$location $_POST["location"];
        }

        
$sql "UPDATE sup_contract SET `contract_from` = ?, `contract_to` = ?, `location` = ? WHERE contract_id = ?";

        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception("sql prepare statement failure: $sql");
        }
        
$sth->setFetchMode(PDO::FETCH_ASSOC);
        if (!
$sth->execute(array($_POST["date_from"], $_POST["date_to"],$location$contract['contract_id']))) {
            throw new 
Exception("sql execute statement failure: $sql");
        }


        
//check the type of contract, no need to do with type 2 (package)
        
if($_POST["contract_type"] == 1){

            
$remarks $_POST["task_remarks"];
            
$titles $_POST["task_title"];
            
$task_category_ids $_POST["task_category_id"];


            
$arraynum $_POST["arraynum"];

                foreach(
$arraynum as $key2 => $rownum){
                    
$start_date_task $_POST["start_date_".$rownum];
                    
$end_date_task $_POST["end_date_".$rownum];

                    foreach(
$titles as $key => $title){ //each task add the start date and end date

                        
$_remarks $remarks[$key];
                        
$_task_category_id $task_category_ids[$key];

                        
$sql "insert into sup_period_task (contract_id, customer_id, task_title, task_remarks,task_category_id, createdate, createby, lastupdate, lastupby, start_date, end_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

                        
//echo $sql;
                        
$parameters = array($contract['contract_id'],$_customer_id$title$_remarks,$_task_category_id$now$_SESSION['webadmin']['id'], $now$_SESSION['webadmin']['id'], $start_date_task$end_date_task);

                        if (!(
$sth $dbh->prepare($sql))) {
                            throw new 
Exception("sql prepare statement failure: $sql");
                        }
                        
$sth->setFetchMode(PDO::FETCH_ASSOC);
                        if (!
$sth->execute($parameters)) {
                            throw new 
Exception("sql execute statement failure: $sql");
                        }

                    }
                    }




        }else if(
$_POST["contract_type"] == 2){

        }else{}



        
redirectAndExit("index.php?message=New Contract Created.");
    }