/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $last_ipcrate = IpcRate::orderBy('month_year', 'desc')->first();
     if (Carbon::parse($last_ipcrate->month_year)->addMonth() < ($date = new Carbon())) {
         $new_ipcrate = new IpcRate();
         $new_ipcrate->user_id = 1;
         $new_ipcrate->index = $last_ipcrate->index;
         $new_ipcrate->month_year = Carbon::parse($last_ipcrate->month_year)->addMonth();
         $new_ipcrate->save();
         $year = Carbon::parse($last_ipcrate->month_year)->year;
         $month = Carbon::parse($last_ipcrate->month_year)->month;
         \Storage::disk('local')->put('IpcRate_' . $month . '-' . $year . '.txt', "Index of consumer price successfully updated: " . $last_ipcrate->index);
     }
 }
 public function save($request, $ipc_rate = false)
 {
     $rules = ['month_year' => 'required', 'index' => 'required|numeric'];
     $messages = ['month_year.required' => 'El campo Año no puede ser vacío', 'index.required' => 'El campo IPC no puede ser vacío', 'index.numeric' => 'El campo IPC sólo se aceptan números'];
     $validator = Validator::make($request->all(), $rules, $messages);
     if ($validator->fails()) {
         return redirect('ipc_rate')->withErrors($validator)->withInput();
     } else {
         $ipcTasa = IpcRate::where('month_year', '=', Util::datePickPeriod($request->month_year))->first();
         if ($ipcTasa) {
             $ipcTasa->user_id = Auth::user()->id;
             $ipcTasa->index = trim($request->index);
             $ipcTasa->save();
             $message = "Índice de Precios al Consumidor actualizado con éxito";
             Session::flash('message', $message);
         } else {
             $message = "Fecha no disponible";
             Session::flash('message', $message);
         }
     }
     return redirect('ipc_rate');
 }
 public function save($request)
 {
     $rules = ['affiliate_id' => 'required'];
     $messages = ['affiliate_id.required' => 'Afiliado no disponible'];
     $validator = Validator::make($request->all(), $rules, $messages);
     if ($validator->fails()) {
         return redirect('affiliate/' . $request->affiliate_id)->withErrors($validator)->withInput();
     } else {
         $direct_contribution = new DirectContribution();
         $data = json_decode($request->data);
         $now = Carbon::now();
         $affiliate = Affiliate::IdIs($request->affiliate_id)->first();
         $last_direct_contribution = DirectContribution::whereYear('created_at', '=', $now->year)->where('deleted_at', '=', null)->orderBy('id', 'desc')->first();
         if ($last_direct_contribution) {
             $number_code = Util::separateCode($last_direct_contribution->code);
             $code = $number_code + 1;
         } else {
             $code = 1;
         }
         $direct_contribution->code = $code . "/" . $now->year;
         $direct_contribution->user_id = Auth::user()->id;
         $direct_contribution->affiliate_id = $request->affiliate_id;
         $direct_contribution->affiliate_name = $affiliate->getTittleName();
         $direct_contribution->affiliate_degree = $affiliate->degree->name;
         $direct_contribution->affiliate_identity_card = $affiliate->identity_card;
         $direct_contribution->affiliate_registration = $affiliate->registration;
         $direct_contribution->quotable = $data->sum_quotable;
         $direct_contribution->retirement_fund = $data->sum_subtotal_retirement_fund;
         $direct_contribution->mortuary_quota = $data->sum_subtotal_mortuary_quota;
         $direct_contribution->subtotal = $data->sum_subtotal;
         $direct_contribution->ipc = $data->sum_subtotal_ipc_rate;
         $direct_contribution->total = $data->sum_total;
         $direct_contribution->save();
         foreach ($data->contributions as $item) {
             $month_year = Carbon::createFromDate($request->year, $item->id_month, 1)->toDateString();
             $contribution = Contribution::where('month_year', '=', $month_year)->where('affiliate_id', '=', $request->affiliate_id)->first();
             $contribution_rate = ContributionRate::where('month_year', '=', $month_year)->first();
             $ipc_rate = IpcRate::where('month_year', '=', $month_year)->first();
             $now = Carbon::now();
             $ipc_actual = IpcRate::select('index')->where('month_year', '=', Carbon::createFromDate($now->year, $now->month, 1)->toDateString())->first();
             if (!$contribution) {
                 $contribution = new Contribution();
                 $contribution->user_id = Auth::user()->id;
                 $contribution->contribution_type_id = 2;
                 $contribution->affiliate_id = $request->affiliate_id;
                 $contribution->direct_contribution_id = $direct_contribution->id;
                 $contribution->month_year = $month_year;
                 $contribution->base_wage = $item->base_wage;
                 $contribution->category_id = $item->category->id;
                 $contribution->seniority_bonus = $item->seniority_bonus;
                 $contribution->study_bonus = $item->study_bonus;
                 $contribution->position_bonus = $item->position_bonus;
                 $contribution->border_bonus = $item->border_bonus;
                 $contribution->east_bonus = $item->east_bonus;
                 $contribution->quotable = $item->quotable;
                 $contribution->retirement_fund = $item->subtotal_retirement_fund;
                 $contribution->mortuary_quota = $item->subtotal_mortuary_quota;
                 $contribution->subtotal = $item->subtotal;
                 $contribution->ipc = $item->subtotal_ipc_rate;
                 $contribution->total = $item->total;
                 $contribution->save();
             }
         }
         $voucher = new Voucher();
         $voucher->user_id = Auth::user()->id;
         $voucher->affiliate_id = $request->affiliate_id;
         $voucher->voucher_type_id = 1;
         $voucher->direct_contribution_id = $direct_contribution->id;
         $last_voucher = Voucher::whereYear('created_at', '=', $now->year)->where('deleted_at', '=', null)->orderBy('id', 'desc')->first();
         if ($last_voucher) {
             $number_code = Util::separateCode($last_voucher->code);
             $code = $number_code + 1;
         } else {
             $code = 1;
         }
         $voucher->code = $code . "/" . $now->year;
         $voucher->total = $data->sum_total;
         $voucher->save();
         $message = "Aportes Guardados";
         Session::flash('message', $message);
     }
     return redirect('direct_contribution/' . $direct_contribution->id);
 }