getLessDate(), $reportCoaFilterAttr->getBetweenTwoDates(), "payment_date" ); return $payment->sum("amount"); } public function sumTotalMade(?ReportCoaFilterAttr $reportCoaFilterAttr) { $payment = lessAndBetweenDateQuery( Payment::whereIn('payment_for', ['po', 'bill']), $reportCoaFilterAttr ? $reportCoaFilterAttr->getLessDate() : null, $reportCoaFilterAttr ? $reportCoaFilterAttr->getBetweenTwoDates() : null, "payment_date" ); return $payment->sum("amount"); } public function sumTotalReceived(?ReportCoaFilterAttr $reportCoaFilterAttr) { $payment = lessAndBetweenDateQuery( Payment::whereIn('payment_for', ['so', 'invoice', 'retainer_invoice']), $reportCoaFilterAttr ? $reportCoaFilterAttr->getLessDate() : null, $reportCoaFilterAttr ? $reportCoaFilterAttr->getBetweenTwoDates() : null, "payment_date" ); return $payment->sum("amount"); } public function setTotalAmount($id, $total) { return Payment::where('id', $id) ->update([ 'amount' => numberOnly($total) ]); } public function syncRetainerInvoices($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getRetainerInvoiceId", "retainerInvoices"); } public function syncInvoices($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getInvoiceId", "invoices"); } public function syncSaleOrders($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getSaleOrderId", "saleOrders"); } public function syncBills($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getBillId", "bills"); } public function syncPurchaseOrders($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getPurchaseOrderId", "purchaseOrders"); } public function syncLabourOrders($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getLabourOrderId", "labourOrders"); } public function syncBillLo($id, array $paymentDetailAttrs) { return $this->syncDetails($id, $paymentDetailAttrs, "getBillLoId", "billLo"); } private function syncDetails($id, $paymentDetailAttrs, $getterFn, $relationFn) { $payment = $this->fetch($id); $pivots = []; foreach ($paymentDetailAttrs as $paymentDetailAttr) { $pivots[$paymentDetailAttr->{$getterFn}()] = [ 'payment_amount' => numberOnly($paymentDetailAttr->getPaymentAmount()), ]; if ($getterFn === "getInvoiceId") { if ($kafdigStatus = KafdigStatus::where('id', 4)->first()) { if ($invoice = Invoice::find($paymentDetailAttr->{$getterFn}()) and $invoice->deal) { $invoice->deal->budgetPlan->order->kafdig_status_id = $kafdigStatus->id; $invoice->deal->budgetPlan->order->save(); } } } } $payment->{$relationFn}()->sync($pivots); return Payment::with($relationFn)->find($id); } public function getRandomId($limit = 10) { $payment = Payment::limit($limit)->inRandomOrder(); return $payment->first()->id ?? null; } public function paginateReceiveType(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { // dd("CADUK"); $paymentFor = ['retainer_invoice', 'invoice', 'so']; $payment = Payment::whereIn('payment_for', $paymentFor); return (new PaginateCollectionAttrBuilder) ->build( $payment->with('customer', 'chartOfAccount'), $paginateBuilderAttr, new PaymentReceive ); } public function paginateMadeType(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { $payment = Payment::where(function($e){ $e->has('purchaseOrders') ->orHas('labourOrders') ->orHas('bills') ->orHas('billLo'); }) ->with('project.deal'); return (new PaginateCollectionAttrBuilder) ->build( $payment->with('supplier', 'chartOfAccount'), $paginateBuilderAttr, new PaymentMade ); } public function fetch($id) { return Payment::with( 'taxDeducated', 'customer', 'supplier', 'saleOrders', 'retainerInvoices', 'invoices', 'purchaseOrders', 'bills.purchaseOrder', 'labourOrders', 'billLo.labourOrder', 'supplier', 'chartOfAccount', 'project.deal' )->findOrFail($id); } public function createActualCashIn($data) { $deal = $this->findDealByCustomerId($data->customer_id); $project = $this->findProjectByCustomerAndSkk($data->customer_id, $deal->skk_number); $totalCashInActual = $this->getTotalCashIn($data->customer_id, $data->payment_date); $endDate = $project->extend_date ?? $project->end_date; $weekDates = $this->getTotalWeekDates($project->start_date, $endDate); $weekPosition = $this->getWeekPosition($weekDates, $data->payment_date); // $find = ProjectCashPlan::where('project_id')->where('week', $weekPosition['week'])->first(); $find = ProjectCashPlan::where('project_id')->where('week', $weekPosition)->first(); if ($find) { $totalCashInActual = $totalCashInActual + $find->cash_in_actual; } $payment = ProjectCashPlan::updateOrCreate( [ "project_id" => $project->id, "week" => $weekPosition, ], [ "cash_in_actual" => $totalCashInActual, ] ); return $payment; } public function findDealByCustomerId($customer_id) { return Deal::select('skk_number')->where('customer_id', $customer_id)->first(); } public function findProjectByCustomerAndSkk($customer_id, $skk_number) { return Project::whereHas('deal', function ($q) use ($customer_id, $skk_number) { $q->where('customer_id', $customer_id)->where('skk_number', $skk_number); })->first(); } public function getTotalCashIn($customer_id, $payment_date) { return Payment::whereIn('payment_for', ['so', 'invoice', 'retainer_invoice']) ->where('customer_id', $customer_id) ->where('payment_date', $payment_date) ->sum('amount'); } public function getTotalWeekDates($startDate, $endDate) { $startDate = Carbon::parse($startDate); $endDate = Carbon::parse($endDate); $dates = []; foreach ($startDate->toPeriod($endDate, '1 week') as $week_start) { $week_dates = []; foreach ($week_start->daysUntil($week_start->copy()->addDays(6)) as $day) { $week_dates[] = $day->toDateString(); } $dates[] = $week_dates; } return $dates; } public function getWeekPosition($dates, $payment_date) { $paymentDate = Carbon::parse($payment_date); $weekPosition = null; $countWeek = count($dates); foreach ($dates as $index => $week) { // Jika $paymentDate kurang dari tanggal dalam week index pertama if ($index === 0 && $paymentDate->lessThan(Carbon::parse($week[0]))) { $weekPosition = 1; break; } // Jika $paymentDate melebihi dari tanggal dalam week terakhir if ($index === ($countWeek - 1) && $paymentDate->greaterThan(Carbon::parse(end($week)))) { $weekPosition = $countWeek; break; } if (in_array($paymentDate->toDateString(), $week)) { $weekPosition = $index + 1; break; } } return $weekPosition; } public function create(PaymentAttr $paymentAttr) { $payment = Payment::create([ "payment_for" => $paymentAttr->getPaymentFor(), "supplier_id" => $paymentAttr->getSupplierId(), "customer_id" => $paymentAttr->getCustomerId(), "project_id" => $paymentAttr->getProjectId(), "chart_of_account_id" => $paymentAttr->getChartOfAccountId(), "payment_date" => formatDate($paymentAttr->getPaymentDate()), "reference" => $paymentAttr->getReference(), "notes" => $paymentAttr->getNotes(), "amount" => numberOnly($paymentAttr->getAmount()), "company_prefix" => $paymentAttr->getCompanyPrefix(), "company_name" => $paymentAttr->getCompanyName() ]); return $this->fetch($payment->id); } public function update(PaymentAttr $paymentAttr, $id) { $payment = $this->fetch($id); $payment->update([ "payment_for" => $paymentAttr->getPaymentFor(), "supplier_id" => $paymentAttr->getSupplierId(), "customer_id" => $paymentAttr->getCustomerId(), "project_id" => $paymentAttr->getProjectId(), "chart_of_account_id" => $paymentAttr->getChartOfAccountId(), "payment_date" => formatDate($paymentAttr->getPaymentDate()), "reference" => $paymentAttr->getReference(), "notes" => $paymentAttr->getNotes(), "amount" => numberOnly($paymentAttr->getAmount()), "company_prefix" => $paymentAttr->getCompanyPrefix(), "company_name" => $paymentAttr->getCompanyName() ]); return $this->fetch($id); } public function delete($id) { $payment = $this->fetch($id); $payment->delete(); return $payment; } }