Export Excel Invoice & Imporve Invoice
This commit is contained in:
@@ -12,6 +12,8 @@ use App\Helpers\Helper;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Services\ExportExcelService;
|
||||
|
||||
class InvoicePaymentController extends Controller
|
||||
{
|
||||
@@ -30,15 +32,11 @@ class InvoicePaymentController extends Controller
|
||||
|
||||
$query->orderBy($orderBy, $direction);
|
||||
})
|
||||
->when($request->input('start_date') , function ($query, $start_date) {
|
||||
$query->where(function ($query) use ($start_date) {
|
||||
$query->where('invoice_payments.created_at', '>=', $start_date);
|
||||
});
|
||||
->when($request->input('start_date'), function ($query, $start_date) {
|
||||
$query->whereDate('invoice_payments.created_at', '>=', $start_date);
|
||||
})
|
||||
->when($request->input('end_date') , function ($query, $end_date) {
|
||||
$query->where(function ($query) use ($end_date) {
|
||||
$query->where('invoice_payments.created_at', '<=', $end_date);
|
||||
});
|
||||
->when($request->input('end_date'), function ($query, $end_date) {
|
||||
$query->whereDate('invoice_payments.created_at', '<=', $end_date);
|
||||
})
|
||||
->when($request->input('status') , function ($query, $status) {
|
||||
$query->where(function ($query) use ($status) {
|
||||
@@ -183,8 +181,10 @@ class InvoicePaymentController extends Controller
|
||||
->leftJoin('request_logs', 'claim_requests.request_log_id','=', 'request_logs.id')
|
||||
->leftJoin('members', 'request_logs.member_id', '=', 'members.id')
|
||||
->where('invoice_payment_details.invoice_payment_id', $id)
|
||||
->whereNull('invoice_payment_details.deleted_by')
|
||||
->orWhere('invoice_payment_details.deleted_by', 0)
|
||||
->where(function ($q) {
|
||||
$q->whereNull('invoice_payment_details.deleted_by')
|
||||
->orWhere('invoice_payment_details.deleted_by', 0);
|
||||
})
|
||||
->select(
|
||||
'claim_requests.id',
|
||||
'request_logs.invoice_no',
|
||||
@@ -240,6 +240,7 @@ class InvoicePaymentController extends Controller
|
||||
'invoice_payments.id',
|
||||
'files.id as file_id',
|
||||
'invoice_payments.amount_paid',
|
||||
'invoice_payments.no_reference',
|
||||
'invoice_payments.payment_number',
|
||||
'files.path',
|
||||
'files.source',
|
||||
@@ -274,6 +275,7 @@ class InvoicePaymentController extends Controller
|
||||
return [
|
||||
'id' => $group->first()->id,
|
||||
'amount_paid' => $group->first()->amount_paid,
|
||||
'no_reference' => $group->first()->no_reference,
|
||||
'payment_number' => $group->first()->payment_number,
|
||||
'files' => $group->map(function ($file) {
|
||||
return [
|
||||
@@ -349,6 +351,7 @@ class InvoicePaymentController extends Controller
|
||||
'start_date' => $request->start_date,
|
||||
'end_date' => $request->start_date,
|
||||
'amount_paid' => $this->normalizeCurrency($valuePayments['amount']),
|
||||
'no_reference' => $valuePayments['noReference'],
|
||||
'status' => 'submitted',
|
||||
'created_by' => auth()->user()->id,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
@@ -405,40 +408,56 @@ class InvoicePaymentController extends Controller
|
||||
'start_date' => $request->start_date,
|
||||
'end_date' => $request->end_date,
|
||||
'amount_paid' => $this->normalizeCurrency($valuePayments['amount']),
|
||||
'no_reference' => $valuePayments['noReference'],
|
||||
'updated_by' => auth()->user()->id,
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
$existingClaims = DB::table('invoice_payment_details')
|
||||
->where('invoice_payment_id', $invoicePaymentId)
|
||||
->whereNull('deleted_at') // hanya data aktif
|
||||
->pluck('claim_request_id')
|
||||
->map(fn($id) => (int)$id)
|
||||
->toArray();
|
||||
|
||||
$newClaims = $request->invoice_payment_details;
|
||||
// pastikan newClaims integer semua
|
||||
$newClaims = array_map('intval', $request->invoice_payment_details);
|
||||
|
||||
// Data yang mau di-insert
|
||||
|
||||
// =============================
|
||||
// INSERT DATA BARU
|
||||
// =============================
|
||||
$claimsToInsert = array_diff($newClaims, $existingClaims);
|
||||
foreach ($claimsToInsert as $claim) {
|
||||
DB::table('invoice_payment_details')->insert([
|
||||
'invoice_payment_id' => $invoicePaymentId,
|
||||
'claim_request_id' => $claim,
|
||||
'updated_by' => auth()->user()->id,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
if (!empty($claimsToInsert)) {
|
||||
foreach ($claimsToInsert as $claim) {
|
||||
DB::table('invoice_payment_details')->insert([
|
||||
'invoice_payment_id' => $invoicePaymentId,
|
||||
'claim_request_id' => $claim,
|
||||
'created_by' => auth()->id(),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Data yang mau di-delete (tidak ada di data baru)
|
||||
|
||||
// =============================
|
||||
// SOFT DELETE DATA YANG DIHAPUS
|
||||
// =============================
|
||||
$claimsToDelete = array_diff($existingClaims, $newClaims);
|
||||
|
||||
if (!empty($claimsToDelete)) {
|
||||
DB::table('invoice_payment_details')
|
||||
->where('invoice_payment_id', $invoicePaymentId)
|
||||
->whereIn('claim_request_id', $claimsToDelete)
|
||||
->whereNull('deleted_at') // penting supaya tidak over-update
|
||||
->update([
|
||||
'deleted_by' => auth()->user()->id,
|
||||
'deleted_by' => auth()->id(),
|
||||
'deleted_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Handle existing files
|
||||
$existingFiles = DB::table('files')
|
||||
->where('files.fileable_id', $invoicePaymentId)
|
||||
@@ -524,7 +543,22 @@ class InvoicePaymentController extends Controller
|
||||
return response()->json(['message' => 'Status berhasil diperbarui']);
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
public function export(Request $request, ExportExcelService $service)
|
||||
{
|
||||
\Log::info('EXPORT REQUEST', $request->all());
|
||||
$filters = [
|
||||
'search' => $request->input('search'),
|
||||
'start_date' => $request->input('start_date'),
|
||||
'end_date' => $request->input('end_date'),
|
||||
'orderBy' => $request->input('orderBy'),
|
||||
'order' => $request->input('order', 'asc'),
|
||||
];
|
||||
|
||||
return $service->exportReport($filters, 'Report-Vale-Payment.xlsx');
|
||||
|
||||
}
|
||||
|
||||
public function export3(Request $request)
|
||||
{
|
||||
$start_date = $request->input('start_date') ? $request->input('start_date') : 'all';
|
||||
$end_date = $request->input('end_date') ? $request->input('end_date') : 'all';
|
||||
|
||||
Reference in New Issue
Block a user