File: /var/www/html/fieldsblaze-heroku/app/Http/Controllers/APIStockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use DB;
use App\Library\UserLib;
use App\Models\User;
use App\Models\Product;
use App\Models\Customer;
use App\Models\PriceBookEntry;
use App\Models\Order;
use App\Models\OrderItems;
use App\Models\OrderStatus;
use App\Models\Countries;
use App\Models\PaymentType;
use App\Models\Stocks;
use App\Models\StockProducts;
use DateTime;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class APIStockController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $userLib;
protected $dateFormat = 'dd-mm-yyyy';
public function __construct(UserLib $userLib)
{
$this->userLib = $userLib;
}
//createVisits
public function createStock(Request $request) {
//TODO What is device_token
$data = $request->json()->all();
$StockId = $this->userLib->useridGenrator();
$CreatedDate = date('Y-m-d H:i:s',time());
$UserId = $request->header('UserId');
$AuthToken = $request->header('AuthToken');
//check auth token
if($this->userLib->tokenChecker($request) == FALSE)
{
$response = [
'success' => false,
'ResponseMessage' => "Authentication Failed",
'ResponseCode' => "301",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
$Products = $data['Products'];
$Products = json_decode(json_encode($Products), true);
$cvdata = array();
$cvdata['StockNo'] = "STOCK-" . time() . '-' . rand();
$cvdata['CustomerId'] = $data['CustomerId'];
$cvdata['StockId'] = $StockId;
$cvdata['UserId'] = $UserId;
$cvdata['Status'] = '1';
$cvdata['IsDeleted'] = '0';
$cvdata['StockDate'] = $CreatedDate;
$cvdata['Updated_at'] = $CreatedDate;
$cvdata['Created_at'] = $CreatedDate;
$cvdata['Created_by'] = $UserId;
$cvdata['Updated_by'] = $UserId;
$StockInsert = Stocks::insert($cvdata);
if(count($Products) > 0)
{
for($i = 0; $i < count($Products); $i++)
{
$ProductData = $Products[$i];
$cvstdata = array();
$cvstdata['CustomerId'] = $data['CustomerId'];
$cvstdata['StockId'] = $StockId;
$cvstdata['ProductId'] = $ProductData['ProductId'];
$cvstdata['ProductName'] = $ProductData['ProductName'];
$cvstdata['ProductPrice'] = $ProductData['ProductPrice'];
$cvstdata['ProductQuantity'] = $ProductData['ProductQuantity'];
$cvstdata['UserId'] = $UserId;
$cvstdata['Status'] = '1';
$cvstdata['IsDeleted'] = '0';
$cvstdata['Updated_at'] = $CreatedDate;
$cvstdata['Created_at'] = $CreatedDate;
$cvstdata['Created_by'] = $UserId;
$cvstdata['Updated_by'] = $UserId;
$StockProductInsert = StockProducts::insert($cvstdata);
}
}
$Stocks = Stocks::select('*')
->where('StockId',$StockId)
->where('Status', '=', '1')
->where('IsDeleted', '=', 0)
->get()->first();
$CustomerId = $Stocks->CustomerId;
$CustomerName = '';
$Customers = Customer::where('CustomerId', $CustomerId)->get();
if ($Customers->count() > 0)
{ //email is not exist
$CustomerData = $Customers->first();
$CustomerName = $CustomerData->CustomerName;
}
$Stocks->CustomerName = $CustomerName;
$StockProducts = StockProducts::where('StockId', $StockId)->get();
$Stocks->Products = array();
if ($StockProducts->count() > 0)
{ //email is not exist
$StockProductData = $StockProducts;
$Stocks->Products = $StockProductData;
}
foreach ($Stocks->Products as $key => $StockProductValue)
{
$CustomerId = $StockProductValue->CustomerId;
$CustomerName='';
$custmer = Customer::where('CustomerId', $CustomerId)->get();
if ($custmer->count() > 0)
{ //email is not exist
$custmer = $custmer->first();
$CustomerName = $custmer->CustomerName;
}
$StockProductValue->CustomerName = $CustomerName;
}
$response = [
'Success' => true,
'ResponseMessage' => "Stock has been added",
'ResponseCode' => "200",
'Stock' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
public function updateStock(Request $request) {
//TODO What is device_token
$data = $request->json()->all();
$StockId = $this->userLib->useridGenrator();
$CreatedDate = date('Y-m-d H:i:s',time());
$UserId = $request->header('UserId');
$AuthToken = $request->header('AuthToken');
//check auth token
if($this->userLib->tokenChecker($request) == FALSE)
{
$response = [
'success' => false,
'ResponseMessage' => "Authentication Failed",
'ResponseCode' => "301",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
$Products = isset($data['Products']) ? $data['Products'] : array();
if($Products){
$Products = json_decode(json_encode($Products), true);
}
$cvdata = array();
$StockId = $data['StockId'];
unset($data['StockId']);
$cvdata['CustomerId'] = $data['CustomerId'];
$cvdata['Status'] = $data['Status'];
$cvdata['IsDeleted'] = $data['IsDeleted'];
$cvdata['Updated_at'] = $CreatedDate;
$cvdata['Updated_by'] = $UserId;
$update = Stocks::where('StockId',$StockId)->update($cvdata);
if(count($Products) > 0)
{
for($i = 0; $i < count($Products); $i++)
{
$ProductData = $Products[$i];
$ProductId = $ProductData['ProductId'];
$CheckStockProducts = StockProducts::where('StockId',$StockId)->where('ProductId',$ProductId)->get();
if(count($CheckStockProducts) > 0)
{
$cvstdata = array();
$cvstdata['CustomerId'] = $data['CustomerId'];
$cvstdata['ProductId'] = $ProductData['ProductId'];
$cvstdata['ProductName'] = $ProductData['ProductName'];
$cvstdata['ProductPrice'] = $ProductData['ProductPrice'];
$cvstdata['ProductQuantity'] = $ProductData['ProductQuantity'];
$cvstdata['Status'] = $ProductData['Status'];
$cvstdata['IsDeleted'] = $ProductData['IsDeleted'];
$cvstdata['Updated_by'] = $UserId;
$cvstdata['Updated_at'] = $CreatedDate;
$update = StockProducts::where('StockId',$StockId)->where('ProductId',$ProductId)->update($cvstdata);
}
else
{
$cvstdata = array();
$cvstdata['CustomerId'] = $data['CustomerId'];
$cvstdata['StockId'] = $StockId;
$cvstdata['ProductId'] = $ProductData['ProductId'];
$cvstdata['ProductName'] = $ProductData['ProductName'];
$cvstdata['ProductPrice'] = $ProductData['ProductPrice'];
$cvstdata['ProductQuantity'] = $ProductData['ProductQuantity'];
$cvstdata['UserId'] = $UserId;
$cvstdata['Status'] = '1';
$cvstdata['IsDeleted'] = '0';
$cvstdata['Updated_at'] = $CreatedDate;
$cvstdata['Created_at'] = $CreatedDate;
$cvstdata['Created_by'] = $UserId;
$cvstdata['Updated_by'] = $UserId;
$StockProductInsert = StockProducts::insert($cvstdata);
}
}
}
$Stocks = Stocks::select('*')
->where('StockId',$StockId)
->where('Status', '=', '1')
->where('IsDeleted', '=', 0)
->get()->first();
$Stocks = json_decode(json_encode($Stocks),true);
$CustomerId = isset($Stocks['CustomerId']) ? $Stocks['CustomerId'] : '';
$CustomerName = '';
if($CustomerId)
{ $Customers = Customer::where('CustomerId', $CustomerId)->get();
if ($Customers->count() > 0)
{ //email is not exist
$CustomerData = $Customers->first();
$CustomerName = $CustomerData->CustomerName;
}
}
if($CustomerName)
{
$Stocks['CustomerName'] = $CustomerName;
}
$Stocks['Products'] = array();
$StockProducts = StockProducts::where('StockId', $StockId)->where('Status', '=', '1')->where('IsDeleted', '=', 0)->get();
if ($StockProducts->count() > 0)
{ //email is not exist
$StockProductData = $StockProducts;
$Stocks['Products'] = $StockProductData;
}
if(count($Stocks['Products']) > 0) {
foreach ($Stocks['Products'] as $key => $StockProductValue)
{
$CustomerId = $StockProductValue->CustomerId;
$CustomerName='';
$custmer = Customer::where('CustomerId', $CustomerId)->get();
if ($custmer->count() > 0)
{ //email is not exist
$custmer = $custmer->first();
$CustomerName = $custmer->CustomerName;
}
$StockProductValue->CustomerName = $CustomerName;
}
}
$response = [
'Success' => true,
'ResponseMessage' => "Stock has been updated",
'ResponseCode' => "200",
'Stock' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
//getStock List
public function getStockList(Request $request, $StockId='') {
$data = $request->json()->all();
$UserId = $request->header('UserId');
$AuthToken = $request->header('AuthToken');
//check auth token
if($this->userLib->tokenChecker($request) == FALSE)
{
$response = [
'success' => false,
'ResponseMessage' => "Authentication Failed",
'ResponseCode' => "301",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
$Stocks = Stocks::select('*')
->leftJoin('tbl_customer', 'tbl_customer.CustomerId', '=', 'tbl_stocks.CustomerId')
->where('tbl_stocks.Status', '=', '1')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->where('tbl_stocks.IsDeleted', '=', '0')
->where('tbl_stocks.Created_by', '=', $UserId)
->orderBy('tbl_stocks.id', 'desc')
->get();
foreach ($Stocks as $key => $StocksValue)
{
$CustomerId = $StocksValue->CustomerId;
$StocksValue->CustomerName='';
$CustomerName='';
$custmer = Customer::where('CustomerId', $CustomerId)->get();
if ($custmer->count() > 0)
{ //email is not exist
$custmer = $custmer->first();
$CustomerName = $custmer->CustomerName;
}
$StocksValue->CustomerName = $CustomerName;
$CreatedFormatedDate = $StocksValue->Created_at;
$CreatedFormatedDate = date("j M Y, g:i a",strtotime($CreatedFormatedDate));
$StocksValue->CreatedFormatedDate = $CreatedFormatedDate;
}
$response = [
'Success' => true,
'ResponseMessage' => "Success",
'ResponseCode' => "200",
'Stocks' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
//getVisitsDetails
public function getStockDetails(Request $request, $StockId='') {
$data = $request->json()->all();
$UserId = $request->header('UserId');
$AuthToken = $request->header('AuthToken');
//check auth token
if($this->userLib->tokenChecker($request) == FALSE)
{
$response = [
'success' => false,
'ResponseMessage' => "Authentication Failed",
'ResponseCode' => "301",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
if($StockId == '')
{
$response = [
'success' => false,
'ResponseMessage' => "Please provide StockId",
'ResponseCode' => "305",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
$Stocks = Stocks::select('*')
->where('StockId',$StockId)
->where('Status', '=', '1')
->where('IsDeleted', '=', 0)
->orderBy('id', 'desc')
->get()->first();
if ($Stocks->count() == 0)
{
$response = [
'Success' => true,
'ResponseMessage' => "Data Not Found",
'ResponseCode' => "202",
'Stock' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
$CustomerId = $Stocks->CustomerId;
$CustomerName = '';
$Customers = Customer::where('CustomerId', $CustomerId)->get();
if ($Customers->count() > 0)
{ //email is not exist
$CustomerData = $Customers->first();
$CustomerName = $CustomerData->CustomerName;
}
$Stocks->CustomerName = $CustomerName;
$CreatedFormatedDate = $Stocks->Created_at;
$CreatedFormatedDate = date("j M Y, g:i a",strtotime($CreatedFormatedDate));
$Stocks->CreatedFormatedDate = $CreatedFormatedDate;
$StockProducts = StockProducts::where('StockId', $StockId)
->where('Status', '=', '1')
->where('IsDeleted', '=', 0)
// ->groupBy('Created_at')
->orderBy('Created_at', 'desc')
->get();
$Stocks->Products = array();
if ($StockProducts->count() > 0)
{ //email is not exist
$StockProductData = $StockProducts;
$Stocks->Products = $StockProductData;
}
foreach ($Stocks->Products as $key => $StockProductValue)
{
$CustomerId = $StockProductValue->CustomerId;
$CustomerName='';
$custmer = Customer::where('CustomerId', $CustomerId)->get();
if ($custmer->count() > 0)
{ //email is not exist
$custmer = $custmer->first();
$CustomerName = $custmer->CustomerName;
}
$StockProductValue->CustomerName = $CustomerName;
$CreatedFormatedDate = $StockProductValue->Created_at;
$CreatedFormatedDate = date("j M Y, g:i a",strtotime($CreatedFormatedDate));
$StockProductValue->CreatedFormatedDate = $CreatedFormatedDate;
}
$response = [
'Success' => true,
'ResponseMessage' => "Success",
'ResponseCode' => "200",
'Stock' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
function count_digit($number) {
return strlen($number);
}
function divider($number_of_digits)
{
$tens="1";
if($number_of_digits>8)
return 10000000;
while(($number_of_digits-1)>0)
{
$tens.="0";
$number_of_digits--;
}
return $tens;
}
//getStock List
public function getStockSearch(Request $request, $SearchText ='') {
$data = $request->json()->all();
$UserId = $request->header('UserId');
$AuthToken = $request->header('AuthToken');
//check auth token
if($this->userLib->tokenChecker($request) == FALSE)
{
$response = [
'success' => false,
'ResponseMessage' => "Authentication Failed",
'ResponseCode' => "301",
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
if($SearchText)
{
//DB::enableQueryLog(); // Enable query log
$Customers = Customer::select('*')
->where('CustomerName', 'iLIKE', '%'.$SearchText.'%')
->where('IsDeleted', '0')
->where('Status', '1')
->where('AssignTo', '=', $UserId)
->orderBy('id', 'desc')
->get();
//dd(DB::getQueryLog()); // Show results of log
$CustomerIds = array();
if($Customers->count() > 0)
{ //email is exist
foreach ($Customers as $key => $CValue)
{
$CustomerId = $CValue['CustomerId'];
$CustomerIds[] = $CustomerId;
}
}
$CustomerIds2 = implode(',',$CustomerIds);
// print_r($CustomerIds);
// DB::enableQueryLog(); // Enable query log
$Stocks = Stocks::select('*')
->where('IsDeleted', '=', 0)
->where('Created_by', '=', $UserId)
->whereIn('CustomerId', $CustomerIds)
->where('Status', '1')
//->orWhere('CollectionType', 'like', '%'.$SearchText.'%')
->orderBy('id', 'desc')
->get();
//dd(DB::getQueryLog()); // Show results of log
}
else
{
$Stocks = Stocks::select('*')
->where('IsDeleted', '=', 0)
->where('Status', '1')
->where('Created_by', '=', $UserId)
->orderBy('id', 'desc')
->get();
}
foreach ($Stocks as $key => $StocksValue)
{
$CustomerId = $StocksValue->CustomerId;
$StocksValue->CustomerName='';
$CustomerName='';
$custmer = Customer::where('CustomerId', $CustomerId)->get();
if ($custmer->count() > 0)
{ //email is not exist
$custmer = $custmer->first();
$CustomerName = $custmer->CustomerName;
}
$StocksValue->CustomerName = $CustomerName;
$CreatedFormatedDate = $StocksValue->Created_at;
$CreatedFormatedDate = date("j M Y, g:i a",strtotime($CreatedFormatedDate));
$StocksValue->CreatedFormatedDate = $CreatedFormatedDate;
}
$response = [
'Success' => true,
'ResponseMessage' => "Success",
'ResponseCode' => "200",
'Stocks' => $Stocks,
'ApiUrl' => env('APP_URL'),
'ApiVersion' => env('APP_VERSION'),
];
return response()->json($response);
}
}