/** * 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(); } }
/** * Function that gets order Information. * * @return Response */ public function addReturn() { // Validate Input. $validator = Validator::make(Input::all(), array('code' => 'required', 'worker' => 'required')); if ($validator->fails()) { return response()->json(['error' => 'El codigo del ataud es necesario!']); } // 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 Item in question. $item = Production::find(Input::get('code')); if (!$item) { return response()->json(['error' => 'No existe un ataud con este codigo!']); } // Make sure item has finished production. $maxStage = $item->maxStage(); if ($item->Stage <= $maxStage) { return response()->json(['error' => 'Este ataud no ha salido del proceso de produccion!']); } // Verify that worker exists and is of correct branch. $worker = Worker::find(Input::get('worker')); if (!$worker) { return response()->json(['error' => 'Este trabajador no existe']); } if ($worker->BranchId != 2) { return response()->json(['error' => 'Este trabajador no es parte de la sucursal correcta!']); } // Set item as being repaired. $item->Stage = 0; $item->save(); // Create Repair Stage for item. $itemStage = ProductionStage::create(array('ProductionId' => $item->Id, 'Stage' => 0, 'WorkerId' => $worker->Id, 'Materials' => '{}', 'Date' => date('Y-m-d'))); // Return information. $response['success'] = 'Devolucion hecha exitosamente!'; return response()->json($response); }
/** * Function that updates record of specified item. * * @return Response */ public function updateItemProduction() { // Validate Input. $validator = Validator::make(Input::all(), array('code' => 'required', 'productCode' => 'required|integer', 'worker' => 'required|integer', 'stage' => 'required|integer')); if ($validator->fails()) { return response()->json(['error' => 'All fields are required!']); } // 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 Item in production. $item = Production::find(Input::get('productCode')); if (!$item) { return response()->json(['error' => 'No existe un ataud con este codigo!']); } // Make sure that item is still in production. $maxStage = $item->maxStage(); if ($item->Stage > $maxStage) { return response()->json(['error' => 'Este ataud ya no esta en produccion!']); } // Now check if we are changing the stage of the item. $itemStage = $item->currentStage(); if (Input::get('stage') != $item->Stage) { // Check if they are trying to take it down a stage. if (Input::get('stage') < $item->Stage) { return response()->json(['error' => 'No puede retroceder un ataud en produccion!']); } // Get current worker of current stage. $worker = Worker::find($itemStage->WorkerId); // Check that he is working today. $salary = $worker->getDateSalary(date('Y-m-d')); if (!$salary) { return response()->json(['error' => "El trabajador asignado para la etapa {$item->Stage} no ha sido agregado a la asistencia de hoy!"]); } // Get the value of the bono. $expense = $item->currentExpense(); $salary->Bonus += $expense->Bonus; $salary->save(); // Now update the stage of item. $item->Stage = Input::get('stage'); $item->save(); // Create new item Stage. $itemStage = ProductionStage::create(array('ProductionId' => $item->Id, 'Stage' => Input::get('stage'), 'WorkerId' => Input::get('worker'), 'Materials' => '{}', 'Date' => date('Y-m-d H:i:s'))); } // Go ahead and save the item. $itemStage->WorkerId = Input::get('worker'); $itemStage->save(); // Inform user. return response()->json(['success' => 'Cambios guardados exitosamente!']); }
use App\User; use App\Worker; use App\Branch; use App\UserLevel; use App\Notification; use App\StorageRequest; use App\Production; use App\ProductionStage; use App\Stock; $currentNotification = Notification::find($notification); $currentNotification->Seen = true; $currentNotification->save(); $permissions = json_decode(UserLevel::find(Auth::user()->UserLevel)->Permissions); $production = Production::find($pId); $stage = ProductionStage::where('ProductionId', '=', $pId)->where('Stage', '=', $stage)->first(); $worker = Worker::find($stage->WorkerId); $product = Stock::where('Code', '=', $production->Code)->where('BranchId', '=', $worker->BranchId)->first(); ?> <!DOCTYPE html> <html lang="es"> <head> <title>Eirene Systema Administrativo</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta charset="UTF-8"> <meta name="csrf-token" content="{{{ Session::token() }}}"> <link href="{{ URL::to('/') }}/css/bootstrap.min.css" rel="stylesheet"> <link href="{{ URL::to('/') }}/css/bootstrap-responsive.min.css" rel="stylesheet"> <link href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600" rel="stylesheet">
public function staffAnalyticsGraph() { // Validate Input. $validator = Validator::make(Input::all(), array('start' => 'required', 'end' => 'required', 'currency' => 'required', 'interval' => '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 branch of the current worker. $worker = Worker::find(Input::get('worker')); $branchId = $worker->BranchId; // Get the cashboxes so we can get dollar values. $cashboxes = Cashbox::where('BranchId', '=', $branchId)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); $valueDollarByDate = array(); // First let's get all the dollar values in this period of time. foreach ($cashboxes as $cashbox) { if (!array_key_exists($cashbox->Date, $valueDollarByDate)) { $valueDollarByDate[$cashbox->Date] = $cashbox->Dollar; } } // Define keys for all arrays. $initDate = date_create(date('Y-m-d', strtotime(Input::get('start')))); $finalDate = date_create(date('Y-m-d', strtotime(Input::get('end')))); $currentDate = $initDate; $currentDollarValue = 1; $totalDays = 0; $earned = array(); $totalBasic = 0; $totalBonus = 0; $totalInsurance = 0; $totalAguinaldo = 0; $totalSales = 0; $totalProduction = 0; $totalProductionVal = 0; // Now add all the date keys we will need. while ($currentDate <= $finalDate) { $c = date_format($currentDate, 'Y-m-d'); $earned[$c]['basic'] = 0; $earned[$c]['bonus'] = 0; $earned[$c]['aguinaldo'] = 0; $earned[$c]['inss'] = 0; $earned[$c]['sales'] = 0; $earned[$c]['production'] = 0; $earned[$c]['productionVal'] = 0; if (!array_key_exists($c, $valueDollarByDate)) { $valueDollarByDate[$c] = $currentDollarValue; } else { $currentDollarValue = $valueDollarByDate[$c]; } $totalDays++; date_add($currentDate, date_interval_create_from_date_string("1 day")); } $response['dayDiff'] = $totalDays; if (Input::get('currency') == 'cordoba') { // Now that we have the worker let's get what he has earned in the defined time period. $salaries = WorkerSalary::where('WorkerId', '=', $worker->Id)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); // Now extract the information we need. foreach ($salaries as $salary) { if ($salary->DayType != 3) { $earned[$salary->Date]['basic'] += $salary->Basic; $totalBasic += $salary->Basic; $earned[$salary->Date]['bonus'] += $salary->Bonus; $totalBonus += $salary->Bonus; $earned[$salary->Date]['aguinaldo'] += $salary->AguinaldoPaid ? $salary->Aguinaldo : $worker->Basic * 0.2465; $totalAguinaldo += $salary->AguinaldoPaid ? $salary->Aguinaldo : $worker->Basic * 0.2465; $earned[$salary->Date]['inss'] += $salary->Insurance; $totalInsurance += $salary->Insurance; } } // Check what kind of worker it is. if ($worker->Category == 1) { // If he sells stuff get his sales. $sales = Sale::where('WorkerId', '=', $worker->Id)->where('Created', '>=', Input::get('start') . ' 00:00:00')->where('Created', '<=', Input::get('end') . ' 23:59:59')->get(); foreach ($sales as $sale) { $earned[date('Y-m-d', strtotime($sale->Created))]['sales'] += $sale->Value; $totalSales += $sale->Value; } } else { if ($worker->Category == 2) { // If he produces stuff let's graph the quantity produced. $stages = ProductionStage::where('Date', '>=', Input::get('start') . ' 00:00:00')->where('Date', '<=', Input::get('end') . '23:59:59')->where('WorkerId', '=', $worker->Id)->get(); foreach ($sates as $stage) { $earned[date('Y-m-d', strtotime($sale->Created))]['production']++; $totalProduction++; } } } } else { // Now that we have the worker let's get what he has earned in the defined time period. $salaries = WorkerSalary::where('WorkerId', '=', $worker->Id)->where('Date', '>=', Input::get('start'))->where('Date', '<=', Input::get('end'))->get(); // Now extract the information we need. foreach ($salaries as $salary) { if ($salary->DayType != 3) { $earned[$salary->Date]['basic'] += $salary->Basic / $valueDollarByDate[$salary->Date]; $totalBasic += $salary->Basic / $valueDollarByDate[$salary->Date]; $earned[$salary->Date]['bonus'] += $salary->Bonus / $valueDollarByDate[$salary->Date]; $totalBonus += $salary->Bonus / $valueDollarByDate[$salary->Date]; $earned[$salary->Date]['aguinaldo'] += ($salary->AguinaldoPaid ? $salary->Aguinaldo : $worker->Basic * 0.2465) / $valueDollarByDate[$salary->Date]; $totalAguinaldo += ($salary->AguinaldoPaid ? $salary->Aguinaldo : $worker->Basic * 0.2465) / $valueDollarByDate[$salary->Date]; $earned[$salary->Date]['inss'] += $salary->Insurance / $valueDollarByDate[$salary->Date]; $totalInsurance += $salary->Insurance / $valueDollarByDate[$salary->Date]; } } // Check what kind of worker it is. if ($worker->Category == 1) { // If he sells stuff get his sales. $sales = Sale::where('WorkerId', '=', $worker->Id)->where('Created', '>=', Input::get('start') . ' 00:00:00')->where('Created', '<=', Input::get('end') . ' 23:59:59')->get(); foreach ($sales as $sale) { $earned[date('Y-m-d', strtotime($sale->Created))]['sales'] += $sale->Value; $totalSales += $sale->Value; } } else { if ($worker->Category == 2) { // If he produces stuff let's graph the quantity produced. $stages = ProductionStage::where('Date', '>=', Input::get('start') . ' 00:00:00')->where('Date', '<=', Input::get('end') . '23:59:59')->where('WorkerId', '=', $worker->Id)->get(); foreach ($sates as $stage) { $earned[date('Y-m-d', strtotime($sale->Created))]['production']++; $totalProduction++; } } } } switch (Input::get('interval')) { case 'days': $graphLabels = array(); $basicByDate = array(); $bonusByDate = array(); $aguinaldoByDate = array(); $insuranceByDate = array(); $salesByDate = array(); $productionByDate = array(); // Prepare graph labels. foreach ($earned as $date => $data) { if (!in_array($date, $graphLabels)) { array_push($graphLabels, $date); } $basicByDate[$date] = $data['basic']; $bonusByDate[$date] = $data['bonus']; $aguinaldoByDate[$date] = $data['aguinaldo']; $insuranceByDate[$date] = $data['inss']; $salesByDate[$date] = $data['sales']; $productionByDate[$date] = $data['production']; } // Return required information. $response['graphLabel'] = $graphLabels; $response['basicByDate'] = $basicByDate; $response['bonusByDate'] = $bonusByDate; $response['aguinaldoByDate'] = $aguinaldoByDate; $response['insuranceByDate'] = $insuranceByDate; $response['salesByDate'] = $salesByDate; $response['productionByDate'] = $productionByDate; $response['totalBasic'] = $totalBasic; $response['totalBonus'] = $totalBonus; $response['totalAguinaldo'] = $totalAguinaldo; $response['totalInsurance'] = $totalInsurance; $response['totalSales'] = $totalSales; $response['totalProduction'] = $totalProduction; $response['state'] = 'Success'; return response()->json($response); break; case 'weeks': $graphLabels = array(); $basicByDate = array(); $bonusByDate = array(); $aguinaldoByDate = array(); $insuranceByDate = array(); $salesByDate = array(); $productionByDate = array(); // Prepare graph labels. foreach ($earned as $date => $data) { if (!in_array(date('W-Y', strtotime($date)), $graphLabels)) { array_push($graphLabels, date('W-Y', strtotime($date))); } if (!array_key_exists(date('W-Y', strtotime($date)), $basicByDate)) { $basicByDate[date('W-Y', strtotime($date))] = $data['basic']; $bonusByDate[date('W-Y', strtotime($date))] = $data['bonus']; $aguinaldoByDate[date('W-Y', strtotime($date))] = $data['aguinaldo']; $insuranceByDate[date('W-Y', strtotime($date))] = $data['inss']; $salesByDate[date('W-Y', strtotime($date))] = $data['sales']; $productionByDate[date('W-Y', strtotime($date))] = $data['production']; } else { $basicByDate[date('W-Y', strtotime($date))] += $data['basic']; $bonusByDate[date('W-Y', strtotime($date))] += $data['bonus']; $aguinaldoByDate[date('W-Y', strtotime($date))] += $data['aguinaldo']; $insuranceByDate[date('W-Y', strtotime($date))] += $data['inss']; $salesByDate[date('W-Y', strtotime($date))] += $data['sales']; $productionByDate[date('W-Y', strtotime($date))] += $data['production']; } } // Return required information. $response['graphLabel'] = $graphLabels; $response['basicByDate'] = $basicByDate; $response['bonusByDate'] = $bonusByDate; $response['aguinaldoByDate'] = $aguinaldoByDate; $response['insuranceByDate'] = $insuranceByDate; $response['salesByDate'] = $salesByDate; $response['productionByDate'] = $productionByDate; $response['totalBasic'] = $totalBasic; $response['totalBonus'] = $totalBonus; $response['totalAguinaldo'] = $totalAguinaldo; $response['totalInsurance'] = $totalInsurance; $response['totalSales'] = $totalSales; $response['totalProduction'] = $totalProduction; $response['state'] = 'Success'; return response()->json($response); break; case 'months': $graphLabels = array(); $basicByDate = array(); $bonusByDate = array(); $aguinaldoByDate = array(); $insuranceByDate = array(); $salesByDate = array(); $productionByDate = array(); // Prepare graph labels. foreach ($earned as $date => $data) { if (!in_array(date('m-Y', strtotime($date)), $graphLabels)) { array_push($graphLabels, date('m-Y', strtotime($date))); } if (!array_key_exists(date('m-Y', strtotime($date)), $basicByDate)) { $basicByDate[date('m-Y', strtotime($date))] = $data['basic']; $bonusByDate[date('m-Y', strtotime($date))] = $data['bonus']; $aguinaldoByDate[date('m-Y', strtotime($date))] = $data['aguinaldo']; $insuranceByDate[date('m-Y', strtotime($date))] = $data['inss']; $salesByDate[date('m-Y', strtotime($date))] = $data['sales']; $productionByDate[date('m-Y', strtotime($date))] = $data['production']; } else { $basicByDate[date('m-Y', strtotime($date))] += $data['basic']; $bonusByDate[date('m-Y', strtotime($date))] += $data['bonus']; $aguinaldoByDate[date('m-Y', strtotime($date))] += $data['aguinaldo']; $insuranceByDate[date('m-Y', strtotime($date))] += $data['inss']; $salesByDate[date('m-Y', strtotime($date))] += $data['sales']; $productionByDate[date('m-Y', strtotime($date))] += $data['production']; } } // Return required information. $response['graphLabel'] = $graphLabels; $response['basicByDate'] = $basicByDate; $response['bonusByDate'] = $bonusByDate; $response['aguinaldoByDate'] = $aguinaldoByDate; $response['insuranceByDate'] = $insuranceByDate; $response['salesByDate'] = $salesByDate; $response['productionByDate'] = $productionByDate; $response['totalBasic'] = $totalBasic; $response['totalBonus'] = $totalBonus; $response['totalAguinaldo'] = $totalAguinaldo; $response['totalInsurance'] = $totalInsurance; $response['totalSales'] = $totalSales; $response['totalProduction'] = $totalProduction; $response['state'] = 'Success'; return response()->json($response); break; case 'years': $graphLabels = array(); $basicByDate = array(); $bonusByDate = array(); $aguinaldoByDate = array(); $insuranceByDate = array(); $salesByDate = array(); $productionByDate = array(); // Prepare graph labels. foreach ($earned as $date => $data) { if (!in_array(date('Y', strtotime($date)), $graphLabels)) { array_push($graphLabels, date('Y', strtotime($date))); } if (!array_key_exists(date('Y', strtotime($date)), $basicByDate)) { $basicByDate[date('Y', strtotime($date))] = $data['basic']; $bonusByDate[date('Y', strtotime($date))] = $data['bonus']; $aguinaldoByDate[date('Y', strtotime($date))] = $data['aguinaldo']; $insuranceByDate[date('Y', strtotime($date))] = $data['inss']; $salesByDate[date('Y', strtotime($date))] = $data['sales']; $productionByDate[date('Y', strtotime($date))] = $data['production']; } else { $basicByDate[date('Y', strtotime($date))] += $data['basic']; $bonusByDate[date('Y', strtotime($date))] += $data['bonus']; $aguinaldoByDate[date('Y', strtotime($date))] += $data['aguinaldo']; $insuranceByDate[date('Y', strtotime($date))] += $data['inss']; $salesByDate[date('Y', strtotime($date))] += $data['sales']; $productionByDate[date('Y', strtotime($date))] += $data['production']; } } // Return required information. $response['graphLabel'] = $graphLabels; $response['basicByDate'] = $basicByDate; $response['bonusByDate'] = $bonusByDate; $response['aguinaldoByDate'] = $aguinaldoByDate; $response['insuranceByDate'] = $insuranceByDate; $response['salesByDate'] = $salesByDate; $response['productionByDate'] = $productionByDate; $response['totalBasic'] = $totalBasic; $response['totalBonus'] = $totalBonus; $response['totalAguinaldo'] = $totalAguinaldo; $response['totalInsurance'] = $totalInsurance; $response['totalSales'] = $totalSales; $response['totalProduction'] = $totalProduction; $response['state'] = 'Success'; return response()->json($response); break; default: // Anything else we just don't do anything. break; } }