159 lines
5.8 KiB
PHP
159 lines
5.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\Business\App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Vat;
|
||
|
|
use App\Models\Sale;
|
||
|
|
use App\Models\Purchase;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Services\PdfService;
|
||
|
|
use App\Traits\DateFilterTrait;
|
||
|
|
use App\Traits\DateRangeTrait;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Maatwebsite\Excel\Facades\Excel;
|
||
|
|
use Modules\Business\App\Exports\ExportVatReport;
|
||
|
|
|
||
|
|
class AcnooVatReportController extends Controller
|
||
|
|
{
|
||
|
|
use DateFilterTrait, DateRangeTrait;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('check.permission:vat-reports.read')->only(['index']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$businessId = auth()->user()->business_id;
|
||
|
|
$vats = Vat::where('business_id', $businessId)->whereStatus(1)->get();
|
||
|
|
|
||
|
|
$duration = $request->custom_days ?: 'today';
|
||
|
|
[$filter_from_date, $filter_to_date] = $this->applyDateRange($duration, $request->from_date, $request->to_date);
|
||
|
|
|
||
|
|
$salesQuery = Sale::with('user:id,name', 'party:id,name,email,phone,type', 'business:id,companyName', 'payment_type:id,name', 'transactions')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0);
|
||
|
|
|
||
|
|
// Date Filter
|
||
|
|
$this->applyDateFilter($salesQuery, $duration, 'saleDate', $request->from_date, $request->to_date);
|
||
|
|
|
||
|
|
$sales = $salesQuery->latest()->paginate(20, ['*'], 'sales');
|
||
|
|
|
||
|
|
$salesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$salesVatTotals[$vat->id] = (clone $salesQuery)->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
$purchasesQuery = Purchase::with('details', 'party', 'details.product', 'details.product.category', 'payment_type:id,name', 'transactions')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0);
|
||
|
|
|
||
|
|
// Date Filter
|
||
|
|
$this->applyDateFilter($purchasesQuery, $duration, 'purchaseDate', $request->from_date, $request->to_date);
|
||
|
|
|
||
|
|
$purchases = $purchasesQuery->latest()->paginate(20, ['*'], 'purchases');
|
||
|
|
|
||
|
|
$purchasesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$purchasesVatTotals[$vat->id] = (clone $purchasesQuery)->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($request->ajax()) {
|
||
|
|
return response()->json([
|
||
|
|
'sale_data' => view('business::reports.vats.sale-datas', compact('sales', 'salesVatTotals', 'vats', 'filter_from_date', 'filter_to_date', 'duration'))->render(),
|
||
|
|
'purchase_data' => view('business::reports.vats.purchase-datas', compact('purchases', 'purchasesVatTotals', 'vats', 'filter_from_date', 'filter_to_date', 'duration'))->render(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return view('business::reports.vats.index', compact('sales', 'salesVatTotals', 'purchases', 'purchasesVatTotals', 'vats', 'filter_from_date', 'filter_to_date', 'duration'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportExcel($type = 'all')
|
||
|
|
{
|
||
|
|
return $this->exportFile($type, 'vat-report.xlsx');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportCsv($type = 'all')
|
||
|
|
{
|
||
|
|
return $this->exportFile($type, 'vat-report.csv');
|
||
|
|
}
|
||
|
|
|
||
|
|
private function exportFile($type, $filename, $format = null)
|
||
|
|
{
|
||
|
|
$businessId = auth()->user()->business_id;
|
||
|
|
|
||
|
|
$sales = collect();
|
||
|
|
$purchases = collect();
|
||
|
|
|
||
|
|
if ($type === 'sales' || $type === 'all') {
|
||
|
|
$sales = Sale::with('user:id,name', 'party:id,name,email,phone,type', 'payment_type:id,name', 'transactions')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0)
|
||
|
|
->latest()
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($type === 'purchases' || $type === 'all') {
|
||
|
|
$purchases = Purchase::with('details', 'party', 'details.product', 'details.product.category', 'payment_type:id,name', 'transactions')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0)
|
||
|
|
->latest()
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
$vats = Vat::where('business_id', $businessId)->get();
|
||
|
|
|
||
|
|
$salesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$salesVatTotals[$vat->id] = $sales->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
$purchasesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$purchasesVatTotals[$vat->id] = $purchases->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
$export = new ExportVatReport($sales, $purchases, $vats, $salesVatTotals, $purchasesVatTotals);
|
||
|
|
|
||
|
|
return Excel::download($export, $filename, $format);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportPdf($type = 'all')
|
||
|
|
{
|
||
|
|
$businessId = auth()->user()->business_id;
|
||
|
|
|
||
|
|
$sales = collect();
|
||
|
|
$purchases = collect();
|
||
|
|
|
||
|
|
if ($type === 'sales' || $type === 'all') {
|
||
|
|
$sales = Sale::with('party', 'payment_type')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0)
|
||
|
|
->latest()
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($type === 'purchases' || $type === 'all') {
|
||
|
|
$purchases = Purchase::with('party', 'payment_type')
|
||
|
|
->where('business_id', $businessId)
|
||
|
|
->where('vat_amount', '>', 0)
|
||
|
|
->latest()
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
$vats = Vat::where('business_id', $businessId)->get();
|
||
|
|
|
||
|
|
$salesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$salesVatTotals[$vat->id] = $sales->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
$purchasesVatTotals = [];
|
||
|
|
foreach ($vats as $vat) {
|
||
|
|
$purchasesVatTotals[$vat->id] = $purchases->where('vat_id', $vat->id)->sum('vat_amount');
|
||
|
|
}
|
||
|
|
|
||
|
|
return PdfService::render('business::reports.vats.pdf', compact('sales', 'salesVatTotals', 'purchases', 'purchasesVatTotals', 'vats', 'type'),'vats-report.pdf');
|
||
|
|
}
|
||
|
|
}
|