/
home
/
sjslayjy
/
public_html
/
mosaram
/
app
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Carbon\Carbon; use Session; class VoiceSearchController extends Controller { public function __construct(){ \DB::statement("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));"); } public function microphone(){ return view('dashboard.microphone.microphone'); } // public function handleCommand(Request $request) // { // $command = strtolower($request->input('command')); // if (strpos($command, 'generate the total member report') !== false || // strpos($command, 'generate the total member') !== false || // strpos($command, 'show me the total member') !== false) { // try { // // Fetch user data including roles // $members = DB::table('users') // ->join('roles', 'users.role_id', '=', 'roles.id') // ->select( // 'users.id', // 'users.name', // 'users.email', // 'roles.role as role_name', // Correctly selecting role from roles table // 'users.is_active as status' // Correctly selecting is_active // ) // ->where('users.is_active', 1) // Filtering active users // ->get(); // // Return the report as JSON // return response()->json(['report' => $members]); // } catch (\Exception $e) { // // Log the error and return a friendly message // \Log::error('Error fetching user data: ' . $e->getMessage()); // return response()->json(['message' => 'An error occurred while processing your command.']); // } // } // return response()->json(['message' => 'Command not recognized']); // } public function getTotalMembers() { // Query to get total active members with their roles $members = DB::table('users') ->join('roles', 'users.role_id', '=', 'roles.id') ->select( 'users.id', 'users.name', 'users.email', 'roles.role as role_name', // Role name from roles table 'users.is_active as status' // Active status ) ->where('users.is_active', 1) // Only active users ->get(); // Return the data as JSON return response()->json(['report' => $members]); } public function getStockData(Request $request) { $voiceCommand = $request->input('command'); $keywords = explode(' ', $voiceCommand); $inventoryData = []; foreach ($keywords as $names) { $inventories = DB::table('inventories') ->join('products', 'inventories.product_id', '=', 'products.id') ->join('warehouses', 'warehouses.id', '=', 'inventories.warehouse_id') ->where('products.name', 'like', $names . '%') ->whereDate('inventories.created_at', '>=', Carbon::now()->subDays(90)) ->get(); foreach ($inventories as $value) { $dealer = DB::table('dealers')->find($value->dealer_id); $warehouse = DB::table('warehouses')->find($value->warehouse_id); $productName = getModelById('Product', $value->product_id)->name; $inventoryData[] = [ 'dealer' => $dealer ? $dealer->name : 'NA', 'product' => $productName, 'warehouse' => $warehouse ? $warehouse->name : 'NA', 'quantity' => $value->quantity, ]; } } // Return the data as JSON return response()->json(['stock' => $inventoryData, 'total' => count($inventoryData)]); } public function getSaleData(Request $request) { $voiceCommand = $request->input('command'); $keywords = explode(' ', $voiceCommand); $data = []; $no = 0; foreach ($keywords as $name) { $loadingSlipInvoices = DB::table('loading_slip_invoices') ->join('products', 'loading_slip_invoices.product_id', '=', 'products.id') ->where('products.name', 'like', $name . '%') ->whereDate('loading_slip_invoices.created_at', '>=', Carbon::now()->subDays(90)) ->get(); foreach ($loadingSlipInvoices as $invoice) { $dealer = DB::table('dealers')->find($invoice->dealer_id); $dealerName = $dealer ? $dealer->name : 'NA'; $productName = getModelById('Product', $invoice->product_id)->name; $data[] = [ 'dealer' => htmlspecialchars($dealerName), 'product' => htmlspecialchars($productName), 'invoice_type' => htmlspecialchars($invoice->invoice_type), 'invoice_number' => htmlspecialchars($invoice->invoice_number), 'quantity' => htmlspecialchars($invoice->quantity), 'rate' => htmlspecialchars($invoice->rate), 'freight_discount' => htmlspecialchars($invoice->freight_discount) ]; $no++; } } return response()->json(['data' => $data, 'total' => $no]); } public function getTotalMembersMccWise(Request $request) { $voice_text = $request->voice_text; $data = array(); $voiceTextArr[1] = 'mcc wise'; $voiceTextArr[2] = 'mcc wise members'; $voiceTextArr[3] = 'mcc wise member'; $voiceTextArr[4] = 'total mcc member'; $voiceTextArr[5] = 'total mcc members'; $is_found = 0; foreach($voiceTextArr as $key=>$text) { if($text && str_contains(strtolower($voice_text), $text)){ $is_found = 1; break; } } if($is_found) { $mccwise_count = \DB::table('membership_details')->select(\DB::raw('count(mcc_code) as member_count'), 'mcc_name', 'mcc_code')->groupBy('mcc_code')->get(); if(!$mccwise_count->isEmpty()) { foreach($mccwise_count as $key=>$value) { $data[$key]['mcc_name'] = $value->mcc_name; $data[$key]['mcc_code'] = $value->mcc_code; $data[$key]['member_count'] = $value->member_count; } $response['status'] = 1; $response['mcc_wise_count'] = $data; } else { $response['status'] = 0; $response['mcc_wise_count'] = 'No member.'; } } else { $response['status'] = 2; $response['mcc_wise_count'] = 'No member.'; } return response()->json($response); } // public function getTotalMembers(Request $requset) { // $voice_data = $request->voice_data; // $keywords = explode(' ', $voice_data); // $html = ""; // $no=0; // $keywords = explode(' ', $data); // foreach($keywords as $names){ // } // $html .= '<table style="font-size:14px;" class="table table-striped table-bordered table-hover">'; // foreach($keywords as $names){ // $products = DB::table('products')->select('products.id','products.name')->where('products.name', 'like',$names.'%')->get(); // if(count($products)>0){ // foreach($products as $value){ // $tokens= \App\Token::where('quantity', '!=', 0)->where('product_id',$value->id)->whereDate('created_at','>=', Carbon::now()->subDays(90))->get(); // foreach($tokens as $token){ // $str="Token $token->unique_id "; // if($token->master_rake_id==null){ // $str.=getModelById('Warehouse',$token->from_warehouse_id)->name; // } // else // { // $str.=getModelById('MasterRake',$token->master_rake_id)->name; // } // if($token->to_type == 1){ // $str.= "<b>Warehouse </b> ".getModelById('Warehouse',$token->warehouse_id)->name."(".getModelById('Warehouse',$token->warehouse_id)->location.")"; // } // elseif($token->to_type == 3){ // $str.= "<b>Dealer </b> ".getModelById('Dealer',$token->dealer_id)->name."(".getModelById('Dealer',$token->dealer_id)->address1.")"; // } // $i = 0; // $html .= '<tr>'; // $html .= '<td>'.$str.'</td>'; // $html .= '</tr>'; // $i++; // $no++; // } // } // } // } // $html .= '</table>'; // echo '<h2 style="font-size:15px;font-weight:bold;">Token(<span id="token1">'.$no.'</span>)</h2>'.$html; // } public function reportIndex(){ return view('voice.index'); } public function directLabourPaymentReportVoice(Request $request){ $data = array(); $labour_payments = \App\DirectLabourPayment::get(); $data['direct_labour_payments'] = $labour_payments; return view('voice.index',$data); } public function totalUserReportVoice() { $users = \App\User::where('is_active', 1) ->where('id', '<>', \Auth::user()->id) ->with('role') ->get(); $data = ['users' => $users, 'totalUserCount' => $users->count()]; return view('voice.index', $data); } public function labourPaymentReportVoice(Request $request) { $data = []; $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); $command = $request->rake_name; $search_phrase = "labour payment report of master rake"; $master_rake_name = trim(str_replace($search_phrase, "", $command)); if (!empty($master_rake_name)) { $matched_rake = \App\MasterRake::where('name', 'LIKE', '%' . $master_rake_name . '%')->first(); if ($matched_rake) { $labour_payments = \App\LabourPayments::where('master_rake_id', $matched_rake->id) ->with('token') ->get(); $data['labour_payments'] = $labour_payments; $data['master_rake_id'] = $matched_rake->id; $data['message'] = "Labour payment report for master rake: $matched_rake->name"; $unique_labour_data = $labour_payments->map(function ($labour_payment) { return [ 'labour_name' => strtolower($labour_payment->labour_name), 'product_name' => $labour_payment->product_name, 'quantity' => $labour_payment->quantity, 'unit_name' => $labour_payment->unit_name, 'paid_amount' => $labour_payment->paid_amount, ]; }); $unique_labour_data = $unique_labour_data->unique(function ($item) { return $item['labour_name'] . $item['product_name'] . $item['quantity'] . $item['unit_name'] . $item['paid_amount']; }); $total_quantity = $labour_payments->sum('quantity'); $total_paid_amount = $labour_payments->sum('paid_amount'); $unique_labour_names_count = $unique_labour_data->pluck('labour_name')->unique()->count(); $unique_product_names = $unique_labour_data->pluck('product_name')->unique()->implode(', '); $data['unique_labour_data'] = $unique_labour_data; $data['unique_labour_names_count'] = $unique_labour_names_count; $data['unique_product_names'] = $unique_product_names; $data['total_quantity'] = $total_quantity; $data['total_paid_amount'] = $total_paid_amount; } else { $data['labour_payments'] = []; $data['message'] = "No master rake found matching: $master_rake_name"; } } else { $data['labour_payments'] = []; $data['message'] = "Master rake name is missing in the command."; } return view('voice.index', $data); } public function rakeFinancialReportVoice(Request $request) { $data = array(); $data['master_rakes'] =\App\MasterRake::where('is_active',1)->get(); if ($request->isMethod('post')){ $validator = \Validator::make($request->all(), array( 'master_rake_id' =>'required', ) ); if($validator->fails()){ return redirect('user/rake-report') ->withErrors($validator) ->withInput(); } else{ $data['rake'] =\App\MasterRake::where('is_active',1)->where('id',$request->master_rake_id)->with('master_rake_products')->first(); $data['master_rake_id'] = $request->master_rake_id; $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->sum('amount'); $data['direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->get(); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->sum('paid_amount'); $data['wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->get(); $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id',$request->master_rake_id)->where('is_freight_paid',1)->sum('freight_paid_amount'); $data['total_tokens'] = \App\Token::where('master_rake_id',$request->master_rake_id)->count('id'); $data['total_loadings'] = \App\ProductLoading::where('master_rake_id',$request->master_rake_id)->count('id'); $data['freight_payments'] = \App\ProductLoading::where('master_rake_id',$request->master_rake_id)->where('is_freight_paid',1)->get(); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->sum('paid_amount'); $data['labour_payments'] = \App\LabourPayments::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->get(); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->sum('paid_amount'); $data['unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id',$request->master_rake_id)->where('is_paid',1)->get(); } } return view('voice.index',$data); } public function totalStockReportVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me total stock of report of"; $productName = str_replace($baseCommand, '', strtolower($voiceCommand)); $productName = trim($productName); // Clean up any extra spaces if (empty($productName)) { return view('voice.index', ['stock' => [], 'total' => 0]) ->with('error', 'Please specify a valid product name.'); } $inventories = DB::table('inventories') ->join('products', 'inventories.product_id', '=', 'products.id') ->join('warehouses', 'warehouses.id', '=', 'inventories.warehouse_id') ->join('dealers', 'dealers.id', '=', 'inventories.dealer_id') ->select( 'dealers.name as dealer_name', 'products.name as product_name', 'warehouses.name as warehouse_name', 'inventories.quantity', 'inventories.created_at' ) ->where('products.name', 'like', '%' . $productName . '%') ->get(); $inventoryData = $inventories->map(function ($value) { return [ 'dealer' => $value->dealer_name ?? 'NA', 'product' => $value->product_name ?? 'NA', 'warehouse' => $value->warehouse_name ?? 'NA', 'quantity' => $value->quantity ?? 0, ]; })->toArray(); $totalStockQuantity = $inventories->sum('quantity'); return view('voice.index', ['stock' => $inventoryData, 'total' => count($inventoryData), 'total_stock_quantity' => $totalStockQuantity]); } public function totalSaleReportDataVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me total sale of"; $productName = str_replace($baseCommand, '', strtolower($voiceCommand)); $productName = trim($productName); if (empty($productName)) { return view('voice.index', ['saleData' => [], 'total' => 0]) ->with('error', 'Please specify a valid product name.'); } $loadingSlipInvoices = DB::table('loading_slip_invoices') ->join('products', 'loading_slip_invoices.product_id', '=', 'products.id') ->join('dealers', 'loading_slip_invoices.dealer_id', '=', 'dealers.id') ->select( 'dealers.name as dealer_name', 'products.name as product_name', 'loading_slip_invoices.invoice_type', 'loading_slip_invoices.invoice_number', 'loading_slip_invoices.quantity', 'loading_slip_invoices.rate', 'loading_slip_invoices.freight_discount' ) ->where('products.name', 'like', '%' . $productName . '%') ->get(); $saleData = $loadingSlipInvoices->map(function ($value) { return [ 'dealer' => $value->dealer_name ?? 'NA', 'product' => $value->product_name ?? 'NA', 'invoice_type' => $value->invoice_type ?? 'NA', 'invoice_number' => $value->invoice_number ?? 'NA', 'quantity' => $value->quantity ?? 0, 'rate' => $value->rate ?? 0, 'freight_discount' => $value->freight_discount ?? 0, ]; })->toArray(); $totalQuantity = $loadingSlipInvoices->sum('quantity'); $totalSaleAmount = $loadingSlipInvoices->reduce(function ($carry, $item) { return $carry + ($item->rate * $item->quantity); }, 0); return view('voice.index', ['saleData' => $saleData, 'total' => count($saleData),'total_sale_quantity' => $totalSaleAmount, 'total_amount' => $totalQuantity]); } public function totalPurchaseReportDataVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me total purchase of"; $productName = str_replace($baseCommand, '', strtolower($voiceCommand)); $productName = trim($productName); if (empty($productName)) { return view('voice.index', ['purchaseData' => [], 'total' => 0]) ->with('error', 'Please specify a valid product name.'); } $products = DB::table('products') ->select('id', 'name') ->where('name', 'like', '%' . $productName . '%') ->get(); $purchaseData = []; foreach ($products as $product) { $totalQuantity = \App\CompanyDi::where('product_id', $product->id)->sum('quantity'); $totalAmount = \App\CompanyDi::where('product_id', $product->id)->sum('total'); $purchaseData[] = [ 'product' => $product->name ?? 'NA', 'quantity' => $totalQuantity ?? 0, 'amount' => $totalAmount ?? 0, ]; } return view('voice.index', ['purchaseData' => $purchaseData, 'total' => count($purchaseData)]); } public function totalInvoiceReportDataVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me total invoice of"; $productName = str_replace($baseCommand, '', strtolower($voiceCommand)); $productName = trim($productName); if (empty($productName)) { return view('voice.index', ['invoices' => [], 'total' => 0]) ->with('error', 'Please specify a valid product name.'); } $keywords = explode(' ', $productName); $invoiceData = []; $invoiceCount = 0; $totalInvoiceAmount = 0; foreach ($keywords as $keyword) { $products = DB::table('products') ->select('id', 'name') ->where('name', 'like', $keyword . '%') ->get(); if (count($products) > 0) { foreach ($products as $product) { $invoices = \App\LoadingSlipInvoice::with('product_loading') ->where('product_id', $product->id) ->get(); foreach ($invoices as $invoice) { // Add invoice data to the array $invoiceData[] = [ 'invoice_number' => $invoice->invoice_number, 'retailer_name' => $invoice->retailer_name, 'amount' => $invoice->total, ]; $invoiceCount++; $totalInvoiceAmount += $invoice->total; } } } } return view('voice.index', ['invoices' => $invoiceData, 'total' => $invoiceCount,'total_amount' => $totalInvoiceAmount,]); } public function totalPieChartDataVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me pie chart data of"; $productName = str_replace($baseCommand, '', strtolower($voiceCommand)); $productName = trim($productName); if (empty($productName)) { return response()->json(['error' => 'Please specify a valid product name.']); } $keywords = explode(' ', $productName); $response = []; // Loop through each keyword and find matching inventories, invoices, and other data foreach ($keywords as $keyword) { // Fetch inventories related to the product within the last 90 days $inventories = DB::table('inventories') ->join('products', 'inventories.product_id', '=', 'products.id') ->join('warehouses', 'warehouses.id', '=', 'inventories.warehouse_id') ->where('products.name', 'like', $keyword . '%') ->get(); // Fetch loading slip invoices related to the product within the last 90 days $loadingSlipInvoices = DB::table('loading_slip_invoices') ->join('products', 'loading_slip_invoices.product_id', '=', 'products.id') ->where('products.name', 'like', $keyword . '%') ->get(); // Fetch products related to the keyword $products = DB::table('products') ->select('id', 'name') ->where('name', 'like', $keyword . '%') ->get(); foreach ($products as $product) { // Fetch purchases for the product within the last 90 days $purchases = \App\CompanyDi::where('product_id', $product->id) ->sum('quantity'); $purchasesCount = \App\CompanyDi::where('product_id', $product->id) ->get(); // Fetch tokens related to the product within the last 90 days $tokens = \App\Token::where('quantity', '!=', 0) ->where('product_id', $product->id) ->get(); // Fetch invoices related to the product within the last 90 days $invoices = \App\LoadingSlipInvoice::with('product_loading') ->where('product_id', $product->id) ->get(); $invoices_total_amount = $invoices->sum('total'); $token_total_amount = $tokens->sum('quantity'); $stock_total_amount = $inventories->sum('quantity'); $sale_total_amount = $loadingSlipInvoices->sum('total'); // Set purchase data $purchaseT = ($purchases == 0) ? 0 : 1; $response['stockTotal'] = count($inventories); $response['saleTotal'] = count($loadingSlipInvoices); $response['purchaseTotal'] = count($purchasesCount); $response['purchaseAmountTotal'] = $purchases; $response['tokenTotal'] = count($tokens); $response['invoiceTotal'] = count($invoices); $response['invoiceTotalAmount'] = $invoices_total_amount; $response['invoiceTokanAmount'] = $token_total_amount; $response['totalStockAmount'] = $stock_total_amount; $response['totalSaleAmount'] = $sale_total_amount; break; } } return view('voice.index', ['response' => $response]); } public function userErrorLogsVoice(Request $request){ $data = array(); $data['modules'] = array_unique(\App\UserErrorLog::pluck('module')->toArray()); $data['logs'] = \App\UserErrorLog::all(); $data['users'] = \App\User::where('role_id','!=',1)->where('is_active',1)->get(); if ($request->isMethod('post')){ $query = \App\UserErrorLog::query(); if($request->user_id){ $query->where('user_id',$request->user_id); $data['user_id'] = $request->user_id; } if($request->module){ $query->where('module',$request->module); $data['current_module'] = $request->module; } $data['logs'] = $query->get(); } return view('voice.index',$data); } public function ErrorLogsVoice(Request $request) { $voiceCommand = $request->input('command'); $baseCommand = "show me log details of"; $userName = str_replace($baseCommand, '', strtolower($voiceCommand)); $userName = trim($userName); $user = \App\User::where('name', 'like', '%' . $userName . '%')->first(); if (!$user) { return view('log.user-error-logs', [ 'logs' => [], 'modules' => \App\UserErrorLog::distinct()->pluck('module')->toArray(), 'users' => \App\User::where('role_id', '!=', 1)->where('is_active', 1)->get(), ])->with('error', "No logs found for the user '{$userName}'! Please provide a valid user name."); } $logs = \App\UserErrorLog::where('user_id', $user->id)->get(); $modules = \App\UserErrorLog::distinct()->pluck('module')->toArray(); $users = \App\User::where('role_id', '!=', 1)->where('is_active', 1)->get(); return view('voice.index', [ 'logs' => $logs, 'modules' => $modules, 'users' => $users, 'current_user' => $user->name, ]); } public function getUserStatusVoice(){ $data = array(); $ids = \App\TrackUser::select(DB::raw('max(id) as id'))->groupBy('user_id')->get(); $data['user_status'] = \App\TrackUser::whereIn('id', $ids)->get(); $low_battery_users = \App\TrackUser::where('batterylevel', '<', 20) ->pluck('user_id'); $data['low_battery_users'] = \App\User::whereIn('id', $low_battery_users)->pluck('name'); $online_users = \App\TrackUser::where('status', 'online') ->pluck('user_id'); $data['online_users'] = \App\User::whereIn('id', $online_users)->pluck('name'); $data['total_users_count'] = \App\User::count(); return view('voice.index', $data); } public function attendanceChartVoice(){ $data['users'] = \App\Location::groupBy('user_id')->get(); $data['month'] = ""; $data['year'] = ""; return view('voice.index',$data); } public function warehouseTransferLoadingsVoice(Request $request) { $data = []; $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $voiceCommand = $request->input('command'); $baseCommand = "show me warehouse transfer of"; if (!empty($voiceCommand) && str_starts_with(strtolower($voiceCommand), $baseCommand)) { $productName = trim(str_replace($baseCommand, '', strtolower($voiceCommand))); if (!empty($productName)) { $product = \App\Product::where('name', 'like', '%' . $productName . '%')->first(); if ($product) { $data['warehouse_transfer_loadings'] = \App\WarehouseTransferLoading::where('product_id', $product->id) ->with([ 'product:id,name', 'unit:id,unit', 'from_warehouse:id,name', 'to_warehouse:id,name', 'transporter:id,name', 'product_brand:id,name,brand_name', ]) ->get(); } else { $data['warehouse_transfer_loadings'] = []; $data['error'] = "No records found for product '{$productName}'. Please provide a valid product name."; } } } else { $data['warehouse_transfer_loadings'] = \App\WarehouseTransferLoading::with([ 'product:id,name', 'unit:id,unit', 'from_warehouse:id,name', 'to_warehouse:id,name', 'transporter:id,name', 'product_brand:id,name,brand_name', ])->get(); } return view('voice.index', $data); } public function indexVoice(Request $request) { $data = []; $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $voiceCommand = $request->input('command'); $baseCommand = "show me restore of"; if (!empty($voiceCommand) && str_starts_with(strtolower($voiceCommand), $baseCommand)) { // Extract product name from the command $productName = trim(str_replace($baseCommand, '', strtolower($voiceCommand))); if (!empty($productName)) { $product = \App\Product::where('name', 'like', '%' . $productName . '%')->first(); if ($product) { $data['returned_products'] = \App\ReturnedProduct::where('product_id', $product->id) ->with([ 'product:id,name', 'unit:id,unit', 'warehouse:id,name', 'transporter:id,name', 'product_brand:id,name,brand_name', ]) ->orderBy('created_at', 'desc') ->get(); } else { $data['returned_products'] = []; $data['error'] = "No restore records found for product '{$productName}'. Please provide a valid product name."; } } } else { // Default empty data when no specific voice command is provided $data['returned_products'] = []; } return view('voice.index', $data); } public function labourPaymentsVoice(Request $request) { $data = []; $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $voiceCommand = $request->input('command'); $baseCommand = "show me standard labour payments of"; if (!empty($voiceCommand) && str_starts_with(strtolower($voiceCommand), $baseCommand)) { // Extract warehouse name from the command $warehouseName = trim(str_replace($baseCommand, '', strtolower($voiceCommand))); if (!empty($warehouseName)) { $warehouse = \App\Warehouse::where('name', 'like', '%' . $warehouseName . '%')->first(); if ($warehouse) { $data['standardizations'] = \App\Standardization::where('warehouse_id', $warehouse->id) ->with('warehouse:id,name') ->get(); } else { $data['standardizations'] = []; $data['error'] = "No standard labour payments found for warehouse '{$warehouseName}'. Please provide a valid warehouse name."; } } } else { $data['standardizations'] = \App\Standardization::whereDate('created_at', '=', date('Y-m-d')) ->with('warehouse:id,name') ->get(); } return view('voice.index', $data); } public function rakePaymentReportVoice(Request $request) { $command = strtolower($request->input('command')); $data = []; if ($command && strpos($command, 'show me rake payment of') !== false) { // Extract the rake details from the command $rakeDetails = str_replace('show me rake payment of', '', $command); $rakeDetails = trim($rakeDetails); if ($rakeDetails) { // Process rake details and fetch data list($rakeName, $rakeDate, $fiscalYear, $additionalInfo) = array_pad(explode('/', $rakeDetails), 4, null); $rake = \App\MasterRake::where('name', $rakeName) ->whereDate('date', $rakeDate) ->first(); if ($rake) { $data['rake_name'] = $rakeName; $data['rake_date'] = $rakeDate; $data['fiscal_year'] = $fiscalYear; $data['additional_info'] = $additionalInfo; // Calculate payments $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $rake->id)->sum('freight_paid_amount'); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $rake->id)->sum('paid_amount'); $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $rake->id)->sum('amount'); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $rake->id)->sum('paid_amount'); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $rake->id)->sum('paid_amount'); // Total payments $data['total_payments'] = $data['total_freight_payments'] + $data['total_labour_payments'] + $data['total_direct_labour_payments'] + $data['total_wagon_unloadings'] + $data['total_unloading_labour_payments']; } else { $data['error'] = 'Rake details not found!'; } } else { $data['error'] = 'Invalid command or missing rake details!'; } } else { $data['error'] = 'Invalid or missing command!'; } return view('voice.index', compact('data')); } public function masterRakesVoice() { $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); $data['master_rakes_count'] = $data['master_rakes']->count(); $data['sessions'] = \App\Session::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['rake_points'] = \App\RakePoint::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); return view('voice.index', $data); } public function masterRakeSummaryVoice(Request $request) { $data = []; $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); $matchedRakes = null; if ($request->isMethod('get')) { $command = strtolower($request->input('command', '')); if ($command && strpos($command, 'show me rake summary of') !== false) { $rakeName = trim(str_ireplace('show me rake summary of', '', $command)); if (!empty($rakeName)) { $matchedRakes = \App\MasterRake::where('name', 'LIKE', '%' . $rakeName . '%') ->where('is_active', 1) ->first(); if (!$matchedRakes) { $data['error'] = 'No rakes found matching the name: ' . $rakeName; } else { $data['rake_name'] = $rakeName; $data['master_rakes'] = [$matchedRakes]; } } else { $data['error'] = 'Invalid command or missing rake name!'; } } $masterRakeId = $matchedRakes->id; if (isset($request->product_id)) { $allotments = \App\RakeProductAllotment::where('master_rake_id', $masterRakeId) ->where('product_id', $request->product_id) ->get(); } else { $allotments = \App\RakeProductAllotment::where('master_rake_id', $masterRakeId)->get(); } if (!is_null($allotments)) { $dates = []; if (isset($request->product_id)) { $product_loadings = \App\ProductLoading::where('master_rake_id', $masterRakeId) ->where('product_id', $request->product_id) ->get(); $data['product_id'] = $request->product_id; } else { $product_loadings = \App\ProductLoading::where('master_rake_id', $masterRakeId)->get(); } if (!is_null($product_loadings)) { foreach ($product_loadings as $product_loading) { array_push($dates, date('Y-m-d', strtotime($product_loading->created_at))); } $data['date_range'] = array_unique($dates); } $data['master_rake_products'] = \App\MasterRakeProduct::where('master_rake_id', $masterRakeId)->get(); } else { $data['date_range'] = []; } if (isset($request->product_id)) { $warehouse_allotments = \App\ProductLoading::where('master_rake_id', $masterRakeId) ->where('loading_slip_type', 2) ->distinct() ->select('warehouse_id') ->get(); } else { $warehouse_allotments = \App\ProductLoading::where('master_rake_id', $masterRakeId) ->where('loading_slip_type', 2) ->distinct() ->select('warehouse_id') ->get(); } $data['allotments'] = $allotments; $data['warehouse_allotments'] = $warehouse_allotments; $data['master_rake_id'] = $masterRakeId; $data['current_master_rake'] = \App\MasterRake::where('id', $masterRakeId)->first(); } else { $data['from_date'] = date('m/d/Y'); $data['to_date'] = date('m/d/Y'); } return view('voice.index', $data); } public function generatedWarehouseTokensVoice(Request $request) { $data = []; $data['acting_company'] = Session::get('acting_company'); $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $command = strtolower($request->input('command', '')); $warehouseId = null; if ($command && strpos($command, 'show me total generated token for the warehouse of') !== false) { $warehouseName = trim(str_ireplace('show me total generated token for the warehouse of', '', $command)); $matchedWarehouse = \App\Warehouse::where('name', 'LIKE', '%' . $warehouseName . '%') ->where('is_active', 1) ->first(); if ($matchedWarehouse) { $warehouseId = $matchedWarehouse->id; $data['warehouse_name'] = $matchedWarehouse->name; } else { $data['error'] = 'No warehouse found with the name: ' . $warehouseName; } } if ($warehouseId) { $tokens = \App\Token::where('is_active', 1) ->where('from_warehouse_id', $warehouseId) ->get(); $totalQuantity = $tokens->sum('quantity'); $totalRemainingQuantity = $tokens->sum(function ($token) { return $token->quantity - totalTokenLoading($token->id); }); $data['tokens'] = $tokens; $data['total_quantity'] = $totalQuantity; $data['total_remaining_quantity'] = $totalRemainingQuantity; } else { $data['tokens'] = []; $data['total_quantity'] = 0; $data['total_remaining_quantity'] = 0; } return view('voice.index', $data); } public function directLabourPaymentWarehouseVoice(Request $request) { // Extract the warehouse name from the command $command = $request->input('command'); // Command received from the request $warehouseName = trim(str_replace('show me direct labour payment of warehouse', '', strtolower($command))); // Initialize an empty data array $data = []; if ($warehouseName) { // Fetch the warehouse ID based on the name $warehouse = \App\Warehouse::where('name', $warehouseName)->first(); if ($warehouse) { // Fetch direct labour payments for the warehouse $labour_payments = \App\DirectLabourPayment::where('warehouse_id', $warehouse->id)->get(); // Get total unique descriptions and their total amount $unique_descriptions = \App\DirectLabourPayment::select('description', \DB::raw('SUM(amount) as total_amount')) ->where('warehouse_id', $warehouse->id) ->groupBy('description') ->get(); // Pass the results to the view $data['direct_labour_payments'] = $labour_payments; $data['unique_descriptions'] = $unique_descriptions; $data['message'] = 'Direct labour payments for warehouse: ' . ucfirst($warehouseName); } else { // Handle case where warehouse is not found $data['error'] = 'Warehouse "' . ucfirst($warehouseName) . '" not found.'; } } else { // Handle case where warehouse name is missing or invalid $data['error'] = 'Invalid command. Please specify a valid warehouse.'; } return view('voice.index', $data); } }