public function print_affiliate($affiliate)
 {
     $header1 = "DIRECCIÓN DE BENEFICIOS ECONÓMICOS";
     $header2 = "UNIDAD DE FONDO DE RETIRO POLICIAL INDIVIDUAL";
     $title = "REPORTE DE AFILIADO";
     $date = Util::getDateEdit(date('Y-m-d'));
     $current_date = Carbon::now();
     $hour = Carbon::parse($current_date)->toTimeString();
     $data = $this->getData($affiliate);
     $affiliate = $data['affiliate'];
     $spouse = $data['spouse'];
     $total_gain = $data['total_gain'];
     $total_public_security_bonus = $data['total_public_security_bonus'];
     $total_quotable = $data['total_quotable'];
     $total_retirement_fund = $data['total_retirement_fund'];
     $total_mortuary_quota = $data['total_mortuary_quota'];
     $total = $data['total'];
     $contributions = Contribution::select(['id', 'month_year', 'degree_id', 'unit_id', 'item', 'base_wage', 'seniority_bonus', 'study_bonus', 'position_bonus', 'border_bonus', 'east_bonus', 'public_security_bonus', 'gain', 'quotable', 'retirement_fund', 'mortuary_quota', 'total'])->where('affiliate_id', $affiliate->id)->get();
     $date = Util::getfulldate(date('Y-m-d'));
     $view = \View::make('affiliates.print.show', compact('header1', 'header2', 'title', 'date', 'hour', 'affiliate', 'spouse', 'total_gain', 'total_public_security_bonus', 'total_quotable', 'total_retirement_fund', 'total_mortuary_quota', 'total', 'contributions'))->render();
     $pdf = \App::make('dompdf.wrapper');
     $pdf->loadHTML($view)->setPaper('legal', 'landscape');
     return $pdf->stream();
 }
 public function SelectData(Request $request)
 {
     $affiliate = Affiliate::idIs($request->affiliate_id)->first();
     $afi["affiliate"] = $affiliate->id;
     $group_contributions = new Collection();
     $now = Carbon::now();
     $from = Carbon::parse($affiliate->date_entry);
     $to = Carbon::createFromDate($now->year, $now->month, 1)->subMonth();
     for ($i = $from->year; $i <= $to->year; $i++) {
         $count = 0;
         $total = 0;
         $base = array();
         $mes = array();
         for ($j = 1; $j <= 12; $j++) {
             $contribution = Contribution::select(['month_year'])->where('affiliate_id', $affiliate->id)->where('month_year', '=', Carbon::createFromDate($i, $j, 1)->toDateString())->first();
             if ($contribution) {
                 $mes["m" . $j] = 1;
                 $count++;
                 $total++;
             } else {
                 $mes["m" . $j] = 0;
                 $total++;
             }
             if ($i == $from->year) {
                 if ($j < $from->month) {
                     $mes["m" . $j] = -1;
                     $total--;
                 }
             }
             if ($i == $to->year) {
                 if ($j > $to->month) {
                     $mes["m" . $j] = -1;
                     $total--;
                 }
             }
             $base = array_merge($base, $mes);
         }
         if ($total == $count) {
             $c["status"] = false;
         } else {
             $c["status"] = true;
         }
         $year = array('year' => $i);
         $group_contributions->push(array_merge($afi, $c, $year, $base));
     }
     return Datatables::of($group_contributions)->editColumn('m1', '<?php if($m1 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m1 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m1 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m2', '<?php if($m2 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m2 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m2 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m3', '<?php if($m3 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m3 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m3 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m4', '<?php if($m4 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m4 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m4 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m5', '<?php if($m5 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m5 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m5 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m6', '<?php if($m6 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m6 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m6 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m7', '<?php if($m7 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m7 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m7 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m8', '<?php if($m8 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m8 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m8 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m9', '<?php if($m9 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m9 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m9 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m10', '<?php if($m10 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m10 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m10 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m11', '<?php if($m11 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m11 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m11 === -1){ ?>&nbsp;<?php } ?>')->editColumn('m12', '<?php if($m12 === 1){ ?><i class="glyphicon glyphicon-check"></i><?php } if($m12 === 0){?><i class="glyphicon glyphicon-unchecked"></i><?php } if($m12 === -1){ ?>&nbsp;<?php } ?>')->addColumn('action', '
                 <?php if($status){ ?>
                     <div class="btn-group" style="margin:-6px 0;">
                         <a href="{{ url("calculation_contribution")}}/{{$affiliate}}/{{$year}}/normal" class="btn btn-success btn-raised btn-sm"><i class="glyphicon glyphicon-edit"></i></a>
                         <a href="" data-target="#" class="btn btn-success btn-raised btn-sm dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                         <ul class="dropdown-menu">
                             <li><a href="{{ url("calculation_contribution")}}/{{$affiliate}}/{{$year}}/reintegro" style="padding:3px 10px;"><i class="glyphicon glyphicon-pencil"></i> reintegro</a></li>
                         </ul>
                     </div>
                 <?php } ?>')->make(true);
 }
 public function period()
 {
     $second = Contribution::select('month_year')->where('contributions.direct_contribution_id', '=', $this->id)->orderBy('id', 'desc')->first();
     $first = Contribution::select('month_year')->where('contributions.direct_contribution_id', '=', $this->id)->orderBy('id', 'asc')->first();
     return "De " . Util::getDateAbrePeriod($first->month_year) . " a " . Util::getDateAbrePeriod($second->month_year);
 }
 public static function getViewModel($affiliate_id, $year, $category_id = null, $last_contribution = null)
 {
     $affiliate = Affiliate::idIs($affiliate_id)->first();
     $categories = Category::where('name', '<>', 'S/N')->get();
     $list_categories = array('' => '');
     foreach ($categories as $item) {
         $list_categories[$item->id] = $item->name;
     }
     $now = Carbon::now();
     $from = Carbon::parse($affiliate->date_entry);
     $to = Carbon::createFromDate($now->year, $now->month, 1)->subMonth();
     $ipc_actual = IpcRate::select('index')->where('month_year', '=', Carbon::createFromDate($now->year, $now->month, 1)->toDateString())->first();
     $months = new Collection();
     $month = array();
     for ($j = 1; $j <= 12; $j++) {
         $contribution = Contribution::select(['month_year'])->where('affiliate_id', $affiliate_id)->where('month_year', '=', Carbon::createFromDate($year, $j, 1)->toDateString())->first();
         $contribution_rate = ContributionRate::where('month_year', '=', Carbon::createFromDate($year, $j, 1)->toDateString())->first();
         $ipc_rate = IpcRate::where('month_year', '=', Carbon::createFromDate($year, $j, 1)->toDateString())->first();
         if (!$contribution) {
             if ($year == $from->year) {
                 if ($j >= $from->month) {
                     $month["id"] = $j;
                     $month["name"] = Util::getMes($j);
                     $month["retirement_fund"] = $contribution_rate ? $contribution_rate->retirement_fund : '0';
                     $month["mortuary_quota"] = $contribution_rate ? $contribution_rate->mortuary_quota : '0';
                     $month["ipc_rate"] = $contribution_rate ? $ipc_rate->index : '0';
                     $months->push($month);
                 }
             } elseif ($year == $to->year) {
                 if ($j <= $to->month) {
                     $month["id"] = $j;
                     $month["name"] = Util::getMes($j);
                     $month["retirement_fund"] = $contribution_rate ? $contribution_rate->retirement_fund : '0';
                     $month["mortuary_quota"] = $contribution_rate ? $contribution_rate->mortuary_quota : '0';
                     $month["ipc_rate"] = $contribution_rate ? $ipc_rate->index : '0';
                     $months->push($month);
                 }
             } else {
                 $month["id"] = $j;
                 $month["name"] = Util::getMes($j);
                 $month["retirement_fund"] = $contribution_rate ? $contribution_rate->retirement_fund : '0';
                 $month["mortuary_quota"] = $contribution_rate ? $contribution_rate->mortuary_quota : '0';
                 $month["ipc_rate"] = $contribution_rate ? $ipc_rate->index : '0';
                 $months->push($month);
             }
         }
     }
     if (!$last_contribution) {
         $date = Carbon::createFromDate($year, $months[0]["id"], 1);
         $last_contribution = Contribution::affiliateidIs($affiliate->id)->where('month_year', '<', $date->toDateString())->first();
         if (!$last_contribution) {
             $last_contribution = new Contribution();
             $base_wage = BaseWage::whereYear('month_year', '<=', $year)->first();
             $last_contribution->base_wage = $base_wage->amount;
             $last_contribution->study_bonus = 0;
             $last_contribution->position_bonus = 0;
             $last_contribution->border_bonus = 0;
             $last_contribution->east_bonus = 0;
         }
         $last_contribution->date = Util::getDateShort($last_contribution->month_year);
     }
     if ($category_id) {
         $affiliate->category_id = $category_id;
     }
     return ['affiliate' => $affiliate, 'list_categories' => $list_categories, 'categories' => Category::where('name', '<>', 'S/N')->orderBy('id', 'asc')->get(array('percentage', 'name', 'id')), 'ipc_actual' => $ipc_actual, 'months' => $months, 'year' => $year, 'last_contribution' => $last_contribution, 'direct_contribution' => ''];
 }