File: /var/www/html/fieldsblaze-heroku/app/Http/Controllers/CommonController.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\Countries;
use App\Models\States;
use App\Models\Customer;
use App\Models\CustomerType;
use App\Models\BeatPlan;
use App\Models\PaymentTerm;
use App\Models\PriceBook;
use App\Models\PriceBookEntry;
use App\Models\ProductFamily;
use App\Models\Product;
use App\Models\Order;
use App\Models\UOM;
use App\Models\GST;
use App\Models\PaymentType;
use App\Models\ExpenseType;
use App\Models\ExpenseTypeTravel;
use App\Models\LeaveType;
use App\Models\OrderStatus;
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\DB;
class CommonController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function index()
{
}
public function countryList(Request $request)
{
// if (empty(session('UserId'))) {
// return redirect('/admin/logout');
// }
//TODO What is device_token
$data = $request->json()->all();
//Check Country is exist or not
$countries = DB::table('tbl_countries')->orderBy('country_name', 'asc')->get();
if ($countries->count() > 0) {
$response = [
'success' => true,
'response_message' => "Success",
'response_code' => "200",
'countries' => $countries,
];
return response()->json($response);
} else {
$response = [
'success' => false,
'response_message' => "data not found.",
'response_code' => "201",
];
return response()->json($response);
}
}
public function countryDetails(Request $request, $country_code)
{
// if (empty(session('UserId'))) {
// return redirect('/admin/logout');
// }
//TODO What is device_token
$data = $request->json()->all();
//Check Country is exist or not
$countries = DB::table('tbl_countries')->where('country_code', $country_code)->get()->first();
if ($countries) {
$response = [
'success' => true,
'response_message' => "Success",
'response_code' => "200",
'country' => $countries,
];
return response()->json($response);
} else {
$response = [
'success' => false,
'response_message' => "data not found.",
'response_code' => "201",
];
return response()->json($response);
}
}
//get state list of country
public function stateList(Request $request, $country_id)
{
//TODO What is device_token
$data = $request->json()->all();
if (!$country_id) {
$response = [
'success' => false,
'response_message' => "country id required.",
'response_code' => "202",
'country_id' => $country_id,
];
return response()->json($response);
}
$countries = DB::table('tbl_countries')->where('id', $country_id)->get()->first();
$country_code = $countries->country_code;
$country_name = $countries->country_name;
//Check Country is exist or not
$states = DB::table('tbl_states')->select('id', 'state_name')->where('country_id', $country_id)->orderBy('state_name', 'asc')->get();
if ($states->count() > 0) {
$response = [
'success' => true,
'response_message' => "Success",
'response_code' => "200",
'country_id' => $country_id,
'country_code' => $country_code,
'country_name' => $country_name,
'states' => $states,
];
return response()->json($response);
} else {
$response = [
'success' => false,
'response_message' => "data not found.",
'response_code' => "201",
];
return response()->json($response);
}
}
//get state list for Web
public function getCountry(Request $request)
{
// if (empty(session('UserId'))) {
// return redirect('/admin/logout');
// }
$countryid = $request->post('country_id');
$countries_data = Countries::orderBy('country_name', 'asc')->get();
echo "<option value=''>Please select state</option>";
foreach ($countries_data as $country) {
$country_id = $country->id;
$country_name = $country->country_name;
if ($country_id == '101' || $country_id == $countryid) {
echo "<option data-id='" . $country_id . "' value='" . $country_id . "' selected='selected'>" . $country_name . "</option>";
} else {
echo "<option data-id='" . $country_id . "' value='" . $country_id . "'>" . $country_name . "</option>";
}
}
}
public function getStates(Request $request)
{
// if (empty(session('UserId'))) {
// return redirect('/admin/logout');
// }
$country_id = $request->post('country_id');
$country_id = isset($country_id) ? $country_id : "101";
$statename = $request->post('state_name');
$state_data = States::where('country_id', $country_id)->orderBy('state_name', 'asc')->get();
echo "<option value=''>Please select state</option>";
foreach ($state_data as $state) {
$state_name = $state->state_name;
if ($state_name == $statename) {
echo "<option value='" . $state_name . "' selected='selected'>" . $state_name . "</option>";
} else {
echo "<option value='" . $state_name . "'>" . $state_name . "</option>";
}
}
}
### Customer List
public function CustomerList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
// $Customer = Customer::all()
// ->where('Status', '=', '1')
// ->where('IsDeleted', '=', '0');
// return response()->json([
// 'status' => 200,
// 'Customer' => $Customer,
// ]);
$Customer = Customer::select('tbl_customer.*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('CustomerName', 'ASC')
->get();
$Retailer = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', '7zbZseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k7')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
$Distributor = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', '4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
$WholeSeller = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', 'ozbVseyyTqvA6n7n5Bk01RvCm40KpPQa8M8k3')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'Customer' => $Customer,
'Retailer' => $Retailer,
'Distributor' => $Distributor,
'WholeSeller' => $WholeSeller,
]);
}
// ### Area Wise Customer
// public function areaCustomer($SalesAreaId){
// $AreaCustomer = Customer::all()
// ->where('SalesArea', $SalesAreaId)
// ->where('Status', '=', '1')
// ->where('IsDeleted', '=', '0')
// ->sortBy('CustomerName');
// return response()->json([
// 'status' => 200,
// 'AreaCustomer' => $AreaCustomer,
// ]);
// }
### Area Wise Distributor List
public function areaDistributorList($SalesAreaId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$AreaDistributor = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->where('SalesArea', $SalesAreaId)
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', '4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2') //DistributorID
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'AreaDistributor' => $AreaDistributor,
]);
}
### AssignTo Wise Distributor List
public function DistributorList($UserId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$Distributor = Customer::select('tbl_customer.*')
->where('AssignTo', $UserId)
->where('CustomerTypeId', '=','4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'Distributor' => $Distributor,
]);
}
### Distributor Wise Retailer List
public function distributorCustomerList($DistributorId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$Retailer = Customer::select('tbl_customer.*', 'tbl_sales_area.Name as SalesArea')
->where('DistributorId', $DistributorId)
->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'Retailer' => $Retailer,
]);
}
### Area Wise Customer List
public function areaCustomerList($SalesAreaId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$AreaCustomer = Customer::select('tbl_customer.*')
->where('SalesArea', $SalesAreaId)
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
$AreaRetailer = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->where('SalesArea', $SalesAreaId)
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', '7zbZseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k7') //RetailerID
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
$AreaDistributor = Customer::select('tbl_customer.*', 'tbl_customer_type.Name as CustomerType')
->where('SalesArea', $SalesAreaId)
->leftJoin('tbl_customer_type', 'tbl_customer.CustomerTypeId', '=', 'tbl_customer_type.CustomerTypeId')
->where('tbl_customer.CustomerTypeId', '=', '4zZVseyyTqvA6n7n5Bk01RvCm30KpPQa8M8k2') //DistributorID
->where('tbl_customer.Status', '=', '1')
->where('tbl_customer.IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'AreaCustomer' => $AreaCustomer,
'AreaRetailer' => $AreaRetailer,
'AreaDistributor' => $AreaDistributor,
]);
}
### AreaWise Customer List
public function AreaWiseCustomerList($UserId = '', $SalesAreaId = '')
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
if($SalesAreaId)
{
$AreaCustomer = Customer::select('tbl_customer.*')
->where('AssignTo', $UserId)
->where('SalesArea', $SalesAreaId)
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('tbl_customer.CustomerName', 'ASC')
->get();
return response()->json([
'status' => 200,
'Message' => 'Success',
'AreaCustomer' => $AreaCustomer,
]);
}
else
{
return response()->json([
'status' => 201,
'Message' => 'Salesareaid is required',
'AreaCustomer' => array(),
]);
}
}
### Order Holder List
public function OrderHolderList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
// $OrderHolder = Order::select('tbl_order.*', 'tbl_customer.CustomerName as CustomerName'
// )
// ->leftJoin('tbl_customer', 'tbl_customer.CustomerId', '=', 'tbl_order.CustomerId')
// ->where('tbl_order.Status', '=', '1')
// ->where('tbl_order.IsDeleted', '=', '0')
// ->get();
$OrderHolder = Customer::select('tbl_customer.*', 'tbl_order.CustomerId')
->leftJoin('tbl_order', 'tbl_order.CustomerId', '=', 'tbl_customer.CustomerId')
->where('tbl_order.Status', '=', '1')
->where('tbl_order.IsDeleted', '=', '0')
->OrderBy('CustomerName', 'ASC')
//->where('tbl_order.CountOrderItem', '>', '0')
->distinct()
->get();
return response()->json([
'status' => 200,
'OrderHolder' => $OrderHolder,
]);
}
### Customer Type List
public function CustomerType()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$CustomerType = CustomerType::select('tbl_customer_type.*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('Name', 'ASC')
->get();
return response()->json([
'status' => 200,
'CustomerType' => $CustomerType,
]);
}
### BeatPlan List
public function BeatPlanList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$BeatPlan = BeatPlan::select('tbl_beat_plan.*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('BeatPlanName', 'ASC')
->get();
return response()->json([
'status' => 200,
'BeatPlan' => $BeatPlan,
]);
}
### PaymentTerm List
public function PaymentTermList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$PaymentTerm = PaymentTerm::select('tbl_payment_terms.*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('Name', 'ASC')
->get();
return response()->json([
'status' => 200,
'PaymentTerm' => $PaymentTerm,
]);
}
### Price Book List
public function PriceList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$PriceBook = PriceBook::select('tbl_price_book.*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('PriceBookName', 'ASC')
->get();
return response()->json([
'status' => 200,
'PriceBook' => $PriceBook,
]);
}
### Price Book Entry List
public function PriceBookEntryList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$PriceBookEntry = PriceBookEntry::all()
->where('Status', '=', '1')
->where('IsDeleted', '=', '0');
return response()->json([
'status' => 200,
'PriceBookEntry' => $PriceBookEntry,
]);
}
### Price Book Entry List for Selected Product
public function PriceBookEntryProductList($ProductId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$PriceBookEntryProduct = PriceBookEntry::all()
->where('ProductId', '=', $ProductId)
->where('Status', '=', '1')
->where('IsDeleted', '=', '0');
return response()->json([
'status' => 200,
'PriceBookEntryProduct' => $PriceBookEntryProduct,
]);
}
### ProductFamily List
public function ProductFamilyList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$ProductFamily = ProductFamily::select('tbl_product_family.*')
->where('Status', '=', '1')
->orderBy('Name', 'ASC')
->get();
return response()->json([
'status' => 200,
'ProductFamily' => $ProductFamily,
]);
}
### Unit List
public function UnitList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$Unit = UOM::select('tbl_unit_of_measurement.*')
->where('Status', '=', '1')
->orderBy('MeasurementName', 'ASC')
->get();
return response()->json([
'status' => 200,
'Unit' => $Unit,
]);
}
### Order Status List
public function OrderStatusList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$OrderStatus = OrderStatus::all()
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->sortBy('OrderStatusTitle');
return response()->json([
'status' => 200,
'OrderStatus' => $OrderStatus,
]);
}
### GST List
public function GstList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$GstSlab = GST::all()
->where('Status', '=', '1');
return response()->json([
'status' => 200,
'GstSlab' => $GstSlab,
]);
}
### Product List
public function ProductList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$Product = Product::all()
->where('Status', '=', '1')
->where('IsDeleted', '=', '0');
return response()->json([
'status' => 200,
'Product' => $Product,
]);
}
// Product List for Selected Customer
public function CustomerProductList($CustomerId)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
// Get Customer PriceBook
$getCustomerPriceBookId = Customer::orderBy('Created_at', 'DESC')
->where('CustomerId', $CustomerId)
->get()
->first();
$PriceBookId = $getCustomerPriceBookId['PriceBookId'];
// Get Product of related PriceBookId
// $CustomerProduct = PriceBookEntry::select('tbl_price_book_entry.*', 'tbl_products.ProductId as ProductId')
// ->leftJoin('tbl_products', 'tbl_products.ProductId', '=', 'tbl_price_book_entry.CustomerId')
// ->where('.tbl_price_book_entry.PriceBookId', $PriceBookId)
// ->where('Status', '=', '1')
// ->where('IsDeleted', '=', '0');
// return response()->json([
// 'status' => 200,
// 'Product' => $CustomerProduct,
// ]);
// $CustomerProduct = PriceBookEntry::where('PriceBookId', $PriceBookId)
// ->where('Status', '=', '1')
// ->where('IsDeleted', '=', '0');
$PriceBookEntry = PriceBookEntry::select('tbl_price_book_entry.*', 'tbl_price_book.PriceBookName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by', 'tbl_products.*', 'tbl_product_family.Name as FamilyName')
->leftJoin('tbl_admin', 'tbl_price_book_entry.Created_by', '=', 'tbl_admin.UserId')
->leftJoin('tbl_price_book', 'tbl_price_book_entry.PriceBookId', '=', 'tbl_price_book.PriceBookId')
->leftJoin('tbl_products', 'tbl_price_book_entry.ProductId', '=', 'tbl_products.ProductId')
->leftJoin('tbl_product_family', 'tbl_products.Family', '=', 'tbl_product_family.ProductFamilyId')
->where('tbl_price_book.IsDeleted', '=', '0')
->where('tbl_price_book.Status', '=', '1')
->where('tbl_price_book_entry.IsDeleted', '=', '0')
->where('tbl_price_book_entry.Status', '=', '1')
->where('tbl_products.IsDeleted', '=', '0')
->where('tbl_products.Status', '=', '1')
->where('tbl_price_book_entry.PriceBookId', '=', $PriceBookId)
->get();
if ($PriceBookEntry->count() > 0)
{
foreach ($PriceBookEntry as $key => $ProductValue)
{
$ProductValue['CustomerId'] = $CustomerId;
$Unit = $ProductValue['Unit'];
$MeasurementName = '';
$UnitData = UOM::where('UnitId', $Unit)->get();
if ($UnitData->count() > 0)
{
$UnitDt = $UnitData->first();
$MeasurementName = $UnitDt->MeasurementName;
}
$ProductValue['UnitName'] = $MeasurementName;
}
}
return response()->json([
'status' => 200,
'PriceBookEntry' => $PriceBookEntry,
]);
}
### PaymentType List
public function PaymentType()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$PaymentType = PaymentType::all()
->where('Status', '=', '1');
return response()->json([
'status' => 200,
'PaymentType' => $PaymentType,
]);
}
### User List
public function UserList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$UserList = User::select('tbl_user.*')
->where('Status', '=', 'Activated')
->where('isActive', '=', '1')
->where('FirstName', '!=', '')
->orderBy('FirstName', 'ASC')
->get();
return response()->json([
'status' => 200,
'User' => $UserList,
]);
}
### User Manager List
public function UserManagerList($SelectedUserId='')
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
if($SelectedUserId)
{
$UserList = User::select( 'tbl_user.UserId', 'tbl_user.FirstName', 'tbl_user.LastName', 'tbl_user.Email','tbl_user.Mobile', 'tbl_profiles.ProfileName as ProfileName')
->LeftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
//->where('tbl_user.Status', '=', 'Activated')
->where('tbl_user.isActive', '=', '1')
->where('tbl_user.FirstName', '!=', '')
->where('tbl_user.UserId', '!=', $SelectedUserId)
//->where('tbl_profiles.ProfileName', 'NOT LIKE', '%Executive%')
//->where('tbl_profiles.ProfileName', 'NOT LIKE', '%Admin%')
->orderBy('tbl_user.FirstName', 'ASC')
->get();
}
else
{
$UserList = User::select( 'tbl_user.UserId', 'tbl_user.FirstName', 'tbl_user.LastName', 'tbl_user.Email','tbl_user.Mobile', 'tbl_profiles.ProfileName as ProfileName')
->LeftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
//->where('tbl_user.Status', '=', 'Activated')
->where('tbl_user.isActive', '=', '1')
->where('tbl_user.FirstName', '!=', '')
->where('tbl_user.FirstName', '!=', '')
//->where('tbl_profiles.ProfileName', 'NOT LIKE', '%Executive%')
//->where('tbl_profiles.ProfileName', 'NOT LIKE', '%Admin%')
->orderBy('tbl_user.FirstName', 'ASC')
->get();
}
return response()->json([
'status' => 200,
'User' => $UserList,
]);
}
### ExpenseType List
public function ExpenseTypeList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$ExpenseType = ExpenseType::select('*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->orderBy('id','asc')
->get();
return response()->json([
'status' => 200,
'ExpenseType' => $ExpenseType,
]);
}
### Expense Travel Type List
public function ExpenseTypeTravelList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$ExpenseTypeTravel = ExpenseTypeTravel::select('*')
->where('Status', '=', '1')
->where('IsDeleted', '=', '0')
->get();
return response()->json([
'status' => 200,
'ExpenseTypeTravel' => $ExpenseTypeTravel,
]);
}
### User Km Rate List for expense modal
public function UserKmRateList($id)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$RateList = User::all()
->where('UserId', '=', $id);
// ->get()->first();
return response()->json([
'status' => 200,
'RateList' => $RateList,
]);
}
### Order List for selected customer
public function orderList($id)
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$OrderList = Order::all()
->where('CustomerId', '=', $id)
->where('IsDeleted', '=', '0');
// ->get()->first();
return response()->json([
'status' => 200,
'OrderList' => $OrderList,
]);
}
### Leave Type List
public function leaveTypeList()
{
if (empty(session('UserId'))) {
return redirect('/admin/logout');
}
$LeaveType = LeaveType::all()
->where('Status', '=', '1')
->where('IsDeleted', '=', '0');
return response()->json([
'status' => 200,
'LeaveType' => $LeaveType,
]);
}
}