2026-03-15 17:08:23 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Business\App\Exports;
|
|
|
|
|
|
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
|
use App\Models\Product;
|
2026-05-14 11:55:22 +07:00
|
|
|
use App\Traits\DateFilterTrait;
|
2026-03-15 17:08:23 +07:00
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
|
|
|
|
|
|
|
|
class ExportBalanceSheet implements FromView
|
|
|
|
|
{
|
2026-05-14 11:55:22 +07:00
|
|
|
use DateFilterTrait;
|
|
|
|
|
|
2026-03-15 17:08:23 +07:00
|
|
|
public function view(): View
|
|
|
|
|
{
|
|
|
|
|
$businessId = auth()->user()->business_id;
|
2026-05-14 11:55:22 +07:00
|
|
|
$duration = request('custom_days') ? : 'today';
|
|
|
|
|
$fromDate = request('from_date');
|
|
|
|
|
$toDate = request('to_date');
|
|
|
|
|
$perPage = request('per_page') ?? 20;
|
2026-03-15 17:08:23 +07:00
|
|
|
|
|
|
|
|
$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);
|
2026-05-14 11:55:22 +07:00
|
|
|
$this->applyDateFilter($productQuery, $duration, 'created_at', $fromDate, $toDate);
|
2026-03-15 17:08:23 +07:00
|
|
|
$products = $productQuery->get()
|
|
|
|
|
->map(function ($item) {
|
|
|
|
|
$item->source = 'product';
|
|
|
|
|
return $item;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$bankQuery = PaymentType::where('business_id', $businessId);
|
2026-05-14 11:55:22 +07:00
|
|
|
$this->applyDateFilter($bankQuery, $duration, 'opening_date', $fromDate, $toDate);
|
2026-03-15 17:08:23 +07:00
|
|
|
$banks = $bankQuery->get()
|
|
|
|
|
->map(function ($item) {
|
|
|
|
|
$item->source = 'bank';
|
|
|
|
|
return $item;
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$asset_datas = $products->merge($banks)->take($perPage);
|
2026-03-15 17:08:23 +07:00
|
|
|
|
|
|
|
|
$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 view('business::balance-sheets.excel-csv', compact('asset_datas', 'total_asset'));
|
|
|
|
|
}
|
|
|
|
|
}
|