/**
  * Function that scans a sale and adds a bonus to selected worker if necessary.
  *
  * @return Response
  */
 public function scanSale()
 {
     // Validate Input.
     $validator = Validator::make(Input::all(), array('staffId' => 'required', 'saleId' => 'required'));
     if ($validator->fails()) {
         $response['state'] = 'Error';
         $response['error'] = 'Debe cargar al trabajador!';
         return response()->json($response);
     }
     // 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 of given Id.
     $response = array();
     $worker = Worker::where('Cedula', '=', Input::get('staffId'))->first();
     if ($worker) {
         // Check if this worker should get a bonus from this.
         if ($worker->BonusSource != 'productionexclusive' && $worker->BonusSource != 'productionsinglecustom') {
             $response['state'] = 'Error';
             $response['error'] = 'Este trabajador no puede ser asignado bonos por facturas.';
             return response()->json($response);
         }
         // Get today's day.
         $todaySalary = WorkerSalary::where('WorkerId', '=', $worker->Id)->where('Date', '=', date('Y-m-d'))->first();
         if (!$todaySalary) {
             $response['state'] = 'Error';
             $response['error'] = 'El trabajador no ha sido agregado en la planilla del dia de hoy!';
             return response()->json($response);
         }
         // Get the sale.
         $sale = Sale::find(Input::get('saleId'));
         if (!$sale) {
             $response['state'] = 'Error';
             $response['error'] = 'Venta Inexistente!';
             return response()->json($response);
         }
         // Get the breakdown.
         $breakdown = SaleBreakdown::where('SaleId', '=', $sale->Id)->get();
         foreach ($breakdown as $b) {
             // Check that this sale breakdown has not been used already.
             $extraData = json_decode($b->ExtraData, true);
             if (is_array($extraData) && array_key_exists('workerId', $extraData)) {
                 // Get the worker.
                 $worker = Worker::find($extraData['workerId']);
                 $response['state'] = 'Error';
                 $response['error'] = 'Esta factura ha sido asignada a ' . $worker->Name . '!';
                 return response()->json($response);
             }
             $product = Stock::where('Code', '=', $b->Code)->where('BranchId', '=', $sale->BranchId)->first();
             if ($product) {
                 $todaySalary->Bonus += $product->Bonus;
                 $todaySalary->save();
             } else {
                 $service = Service::where('Code', '=', $b->Code)->where('BranchId', '=', $sale->BranchId)->first();
                 $todaySalary->Bonus += $service->Bonus;
                 $todaySalary->save();
             }
             // Add extra data to breakdown.
             $extraData['workerId'] = $worker->Id;
             $b->ExtraData = json_encode($extraData);
             $b->save();
         }
         // Return response.
         $response['state'] = 'Success';
         $response['message'] = 'Factura agregada exitosamente!';
         return response()->json($response);
     } else {
         $response['state'] = 'Error';
         $response['error'] = 'Trabajador Inexistente!';
         return response()->json($response);
     }
 }
Exemple #2
0
 /**
  * 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);
 }