203 lines
9.4 KiB
PHP
203 lines
9.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\Business\App\Http\Controllers;
|
||
|
|
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use App\Models\Sale;
|
||
|
|
use App\Models\Stock;
|
||
|
|
use App\Models\Product;
|
||
|
|
use App\Models\Purchase;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
|
||
|
|
class AcnooLossProfitDetailReportController extends Controller
|
||
|
|
{
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('check.permission:loss-profit-reports.read')->only(['index']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function lossProfitDetails()
|
||
|
|
{
|
||
|
|
$businessId = auth()->user()->business_id;
|
||
|
|
$today = Carbon::today()->format('Y-m-d');
|
||
|
|
|
||
|
|
$salesQuery = Sale::where('business_id', $businessId)->whereDate('created_at', $today);
|
||
|
|
$purchaseQuery = Purchase::where('business_id', $businessId)->whereDate('created_at', $today);
|
||
|
|
$productQuery = Product::where('business_id', $businessId);
|
||
|
|
|
||
|
|
// Opening stock (before today) from stocks table
|
||
|
|
$opening_stock_by_purchase = Stock::whereHas('product', function ($query) use ($businessId) {
|
||
|
|
$query->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<', $today)
|
||
|
|
->sum(DB::raw('productPurchasePrice * productStock'));
|
||
|
|
|
||
|
|
// Closing stock (up to today) from stocks table
|
||
|
|
$closing_stock_by_purchase = Stock::whereHas('product', function ($query) use ($businessId) {
|
||
|
|
$query->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<=', $today)
|
||
|
|
->sum(DB::raw('productPurchasePrice * productStock'));
|
||
|
|
|
||
|
|
$total_purchase_price = (clone $purchaseQuery)->sum('totalAmount');
|
||
|
|
$total_purchase_shipping_charge = (clone $purchaseQuery)->sum('shipping_charge');
|
||
|
|
$total_purchase_discount = (clone $purchaseQuery)->sum('discountAmount');
|
||
|
|
$all_purchase_return = (clone $purchaseQuery)->with([
|
||
|
|
'purchaseReturns' => function ($query) {
|
||
|
|
$query->withSum('details as total_return_amount', 'return_amount');
|
||
|
|
}
|
||
|
|
])
|
||
|
|
->get()
|
||
|
|
->flatMap
|
||
|
|
->purchaseReturns
|
||
|
|
->sum('total_return_amount');
|
||
|
|
|
||
|
|
$opening_stock_by_sale = Stock::whereHas('product', function ($query) use ($businessId) {
|
||
|
|
$query->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<', $today)
|
||
|
|
->sum(DB::raw('productSalePrice * productStock'));
|
||
|
|
|
||
|
|
$closing_stock_by_sale = Stock::whereHas('product', function ($query) use ($businessId) {
|
||
|
|
$query->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<=', $today)
|
||
|
|
->sum(DB::raw('productSalePrice * productStock'));
|
||
|
|
|
||
|
|
$total_sale_price = (clone $salesQuery)->sum('totalAmount');
|
||
|
|
$total_sale_shipping_charge = (clone $salesQuery)->sum('shipping_charge');
|
||
|
|
$total_sale_discount = (clone $salesQuery)->sum('discountAmount');
|
||
|
|
$total_sale_rounding_off = (clone $salesQuery)->sum('rounding_amount');
|
||
|
|
|
||
|
|
$all_sale_return = (clone $salesQuery)->with([
|
||
|
|
'saleReturns' => function ($query) {
|
||
|
|
$query->withSum('details as total_return_amount', 'return_amount');
|
||
|
|
}
|
||
|
|
])
|
||
|
|
->get()
|
||
|
|
->flatMap
|
||
|
|
->saleReturns
|
||
|
|
->sum('total_return_amount');
|
||
|
|
|
||
|
|
return view('business::reports.loss-profits-details.index', compact('opening_stock_by_purchase', 'closing_stock_by_purchase', 'total_purchase_price', 'total_purchase_shipping_charge', 'total_purchase_discount', 'all_purchase_return', 'all_sale_return', 'opening_stock_by_sale', 'closing_stock_by_sale', 'total_sale_price', 'total_sale_shipping_charge', 'total_sale_discount', 'total_sale_rounding_off'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function lossProfitFilter(Request $request)
|
||
|
|
{
|
||
|
|
$businessId = auth()->user()->business_id;
|
||
|
|
|
||
|
|
$startDate = Carbon::today();
|
||
|
|
$endDate = Carbon::today();
|
||
|
|
|
||
|
|
switch ($request->custom_days) {
|
||
|
|
case 'yesterday':
|
||
|
|
$startDate = $endDate = Carbon::yesterday();
|
||
|
|
break;
|
||
|
|
case 'last_seven_days':
|
||
|
|
$startDate = Carbon::today()->subDays(6);
|
||
|
|
break;
|
||
|
|
case 'last_thirty_days':
|
||
|
|
$startDate = Carbon::today()->subDays(29);
|
||
|
|
break;
|
||
|
|
case 'current_month':
|
||
|
|
$startDate = Carbon::now()->startOfMonth();
|
||
|
|
$endDate = Carbon::now()->endOfMonth();
|
||
|
|
break;
|
||
|
|
case 'last_month':
|
||
|
|
$startDate = Carbon::now()->subMonth()->startOfMonth();
|
||
|
|
$endDate = Carbon::now()->subMonth()->endOfMonth();
|
||
|
|
break;
|
||
|
|
case 'current_year':
|
||
|
|
$startDate = Carbon::now()->startOfYear();
|
||
|
|
$endDate = Carbon::now()->endOfYear();
|
||
|
|
break;
|
||
|
|
case 'custom_date':
|
||
|
|
if ($request->from_date && $request->to_date) {
|
||
|
|
$startDate = Carbon::parse($request->from_date);
|
||
|
|
$endDate = Carbon::parse($request->to_date);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
$salesQuery = Sale::where('business_id', $businessId)
|
||
|
|
->whereBetween('created_at', [$startDate, $endDate]);
|
||
|
|
|
||
|
|
$purchaseQuery = Purchase::where('business_id', $businessId)
|
||
|
|
->whereBetween('created_at', [$startDate, $endDate]);
|
||
|
|
|
||
|
|
$productQuery = Product::where('business_id', $businessId);
|
||
|
|
|
||
|
|
// Opening stock by purchase (before start date)
|
||
|
|
$opening_stock_by_purchase = Stock::whereHas('product', function ($q) use ($businessId) {
|
||
|
|
$q->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<', $startDate)
|
||
|
|
->sum(DB::raw('productPurchasePrice * productStock'));
|
||
|
|
|
||
|
|
// Closing stock by purchase (up to end date)
|
||
|
|
$closing_stock_by_purchase = Stock::whereHas('product', function ($q) use ($businessId) {
|
||
|
|
$q->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<=', $endDate)
|
||
|
|
->sum(DB::raw('productPurchasePrice * productStock'));
|
||
|
|
|
||
|
|
$total_purchase_price = (clone $purchaseQuery)->sum('totalAmount');
|
||
|
|
$total_purchase_shipping_charge = (clone $purchaseQuery)->sum('shipping_charge');
|
||
|
|
$total_purchase_discount = (clone $purchaseQuery)->sum('discountAmount');
|
||
|
|
|
||
|
|
$all_purchase_return = (clone $purchaseQuery)->with([
|
||
|
|
'purchaseReturns' => function ($query) {
|
||
|
|
$query->withSum('details as total_return_amount', 'return_amount');
|
||
|
|
}
|
||
|
|
])->get()->flatMap->purchaseReturns->sum('total_return_amount');
|
||
|
|
|
||
|
|
// Opening stock by sale
|
||
|
|
$opening_stock_by_sale = Stock::whereHas('product', function ($q) use ($businessId) {
|
||
|
|
$q->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<', $startDate)
|
||
|
|
->sum(DB::raw('productSalePrice * productStock'));
|
||
|
|
|
||
|
|
// Closing stock by sale
|
||
|
|
$closing_stock_by_sale = Stock::whereHas('product', function ($q) use ($businessId) {
|
||
|
|
$q->where('business_id', $businessId);
|
||
|
|
})
|
||
|
|
->whereDate('created_at', '<=', $endDate)
|
||
|
|
->sum(DB::raw('productSalePrice * productStock'));
|
||
|
|
|
||
|
|
$total_sale_price = (clone $salesQuery)->sum('totalAmount');
|
||
|
|
$total_sale_shipping_charge = (clone $salesQuery)->sum('shipping_charge');
|
||
|
|
$total_sale_discount = (clone $salesQuery)->sum('discountAmount');
|
||
|
|
$total_sale_rounding_off = (clone $salesQuery)->sum('rounding_amount') ?? 5;
|
||
|
|
$total_sale_rounding_off = 5;
|
||
|
|
|
||
|
|
$all_sale_return = (clone $salesQuery)->with([
|
||
|
|
'saleReturns' => function ($query) {
|
||
|
|
$query->withSum('details as total_return_amount', 'return_amount');
|
||
|
|
}
|
||
|
|
])->get()->flatMap->saleReturns->sum('total_return_amount');
|
||
|
|
|
||
|
|
|
||
|
|
if ($request->ajax()) {
|
||
|
|
return response()->json([
|
||
|
|
'opening_stock_by_purchase' => currency_format($opening_stock_by_purchase, currency: business_currency()),
|
||
|
|
'closing_stock_by_purchase' => currency_format($closing_stock_by_purchase, currency: business_currency()),
|
||
|
|
'total_purchase_price' => currency_format($total_purchase_price, currency: business_currency()),
|
||
|
|
'total_purchase_shipping_charge' => currency_format($total_purchase_shipping_charge, currency: business_currency()),
|
||
|
|
'total_purchase_discount' => currency_format($total_purchase_discount, currency: business_currency()),
|
||
|
|
'all_purchase_return' => currency_format($all_purchase_return, currency: business_currency()),
|
||
|
|
'all_sale_return' => currency_format($all_sale_return, currency: business_currency()),
|
||
|
|
'opening_stock_by_sale' => currency_format($opening_stock_by_sale, currency: business_currency()),
|
||
|
|
'closing_stock_by_sale' => currency_format($closing_stock_by_sale, currency: business_currency()),
|
||
|
|
'total_sale_price' => currency_format($total_sale_price, currency: business_currency()),
|
||
|
|
'total_sale_shipping_charge' => currency_format($total_sale_shipping_charge, currency: business_currency()),
|
||
|
|
'total_sale_discount' => currency_format($total_sale_discount, currency: business_currency()),
|
||
|
|
'total_sale_rounding_off' => currency_format($total_sale_rounding_off, currency: business_currency()),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|