File: /var/www/vhost/disk-apps/dev-beg.teky.com.co/app/Http/Controllers/Api/InventoriesApiController.php
<?php
namespace App\Http\Controllers\Api;
use App\Storage;
use App\Location;
use App\Inventory;
use Carbon\Carbon;
use App\TypeProcess;
use App\TransaccionInventory;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class InventoriesApiController extends Controller
{
public $currentQuantityOrigin = null;
public $currentQuantityDestination = null;
private function defineQuantity($type_process_id,$item_id,$storage_id,$quantity,$location_id,$destination_storage_id,$destination_location_id)
{
$operation = '+';
$invetory = Inventory::where([['item_id', $item_id],['storage_id', $storage_id], ['location_id', $location_id]])->first();
$this->currentQuantityOrigin = $invetory->quantity_available;
switch ($type_process_id) {
case '1':
$invetory->quantity_available += $quantity;
break;
case '2':
$invetory->quantity_available -= $quantity;
$operation = '-';
break;
case '3':
$invetory->quantity_available -= $quantity;
$operation = '-';
$destination = Inventory::where([['item_id', $item_id],['storage_id', $destination_storage_id],['location_id', $destination_location_id]])->first();
$this->currentQuantityDestination = $invetory->quantity_available;
if($destination){
$destination->quantity_available += $quantity;
$destination->update();
break;
}
break;
case '4':
$stringValue = strval($quantity);
if(Str::contains($stringValue, '-')){
$invetory->quantity_available -= abs($quantity);
$operation = '';
}else{
$invetory->quantity_available += $quantity;
}
break;
default:
break;
}
$invetory->update();
return $operation;
}
public function getTypeProcesses()
{
try {
$typeProcesses = TypeProcess::where('active', true)->get();
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => array('typeProcesses' => $typeProcesses)));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function getStorages()
{
try {
$storages = Storage::where('active', true)->with('inventories')->orderBy('name', 'asc')->get();
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => array('storages' => $storages)));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function getLocations($storage_id)
{
try {
$locations = Location::where([['storage_id', $storage_id],['active', true]])->get();
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => $locations));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function gerRecords($location_id, $item_id, $storage_id)
{
try {
$records = TransaccionInventory::where([['item_id', $item_id],['storage_id', $storage_id],['location_id', $location_id]])
->with('typeProcess')
->orderBy('id', 'desc')
->take(3)
->get();
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => $records));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function validationDestination($item_id, $destination_storage_id, $destination_location_id)
{
try {
$destination = Inventory::where([['item_id', $item_id],['storage_id', $destination_storage_id],['location_id', $destination_location_id]])->first();
if(!$destination){
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => '', "data" => null));
}
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => null));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function getItems($location_code)
{
try {
$location = Location::where([['code', $location_code],['active', true]])->with('storage')->first();
if(!$location){
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => '', "data" => null));
}
$items = Inventory::where([['storage_id', $location->storage_id],['location_id', $location->id]])->with('item')->get();
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => array('items' => $items, 'location' => $location)));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
public function createTransaccion(Request $request)
{
try {
$operation = $this->defineQuantity(
$request->input('type_process_id'),$request->input('item_id'),
$request->input('storage_id'),$request->input('quantity'),
$request->input('location_id'),
$request->input('destination_storage_id'), $request->input('destination_location_id')
);
$date_move = $request->input('type_process_id') == 3 ? Carbon::instance(Carbon::now())->format('Y-m-d') : null;
TransaccionInventory::create(array_merge($request->all(), [
'user_id' => Auth::user()->id,
'document_number' => Str::uuid(),
'quantity' => $operation . $request->input('quantity'),
'move_at' => $date_move,
'current_quantity_origin' => $this->currentQuantityOrigin,
'current_quantity_destination' => $this->currentQuantityDestination
]));
if($request->input('type_process_id') == 3){
TransaccionInventory::create(array_merge($request->all(), [
'user_id' => Auth::user()->id,
'document_number' => Str::uuid(),
'quantity' => "+" . $request->input('quantity'),
'storage_id' => $request->input('destination_storage_id'),
'location_id' => $request->input('destination_location_id'),
'destination_storage_id' => $request->input('storage_id'),
'destination_location_id' => $request->input('location_id'),
'move_at' => $date_move,
'current_quantity_origin' => $this->currentQuantityOrigin,
'current_quantity_destination' => $this->currentQuantityDestination
]));
}
return response(array("status" => true, "type" => "success", "title" => "", "message" => "", "data" => null));
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_global'), "data" => $th->getMessage()));
}
}
}