SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | namespace App\Http\Controllers; | |
4 | ||
5 | use Illuminate\Http\Request; | |
6 | use App\Http\Requests; | |
7 | use Illuminate\Support\Facades\Input; | |
8 | use Validator; | |
9 | use Illuminate\Support\Facades\Redirect; | |
10 | use App\Datatables; | |
11 | use Illuminate\Support\Facades\Config; | |
12 | use Illuminate\Support\Facades\Session; | |
13 | use Illuminate\Support\Facades\Lang; | |
14 | use Illuminate\Support\Facades\Auth; | |
15 | use App\DeliveryOrder; | |
16 | use App\Clients; | |
17 | use App\Locations; | |
18 | use App\TruckRegistration; | |
19 | use Illuminate\Support\Facades\Event; | |
20 | use App\Events\DO_CreatedEvent; | |
21 | use App\SmsHistory; | |
22 | use App\Jobs\SendSmsToNextTruck; | |
23 | use App\Http\Controllers\Controller; | |
24 | use Illuminate\Support\Facades\DB; | |
25 | use App\Settings; | |
26 | use App\DeliveryOrderRatio; | |
27 | use App\Traders; | |
28 | use App\Console\Commands\SendNextSms; | |
29 | use stdClass; | |
30 | ||
31 | class DeliveryOrderController extends Controller | |
32 | { | |
33 | public function __construct(DeliveryOrder $do, SmsHistory $smsh, TruckRegistration $tr) | |
34 | { | |
35 | $this->deliveryorder = $do; | |
36 | $this->truck = $tr; | |
37 | $this->sms_history = $smsh; | |
38 | } | |
39 | ||
40 | public function create() | |
41 | { | |
42 | if(!Auth::user()->can("deliveryorder.create")) { | |
43 | ||
44 | Session::flash('danger', Lang::get('messages.access_denied')); | |
45 | return Redirect::route('home'); | |
46 | ||
47 | } | |
48 | ||
49 | $model = new DeliveryOrder; | |
50 | ||
51 | $importers = array('' => '--Please Select--') + Clients::lists('name', 'id')->toArray(); | |
52 | $traders = array('' => '--Please Select--') + Traders::lists('name', 'id')->toArray(); | |
53 | $truck_from = Config::get('customsettings.truck_from'); | |
54 | $locations = array('' => '--Please Select--') + Locations::lists('location', 'id')->toArray(); | |
55 | $truck_nos = array('' => '--Please Select--') + TruckRegistration::lists('truck_no', 'id')->toArray(); | |
56 | ||
57 | return View('deliveryorders.create', compact('model', 'importers', 'truck_from', 'locations', 'truck_nos','traders')); | |
58 | } | |
59 | ||
60 | public function store() | |
61 | { | |
62 | if(!Auth::user()->can("deliveryorder.create")) { | |
63 | ||
64 | Session::flash('danger', Lang::get('messages.access_denied')); | |
65 | return Redirect::route('home'); | |
66 | ||
67 | } | |
68 | $input = Input::except('_token'); | |
69 | ||
70 | $rules = DeliveryOrder::$rules; //array('do_no' => 'required|unique:delivery_orders,do_no') + | |
71 | ||
72 | if(isset($input['truck_from']) && $input['truck_from'] == 'importer') | |
73 | { | |
74 | $rules['truck'] = 'required'; | |
75 | $rules['contact_no'] = 'required|numeric'; | |
76 | $rules['driver_license_no'] = 'required'; | |
77 | } | |
78 | $validation = Validator::make($input, $rules); | |
79 | ||
80 | if ($validation->passes()) | |
81 | { | |
82 | $input['status'] = 'pending'; | |
83 | ||
84 | if($input['truck_from'] == 'importer') | |
85 | { | |
86 | $input['truck_no'] = $input['truck']; | |
87 | ||
88 | if($input['truck_no'] != '') | |
89 | $input['status'] = 'mapped'; | |
90 | ||
91 | unset($input['truck']); | |
92 | } | |
93 | ||
94 | $input['do_no'] = DeliveryOrder::getDONo(); | |
95 | ||
96 | $DO = $this->deliveryorder->create($input); | |
97 | ||
98 | if(isset($input['truck_from']) && $input['truck_from'] == 'internal' && $DO->status == 'pending') | |
99 | { | |
100 | Event::fire(new DO_CreatedEvent($DO)); | |
101 | } | |
102 | ||
103 | if(isset($input['truck_from']) && $input['truck_from'] == 'importer') { | |
104 | //$this->deliveryorder->do_truck_mapping($DO->id); | |
105 | #Send confirmation sms | |
106 | $this->sendConfirmationSms($DO->id, $DO->do_no, 0, $DO->truck_no, $DO->driver_license_no, $DO->contact_no, 0); | |
107 | } | |
108 | ||
109 | Session::flash('success', Lang::get('messages.deliveryorder_create_success')); | |
110 | return Redirect::route('deliveryorder.index'); | |
111 | } | |
112 | ||
113 | return Redirect::route('deliveryorder.create') | |
114 | ->withInput() | |
115 | ->withErrors($validation) | |
116 | ->with('message', 'There were validation errors.'); | |
117 | } | |
118 | ||
119 | public function edit($id) | |
120 | { | |
121 | if(!Auth::user()->can("deliveryorder.update")) { | |
122 | ||
123 | Session::flash('danger', Lang::get('messages.access_denied')); | |
124 | return Redirect::route('home'); | |
125 | ||
126 | } | |
127 | $model = $this->deliveryorder | |
128 | ->select(array( | |
129 | 'delivery_orders.id', | |
130 | 'delivery_orders.do_no', | |
131 | 'delivery_orders.importer', | |
132 | 'delivery_orders.trader', | |
133 | 'delivery_orders.truck_from', | |
134 | 'delivery_orders.location', | |
135 | 'delivery_orders.driver_license_no', | |
136 | 'delivery_orders.contact_no', | |
137 | 'delivery_orders.mapping_time', | |
138 | 'delivery_orders.entry_time', | |
139 | 'delivery_orders.load_in_truck', | |
140 | 'delivery_orders.out_time', | |
141 | 'delivery_orders.late_mark', | |
142 | 'delivery_orders.penalty_amount', | |
143 | 'delivery_orders.truck_no as importer_truck_no', | |
144 | 'trucks.truck_no', | |
145 | 'trucks.id as truck_id' | |
146 | )) | |
147 | ->leftJoin('trucks','delivery_orders.truck','=','trucks.id') | |
148 | ->where('delivery_orders.id',$id) | |
149 | ->first(); | |
150 | ||
151 | $model->entry_time = ($model->entry_time != '0000-00-00 00:00:00') ? date('d/m/Y h:i a', strtotime($model->entry_time)) : ''; | |
152 | $model->out_time = ($model->out_time != '0000-00-00 00:00:00') ? date('d/m/Y h:i a', strtotime($model->out_time)) : ''; | |
153 | ||
154 | $traders = array('' => '--Please Select--') + Traders::lists('name', 'id')->toArray(); | |
155 | $importers = array('' => '--Please Select--') + Clients::lists('name', 'id')->toArray(); | |
156 | $truck_from = Config::get('customsettings.truck_from'); | |
157 | $locations = array('' => '--Please Select--') + Locations::lists('location', 'id')->toArray(); | |
158 | $truck_nos = array('' => '--Please Select--') + TruckRegistration::lists('truck_no', 'id')->toArray(); | |
159 | $inout_time_duration_allowed = Config::get('customsettings.inout_time_duration_allowed'); | |
160 | ||
161 | $permission_general_details = (Auth::user()->can("deliveryorder.generaldetails_update")) ? true : false; | |
162 | $permission_mapping_details = (Auth::user()->can("deliveryorder.mappingdetails")) ? true : false; | |
163 | $permission_entry_details = (Auth::user()->can("deliveryorder.entrydetails_update")) ? true : false; | |
164 | ||
165 | return View('deliveryorders.edit', compact('model', 'importers', 'truck_from', 'locations', 'inout_time_duration_allowed','permission_general_details','permission_mapping_details','permission_entry_details', 'truck_nos','traders')); | |
166 | } | |
167 | ||
168 | public function update($id) | |
169 | { | |
170 | if(!Auth::user()->can("deliveryorder.update")) { | |
171 | ||
172 | Session::flash('danger', Lang::get('messages.access_denied')); | |
173 | return Redirect::route('home'); | |
174 | ||
175 | } | |
176 | $input = Input::except('_token','_method'); | |
177 | $deliveryorder_details = $this->deliveryorder->find($id); | |
178 | $rules = DeliveryOrder::$rules; //array('do_no' => 'required|unique:delivery_orders,do_no,'.$id) + | |
179 | //date validation | |
180 | if(isset($input['truck_from']) && $input['truck_from'] === 'importer' && $deliveryorder_details->status == 'mapped') | |
181 | { | |
182 | $rules['contact_no'] = 'required|numeric'; | |
183 | $rules['driver_license_no'] = 'required'; | |
184 | $rules['truck'] = 'required'; | |
185 | } | |
186 | else if(isset($input['truck_from']) && $input['truck_from'] === 'internal') | |
187 | { | |
188 | $input['truck_no'] = ''; | |
189 | $input['contact_no'] = ''; | |
190 | } | |
191 | ||
192 | ||
193 | if((Auth::user()->can("deliveryorder.generaldetails_update") || Auth::user()->can("deliveryorder.mappingdetails")) && $deliveryorder_details->status == 'mapped') | |
194 | { | |
195 | $rules['truck'] = 'required'; | |
196 | } | |
197 | ||
198 | $rules['do_no'] = ''; | |
199 | unset($input['do_no']); | |
200 | ||
201 | if(isset($input['entry_time']) && !empty($input['entry_time'])) | |
202 | { | |
203 | $rules['out_time'] = 'required|enddatetime_after_startdatetime:entry_time'; | |
204 | //$messages["validation.enddatetime_after_startdatetime"] = ""; | |
205 | } | |
206 | //else | |
207 | { | |
208 | //$messages = array(); | |
209 | } | |
210 | $validation = Validator::make($input, $rules); | |
211 | ||
212 | if ($validation->passes()) | |
213 | { | |
214 | $do_truck_old = $deliveryorder_details->truck; | |
215 | if(isset($input['entry_time']) && !empty($input['entry_time'])) | |
216 | { | |
217 | $input['entry_time'] = date_format(date_create_from_format('d/m/Y h:i a', Input::get('entry_time')), 'Y-m-d H:i:s'); | |
218 | $input['out_time'] = date_format(date_create_from_format('d/m/Y h:i a', $input['out_time']), 'Y-m-d H:i:s'); | |
219 | } | |
220 | if(isset($input['truck_from']) && $input['truck_from'] === 'importer') | |
221 | { | |
222 | $input['truck_no'] = $input['truck']; | |
223 | $input['truck'] = 0; | |
224 | ||
225 | if($input['truck_no'] != '') | |
226 | $input['status'] = 'mapped'; | |
227 | } | |
228 | else if(isset($input['truck_from']) && $input['truck_from'] === 'internal' && $input['truck'] != '') | |
229 | { | |
230 | $input['truck_no'] = ''; | |
231 | $input['status'] = 'mapped'; | |
232 | } | |
233 | // | |
234 | ||
235 | $deliveryorder_details->update($input); | |
236 | ||
237 | $do_details_new = $this->deliveryorder->find($id); | |
238 | ||
239 | if(isset($input['truck_from']) && $input['truck_from'] === 'internal' && $input['truck'] != '' && $do_truck_old != $input['truck']) | |
240 | { | |
241 | $truck_det = $this->truck->find($do_details_new->truck); | |
242 | $license = (trim($input['driver_license_no']) != '') ? $input['driver_license_no'] : $truck_det->driver_license_no; | |
243 | ||
244 | $this->sendConfirmationSms($do_details_new->id, $do_details_new->do_no, $do_details_new->truck, $truck_det->truck_no, $license, $truck_det->contact_no, 0); | |
245 | } | |
246 | else if(isset($input['truck_from']) && $input['truck_from'] === 'importer' && $input['truck_no'] != '' && $do_truck_old != $input['truck_no']) | |
247 | { | |
248 | $this->sendConfirmationSms($do_details_new->id, $do_details_new->do_no, 0, $do_details_new->truck_no, $do_details_new->driver_license_no, $do_details_new->contact_no, 0); | |
249 | } | |
250 | ||
251 | if($deliveryorder_details->truck != '') | |
252 | { | |
253 | TruckRegistration::calculateTotalPenalty($deliveryorder_details->truck); | |
254 | } | |
255 | ||
256 | Session::flash('success', Lang::get('messages.deliveryorder_update_success')); | |
257 | return Redirect::route('deliveryorder.index'); | |
258 | } | |
259 | ||
260 | return Redirect::route('deliveryorder.edit', $id) | |
261 | ->withInput() | |
262 | ->withErrors($validation) | |
263 | ->with('message', 'There were validation errors.'); | |
264 | ||
265 | } | |
266 | public function destroy($id) | |
267 | { | |
268 | //if(Auth::user()->can("truck.delete")) | |
269 | { | |
270 | $deliveryorder = $this->deliveryorder->find($id); | |
271 | $deliveryorder->delete(); | |
272 | return Redirect::route('deliveryorder.index') | |
273 | ->with('success', Lang::get('messages.deliveryorder_delete_success')); | |
274 | } | |
275 | // else | |
276 | // { | |
277 | // Session::flash('danger', Lang::get('messages.location_delete_failed')); | |
278 | // return Redirect::route('location.index'); | |
279 | // } | |
280 | } | |
281 | ||
282 | public function destroyMany() { | |
283 | ||
284 | if(Auth::user()->can("deliveryorder.delete")) { | |
285 | ||
286 | $ids = Input::get('id'); | |
287 | $this->deliveryorder->destroy($ids); | |
288 | ||
289 | } else { | |
290 | ||
291 | Session::flash('danger', Lang::get('messages.access_denied')); | |
292 | return Redirect::route('home'); | |
293 | ||
294 | } | |
295 | ||
296 | return Redirect::route('deliveryorder.index') | |
297 | ->with('success', Lang::get('messages.deliveryorder_delete_success')); | |
298 | } | |
299 | ||
300 | public function index() | |
301 | { | |
302 | if(!Auth::user()->can("deliveryorder.view")) { | |
303 | ||
304 | Session::flash('danger', Lang::get('messages.access_denied')); | |
305 | return Redirect::route('home'); | |
306 | ||
307 | } | |
308 | return View( 'deliveryorders.index' ); | |
309 | } | |
310 | ||
311 | public function show($id) | |
312 | { | |
313 | if(!Auth::user()->can("deliveryorder.update")) { | |
314 | ||
315 | Session::flash('danger', Lang::get('messages.access_denied')); | |
316 | return Redirect::route('home'); | |
317 | } | |
318 | ||
319 | $delivery_order = $this->deliveryorder | |
320 | ->select(array( | |
321 | 'delivery_orders.id', | |
322 | 'delivery_orders.do_no', | |
323 | 'delivery_orders.truck_from', | |
324 | 'locations.location', | |
325 | 'delivery_orders.driver_license_no', | |
326 | 'delivery_orders.contact_no as importer_contact_no', | |
327 | 'trucks.contact_no as internal_contact_no', | |
328 | 'delivery_orders.mapping_time', | |
329 | 'delivery_orders.entry_time', | |
330 | 'delivery_orders.load_in_truck', | |
331 | 'delivery_orders.out_time', | |
332 | 'delivery_orders.late_mark', | |
333 | 'delivery_orders.penalty_amount', | |
334 | 'delivery_orders.truck_no as importer_truck_no', | |
335 | 'trucks.truck_no', | |
336 | 'traders.name as trader_name', | |
337 | 'clients.name as importer_name' | |
338 | )) | |
339 | ->leftJoin('trucks','delivery_orders.truck','=','trucks.id') | |
340 | ->leftJoin('traders','delivery_orders.trader','=','traders.id') | |
341 | ->leftJoin('clients','delivery_orders.importer','=','clients.id') | |
342 | ->leftJoin('locations','delivery_orders.location','=','locations.id') | |
343 | ->where('delivery_orders.id',$id) | |
344 | ->first(); | |
345 | ||
346 | $delivery_order->entry_time = ($delivery_order->entry_time != '0000-00-00 00:00:00') ? date('d/m/Y h:i:s', strtotime($delivery_order->entry_time)) : ''; | |
347 | $delivery_order->out_time = ($delivery_order->out_time != '0000-00-00 00:00:00') ? date('d/m/Y h:i:s', strtotime($delivery_order->out_time)) : ''; | |
348 | /* | |
349 | $traders = array('' => '--Please Select--') + Traders::lists('name', 'id')->toArray(); | |
350 | $importers = array('' => '--Please Select--') + Clients::lists('name', 'id')->toArray(); | |
351 | $truck_from = Config::get('customsettings.truck_from'); | |
352 | $locations = array('' => '--Please Select--') + Locations::lists('location', 'id')->toArray(); | |
353 | $truck_nos = array('' => '--Please Select--') + TruckRegistration::lists('truck_no', 'id')->toArray(); | |
354 | $inout_time_duration_allowed = Config::get('customsettings.inout_time_duration_allowed'); | |
355 | */ | |
356 | $permission_general_details = (Auth::user()->can("deliveryorder.generaldetails_update")) ? true : false; | |
357 | $permission_mapping_details = (Auth::user()->can("deliveryorder.mappingdetails")) ? true : false; | |
358 | $permission_entry_details = (Auth::user()->can("deliveryorder.entrydetails_update")) ? true : false; | |
359 | ||
360 | return View('deliveryorders.show', compact('delivery_order','permission_general_details','permission_mapping_details','permission_entry_details')); | |
361 | ||
362 | } | |
363 | ||
364 | public static function getDatatableAjax($user) { | |
365 | ||
366 | $query = DeliveryOrder::select(array( | |
367 | 'delivery_orders.id', | |
368 | 'delivery_orders.created_at', | |
369 | 'delivery_orders.do_no', | |
370 | 'delivery_orders.truck_from', | |
371 | 'clients.name as importer_name', | |
372 | 'traders.name as trader_name', | |
373 | 'locations.location', | |
374 | 'delivery_orders.status', | |
375 | )) | |
376 | ->leftJoin('locations','delivery_orders.location','=','locations.id') | |
377 | ->leftJoin('clients','delivery_orders.importer','=','clients.id') | |
378 | ->leftJoin('traders','delivery_orders.trader','=','traders.id') | |
379 | ->whereRaw('delivery_orders.deleted_at is null'); | |
380 | ||
381 | $button_group_open = '<div class="btn-group"> | |
382 | <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"> | |
383 | Action | |
384 | <span class="caret"></span> | |
385 | </a>'; | |
386 | ||
387 | $ul_open = '<ul class="dropdown-menu">'; | |
388 | ||
389 | $update_access = $user->can("deliveryorder.update"); | |
390 | $delete_access = $user->can("deliveryorder.delete"); | |
391 | $view_access = $user->can("deliveryorder.view"); | |
392 | ||
393 | if ($view_access) { | |
394 | $view_action = '<li> | |
395 | <a href="{{ route(\'deliveryorder.show\', $id) }}"> | |
396 | <i class="fa fa-info-circle"></i> View | |
397 | </a> | |
398 | </li>'; | |
399 | } else | |
400 | $view_action = ''; | |
401 | if ($update_access) { | |
402 | $edit_action = '<li> | |
403 | <a href="{{ route(\'deliveryorder.edit\', $id) }}"> | |
404 | <i class="fa fa-edit"></i> Edit | |
405 | </a> | |
406 | </li>'; | |
407 | } else | |
408 | $edit_action = ''; | |
409 | ||
410 | if ($delete_access) { | |
411 | $delete_action = '<li> | |
412 | <a data-message="This action cannot be undone. Do you still want to delete this delivery order?" data-method="DELETE" class="action_link" href="{{ route("deliveryorder.destroy", $id) }}"> | |
413 | <i class="fa fa-trash-o"></i> Delete | |
414 | </a> | |
415 | </li>'; | |
416 | } else | |
417 | $delete_action = ''; | |
418 | ||
419 | $ul_close = '</ul">'; | |
420 | $button_group_close = '</div>'; | |
421 | ||
422 | if ($view_action || $update_access || $delete_access) | |
423 | $action = $button_group_open . $ul_open .$view_action. $edit_action . $delete_action . $ul_close . $button_group_close; | |
424 | else | |
425 | $action = '<span class="permission-denied">Permission Denied</span>'; | |
426 | ||
427 | return Datatables::of ( $query )->add_column ( 'action', $action ) | |
428 | ->edit_column('truck_from', '{{ ucfirst($truck_from) }}') | |
429 | ->edit_column('status', '{{ ucfirst($status) }}') | |
430 | ->edit_column('do_no', '<a href="{{ route(\'deliveryorder.show\', $id) }}"> {{ $do_no }} </a>') | |
431 | ->make (); | |
432 | } | |
433 | ||
434 | public static function sendRequestSms($DO, $truck) | |
435 | { | |
436 | - | $message = 'Forward message to: 9223011234, Location:'.$DO->location.'. Reply:<yes/no> DO:'.$DO->do_no.' License:<optional>'; |
436 | + | $location = Locations::find($delivery_order->location); |
437 | $message = 'Forward message to: 9223011234, Location:'.$location->location.'. Reply:<yes/no> DO:'.$DO->do_no.' License:<optional>'; | |
438 | ||
439 | $smsData = array( | |
440 | 'do_id' => $DO->id, | |
441 | 'truck_id' => $truck->id, | |
442 | 'to_contact_no' => $truck->contact_no, | |
443 | 'type' => 'request', | |
444 | 'message' => $message, | |
445 | ||
446 | ); | |
447 | ||
448 | $sms = SmsHistory::create($smsData); | |
449 | ||
450 | $phone_no = $truck->contact_no; | |
451 | $msg = rawurlencode($message); | |
452 | ||
453 | $url = 'http://bulkpush.mytoday.com/BulkSms/SingleMsgApi?feedid=358334&username=9821682191&password=dtjgt&To='.$phone_no.'&Text='.$msg.'&time=&senderid='; | |
454 | SendNextSms::callurl($url); | |
455 | } | |
456 | ||
457 | public function parseSms() | |
458 | { | |
459 | $msisdn = Input::get('msisdn'); | |
460 | $circle = Input::get('circle'); | |
461 | $operator = Input::get('operator'); | |
462 | $message = Input::get('message'); | |
463 | ||
464 | $message = trim($message); | |
465 | $reply = $this->getValue(stristr($message, 'reply:'));// | |
466 | ||
467 | $do_no = $this->getValue(stristr($message, 'do:')); | |
468 | $license = $this->getValue(stristr($message, 'license:')); | |
469 | ||
470 | $do_result = DeliveryOrder::where('do_no',trim($do_no))->select('id')->first(); | |
471 | if(count($do_result)) | |
472 | { | |
473 | $do_id = $do_result->id; | |
474 | } | |
475 | else | |
476 | { | |
477 | return 'do_no not valid'; | |
478 | } | |
479 | //DB::enableQueryLog(); | |
480 | if($msisdn != '') | |
481 | { | |
482 | $new_msisdn = str_replace('91', '', $msisdn); | |
483 | $tr_result = TruckRegistration::leftJoin('sms_history','sms_history.truck_id','=','trucks.id') | |
484 | ->where('trucks.contact_no', $new_msisdn) | |
485 | ->where('sms_history.do_id', $do_id) | |
486 | ->whereRaw('sms_history.created_at >= DATE_SUB( NOW( ) , INTERVAL '.Settings::where('setting_key', 'delay_between_sms')->value('setting_value').' MINUTE)') | |
487 | ->orderBy('sms_history.created_at','desc') | |
488 | ->first(); | |
489 | //dd(DB::getQueryLog()); | |
490 | //exit(); | |
491 | if(count($tr_result)) | |
492 | { | |
493 | // echo "<pre>"; | |
494 | // print_r($tr_result); | |
495 | // exit(); | |
496 | $truck_id = $tr_result->truck_id; | |
497 | } | |
498 | else | |
499 | { | |
500 | return 'Contact no. not found in database'; | |
501 | } | |
502 | } | |
503 | else | |
504 | { | |
505 | return 'contact_no not valid'; | |
506 | } | |
507 | if(trim($license) == '') | |
508 | { | |
509 | $license = $tr_result->driver_license_no; | |
510 | } | |
511 | ||
512 | $this->smsResponse($do_id, $truck_id, $message, strtolower($reply), $license); | |
513 | die; | |
514 | } | |
515 | ||
516 | public function getValue($str) | |
517 | { | |
518 | $colon_pos = strpos($str, ':'); | |
519 | $space_pos = strpos($str, ' '); | |
520 | ||
521 | #for last parameter | |
522 | if($space_pos === false) | |
523 | { | |
524 | $space_pos = strlen($str); | |
525 | } | |
526 | ||
527 | $value = $this->my_substr_function($str, $colon_pos+1, $space_pos); | |
528 | return $value; | |
529 | } | |
530 | ||
531 | #get string between two indexes | |
532 | public function my_substr_function($str, $start, $end) | |
533 | { | |
534 | return substr($str, $start, $end - $start); | |
535 | } | |
536 | ||
537 | public function smsResponse($do_no, $truck_no, $message, $reply, $license) | |
538 | { | |
539 | // print_r('reply yes'); | |
540 | // print_r($truck_no); | |
541 | // exit(); | |
542 | ||
543 | //for now do_no is do_id | |
544 | $delivery_order = $this->deliveryorder->find($do_no); | |
545 | $truck = $this->truck->find($truck_no); | |
546 | ||
547 | $request= $this->sms_history->select('id','to_contact_no') | |
548 | ->where('sms_history.do_id', $do_no) | |
549 | ->where('sms_history.truck_id', $truck_no) | |
550 | ->whereRaw('sms_history.created_at >= DATE_SUB( NOW( ) , INTERVAL '.Settings::where('setting_key', 'delay_between_sms')->value('setting_value').' MINUTE)') | |
551 | ->where('type','request') | |
552 | ->orderBy('created_at','desc')->get(); | |
553 | ||
554 | if(count($request) && !is_null($delivery_order) && $delivery_order->status == 'pending') | |
555 | { | |
556 | $request = $this->sms_history->select('id','to_contact_no')->where('do_id',$do_no)->where('truck_id',$truck_no)->orderBy('created_at','desc')->where('type','request')->first(); | |
557 | ||
558 | $smsData = array( | |
559 | 'do_id' => $do_no, | |
560 | 'truck_id' => $truck_no, | |
561 | 'to_contact_no' => $request->to_contact_no, | |
562 | 'type' => 'reply', | |
563 | 'message' => $message, | |
564 | 'request_id' => $request->id | |
565 | ||
566 | ); | |
567 | $this->sms_history->create($smsData); | |
568 | ||
569 | if($reply == 'yes') | |
570 | { | |
571 | #Map truck with DO | |
572 | $this->deliveryorder->where('id',$do_no)->update( | |
573 | array( | |
574 | 'status' => 'mapped', | |
575 | 'truck' => $truck_no, | |
576 | 'mapping_time' => date('Y-m-d H:i:s'), | |
577 | 'driver_license_no' => $license | |
578 | ) | |
579 | ); | |
580 | ||
581 | #Update sms_history table | |
582 | $this->sms_history->where('id',$request->id)->update(array('status' => 'accepted')); | |
583 | ||
584 | #Send confirmation sms | |
585 | $this->sendConfirmationSms($delivery_order->id, $delivery_order->do_no, $truck->id, $truck->truck_no, $license, $request->to_contact_no, $request->id); | |
586 | ||
587 | #increase the ratio value for the truck for the particular location | |
588 | $delivery_order_trucks = DeliveryOrder::select('trucks.location_ratio', 'delivery_orders.location', 'delivery_orders.truck') | |
589 | ->leftjoin('trucks','delivery_orders.truck','=','trucks.id') | |
590 | ->find($do_no); //start here | |
591 | $location_ratio_arr = json_decode($delivery_order_trucks->location_ratio, true); // true, to get json as an array | |
592 | ||
593 | if(array_key_exists($delivery_order_trucks->location, $location_ratio_arr)) | |
594 | { | |
595 | $location_ratio_arr[$delivery_order_trucks->location]++; | |
596 | ||
597 | $this->truck->where('id',$delivery_order_trucks->truck)->update(array('location_ratio' => json_encode($location_ratio_arr))); | |
598 | } | |
599 | ||
600 | #check and reset location ratio against each truck if trips done for all locations for specified no of times | |
601 | $this->checkAndResetLocationRatio($truck_no); | |
602 | } | |
603 | else if($reply == 'no') | |
604 | { | |
605 | #Update sms_history table | |
606 | $this->sms_history->where('do_id',$do_no )->where('truck_id',$truck_no)->where('type','request')->update(array('status' => 'rejected')); | |
607 | } | |
608 | } | |
609 | die('Done in SMS Response'.$do_no.'::'.$truck_no.'::'.$message); | |
610 | } | |
611 | ||
612 | public function sendConfirmationSms($do_id, $do_no, $truck_id, $truck_no, $license, $to_contact_no, $request_id) | |
613 | { | |
614 | $confirmation_message = 'Confirmation SMS for DO:'.$do_no.' Truck:'.$truck_no.' '. 'License:'.$license; | |
615 | $confirmation_sms_data = array( | |
616 | 'do_id' => $do_id, | |
617 | 'truck_id' => $truck_id, | |
618 | 'to_contact_no' => $to_contact_no, | |
619 | 'type' => 'confirmation', | |
620 | 'message' => $confirmation_message, | |
621 | 'request_id' => $request_id | |
622 | ); | |
623 | $this->sms_history->create($confirmation_sms_data); | |
624 | ||
625 | $phone_no = $to_contact_no; | |
626 | $msg = rawurlencode($confirmation_message); | |
627 | ||
628 | $url = 'http://bulkpush.mytoday.com/BulkSms/SingleMsgApi?feedid=358334&username=9821682191&password=dtjgt&To='.$phone_no.'&Text='.$msg.'&time=&senderid='; | |
629 | SendNextSms::callurl($url); | |
630 | } | |
631 | ||
632 | public function checkAndResetLocationRatio($truck_id) | |
633 | { | |
634 | $truck = $this->truck->find($truck_id); | |
635 | if(!is_null($truck)) | |
636 | { | |
637 | $location_values = Locations::lists('ratio_value', 'id')->toArray(); | |
638 | //$no_of_locations = count($location_values); | |
639 | $location_ratio_array = json_decode($truck->location_ratio, true); | |
640 | $location_ratio_array_update = $location_ratio_array; | |
641 | $no_of_truck_locations = count($location_ratio_array); | |
642 | $counter = 0; | |
643 | foreach ($location_ratio_array as $location => $value) | |
644 | { | |
645 | if(array_key_exists($location, $location_values) && $value >= $location_values[$location]) | |
646 | { | |
647 | $counter++; | |
648 | $location_ratio_array_update[$location] = 0; | |
649 | } | |
650 | } | |
651 | ||
652 | #reset only if all location ratios are full | |
653 | if($counter == $no_of_truck_locations) | |
654 | { | |
655 | $truck->update(array('location_ratio' => json_encode($location_ratio_array_update))); | |
656 | } | |
657 | } | |
658 | } | |
659 | ||
660 | public function deliveryOrderRatioEdit() | |
661 | { | |
662 | if(!Auth::user()->can("deliveryorder.update")) | |
663 | { | |
664 | Session::flash('danger', Lang::get('messages.access_denied')); | |
665 | return Redirect::route('home'); | |
666 | ||
667 | } | |
668 | ||
669 | $locations = Locations::withTrashed()->lists('location', 'id')->toArray(); | |
670 | $active_locations = Locations::lists('id')->toArray(); | |
671 | $do_ratios = DeliveryOrderRatio::orderBy('id', 'desc')->limit(10)->get(array('date','location_id_ratio'))->toArray(); | |
672 | ||
673 | foreach ($do_ratios as $do_key => $ratio) | |
674 | { | |
675 | $ratio['location_id_ratio'] = json_decode($ratio['location_id_ratio'], TRUE); | |
676 | $total = 0; | |
677 | $ratio_value = array(); | |
678 | foreach($ratio['location_id_ratio'] as $key =>$value) | |
679 | { | |
680 | $total += $value; | |
681 | } | |
682 | ||
683 | $ratio['total'] = $total; | |
684 | foreach($ratio['location_id_ratio'] as $key =>$value) | |
685 | { | |
686 | array_push($ratio_value, ceil((($value/$total)*10))); | |
687 | } | |
688 | ||
689 | foreach ($locations as $loc_key => $loc_value) | |
690 | { | |
691 | if(!array_key_exists($loc_key, $ratio['location_id_ratio'])) | |
692 | $ratio['location_id_ratio'][$loc_key] = '-'; | |
693 | } | |
694 | ||
695 | $ratio['date'] = date_format(date_create_from_format('Y-m-d', $ratio['date']), 'd/m/Y'); | |
696 | ||
697 | $ratio['ratio'] = implode(':', $ratio_value); | |
698 | $do_ratios[$do_key] = $ratio; | |
699 | } | |
700 | ||
701 | return View('deliveryorders.delivery_order_ratios', compact('locations','do_ratios','active_locations')); | |
702 | } | |
703 | ||
704 | public function deliveryOrderRatioUpdate() | |
705 | { | |
706 | if(!Auth::user()->can("deliveryorder.update")) { | |
707 | ||
708 | Session::flash('danger', Lang::get('messages.access_denied')); | |
709 | return Redirect::route('home'); | |
710 | ||
711 | } | |
712 | ||
713 | $input = Input::except('_token','_method'); | |
714 | ||
715 | $location_inputs = Input::get('location'); | |
716 | $count = 0; | |
717 | foreach ($location_inputs as $key => $l_input) | |
718 | { | |
719 | if($l_input == '') | |
720 | $location_inputs[$key] = 0; | |
721 | ||
722 | if($location_inputs[$key] == 0) | |
723 | $count++; | |
724 | } | |
725 | ||
726 | if($count == count($location_inputs)) | |
727 | { | |
728 | Session::flash('danger', 'Atleast one Loction should have at least one Delivery Order'); | |
729 | return Redirect::route('deliveryorder.deliveryorderratiosedit') | |
730 | ->withInput(); | |
731 | } | |
732 | ||
733 | $rules = DeliveryOrderRatio::$rules; | |
734 | $validation = Validator::make($input, $rules); | |
735 | if ($validation->passes()) | |
736 | { | |
737 | $data = array( | |
738 | 'date' => date_format(date_create_from_format('d/m/Y', $input['date']), 'Y-m-d'), | |
739 | 'location_id_ratio' => json_encode($location_inputs) | |
740 | ); | |
741 | DeliveryOrderRatio::insert($data); | |
742 | ||
743 | $total = 0; | |
744 | foreach($location_inputs as $key =>$value) | |
745 | { | |
746 | $total += $value; | |
747 | } | |
748 | ||
749 | $str = ' (CASE '; | |
750 | foreach($location_inputs as $key =>$value) | |
751 | { | |
752 | $ratio_value = ceil((($value/$total)*10)); | |
753 | $str .= " WHEN id = $key THEN $ratio_value "; | |
754 | } | |
755 | $str .= ' END) '; | |
756 | ||
757 | DB::update("UPDATE locations SET ratio_value = $str "); | |
758 | ||
759 | Session::flash('success', Lang::get('messages.deliveryorder_ratios_update')); | |
760 | return Redirect::route('deliveryorder.deliveryorderratiosedit'); | |
761 | } | |
762 | ||
763 | return Redirect::route('deliveryorder.deliveryorderratiosedit') | |
764 | ->withInput() | |
765 | ->withErrors($validation) | |
766 | ->with('message', 'There were validation errors.'); | |
767 | } | |
768 | } |