/
home
/
henzagold
/
server
/
app
/
Http
/
Controllers
/
Adm
/
File Upload :
llllll
Current File: /home/henzagold/server/app/Http/Controllers/Adm/WorkshopController.php
<?php namespace App\Http\Controllers\Adm; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\InventoryWorkshop; use App\Models\InventoryTransaction; use App\Models\Order; use App\Models\Product; use App\Models\ProductVariant; use App\Models\SingleCustomerOrders; use App\Models\User; use App\Models\Customer; use App\Models\Workshop; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use Morilog\Jalali\Jalalian; class WorkshopController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\jsonResponse */ public function index(Request $request) { $records = Workshop::with('user'); TableEngine::create($records, $request->all()) ->initSearch([]) ->select([ '*', ])->export($records); return response()->json($records); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\jsonResponse */ public function store(Request $request) { // dd($request->all()); // $validator = Validator::make($request->all(), [ // 'user_id' => 'required', // ]); // if ($validator->fails()) { // return response()->json($validator->errors(),422); // } // $customer_req = [ // "fname" => $request->user_fname, // "lname" => $request->user_lname, // "phone" => $request->user_phone, // ]; // $requestFake = new \Illuminate\Http\Request($customer_req); $customer = new customer(); $customer->fname = $request->fname; $customer->lname = $request->lname; $customer->mobile = $request->mobile; $customer->password = $request->mobile; $customer->sts_verify_step_one = 2; $customer->sts_verify_step_two = 2; $customer->type = 5; $customer->save(); // dd($customer); $workshop_req = [ "name" => $request->name, "address" => $request->address, "phone" => $request->phone, "user_id" => $customer->id, ]; $Workshop = Workshop::create($workshop_req); // User::where('fname', $customer_req->fname) // ->where('lname', $customer_req->lname) // ->where('fname', $customer_req->fname) // ->update(['Workshop_id'=>$Workshop->id]); // User::findOrFail($customer->id) // ->update(['Workshop_id'=>$Workshop->id]); return response()->json(['id'=>$Workshop->id]); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\jsonResponse */ public function show($id) { $Workshop = Workshop::with('user')->findOrFail($id); return response()->json($Workshop); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\jsonResponse */ public function update(Request $request, $id) { // $validator = Validator::make($request->all(), [ // 'workshop_id' => 'required', // ]); // if ($validator->fails()) { // return response()->json($validator->errors(),422); // } $workshop_req = [ 'name' => $request->name ?? '', 'address' => $request->address ?? '', 'phone' => $request->phone ?? '' ]; $Workshop = Workshop::findOrFail($id); // if ($Workshop->user_id != $request->user_id){ // $new_user = Customer::findOrFail($request->user_id); // if (!is_null($new_user->Workshop_id)){ // return response()->json('این کاربر قبلا به عنوان مدیر شعبه ای دیگر انتخاب شده است',422); // } // Customer::findOrFail($Workshop->user_id) // ->update(['Workshop_id'=>null]); // $new_user->update(['Workshop_id'=>$id]); // } $customer_req = [ 'fname' => $request->fname ?? '', 'lname' => $request->lname ?? '', 'name' => '', 'mobile' => $request->mobile ?? '' ]; if (!empty($customer_req['fname']) || !empty($customer_req['lname'])) { $customer_req['name'] = trim(($customer_req['fname'] ?? '') . ' ' . ($customer_req['lname'] ?? '')); } Customer::findOrFail($Workshop->user_id) ->update($customer_req); $Workshop->update($workshop_req); return response()->json(true); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\jsonResponse */ public function destroy($id) { $Workshop = Workshop::findOrFail($id)->delete(); User::findOrFail($Workshop->user_id) ->update(['Workshop_id'=>null]); return response()->json(true); } public function activeWorkshop($id){ $Workshop = Workshop::findOrFail($id); $Workshop->update([ 'is_active'=> !$Workshop->is_active ]); return response()->json(true); } public function listWorkshop(){ $Workshop = Workshop::select('name','id')->get()->toArray(); return response()->json($Workshop); } public function listUser(){ $user = User::select('name','id')->get()->toArray(); return response()->json($user); } //تخصیص محصول به شعبه public function allocateProduct(Request $request){ $data = $request->all(); $old_products = Product::where('Workshop_id',$data['Workshop_id'])->select('id')->get()->pluck('id')->toArray(); foreach ($data['products'] as $item){ $product = Product::findOrFail($item); if(!in_array($item,$old_products)){ if (!is_null($product->Workshop_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update(['Workshop_id'=>$data['Workshop_id']]); $this->createInventoryTransaction($item,$data['Workshop_id'],1); } else{ $key = array_search($item,$old_products); unset($old_products[$key]); } } foreach ($old_products as $old){ Product::findOrFail($old)->update(['Workshop_id'=>null]); $this->createInventoryTransaction($old,$data['Workshop_id'],2); } return response()->json(true); } //تخصیص محصول به شعبه public function addProduct(Request $request){ $data = $request->all(); $Workshop = Workshop::findOrFail($data['Workshop_id']); $inventory_Workshop = InventoryWorkshop::create([ 'Workshop_id'=>$data['Workshop_id'], 'allocator_user_id'=>auth()->id(), 'name'=>$Workshop->name ]); foreach ($data['products'] as $item){ $product = Product::findOrFail($item); if (!is_null($product->Workshop_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update([ 'Workshop_id'=>$data['Workshop_id'], 'inventory_id'=>$inventory_Workshop->id ]); $this->createInventoryTransaction($item,$data['Workshop_id'],1,$inventory_Workshop); } return response()->json(['id'=>$inventory_Workshop->id]); } public function updateProduct(Request $request,$id){ $data = $request->all(); $inventoryWorkshop = InventoryWorkshop::findOrFail($id); $old_products = Product::where('inventory_id',$id)->select('id')->get()->pluck('id')->toArray(); foreach ($data['products'] as $item){ $product = Product::findOrFail($item); if(!in_array($item,$old_products)){ if (!is_null($product->Workshop_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update([ 'Workshop_id'=>$data['Workshop_id'], 'inventory_id'=>$id ]); $this->createInventoryTransaction($item,$data['Workshop_id'],1,$inventoryWorkshop); } else{ $key = array_search($item,$old_products); unset($old_products[$key]); } } foreach ($old_products as $old){ Product::findOrFail($old)->update([ 'Workshop_id'=>null, 'inventory_id'=>null ]); $this->createInventoryTransaction($old,$data['Workshop_id'],2,$inventoryWorkshop); } return response()->json(true); } public function getProduct($inventory_id){ $inventory = InventoryWorkshop::with(['product'=>function($q){ return $q->with(['collect.collection', 'image', 'variants.promotion', 'gallery']); },'Workshop']) ->select(['*',InventoryWorkshop::$SELECT_CAJ]) ->findOrFail($inventory_id); return response()->json($inventory); } public function deleteProduct($inventory_id){ $inventoryWorkshop = InventoryWorkshop::findOrFail($inventory_id); $old_products = Product::where('inventory_id',$inventory_id)->get(); foreach ($old_products as $old){ $old->update([ 'Workshop_id'=>null, 'inventory_id'=>null ]); $this->createInventoryTransaction($old->id,$inventoryWorkshop['Workshop_id'],2,$inventoryWorkshop); } $inventoryWorkshop->delete(); return response()->json(true); } public function createInventoryTransaction($product_id,$Workshop_id,$type,$inventory_Workshop){ $variant = ProductVariant::where('product_id_id',$product_id)->first(); InventoryTransaction::create([ 'product_variant_id' => $variant->id, 'product_id' => $product_id, 'Workshop_id' => $Workshop_id, 'allocator_user_id' => auth()->id(), 'type'=>$type ]); if ($type == 1){ $inventory_Workshop->increment('count_item'); $inventory_Workshop->increment('sum_weight',$variant->grams); }else{ $inventory_Workshop->decrement('count_item'); $inventory_Workshop->decrement('sum_weight',$variant->grams); } } // public function createInventoryTransaction($product_id,$Workshop_id,$type){ // $variant = ProductVariant::where('product_id_id',$product_id)->first(); // InventoryTransaction::create([ // 'product_variant_id' => $variant->id, // 'product_id' => $product_id, // 'Workshop_id' => $Workshop_id, // 'allocator_user_id' => auth()->id(), // 'type'=>$type // ]); // if ($type == 1){ // $inventory_Workshop = InventoryWorkshop::where('Workshop_id', $Workshop_id) // ->where('product_id',$product_id)->first(); // if (is_null($inventory_Workshop)){ // InventoryWorkshop::create([ // 'product_variant_id' => $variant->id, // 'product_id' => $product_id, // 'Workshop_id' => $Workshop_id, // 'allocator_user_id' => auth()->id(), // ]); // } // }else{ // InventoryWorkshop::where('Workshop_id', $Workshop_id) // ->where('product_id',$product_id)->delete(); // } // } public function productTable(Request $request){ $records = InventoryWorkshop::with(['allocator_user','product'=>function($q){ return $q->with(['collect.collection', 'image', 'variants.promotion', 'gallery']); }]); if (!is_null(auth()->user()->Workshop_id)){ $records->where('Workshop_id',auth()->user()->Workshop_id); } TableEngine::create($records, $request->all()) ->initSearch(['name']) ->select([ '*', InventoryWorkshop::$SELECT_CAJ ])->export($records); return response()->json($records); } public function dashboard(){ $first_month = (new Jalalian(Jalalian::now()->getYear(), Jalalian::now()->getMonth(), 1))->toCarbon(); $end_month = self::getEndOfMonth(); $orders_month = SingleCustomerOrders::where('Workshop_id',auth()->user()->Workshop_id) ->whereDate('created_at','>=',$first_month) ->whereDate('created_at','<=',$end_month); $total_price_month = $orders_month->sum('total_price'); $total_weight_month = $orders_month->sum('total_weight'); $orders_today = SingleCustomerOrders::where('Workshop_id',auth()->user()->Workshop_id)->whereDate('created_at',now()); $total_price_today = $orders_today->sum('total_price'); $total_weight_today = $orders_today->sum('total_weight'); $orders_yesterday = SingleCustomerOrders::where('Workshop_id',auth()->user()->Workshop_id)->whereDate('created_at',now()->subDay()); $total_price_yesterday = $orders_yesterday->sum('total_price'); $total_weight_yesterday = $orders_yesterday->sum('total_weight'); $orders_all = SingleCustomerOrders::where('Workshop_id',auth()->user()->Workshop_id); $total_price_all = $orders_all->sum('total_price'); $total_weight_all = $orders_all->sum('total_weight'); $first_week = now()->startOfWeek()->subDays(2); $end_week = now()->endOfWeek()->subDays(2); $orders_week = SingleCustomerOrders::with(['customer'=>function($q){ return $q->select(['id','name','phone']); }])->where('Workshop_id',auth()->user()->Workshop_id) ->whereDate('created_at','>=',$first_week) ->whereDate('created_at','<=',$end_week) ->select(['*',SingleCustomerOrders::$SELECT_CAJ]) ->get(); $max_order = Product::with(['promotion','collect.collection', 'image', 'variants.promotion', 'gallery'])->where('Workshop_id',auth()->user()->Workshop_id) ->where('count_order','>',0) ->orderBy('count_order','desc') ->limit(10) ->get(); return response()->json([ 'total_price_month'=>$total_price_month, 'total_weight_month'=>$total_weight_month, 'total_price_today'=>$total_price_today, 'total_weight_today'=>$total_weight_today, 'total_price_yesterday'=>$total_price_yesterday, 'total_weight_yesterday'=>$total_weight_yesterday, 'total_price_all'=>$total_price_all, 'total_weight_all'=>$total_weight_all, 'list_orders_week'=>$orders_week, 'max_order'=>$max_order ]); } public static function getEndOfMonth() { $jdt = jdate(); if ($jdt->isLeapYear() && $jdt->getMonth() === 12) { $lastDayOfCurrentMonth = 29; }else{ $lastDayOfCurrentMonth = $jdt->getMonth() > 6 ? 30 : 31; } return (new Jalalian($jdt->getYear(), $jdt->getMonth(), $lastDayOfCurrentMonth))->toCarbon(); } public function adminDashboard(){ $first_month = (new Jalalian(Jalalian::now()->getYear(), Jalalian::now()->getMonth(), 1))->toCarbon(); $end_month = self::getEndOfMonth(); $sell_all = SingleCustomerOrders::whereNotNull('Workshop_id')->sum('total_weight'); $sell_day = SingleCustomerOrders::whereNotNull('Workshop_id')->whereDate('created_at',now())->sum('total_weight'); $sell_month = SingleCustomerOrders::with('Workshop')->whereNotNull('Workshop_id') ->whereDate('created_at','>=',$first_month) ->whereDate('created_at','<=',$end_month) ->sum('total_weight'); $orders_today = SingleCustomerOrders::with('Workshop')->whereNotNull('Workshop_id') ->whereDate('created_at',now()) ->select(DB::raw('sum(total_weight) as sum'),'Workshop_id') ->groupBy('Workshop_id') ->orderBy('sum','desc') ->get(); $orders_month = SingleCustomerOrders::with('Workshop')->whereNotNull('Workshop_id') ->whereDate('created_at','>=',$first_month) ->whereDate('created_at','<=',$end_month) ->select(DB::raw('sum(total_weight) as sum'),'Workshop_id') ->groupBy('Workshop_id') ->orderBy('sum','desc') ->limit(5) ->get(); return response()->json([ 'sell_all'=>$sell_all, 'sell_day'=>$sell_day, 'sell_month'=>$sell_month, 'list_Workshop_in_day'=>$orders_today, 'list_Workshop_in_mounth'=>$orders_month ]); } }
Copyright ©2k19 -
Hexid
|
Tex7ure