Пример #1
0
 /**
  * store a resource 
  * @param  Request 	$request http request
  * @param  mixed  	$id      id of the resource for updating
  * @return jsend           	 jsend with newly stored source
  */
 function store($id = null)
 {
     ////////////////
     // Load Data  //
     ////////////////
     if ($id) {
         $data = Model::find($id);
         if (!$data) {
             return app()->abort(404);
         }
     } else {
         $data = new Model();
     }
     ///////////////////////////////////
     // Assign posted data to Data    //
     ///////////////////////////////////
     $data->fill($this->request->input());
     $data->slug = Model::generateSlug($data->name, $data->_id);
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     // REMOVE THIS AFTERWARD //
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     // $data->created_at = $this->request->input('created_at');
     ///////////////////////////////////////////////////////////////////
     // 							Validate data 						 //
     ///////////////////////////////////////////////////////////////////
     # Validate Travel Agent
     $travel_agent = TravelAgent::find($this->request->input('travel_agent_id'));
     if (!$travel_agent) {
         return response()->json(JSend::fail(['travel_agent_id' => ['Travel Agent is not recognized']])->asArray())->setCallback($this->request->input('callback'));
     }
     $data->travel_agent_id = $travel_agent->id;
     ///////////////////////
     // EMBED IMAGES 	 //
     ///////////////////////
     foreach ($this->request->input('images') as $x) {
         $images[] = new Image($x);
     }
     if (!$data->syncImages($images)) {
         return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback'));
     }
     ///////////////////////
     // EMBED TAGS	 	 //
     ///////////////////////
     foreach ($this->request->input('tags') as $x) {
         if ($x['tag'] && $x['type']) {
             $tags[] = new Tag($x);
         }
     }
     if (!$data->syncTags($tags)) {
         return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback'));
     }
     ////////////////////////
     // EMBED TourSchedule //
     ////////////////////////
     foreach ($this->request->input('schedules') as $x) {
         if ($x['_id']) {
             $tour_schedule = $data->schedules->where('_id', $x['_id'])->first();
             if (!$tour_schedule) {
                 return app()->abort(404);
             }
             $tour_schedule->fill($x);
         } else {
             $tour_schedule = new TourSchedule($x);
         }
         //////////////////////
         // Sync Promo 		//
         //////////////////////
         $promos = [];
         foreach ($x['promos'] as $promo_array) {
             if ($promo_array['_id']) {
                 $promo = $tour_schedule->promos->where('_id', $promo_array['_id']['$id'])->first();
                 if (!$promo) {
                     return app()->abort(404);
                 }
                 $promo->fill($promo_array);
                 $promos[] = $promo;
             } else {
                 $promos[] = new Promo($promo_array);
             }
         }
         if (!$tour_schedule->syncPromo($promos)) {
             return response()->json(JSend::fail($tour_schedule->getErrors())->asArray())->setCallback($this->request->input('callback'));
         }
         //////////////////////
         // Sync Voucher 	//
         //////////////////////
         $vouchers = [];
         foreach ($x['vouchers'] as $voucher_array) {
             if ($voucher_array['_id']) {
                 $voucher = $tour_schedule->vouchers->where('_id', $voucher_array['_id']['$id'])->first();
                 if (!$voucher) {
                     return App()->abort(404);
                 }
                 $voucher->fill($voucher_array);
                 $vouchers[] = $voucher;
             } else {
                 $vouchers[] = new Voucher($voucher_array);
             }
         }
         if (!$tour_schedule->syncVoucher($vouchers)) {
             return response()->json(JSend::fail($tour_schedule->getErrors())->asArray())->setCallback($this->request->input('callback'));
         }
         $tour_schedules[] = $tour_schedule;
     }
     if (!$data->syncTourSchedules($tour_schedules)) {
         return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback'));
     }
     ///////////////////////
     // EMBED PACKAGE  	 //
     ///////////////////////
     if (!$data->syncTourPackage(new TourPackage($this->request->input('package')))) {
         return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback'));
     }
     //////////////////
     // Check Top Up //
     //////////////////
     if ($data->ads) {
         $older_data = $data->getOriginal();
     }
     ///////////
     // Store //
     ///////////
     if ($data->save()) {
         $new_ads = $data->ads;
         if ($data->ads) {
             $ids_inc = [];
             foreach ($data->ads as $key => $value) {
                 //1. Check if new data
                 if (empty($value['ad_id'])) {
                     $new_ad = $data->ads[$key];
                     $new_ad['ad_id'] = uniqid();
                     $new_ads[$key] = $new_ad;
                     $attributes['name'] = 'Top Up';
                     $attributes['ads_id'] = $new_ad['ad_id'];
                     $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d');
                     $attributes['travel_agent']['_id'] = $data->travel_agent['_id'];
                     $attributes['travel_agent']['name'] = $data->travel_agent['name'];
                     $attributes['credit']['started_at'] = $data->ads[$key]['started_at'];
                     $attributes['credit']['initial_credit'] = $data->ads[$key]['initial_credit'] * 1;
                     $attributes['credit']['daily_rate'] = $data->ads[$key]['daily_rate'] * 1;
                     $attributes['credit']['today_balance'] = $data->ads[$key]['today_balance'] * 1;
                     $attributes['credit']['credit_balance'] = $data->ads[$key]['credit_balance'] * 1;
                     $attributes['credit']['tags'] = $data->ads[$key]['tags'];
                     $attributes['tour']['_id'] = $data['_id'];
                     $attributes['tour']['name'] = $data['name'];
                     $attributes['tour']['tags'] = $data['tags']->toArray();
                     $top_up = new TopUpLog();
                     $top_up->fill($attributes);
                     if (!$top_up->save()) {
                         $error_responses = [];
                         foreach ($top_up->getErrors() as $field => $errors) {
                             $error_responses[$field] = implode(', ', $errors);
                         }
                         return response()->json(JSend::fail($top_up->getErrors())->asArray());
                     }
                 } elseif ($older_data['ads'][$key]['started_at'] != $data->ads[$key]->started_at || $older_data['ads'][$key]['initial_credit'] != $data->ads[$key]->initial_credit || $older_data['ads'][$key]['daily_rate'] != $data->ads[$key]->daily_rate || $older_data['ads'][$key]['today_balance'] != $data->ads[$key]->today_balance || $older_data['ads'][$key]['credit_balance'] != $data->ads[$key]->credit_balance) {
                     //emptied last balance
                     $last_balance = TopUpLog::where('ads_id', $older_data['ads'][$key]['ad_id'])->orderBy('created_at', 'desc')->first();
                     if ($last_balance && $last_balance['credit']['today_balance'] > 0) {
                         $attributes['name'] = 'Emptied Top Up';
                         $attributes['ads_id'] = $older_data['ads'][$key]['ad_id'];
                         $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d');
                         $attributes['travel_agent']['_id'] = $data->travel_agent['_id'];
                         $attributes['travel_agent']['name'] = $data->travel_agent['name'];
                         $attributes['credit']['started_at'] = $older_data['ads'][$key]['started_at'];
                         $attributes['credit']['initial_credit'] = $older_data['ads'][$key]['initial_credit'] * 1;
                         $attributes['credit']['daily_rate'] = $older_data['ads'][$key]['daily_rate'] * 1;
                         $attributes['credit']['today_balance'] = 0 * 1;
                         $attributes['credit']['credit_balance'] = (0 - $older_data['ads'][$key]['today_balance']) * 1;
                         $attributes['credit']['tags'] = $older_data['ads'][$key]['tags'];
                         $attributes['tour']['_id'] = $older_data['_id'];
                         $attributes['tour']['name'] = $older_data['name'];
                         $attributes['tour']['tags'] = $older_data['tags'];
                         $top_up = new TopUpLog();
                         $top_up->fill($attributes);
                         if (!$top_up->save()) {
                             $error_responses = [];
                             foreach ($top_up->getErrors() as $field => $errors) {
                                 $error_responses[$field] = implode(', ', $errors);
                             }
                             return response()->json(JSend::fail($top_up->getErrors())->asArray());
                         }
                     }
                     //create new top up
                     $new_ad = $data->ads[$key];
                     $new_ads[$key] = $new_ad;
                     $attributes['name'] = 'Edited Top Up';
                     $attributes['ads_id'] = $new_ad['ad_id'];
                     $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d');
                     $attributes['travel_agent']['_id'] = $data->travel_agent['_id'];
                     $attributes['travel_agent']['name'] = $data->travel_agent['name'];
                     $attributes['credit']['started_at'] = $data->ads[$key]['started_at'] * 1;
                     $attributes['credit']['initial_credit'] = $data->ads[$key]['initial_credit'] * 1;
                     $attributes['credit']['daily_rate'] = $data->ads[$key]['daily_rate'] * 1;
                     $attributes['credit']['today_balance'] = $data->ads[$key]['initial_credit'] * 1;
                     $attributes['credit']['credit_balance'] = $data->ads[$key]['initial_credit'] * 1;
                     $attributes['credit']['tags'] = $data->ads[$key]['tags'];
                     $attributes['tour']['_id'] = $data['_id'];
                     $attributes['tour']['name'] = $data['name'];
                     $attributes['tour']['tags'] = $data['tags']->toArray();
                     $top_up = new TopUpLog();
                     $top_up->fill($attributes);
                     if (!$top_up->save()) {
                         $error_responses = [];
                         foreach ($top_up->getErrors() as $field => $errors) {
                             $error_responses[$field] = implode(', ', $errors);
                         }
                         return response()->json(JSend::fail($top_up->getErrors())->asArray());
                     }
                 }
                 if ($new_ad['ad_id']) {
                     $ids_inc[] = $new_ad['ad_id'];
                 }
             }
             //3. Check if deleted data
             foreach ($older_data['ads'] as $key => $value) {
                 if (!in_array($value['ad_id'], $ids_inc)) {
                     $attributes['name'] = 'Deleted Top Up';
                     $attributes['ads_id'] = $older_data['ads'][$key]['ad_id'];
                     $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d');
                     $attributes['travel_agent']['_id'] = $older_data['travel_agent']['_id'];
                     $attributes['travel_agent']['name'] = $older_data['travel_agent']['name'];
                     $attributes['credit']['started_at'] = $older_data['ads'][$key]['started_at'] * 1;
                     $attributes['credit']['initial_credit'] = $older_data['ads'][$key]['initial_credit'] * 1;
                     $attributes['credit']['daily_rate'] = $older_data['ads'][$key]['daily_rate'] * 1;
                     $attributes['credit']['today_balance'] = 0 * 1;
                     $attributes['credit']['credit_balance'] = (0 - $older_data['ads'][$key]['today_balance']) * 1;
                     $attributes['credit']['tags'] = $older_data['ads'][$key]['tags'];
                     $attributes['tour']['_id'] = $older_data['_id'];
                     $attributes['tour']['name'] = $older_data['name'];
                     $attributes['tour']['tags'] = $older_data['tags'];
                     $top_up = new TopUpLog();
                     $top_up->fill($attributes);
                     if (!$top_up->save()) {
                         $error_responses = [];
                         foreach ($top_up->getErrors() as $field => $errors) {
                             $error_responses[$field] = implode(', ', $errors);
                         }
                         return response()->json(JSend::fail($top_up->getErrors())->asArray());
                     }
                 }
             }
             $data->ads = $new_ads;
             if (!$data->save()) {
                 return response()->json(JSend::fail($data->getErrors())->asArray());
             }
         }
         return response()->json(JSend::success(['data' => $data])->asArray());
     } else {
         $error_responses = [];
         foreach ($data->getErrors() as $field => $errors) {
             $error_responses[$field] = implode(', ', $errors);
         }
         return response()->json(JSend::fail($data->getErrors())->asArray());
     }
 }