<?php

        $estadoDeCuenta = $data['data'];
       // var_dump($estadoDeCuenta);exit;
        $pdf = new PDF();
        $pdf->AddPage('', '');
        
        $company = new CompanyRepository();
        $company->setOptions($company->getById(1));
        $pdf->SetFillColor(200,200,200);        
        $pdf->SetFont('Arial','B','12');
        $pdf->Cell(190,7, $company->getName(), '0', 1, 'R');
        $x = $pdf->GetX();
        $pdf->SetX(0);
        $pdf->Cell(10,15, "", '0', 0, 'L',true);
        $pdf->Cell(60,15, "Estado de cuenta sucursales", '0', 1, 'R',true);
        $pdf->SetX($x);
        $pdf->SetFont('Arial','','8');
        $pdf->Cell(60, 5, 'Creado: '.date('d/m/Y').' at '.date('H:i:s'), 0,1,'R');
        $pdf->ln(3);
        $pdf->SetFont('Arial', '', '8');
        $pdf->SetTextColor(0);
       
        foreach ($estadoDeCuenta as $idCliente => $info) {
            $datosCliente = $info['datosCliente'];
            // var_dump($datosCliente); exit;
            $saldoPendienteTotalPorCliente = 0;
            
            $pdf->SetFont('Arial', 'B', '8');
            $pdf->cell(100, 4, "Sucursal: " .$datosCliente['customer_name'], 0, 1, 'L');
            //$pdf->cell(100, 4, "Telefono: " .$datosCliente['phone'], 0, 1, 'L');
          //  $pdf->cell(100, 4, "E-mail: " .$datosCliente['email'], 0, 1, 'L');
            //  $datosCliente['nombre'];
                          
            $pdf->SetFont('Arial', 'B', '8');
            $pdf->Cell(15, 5, "Fecha", 'B', 0, 'C');
            $pdf->Cell(25, 5, "Area", 'B', 0, 'C');
            $pdf->Cell(40, 5, "No Factura", 'B', 0, 'C');
            $pdf->Cell(20, 5, "Total", 'B', 0, 'R');
            $pdf->Cell(20, 5, "Saldo", 'B', 0, 'R');
            $pdf->Cell(20, 5, "Estado", 'B', 0, 'C');
            $pdf->Cell(55, 5, "Saldo", 'B', 1, 'R');
            $pdf->Ln(1);
            
            $pdf->SetFont('Arial', '', '8');
            foreach ($info['payments_by_invoice_id'] as $factura) {
                $pdf->SetFillColor(247,248,249);
                
                $saldoPendiente = $factura['total_sale'];
                $saldoPendienteTotalPorCliente +=  $factura['balance'];
             
                $pdf->Cell(15, 5, $factura['formatedDate'], '0', 0, 'C', true);
                $pdf->Cell(25, 5, $factura['document_name'], '0', 0, 'C', true);
                $pdf->Cell(40, 5, htmlentities($factura['invoice_num']), '0', 0, 'C', true);
                $pdf->Cell(20, 5, '$'.number_format($factura['total'], 2), '0', 0, 'R', true);
                $pdf->Cell(20, 5, '$'.number_format($factura['total'], 2), '0', 0, 'R', true);
                $pdf->Cell(20, 5, $factura['statusName'], '0', 0, 'C', true);
                $pdf->Cell(55, 5, '$'.number_format($factura['balance'], 2), '0', 1, 'R', true);
                  
                #Si no existen pagos no se recoree array
                if (isset($factura['payments']) && $factura['payments'] != '' && $factura['payments'] != null) {
                    $pagos = $factura['payments'];
                    
                    foreach ($pagos as $pago) {
                        $comprobante = "Pago";
                        $monto = $pago['payment_amount'];
                        if ($pago['payment_status']!='3') {
                            $saldoPendiente -=  $monto;
                        }
                        $pdf->Cell(15, 8, $pago['payment_date'], '0', 0, 'C');
                        //comprobante
                        $pdf->Cell(25, 8, htmlentities($comprobante), '0', 0, 'C');
                        //$pdf->Cell(15, 5, $pago['serie'], '0', 0, 'C');
                        $pdf->Cell(40, 8, substr(utf8_decode($pago['payment_method']),0,25)."\n".$pago['payment_num_operation'], '0', 0, 'C');
                        
                        //Tipo documento
                        //$pdf->Cell(30, 5, $pago['tipo'], '0', 0, 'C');
                        //importe
                        $pdf->Cell(20, 8, '$' . number_format($pago['payment_amount'], 2), '0', 0, 'R');
                        //saldo resto de deuda
                        $pdf->Cell(20, 8, '$' .number_format($saldoPendiente, 2), '0', 0, 'R');
                        //estado de doc
                        $pdf->Cell(20, 8, $pago['payment_status_name'], '0', 0, 'C');
                        //saldopendiente
                        $pdf->Cell(30, 8, '', 0, 1, 'R');
                    }
                
                }
            }
            $pdf->Ln(1);
            $pdf->Cell(165, 5, "Saldo total Pendiente", '1', 0, 'R');
            $pdf->Cell(30, 5, '$'.number_format($saldoPendienteTotalPorCliente, 2), '1', 1, 'R');
            $pdf->Ln(5);
        }
        
        if ($this->saveFile == true) {
            $pdf->Output($this->getTempFolder()."/".$this->getNombreArchivo().".pdf", "F");
        } else {
            $pdf->Output('D','EstadoDeCuentaSucursales.pdf');
        }
