/** * Function that adds a loan to defined worker. * * @return Response */ public function loanStaff() { // Validate Input. $validator = Validator::make(Input::all(), array('worker' => 'required', 'formData' => 'required')); if ($validator->fails()) { // No reason why staffId and dayType would not be provided so return nothing. return response()->json(array()); } // 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()); } // Check if user has permission to give loans. $userToVerify = User::where('Username', '=', Input::get('formData')['plsusername'])->first(); if (!$userToVerify) { $response['state'] = 'Error'; $response['error'] = 'Este usuario no existe!'; return response()->json($response); } if (Auth::validate(array('Username' => Input::get('formData')['plsusername'], 'password' => Input::get('formData')['plspassword'] . $userToVerify->Salt, 'Type' => 1))) { // If user was verified make sure user has permission to withdraw money. $permissions = json_decode(UserLevel::find($userToVerify->UserLevel)->Permissions); if ($permissions->permissions->staff->loan->can != "true") { $response['state'] = 'Error'; $response['error'] = 'Este usuario no tiene permitido dar prestamos!'; return response()->json($response); } // Get the worker. $worker = Worker::where('Cedula', '=', Input::get('worker'))->first(); if (!$worker) { $response['state'] = 'Error'; $response['error'] = 'No se encontro el trabajador!'; return response()->json($response); } // Get the current cashbox. $cashbox = Cashbox::where('UserId', '=', Auth::user()->Id)->where('Close', '=', NULL)->first(); if (!$cashbox) { $response['state'] = 'Error'; $response['error'] = 'No hay una caja abierta de la cual se pueda retirar fondos!'; return response()->json($response); } // Now give em the loan. $loan = WorkerLoan::create(array('Date' => date('Y-m-d'), 'Amount' => Input::get('formData')['plsamount'], 'State' => 2, 'Type' => 1, 'Processed' => false, 'WorkerId' => $worker->Id)); // Make the transaction. $transaction = CashboxTransaction::create(array('CashboxId' => $cashbox->Id, 'DateTime' => date('Y-m-d H:i:s'), 'Type' => 3, 'Amount' => Input::get('formData')['plsamount'], 'Reason' => 'Prestamo a ' . $worker->Name)); $response['state'] = 'Success'; $response['message'] = 'El prestamo fue realizado exitosamente!'; return response()->json($response); } else { $response['state'] = 'Error'; $response['error'] = 'Este usuario o contraseƱa incorrectos!'; return response()->json($response); } }
/** * Execute the console command. * * @return mixed */ public function handle() { // Get all workers that haven't been fired. $workers = Worker::where('Fired', '=', false)->get(); foreach ($workers as $worker) { // First let's check if it has a long term loan active. $currentMonth = date('Y-m'); if ($worker->Loan > 0) { // Let's check if we have gotten this months loan payment. $loans = WorkerLoan::where('WorkerId', '=', $worker->Id)->where('Date', '>', $currentMonth . '-01')->where('Type', '=', 2)->get(); if (count($loans) == 0) { // Charge worker for loan. $charge = $worker->Loan / $worker->Months; $loan = WorkerLoan::create(array('Date' => date('Y-m-d'), 'Amount' => $charge, 'Processed' => false, 'State' => 2, 'Type' => 2, 'WorkerId' => $worker->Id)); $worker->Loan -= $charge; $worker->Months--; $worker->save(); } } // Now let's check if we have gotten this month's insurance fee. $loans = WorkerLoan::where('WorkerId', '=', $worker->Id)->where('Date', '>', $currentMonth . '-01')->where('Type', '=', 3)->get(); if (count($loans) == 0) { // Charge worker for loan. $charge = $worker->Insurance - $worker->Patron; if ($charge > 0) { $loan = WorkerLoan::create(array('Date' => date('Y-m-d'), 'Amount' => $charge, 'Processed' => false, 'State' => 2, 'Type' => 3, 'WorkerId' => $worker->Id)); } } // Get workers days. $days = WorkerSalary::where('SystemProcess', '=', false)->where('WorkerId', '=', $worker->Id)->get(); // Now let's check if we need to add a bonus to the worker. if ($worker->BonusPercentage > 0) { switch ($worker->BonusSource) { case 'production': foreach ($days as $day) { if ($day->DayType != 3 && ($day->DayType < 5 || $day->DayType == 7)) { // If worker is on general production then let's get what was produced and the price of what was produced. $produced = ProductionStage::where('Date', '>', $day->Date . ' 00:00:01')->get(); $totalProduced = 0; $pIds = array(); foreach ($produced as $p) { // Check if we have already checked this product. if (!in_array($p->ProductionId, $pIds)) { array_push($pIds, $p->ProductionId); // Check if product is finished. $product = Production::find($p->ProductionId); if ($product->Stage == $product->maxStage()) { $finalProduct = Stock::where('Code', '=', $product->Code)->where('BranchId', '=', $product->BranchId)->first(); $totalProduced += $finalProduct->Price; } } } $day->Bonus += $totalProduced * ($worker->BonusPercentage / 100); } // Add insurance Patron Value for the day. // TODO: I should check how many days are in this month, $day->Insurance = $worker->Patron / 30; $day->save(); } break; case 'productionsingle': foreach ($days as $day) { if ($day->DayType != 3 && ($day->DayType < 5 || $day->DayType == 7)) { // If worker is on single production then let's get what he produced and the price of what was produced. $produced = ProductionStage::where('Date', '>', $day->Date . ' 00:00:01')->where('WorkerId', '=', $worker->Id)->get(); $totalProduced = 0; $pIds = array(); foreach ($produced as $p) { // Check if we have already checked this product. if (!in_array($p->ProductionId, $pIds)) { array_push($pIds, $p->ProductionId); // Check if product is finished. $product = Production::find($p->ProductionId); if ($product->Stage == $product->maxStage()) { $finalProduct = Stock::where('Code', '=', $product->Code)->where('BranchId', '=', $product->BranchId)->first(); $totalProduced += $finalProduct->Price; } } } $day->Bonus += $totalProduced * ($worker->BonusPercentage / 100); } // Add insurance Patron Value for the day. // TODO: I should check how many days are in this month, $day->Insurance = $worker->Patron / 30; $day->save(); } break; case 'sales': foreach ($days as $day) { if ($day->DayType != 3 && ($day->DayType < 5 || $day->DayType == 7)) { // If worker is on general sales then let's get all sales. $sales = Sale::where('Created', '>=', $day->Date . ' 00:00:01')->where('Created', '<=', $day->Date . ' 23:59:59')->where('BranchId', '=', $worker->BranchId)->get(); $totalSales = 0; foreach ($sales as $sale) { $totalSales += $sale->Value; } $day->Bonus += $totalSales * ($worker->BonusPercentage / 100); } // Add insurance Patron Value for the day. // TODO: I should check how many days are in this month, $day->Insurance = $worker->Patron / 30; $day->save(); } break; case 'salessingle': foreach ($days as $day) { if ($day->DayType != 3 && ($day->DayType < 5 || $day->DayType == 7)) { // If worker is on general sales then let's get all sales. $sales = Sale::where('Created', '>=', $day->Date . ' 00:00:01')->where('Created', '<=', $day->Date . ' 23:59:59')->where('WorkerId', '=', $worker->Id)->get(); $totalSales = 0; foreach ($sales as $sale) { $totalSales += $sale->Value; } $day->Bonus += $totalSales * ($worker->BonusPercentage / 100); } // Add insurance Patron Value for the day. // TODO: I should check how many days are in this month, $day->Insurance = $worker->Patron / 30; $day->save(); } break; } } } // Get all days that have not been processed and set them as being processed. $allDays = WorkerSalary::where('SystemProcess', '=', false)->get(); foreach ($allDays as $day) { $day->SystemProcess = true; $day->save(); } }
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); }