100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\AssetExchange\Http\Controllers\Concerns\AuthorizesAssetExchange;
|
|
use Modules\AssetExchange\Models\Commission;
|
|
use Modules\AssetExchange\Services\MarketIntelService;
|
|
use Modules\AssetExchange\Utils\AssetExchangeUtil;
|
|
|
|
class ReportController extends Controller
|
|
{
|
|
use AuthorizesAssetExchange;
|
|
|
|
public function __construct(
|
|
protected AssetExchangeUtil $aexUtil,
|
|
protected MarketIntelService $marketIntelService
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeAexAccess('aex.reports.view');
|
|
|
|
$business = $this->aexUtil->getBusiness();
|
|
if (! $business) {
|
|
abort(404);
|
|
}
|
|
|
|
$commissions = collect();
|
|
if (Schema::hasTable('aex_commissions')) {
|
|
$query = Commission::where('business_id', $business->id)
|
|
->with(['deal.listing', 'user']);
|
|
|
|
if ($status = $request->get('status')) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
$commissions = $query->orderByDesc('created_at')->limit(500)->get();
|
|
}
|
|
|
|
return view('assetexchange::reports.index', [
|
|
'commissions' => $commissions,
|
|
'statuses' => $this->aexUtil->statusLabels(),
|
|
'filters' => $request->only(['status', 'listing_id', 'source']),
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$this->authorizeAexAccess('aex.reports.view');
|
|
|
|
$business = $this->aexUtil->getBusiness();
|
|
if (! $business) {
|
|
abort(404);
|
|
}
|
|
|
|
$filters = $request->only(['listing_id', 'source']);
|
|
|
|
try {
|
|
$csv = $this->marketIntelService->exportCsv($business, $filters);
|
|
} catch (\Throwable $e) {
|
|
return redirect()->back()->with('status', $e->getMessage());
|
|
}
|
|
|
|
$filename = 'aex-market-comps-'.now()->format('Y-m-d').'.csv';
|
|
|
|
return response()->streamDownload(function () use ($csv) {
|
|
echo $csv;
|
|
}, $filename, [
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
]);
|
|
}
|
|
|
|
public function importIeComps(Request $request)
|
|
{
|
|
$this->authorizeAexAccess('aex.reports.view');
|
|
|
|
$business = $this->aexUtil->getBusiness();
|
|
if (! $business) {
|
|
abort(404);
|
|
}
|
|
|
|
$listingId = $request->input('listing_id') ? (int) $request->input('listing_id') : null;
|
|
|
|
try {
|
|
$count = $this->marketIntelService->importFromIeBenchmarks($business, $listingId);
|
|
$msg = __('assetexchange::lang.import_ie_comps_done', ['count' => $count]);
|
|
} catch (\Throwable $e) {
|
|
return redirect()->back()->with('status', $e->getMessage());
|
|
}
|
|
|
|
return redirect()
|
|
->action([self::class, 'index'])
|
|
->with('status', $msg);
|
|
}
|
|
}
|