public function Data()
 {
     $ipc_rates = IpcRate::select(['month_year', 'index']);
     return Datatables::of($ipc_rates)->editColumn('year', function ($ipc_rate) {
         return Carbon::parse($ipc_rate->month_year)->year;
     })->addColumn('month', function ($ipc_rate) {
         return Util::getMes(Carbon::parse($ipc_rate->month_year)->month);
     })->editColumn('index', function ($ipc_rate) {
         return Util::formatMoney($ipc_rate->index);
     })->make(true);
 }
 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);
 }