<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PaymentsReportController
 *
 * @author f_ben
 */
class ReportPaymentsFBController extends SecurityController {

    public function __construct() {
        parent::__construct();
    }

    public function getIndexUser() {
//        $user = User;
        return View::make('paymentslttm.reports.paymentslt.indexuser');
    }

    public function getIndex() {
        require_once('Debug.class.php');
        $schoolYear = new SchoolYear;
        $enablePaymentScy = $schoolYear->getSchoolYearEnabledPayment();
        $enablePaymentScy = array('' => '-Seleccione-') + $enablePaymentScy;
        $currentScy = $schoolYear->getSchoolYearMatriculeProcesActive();
        return View::make('paymentstm.reports.paymentsfb.index')
                        ->with('schoolYears', $enablePaymentScy)
                        ->with('currentScy', $currentScy->serial_scy);
    }

    public function postDates() {
        $school_year = SchoolYear::find(Input::get('serial_scy'));
        $school_year->invoice_start_date_scy_txt = date('Y-m', strtotime($school_year->invoice_start_date_scy));
        $school_year->invoice_start_date_scy = date('Y-m', strtotime('+1 month', strtotime($school_year->invoice_start_date_scy)));
        $school_year->end_date_scy = date('Y-m', strtotime("+$school_year->invoice_number_scy month", strtotime($school_year->invoice_start_date_scy_txt)));
        $numberRept = $school_year->invoice_number_scy - 1;
        $school_year->end_date_scy_txt = date('Y-m', strtotime("+$numberRept month", strtotime($school_year->invoice_start_date_scy_txt)));
        return View::make('paymentstm.reports.paymentslt.loads.dates')
                        ->with('school_year', $school_year);
    }

    public function postReport() {
        set_time_limit(0);
        require_once('Debug.class.php');
        Excel::create('Pagos realizados', function ($excel) {
            $data = Input::all();
            $currentDeferred = new CurrentDeferred();
           $paymentButtons = $currentDeferred->getAllCurrentDeferredButtons();
            $schoolSetup = new SchoolSetup();
            $schoolSetupInfo = $schoolSetup->getSchoolSetupInfo();
                    $excel->sheet('Pagos con boton de pagos ' , function ($sheet)  {
                        $widhts = array('A' => 20, 'B' => 40, 'C' => 40, 'D' => 40, 'E' => 22, 'F' => 18, 'G' => 11, 'H' => 10, 'I' => 10, 'J' => 10, 'K' => 10, 'L' => 10, 'M' => 17, 'N' => 45, 'O' => 15, 'P' => 12, 'Q' => 12, 'R' => 12);
                        $sheet->getStyle("A1:R500")->getAlignment()->setWrapText(true);
                        $sheet->setWidth($widhts);
                        $sheet->loadView('paymentstm.reports.paymentsfb.loads.paymentbuttonsng')
                                ->with('data', Input::all());
                    });
        })->download('xlsx');
    }

    public function postStudents() {
        $data = Input::all();
        $currentDeferred = new CurrentDeferred();
        return View::make('paymentstm.reports.paymentsfb.loads.students')->with('data', Input::all());
    }

    public function postReportUser() {
        require_once('Debug.class.php');
        Excel::create('Pagos realizados', function ($excel) {
            $data = Input::all();
            $payementDetaiiTotalInvoice = new PaymentDetailTotalInvoice();
            $paymetnConceptGroupPaymeentDetails = $payementDetaiiTotalInvoice->getPaymentConceptGroupsByDate(Input::get('txtStartDate'), Input::get('txtStartDate'), Input::get('txtEndDate'));
            foreach ($paymetnConceptGroupPaymeentDetails as $paymenConcepGroup) {
                $payementMethods = $payementDetaiiTotalInvoice->getPaymentConceptGroupsByDatePaymentConceptGroup(Input::get('txtStartDate'), Input::get('txtEndDate'), $paymenConcepGroup->serial_pcg);
                $excel->sheet('Formas de pago en caja ' . $paymenConcepGroup->description_name_pcg, function ($sheet) use ($paymenConcepGroup, $payementMethods) {
                    set_time_limit(0);
                    $payementDetaiiTotalInvoice = new PaymentDetailTotalInvoice();
                    $user = new User;
                    $currentUser = $user->getCurrentUserProfile();
                    $users = $payementDetaiiTotalInvoice->getUsuariosByDatePaymentConceptGroup(Input::get('txtStartDate'), Input::get('txtEndDate'), $paymenConcepGroup->serial_pcg, $currentUser->serial_usr);
                    $widhts = array('A' => 12, 'B' => 50, 'C' => 7, 'D' => 40, 'E' => 12, 'F' => 29, 'G' => 18, 'H' => '50', 'I' => 14, 'J' => 14, 'K' => 14, 'L' => 14, 'M' => 14, 'N' => 14, 'O' => 14, 'P' => 14, 'Q' => 14, 'R' => 14, 'S' => 14, 'T' => 14);
                    $sheet->setWidth($widhts);
                    $sheet->getStyle("A1:T1500")->getAlignment()->setWrapText(true);
                    $sheet->loadView('paymentslttm.reports.paymentslt.loads.paymentmethods')
                            ->with('data', Input::all())
                            ->with('paymenConcepGroup', $paymenConcepGroup)
                            ->with('users', $users)
                            ->with('payementMethods', $payementMethods);
                });
            }
        })->download('xlsx');
    }

}
