fix feature report stock product
This commit is contained in:
102
app/Http/Controllers/Reports/ReportStockProductsController.php
Normal file
102
app/Http/Controllers/Reports/ReportStockProductsController.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Product;
|
||||
use App\Models\Dealer;
|
||||
use App\Models\Stock;
|
||||
use App\Models\StockLog;
|
||||
use App\Services\StockReportService;
|
||||
use App\Exports\StockProductsExport;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yajra\DataTables\DataTables;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class ReportStockProductsController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$menu = Menu::where('link','reports.stock-product.index')->first();
|
||||
abort_if(!Gate::allows('view', $menu), 403);
|
||||
|
||||
return view('reports.stock-products');
|
||||
}
|
||||
|
||||
public function getData(Request $request)
|
||||
{
|
||||
$menu = Menu::where('link','reports.stock-product.index')->first();
|
||||
abort_if(!Gate::allows('view', $menu), 403);
|
||||
|
||||
if ($request->ajax()) {
|
||||
$filterDate = $request->get('filter_date');
|
||||
|
||||
$stockService = new StockReportService();
|
||||
$reportData = $stockService->getOptimizedStockReportData($filterDate);
|
||||
|
||||
return DataTables::of($reportData['data'])
|
||||
->addIndexColumn()
|
||||
->addColumn('product_info', function($row) {
|
||||
return "<strong>{$row['product_name']}</strong><br><small class='text-muted'>{$row['product_code']}</small>";
|
||||
})
|
||||
->addColumn('total_stock', function($row) {
|
||||
return number_format($row['total_stock'], 2);
|
||||
})
|
||||
->rawColumns(['product_info'])
|
||||
->make(true);
|
||||
}
|
||||
|
||||
return response()->json(['error' => 'Invalid request'], 400);
|
||||
}
|
||||
|
||||
public function getDealers()
|
||||
{
|
||||
$dealers = Dealer::orderBy('name')->get(['id', 'name', 'dealer_code']);
|
||||
return response()->json($dealers);
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
try {
|
||||
$menu = Menu::where('link','reports.stock-product.index')->first();
|
||||
abort_if(!Gate::allows('view', $menu), 403);
|
||||
|
||||
$filterDate = $request->get('filter_date');
|
||||
|
||||
$stockService = new StockReportService();
|
||||
$reportData = $stockService->getOptimizedStockReportData($filterDate);
|
||||
|
||||
// Validate report data
|
||||
if (!isset($reportData['data']) || !isset($reportData['dealers'])) {
|
||||
throw new \Exception('Invalid report data structure');
|
||||
}
|
||||
|
||||
// Debug: Log dealer names to identify problematic characters
|
||||
Log::info('Export data validation', [
|
||||
'data_count' => count($reportData['data']),
|
||||
'dealers_count' => count($reportData['dealers']),
|
||||
'dealer_names' => $reportData['dealers']->pluck('name')->toArray(),
|
||||
'first_data_row' => isset($reportData['data'][0]) ? array_keys($reportData['data'][0]) : []
|
||||
]);
|
||||
|
||||
$fileName = 'laporan_stok_produk_' . ($filterDate ?: date('Y-m-d')) . '.xlsx';
|
||||
|
||||
return Excel::download(new StockProductsExport($reportData), $fileName);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Export error: ' . $e->getMessage(), [
|
||||
'filter_date' => $request->get('filter_date'),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
return back()->with('error', 'Gagal mengexport data: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user