/
move_asset.php
216 lines (186 loc) · 6.64 KB
/
move_asset.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
<?php
require_once 'support/config.php';
if(!isLoggedIn()){
toLogin();
die();
}
if(!AllowUser(array(1,2))){
redirect("index.php");
}
if($_POST['type']<>"out" && $_POST['type']<>"in" || (empty($_POST['id']))){
redirect("assets.php");
}
$inputs=$_POST;
switch ($inputs['type']) {
case 'out':
# Checkout
# Validate input
$error_msg="";
if(empty($inputs['user_id'])){
$error_msg.="Select a user.<br/>";
}
if(empty($inputs['checkout_date']) || $inputs['checkout_date']=="mm/dd/yyyy" ){
$error_msg.="Select checkout date.<br/>";
}
else{
try {
$checkout_date=format_date($inputs['checkout_date']);
} catch (Exception $e) {
$error_msg.="Invalid checkout date entered.<br/>";
}
if(!empty($checkout_date)){
$inputs['checkout_date']=$checkout_date;
}
else{
$inputs['checkout_date']="";
}
// die;
}
if(!empty($inputs['expected_checkin_date']) ){
// var_dump($inputs['expected_checkin_date']);
try {
$expected_checkin_date=format_date($inputs['expected_checkin_date']);
} catch (Exception $e) {
$error_msg.="Invalid expected check in date entered.<br/>";
}
if(!empty($expected_checkin_date)){
$inputs['expected_checkin_date']=$expected_checkin_date;
}
else{
$inputs['expected_checkin_date']="";
}
}
// var_dump($inputs);
// var_dump($error_msg);
// die;
if(!empty($error_msg)){
$_SESSION[WEBAPP]['frm_inputs']=$inputs;
Alert("You have the following errors: <br/>".$error_msg,"danger");
redirect("check_asset.php?id=".$inputs['id']."&type=".$inputs['type']);
die;
}
else
{
#Okay for Saving
// $selected_user=$con->myQuery("SELECT id,location_id FROM users WHERE id=?",array($inputs['user_id']))->fetch(PDO::FETCH_ASSOC);
#Validate if there is a selected user
$asset_inputs['id']=$inputs['id'];
$asset_inputs['asset_name']=$inputs['asset_name'];
$asset_inputs['user_id']=$inputs['user_id'];
$asset_inputs['check_out_date']=$inputs['checkout_date'];
$asset_inputs['expected_check_in_date']=$inputs['expected_checkin_date'];
$location_id=$con->myQuery("SELECT location_id FROM users WHERE id=?",array($_POST['user_id']))->fetchColumn();
if(empty($location_id)){
$asset_inputs['location_id']=0;
}
else
{
$asset_inputs['location_id']=$location_id;
}
#transaction here
$con->myQuery("UPDATE assets SET user_id=:user_id,asset_name=:asset_name,check_out_date=:check_out_date,expected_check_in_date=:expected_check_in_date,location_id=:location_id WHERE id=:id",$asset_inputs);
$activity_input['admin_id']=$_SESSION[WEBAPP]['user']['id'];
$activity_input['user_id']=$inputs['user_id'];
$activity_input['notes']=$inputs['notes'];
$activity_input['category_type_id']=1;
$activity_input['item_id']=$inputs['id'];
$con->myQuery("INSERT INTO activities(admin_id,user_id,action,notes,action_date,category_type_id,item_id) VALUES(:admin_id,:user_id,'Asset Checkout',:notes,NOW(),:category_type_id,:item_id)",$activity_input);
#end of transaction
}
break;
case 'in':
# Checkin
# Validate input
$error_msg="";
if(empty($inputs['asset_status_id'])){
$error_msg.="Select asset status.<br/>";
}
if(!empty($error_msg)){
Alert("You have the following errors: <br/>".$error_msg,"danger");
redirect("check_asset.php?id=".$inputs['id']."&type=".$inputs['type']);
die;
}
else
{
#Okay for Saving
$asset_inputs['id']=$inputs['id'];
$asset_inputs['asset_name']=$inputs['asset_name'];
#transaction here
$location_id=$_SESSION[WEBAPP]['user']['location_id'];
if(empty($location_id)){
$asset_inputs['location_id']=0;
}
else
{
$asset_inputs['location_id']=$location_id;
}
// var_dump($_POST);
// var_dump($asset_inputs);
$asset_inputs['asset_status_id']=$_POST['asset_status_id'];
// die;
$con->myQuery("UPDATE assets SET asset_status_id=:asset_status_id,user_id=0,asset_name=:asset_name,check_out_date='0000-00-00',expected_check_in_date='0000-00-00',location_id=:location_id WHERE id=:id",$asset_inputs);
$activity_input['admin_id']=$_SESSION[WEBAPP]['user']['id'];
$activity_input['user_id']="NULL";
$activity_input['notes']=$inputs['notes'];
$activity_input['category_type_id']=1;
$activity_input['item_id']=$inputs['id'];
// echo "INSERT INTO activities(admin_id,user_id,action,notes,action_date,category_type_id,item_id) VALUES({$activity_input['admin_id']},{$activity_input['user_id']},'Asset Checkin',{$activity_input['notes']},NOW(),{$activity_input['category_type_id']},{$activity_input['item_id']})";
// die;
$con->myQuery("INSERT INTO activities(admin_id,user_id,action,notes,action_date,category_type_id,item_id) VALUES(:admin_id,:user_id,'Asset Checkin',:notes,NOW(),:category_type_id,:item_id)",$activity_input);
#end of transaction
}
break;
default:
redirect("assets.php");
break;
}
Alert("Asset checked in.","success");
redirect("assets.php");
die;
if(!empty($_POST)){
//Validate form inputs
$inputs=$_POST;
$errors="";
if (empty($inputs['model_id'])){
$errors.="Select a model. <br/>";
}
if (empty($inputs['asset_status_id'])){
$errors.="Select a status for the asset. <br/>";
}
if (empty($inputs['asset_status_id'])){
$errors.="Select a status for the asset. <br/>";
}
if( empty($inputs['purchase_cost'])){
$inputs['purchase_cost']=0;
}
if($errors!=""){
Alert("You have the following errors: <br/>".$errors,"danger");
redirect("frm_assets.php");
die;
}
else{
//IF id exists update ELSE insert
if(empty($inputs['id'])){
//Insert
unset($inputs['id']);
$asset_tag=date("Ynd");
$stmt=$con->myQuery("INSERT INTO assets(asset_model_id,asset_status_id,serial_number,asset_name,purchase_date,purchase_cost,order_number,notes,location_id) VALUES(:model_id,:asset_status_id,:serial_number,:asset_name,:purchase_date,:purchase_cost,:order_number,:notes,:location_id)",$inputs);
$asset_id=$con->lastInsertId();
$asset_tag=date("Ynd").$asset_id;
$con->myQuery("UPDATE assets SET asset_tag=? WHERE id=?",array($asset_tag,$asset_id));
}
else{
//Update
$con->myQuery("UPDATE assets SET asset_model_id=:model_id,asset_status_id=:asset_status_id,serial_number=:serial_number,asset_name=:asset_name,purchase_date=:purchase_date,order_number=:order_number,purchase_cost=:purchase_cost,notes=:notes,location_id=:location_id WHERE id=:id",$inputs);
}
Alert("Save succesful","success");
redirect("assets.php");
}
die;
}
else{
redirect('index.php');
die();
}
redirect('index.php');
?>