From 747836be91653f39348c8f110400e58b8a1b6419 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 22 Jun 2026 12:26:38 +0700 Subject: [PATCH] refactor: replace individual attribute assignments and save calls with mass update() calls across service classes --- app/Services/Account/ProfileService.php | 12 +++--- .../Finance/EmployeeAdvanceService.php | 40 ++++++++++--------- app/Services/Finance/ExpenseService.php | 12 +++--- app/Services/Hr/AttendanceService.php | 11 ++--- app/Services/Hr/EmployeeService.php | 26 +++++++----- app/Services/Hr/LeaveRequestService.php | 27 +++++++------ app/Services/Manage/PurchaseService.php | 20 +++++----- app/Services/Master/CategoryService.php | 2 +- app/Services/Master/CustomerService.php | 2 +- app/Services/Master/ProductService.php | 19 +++++---- app/Services/Master/RawMaterialService.php | 19 +++++---- app/Services/Master/SupplierService.php | 2 +- app/Services/System/RoleService.php | 5 ++- 13 files changed, 111 insertions(+), 86 deletions(-) diff --git a/app/Services/Account/ProfileService.php b/app/Services/Account/ProfileService.php index 976dd1b..d3dbcbf 100644 --- a/app/Services/Account/ProfileService.php +++ b/app/Services/Account/ProfileService.php @@ -14,9 +14,10 @@ class ProfileService public function update(array $validated, User $user): void { DB::transaction(function () use ($user, $validated): void { - $user->email = $validated['email']; - $user->username = $validated['username']; - $user->save(); + $user->update([ + 'email' => $validated['email'], + 'username' => $validated['username'], + ]); $user->profile()->updateOrCreate( ['user_id' => $user->id], @@ -36,7 +37,8 @@ public function update(array $validated, User $user): void */ public function updatePassword(User $user, string $password): void { - $user->password = Hash::make($password); - $user->save(); + $user->update([ + 'password' => Hash::make($password), + ]); } } diff --git a/app/Services/Finance/EmployeeAdvanceService.php b/app/Services/Finance/EmployeeAdvanceService.php index 1732a14..c45161a 100644 --- a/app/Services/Finance/EmployeeAdvanceService.php +++ b/app/Services/Finance/EmployeeAdvanceService.php @@ -100,10 +100,11 @@ public function create(array $validated, User $user): void public function update(EmployeeAdvance $employeeAdvance, array $validated, User $user): void { DB::transaction(function () use ($employeeAdvance, $validated): void { - $employeeAdvance->amount = (int) $validated['amount']; - $employeeAdvance->description = $validated['description']; - $employeeAdvance->due_date = $validated['due_date']; - $employeeAdvance->save(); + $employeeAdvance->update([ + 'amount' => (int) $validated['amount'], + 'description' => $validated['description'], + 'due_date' => $validated['due_date'], + ]); }); $this->pushNotificationService->sendToRoles( @@ -146,11 +147,12 @@ public function approve(EmployeeAdvance $employeeAdvance, User $user): void $user, ); - $employeeAdvance->cash_transaction_id = $cashTransaction->id; - $employeeAdvance->status = EmployeeAdvanceStatus::APPROVED; - $employeeAdvance->verified_at = now(); - $employeeAdvance->verified_by_id = $user->id; - $employeeAdvance->save(); + $employeeAdvance->update([ + 'cash_transaction_id' => $cashTransaction->id, + 'status' => EmployeeAdvanceStatus::APPROVED, + 'verified_at' => now(), + 'verified_by_id' => $user->id, + ]); }); $employeeAdvance->loadMissing('employee.user'); @@ -167,10 +169,11 @@ public function approve(EmployeeAdvance $employeeAdvance, User $user): void public function reject(EmployeeAdvance $employeeAdvance, string $reason, User $user): void { DB::transaction(function () use ($employeeAdvance, $user, $reason): void { - $employeeAdvance->status = EmployeeAdvanceStatus::REJECTED; - $employeeAdvance->verified_at = now(); - $employeeAdvance->verified_by_id = $user->id; - $employeeAdvance->save(); + $employeeAdvance->update([ + 'status' => EmployeeAdvanceStatus::REJECTED, + 'verified_at' => now(), + 'verified_by_id' => $user->id, + ]); $employeeAdvance->rejection()->create([ 'reason' => $reason, @@ -206,11 +209,12 @@ public function pay(EmployeeAdvance $employeeAdvance, User $user): void $user, ); - $employeeAdvance->repayment_cash_transaction_id = $cashTransaction->id; - $employeeAdvance->paid_at = now(); - $employeeAdvance->paid_by_id = $user->id; - $employeeAdvance->status = EmployeeAdvanceStatus::PAID; - $employeeAdvance->save(); + $employeeAdvance->update([ + 'repayment_cash_transaction_id' => $cashTransaction->id, + 'paid_at' => now(), + 'paid_by_id' => $user->id, + 'status' => EmployeeAdvanceStatus::PAID, + ]); }); } diff --git a/app/Services/Finance/ExpenseService.php b/app/Services/Finance/ExpenseService.php index 369adfe..98be368 100644 --- a/app/Services/Finance/ExpenseService.php +++ b/app/Services/Finance/ExpenseService.php @@ -72,8 +72,9 @@ public function create(array $validated, User $user): void $user, ); - $expense->cash_transaction_id = $cashTransaction->id; - $expense->save(); + $expense->update([ + 'cash_transaction_id' => $cashTransaction->id, + ]); $this->syncPhotos($expense, $validated); @@ -97,9 +98,10 @@ public function update(Expense $expense, array $validated): void $amount = (int) $validated['amount']; $description = $validated['description']; - $expense->amount = $amount; - $expense->description = $description; - $expense->save(); + $expense->update([ + 'amount' => $amount, + 'description' => $description, + ]); if ($expense->cashTransaction) { $this->cashService->updateReferencedTransaction( diff --git a/app/Services/Hr/AttendanceService.php b/app/Services/Hr/AttendanceService.php index 3e3b71f..9e5671e 100644 --- a/app/Services/Hr/AttendanceService.php +++ b/app/Services/Hr/AttendanceService.php @@ -135,11 +135,12 @@ public function checkOut(array $validated, User $user): void $checkOutAt = now(); $workDurationMinutes = (int) $attendance->check_in_at->diffInMinutes($checkOutAt); - $attendance->check_out_at = $checkOutAt; - $attendance->check_out_latitude = $validated['latitude']; - $attendance->check_out_longitude = $validated['longitude']; - $attendance->work_duration_minutes = $workDurationMinutes; - $attendance->save(); + $attendance->update([ + 'check_out_at' => $checkOutAt, + 'check_out_latitude' => $validated['latitude'], + 'check_out_longitude' => $validated['longitude'], + 'work_duration_minutes' => $workDurationMinutes, + ]); $this->mediaService->addBase64Image( $attendance, diff --git a/app/Services/Hr/EmployeeService.php b/app/Services/Hr/EmployeeService.php index df718d8..a9ed757 100644 --- a/app/Services/Hr/EmployeeService.php +++ b/app/Services/Hr/EmployeeService.php @@ -92,9 +92,10 @@ public function update(User $user, array $validated): void $employee = $user->employee; DB::transaction(function () use ($validated, $user, $employee): void { - $user->email = $validated['email']; - $user->username = $validated['username']; - $user->save(); + $user->update([ + 'email' => $validated['email'], + 'username' => $validated['username'], + ]); $user->profile()->updateOrCreate( ['user_id' => $user->id], @@ -111,10 +112,11 @@ public function update(User $user, array $validated): void if ($hasEmployee) { if ($employee) { - $employee->join_date = $validated['join_date']; - $employee->employment_status = $validated['employment_status']; - $employee->base_salary = $validated['base_salary']; - $employee->save(); + $employee->update([ + 'join_date' => $validated['join_date'], + 'employment_status' => $validated['employment_status'], + 'base_salary' => $validated['base_salary'], + ]); } else { Employee::create([ 'user_id' => $user->id, @@ -135,8 +137,9 @@ public function update(User $user, array $validated): void public function toggleStatus(User $user, array $validated): void { - $user->is_active = $validated['is_active']; - $user->save(); + $user->update([ + 'is_active' => $validated['is_active'], + ]); if (! $validated['is_active']) { DB::table('sessions')->where('user_id', $user->id)->delete(); @@ -145,8 +148,9 @@ public function toggleStatus(User $user, array $validated): void public function resetPassword(User $user): void { - $user->password = config('auth.password_default'); - $user->save(); + $user->update([ + 'password' => config('auth.password_default'), + ]); DB::table('sessions')->where('user_id', $user->id)->delete(); } diff --git a/app/Services/Hr/LeaveRequestService.php b/app/Services/Hr/LeaveRequestService.php index 9a4cab2..53894c3 100644 --- a/app/Services/Hr/LeaveRequestService.php +++ b/app/Services/Hr/LeaveRequestService.php @@ -96,10 +96,11 @@ public function update(LeaveRequest $leaveRequest, array $validated, User $user) $this->ensureMinimumLeadTime($startDate); - $leaveRequest->start_date = $startDate; - $leaveRequest->end_date = $endDate; - $leaveRequest->total_days = $this->calculateTotalDays($startDate, $endDate); - $leaveRequest->save(); + $leaveRequest->update([ + 'start_date' => $startDate, + 'end_date' => $endDate, + 'total_days' => $this->calculateTotalDays($startDate, $endDate), + ]); $this->pushNotificationService->sendToRoles( '✏️ Pengajuan Cuti Diperbarui', @@ -127,10 +128,11 @@ public function delete(LeaveRequest $leaveRequest): void public function approve(LeaveRequest $leaveRequest, User $user): void { DB::transaction(function () use ($leaveRequest, $user): void { - $leaveRequest->status = LeaveRequestStatus::APPROVED; - $leaveRequest->verified_at = Carbon::now(); - $leaveRequest->verified_by_id = $user->id; - $leaveRequest->save(); + $leaveRequest->update([ + 'status' => LeaveRequestStatus::APPROVED, + 'verified_at' => Carbon::now(), + 'verified_by_id' => $user->id, + ]); }); $leaveRequest->loadMissing('employee.user'); @@ -147,10 +149,11 @@ public function approve(LeaveRequest $leaveRequest, User $user): void public function reject(LeaveRequest $leaveRequest, string $reason, User $user): void { DB::transaction(function () use ($leaveRequest, $user, $reason): void { - $leaveRequest->status = LeaveRequestStatus::REJECTED; - $leaveRequest->verified_at = Carbon::now(); - $leaveRequest->verified_by_id = $user->id; - $leaveRequest->save(); + $leaveRequest->update([ + 'status' => LeaveRequestStatus::REJECTED, + 'verified_at' => Carbon::now(), + 'verified_by_id' => $user->id, + ]); $leaveRequest->rejection()->create([ 'reason' => $reason, diff --git a/app/Services/Manage/PurchaseService.php b/app/Services/Manage/PurchaseService.php index dffc5c7..3bf45fc 100644 --- a/app/Services/Manage/PurchaseService.php +++ b/app/Services/Manage/PurchaseService.php @@ -237,8 +237,9 @@ public function create(array $validated, User $user): Purchase ]); foreach ($draftItems as $item) { - $item->purchase_id = $purchase->id; - $item->save(); + $item->update([ + 'purchase_id' => $purchase->id, + ]); $this->incrementStock($item); } @@ -278,13 +279,14 @@ public function update(Purchase $purchase, array $validated): void $shippingCost = (int) ($validated['shipping_cost'] ?? 0); $total = max($subtotal - $discount + $shippingCost, 0); - $purchase->supplier_id = $validated['supplier_id']; - $purchase->subtotal = $subtotal; - $purchase->discount = $discount; - $purchase->shipping_cost = $shippingCost; - $purchase->total = $total; - $purchase->notes = $validated['notes'] ?? null; - $purchase->save(); + $purchase->update([ + 'supplier_id' => $validated['supplier_id'], + 'subtotal' => $subtotal, + 'discount' => $discount, + 'shipping_cost' => $shippingCost, + 'total' => $total, + 'notes' => $validated['notes'] ?? null, + ]); foreach ($lineItems as $itemData) { $purchaseItem = $purchase->items()->create($itemData); diff --git a/app/Services/Master/CategoryService.php b/app/Services/Master/CategoryService.php index dd5b909..9713352 100644 --- a/app/Services/Master/CategoryService.php +++ b/app/Services/Master/CategoryService.php @@ -41,7 +41,7 @@ public function create(array $validated): void */ public function update(Category $category, array $validated): void { - $category->fill($validated)->save(); + $category->update($validated); } public function delete(Category $category): void diff --git a/app/Services/Master/CustomerService.php b/app/Services/Master/CustomerService.php index 5fb7a4e..9a13084 100644 --- a/app/Services/Master/CustomerService.php +++ b/app/Services/Master/CustomerService.php @@ -43,7 +43,7 @@ public function create(array $validated): void */ public function update(Customer $customer, array $validated): void { - $customer->fill($validated)->save(); + $customer->update($validated); } public function delete(Customer $customer): void diff --git a/app/Services/Master/ProductService.php b/app/Services/Master/ProductService.php index 351b5cb..d4668e8 100644 --- a/app/Services/Master/ProductService.php +++ b/app/Services/Master/ProductService.php @@ -94,9 +94,10 @@ public function create(array $validated): void public function update(Product $product, array $validated): void { DB::transaction(function () use ($validated, $product): void { - $product->name = $validated['name']; - $product->description = $validated['description'] ?? null; - $product->save(); + $product->update([ + 'name' => $validated['name'], + 'description' => $validated['description'] ?? null, + ]); $product->categories()->sync($validated['category_ids']); @@ -117,9 +118,10 @@ public function update(Product $product, array $validated): void foreach ($validated['variants'] as $index => $variantData) { if (! empty($variantData['id'])) { $variant = $product->variants()->findOrFail($variantData['id']); - $variant->name = $variantData['name']; - $variant->stock = $variantData['stock']; - $variant->save(); + $variant->update([ + 'name' => $variantData['name'], + 'stock' => $variantData['stock'], + ]); $this->syncVariantImages($variant, $variantData, $index); continue; @@ -132,8 +134,9 @@ public function update(Product $product, array $validated): void public function toggleStatus(Product $product, array $validated): void { - $product->is_active = $validated['is_active']; - $product->save(); + $product->update([ + 'is_active' => $validated['is_active'], + ]); } public function delete(Product $product): void diff --git a/app/Services/Master/RawMaterialService.php b/app/Services/Master/RawMaterialService.php index 1505d59..aec1b2f 100644 --- a/app/Services/Master/RawMaterialService.php +++ b/app/Services/Master/RawMaterialService.php @@ -94,8 +94,9 @@ public function create(array $validated): void public function update(RawMaterial $rawMaterial, array $validated): void { DB::transaction(function () use ($validated, $rawMaterial): void { - $rawMaterial->name = $validated['name']; - $rawMaterial->save(); + $rawMaterial->update([ + 'name' => $validated['name'], + ]); $submittedPriceIds = collect($validated['prices']) ->pluck('id') @@ -114,10 +115,11 @@ public function update(RawMaterial $rawMaterial, array $validated): void foreach ($validated['prices'] as $index => $priceData) { if (! empty($priceData['id'])) { $price = $rawMaterial->prices()->findOrFail($priceData['id']); - $price->variant = $priceData['variant']; - $price->price = $priceData['price']; - $price->stock = $priceData['stock']; - $price->save(); + $price->update([ + 'variant' => $priceData['variant'], + 'price' => $priceData['price'], + 'stock' => $priceData['stock'], + ]); $this->syncPriceImages($price, $priceData, $index); continue; @@ -130,8 +132,9 @@ public function update(RawMaterial $rawMaterial, array $validated): void public function toggleStatus(RawMaterial $rawMaterial, array $validated): void { - $rawMaterial->is_active = $validated['is_active']; - $rawMaterial->save(); + $rawMaterial->update([ + 'is_active' => $validated['is_active'], + ]); } public function delete(RawMaterial $rawMaterial): void diff --git a/app/Services/Master/SupplierService.php b/app/Services/Master/SupplierService.php index b7b3bef..a236b27 100644 --- a/app/Services/Master/SupplierService.php +++ b/app/Services/Master/SupplierService.php @@ -43,7 +43,7 @@ public function create(array $validated): void */ public function update(Supplier $supplier, array $validated): void { - $supplier->fill($validated)->save(); + $supplier->update($validated); } public function delete(Supplier $supplier): void diff --git a/app/Services/System/RoleService.php b/app/Services/System/RoleService.php index 2f9a89d..9a91a6b 100644 --- a/app/Services/System/RoleService.php +++ b/app/Services/System/RoleService.php @@ -56,10 +56,11 @@ public function update(Role $role, array $validated): void { DB::transaction(function () use ($role, $validated): void { // Protect developer and owner names from changing + $updateData = []; if (! in_array($role->name, [EnumsRole::DEVELOPER->value, EnumsRole::OWNER->value], true)) { - $role->name = Str::slug($validated['name']); + $updateData['name'] = Str::slug($validated['name']); } - $role->save(); + $role->update($updateData); $role->syncPermissions($validated['permissions'] ?? []); });