HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/fieldsblaze-heroku/app/Http/Controllers/StockController.php
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Customer;
use App\Models\PriceBookEntry;
use App\Models\Stocks;
use App\Models\StockProducts;
use App\Models\Product;
use App\Library\UserLib;

class StockController extends Controller
{
    protected $userLib;
    public function __construct(UserLib $userLib)
    {
        $this->userLib = $userLib;
    }

    public function allStock()
    {

        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '0')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        $trashStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '1')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        return view('admin.all-stock', ['AllStock' => $AllStock, 'trashStock' => $trashStock]);
    }

    public function returnStock()
    {

        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllReturnStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '0')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        $trashRetrunStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '1')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        return view('admin.return-stock', ['AllStock' => $AllReturnStock, 'trashStock' => $trashRetrunStock]);
    }

    // Trash Stock
    public function trashStock()
    {

        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '0')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        $trashStock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_stocks.IsDeleted', '1')
            ->orderBy('tbl_stocks.id', 'desc')
            ->get();
        return view('admin.trash-stock', ['AllStock' => $AllStock, 'trashStock' => $trashStock]);
    }

    // View Stock
    public function viewStock($StockId)
    {

        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $Stock = Stocks::select('tbl_stocks.*', 'tbl_user.FirstName as UserFirstName', 'tbl_user.LastName as UserLastName', 'tbl_customer.CustomerName as CustomerName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->where('tbl_stocks.StockId', $StockId)
            ->leftJoin('tbl_admin', 'tbl_stocks.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_stocks.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_stocks.CustomerId', '=', 'tbl_customer.CustomerId')
            ->get()
            ->first();
        $StockProduct = StockProducts::select('tbl_stock_products.*')
            ->where('StockId', $StockId)
            ->where('IsDeleted', '0')
            ->get();

        return view('admin.view-stock', compact('Stock', 'StockProduct'));
    }

    ### Store New Target
    public function storeStock(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $CustomerId = $request->input('CustomerName');

        // Get PriceBook
        $getCustomerPriceBookId = Customer::orderBy('Created_at', 'DESC')->first();
        $PriceBookId = $getCustomerPriceBookId['PriceBookId'];
        
        $UserId = $request->input('addUserName');
        $ProductId = $request->input('addStockProduct');

        $SDate = '';
        if ($request->input('stockDate')) {
            $SDate = str_replace('/', '-', $request->input('stockDate'));
        }
        $StockDate = date('d-m-Y', strtotime($SDate));

        $data['StockId'] =  $this->userLib->useridGenrator();
        $data['StockDate'] =  $StockDate;
        $data['StockNo'] = "STK-" . time() . '-' . rand();
        $data['CustomerId'] = $CustomerId;
        $data['UserId'] = $UserId;
        $data['IsDeleted'] = 0;
        $data['StockStatus'] = 0;
        $data['Status'] = 1;
        $data['Created_by'] = session('UserId');
        $data['Created_at'] = date('Y-m-d H:i:s', time());
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());
        $data['DeviceName'] = 'Web';

        $insert_stock = Stocks::insert($data);

        if ($insert_stock) {

            $latestStockId = Stocks::orderBy('Created_at', 'DESC')->first();
            $StockId = $latestStockId['StockId'];

            // Get Product Name
            $get_product = Product::select('tbl_products.*')
                ->where('ProductId', $ProductId)
                ->get()
                ->first();
            $ProductName = $get_product['ProductName'];

            // Get Product Price
            $get_product_Price = PriceBookEntry::select('tbl_price_book_entry.*')
                ->where('ProductId', $ProductId)
                ->where('PriceBookId', $PriceBookId)
                ->get()
                ->first();
            $ProductPrice = $get_product_Price['ListPrice'];

            // Insert Line Item 
            $pi_data['StockProductId'] = $this->userLib->useridGenrator();
            $pi_data['StockId'] = $StockId;
            $pi_data['ProductId'] = $ProductId;
            $pi_data['UserId'] = $UserId;
            $pi_data['CustomerId'] = $CustomerId;
            $pi_data['ProductName'] = $ProductName;
            $pi_data['ProductPrice'] = $ProductPrice;
            $pi_data['ProductQuantity'] = $request->input('addStockProductQuantity');
            $pi_data['UserId'] = $request->input('UserName');
            $pi_data['Status'] = 1;
            $pi_data['IsDeleted'] = 0;
            $pi_data['Created_by'] = session('UserId');
            $pi_data['Created_at'] = date('Y-m-d H:i:s', time());
            $pi_data['Updated_by'] = session('UserId');
            $pi_data['Updated_at'] = date('Y-m-d H:i:s', time());

            $insert_stock_product = StockProducts::insert($pi_data);

            if ($insert_stock_product) {
                return response()->json('status_success');
            } else {
                return response()->json('status_failed');
            }
        }
    }


    #### Delete Stock
    public function deleteStock($StockId)
    {
        $CreatedDate = date('Y-m-d H:i:s', time());

        $data = array();
        $data['Updated_at'] = $CreatedDate;
        $data['Updated_by'] = session('UserId');
        $data['IsDeleted'] = '1';

        $stock_delete = Stocks::where('StockId', $StockId)->update($data);
        if ($stock_delete) {
            echo '1';
        } else {
            echo '0';
        }
    }

    #### Delete Permanent
    public function destroyStock($StockId)
    {
        $destroyData = Stocks::where('StockId', $StockId);
        $destroyData->delete();
    }
    #### Restore Performance
    public function restoreStock($StockId)
    {
        $CreatedDate = date('Y-m-d H:i:s', time());

        $data = array();
        $data['Updated_at'] = $CreatedDate;
        $data['Updated_by'] = session('UserId');
        $data['IsDeleted'] = '0';

        $stock_restore = Stocks::where('StockId', $StockId)->update($data);
        if ($stock_restore) {
            echo '1';
        } else {
            echo '0';
        }
    }

    #### Delete Selected Record
    public function deleteSelectedStock(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $id = $request->input('SelectedStock');
        if ($id) {
            foreach ($id as $StockId) {
                $checkData = Stocks::find($StockId);
                if ($checkData->IsDeleted == '0') {
                    $CreatedDate = date(
                        'Y-m-d H:i:s',
                        time()
                    );
                    $data = array();
                    $data['Updated_at'] = $CreatedDate;
                    $data['IsDeleted'] = '1';

                    $selected_entry = Stocks::where('id', $StockId)->update($data);
                    if ($selected_entry) {
                        echo '1';
                    } else {
                        echo '0';
                    }
                } else {
                    $selected_entry = Stocks::find($StockId);
                    $selected_entry->delete();
                }
            }
        }

        return redirect()->back()->with('status_success');
    }

    #### Delete Stock Item
    public function deleteStockProduct($StockProductId)
    {
        $CreatedDate = date('Y-m-d H:i:s', time());

        $data = array();
        $data['Updated_at'] = $CreatedDate;
        $data['Updated_by'] = session('UserId');
        $data['IsDeleted'] = '1';

        $stock_item_delete = StockProducts::where('StockProductId', $StockProductId)->update($data);
        if ($stock_item_delete) {
            echo '1';
        } else {
            echo '0';
        }
    }

}