ok
Direktori : /home2/selectio/public_html/myclassicfit.com/app/Http/Controllers/api/v1/ |
Current File : /home2/selectio/public_html/myclassicfit.com/app/Http/Controllers/api/v1/CustomerController.php |
<?php namespace App\Http\Controllers\api\v1; use App\CPU\CustomerManager; use App\CPU\Helpers; use App\CPU\ImageManager; use App\Http\Controllers\Controller; use App\Model\DeliveryCountryCode; use App\Model\DeliveryZipCode; use App\Model\Order; use App\Model\OrderDetail; use App\Model\ShippingAddress; use App\Model\SupportTicket; use App\Model\SupportTicketConv; use App\Model\Wishlist; use App\Traits\CommonTrait; use App\User; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use Intervention\Image\Facades\Image; use function App\CPU\translate; class CustomerController extends Controller { use CommonTrait; public function info(Request $request) { return response()->json($request->user(), 200); } public function create_support_ticket(Request $request) { $validator = Validator::make($request->all(), [ 'subject' => 'required', 'type' => 'required', 'description' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $request['customer_id'] = $request->user()->id; $request['priority'] = 'low'; $request['status'] = 'pending'; try { CustomerManager::create_support_ticket($request); } catch (\Exception $e) { return response()->json([ 'errors' => [ 'code' => 'failed', 'message' => 'Something went wrong', ], ], 422); } return response()->json(['message' => 'Support ticket created successfully.'], 200); } public function account_delete(Request $request, $id) { if($request->user()->id == $id) { $user = User::find($id); ImageManager::delete('/profile/' . $user['image']); $user->delete(); return response()->json(['message' => translate('Your_account_deleted_successfully!!')],200); }else{ return response()->json(['message' =>'access_denied!!'],403); } } public function reply_support_ticket(Request $request, $ticket_id) { $support = new SupportTicketConv(); $support->support_ticket_id = $ticket_id; $support->admin_id = 1; $support->customer_message = $request['message']; $support->save(); return response()->json(['message' => 'Support ticket reply sent.'], 200); } public function get_support_tickets(Request $request) { return response()->json(SupportTicket::where('customer_id', $request->user()->id)->get(), 200); } public function get_support_ticket_conv($ticket_id) { return response()->json(SupportTicketConv::where('support_ticket_id', $ticket_id)->get(), 200); } public function add_to_wishlist(Request $request) { $validator = Validator::make($request->all(), [ 'product_id' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $wishlist = Wishlist::where('customer_id', $request->user()->id)->where('product_id', $request->product_id)->first(); if (empty($wishlist)) { $wishlist = new Wishlist; $wishlist->customer_id = $request->user()->id; $wishlist->product_id = $request->product_id; $wishlist->save(); return response()->json(['message' => translate('successfully added!')], 200); } return response()->json(['message' => translate('Already in your wishlist')], 409); } public function remove_from_wishlist(Request $request) { $validator = Validator::make($request->all(), [ 'product_id' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $wishlist = Wishlist::where('customer_id', $request->user()->id)->where('product_id', $request->product_id)->first(); if (!empty($wishlist)) { Wishlist::where(['customer_id' => $request->user()->id, 'product_id' => $request->product_id])->delete(); return response()->json(['message' => translate('successfully removed!')], 200); } return response()->json(['message' => translate('No such data found!')], 404); } public function wish_list(Request $request) { $wishlist = Wishlist::whereHas('wishlistProduct',function($q){ return $q; })->with(['product'])->where('customer_id', $request->user()->id)->get(); return response()->json($wishlist, 200); } public function address_list(Request $request) { return response()->json(ShippingAddress::where('customer_id', $request->user()->id)->latest()->get(), 200); } public function add_new_address(Request $request) { $validator = Validator::make($request->all(), [ 'contact_person_name' => 'required', 'address_type' => 'required', 'address' => 'required', 'city' => 'required', 'zip' => 'required', 'country' => 'required', 'phone' => 'required', 'latitude' => 'required', 'longitude' => 'required', 'is_billing' => 'required' ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $zip_restrict_status = Helpers::get_business_settings('delivery_zip_code_area_restriction'); $country_restrict_status = Helpers::get_business_settings('delivery_country_restriction'); if ($country_restrict_status && !self::delivery_country_exist_check($request->input('country'))) { return response()->json(['message' => translate('Delivery_unavailable_for_this_country')], 403); } elseif ($zip_restrict_status && !self::delivery_zipcode_exist_check($request->input('zip'))) { return response()->json(['message' => translate('Delivery_unavailable_for_this_zip_code_area')], 403); } $address = [ 'customer_id' => $request->user()->id, 'contact_person_name' => $request->contact_person_name, 'address_type' => $request->address_type, 'address' => $request->address, 'city' => $request->city, 'zip' => $request->zip, 'country' => $request->country, 'phone' => $request->phone, 'latitude' => $request->latitude, 'longitude' => $request->longitude, 'is_billing' => $request->is_billing, 'created_at' => now(), 'updated_at' => now(), ]; ShippingAddress::insert($address); return response()->json(['message' => translate('successfully added!')], 200); } public function update_address(Request $request) { $shipping_address = ShippingAddress::where(['customer_id' => $request->user()->id, 'id' => $request->id])->first(); if (!$shipping_address) { return response()->json(['message' => translate('not_found')], 200); } $zip_restrict_status = Helpers::get_business_settings('delivery_zip_code_area_restriction'); $country_restrict_status = Helpers::get_business_settings('delivery_country_restriction'); if ($country_restrict_status && !self::delivery_country_exist_check($request->input('country'))) { return response()->json(['message' => translate('Delivery_unavailable_for_this_country')], 403); } elseif ($zip_restrict_status && !self::delivery_zipcode_exist_check($request->input('zip'))) { return response()->json(['message' => translate('Delivery_unavailable_for_this_zip_code_area')], 403); } $shipping_address->update([ 'customer_id' => $request->user()->id, 'contact_person_name' => $request->contact_person_name, 'address_type' => $request->address_type, 'address' => $request->address, 'city' => $request->city, 'zip' => $request->zip, 'country' => $request->country, 'phone' => $request->phone, 'latitude' => $request->latitude, 'longitude' => $request->longitude, 'is_billing' => $request->is_billing, 'created_at' => now(), 'updated_at' => now(), ]); return response()->json(['message' => translate('update_successful')], 200); } public function delete_address(Request $request) { $validator = Validator::make($request->all(), [ 'address_id' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } if (DB::table('shipping_addresses')->where(['id' => $request['address_id'], 'customer_id' => $request->user()->id])->first()) { DB::table('shipping_addresses')->where(['id' => $request['address_id'], 'customer_id' => $request->user()->id])->delete(); return response()->json(['message' => 'successfully removed!'], 200); } return response()->json(['message' => translate('No such data found!')], 404); } public function get_order_list(Request $request) { $orders = Order::with('delivery_man')->where(['customer_id' => $request->user()->id])->get(); $orders->map(function ($data) { $data['shipping_address_data'] = json_decode($data['shipping_address_data']); $data['billing_address_data'] = json_decode($data['billing_address_data']); return $data; }); return response()->json($orders, 200); } public function get_order_details(Request $request) { $validator = Validator::make($request->all(), [ 'order_id' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $details = OrderDetail::with('seller.shop')->where(['order_id' => $request['order_id']])->get(); $details->map(function ($query) { $query['variation'] = json_decode($query['variation'], true); $query['product_details'] = Helpers::product_data_formatting(json_decode($query['product_details'], true)); return $query; }); return response()->json($details, 200); } public function get_order_by_id(Request $request) { $validator = Validator::make($request->all(), [ 'order_id' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } $order = Order::where(['id' => $request['order_id']])->first(); $order['shipping_address_data'] = json_decode($order['shipping_address_data']); $order['billing_address_data'] = json_decode($order['billing_address_data']); return response()->json($order, 200); } public function update_profile(Request $request) { $validator = Validator::make($request->all(), [ 'f_name' => 'required', 'l_name' => 'required', 'phone' => 'required', ], [ 'f_name.required' => translate('First name is required!'), 'l_name.required' => translate('Last name is required!'), ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } if ($request->has('image')) { $imageName = ImageManager::update('profile/', $request->user()->image, 'png', $request->file('image')); } else { $imageName = $request->user()->image; } if ($request['password'] != null && strlen($request['password']) > 5) { $pass = bcrypt($request['password']); } else { $pass = $request->user()->password; } $userDetails = [ 'f_name' => $request->f_name, 'l_name' => $request->l_name, 'phone' => $request->phone, 'image' => $imageName, 'password' => $pass, 'updated_at' => now(), ]; User::where(['id' => $request->user()->id])->update($userDetails); return response()->json(['message' => translate('successfully updated!')], 200); } public function update_cm_firebase_token(Request $request) { $validator = Validator::make($request->all(), [ 'cm_firebase_token' => 'required', ]); if ($validator->fails()) { return response()->json(['errors' => Helpers::error_processor($validator)], 403); } DB::table('users')->where('id', $request->user()->id)->update([ 'cm_firebase_token' => $request['cm_firebase_token'], ]); return response()->json(['message' => translate('successfully updated!')], 200); } public function get_restricted_country_list(Request $request) { $stored_countries = DeliveryCountryCode::orderBy('country_code', 'ASC')->pluck('country_code')->toArray(); $country_list = COUNTRIES; $countries = array(); foreach ($country_list as $country) { if (in_array($country['code'], $stored_countries)) { $countries []= $country['name']; } } if($request->search){ $countries = array_values(preg_grep('~' . $request->search . '~i', $countries)); } return response()->json($countries, 200); } public function get_restricted_zip_list(Request $request) { $zipcodes = DeliveryZipCode::orderBy('zipcode', 'ASC') ->when($request->search, function ($query) use($request){ $query->where('zipcode', 'like', "%{$request->search}%"); }) ->get(); return response()->json($zipcodes, 200); } }