File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/InventoryTweakController.php
<?php
namespace App\Http\Controllers;
use App\InventoryTweak;
use App\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\OrderController;
use App\ProductAttribute;
use App\Attribute;
use App\Parameter;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use DataTables;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Events\InventoryModification;
use App\InventoryMovementType;
class InventoryTweakController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view("inventory_tweaks.list");
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$products = Product::where("active", true)->get();
$inventory_movement_types = InventoryMovementType::all();
return view("inventory_tweaks.create", compact("products", "inventory_movement_types"));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try{
DB::beginTransaction();
$product_attribute = ProductAttribute::where('product_id', $request->product_id)->where('value', $request->product_attribute_value)->first();
if($request->quantity<0 && $product_attribute->available_units<abs($request->quantity)){
return response(array('r' => true, "type" => "error", 'm' => 'Error: ', "data" => null));
}else{
$o = new InventoryTweak;
$o->user_id = Auth::user()->id;
$o->product_id = $request->product_id;
$o->inventory_movement_type_id = $request->inventory_movement_type_id;
$o->sku_attribute = $request->product_attribute_sku;
$o->value_attribute = $request->product_attribute_value;
$o->quantity = $request->quantity;
$o->description = $request->description;
$o->save();
// Modificar inventario
$this->updateInventory($request->product_id, $request->product_attribute_id, $request->product_attribute_value, $request->quantity, $request);
// EnvĂo de notificaciones
$parameters = Parameter::where('id', 1)->first();
if($parameters->mailing_inventory){
$product = Product::where("id", $request->product_id)->first();
$inventory_movement_type = InventoryMovementType::find($request->inventory_movement_type_id);
$attribute = Attribute::where('id', $product_attribute->attribute_id)->first();
$data_modification = array(
'product_id' => $request->product_id,
'product_name' => $product->name,
'product_plu' => $product->plu,
'product_attribute_sku' => $request->product_attribute_sku,
'product_attribute_value' => $request->product_attribute_value,
'product_attribute_name' => $attribute->name,
'inventory_movement_type' => $inventory_movement_type->name,
'quantity' => $request->quantity,
'description' => $request->description
);
event(new InventoryModification($data_modification));
}
DB::commit();
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => null));
}
} catch (\Exception $e) {
DB::rollBack();
return array('r' => false, 'd' => null, 'm' => 'Error: ' . $e->getMessage());
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function tableFilter(){
$obj = InventoryTweak::with('product')
->with('user')
->with('inventory_movement_type')
->orderBy('created_at','desc');
return DataTables::of($obj)
->editColumn('user_id', function ($obj) {
return $obj->user-> first_name . ' ' . $obj->user->last_name . ' - ' . $obj->user->document;
})
->editColumn('inventory_movement_type_id', function ($obj) {
if($obj->inventory_movement_type){
return $obj->inventory_movement_type->name;
}
return "N/A";
})
->editColumn('product_id', function ($obj) {
if($obj->sku_attribute){
return $obj->product->name . " - " . $obj->sku_attribute;
}else {
return $obj->product->name;
}
})
->editColumn('created_at', function ($obj) {
return $obj->created_at->format("d/m/Y H:i:s");
})
->make(true);
}
// Ajustes de inventario por el usuario
public function updateInventory($product_id, $product_attribute_id, $value_attribute, $quantity, $request){
$c = new OrderController($request);
$operacion = "sum";
$quantity_sho = $quantity;
if($quantity < 0){
$quantity = abs($quantity);
$operacion = "res";
}
$c->modifyQuanityProduct($product_id, $quantity, $operacion);
if($product_attribute_id){
$attributes = new Collection();
$attr = array(["attribute_id" => $product_attribute_id, 'value' => $value_attribute]);
$attributes = collect($attr)->map(function ($item) {
return (object) $item;
});
$c->modifyQuanityProductAttributes($attributes, $quantity, $operacion, $product_id);
$product_attribute = ProductAttribute::where('id', $request->product_attribute_real_id)->first();
if($product_attribute){
$sho = new ShopifyController();
$sho->updateInventoryLevel($product_attribute->reference_shopify_variation_id,$quantity_sho);
}
}
}
public function attributesProduct($product_id){
return ProductAttribute::where('product_id', $product_id)->with('attribute')->get();
}
}