where('business_id', auth()->user()->business_id)->latest()->get(); return response()->json([ 'message' => __('Data fetched successfully.'), 'data' => $payrolls, ]); } public function show($id) { $data = Payroll::with(['employee:id,name', 'branch:id,name', 'transactions:id,platform,transaction_type,amount,date,invoice_no,reference_id,payment_type_id,meta', 'transactions.paymentType:id,name'])->where('business_id', auth()->user()->business_id)->find($id); return response()->json([ 'message' => __('Data fetched successfully.'), 'data' => $data, ]); } public function store(Request $request) { $request->validate([ 'employee_id' => 'required|integer|exists:employees,id', 'month' => 'required|string', 'date' => 'nullable|date', 'note' => 'nullable|string', 'payemnt_year' => 'required|string', ]); $exists = Payroll::where('employee_id', $request->employee_id) ->where('month', $request->month) ->where('payemnt_year', $request->payemnt_year) ->where('business_id', auth()->user()->business_id) ->exists(); if ($exists) { return response()->json([ 'message' => __('This employee has already been paid for this month and year.'), ], 422); } $employee = Employee::where('business_id', auth()->user()->business_id)->findOrFail($request->employee_id); $data = Payroll::create($request->except(['business_id', 'amount']) + [ 'business_id' => auth()->user()->business_id, 'amount' => $employee->amount, ]); // Handle payments $payments = collect($request->payments ?? [])->map(function ($payment) use ($employee) { $payment['amount'] = $employee->amount; return $payment; })->toArray(); // Trigger multipayment event event(new MultiPaymentProcessed( $payments, $data->id, 'payroll', $employee->amount ?? 0, )); return response()->json([ 'message' => __('Data saved successfully.'), 'data' => $data, ]); } public function update(Request $request, string $id) { $request->validate([ 'employee_id' => 'required|integer|exists:employees,id', 'date' => 'nullable|date', 'month' => 'required|string', 'note' => 'nullable|string', 'payemnt_year' => 'required|string', ]); $Payroll = Payroll::find($id); if (!$Payroll) { return response()->json([ 'message' => __('Data not found.'), ], 404); } $exists = Payroll::where('employee_id', $request->employee_id) ->where('month', $request->month) ->where('payemnt_year', $request->payemnt_year) ->where('business_id', auth()->user()->business_id) ->where('id', '!=', $id) ->exists(); if ($exists) { return response()->json([ 'message' => __('This employee has already been paid for this month and year.'), ], 422); } $Payroll->update($request->except(['business_id', 'amount'])); return response()->json([ 'message' => __('Data update successfully.'), 'data' => $Payroll, ]); } public function destroy(string $id) { $Payroll = Payroll::where('business_id', auth()->user()->business_id)->find($id); if (!$Payroll) { return response()->json([ 'message' => __('Data not found.'), ], 404); } $Payroll->delete(); return response()->json([ 'message' => __('Data deleted successfully.'), ]); } }