<?php

class FechaSemList extends TPage
{
    public function __construct($param)
    {
        parent::__construct();
        new TSession;

        $this->ini = parse_ini_file('app/config/application.ini', true);

        $primeirodia = substr($param["method"],0,10);

        $ultimodia = date('Y-m-d', strtotime("+6 days",strtotime($primeirodia)));
        $porc_cartao1 = $this->ini['general']['txcard1'];
        $porc_cartao2 = $this->ini['general']['txcard1'];

        TTransaction::open('bingo');

        // Cria códigos de Despesa
        $a_despesas = array();
        $repository = new TRepository('codigo');
        $criteria = new TCriteria;
        $criteria->setProperty('order', 'id asc');
        $criteria->add(new TFilter('codigo', '>', 153));
        $codes = $repository->load($criteria);
        foreach ($codes as $code){
            $a_despesas[$code->codigo] = [$code->descricao,0,0,0,0,0,0,0,0];
        }

        // create the datagrid - Contadores
        $this->datagrid0 = new TQuickGrid;
        $this->datagrid0->style = 'width: 100%';
        $dias_semana = array(0=>'DOMINGO',1=>'SEGUNDA',2=>'TERÇA',3=>'QUARTA',4=>'QUINTA',5=>'SEXTA',6=>'SÁBADO',7=>'DOMINGO',8=>'SEGUNDA',9=>'TERÇA',10=>'QUARTA',11=>'QUINTA',12=>'SEXTA');
        $this->datagrid0->addQuickColumn('DESPESA / DIA'  ,'tipo'  ,'left', 150);
        $dia1 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']  ],'dia1','center', 150);$dia1->setTransformer(array($this, 'formatsaldo'));
        $dia2 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+1],'dia2','center', 150);$dia2->setTransformer(array($this, 'formatsaldo'));
        $dia3 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+2],'dia3','center', 150);$dia3->setTransformer(array($this, 'formatsaldo'));
        $dia4 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+3],'dia4','center', 150);$dia4->setTransformer(array($this, 'formatsaldo'));
        $dia5 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+4],'dia5','center', 150);$dia5->setTransformer(array($this, 'formatsaldo'));
        $dia6 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+5],'dia6','center', 150);$dia6->setTransformer(array($this, 'formatsaldo'));
        $dia7 = $this->datagrid0->addQuickColumn($dias_semana[$this->ini['general']['semana']+6],'dia7','center', 150);$dia7->setTransformer(array($this, 'formatsaldo'));
        $total=$this->datagrid0->addQuickColumn('TOTAL','total','center', 150);
        $total->setTransformer(array($this, 'formatsaldo'));
        $this->datagrid0->createModel();

        $obj1 = new stdclass();$obj1->tipo = 'ARRECADADO';$obj1->dia1 = 0;$obj1->dia2 = 0;$obj1->dia3 = 0;$obj1->dia4 = 0;$obj1->dia5 = 0;$obj1->dia6 = 0;$obj1->dia7 = 0;$obj1->total = 0;
        $obj2 = new stdclass();$obj2->tipo = 'PRÊMIOS';$obj2->dia1 = 0;$obj2->dia2 = 0;$obj2->dia3 = 0;$obj2->dia4 = 0;$obj2->dia5 = 0;$obj2->dia6 = 0;$obj2->dia7 = 0;$obj2->total = 0;
        $obj3 = new stdclass();$obj3->tipo = 'SALDO';$obj3->dia1 = 0;$obj3->dia2 = 0;$obj3->dia3 = 0;$obj3->dia4 = 0;$obj3->dia5 = 0;$obj3->dia6 = 0;$obj3->dia7 = 0;$obj3->total = 0;
        $obj4 = new stdclass();$obj4->tipo = '';$obj4->dia1 = '9999999999';$obj4->dia2 = '9999999999';$obj4->dia3 = '9999999999';$obj4->dia4 = '9999999999';$obj4->dia5 = '9999999999';$obj4->dia6 = '9999999999';$obj4->dia7 = '9999999999';$obj4->total = '9999999999';
        $obj5 = new stdclass();$obj5->tipo = 'VALES (ENTRADA)';$obj5->dia1 = 0;$obj5->dia2 = 0;$obj5->dia3 = 0;$obj5->dia4 = 0;$obj5->dia5 = 0;$obj5->dia6 = 0;$obj5->dia7 = 0;$obj5->total = 0;
        $obj6 = new stdclass();$obj6->tipo = 'VALES (SAÍDA)';$obj6->dia1 = 0;$obj6->dia2 = 0;$obj6->dia3 = 0;$obj6->dia4 = 0;$obj6->dia5 = 0;$obj6->dia6 = 0;$obj6->dia7 = 0;$obj6->total = 0;
        $obj7 = new stdclass();$obj7->tipo = 'SALDO DOS VALES';$obj7->dia1 = 0;$obj7->dia2 = 0;$obj7->dia3 = 0;$obj7->dia4 = 0;$obj7->dia5 = 0;$obj7->dia6 = 0;$obj7->dia7 = 0;$obj7->total = 0;
        $obj8 = new stdclass();$obj8->tipo = $porc_cartao1.'% CARTÃO';$obj8->dia1 = 0;$obj8->dia2 = 0;$obj8->dia3 = 0;$obj8->dia4 = 0;$obj8->dia5 = 0;$obj8->dia6 = 0;$obj8->dia7 = 0;$obj8->total = 0;
        $obj9 = new stdclass();$obj9->tipo = $porc_cartao2.'% CARTÃO';$obj9->dia1 = 0;$obj9->dia2 = 0;$obj9->dia3 = 0;$obj9->dia4 = 0;$obj9->dia5 = 0;$obj9->dia6 = 0;$obj9->dia7 = 0;$obj9->total = 0;
        $conta = 1;
        $total_entra = 0;
        $total_sai = 0;
        $totval_entra = 0;
        $totval_sai = 0;
        $total_cartao1 = 0;
        $total_cartao2 = 0;
        $a_total_despesas=array('TOTAL',0,0,0,0,0,0,0,0);
//        foreach ($reads as $read) {
        $dia = date('Y-m-d', strtotime("-1 days",strtotime($primeirodia)));
        for($nloop=1;$nloop < 8;$nloop++){
            $dia = date('Y-m-d', strtotime("+1 days",strtotime($dia))).' 00:00:00';
            $noite = date('Y-m-d', strtotime($dia)).' 23:59:59';

            // Recupera Contadores
            $repository = new TRepository('leitura');
            $criteria = new TCriteria;
            $criteria->add(new TFilter('fechado', 'BETWEEN', $dia));
            $criteria->add(new TFilter('', '', $noite));
            $reads = $repository->load($criteria);
            $conta_entra = 0;
            $conta_saida = 0;
            foreach ($reads as $read){
                $conta_entra += (float)(($read->qtde/6) * $read->valor);
                $conta_saida += (float)($read->linha+$read->bingo+$read->bingo2+$read->extra);
            }

            // Recupera Lançamentos
            $repository = new TRepository('movimento');
            $criteria = new TCriteria;
            $criteria->setProperty('order', 'fechado asc');

            $criteria->add(new TFilter('fechado', 'BETWEEN', $dia));
            $criteria->add(new TFilter('', '', $noite));

            $leitura = $repository->load($criteria);
            $vales_entra = 0;
            $vales_saida = 0;
            $cartao1 = 0;
            $cartao2 = 0;
            $cheque = 0;
            foreach ($leitura as $leit){
                if($leit->maquina == 103) {$vales_entra += (float)(($leit->valor));}
                if($leit->maquina == 158) {$vales_saida += (float)(($leit->valor*-1));}
                if($leit->maquina == 151) {$cartao1 += (float)(($leit->valor)*($porc_cartao1/100));}
                if($leit->maquina == 152) {$cartao2 += (float)(($leit->valor)*($porc_cartao2/100));}
                if($leit->maquina == 153) {$cheque += (float)($leit->valor);}
                foreach ($a_despesas as $key => $desp){
                    if($leit->maquina == $key) {
                        $a_despesas[$key][$conta] += (float)(($leit->valor));
                        $a_total_despesas[$conta] += (float)(($leit->valor));
                        $a_despesas[$key][8] += (float)(($leit->valor));
                        $a_total_despesas[8] += (float)(($leit->valor));
                    }
                }
            }

            // Soma cartão na despesa
            $a_total_despesas[$conta] += $cartao1+$cartao2;
            $a_total_despesas[8] += $cartao1+$cartao2;

            $total_entra += $conta_entra;
            $total_sai += $conta_saida;
            $totval_entra += $vales_entra;
            $totval_sai += $vales_saida;
            $total_cartao1 += $cartao1;
            $total_cartao2 += $cartao2;
            if($conta == 1) {
                $obj1->dia1 = $conta_entra;
                $obj2->dia1 = $conta_saida*-1;
                $obj3->dia1 = $conta_entra - $conta_saida;
                $obj5->dia1 = $vales_entra;
                $obj6->dia1 = $vales_saida*-1;
                $obj7->dia1 = $vales_entra - $vales_saida;
                $obj8->dia1 = $cartao1;
                $obj9->dia1 = $cartao2;
            }elseif($conta == 2) {
                $obj1->dia2 = $conta_entra;
                $obj2->dia2 = $conta_saida*-1;
                $obj3->dia2 = $conta_entra - $conta_saida;
                $obj5->dia2 = $vales_entra;
                $obj6->dia2 = $vales_saida*-1;
                $obj7->dia2 = $vales_entra - $vales_saida;
                $obj8->dia2 = $cartao1;
                $obj9->dia2 = $cartao2;
            }elseif($conta == 3) {
                $obj1->dia3 = $conta_entra;
                $obj2->dia3 = $conta_saida*-1;
                $obj3->dia3 = $conta_entra - $conta_saida;
                $obj5->dia3 = $vales_entra;
                $obj6->dia3 = $vales_saida*-1;
                $obj7->dia3 = $vales_entra - $vales_saida;
                $obj8->dia3 = $cartao1;
                $obj9->dia3 = $cartao2;
            }elseif($conta == 4) {
                $obj1->dia4 = $conta_entra;
                $obj2->dia4 = $conta_saida*-1;
                $obj3->dia4 = $conta_entra - $conta_saida;
                $obj5->dia4 = $vales_entra;
                $obj6->dia4 = $vales_saida*-1;
                $obj7->dia4 = $vales_entra - $vales_saida;
                $obj8->dia4 = $cartao1;
                $obj9->dia4 = $cartao2;
            }elseif($conta == 5) {
                $obj1->dia5 = $conta_entra;
                $obj2->dia5 = $conta_saida*-1;
                $obj3->dia5 = $conta_entra - $conta_saida;
                $obj5->dia5 = $vales_entra;
                $obj6->dia5 = $vales_saida*-1;
                $obj7->dia5 = $vales_entra - $vales_saida;
                $obj8->dia5 = $cartao1;
                $obj9->dia5 = $cartao2;
            }elseif($conta == 6) {
                $obj1->dia6 = $conta_entra;
                $obj2->dia6 = $conta_saida*-1;
                $obj3->dia6 = $conta_entra - $conta_saida;
                $obj5->dia6 = $vales_entra;
                $obj6->dia6 = $vales_saida*-1;
                $obj7->dia6 = $vales_entra - $vales_saida;
                $obj8->dia6 = $cartao1;
                $obj9->dia6 = $cartao2;
            }elseif($conta == 7) {
                $obj1->dia7 = $conta_entra;
                $obj2->dia7 = $conta_saida*-1;
                $obj3->dia7 = $conta_entra - $conta_saida;
                $obj5->dia7 = $vales_entra;
                $obj6->dia7 = $vales_saida*-1;
                $obj7->dia7 = $vales_entra - $vales_saida;
                $obj8->dia7 = $cartao1;
                $obj9->dia7 = $cartao2;
            }
            $conta++;
        }

        $obj1->total = $total_entra;
        $obj2->total = $total_sai*-1;
        $obj3->total = $total_entra - $total_sai;
        $obj5->total = $totval_entra;
        $obj6->total = $totval_sai*-1;
        $obj7->total = $totval_entra - $totval_sai;
        $obj8->total = $total_cartao1;
        $obj9->total = $total_cartao2;

        $this->datagrid0->addItem($obj1);
        $this->datagrid0->addItem($obj2);
        $this->datagrid0->addItem($obj3);
        $this->datagrid0->addItem($obj4);
        $this->datagrid0->addItem($obj5);
        $this->datagrid0->addItem($obj6);
        $this->datagrid0->addItem($obj7);
        $this->datagrid0->addItem($obj4);
        if($total_cartao1 != 0){$this->datagrid0->addItem($obj8);}
        if($total_cartao2 != 0){$this->datagrid0->addItem($obj9);}

        foreach ($a_despesas as $desp){
            $obj = new stdclass();
            $obj->tipo = $desp[0];
            $obj->dia1 = $desp[1];
            $obj->dia2 = $desp[2];
            $obj->dia3 = $desp[3];
            $obj->dia4 = $desp[4];
            $obj->dia5 = $desp[5];
            $obj->dia6 = $desp[6];
            $obj->dia7 = $desp[7];
            $obj->total = $desp[8];
            if($desp[8] != 0){$this->datagrid0->addItem($obj);}
        }

        $obj = new stdclass();
        $obj->tipo = 'TOTAL DE DESPESAS';
        $obj->dia1 = $a_total_despesas[1];
        $obj->dia2 = $a_total_despesas[2];
        $obj->dia3 = $a_total_despesas[3];
        $obj->dia4 = $a_total_despesas[4];
        $obj->dia5 = $a_total_despesas[5];
        $obj->dia6 = $a_total_despesas[6];
        $obj->dia7 = $a_total_despesas[7];
        $obj->total = $a_total_despesas[8];
        $this->datagrid0->addItem($obj);

        $this->datagrid0->addItem($obj4);

        $azul = $obj3->total + $obj7->total + $obj->total;

        $obj10 = new stdclass();
        if($azul < 0){$obj10->tipo = 'PREJUÍZO DA SEMANA';}else{$obj10->tipo = 'LUCRO DA SEMANA';}
        $obj10->dia1 = $obj3->dia1 + $obj7->dia1 + $obj->dia1;
        $obj10->dia2 = $obj3->dia2 + $obj7->dia2 + $obj->dia2;
        $obj10->dia3 = $obj3->dia3 + $obj7->dia3 + $obj->dia3;
        $obj10->dia4 = $obj3->dia4 + $obj7->dia4 + $obj->dia4;
        $obj10->dia5 = $obj3->dia5 + $obj7->dia5 + $obj->dia5;
        $obj10->dia6 = $obj3->dia6 + $obj7->dia6 + $obj->dia6;
        $obj10->dia7 = $obj3->dia7 + $obj7->dia7 + $obj->dia7;
        $obj10->total = $obj3->total + $obj7->total + $obj->total;
        $this->datagrid0->addItem($obj10);

        $socios = $this->ini['general']['socios'];
        if( $socios > 1){
            $this->datagrid0->addItem($obj4);
            $obj10 = new stdclass();
            if($azul < 0){$obj10->tipo = 'PREJUÍZO INDIVIDUAL';}else{$obj10->tipo = 'LUCRO INDIVIDUAL';}
            $obj10->dia1 = ($obj3->dia1 + $obj7->dia1 + $obj->dia1)/$socios;
            $obj10->dia2 = ($obj3->dia2 + $obj7->dia2 + $obj->dia2)/$socios;
            $obj10->dia3 = ($obj3->dia3 + $obj7->dia3 + $obj->dia3)/$socios;
            $obj10->dia4 = ($obj3->dia4 + $obj7->dia4 + $obj->dia4)/$socios;
            $obj10->dia5 = ($obj3->dia5 + $obj7->dia5 + $obj->dia5)/$socios;
            $obj10->dia6 = ($obj3->dia6 + $obj7->dia6 + $obj->dia6)/$socios;
            $obj10->dia7 = ($obj3->dia7 + $obj7->dia7 + $obj->dia7)/$socios;
            $obj10->total = ($obj3->total + $obj7->total + $obj->total)/$socios;
            $this->datagrid0->addItem($obj10);
        }

        // creates a frame
        $this->frame = new TFrame;

        $lab_cxdia = new TLabel('dia');
        $lab_cxdia->setFontSize(17);
        $lab_cxdia->setFontFace('Trebuchet MS');
        $lab_cxdia->setValue('FECHAMENTOS - ');
        $this->frame->add($lab_cxdia);

        $lab_dia = new TLabel('dia');
        $lab_dia->setFontSize(17);
        $lab_dia->setFontFace('Trebuchet MS');
        $form_dia = substr($primeirodia,8,2).'/'.substr($primeirodia,5,2).'/'.substr($primeirodia,0,4)." A ".substr($ultimodia,8,2).'/'.substr($ultimodia,5,2).'/'.substr($ultimodia,0,4);
        $lab_dia->setValue($form_dia);
        $this->frame->add($lab_dia);

        // reset the criteria for record count
        $criteria->resetProperties();
        $count= $repository->count($criteria);

        // close the transaction
        TTransaction::close();
        $this->loaded = true;

        // creates the form and a inner table
        $this->form = new TForm('form_pdf_report');
        $table = new TTable;
        $this->form->add($table);

        // creates an action button
        $save_button = new TButton('generate');
        $save_button->setAction(new TAction(array($this, 'onGenerate')), 'Generate');
        $save_button->setImage('fa:check-circle-o green');

        // add a row for the form action
        $table->addRowSet($save_button);

        // define wich are the form fields
        $this->form->setFields(array($save_button));

        // wrap the page content using vertical box
        $vbox = new TVBox;
        $vbox->style = 'width: 100%; text-align: center;';
        $vbox->add($this->frame);
        $vbox->add($this->datagrid0)->style = 'width: 100%; text-align: center;overflow-x:auto;';
//        $vbox->add($this->form);
        parent::add($vbox);

        // converts the HTML template into PDF
//        $this->dompdf = new \Dompdf\Dompdf();
//        $this->dompdf->loadHtml($this->datagrid0->getContents());
//        $this->dompdf->setPaper('A4', 'portrait');
//        $this->dompdf->render();

    }

    /**
     * Format saldo
     */
    public function formatSaldo($stock, $object, $row)
    {

        if($stock == '9999999999'){return "";}
        $number = number_format($stock, 2, ',', '.');
        if($stock < 0){$number = number_format($stock*-1, 2, ',', '.');}
//        if($object->maquinas =='TOTAL DE ENTRADAS'){$row->style = "background: #00BFFF";}
//        if($object->maquinas =='TOTAL DE PAGAMENTOS'){$row->style = "background: #FF3030";}
        $row->style = "background: #C1CDC1";
        if($object->tipo =='SALDO DAS MÁQUINAS'){$row->style = "background: #90EE90";}
        if($object->tipo =='SALDO DOS VALES'){$row->style = "background: #90EE90";}
        if($object->tipo =='TOTAL DE DESPESAS'){$row->style = "background: #90EE90";}
        if($object->tipo =='LUCRO DA SEMANA'){$row->style = "background: #00BFFF";}
        if($object->tipo =='PREJUÍZO DA SEMANA'){$row->style = "background: #FFFF00";}

        if ($stock < 0)
        {
            return "<span style='color:red'>$number</span>";
        }
        else
        {
            return "<span style='color:blue'>$number</span>";
        }
    }
    function onGenerate()
    {
        try
        {
            // write and open file
            file_put_contents('app/output/document.pdf', $this->dompdf->output());

            // open window to show pdf
            $window = TWindow::create(_t('Document HTML->PDF'), 0.8, 0.8);
            $object = new TElement('object');
            $object->data  = 'app/output/document.pdf';
            $object->type  = 'application/pdf';
            $object->style = "width: 100%; height:calc(100% - 10px)";
            $window->add($object);
            $window->show();

        }
        catch (Exception $e) // in case of exception
        {
            new TMessage('error', $e->getMessage());
            TTransaction::rollback();
        }
    }
}
