/
home
/
old_henza
/
server
/
app
/
Http
/
Controllers
/
Adm
/
File Upload :
llllll
Current File: /home/old_henza/server/app/Http/Controllers/Adm/BranchController.php
<?php namespace App\Http\Controllers\Adm; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Branch; use App\Models\InventoryBranch; 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 Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use Morilog\Jalali\Jalalian; class BranchController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\jsonResponse */ public function index(Request $request) { $records = Branch::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) { $validator = Validator::make($request->all(), [ 'user_id' => 'required', ]); if ($validator->fails()) { return response()->json($validator->errors(),422); } $branch = Branch::create($request->all()); User::findOrFail($request->user_id) ->update(['branch_id'=>$branch->id]); return response()->json(['id'=>$branch->id]); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\jsonResponse */ public function show($id) { $branch = Branch::with('user')->findOrFail($id); return response()->json($branch); } /** * 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(), [ 'user_id' => 'required', ]); if ($validator->fails()) { return response()->json($validator->errors(),422); } $branch = Branch::findOrFail($id); if ($branch->user_id != $request->user_id){ $new_user = User::findOrFail($request->user_id); if (!is_null($new_user->branch_id)){ return response()->json('این کاربر قبلا به عنوان مدیر شعبه ای دیگر انتخاب شده است',422); } User::findOrFail($branch->user_id) ->update(['branch_id'=>null]); $new_user->update(['branch_id'=>$id]); } $branch->update($request->all()); return response()->json(true); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\jsonResponse */ public function destroy($id) { $branch = Branch::findOrFail($id)->delete(); User::findOrFail($branch->user_id) ->update(['branch_id'=>null]); return response()->json(true); } public function activeBranch($id){ $branch = Branch::findOrFail($id); $branch->update([ 'is_active'=> !$branch->is_active ]); return response()->json(true); } public function listBranch(){ $branch = Branch::select('name','id')->get()->toArray(); return response()->json($branch); } 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('branch_id',$data['branch_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->branch_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update(['branch_id'=>$data['branch_id']]); $this->createInventoryTransaction($item,$data['branch_id'],1); } else{ $key = array_search($item,$old_products); unset($old_products[$key]); } } foreach ($old_products as $old){ Product::findOrFail($old)->update(['branch_id'=>null]); $this->createInventoryTransaction($old,$data['branch_id'],2); } return response()->json(true); } //تخصیص محصول به شعبه public function addProduct(Request $request){ $data = $request->all(); $branch = Branch::findOrFail($data['branch_id']); $inventory_branch = InventoryBranch::create([ 'branch_id'=>$data['branch_id'], 'allocator_user_id'=>auth()->id(), 'name'=>$branch->name ]); foreach ($data['products'] as $item){ $product = Product::findOrFail($item); if (!is_null($product->branch_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update([ 'branch_id'=>$data['branch_id'], 'inventory_id'=>$inventory_branch->id ]); $this->createInventoryTransaction($item,$data['branch_id'],1,$inventory_branch); } return response()->json(['id'=>$inventory_branch->id]); } public function updateProduct(Request $request,$id){ $data = $request->all(); $inventoryBranch = InventoryBranch::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->branch_id)){ return response()->json('این محصول قبلا اختصاص داده شده است',422); } $product->update([ 'branch_id'=>$data['branch_id'], 'inventory_id'=>$id ]); $this->createInventoryTransaction($item,$data['branch_id'],1,$inventoryBranch); } else{ $key = array_search($item,$old_products); unset($old_products[$key]); } } foreach ($old_products as $old){ Product::findOrFail($old)->update([ 'branch_id'=>null, 'inventory_id'=>null ]); $this->createInventoryTransaction($old,$data['branch_id'],2,$inventoryBranch); } return response()->json(true); } public function getProduct($inventory_id){ $inventory = InventoryBranch::with(['product'=>function($q){ return $q->with(['collect.collection', 'image', 'variants.promotion', 'gallery']); },'branch']) ->select(['*',InventoryBranch::$SELECT_CAJ]) ->findOrFail($inventory_id); return response()->json($inventory); } public function deleteProduct($inventory_id){ $inventoryBranch = InventoryBranch::findOrFail($inventory_id); $old_products = Product::where('inventory_id',$inventory_id)->get(); foreach ($old_products as $old){ $old->update([ 'branch_id'=>null, 'inventory_id'=>null ]); $this->createInventoryTransaction($old->id,$inventoryBranch['branch_id'],2,$inventoryBranch); } $inventoryBranch->delete(); return response()->json(true); } public function createInventoryTransaction($product_id,$branch_id,$type,$inventory_branch){ $variant = ProductVariant::where('product_id_id',$product_id)->first(); InventoryTransaction::create([ 'product_variant_id' => $variant->id, 'product_id' => $product_id, 'branch_id' => $branch_id, 'allocator_user_id' => auth()->id(), 'type'=>$type ]); if ($type == 1){ $inventory_branch->increment('count_item'); $inventory_branch->increment('sum_weight',$variant->grams); }else{ $inventory_branch->decrement('count_item'); $inventory_branch->decrement('sum_weight',$variant->grams); } } // public function createInventoryTransaction($product_id,$branch_id,$type){ // $variant = ProductVariant::where('product_id_id',$product_id)->first(); // InventoryTransaction::create([ // 'product_variant_id' => $variant->id, // 'product_id' => $product_id, // 'branch_id' => $branch_id, // 'allocator_user_id' => auth()->id(), // 'type'=>$type // ]); // if ($type == 1){ // $inventory_branch = InventoryBranch::where('branch_id', $branch_id) // ->where('product_id',$product_id)->first(); // if (is_null($inventory_branch)){ // InventoryBranch::create([ // 'product_variant_id' => $variant->id, // 'product_id' => $product_id, // 'branch_id' => $branch_id, // 'allocator_user_id' => auth()->id(), // ]); // } // }else{ // InventoryBranch::where('branch_id', $branch_id) // ->where('product_id',$product_id)->delete(); // } // } public function productTable(Request $request){ $records = InventoryBranch::with(['allocator_user','product'=>function($q){ return $q->with(['collect.collection', 'image', 'variants.promotion', 'gallery']); }]); if (!is_null(auth()->user()->branch_id)){ $records->where('branch_id',auth()->user()->branch_id); } TableEngine::create($records, $request->all()) ->initSearch(['name']) ->select([ '*', InventoryBranch::$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('branch_id',auth()->user()->branch_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('branch_id',auth()->user()->branch_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('branch_id',auth()->user()->branch_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('branch_id',auth()->user()->branch_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('branch_id',auth()->user()->branch_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('branch_id',auth()->user()->branch_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('branch_id')->sum('total_weight'); $sell_day = SingleCustomerOrders::whereNotNull('branch_id')->whereDate('created_at',now())->sum('total_weight'); $sell_month = SingleCustomerOrders::with('branch')->whereNotNull('branch_id') ->whereDate('created_at','>=',$first_month) ->whereDate('created_at','<=',$end_month) ->sum('total_weight'); $orders_today = SingleCustomerOrders::with('branch')->whereNotNull('branch_id') ->whereDate('created_at',now()) ->select(DB::raw('sum(total_weight) as sum'),'branch_id') ->groupBy('branch_id') ->orderBy('sum','desc') ->get(); $orders_month = SingleCustomerOrders::with('branch')->whereNotNull('branch_id') ->whereDate('created_at','>=',$first_month) ->whereDate('created_at','<=',$end_month) ->select(DB::raw('sum(total_weight) as sum'),'branch_id') ->groupBy('branch_id') ->orderBy('sum','desc') ->limit(5) ->get(); return response()->json([ 'sell_all'=>$sell_all, 'sell_day'=>$sell_day, 'sell_month'=>$sell_month, 'list_branch_in_day'=>$orders_today, 'list_branch_in_mounth'=>$orders_month ]); } }
Copyright ©2k19 -
Hexid
|
Tex7ure