Created
September 22, 2019 16:03
-
-
Save ahmadwaliesipick/42eac777d6d5788be9403989fb1a64f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use App\Http\Requests\InventoryCreateRequest; | |
use App\Http\Requests\InventoryGetRequest; | |
use App\Inventory; | |
use App\Product; | |
use App\Vendor; | |
use Illuminate\Http\Request; | |
use Dingo\Api\Routing\Helpers; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\DB; | |
/** | |
* @group Inventory management | |
* | |
* */ | |
class InventoryController extends Controller | |
{ | |
use Helpers; | |
/** | |
* Create Inventory | |
* | |
*/ | |
public function create(InventoryCreateRequest $request){ | |
//create_vendor and return id @todo | |
$data=[ | |
'user_id'=>Auth::user()->id, | |
'product_id'=>$request->product_id, | |
'invoice_id'=>$request->invoice_id, | |
'invoice_date'=>$request->invoice_date, | |
'company_id'=>$request->company_id, | |
'quantity'=>$request->quantity, | |
'action'=>$request->action, | |
]; | |
return $this->response->array(Inventory::create($data)); | |
} | |
/** | |
* Get Inventories | |
* | |
*/ | |
public function get(InventoryGetRequest $request){ | |
$inventories = DB::table('inventories') | |
->select('products.id','products.title','products.price','categories.name as category',DB::raw('MAX(inventories.updated_at) as updated_at'), | |
DB::raw('sum((CASE WHEN inventories.action = "up" THEN inventories.quantity ELSE 0 END)) as up_total'), | |
DB::raw('sum((CASE WHEN inventories.action = "down" THEN inventories.quantity ELSE 0 END)) as down_total')) | |
->join('products','inventories.product_id','=','products.id') | |
->join('categories','categories.id','=','products.category_id') | |
->groupBy('product_id'); | |
$inventories = $inventories->whereNull('inventories.deleted_at'); | |
if(!empty($request->company_id)){ | |
$inventories = $inventories->where('inventories.company_id',$request->company_id); | |
} | |
$allUsedTotal = $inventories; | |
$allUsedTotal =$allUsedTotal->get(); | |
$totalSumUpAll= 0; | |
$totalSumDownAll= 0; | |
$totalSumUpF=0; | |
$totalSumDownF=0; | |
foreach($allUsedTotal as &$inventory){ | |
$totalSumUp = $totalSumUpAll + $inventory->up_total; | |
$totalSumDown = $totalSumDownAll + $inventory->down_total; | |
$inventory->total_up_all = $totalSumUp-$totalSumDown; | |
$totalSumUpF =$totalSumUpF+$inventory->total_up_all; | |
$totalSumDownF = $totalSumDownF + ($inventory->total_up_all * $inventory->price); | |
} | |
$allUsedTotal = $allUsedTotal->toArray(); | |
$inventories = $inventories->whereNull('inventories.deleted_at'); | |
if(!empty($request->sortBy)){ | |
$descending= 'desc'; | |
if(!empty($request->descending) && $request->descending == 'false'){ | |
$descending= 'asc'; | |
} | |
if($request->sortBy == 'id' || $request->sortBy == 'price' || $request->sortBy == 'title'){ | |
$inventories = $inventories->orderBy($request->sortBy,$descending); | |
} | |
}else{ | |
$inventories = $inventories->orderBy('down_total','desc'); | |
} | |
if($request->date_to && $request->date){ | |
$inventories = $inventories->whereBetween('inventories.updated_at', [$request->date.' 00:00:01', | |
$request->date_to.' 23:59:59']); | |
}elseif($request->date){ | |
$inventories = $inventories->whereDate('inventories.updated_at', $request->date); | |
} | |
if($request->title){ | |
$inventories = $inventories->where('title','like', '%'.$request->title.'%'); | |
} | |
$calculateInventories = $inventories; | |
$calculateInventories = $calculateInventories->orderBy('down_total','desc'); | |
$calculateInventories = $calculateInventories->get(); | |
$totalSumUp=0; | |
$totalSumDown=0; | |
$totalSumUpCost=0; | |
$sumDownCost=0; | |
$sumTotal=0; | |
foreach($calculateInventories as &$inventory){ | |
$index = array_search($inventory->id, array_column($allUsedTotal, 'id')); | |
$total_up_all = $allUsedTotal[$index]->total_up_all; | |
$totalSumDown = $totalSumDown + $inventory->down_total; | |
$totalSumUp = $totalSumUp + $total_up_all; | |
$sumDownCost = $sumDownCost + $inventory->down_total * $inventory->price; | |
$totalSumUpCost = $totalSumUpCost + ($total_up_all * $inventory->price); | |
$inventory->sum_total = ($total_up_all - $inventory->down_total) *$inventory->price; | |
$sumTotal = $sumTotal+$inventory->sum_total; | |
} | |
if( !empty($request->size) && $request->size > 0){ | |
$inventories = $inventories->paginate($request->size); | |
}else{ | |
$inventories = $inventories->paginate($inventories->get()->count()); | |
} | |
foreach($inventories as &$inventory){ | |
$index = array_search($inventory->id, array_column($allUsedTotal, 'id')); | |
$inventory->sum_up_all =$allUsedTotal[$index]->total_up_all; | |
$total =$inventory->sum_up_all; | |
$inventory->sum_total =$total*$inventory->price; | |
} | |
$custom = collect([ | |
'total_down'=>number_format($totalSumDown), | |
'down_cost'=>number_format($sumDownCost), | |
'total_up'=>number_format($totalSumUp), | |
'total_up_cost'=>number_format($totalSumUpCost), | |
'total_prices'=>number_format($sumTotal), | |
'total_available_stock'=>number_format($totalSumUpF), | |
'total_stock'=>number_format($totalSumDownF)]); | |
$inventories = $custom->merge($inventories); | |
return $inventories; | |
} | |
/** | |
* Get Inventories | |
* | |
*/ | |
public function delete(InventoryGetRequest $request){ | |
return Inventory::where('id',$request->id)->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment