103 lines
3.9 KiB
PHP
103 lines
3.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\Business\App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Sale;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Party;
|
||
|
|
use App\Services\PdfService;
|
||
|
|
use App\Traits\DateFilterTrait;
|
||
|
|
use App\Traits\DateRangeTrait;
|
||
|
|
use Maatwebsite\Excel\Facades\Excel;
|
||
|
|
use Modules\Business\App\Exports\ExportBillWisePofit;
|
||
|
|
|
||
|
|
class AcnooBillWiseProfitController extends Controller
|
||
|
|
{
|
||
|
|
use DateFilterTrait, DateRangeTrait;
|
||
|
|
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$parties = Party::where('business_id', auth()->user()->business_id)->where('type', '!=', 'Supplier')->latest()->get();
|
||
|
|
|
||
|
|
$salesQuery = Sale::with('party:id,name')
|
||
|
|
->where('business_id', auth()->user()->business_id);
|
||
|
|
|
||
|
|
$salesQuery->when($request->search, function ($query) use ($request) {
|
||
|
|
$query->where(function ($q) use ($request) {
|
||
|
|
$q->where('lossProfit', 'like', '%' . $request->search . '%')
|
||
|
|
->orWhere('totalAmount', 'like', '%' . $request->search . '%')
|
||
|
|
->orWhere('invoiceNumber', 'like', '%' . $request->search . '%')
|
||
|
|
->orWhereHas('party', function ($q) use ($request) {
|
||
|
|
$q->where('name', 'like', '%' . $request->search . '%');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
$salesQuery->when($request->party_id, function ($query, $partyId) {
|
||
|
|
if ($partyId === 'Guest') {
|
||
|
|
$query->whereNull('party_id');
|
||
|
|
} else {
|
||
|
|
$query->where('party_id', $partyId);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
$duration = $request->custom_days ?: 'today';
|
||
|
|
[$filter_from_date, $filter_to_date] = $this->applyDateRange($duration, $request->from_date, $request->to_date);
|
||
|
|
|
||
|
|
$this->applyDateFilter($salesQuery, $duration, 'saleDate', $request->from_date, $request->to_date);
|
||
|
|
|
||
|
|
$profits = $salesQuery->latest()->paginate($request->per_page ?? 20)->appends($request->query());
|
||
|
|
|
||
|
|
$loss = (clone $salesQuery)->where('lossProfit', '<=', 0)->get()
|
||
|
|
->sum(function ($sale) {
|
||
|
|
return abs($sale->lossProfit);
|
||
|
|
});
|
||
|
|
|
||
|
|
$profit = (clone $salesQuery)->where('lossProfit', '>', 0)->sum('lossProfit');
|
||
|
|
|
||
|
|
$total_sale_amount = (clone $salesQuery)->sum('totalAmount');
|
||
|
|
|
||
|
|
if ($request->ajax()) {
|
||
|
|
return response()->json([
|
||
|
|
'data' => view('business::bill-wise-profits.datas', compact('profits', 'filter_from_date', 'filter_to_date', 'duration'))->render(),
|
||
|
|
'total_loss' => currency_format($loss, currency: business_currency()),
|
||
|
|
'total_profit' => currency_format($profit, currency: business_currency()),
|
||
|
|
'total_sale_amount' => currency_format($total_sale_amount, currency: business_currency()),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return view('business::bill-wise-profits.index', compact('profits', 'loss', 'profit', 'total_sale_amount', 'filter_from_date', 'filter_to_date', 'duration', 'parties'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(string $id)
|
||
|
|
{
|
||
|
|
$lossProfit = Sale::select('id', 'invoiceNumber', 'party_id')
|
||
|
|
->where('business_id', auth()->user()->business_id)
|
||
|
|
->with('party:id,name', 'details', 'details.product:id,productName', 'details.stock:id,batch_no')
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
return response()->json($lossProfit);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportExcel()
|
||
|
|
{
|
||
|
|
return Excel::download(new ExportBillWisePofit, 'bill-wise-profit.xlsx');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportCsv()
|
||
|
|
{
|
||
|
|
return Excel::download(new ExportBillWisePofit, 'bill-wise-profit.csv');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportPdf()
|
||
|
|
{
|
||
|
|
$profits = Sale::with('party:id,name')
|
||
|
|
->where('business_id', auth()->user()->business_id)
|
||
|
|
->latest()
|
||
|
|
->get();
|
||
|
|
|
||
|
|
return PdfService::render('business::bill-wise-profits.pdf', compact('profits'),'bill-wise-profit-report.pdf');
|
||
|
|
}
|
||
|
|
}
|