Ejemplo n.º 1
0
 public function staffPayAguinaldo()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('formData' => 'required', 'worker' => 'required'));
     if ($validator->fails()) {
         return response()->json(['error' => 'Informacion incompleta!']);
     }
     // Check that user is part of authorized staff.
     if (Auth::user()->Type != 1) {
         // If they are unauthorized no point in returning anything.
         return response()->json(array());
     }
     // Get the worker.
     $worker = Worker::where('Cedula', '=', Input::get('worker'))->first();
     $branchId = $worker->BranchId;
     $user = User::where('TypeId', '=', $worker->Id)->where('Type', '=', 1)->first();
     // Now get the cashbox.
     $cashbox = Cashbox::where('BranchId', '=', $branchId)->where('Close', '=', null)->where('UserId', '=', Auth::user()->Id)->first();
     if ($cashbox == null && Input::get('formData')['ppsPaymentSource'] == 1) {
         $response['state'] = 'Error';
         $response['error'] = 'No hay una caja abierta de la cual retirar los fondos!';
         return response()->json($response);
     }
     // Get the salary of the worker and his loans.
     $salaries = WorkerSalary::where('WorkerId', '=', $worker->Id)->where('AguinaldoPaid', '=', false)->get();
     $loans = WorkerLoan::where('WorkerId', '=', $worker->Id)->where('Processed', '=', false)->get();
     $totalAguinaldo = 0;
     $totalLoan = 0;
     $workedDays = 0;
     foreach ($salaries as $salary) {
         if ($salary->DayType != 3 || $salary->DayType != 5) {
             $totalAguinaldo += $worker->Basic * 0.2465;
             $workedDays++;
         }
         $salary->AguinaldoPaid = true;
         $salary->save();
     }
     foreach ($loans as $loan) {
         $totalLoan += $loan->Amount;
         $loan->Processed = true;
         $loan->save();
     }
     // Create workerpayment.
     $payment = WorkerPayment::create(array('WorkerId' => $worker->Id, 'Date' => date('Y-m-d'), 'WorkedDays' => $workedDays, 'VacationDays' => $worker->Vacation, 'TotalBasic' => 0, 'TotalBonus' => 0, 'TotalLoan' => $totalLoan, 'TotalInsurance' => 0, 'TotalAguinaldo' => $totalAguinaldo));
     if (Input::get('formData')['ppaPaymentSource'] == 1) {
         $transaction = CashboxTransaction::create(array('CashboxId' => $cashbox->Id, 'DateTime' => date('Y-m-d H:i:s'), 'Type' => 3, 'Amount' => $totalAguinaldo - $totalLoan, 'Reason' => 'Pago aguinaldo de ' . $worker->Name));
     } else {
         if (Input::get('formData')['ppsPaymentSource'] == 2) {
             // TODO: Add Bank Account transaction information.
         }
     }
     $worker->save();
     $response['state'] = 'Success';
     $response['paymentInfo'] = $payment;
     $response['worker'] = $worker;
     return response()->json($response);
 }