/
home
/
henzagold
/
server
/
app
/
Http
/
Controllers
/
Api
/
File Upload :
llllll
Current File: /home/henzagold/server/app/Http/Controllers/Api/ApiMobileController.php
<?php namespace App\Http\Controllers\Api; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Ciny; use App\Models\Major\Cash; use App\Models\Category; use App\Models\ConfigStatic; use App\Models\Customer; use App\Models\Major\Favorite; use App\Models\Major\Promotion; use App\Models\Major\PromotionIgnore; use App\Models\Major\PromotionItem; use App\Models\Major\PromotionUsed; use App\Models\Notify; use App\Models\Order; use App\Models\OrderActivity; use App\Models\OrderItem; use App\Models\Product; use App\Models\ProductBrand; use App\Models\ProductVariant; use App\Models\SingleCustomerOrders; use App\Models\Sms; use App\Models\Utility; use App\Models\WebServiceGold; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Morilog\Jalali\Jalalian; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; class ApiMobileController extends Controller { public function userAuth(Request $request) { $this->validate($request, ['username' => 'required']); $customer = Customer::active()->where('mobile', Utility::faTOen($request->username))->firstOrFail(); $password = rand(1000, 9999); $customer->send_password = $password; $customer->save(); Sms::Send($customer->mobile,"<#> code is ".$password."\n".'رمز عبور یکبار مصرف شما جهت ورود به اپلیکشن هنزا گلد '.$password.' می باشد'."\n".'aMo6iFyUQaY'); //Sms::Send($customer->mobile, 'رمز عبور یکبار مصرف شما جهت ورود به اپلیکشن هنزا گلد ' . $password . ' می باشد'); return new JsonResponse(true); } public function userVerfiy(Request $request) { $this->validate($request, ['username' => 'required', 'password' => 'required']); $customer = Customer::with(['reagent'])->where([['mobile', Utility::faTOen($request->username)]])->firstOrFail(); $customer->token_mobile = $request->token_mobile; if (Utility::faTOen($request->password) != $customer->send_password) { abort(422); } //$customer->increment('count_login'); if ($customer->api_token == null) { $customer->api_token = \Illuminate\Support\Str::random(60); } $customer->save(); $customer->to = config('havaleh'); $customer->order_title = Order::$STS_TAB; //Filter value $customer->filterColor = ProductVariant::$COLOR_STR; //$customer->filterBrand=ConfigStatic::where('key','brand')->get(); $customer->filterBrand = ProductBrand::select(['*', 'title as val'])->get(); $customer->filterGram = []; $customer->favorits = Favorite::where('customer_id', $customer->id)->select('product_id')->get()->pluck('product_id'); $customer = $customer->toArray(); $customer['filterGram'][] = strval(ProductVariant::min('grams')); $customer['filterGram'][] = strval(ProductVariant::max('grams')); $customer['filterPercentPay'] = []; $customer['filterPercentPay'][] = strval(ProductVariant::min('percentage_pay') + $customer['percent']); $customer['filterPercentPay'][] = strval(ProductVariant::max('percentage_pay') + $customer['percent']); return new JsonResponse($customer); } //old method for login public function login(Request $request) { $this->validate($request, ['username' => 'required', 'password' => 'required']); $customer = Customer::with(['reagent'])->where('mobile', $request->username)->firstOrFail(); if (!Hash::check($request->password, $customer->password)) { abort(422); } if ($customer->api_token == null) { $customer->api_token = \Illuminate\Support\Str::random(60); } $customer->save(); $customer->to = config('havaleh'); $customer->order_title = Order::$STS_TAB; //Filter value $customer->filterColor = ProductVariant::$COLOR_STR; //$customer->filterBrand=ConfigStatic::where('key','brand')->get(); $customer->filterBrand = ProductBrand::select(['*', 'title as val'])->get(); $customer->filterGram = []; $customer->favorits = Favorite::where('customer_id', $customer->id)->select('product_id')->get()->pluck('product_id'); $customer = $customer->toArray(); $customer['filterGram'][] = strval(ProductVariant::min('grams')); $customer['filterGram'][] = strval(ProductVariant::max('grams')); $customer['filterPercentPay'] = []; $customer['filterPercentPay'][] = strval(ProductVariant::min('percentage_pay') + $customer['percent']); $customer['filterPercentPay'][] = strval(ProductVariant::max('percentage_pay') + $customer['percent']); return new JsonResponse($customer); } public function me(Request $request) { //$d = auth()->user()->toArray(); $d = Customer::with(['reagent','image','workshop'])->where('id', auth()->id()) ->select(['*',Customer::$SELECT_BRJ]) ->firstOrFail()->toArray(); //Log::info($d); $d['to'] = config('havaleh'); $d['order_title'] = Order::$STS_TAB; //Filter value $d['filterColor'] = ProductVariant::$COLOR_STR; //$d['filterBrand']=ConfigStatic::where('key','brand')->get(); $d['filterBrand'] = ProductBrand::select(['*', 'title as val'])->orderBy('priority','desc')->get(); $d['filterGram'] = []; $d['filterGram'][] = strval(ProductVariant::min('grams')); $d['filterGram'][] = strval(ProductVariant::max('grams')); $d['filterPercentPay'] = []; $d['filterPercentPay'][] = strval(ProductVariant::min('percentage_pay') + auth()->user()->percent); $d['filterPercentPay'][] = strval(ProductVariant::max('percentage_pay') + auth()->user()->percent); $d['favorits'] = Favorite::where('customer_id', auth()->user()->id)->select('product_id')->get()->pluck('product_id'); return new JsonResponse($d); } public function setFindoc(Request $request) { Log::info('submit finical document'); Log::info($request->all()); $input = $request->all(); $input['customer_id'] = auth()->id(); $input['value'] = ($input['value'] ?? $input['w'] ?? 0);// * -1; $input['title'] = 'ثبت سند' . Arr::get(Cash::$TYPES, $input['type'] ?? 0); $input['confirmed'] = 0; if ($input['type'] == 1) { $input['original_value'] = $input['value']; $input['value'] = ($input['original_value'] * $input['ayar']) / 750; } /*if( ($input['type']==9) ) { $input['confirmed']=1; // Customer::findOrFail(auth()->id())->decrement('remain',) } else{ Sms::Send('09135517425', 'هنزا گلد'."\n".'سندی توسط آقای / خانم '.auth()->user()->fname.' '.auth()->user()->lname.' در سیستم ثبت شد.لطفا نسبت به بررسی و تایید آن اقدام نمایید. '); }*/ Cash::create($input); Sms::Send('09195540836', 'هنزا گلد' . "\n" . 'سندی توسط آقای / خانم ' . auth()->user()->fname . ' ' . auth()->user()->lname . ' در سیستم ثبت شد.لطفا نسبت به بررسی و تایید آن اقدام نمایید. '); Sms::Send('09128081392', 'هنزا گلد' . "\n" . 'سندی توسط آقای / خانم ' . auth()->user()->fname . ' ' . auth()->user()->lname . ' در سیستم ثبت شد.لطفا نسبت به بررسی و تایید آن اقدام نمایید. '); //Sms::Send('09382507041', 'هنزا گلد'."\n".'سندی توسط آقای / خانم '.auth()->user()->fname.' '.auth()->user()->lname.' در سیستم ثبت شد.لطفا نسبت به بررسی و تایید آن اقدام نمایید. '); return new JsonResponse(true); } public function updateProfile(Request $request) { Log::info('update profile'); Log::info($request->all()); $customer = Customer::with(['reagent'])->where('id', auth()->id())->firstOrFail(); $customer->update($request->all()); $customer->save(); $customer->to = config('havaleh'); return new JsonResponse($customer); } public function getProduct($id){ $product = Product::with(['thumb', 'variants', 'tmobile', 'brand', 'category' , 'images'])->findOrFail($id)->toArray(); $r = [['image'=>$product['tmobile']]]; foreach ($product['images'] as $item) { $r[] = $item; } $product['images'] = $r; return response()->json($product); } public function myChild(Request $request) { $text_search = $request->text_search ?? '%'; // $records = Customer::active()->with('image')->where('reagent_id', auth()->id()) $records = Customer::with('image')->where('reagent_id', auth()->id()) ->where(function ($q) use ($text_search) { return $q->where('name', 'like', '%' . $text_search . '%') ->orWhere('mobile', 'like', '%' . $text_search . '%') ->OrWhere('phone', 'like', '%' . $text_search . '%') ->OrWhere('store_name', 'like', '%' . $text_search . '%') ->OrWhere('pasazh_name', 'like', '%' . $text_search . '%'); })->selectify()->get(); foreach ($records as $record) { if (is_null($record->api_token)) { $record->api_token = \Illuminate\Support\Str::random(60); $record->save(); } //Filter value $record->filterColor = ProductVariant::$COLOR_STR; //$record->filterBrand=ConfigStatic::where('key','brand')->get(); $record->filterBrand = ProductBrand::select(['*', 'title as val'])->get(); $record->filterGram = array(strval(ProductVariant::min('grams')), strval(ProductVariant::max('grams'))); $record->filterPercentPay = array(strval(ProductVariant::min('percentage_pay') + $record->percent), strval(ProductVariant::max('percentage_pay') + $record->percent)); $record->favorits = Favorite::where('customer_id', $record->id)->select('product_id')->get()->pluck('product_id'); } return new JsonResponse($records); } public function myOrder(Request $request) { $orders = Order::with(['items' => function ($q) { return $q->with(['variants']); }, 'parent', 'activity']) ->select(['*', Order::$SELECT_UAJ, Order::$SELECT_CAJ, Order::$SELECT_STS_STR, Order::$SELECT_COLOR, Order::$SELECT_IMG]) ->orderBy('id', 'DESC') ->me(); $Customer = Customer::with(['workshop'])->where('id', auth()->id())->first(); if ($request->has('sts')) { /*switch ($request->sts) { case 1: $orders->where('sts',0); break; case 2: $orders->where('sts', '<', 0); break; }*/ if ($request->sts > -1) { $orders->where('sts', $request->sts); } } return new JsonResponse(['page' => 0, 'rows' => $orders->get()]); } public function getNotify(Request $request) { $count = Notify::me()->count(); $records = Notify::me()->orderBy('id', 'DESC')->limit(20)->offset(($request->page ?? 0) * 20) ->select(['*', Cash::$SELECT_CAJ, Cash::$SELECT_UAJ, Cash::$SELECT_TYPE_STR])->get(); return new JsonResponse(['page' => $request->page ?? 0, 'rows' => $records,'count'=>$count]); } public function getCountNotify() { $count = Notify::me()->where('read',false)->count(); return response()->json($count); } public function myFindoc(Request $request) { $records = Cash::me()->limit(20)->orderBy('id', 'DESC')->offset(($request->page ?? 0) * 20) ->select(['*', Cash::$SELECT_CAJ, Cash::$SELECT_UAJ, Cash::$SELECT_TYPE_STR, Cash::$SELECT_STS_STR])->get(); return new JsonResponse(['page' => $request->page ?? 0, 'rows' => $records]); } public function front(Request $request) { $goldPrice = WebServiceGold::getPriceGold18Ayar(); $count_login = 0; if (auth('api-customer')->check()){ $customer = Customer::findOrFail(auth('api-customer')->user()->id); $customer->increment('count_login'); $count_login = $customer->count_login; } $cat = Category::with(['image' , 'icon'])->where('type',2)->where('is_active',true)->get(); if ($request->has('cat') && $request->cat != 0) { $product = Product::mobile()->orderBy('id', 'DESC')->with(['thumb', 'variants', 'tmobile', 'brand', 'category']); if ($request->has('cat') && $request->cat != 0) { $product->whereHas('collect', function ($q) use ($request) { return $q->where('collection_id_id', $request->cat); }); } return new JsonResponse(['products' => $product->limit(10)->get(), 'more' => $product->limit(10)->offset(10)->get(), 'cats' => $cat, 'gold_price'=>$goldPrice]); } else { //new method $temp = []; $temp['products'] = []; $temp["more"] = []; $temp["cats"] = $cat; foreach ($temp["cats"] as $item) { $records = Product::mobile()->with(['category', 'thumb', 'variants', 'brand', 'tmobile']) ->whereHas('collect.collection', function ($q) use ($item) { return $q->where('id', $item->id); }); if ($count_login > 1) { $records = $records->inRandomOrder()->limit(1); } else { $records = $records->orderBy('created_at', 'desc')->limit(1); } $temp['products'] = array_merge($temp["products"], $records->get()->toArray()); } shuffle($temp['products']); /*collect.collection*/ $temp["more"] = array_merge($temp["more"], Product::mobile()->with(['category', 'thumb', 'variants', 'brand', 'tmobile']) ->orderBy('created_at', 'desc') ->limit(10) ->get()->toArray() ); $config = ConfigStatic::where('key','__list__tags') ->where('val','لوکس') ->first(); // $temp['luxe'] = Product::mobile()->whereHas('tags',function ($q)use($config){ // return $q->where('const_id',$config->id); // })->orderBy('id', 'DESC')->limit(10)->with(['thumb', 'variants', 'tmobile', 'brand', 'category'])->get(); $temp['luxe'] = Product::mobile()->whereHas('variants',function ($q){ return $q->where('grams','>',15); })->orderBy('id', 'DESC')->limit(10)->with(['thumb', 'variants', 'tmobile', 'brand', 'category'])->get(); $temp['gold_price'] = $goldPrice; $temp['ciny'] = Ciny::where('is_active',1)->get(); return new JsonResponse($temp); } } public function getCinyByCollectionId($collection_id){ $cinies = Ciny::where('is_active',1)->where('collection_id',$collection_id)->get(); return response()->json($cinies); } public function getProducts(Request $request) { if (is_string($request->colors)) { $colors = json_decode($request->colors ?? '[]'); } else { $colors = $request->input('colors', []); } if (is_string($request->brands)) { $brands = json_decode($request->brands ?? '[]'); } else { $brands = $request->input('brands', []); } if (is_string($request->limit_gram)) { $limit_gram = json_decode($request->limit_gram ?? '[]'); } else { $limit_gram = $request->input('limit_gram', []); } if (is_string($request->limit_percent)) { $limit_percent = json_decode($request->limit_percent ?? '[]'); } else { $limit_percent = $request->input('limit_percent', []); } if (is_string($request->cats)) { $cats = json_decode($request->cats ?? '[]'); } else { $cats = $request->input('cats', []); } $product = Product::mobile()->orderBy('id', 'DESC')->with(['thumb', 'variants', 'tmobile', 'brand', 'category']); if (sizeof($cats) > 0) { $product->whereHas('collect', function ($q) use ($cats) { return $q->whereIn('collection_id_id', $cats); }); } if ($request->has('text') && !empty(trim($request->text))) { $text = $this->convert($request->text); //$product->whereRaw('title' , 'like' , '%'.$request->text.'%'); //ilike $product->whereRaw('lower(`title`) like ?', ['%' . $text . '%']); //whereRaw("UPPER('{$column}') LIKE '%'". strtoupper($value)."'%'"); } if (sizeof($colors) > 0) { $product->whereHas('variants', function ($q) use ($colors) { return $q->whereIn('color', $colors); }); } if (sizeof($limit_gram) > 0) { $product->whereHas('variants', function ($q) use ($limit_gram) { return $q->whereBetween('grams', $limit_gram); }); } if (sizeof($limit_percent) > 0 and auth('api-customer')->check()) { $limit_percent[0] = $limit_percent[0] - auth('api-customer')->user()->percent; $limit_percent[1] = $limit_percent[1] - auth('api-customer')->user()->percent; $product->whereHas('variants', function ($q) use ($limit_percent) { return $q->whereBetween('percentage_pay', $limit_percent); }); } if (sizeof($brands) > 0) { $product->whereIn('brand_id', $brands); } if ($request->has('ciny_id') && count($request->ciny_id) > 0) { $ciny_ids = $request->ciny_id; $product->whereHas('ciny', function ($q)use($ciny_ids){ $a = $q->whereIn('ciny_id',$ciny_ids); return $a; }); } if ($request->has_discount) { $promotion_product = PromotionItem::with(['promotion']) ->whereHas('promotion', function ($q) { $q->active()->where('type', 1); })->pluck('item_id'); $promotion_category = PromotionItem::with(['promotion']) ->whereHas('promotion', function ($q) { $q->active()->where('type', 2); })->pluck('item_id'); $product->whereIn('id', $promotion_product) ->orWhereHas('collect', function ($q) use ($promotion_category) { return $q->whereIn('collection_id_id', $promotion_category); }); } $export = []; $export['page'] = $request->page ?? 0; $export['limit'] = $request->limit ?? 10; $export['count'] = $product->count(); if ($export['count'] < $export['limit'] * ($export['page'] - 1)) { $export['page'] = 0; } $export['rows'] = $product->limit($export['limit'])->offset($export['limit'] * ($export['page']))->get(); return response()->json($export); } public function setProduct(Request $request) { return new JsonResponse(['id' => 10]); } public function toggleFavorite(Request $request) { $this->validate($request, [ 'id' => 'required' ]); $favorite = Favorite::where([ ['product_id', $request->id], ['customer_id', auth()->id()] ])->delete(); if ($favorite == null) { $favorite = new Favorite(); $favorite->product_id = $request->id; $favorite->customer_id = auth()->id(); $favorite->save(); } return response()->json(true); } public function getFavorites(Request $request) { $favorites = Favorite::orderBy('updated_at', 'DESC')->where('customer_id', auth()->user()->id)->select('product_id')->get()->pluck('product_id'); $products = Product::with(['thumb', 'variants', 'tmobile', 'brand', 'category']) ->whereIn('id', $favorites)->where('is_active',true); $export = []; $export['count'] = $products->count(); $export['page'] = $request->page ?? 1; if ($export['count'] < 12 * ($export['page'] - 1)) { $export['page'] = 1; } $export['rows'] = $products->limit(12)->offset(12 * ($export['page'] - 1))->get(); return response()->json($export); } public function updateTokenMobile(Request $request) { $customer = Customer::findOrFail(auth()->user()->id); // $customer->increment('count_login'); $this->validate($request, [ 'token_mobile' => 'required' ]); $customer->update($request->all()); return response()->json(true); } public function getConfig(Request $request) { $config = []; //Mobile Main Slider $config['main_slider'] = json_decode(ConfigStatic::where('key', 'mobile_main_slider')->pluck('val')->first(), true); foreach ($config['main_slider'] as &$item) { $item['src'] = Utility::baseURLStorage() . '/' . $item['src']; } //about text $config['about'] = ConfigStatic::where('key', 'about')->pluck('val')->first(); return response()->json($config); } public function getPromotion(Request $request) { $promotion = Promotion::active()->get()->toArray(); // $promotion['ignore'] = PromotionIgnore::select(['id','type','item_id'])->get(); //start -- remove after fix bug from mobile // $promotion = array_filter($promotion, function ($record) { // return count($record['items']) > 0; // }); //end -- remove after fix bug from mobile return response()->json($promotion); } public function getIgnore(Request $request) { $ignore = PromotionIgnore::select(['id','type','item_id'])->get(); return response()->json($ignore); } public function setOrder(Request $request) { Log::info('set order'); Log::info('customer id '); Log::info(auth()->id()); Log::info($request->all()); $customer = Customer::findOrFail(auth()->id()); $gramLevel = $customer->gram_level; //calculate all gram with discount $allGram = array_sum(array_map(function ($item) { $percent = ($item['percent_pay'] ?? 0) - ($item['discount'] ?? 0); $gram = ($item['rash_weight'] ?? 0) + (($item['rash_weight'] ?? 0) * ($percent / 100)); return $gram * ($item['qty'] ?? 0); }, $request->items)); //calculate all gram without percent on gram $wage = array_sum(array_map(function ($item) { return ($item['rash_weight'] ?? 0) * $item['qty'] ?? 0; }, $request->items)); /* $percent = array_sum(array_map(function ($item) { return ($item['wage_weight'] ?? 0) * $item['qty'] ?? 0; }, $request->items));*/ //calculate all rial_pay $rial_input = array_sum(array_map(function ($item) { return ($item['rial_pay'] ?? 0) * $item['qty'] ?? 0; }, $request->items)); //calculate all discount(promotion) $discount = array_sum(array_map(function ($item) { return ($item['discount'] ?? 0); }, $request->items)); if ($request->id) { $order = Order::findOrFail($request->id); } $remain = $customer->remain; $inValidOrder = ($allGram + $remain) > $gramLevel; //update order if ($request->id) { //update order $order->update([ 'input' => $allGram, 'w' => $allGram, 'wage' => $allGram - $wage, 'percent' => 0, 'price' => 0, 'sts' => ($inValidOrder) ? 0 : 1, 'rial_input' => $rial_input, 'discount' => $discount, ]); OrderItem::where('order_id', $order->id)->forceDelete(); } //add(create) order else { $order = Order::create([ 'customer_id' => auth()->id(), 'reagent_id' => auth()->user()->reagent_id, 'type' => ($request->from_parent) ? 1 : 2, 'input' => $allGram, 'w' => $allGram, 'wage' => $allGram - $wage, 'percent' => 0, 'price' => 0, 'sts' => ($inValidOrder) ? 0 : 1, 'confirmed' => ($request->from_parent) ? 2 : 1, 'rial_input' => $rial_input, 'discount' => $discount, ]); //send sms for admin $admin_message = 'هنزا گلد' . "\n" . 'سفارشی توسط ' . $customer->fname . ' ' . $customer->lname . "\n" . ' به وزن ' . Utility::round($allGram) . "\n" . 'در سیستم ثبت شد.'; Sms::Send('09128081392', $admin_message); Sms::Send('09124871664', $admin_message); Sms::Send('09124406015', $admin_message); Sms::Send('09120363240', $admin_message); auth()->user()->increment('open_order'); auth()->user()->increment('total_order'); } /*if(!$inValidOrder) { $customer->increment('remain',$allGram); }*/ foreach ($request->items as $item) { $v = ProductVariant::findOrFail($item['variant']); $p = Product::findOrFail($item['product']); $percent = ($item['percent_pay'] ?? 0) - ($item['discount'] ?? 0); $w = ($item['rash_weight'] ?? 0) + (($item['rash_weight'] ?? 0) * ($percent / 100)); $order_item = OrderItem::create([ 'order_id' => $order->id, 'variant_id' => $item['variant'], 'product_id' => $item['product'], 'product_title' => $p->title, 'variant_title' => $v->title, 'percent' => $percent, 'original_percent' => $item['percent_pay'], 'rial_pay' => $item['rial_pay'] ?? 0, 'wage' => $w - ($item['rash_weight'] ?? 0), 'qty' => $item['qty'] ?? 0, 'sts' => 1, 'w' => $w, 'note' => $item['note'], 'rash_weight' => $item['rash_weight'] ?? 0, 'original_rash_weight' => $item['rash_weight'] ?? 0, 'discount' => $item['discount'] ?? 0, ]); if (isset($item['promotion_id']) && $item['promotion_id'] != null) { //start -- remove after fix bug from mobile $promotion_item_id = $item['promotion_item_id']; $promotion = Promotion::where('id', $item['promotion_id'])->first(); if ($promotion->type == 3) { $promotionItem = PromotionItem::where('promotion_id', $item['promotion_id']) ->where('item_id', auth()->id())->first(); if ($promotionItem != null) { $promotion_item_id = $promotionItem->id; } } //end -- remove after fix bug from mobile \App\Models\Major\PromotionUsed::create([ 'customer_id' => auth()->user()->id, 'promotion_id' => $item['promotion_id'], 'promotion_item_id' => $promotion_item_id, 'order_id' => $order->id, 'order_item_id' => $order_item->id, ]); } } OrderActivity::where('order_id', $order->id)->forceDelete(); foreach (Order::$STS as $key => $item) { ; OrderActivity::create([ 'order_id' => $order->id, 'customer_id' => auth()->id(), 'title' => $item, 'note' => (($key == 1) && $inValidOrder) ? 'تعلیق سفارش به دلیل سفارش بیش از سقف مجاز' : '', 'proceed' => ($inValidOrder) ? $key == 0 : $key == 1, ]); } if ($inValidOrder) { Notify::create([ 'title' => 'ثبت سفارش شماره' . 'ORD-' . $order->id, 'customer_id' => auth()->id(), 'note' => "سقارش شما ثبت شد اما به دلیل عبور از سقف مجاز گرمی در حالت معلق قرار گرفت ", 'read' => false, 'value' => 1, 'type' => 1, ]); } else { Notify::create([ 'title' => 'ثبت سفارش شماره' . 'ORD-' . $order->id, 'customer_id' => auth()->id(), 'note' => "سفارش شما ثبت شد و در انتظار تایید مدیر سیستم می باشد. ", 'read' => false, 'value' => 1, 'type' => 1, ]); } //update cash if ($request->id) { $cash = Cash::where('order_id', $order->id)->where('type', 9); $cash->update([ 'input' => $order->w, 'value' => $order->w, 'rial_input' => $order->rial_input, 'discount' => $order->discount, ]); //send sms for admin $admin_message = 'هنزا گلد' . "\n" . 'سفارشی توسط ' . $customer->fname . ' ' . $customer->lname . "\n" . ' به وزن ' . Utility::round($allGram) . "\n" . 'در سیستم ویرایش شد.'; Sms::Send('09128081392', $admin_message); Sms::Send('09124871664', $admin_message); Sms::Send('09124406015', $admin_message); //Sms::Send('09135517425', $admin_message); } else { Cash::create([ 'customer_id' => auth()->id(), 'input' => $order->w, 'type' => 9, 'sts' => -1, 'title' => 'ثبت سند برای سفارش ' . 'ORD-' . $order->id, 'value' => $order->w, 'confirmed' => 1, 'order_id' => $order->id, 'rial_input' => $order->rial_input, 'discount' => $order->discount, ]); } //Cash::where('order_id',$order->id)->delete(); auth()->user()->increment('notify_count'); $reagent = Customer::find($customer->reagent_id); if ($request->from_parent) { /* if($inValidOrder) { Sms::Send($reagent->mobile, 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید اما به دلیل عبور از سقف مجاز گرمی در حالت معلق قرار گرفت. هنزا گلد'); } else { Sms::Send($reagent->mobile, 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید. هنزا گلد'); }*/ $confiremLink = 'https://henzagold.com/oapi/confirem/order?cId=' . $customer->id . '&oId=' . $order->id; //Sms::Send($customer->mobile, 'هنزا گلد'."\n".'همکار گرامی '.$customer->fname.' '.$customer->lname."\n".'سفارشی به وزن '.$allGram.' گرم توسط آقای '.$reagent->fname.' '.$reagent->lname.' برای شما در سیستم ثبت شده است.'."\n".'در صورت مورد تایید بودن عدد 1 را به همین شماره ارسال بفرمایید.'); Sms::Send($customer->mobile, 'هنزا گلد' . "\n" . 'همکار گرامی ' . $customer->fname . ' ' . $customer->lname . "\n" . 'سفارشی به وزن ' . $allGram . ' گرم توسط آقای ' . $reagent->fname . ' ' . $reagent->lname . ' برای شما در سیستم ثبت شده است.' . "\n" . 'در صورت مورد تایید بودن سفارش بر روی لینک زیر کلیک نمایید.' . "\n" . $confiremLink); CustomerController::updateGramSell($reagent->id); } else { //$reagent=Customer::findOrFail($customer->reagent_id); if ($inValidOrder) { Sms::Send($customer->mobile, 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید اما به دلیل عبور از سقف مجاز گرمی در حالت معلق قرار گرفت. هنزا گلد'); Notify::store($customer->token_mobile, 'هنزاگلد', 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید اما به دلیل عبور از سقف مجاز گرمی در حالت معلق قرار گرفت.'); } else { Sms::Send($customer->mobile, 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید. هنزا گلد'); Notify::store($customer->token_mobile, 'هنزاگلد', 'سفارش شما به شماره ' . $order->id . ' به ثبت رسید. '); } if ($reagent != null) { Sms::Send($reagent->mobile, 'هنزا گلد' . "\n" . 'سفارشی توسط ' . $customer->fname . ' ' . $customer->lname . ' به شماره ' . $order->id . 'و به وزن ' . $allGram . ' به ثبت رسید.'); Notify::store($reagent->token_mobile, 'هنزاگلد', 'سفارشی توسط ' . $customer->fname . ' ' . $customer->lname . ' به شماره ' . $order->id . 'و به وزن ' . $allGram . ' به ثبت رسید.'); } } CustomerController::updateRemain($customer->id); CustomerController::updateGramSell($customer->id); return new JsonResponse(['id' => 'ORD-' . $order->id]); } public function getChildOrder(Request $request) { $records = Order::with(['items' => function ($q) { return $q->with(['variants']); }, 'customer']) ->orderBy('id', 'DESC') ->where('sts', 5) ->where('reagent_id', auth()->id()); TableEngine::create($records, $request->all()) ->initSearch(['id', 'customer_id', 'customer.name', 'customer.mobile']) ->select([ '*', Order::$SELECT_CAJ, Order::$SELECT_UAJ, Order::$SELECT_STS_STR, Order::$SELECT_COLOR, Order::$SELECT_IMG ])->export($records); return response()->json($records); } public function readNotify(Request $request , $id){ $record = Notify::findOrFail($id); $record->update(['read'=>true]); return response()->json(true); } public function updateChildOrder(Request $request, $id) { Log::info('update order child'); Log::info('customer id '); Log::info(auth()->id()); Log::info($request->all()); //calculate all gram with discount $allGram = array_sum(array_map(function ($item) { $percent = ($item['percent_pay'] ?? 0) ; $gram = ($item['rash_weight'] ?? 0) + (($item['rash_weight'] ?? 0) * ($percent / 100)); return $gram * ($item['qty'] ?? 0); }, $request->items)); //calculate all gram without percent on gram $wage = array_sum(array_map(function ($item) { return ($item['rash_weight'] ?? 0) * $item['qty'] ?? 0; }, $request->items)); //calculate all rial_pay $rial_input = array_sum(array_map(function ($item) { return ($item['rial_pay'] ?? 0) * $item['qty'] ?? 0; }, $request->items)); $order = Order::findOrFail($id); $sts=5; if(isset($request->final_submit) && $request->final_submit=='true') { $activity = OrderActivity::where('order_id',$id)->orderBy('id','desc')->first(); $activity->update([ 'proceed'=>1 ]); $sts=6; } $order->update([ 'input' => $allGram, 'w' => $allGram, 'wage' => $allGram - $wage, 'rial_input' => $rial_input, 'sts'=>$sts ]); foreach ($request->items as $item) { $percent = ($item['percent_pay'] ?? 0) ; $item['w'] = ($item['rash_weight'] ?? 0) + (($item['rash_weight'] ?? 0) * ($percent / 100)); $order_item = OrderItem::findorFail($item['id']); $order_item->update($item); } $cash = Cash::where('order_id', $order->id)->where('type', 9); $cash->update([ 'input' => $order->w, 'value' => $order->w, 'rial_input' => $order->rial_input, 'discount' => $order->discount, ]); CustomerController::updateRemain($order->customer_id); CustomerController::updateGramSell(auth()->id()); return new JsonResponse(['id' => 'ORD-' . $order->id]); } public function getLuxeProduct(Request $request){ $config = ConfigStatic::where('key','__list__tags') ->where('val','لوکس') ->first(); $product = Product::mobile()->whereHas('variants',function ($q){ return $q->where('grams','>',15); })->orderBy('id', 'DESC')->limit(10)->with(['thumb', 'variants', 'tmobile', 'brand', 'category']); if ($request->has('search')){ $product->whereRaw('lower(`title`) like ?', ['%' . $request->search . '%']); } return response()->json($this->addLimit($product,$request)); } public function checkVersion(Request $request){ $config = Arr::get(config(),'version.version',false); if ($config){ if ( $request->has('version')){ $version = $request->version; if ($config != $version){ return response()->json(['version'=>false]); }else{ return response()->json(['version'=>true]); } } } } public function PriceFromWebService(){ $data=ConfigStatic::where('key','gold_price_pwa')->first(); $data=json_decode($data->val,true); return response()->json($data); } public function hesabDetail(Request $request){ $me = auth()->user(); if (is_null($me->mcode)){ throw new UnprocessableEntityHttpException('کد مشتری برای شما تعریف نشده است'); } if (!$request->has('page')){ $page = 1; } else{ $page = $request->page; } if (!$request->has('limit')){ $limit = 10; } else{ $limit = $request->limit; } $filters = []; $filters[] = [ "field"=> "mcode", "value"=> floatval($me->mcode.'.0'), // "value"=> $request->value, "op"=> "" ]; $http = Http::post('http://37.152.185.182:8002/api/hesab-detail/table',[ 'page'=>$page, 'limit'=>$limit, 'filters'=> $filters, 'sort'=>'-ID' ])->json(); return response()->json($http); } public function moshtarian(Request $request){ if (! $request->name){ throw new UnprocessableEntityHttpException('وارد کردن فیلد نام اجباری است'); } $filters = []; $filters[] = [ "field"=> "name", "value"=> $request->name, "op"=> "like" ]; $http = Http::post('http://37.152.185.182:8002/api/moshtarian/table',[ 'filters'=> $filters ])->json(); return response()->json($http['rows'][0]); } public function moshtarianFilter(){ $search = auth()->user()->mcode; if (is_null($search)){ throw new UnprocessableEntityHttpException('کد مشتری برای شما تعریف نشده است'); } $filters = [ [ "field"=>"code", "op"=>"", "value"=>floatval($search.'.0') ] ]; $http = Http::post('http://37.152.185.182:8002/api/moshtarian/table',[ 'filters'=> $filters, ])->json(); return response()->json($http['rows'][0]); } }
Copyright ©2k19 -
Hexid
|
Tex7ure