mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-08-04 00:16:08 -04:00
192 lines
7.9 KiB
PHP
192 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\StaffPayroll\Http\Controllers\API;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Modules\StaffPayroll\Entities\Invoice;
|
|
use Modules\StaffPayroll\Entities\InvoiceItem;
|
|
use Modules\StaffPayroll\Transformers\InvoiceItemResource;
|
|
|
|
class InvoiceItemController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$invoiceItems = InvoiceItem::query()
|
|
->with([
|
|
'invoice.user',
|
|
'video',
|
|
])
|
|
->when(auth()->user()->role->type !== 'superadmin', fn ($query) => $query->whereHas('invoice.user', fn ($query) => $query->where('id', auth()->id())))
|
|
->paginate();
|
|
|
|
return InvoiceItemResource::collection($invoiceItems);
|
|
}
|
|
|
|
public function total(Request $request)
|
|
{
|
|
$totalAmount = InvoiceItem::query()
|
|
->when(!permissionCheck('invoices.view.all') && permissionCheck('invoices.index'), function($query) {
|
|
$query->whereHas('invoice', function($query) {
|
|
$query->where('user_id', auth()->id());
|
|
});
|
|
})
|
|
->when($request->filled('invoice_no'), function($query) use($request) {
|
|
$invoice_no = $request->invoice_no;
|
|
|
|
$query->whereHas('invoice', function($query) use($invoice_no) {
|
|
$query->where('id', $invoice_no);
|
|
});
|
|
})
|
|
->when($request->filled('creators'), function($query) use($request) {
|
|
$creators = $request->creators;
|
|
|
|
$query->whereHas('invoice', function($query) use($creators) {
|
|
$query->whereIn('user_id', $creators);
|
|
});
|
|
})
|
|
->when($request->filled('start_date') && $request->filled('end_date'), function($query) use($request) {
|
|
$query->whereHas('invoice', function($query) use($request) {
|
|
$query->whereBetween(DB::raw('DATE(invoices.created_at)'), [$request->start_date, $request->end_date]);
|
|
});
|
|
})
|
|
->when($request->filled('status') && $request->status != 'ALL', function($query) use($request) {
|
|
$status = $request->status;
|
|
|
|
$query->whereHas('invoice', function($query) use($status) {
|
|
$query->where('status', $status);
|
|
});
|
|
})
|
|
->when($request->filled('approval') && $request->approval != 'ALL', function($query) use($request) {
|
|
$approval = $request->approval;
|
|
|
|
$query->whereHas('invoice', function($query) use($approval) {
|
|
if ($approval == 'APPROVED') {
|
|
$query->where('is_approved', true);
|
|
}
|
|
else {
|
|
$query->where('is_approved', false);
|
|
}
|
|
});
|
|
})
|
|
->sum(DB::raw('amount * quantity'));
|
|
|
|
$totalPaid = InvoiceItem::query()
|
|
->whereHas('invoice', fn ($query) => $query->where('status', Invoice::STATUS_PAID))
|
|
->when(!permissionCheck('invoices.view.all') && permissionCheck('invoices.index'), function($query) {
|
|
$query->whereHas('invoice', function($query) {
|
|
$query->where('user_id', auth()->id());
|
|
});
|
|
})
|
|
->when($request->filled('invoice_no'), function($query) use($request) {
|
|
$invoice_no = $request->invoice_no;
|
|
|
|
$query->whereHas('invoice', function($query) use($invoice_no) {
|
|
$query->where('id', $invoice_no);
|
|
});
|
|
})
|
|
->when($request->filled('creators'), function($query) use($request) {
|
|
$creators = $request->creators;
|
|
|
|
$query->whereHas('invoice', function($query) use($creators) {
|
|
$query->whereIn('user_id', $creators);
|
|
});
|
|
})
|
|
->when($request->filled('start_date') && $request->filled('end_date'), function($query) use($request) {
|
|
$query->whereHas('invoice', function($query) use($request) {
|
|
$query->whereBetween(DB::raw('DATE(invoices.created_at)'), [$request->start_date, $request->end_date]);
|
|
});
|
|
})
|
|
->when($request->filled('status') && $request->status != 'ALL', function($query) use($request) {
|
|
$status = $request->status;
|
|
|
|
$query->whereHas('invoice', function($query) use($status) {
|
|
$query->where('status', $status);
|
|
});
|
|
})
|
|
->when($request->filled('approval') && $request->approval != 'ALL', function($query) use($request) {
|
|
$approval = $request->approval;
|
|
|
|
$query->whereHas('invoice', function($query) use($approval) {
|
|
if ($approval == 'APPROVED') {
|
|
$query->where('is_approved', true);
|
|
}
|
|
else {
|
|
$query->where('is_approved', false);
|
|
}
|
|
});
|
|
})
|
|
->sum(DB::raw('amount * quantity'));
|
|
|
|
$totalDue = InvoiceItem::query()
|
|
->whereHas('invoice', fn ($query) => $query->where('status', Invoice::STATUS_OVERDUE))
|
|
->when(!permissionCheck('invoices.view.all') && permissionCheck('invoices.index'), function($query) {
|
|
$query->whereHas('invoice', function($query) {
|
|
$query->where('user_id', auth()->id());
|
|
});
|
|
})
|
|
->when($request->filled('invoice_no'), function($query) use($request) {
|
|
$invoice_no = $request->invoice_no;
|
|
|
|
$query->whereHas('invoice', function($query) use($invoice_no) {
|
|
$query->where('id', $invoice_no);
|
|
});
|
|
})
|
|
->when($request->filled('creators'), function($query) use($request) {
|
|
$creators = $request->creators;
|
|
|
|
$query->whereHas('invoice', function($query) use($creators) {
|
|
$query->whereIn('user_id', $creators);
|
|
});
|
|
})
|
|
->when($request->filled('start_date') && $request->filled('end_date'), function($query) use($request) {
|
|
$query->whereHas('invoice', function($query) use($request) {
|
|
$query->whereBetween(DB::raw('DATE(invoices.created_at)'), [$request->start_date, $request->end_date]);
|
|
});
|
|
})
|
|
->when($request->filled('status') && $request->status != 'ALL', function($query) use($request) {
|
|
$status = $request->status;
|
|
|
|
$query->whereHas('invoice', function($query) use($status) {
|
|
$query->where('status', $status);
|
|
});
|
|
})
|
|
->when($request->filled('approval') && $request->approval != 'ALL', function($query) use($request) {
|
|
$approval = $request->approval;
|
|
|
|
$query->whereHas('invoice', function($query) use($approval) {
|
|
if ($approval == 'APPROVED') {
|
|
$query->where('is_approved', true);
|
|
}
|
|
else {
|
|
$query->where('is_approved', false);
|
|
}
|
|
});
|
|
})
|
|
->sum(DB::raw('amount * quantity'));
|
|
|
|
return [
|
|
'total' => $totalAmount,
|
|
'total_paid' => $totalPaid,
|
|
'total_due' => $totalDue
|
|
];
|
|
}
|
|
|
|
public function highestTotalAmount()
|
|
{
|
|
$invoiceItem = InvoiceItem::query()
|
|
->select(DB::raw('invoice_id, sum(quantity * amount) as total'))
|
|
->groupBy('invoice_id')
|
|
->orderBy('total', 'desc')
|
|
->first();
|
|
|
|
$total = $invoiceItem ? $invoiceItem->total : 100;
|
|
|
|
return [
|
|
'total' => (float) $total
|
|
];
|
|
}
|
|
}
|