Order.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Helpers\CommonMethods;
  4. use App\Jobs\SendOrderNotifications;
  5. use App\Jobs\SendOrderPusherNotification;
  6. use App\Models\ExtraOptionItems;
  7. use App\Models\ExtraOptions;
  8. use App\Models\OrderActivities;
  9. use App\Models\OrderNotifications;
  10. use App\Models\Orders;
  11. use App\Models\Outlets;
  12. use App\Models\Restaurants;
  13. use App\Models\RestoSMSs;
  14. use App\Models\Discounts;
  15. use Carbon\Carbon;
  16. use App\Models\DiscountWithOrder;
  17. use App\Models\Loyalties;
  18. use App\Models\CustomerLoyaltyHistories;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\Artisan;
  21. use Illuminate\Support\Facades\Auth;
  22. use Illuminate\Support\Facades\Lang;
  23. use Pusher\Pusher;
  24. use Log;
  25. use Illuminate\Support\Facades\Http;
  26. use DB;
  27. class Order extends Controller
  28. {
  29. private $resto_id;
  30. function __construct(){
  31. }
  32. public function orders(){
  33. //$recent_till = Carbon::now()->subDays(2);;
  34. $this->resto_id = CommonMethods::getRestuarantID();
  35. $start_page = isset($request->start_page)?$request->start_page-1:0;
  36. $limit = env('ORDER_NUMBER_ITEMS');
  37. $offset = $start_page * $limit;
  38. $orders = Orders::where('resto_id',$this->resto_id)->whereIN('status' ,['Placed','Send_to_Kitchen','On_Road','Accepted'])->orderBy('created_at','DESC')->offset($offset)->limit($limit)->get()->toJson();
  39. // $orders = Auth::user()->restaurants->orders;
  40. $data = [
  41. 'orders' => $orders
  42. ];
  43. if(isset($_GET['type']) && $_GET['type']=="discount")
  44. return view('orders.orders-discount',$data);
  45. else
  46. return view('orders.orders',$data);
  47. }
  48. public function all_status_count(){
  49. $resto_id = CommonMethods::getRestuarantID();
  50. $this->resto_id = $resto_id;
  51. $orders = Orders::select(DB::raw(' count(status) as status_count'),'status')->where('resto_id',$this->resto_id)->whereIN('status' ,['Placed','Send_to_Kitchen','On_Road','Accepted'])->groupBy('status')->get();
  52. $order_statuses = ['Placed','Send_to_Kitchen','On_Road','Accepted'];
  53. $a = [];
  54. foreach($orders as $order){
  55. $status[$order->status] = array('status_count'=>$order->status_count,'status'=>$order->status);
  56. }
  57. // dd($status);
  58. $aa = [];
  59. foreach($order_statuses as $st){
  60. if(isset($status[$st]))
  61. $aa[] = $status[$st];
  62. else{
  63. $aa[] = array('status_count'=>0,'status'=>$st);
  64. }
  65. }
  66. return response()->json($aa);
  67. }
  68. public function load_live_order(Request $request){
  69. $this->resto_id = CommonMethods::getRestuarantID();
  70. $status=$request->status;
  71. $resto = Restaurants::find($this->resto_id);
  72. $time_zone = "Asia/Dubai";
  73. if(!empty($resto->time_zone)){
  74. config(['app.timezone' => $resto->time_zone]);
  75. date_default_timezone_set($resto->time_zone);
  76. $time_zone = $resto->time_zone;
  77. // dd(\Carbon\Carbon::now()->format('Y-m-d H:i:s'));
  78. }
  79. $limit = 5;
  80. // $exitCode = Artisan::call('cache:clear');
  81. // $exitCode = Artisan::call('route:clear');
  82. // $exitCode = Artisan::call('view:clear');
  83. // $exitCode = Artisan::call('config:cache');
  84. // $exitCode = Artisan::call('config:clear');
  85. $s = ['Placed','Send_to_Kitchen','On_Road','Accepted'];
  86. if($status=="new")
  87. {
  88. // $limit = 5;
  89. $s = ['Placed'];
  90. }
  91. if($status=="kitchen")
  92. $s = ['Accepted','Send_to_Kitchen'];
  93. if($status=="route")
  94. $s = ['On_Road'];
  95. //$orders = Orders::where('resto_id',$this->resto_id)->whereIN('status' ,$s)->orderBy('created_at','DESC')->paginate($limit);
  96. $start_page = isset($request->start_page)?$request->start_page-1:0;
  97. $order_limit = $request->number_of_orders;//2;//env('ORDER_NUMBER_ITEMS');
  98. $order_offset = $start_page * $order_limit;
  99. //dump($order_limit.' '.$order_offset);
  100. $orders = Orders::where('resto_id',$this->resto_id)->whereIn('status' ,$s)->skip($order_offset)->take($order_limit)->orderBy('created_at','DESC')->get();
  101. $orders_total = Orders::where('resto_id',$this->resto_id)->whereIn('status' ,$s)->count();
  102. $liveOrders = NULL;
  103. if(isset($orders) && $orders->count() > 0){
  104. foreach($orders as $order){
  105. $status = "";
  106. $bg="";
  107. $box_bg="";
  108. $remaining_min = 0;
  109. $order_delivery_time = !empty($order->delivery_preparation_time)?$order->delivery_preparation_time:0;
  110. $created_at = $order->created_at;
  111. // dump("Delivery Time : ".$order_delivery_time);
  112. $till_to = strtotime("+".$order_delivery_time.' minutes',strtotime($order->updated_at));
  113. // dump("Till To : ".date('H:i',$till_to));
  114. $updated_at = Carbon::parse(date('Y-m-d H:i',$till_to));
  115. $now = Carbon::now();
  116. // dump("Update time: ".$updated_at);
  117. $diff = $updated_at->diffInMinutes($now);
  118. $order_placed_diff = Carbon::parse($created_at)->diffInMinutes($now);
  119. // dump($order_placed_diff);
  120. // dump("Difference: ".$diff);
  121. if($diff <=$order_delivery_time )
  122. $remaining_min = $diff;
  123. // dump("Remain_min ".$remaining_min);
  124. if($order->status=="Placed"){
  125. $status = "New";
  126. $bg="blu-bg";
  127. $box_bg="bg-danger";
  128. $bg_color="#0ED0DF";
  129. }
  130. if($order->status=="Accepted" || $order->status=="Send_to_Kitchen"){
  131. $status = "In Prep";
  132. $bg="org-bg";
  133. $bg_color="orange";
  134. }
  135. if($order->status=="On_Road"){
  136. $status = "In Route";
  137. $bg = "blu-bg";
  138. $bg_color="#0ED0DF";
  139. }
  140. $liveOrders[] = array(
  141. 'id' => $order->id,
  142. 'order_ref' => $order->order_ref,
  143. 'campaign_type' => !empty($order->campaign_type) && $order->order_type!="dining"?ucwords($order->campaign_type):"Direct",
  144. 'status' => $status,
  145. 'bg'=>$bg,
  146. 'box_bg'=>$box_bg,
  147. 'bg_color'=> $bg_color,
  148. 'remaining_min'=>$remaining_min,
  149. 'remaining_min_milliseconds'=>strtotime($order->created_at),
  150. 'created_at' => Carbon::createFromTimeStamp(strtotime($order->created_at))->diffForHumans(),
  151. 'current_time'=>Carbon::now()->format('H:i'),
  152. 'updated_at'=> $updated_at->format('H:i'),
  153. 'placed_min'=>$order_placed_diff,
  154. );
  155. }
  156. }
  157. if(isset($liveOrders)){
  158. $liveOrders['pagination']= array(
  159. 'next_page'=>$start_page==0?2:$request->start_page+1,'total_orders'=>$orders_total
  160. );
  161. return response()->json(array('type'=>'success','orders'=>$liveOrders));
  162. }
  163. else
  164. echo json_encode(array('type'=>'error','message'=>'No order found'));
  165. }
  166. public function get_detail_json($id){
  167. $order = Orders::where('id',$id)->with(['order_with_discounts'])->first();
  168. $this->resto_id = CommonMethods::getRestuarantID();
  169. $resto = Restaurants::find($this->resto_id);
  170. $recipes = NULL;
  171. if(empty($order->selected_area_formatted) && $order->order_type=="delivery"){
  172. // dump($order->selected_area);
  173. $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($order->selected_area)."&key=AIzaSyBFh6fzq8G7dgWLfz8kccvTlmPCSI_uWXQ";
  174. $curl = curl_init();
  175. curl_setopt_array($curl, array(
  176. CURLOPT_URL => $url,
  177. CURLOPT_RETURNTRANSFER => true,
  178. CURLOPT_ENCODING => '',
  179. CURLOPT_MAXREDIRS => 10,
  180. CURLOPT_TIMEOUT => 0,
  181. CURLOPT_FOLLOWLOCATION => true,
  182. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  183. CURLOPT_CUSTOMREQUEST => 'GET',
  184. ));
  185. $response = curl_exec($curl);
  186. curl_close($curl);
  187. //echo $response;
  188. $result = (json_decode($response));
  189. $formatted_address = isset($result->results[0])?$result->results[0]->formatted_address:"";
  190. $order->selected_area_formatted = $formatted_address;
  191. $order->save();
  192. }
  193. $country_code = $resto->countries->country_code;
  194. $new_phone = isset($order->customers->users)?$order->customers->users->email:NULL;
  195. if(isset($order->customers) && isset($order->customers->users)){
  196. $pos = strpos($order->customers->users->email, (string)$country_code);
  197. if ($pos !== false) {
  198. $new_phone = substr_replace($order->customers->users->email, '', $pos, strlen(env('COUNTRY_CODE')));
  199. }
  200. }
  201. if(isset($order->orderItems) && $order->orderItems->count() > 0){
  202. $extra_price = 0;
  203. $recipes = [];$ex = [];
  204. foreach($order->orderItems as $item){
  205. $ex= NULL;
  206. $recipe = $item->recipes;
  207. $cate_name = "";
  208. $categories = isset($recipe->categories)?$recipe->categories->pluck('category_id'):NULL;
  209. //dd($categories);
  210. if($categories){
  211. $categories = \App\Models\Categories::whereIn('id',$categories)->pluck('name')->toArray();
  212. $cate_name = isset($categories[0])?$categories[0]:"";
  213. }
  214. $extra_options = NULL;
  215. $opt = [];
  216. if(!empty($item->extra_options)){
  217. $ss_item = [];
  218. $extra_options = json_decode($item->extra_options);
  219. $extra_options_array = (json_decode($item->extra_options,true));
  220. if(isset($extra_options_array['sub_items']) && count($extra_options_array['sub_items']) > 0){
  221. $extra_sub_options = $extra_options_array['sub_items'];
  222. foreach($extra_sub_options as $k=>$s){
  223. $itm = \App\ExtraOptionItems::find($s['id']);
  224. $ss_item[$k] = array('id'=>$itm->id,'name'=>$itm->name);
  225. }
  226. }
  227. //$opt = "<ul>";
  228. if(isset($extra_options)){
  229. foreach($extra_options as $option){
  230. //dump($option);
  231. if(isset($option->id)){
  232. $itm = \App\Models\ExtraOptionItems::find($option->id);
  233. if(isset($itm)){
  234. $ex[] = array('id'=>$option->id,'price'=>$option->price,'name'=>$itm->name,'name_ar'=>$itm->name_arabic ,'sub_items'=>isset($ss_item[$option->id])?$ss_item:null);
  235. // dump($ex);
  236. $opt[] = $itm->name;
  237. // $opt.="<li>".$itm->name.' <span class="ml-2 badge badge-danger">'.($itm->price).'</span>';
  238. $extra_price = $extra_price+$option->price;
  239. if(isset($option->sub_items)){
  240. foreach($option->sub_items as $sub){
  241. $itm = \App\ExtraOptionItems::find($sub->sub_item_id);
  242. $extra_price = $extra_price+$sub->price;
  243. }
  244. }
  245. //$opt.="</li>";
  246. }
  247. }
  248. }
  249. }
  250. // $opt.="</ul>";
  251. }
  252. if(isset($extra_options->color_size)){
  253. $ex = array('color'=>$extra_options->color_size->color,'size'=>$extra_options->color_size->size);
  254. }
  255. $resto_metas = \App\Helpers\CommonMethods::getRestoMetas($resto);
  256. $imgix = isset($resto_metas['IMGIX_SOURCE']) && !empty($resto_metas['IMGIX_SOURCE'])?$resto_metas['IMGIX_SOURCE']:"https://meemappaws.imgix.net";
  257. $recipes[] = array(
  258. 'recipe_name'=>isset($item->recipes)?$item->recipes->name:"",
  259. 'recipe_arabic_name'=>isset($item->recipes)?$item->recipes->arabic_name:"",
  260. 'recipe_image' => isset($item->recipes->main_images)?CommonMethods::changeImageLinkAWStoImgix($item->recipes->main_images->file_name, $imgix,
  261. env('PRODUCT_THUMBNAIL_IMG_HEIGHT'),
  262. env('PRODUCT_THUMBNAIL_IMG_WIDTH'),
  263. 100):"",
  264. 'quantity'=>$item->qty,
  265. 'item_price'=>$item->price,
  266. 'discount_amount'=>$item->discount_amount,
  267. 'discount_type'=>$item->discount_type,
  268. 'total_price'=>($item->qty*$item->price),
  269. 'extra_options'=>$ex
  270. );
  271. }
  272. }
  273. $status = "";
  274. $bg="";
  275. $next_action="";
  276. $next_status="";
  277. $bg_color="";
  278. if($order->status=="Placed"){
  279. $status = "New";
  280. $bg="bg-danger";
  281. $box_bg="bg-danger";
  282. $next_action='Accepted';
  283. $next_status="Accepted";
  284. }
  285. if($order->status=="Accepted" || $order->status=="Send_to_Kitchen"){
  286. $status = "In Prep";
  287. $bg="org-bg";
  288. $next_action=$order->order_type=="delivery"?'On Road':'Pick Up';
  289. $next_status="On_Road";
  290. $bg_color="orange";
  291. }
  292. if($order->status=="On_Road"){
  293. $status = "In Route";
  294. $bg = "blu-bg";
  295. $bg_color="#0ED0DF";
  296. $next_action=$order->order_type=="delivery"?'Delivered':'Picked Up';
  297. $next_status="Has_Delivered";
  298. }
  299. $address = "";
  300. $delivery_notes = "";
  301. if(isset($order->customers) && isset($order->customers->customer_addresses)){
  302. $address .= isset($order->customers->customer_addresses[0])?ucwords($order->customers->customer_addresses[0]->label):NULL;
  303. $address.= isset($order->customers->customer_addresses[0])?", ".$order->customers->customer_addresses[0]->area:NULL;
  304. $address.= isset($order->customers->customer_addresses[0])?",".$order->customers->customer_addresses[0]->address:NULL;
  305. }
  306. if(isset($order->customers) && isset($order->customers->customer_addresses)){
  307. $delivery_notes .= isset($order->customers->customer_addresses[0])?$order->customers->customer_addresses[0]->instructions:NULL;
  308. }
  309. $outlet = Outlets::with('delivery_feature')->find($order->outlet_id);
  310. $customer_name = $order->customer_name;
  311. if(empty($customer_name)){
  312. $customer_name = $order->customers->name;
  313. }
  314. $estimated_time = isset($outlet->delivery_feature)?explode(' - ',$outlet->delivery_feature->estimated_time):[];
  315. $time_from = count($estimated_time) > 0?$estimated_time[0]:0;
  316. $time_to = 0;
  317. $r = [];
  318. if(count($estimated_time) > 1)
  319. $r = explode(":",$estimated_time[1]);
  320. $time_to = count($r) > 1 ?$r[0]:0;
  321. $type = count($r) > 1 ?$r[1]:"DAYS";
  322. $estimated_time = array(
  323. 'preparation_time'=>$time_from,
  324. 'preparation_delivery_time'=>$time_to,
  325. 'time_type'=>$type
  326. );
  327. if(session('app_lang') !==null){
  328. $lang = session('app_lang');
  329. app()->setLocale($lang);
  330. }
  331. $label_next_status = trans(strtolower('label.'.$next_status));
  332. $languages = Lang::get('language');
  333. dd($languages);
  334. $languagesFlip = array_flip($languages);
  335. $order_delivery_time = Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->format('Y-m-d H:i');
  336. $order = array(
  337. 'order_ref' => str_pad($order->order_ref,6,0,STR_PAD_LEFT),
  338. 'estimated_code'=>$estimated_time,
  339. 'brand_name' => $resto->name,
  340. 'outlet_name' => isset($outlet)?$outlet->name:"None",
  341. 'order_type' => $order->order_type=="dining"?"Dine in":ucwords($order->order_type),
  342. 'delivery_at' => !empty($order->order_deliver_time)?Carbon::parse($order->order_deliver_time)->format('d/M/Y h:i a'):date('d/M/Y h:i a',strtotime($order->created_at))." - ASAP",
  343. 'customer' =>$customer_name ,
  344. 'delivery_fee' => $order->delivery_fee,
  345. 'phone' => $new_phone,
  346. 'channel' => !empty($order->campaign_type) && $order->order_type!="dining"?ucwords($order->campaign_type):"Direct",
  347. 'order_placed' =>\Carbon\Carbon::createFromTimeStamp(strtotime($order->created_at))->diffForHumans(),
  348. 'payment' => $order->payment_mode=="COD"?"Cash":"Card",
  349. 'status'=>$status,
  350. 'bg'=>$bg,
  351. 'total_price' => $order->total_price,
  352. 'recipes'=>$recipes,
  353. 'next_action'=>$next_action,
  354. 'next_status' =>$next_status,
  355. 'next_status_label'=> ($label_next_status),
  356. 'address'=>$order->order_type!="paymentlink"?$address:$order->selected_area_formatted,
  357. 'geo_location' => isset($order->customers->customer_addresses[0])?$order->customers->customer_addresses[0]->selected_lat_lng:$order->selected_area_formatted,
  358. 'for_table'=>$order->for_table,
  359. 'formatted_address'=>$order->selected_area_formatted,
  360. 'google_formatted_address'=> $order->selected_area,
  361. 'order_instructions'=>$order->order_instructions,
  362. 'delivery_notes' => $order->order_type=="delivery"?$delivery_notes:$order->order_instructions,
  363. 'bg_color'=> $bg_color,
  364. 'recipient_name' => isset($order->recipients)?$order->recipients->recipient_name:null,
  365. 'recipient_phone' => isset($order->recipients)?$order->recipients->recipient_phone:null,
  366. 'greeting_message' => isset($order->recipients)?$order->recipients->greeting_message:null,
  367. 'discount_with_order'=>isset($order->order_with_discounts)?array(
  368. 'discount_type'=>$order->order_with_discounts->discount_type,
  369. 'discount_code'=>$order->order_with_discounts->discount_code,//TODO to display discount code
  370. 'discount_value'=>$order->order_with_discounts->discount_value,
  371. 'discounted_amount'=>$order->order_with_discounts->discounted_amount,
  372. 'total_order_value'=>$order->order_with_discounts->total_order_value,
  373. 'is_for_whole_order'=>$order->order_with_discounts->is_for_whole_order,
  374. 'is_delivery_discount'=>$order->order_with_discounts->is_delivery_discount,
  375. 'delivery_discount_value'=>$order->order_with_discounts->delivery_discount_value,
  376. 'delivery_discount_type'=>$order->order_with_discounts->delivery_discount_type,
  377. ):null,
  378. 'delivery_update_status_url'=>(env('DELIVERY_BOY_LINK').'?q='.base64_encode($order->id).'&r='.base64_encode($order->order_ref).'&time='.Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->timestamp),
  379. 'link_expire_time'=>Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->format('Y-m-d H:i')
  380. );
  381. echo json_encode($order);
  382. }
  383. public function getOrderDetail($id){
  384. $action = request()->get('action');
  385. $order = Orders::where('id',$id)->with(['order_with_discounts','orderItems','customers','restaurants','outlet'])->first();
  386. $resto = $order->restaurants;
  387. $resto_metas = \App\Helpers\CommonMethods::getRestoMetas($resto);
  388. $imgix = isset($resto_metas['IMGIX_SOURCE']) && !empty($resto_metas['IMGIX_SOURCE'])?$resto_metas['IMGIX_SOURCE']:"https://meemappaws.imgix.net";
  389. $grand_total_price = 0;
  390. $status = "";
  391. $bg="";
  392. $next_action="";
  393. $next_status="";
  394. $bg_color="";
  395. if($order->status=="Placed"){
  396. $status = "New";
  397. $bg="bg-danger";
  398. $box_bg="bg-danger";
  399. $next_action='Accepted';
  400. $next_status="Accepted";
  401. }
  402. if($order->status=="Accepted" || $order->status=="Send_to_Kitchen"){
  403. $status = "In Prep";
  404. $bg="org-bg";
  405. $next_action=$order->order_type=="delivery"?'On Road':'Pick Up';
  406. $next_status="On_Road";
  407. $bg_color="orange";
  408. }
  409. if($order->status=="On_Road"){
  410. $status = "In Route";
  411. $bg = "blu-bg";
  412. $bg_color="#0ED0DF";
  413. $next_action=$order->order_type=="delivery"?'Delivered':'Picked Up';
  414. $next_status="Has_Delivered";
  415. }
  416. $outlet = Outlets::with('delivery_feature')->find($order->outlet_id);
  417. $estimated_time = isset($outlet->delivery_feature)?explode(' - ',$outlet->delivery_feature->estimated_time):[];
  418. $time_from = count($estimated_time) > 0?$estimated_time[0]:0;
  419. $time_to = 0;
  420. $r = [];
  421. if(count($estimated_time) > 1)
  422. $r = explode(":",$estimated_time[1]);
  423. $time_to = count($r) > 1 ?$r[0]:0;
  424. $type = count($r) > 1 ?$r[1]:"DAYS";
  425. $estimated_time = array(
  426. 'preparation_time'=>$time_from,
  427. 'preparation_delivery_time'=>$time_to,
  428. 'time_type'=>$type
  429. );
  430. if(session('app_lang') !==null){
  431. $lang = session('app_lang');
  432. app()->setLocale($lang);
  433. }
  434. $now = Carbon::now();
  435. // dump("Update time: ".$updated_at);
  436. // dd($order);
  437. $label_next_status = trans('label.'.strtolower($next_status));
  438. $info = array(
  439. 'id'=>$order->id,
  440. 'order_ref' => str_pad($order->order_ref,6,0,STR_PAD_LEFT),
  441. 'order_url'=>$order->order_url,
  442. 'order_type'=>ucwords( $order->order_type),
  443. 'status'=>$status,
  444. 'bg'=>$bg,
  445. 'order_instructions'=>$order->order_instructions,
  446. 'delivery_at' => !empty($order->order_deliver_time)?Carbon::parse($order->order_deliver_time)->format('d/M/Y h:i a'):date('d/M/Y h:i a',strtotime($order->created_at))." - ASAP",
  447. 'channel'=>$order->campaign_type,
  448. 'order_placed' =>\Carbon\Carbon::createFromTimeStamp(strtotime($order->created_at))->diffForHumans(),
  449. 'payment' => $order->payment_mode=="COD"?"Cash":"Card",
  450. 'next_action'=>$next_action,
  451. 'next_status' =>$next_status,
  452. 'next_status_label'=> ($label_next_status),
  453. 'delivery_update_status_url'=>(env('DELIVERY_BOY_LINK').'?q='.base64_encode($order->id).'&r='.base64_encode($order->order_ref).'&time='.Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->timestamp),
  454. 'estimated_time'=>$estimated_time,
  455. 'order_placed'=>Carbon::parse($order->created_at)->diffForHumans(),
  456. 'tax_info'=>isset($resto_metas['DISPLAY_TAX_INFO'])?$resto_metas['DISPLAY_TAX_INFO']:"",
  457. 'order_created_at'=>Carbon::parse($order->created_at) ->format('d/M/Y h:i a'),
  458. 'order_updated_at'=>Carbon::parse($order->updated_at)->format('d/M/Y h:i a'),
  459. 'recipient_name' => isset($order->recipients)?$order->recipients->recipient_name:null,
  460. 'recipient_phone' => isset($order->recipients)?$order->recipients->recipient_phone:null,
  461. 'greeting_message' => isset($order->recipients)?$order->greeting_message:null,
  462. );
  463. $resto_outlet = array(
  464. 'business_name' => $resto->name,
  465. 'business_image'=>isset($resto->home_images)?CommonMethods::changeImageLinkAWStoImgix($resto->photos->file_name, $imgix,
  466. 300,
  467. 300,
  468. 100):"",
  469. 'outlet_name' => isset($order->outlet)?$order->outlet->name:"None",
  470. 'business_address'=>$resto->address,
  471. 'whatsapp_number_notification'=>$resto->whatsapp_number_notification
  472. );
  473. $address = $order->selected_area_formatted;
  474. $google_formatted = $order->selected_area;
  475. $geo_location = !empty($order->selected_lat_lng)?$order->selected_lat_lng:$order->selected_area;
  476. $lng_lat = $order->selected_lat_lng;
  477. if(empty($address)){
  478. $address = isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->address:"N/A";
  479. $google_formatted = isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->address:"N/A";
  480. $geo_location = isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->selected_lat_lng:"N/A";;
  481. }
  482. if($lng_lat==""){
  483. //dump($order->customers->main_address);
  484. $lng_lat = isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->selected_lat_lng:"";;
  485. }
  486. $country_code = $resto->countries->country_code;
  487. $new_phone = isset($order->customers->users)?$order->customers->users->email:NULL;
  488. if(isset($order->customers) && isset($order->customers->users)){
  489. $pos = strpos($order->customers->users->email, (string)$country_code);
  490. if ($pos !== false) {
  491. $new_phone = substr_replace($order->customers->users->email, '', $pos, strlen(env('COUNTRY_CODE')));
  492. }
  493. }
  494. $customer = array(
  495. 'customer_id'=>$order->customer_id,
  496. 'customer_name' => empty($order->customer_name) && isset($order->customers)?$order->customers->name:$order->customer_name,
  497. 'customer_phone'=>$new_phone,
  498. 'deliveryAddress'=>$address,
  499. 'google_formatted_address'=>$google_formatted,
  500. 'geo_location' =>$geo_location,
  501. 'geo_points'=>$lng_lat,
  502. 'delivery_instructions'=>isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->instructions:"N/A"
  503. );
  504. $orderItems = isset($order->orderItems) && $order->orderItems->count() > 0?$order->orderItems:null;
  505. $order_items = [];
  506. $order_discounts = $order->order_with_discounts;
  507. $discountInfo = Null;
  508. $is_discount = isset($order_discounts)?true:false;
  509. if($is_discount){
  510. $discount = $order_discounts->discounts;
  511. //dd($order_discounts);
  512. // dump($order_discounts);
  513. $discount = Discounts::find($order_discounts->discount_id);
  514. //dd($discount);
  515. $discountInfo = array(
  516. 'discount_name' => $discount->discount_name,
  517. 'discount_code' => $discount->discount_code,
  518. 'discount_amount' => $discount->amount.($discount->discount_type=="percentage"?"%":""),
  519. 'start_date' => $discount->start_datetime,
  520. 'end_date' => $discount->end_datetime
  521. );
  522. }
  523. $is_delivery_discount = isset($order_discounts) && $order_discounts->is_delivery_discount=="Yes"?true:false;
  524. $is_whole_order_discount = false;
  525. $applied_discount = 0;
  526. $discounted_delivery_price = 0;
  527. $discounted_price = 0;
  528. $applied_delivery_discount = 0;
  529. if($is_discount){
  530. $is_whole_order_discount = $order_discounts->is_for_whole_order=="Yes"?true:false;
  531. if($order_discounts->discount_type=="percentage")
  532. $applied_discount = $order_discounts->discount_value/100;
  533. else
  534. $applied_discount = $order_discounts->discount_value;
  535. if($order_discounts->is_delivery_discount=="Yes"){
  536. if($order_discounts->delivery_discount_type=="percentage")
  537. $applied_delivery_discount = $order_discounts->delivery_discount_value/100;
  538. else
  539. $applied_delivery_discount = $order_discounts->delivery_discount_value;
  540. }
  541. if(isset($order_discounts) && $order_discounts->is_delivery_discount=="Yes" && $order->delivery_fee > 0){
  542. if($order_discounts->delivery_discount_type=="percentage")
  543. $discounted_delivery_price = $order->delivery_fee * $applied_delivery_discount;
  544. else
  545. $discounted_delivery_price = $applied_delivery_discount;
  546. }
  547. }
  548. $order_loyalty = $order->loyalty_with_orders;
  549. $is_loyalty = isset($order_loyalty)?true:false;
  550. $loyalty_amount = 0;
  551. if($is_loyalty){
  552. $loyalty_amount = $order_loyalty->loyalty_amount;
  553. }
  554. if(isset($orderItems)){
  555. foreach($orderItems as $item){
  556. if(isset($item->recipes)){
  557. $sub_items = [];
  558. if(!empty($item->extra_options)){
  559. $extra_options_array = (json_decode($item->extra_options,true));
  560. if(isset($extra_options_array) && count($extra_options_array) > 0){
  561. $extra_sub_options = $extra_options_array;
  562. if(count($extra_sub_options) > 0){
  563. foreach($extra_sub_options as $sub_option){
  564. if(isset($sub_option['id'])){
  565. $sub_opt = ExtraOptionItems::find($sub_option['id']);
  566. $qty = isset($sub_items[$sub_opt['id']])?$sub_items[$sub_opt['id']]['quantity']+1:1;
  567. $grand_total_price = $grand_total_price+($sub_option['price'] * $qty*$item->qty);
  568. $sub_items[$sub_opt['id']] = array(
  569. 'id'=>$sub_option['id'],
  570. 'name'=>strip_tags($sub_opt->name),
  571. 'price'=>$sub_option['price'] > 0?array('normal_price'=>$sub_option['price'],
  572. 'formatted_price'=>number_format($sub_option['price'],2).' '.$resto_metas['BUSSINESS_CCY']):null,
  573. 'quantity'=>$qty
  574. );
  575. }else{
  576. if(isset($extra_sub_options['color_size'])){
  577. $color = '<div style="width: 20px; height: 20px; background-color: '.$extra_sub_options['color_size']['color'].'; border-radius: 20px; float:right;;margin-left: 10px"></div>';
  578. $size = '<div style="float:right;font-size: 14px;margin-left: 8px;font-weight: 700;"> '.$extra_sub_options['color_size']['size'].'</div>';
  579. $name = $color. ' '.$size;
  580. $sub_items[] = array('id'=>'color_size','name'=>$name,'price'=>'N/A','quantity'=>1);
  581. }
  582. ;
  583. }
  584. //dump($sub_opt->name);
  585. }
  586. }
  587. }
  588. }
  589. // $extra_options = json_decode($item->extra_options);
  590. // dd($item->extra_options);
  591. // if(isset($item->extra_options->color_size)){
  592. // $sub_items[] = array('color'=>$extra_options->color_size->color,'size'=>$extra_options->color_size->size);
  593. // }
  594. $order_items[] = array(
  595. 'item_name'=>isset($item->recipes)?$item->recipes->name:"N/A",
  596. 'item_price'=>isset($item->price)?array('normal_price'=>$item->price,'formatted_price'=>number_format($item->price).' '. $resto_metas['BUSSINESS_CCY']):"N/A",
  597. 'item_arabic_name'=>isset($item->recipes)?$item->recipes->arabic_name:"",
  598. 'item_image' => isset($item->recipes->main_images)?CommonMethods::changeImageLinkAWStoImgix($item->recipes->main_images->file_name, $imgix,
  599. env('PRODUCT_THUMBNAIL_IMG_HEIGHT'),
  600. env('PRODUCT_THUMBNAIL_IMG_WIDTH'),
  601. 100):"",
  602. 'quantity'=>$item->qty,
  603. 'sub_items'=>count($sub_items) > 0 ?array_values($sub_items):null,
  604. );
  605. $grand_total_price = $grand_total_price+($item->price * $item->qty);
  606. }
  607. }
  608. }
  609. //dump($grand_total_price);
  610. $sub_total = $grand_total_price;
  611. if($is_whole_order_discount){
  612. if($order_discounts->discount_type=="percentage")
  613. $discounted_price = $grand_total_price * $applied_discount;
  614. else
  615. $discounted_price = $applied_discount;
  616. }
  617. $grand_total_price = $grand_total_price + (!empty($order->delivery_fee)?$order->delivery_fee:0);
  618. if($is_loyalty){
  619. $grand_total_price = $grand_total_price-$loyalty_amount;
  620. }
  621. $order_basket = array(
  622. 'items'=>$order_items,
  623. 'delivery_fee'=>array('normal_price'=>$order->delivery_fee,'formatted_price'=>number_format($order->delivery_fee,2).' '. $resto_metas['BUSSINESS_CCY']),
  624. 'order_total_price'=>array('normal_price'=>$order->total_price,'formatted_price'=>number_format($order->total_price,2).' '. $resto_metas['BUSSINESS_CCY']),
  625. 'is_discount'=>$is_discount,
  626. 'discount_info'=>$discountInfo,
  627. 'is_delivery_discount'=>$is_delivery_discount,
  628. 'is_loyalty'=>$is_loyalty,
  629. 'loyalty_amount'=>$is_loyalty?array('normal_price'=>$loyalty_amount,'formatted_price'=>number_format($loyalty_amount,2).' '.$resto_metas['BUSSINESS_CCY']):null,
  630. 'discounted_amount'=>array('normal_price'=>round($discounted_price,2),'formatted_price'=>number_format($discounted_price,2).' '. $resto_metas['BUSSINESS_CCY']),
  631. 'delivery_discounted_amount'=>array('normal_price'=>round($discounted_delivery_price,2),'formatted_price'=>number_format($discounted_delivery_price,2).' ' . $resto_metas['BUSSINESS_CCY']),
  632. 'discounted_total_price'=>($grand_total_price - $discounted_price-$discounted_delivery_price).' ' . $resto_metas['BUSSINESS_CCY'],
  633. 'discounted_total_price'=>array('normal_price'=>($grand_total_price - $discounted_price-$discounted_delivery_price),'formatted_price'=>number_format(($grand_total_price - $discounted_price-$discounted_delivery_price),2).' '. $resto_metas['BUSSINESS_CCY']),
  634. 'sub_total'=>array('normal_price'=>($sub_total),'formatted_price'=>number_format(($sub_total ),2).' '. $resto_metas['BUSSINESS_CCY']),
  635. 'grand_total'=>array('normal_price'=>$grand_total_price,'formatted_price'=>number_format($grand_total_price,2).' '. $resto_metas['BUSSINESS_CCY']),
  636. );
  637. if($order_basket['sub_total']['normal_price'] <=0){
  638. $order_basket['sub_total'] = $order_basket['order_total_price'];
  639. $order_basket['grand_total'] = $order_basket['order_total_price'];
  640. }
  641. $delivery_boy = array(
  642. 'delivery_update_status_url'=>(env('DELIVERY_BOY_LINK').'?q='.base64_encode($order->id).'&r='.base64_encode($order->order_ref).'&time='.Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->timestamp),
  643. 'link_expire_time'=>Carbon::now()->addMinutes(env('DELIVERY_BOY_LINK_EXPIRY'))->format('Y-m-d H:i')
  644. );
  645. $orderDetails = array(
  646. 'business_info'=>$resto_outlet,
  647. 'order_info'=>$info,
  648. 'customer'=>$customer,
  649. 'basket'=>$order_basket,
  650. 'delivery_boy'=>$delivery_boy
  651. );
  652. if($action && !empty($action) && $action=="print"){
  653. return view('orders.print-order',['order'=>$orderDetails]);
  654. }
  655. return response()->json($orderDetails,200);
  656. }
  657. public function show($id){
  658. $order = Orders::find($id);
  659. $activities = OrderActivities::where('order_id',$id)->pluck('status')->toArray();
  660. // dd($activities);
  661. $data = [
  662. 'order' => $order,
  663. 'activities' =>$activities
  664. ];
  665. return view('orders.show',$data);
  666. }
  667. public function update_status(Request $request){
  668. $id = $request->id;
  669. $status = $request->status;
  670. $order = Orders::find($id);
  671. if($status==$order->status){
  672. // if(1){
  673. return response()->json(array('type'=>'error','message'=>'Order is already updated as '.$status));
  674. }
  675. $this->resto_id = CommonMethods::getRestuarantID();
  676. $resto = Restaurants::find($this->resto_id);
  677. $time_zone = "Asia/Dubai";
  678. if(!empty($resto->time_zone)){
  679. config(['app.timezone' => $resto->time_zone]);
  680. date_default_timezone_set($resto->time_zone);
  681. }
  682. if($status=="Accepted"){
  683. //If credit card payment success then update order count
  684. //$disCountCounter = DiscountWithOrder::where('order_id',$order->id);
  685. //$disCountCounter->is_parked=0;
  686. //$disCountCounter->save();// Giving error fix later
  687. DB::statement("UPDATE tb_dm_order_with_discount SET is_parked=0 where order_id = ".$id);
  688. //Logic to check if the order is availed or not
  689. $order->preparation_time = !empty($request->preparation_time)?$request->preparation_time:"";
  690. $order->delivery_preparation_time = !empty($request->preperation_delivery)?$request->preperation_delivery:"";
  691. $order->preparation_type = !empty($request->preparation_type)?$request->preparation_type:"";
  692. }
  693. $order->status = $status;
  694. $order->save();
  695. $loyltyStatus = "";
  696. $rejectedStatus = ['Rejected','Cancelled_by_Customer','Rejected_by_User','Close'];
  697. if(in_array($status,$rejectedStatus))
  698. $loyltyStatus = 'cancelled';
  699. if($status=="Has_Delivered")
  700. $loyltyStatus = "processed";
  701. // if($status=="Has_Delivered"){
  702. if(!empty($loyltyStatus)){
  703. $loylty = CustomerLoyaltyHistories::where('order_id',$id)->where('status','initial')->first();
  704. // dump($loylty);
  705. if(isset($loylty)){
  706. // $loylty->status = $loyltyStatus;
  707. //$loylty->save();
  708. $loylty = CustomerLoyaltyHistories::where('order_id',$id)->where('status','initial')->update(['status'=>$loyltyStatus]);
  709. }
  710. }
  711. // }
  712. $restuarant = Restaurants::find($order->resto_id);
  713. if(strtolower($status)=="accepted" || strtolower($status)=="rejected" || strtolower($status)=="rejected_by_user"){
  714. if(!empty($restuarant->domain_name))
  715. $url = $restuarant->domain_name.$restuarant->resto_unique_name."/track/order?order=".$id."&ref=".$order->order_ref;
  716. else
  717. $url = env('QRCODE_HOST_ORDER').$restuarant->resto_unique_name."/track/order?order=".$id."&ref=".$order->order_ref;
  718. //dump($url);
  719. if(strtolower($status)=="accepted"){
  720. // $this->sendAcceptOrderNotification($order);
  721. $message =env('ORDER_ACCEPTED_MESSAGE').' '.$url;
  722. $message = "Dear Customer, Your Order #".$order->order_ref." is accepted , kindly track your order at ".$url." ";
  723. if($restuarant->default_lang=="ar"){
  724. $message = "عزيزي العميل طلبك {{1}} تم {{2}} ، لتتبع طلبك افتح الرابط {{3}}";
  725. $message = str_replace(["{{1}}",'{{2}}','{{3}}'],["#".$order->order_ref,'قبوله',$url],$message);
  726. // echo $message;
  727. }
  728. }
  729. else{
  730. $message = env('ORDER_CANCELLED_MESSAGE');
  731. $message = "Dear Customer, Your Order #".$order->order_ref." is rejected , kindly track your order at ".$url." ";
  732. $message = "Dear Customer, Your Order #".$order->order_ref." is cancelled.";
  733. $restuarant = Restaurants::find($order->resto_id);
  734. if($restuarant->default_lang=="ar"){
  735. //$message = "عزيزي العميل طلبك {{1}} تم {{2}} ، لتتبع طلبك افتح الرابط {{3}}";
  736. //$url = "";
  737. // $message = str_replace(["{{1}}",'{{2}}','{{3}}'],["#".$order->order_ref,'رفضه',$url],$message);
  738. $message = "عزيزي العميل، طلبك {{1}} تم {{2}}";
  739. $url = "";
  740. $message = str_replace(["{{1}}",'{{2}}'],["#".$order->order_ref,'الغاؤه ، نعتذر عن عدم القدرة على تلبية طلبك'],$message);
  741. // echo $message;
  742. }
  743. }
  744. $mobile_number = isset($order->customers)?$order->customers->users->email:NULL;
  745. //$mobile_number = "923459635387";
  746. if($restuarant->allow_whatsapp_notifications_to_customera=="Yes") {
  747. if (isset($mobile_number)) {
  748. $sms = new RestoSMSs();
  749. $sms->resto_id = $order->resto_id;
  750. $sms->msg = $message;
  751. $sms->msg_purpose = "TRACK_ORDER_SMS";
  752. $sms->msisdn = $mobile_number;
  753. $sms->status = 1;
  754. $sms->save();
  755. $sms_id = $sms->id;
  756. $usrname = 'meem_food_order';
  757. if (env('OTP_TEST_IN_DEV') == "1") {//ALWAYS ZERO IN PROD
  758. $usrname = 'meem_food_order1';
  759. }
  760. $data = [
  761. 'usrname' => $usrname,
  762. 'pwd' => 'meem@kkew#9',
  763. 'msisdn' => $mobile_number,
  764. 'smstxt' => $message,
  765. 'pricepoint' => 1,
  766. 'jsonstr' => 'Future'
  767. ];
  768. Log::info('TRACK_ORDER_SMS Data: ' . json_encode($data));
  769. if (env('OTP_DONT_SEND_REQ') == "0") {//IN PROD THIS SHOULD BE 0 IN PROD
  770. $wsdlurl = env('WHATSAPP_OPT_API');
  771. // $this->sendWhatsappQueueMessage($wsdlurl,$data,$sms_id);
  772. $params = array(
  773. 'message_data' => $data,
  774. 'sms_id' => $sms_id,
  775. 'url' => $wsdlurl
  776. );
  777. dispatch(new SendOrderNotifications($params));
  778. }
  779. }
  780. }else{
  781. $data = [
  782. 'msisdn' => $mobile_number,
  783. 'smstxt' => $message,
  784. 'pricepoint'=>1,
  785. 'jsonstr'=>'Future'
  786. ];
  787. Log::info('Without Whatsapp TRACK_ORDER_SMS Data: '.json_encode($data));
  788. }
  789. }
  790. OrderActivities::add_order_activity($id,$status);
  791. $data['order_id'] = $id;
  792. $data['order_ref'] = $order->order_ref;
  793. $data['customer_id'] = $order->customer_id;
  794. $data['status'] = $status;
  795. $data['order_resto_id'] = $order->resto_id;
  796. $data['notification_for'] ="update-order-status";
  797. dispatch(new SendOrderPusherNotification($data));
  798. }
  799. public function update_instruction(Request $request){
  800. $id = $request->id;
  801. $txt = $request->text;
  802. $order = Orders::find($id);
  803. $order->order_instructions = $txt;
  804. $order->save();
  805. }
  806. public function ajax_order(){
  807. $offset = request()->get('start');
  808. $limit = request()->get('length');
  809. $draw = request()->get('draw');
  810. // $offset = ($offset-1) * $limit;
  811. $this->resto_id = CommonMethods::getRestuarantID();
  812. // $resto = Restaurants::find($this->resto_id);
  813. $orders = Orders::where('resto_id',$this->resto_id )->orderBy('created_at','DESC')->offset($offset)->limit($limit)->get();
  814. $custom_status['Placed'] = ['Accepted'=>'Accepted','Rejected'=>'Rejected', 'Rejected_by_User'=>'Rejected by User'];
  815. $custom_status['Send_to_Kitchen'] = ['On_Road'=>'On the Way','Rejected'=>'Rejected', 'Rejected_by_User'=>'Rejected by User'];
  816. $custom_status['On_Road'] = ['Has_Delivered'=>'Delivered','Rejected'=>'Rejected', 'Rejected_by_User'=>'Rejected by User'];
  817. $custom_statuses = ['Rejected_by_User'=>'Rejected by User','Accepted'=>'Accepted','Rejected'=>'Rejected','Placed'=>'Placed','Send_to_Kitchen'=>'Send to Kitchen','On_Road'=>'On the Way', 'Has_Delivered'=>'Delivered','Served'=>"Served","Cancelled_by_Customer"=>"Cancelled"];
  818. $custom_status['Accepted'] = ['On_Road'=>'On the Way','Rejected'=>'Rejected', 'Rejected_by_User'=>'Rejected by User'];
  819. $data = NULL;
  820. foreach($orders as $order){
  821. $data[] = [
  822. $order->order_ref,
  823. CommonMethods::formatDateTime($order->created_at),
  824. $order->customer_name,
  825. isset($order->customers)?(str_replace(env('COUNTRY_CODE'),'',$order->customers->users->email)):"",
  826. isset($order->customers) && isset($order->customers->customer_addresses[0])?$order->customers->customer_addresses[0]->address:"",
  827. number_format($order->total_price+$order->delivery_fee),
  828. isset($custom_statuses[$order->status])?$custom_statuses[$order->status]:"",
  829. NULL
  830. ];
  831. }
  832. return response()->json(array('data'=>$data));
  833. }
  834. public function print_order($id){
  835. $order = Orders::find($id);
  836. $data = [
  837. 'order' => $order
  838. ];
  839. return view('orders.print-order',$data);
  840. }
  841. public function order_history(){
  842. //$recent_till = Carbon::now()->subDays(2);;
  843. $this->resto_id = CommonMethods::getRestuarantID();
  844. if(!isset($_GET['order_ref']))
  845. $orders = Orders::where('resto_id',$this->resto_id)->whereIN('status' ,['Rejected_by_User','Rejected','Has_Delivered','Close'])->orderBy('created_at','DESC')->whereNull('deleted_at')->paginate(50);
  846. else{
  847. $params = $_GET;
  848. $orders = Orders::whereNull('deleted_at')->whereIN('status' ,['Rejected_by_User','Rejected','Has_Delivered','Close','Accepted'])->orderBy('created_at','DESC')->where('resto_id',$this->resto_id)->where(function ($q) use($params){
  849. if(isset($params['order_ref']) && !empty($params['order_ref'])){
  850. $q->where('order_ref',$params['order_ref']);
  851. }
  852. if(isset($params['outlet_id']) && !empty($params['outlet_id'])){
  853. $q->where('outlet_id',$params['outlet_id']);
  854. }
  855. if(isset($params['order_status']) && !empty($params['order_status'])){
  856. $q->where('status',$params['order_status']);
  857. }
  858. if(isset($params['date_range']) && !empty($params['date_range'])){
  859. $dates = explode(' - ',$params['date_range']);
  860. $start_date = Carbon::parse($dates[0])->subDay()->format('Y-m-d');
  861. $end_date = Carbon::parse($dates[1])->addDay()->format('Y-m-d');
  862. $q->whereBetween('created_at',[$start_date,$end_date]);
  863. // $q->where('outlet_id',$params['outlet_id']);
  864. }
  865. if(isset($params['phone_number']) && !empty($params['phone_number'])){
  866. $q->whereHas('customers.users',function($r) use($params){
  867. $r->where('email','LIKE','%'.$params['phone_number'].'%');
  868. });
  869. }
  870. })->get();
  871. }
  872. // $orders = Auth::user()->restaurants->orders;
  873. $data = [
  874. 'orders' => $orders
  875. ];
  876. return view('orders.orders-history',$data);
  877. }
  878. function send_message($mobile_number,$message,$sms_id){
  879. dd('ETST');
  880. // $mobile_number = '9647834000012';
  881. $usrname = 'meem_food_order';
  882. //OTP_TEST_IN_DEV0,DISABLE_OTP=0,CONSIDER_OTP_ALWAYS_TRUE=0 in prod to make it live
  883. if(env('OTP_TEST_IN_DEV')=="1"){//ALWAYS ZERO IN PROD
  884. $usrname = 'meem_food_order1';
  885. }
  886. $data = [
  887. 'usrname'=>$usrname,
  888. 'pwd'=>'meem@kkew#9',
  889. 'msisdn' => $mobile_number,
  890. 'smstxt' => $message,
  891. 'pricepoint'=>1,
  892. 'jsonstr'=>'Future'
  893. ];
  894. dd($data);
  895. // $wsdlurl = 'https://taiftec.com/blkdlr/bulk/sendmt';
  896. $wsdlurl="";//Uncomment in prod
  897. Log::info('SMS Data: '.json_encode($data));
  898. if(env('OTP_DONT_SEND_REQ')=="0"){//IN PROD THIS SHOULD BE 0 IN PROD
  899. $wsdlurl = env('SMS_OPT_API');
  900. $response = Http::acceptJson()->post($wsdlurl,$data);
  901. $response = $response->json();
  902. $response = json_encode($response);
  903. Log::info('SMS Response: '.$response);
  904. $sms = RestoSMSs::find($sms_id);
  905. $sms->otp_req = json_encode($data);
  906. $sms->otp_res = $response;
  907. $sms->save();
  908. }
  909. if(env('OTP_CONSIDER_ALWAYS_TRUE')=="1"){//IN PROD THIS SHOULD BE 0 IN PROD
  910. $a = [
  911. "reqid" => "20210611025322000469",
  912. "mtid" => "18",
  913. "errcode" => 1,
  914. "status" => "ACCEPTED"];
  915. $response = json_encode($a);
  916. $access_token = Session::get('access_token');
  917. $sms_url = env('RESTO_API_URL').'update/sms';
  918. Http::post($sms_url,['sms_id'=>$sms_id,'req'=>json_encode($data),'resp'=>$response]);
  919. return $response;
  920. }
  921. // dd($response->json());
  922. }
  923. public function sendWhatsappQueueMessage($url, $data,$sms_id){
  924. $response = Http::acceptJson()->post($url,$data);
  925. $response = $response->json();
  926. $response = json_encode($response);
  927. Log::info('TRACK_ORDER_SMS Response: '.$response);
  928. $sms = RestoSMSs::find($sms_id);
  929. $sms->otp_req = json_encode($data);
  930. $resp = json_decode($response);
  931. $sms->otp_req_status = strtoupper($resp->status)=="ACCEPTED"?"SUCCESS":"FAIL";
  932. $sms->otp_res = $response;
  933. $sms->save();
  934. }
  935. }