<?php  
class PurchaseGoodAndServicePDF {
    private $pathfFileCreated = null; 
    private $fileName = null; 
    private $pathfForLink = null;
    
    public function __construct($idVenta,$createFile = null) {
        $pdf = new PDF();
        $shipping = new PurchaseGoodAndServiceRepository();        
        $shippingData = $shipping->getById($idVenta);
        $shipping->setOptions($shippingData);
        $shippingDetalles = $shipping->getPurchaseDetailsSaved($idVenta);
        $vendorData = $shipping->getVendorInfoComplete();
        
        $empresa = new CompanyRepository();
        $empresaData = $empresa->getById(1);
        
        $pdf = new PDF();
        if($shippingData['status']=='2'){
            //Modifique metodo AddPage para qe ricibiera un tercer parametro, dicho parametro contiene el texto para a marca de agua.
            // para crear la marca de agua agregue las funciones Header y RotatedText en pdf.php
            $pdf->AddPage('','','','Canceled');
        }elseif($shippingData['status']=='1'){
            //Modifique metodo AddPage para qe ricibiera un tercer parametro, dicho parametro contiene el texto para a marca de agua.
            // para crear la marca de agua agregue las funciones Header y RotatedText en pdf.php
            $pdf->AddPage('','',' Pending');
        }
        else{
            $pdf->AddPage();
        }
        
        $pdf->SetFillColor(200,200,200);
        
        $pdf->SetFont('Arial','B','16');
        $pdf->Cell(190,7, 'PURCHASE #'.$shipping->getPurchaseNumber(), '0', 1, 'R');

        $pdf->Image(ROOT."/public/app/img/logo.png",10,2,50,0);
        
        /* PARA PONER IMG COMO MARCA DE AGUA
        $pdf->SetAlpha(0.5);   
        $pdf->Image(ROOT."/public/img/pdf_background.jpg",50,100,100,0);
        $pdf->SetAlpha(1);   */
        
        $pdf->Ln(2);           
        $pdf->Cell(135, 5, "", '0', 0, 'L');
        $pdf->SetFont('Arial','B','8');
        $pdf->Cell(30, 5, "Purchase Date ", '0', 0, 'L');
        $pdf->SetFont('Arial','','8');
        $pdf->Cell(25, 5, $shippingData['formatedDate'], '0', 1, 'R');  
        
        $pdf->Cell(135, 5, "", '0', 0, 'L');
        $pdf->SetFont('Arial','B','8');
        $pdf->Cell(30, 5, "Payment Terms", '', 0, 'L');
        $pdf->SetFont('Arial','','8');
        $pdf->Cell(25, 5, $shippingData['paymentTermsName'], '0', 1, 'R');     
        
        $pdf->Cell(135, 5, "", '0', 0, 'L');
        $pdf->SetFont('Arial','B','8');
        $pdf->Cell(30, 5, "Due Date", '', 0, 'L');
        $pdf->SetFont('Arial','','8');
        $pdf->Cell(25, 5, $shippingData['formatedDueDate'], '0', 1, 'R');            
        
        $pdf->SetFont('Arial','B','8');        
        $pdf->Cell(35, 5, "BILL TO", '0', 1, 'L');
        $pdf->ln(1);
        
        $pdf->SetFont('Arial','B','10');
        $pdf->Cell(120, 5, $empresaData['name'], '0', 1, 'L');
        
        $pdf->SetFont('Arial','','8');        
        $direccion = '';
        if(trim($empresaData['address'])!== ''){$direccion .= $empresaData['address'];}
        if(trim($empresaData['city'])!== ''){$direccion .= "\n".$empresaData['city'];}
        if(trim($empresaData['state'])!== ''){$direccion .= ",".$empresaData['state'];}
        if(trim($empresaData['zipcode'])!== ''){$direccion .= " ".$empresaData['zipcode'];}
        if(trim($empresaData['phone'])!== ''){$direccion .= "\nPhone:".$empresaData['phone'];}        
        $pdf->Cell(70, 10, $direccion, '0', 1, 'L');            
               
        $pdf->ln(5);
       
        $pdf->SetFont('Arial','B','8');        
        $pdf->Cell(35, 5, "VENDOR", '0', 1, 'L');
        $pdf->ln(1);
        
        $pdf->SetFont('Arial','B','10');
        $pdf->Cell(120, 5, utf8_decode($vendorData['name']), '0', 1, 'L');
        
        $pdf->SetFont('Arial','','8');
        $vendorString = '';
        if(trim($vendorData['address'])!== ''){$vendorString .= $vendorData['address'];}
        if(trim($vendorData['city'])!== ''){$vendorString .= "\n".$vendorData['city'];}
        if(trim($vendorData['state'])!== ''){$vendorString .= ",".$vendorData['state'];}
        if(trim($vendorData['zipcode'])!== ''){$vendorString .= " ".$vendorData['zipcode'];}
        if(trim($vendorData['phone'])!== ''){$vendorString .= "\nPhone:".$vendorData['phone'];}        
        $pdf->Cell(170, 10, $vendorString, '0', 1, 'L');    

        $pdf->Ln(5);        
        $pdf->SetFont('Arial','B','8');
        $pdf->Cell(20, 6, "CODE", 'B', 0, 'C');
        $pdf->Cell(115, 6, "DESCRIPTION", 'B', 0, 'L');
        $pdf->Cell(15, 6, "QUANTITY", 'B', 0, 'R');
        $pdf->Cell(20, 6, "COST", 'B', 0, 'R'); 
        $pdf->Cell(20, 6, "TOTAL", 'B', 1, 'R');      
        
        $cantidadItems = 0;
        $total_importe = 0;
        $total_descuentos = 0;
        $total_subtotal = 0;
        $total_impuestos = 0;
        $taxes_applied = array();
        $total = 0;
        
        $pdf->SetFont('Arial','','8');
        foreach($shippingDetalles as $detalle){      
            $cantidadItems += $detalle['quantity'];
            $total_importe += $detalle['cost'] * $detalle['quantity'];
            $total_descuentos += $detalle['discount_amount'] + $detalle['discount_general_amount'];
            $total_subtotal += $detalle['amount'];
            $total_impuestos += $detalle['taxes_amount'];
            $total += $detalle['total'];
            
            if(isset($taxes_applied[$detalle['taxName'].' '.$detalle['taxes_rate']], $taxes_applied)){
                $taxes_applied[$detalle['taxName']] += $detalle['taxes_amount'];
            }else{
                $taxes_applied[$detalle['taxName']] = $detalle['taxes_amount'];
            }             
            
            if($detalle['description_details'] != ''){
                $pdf->Cell(20, 5, htmlentities($detalle['code']), '0', 0, 'C');
                $pdf->Cell(115, 5, utf8_decode($detalle['description']), '0', 0, 'L');
                $pdf->Cell(15, 5, number_format($detalle['quantity'],2), '0', 0, 'R');
                $pdf->Cell(20, 5, number_format($detalle['cost'],2), '0', 0, 'R');
                $pdf->Cell(20, 5, number_format($detalle['total'],2), '0', 1, 'R');
                
                $x = $pdf->GetX();
                $y = $pdf->GetY();
                
                $pdf->SetXY($x + 22, $y); /*<=== aqui le puse 25 en lugar de 20; para dejarle margen isquierdo*/
                $pdf->Multicell(115, 4, htmlentities($detalle['description_details']), 'B','L');
                
                $yDesc = $pdf->GetY() - $y;
                
                $pdf->SetXY($x, $y);
                $pdf->Multicell(22, $yDesc, '', 'B','C');
                
                $pdf->SetXY($x + 135, $y);                
                $pdf->Multicell(15, $yDesc, '', 'B','C');
                
                $pdf->SetXY($x + 150, $y);                
                $pdf->Multicell(20, $yDesc, '', 'B','C');
                
                $pdf->SetXY($x + 170, $y);                
                $pdf->Multicell(20, $yDesc, '', 'B','C');

            }else{
                $pdf->Cell(20, 5, htmlentities($detalle['code']), 'B', 0, 'C');
                $pdf->Cell(115, 5, utf8_decode($detalle['description']), 'B', 0, 'L');
                $pdf->Cell(15, 5, number_format($detalle['quantity'],2), 'B', 0, 'R');
                $pdf->Cell(20, 5, number_format($detalle['cost'],2), 'B', 0, 'R');
                $pdf->Cell(20, 5, number_format($detalle['total'],2), 'B', 1, 'R');
            }            
        }          
        
        $pdf->Ln(5);  
        
        if($total_descuentos > 0 || $total_impuestos > 0){
            $pdf->SetFont('Arial','B','8');
            $pdf->Cell(170, 5, 'SUBTOTAL', '0', 0, 'R');
            $pdf->SetFont('Arial','','8');
            $pdf->Cell(20, 5, number_format($total_importe,2), '0', 1, 'R');

            $pdf->SetFont('Arial','B','8');
            $pdf->Cell(170, 5, 'DISCOUNTS', '0', 0, 'R');
            $pdf->SetFont('Arial','','8');
            $pdf->Cell(20, 5, '-'.number_format($total_descuentos,2), '0', 1, 'R');

            if($total_impuestos > 0){
                foreach($taxes_applied as $taxName => $taxAmount){
                    if($taxAmount > 0){
                        $pdf->SetFont('Arial','B','8');
                        $pdf->Cell(170, 5, $taxName, '0', 0, 'R');
                        $pdf->SetFont('Arial','','8');
                        $pdf->Cell(20, 5, number_format($taxAmount,2), '0', 1, 'R');
                    }                
                }
            }
        }        
        
        $pdf->SetFont('Arial','B','8');
        $pdf->Cell(170, 5, 'TOTAL', '0', 0, 'R');
        $pdf->SetFont('Arial','','8');
        $pdf->Cell(20, 5, number_format($total,2), '0', 1, 'R');
         
        if($createFile){
            $name = "Purchase-{$shipping->getPurchaseNumber()}.pdf";
            $pdf->Output(ROOT."/app/resources/docs/temp/pdf/purchase/$name","F");
            $this->fileName  = $name;
            $this->pathfFileCreated  = ROOT."/app/resources/docs/temp/pdf/purchase/$name";
            $this->pathfForLink  = "/app/resources/docs/temp/pdf/purchase/$name";
        }else{
            $pdf->Output();
        }
    }    
        
    public function getPathFileCreated(){
        return $this->pathfFileCreated;
    }
    
    public function getFileName(){
        return $this->fileName;
    }
    
    public function getPathForLink(){
        return $this->pathfForLink;
    }
}