149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Business\App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\PaymentType;
|
|
use Illuminate\Http\Request;
|
|
use App\Traits\DateFilterTrait;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\PdfService;
|
|
use App\Traits\DateRangeTrait;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Modules\Business\App\Exports\ExportBalanceSheet;
|
|
|
|
class AcnooBalanceSheetController extends Controller
|
|
{
|
|
use DateFilterTrait, DateRangeTrait;
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$businessId = auth()->user()->business_id;
|
|
$duration = $request->custom_days ? : 'today';
|
|
$fromDate = $request->from_date;
|
|
$toDate = $request->to_date;
|
|
$perPage = $request->per_page ?? 20;
|
|
|
|
[$filter_from_date, $filter_to_date] = $this->applyDateRange($duration, $request->from_date, $request->to_date);
|
|
|
|
$productQuery = Product::select('id', 'business_id', 'productName', 'product_type', 'created_at')
|
|
->with(['stocks:id,business_id,product_id,productStock,productPurchasePrice', 'combo_products.stock'])
|
|
->where('business_id', $businessId);
|
|
$this->applyDateFilter($productQuery, $duration, 'created_at', $fromDate, $toDate);
|
|
$products = $productQuery->get()
|
|
->map(function ($item) {
|
|
$item->source = 'product';
|
|
return $item;
|
|
});
|
|
|
|
$bankQuery = PaymentType::where('business_id', $businessId);
|
|
$this->applyDateFilter($bankQuery, $duration, 'opening_date', $fromDate, $toDate);
|
|
$banks = $bankQuery->get()
|
|
->map(function ($item) {
|
|
$item->source = 'bank';
|
|
return $item;
|
|
});
|
|
|
|
$asset_datas = $products->merge($banks);
|
|
$page = LengthAwarePaginator::resolveCurrentPage();
|
|
$products = $asset_datas->slice(($page - 1) * $perPage, $perPage)->values();
|
|
$asset_datas = new LengthAwarePaginator(
|
|
$products,
|
|
$asset_datas->count(),
|
|
$perPage,
|
|
$page,
|
|
['path' => request()->url(), 'query' => request()->query()]
|
|
);
|
|
|
|
$total_stock_value = 0;
|
|
foreach ($products as $product) {
|
|
if (in_array($product->product_type, ['single', 'variant'])) {
|
|
foreach ($product->stocks as $stock) {
|
|
$total_stock_value += $stock->productStock * $stock->productPurchasePrice;
|
|
}
|
|
}
|
|
|
|
if ($product->product_type === 'combo') {
|
|
foreach ($product->combo_products as $combo) {
|
|
$childStock = $combo->stock;
|
|
if ($childStock) {
|
|
$total_stock_value += ($childStock->productStock / $combo->quantity) * $combo->purchase_price;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$totalBankBalance = $banks->sum('balance');
|
|
$total_asset = $total_stock_value + $totalBankBalance;
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'data' => view('business::balance-sheets.datas', compact('asset_datas', 'total_asset', 'filter_from_date', 'filter_to_date', 'duration'))->render(),
|
|
'total_asset' => currency_format($total_asset, currency: business_currency()),
|
|
]);
|
|
}
|
|
|
|
return view('business::balance-sheets.index', compact('asset_datas', 'total_asset', 'filter_from_date', 'filter_to_date', 'duration'));
|
|
}
|
|
|
|
public function exportExcel()
|
|
{
|
|
return Excel::download(new ExportBalanceSheet, 'balance-sheet.xlsx');
|
|
}
|
|
|
|
public function exportCsv()
|
|
{
|
|
return Excel::download(new ExportBalanceSheet, 'balance-sheet.csv');
|
|
}
|
|
|
|
public function exportPdf(Request $request)
|
|
{
|
|
$businessId = auth()->user()->business_id;
|
|
|
|
// PRODUCT
|
|
$productQuery = Product::select('id', 'business_id', 'productName', 'product_type', 'created_at')
|
|
->with(['stocks:id,business_id,product_id,productStock,productPurchasePrice', 'combo_products.stock'])
|
|
->where('business_id', $businessId);
|
|
$products = $productQuery->get()
|
|
->map(function ($item) {
|
|
$item->source = 'product';
|
|
return $item;
|
|
});
|
|
|
|
// BANK
|
|
$bankQuery = PaymentType::where('business_id', $businessId);
|
|
$banks = $bankQuery->get()
|
|
->map(function ($item) {
|
|
$item->source = 'bank';
|
|
return $item;
|
|
});
|
|
|
|
$asset_datas = $products->merge($banks);
|
|
|
|
// TOTAL STOCK VALUE
|
|
$total_stock_value = 0;
|
|
foreach ($products as $product) {
|
|
if (in_array($product->product_type, ['single', 'variant'])) {
|
|
foreach ($product->stocks as $stock) {
|
|
$total_stock_value += $stock->productStock * $stock->productPurchasePrice;
|
|
}
|
|
}
|
|
|
|
if ($product->product_type === 'combo') {
|
|
foreach ($product->combo_products as $combo) {
|
|
$childStock = $combo->stock;
|
|
if ($childStock) {
|
|
$total_stock_value += ($childStock->productStock / $combo->quantity) * $combo->purchase_price;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$totalBankBalance = $banks->sum('balance');
|
|
$total_asset = $total_stock_value + $totalBankBalance;
|
|
|
|
return PdfService::render('business::balance-sheets.pdf', compact('asset_datas', 'total_asset'),'balance-sheet-report.pdf');
|
|
}
|
|
}
|