<?php  
class WastePDF {
    private $pathfFileCreated = null; 
    public function __construct($idStoreRequest,$createFile = null) {
        $waste = new WasteRepository();
        $wasteData = $waste->getById($idStoreRequest);
        $waste->setOptions($wasteData);
        $waste->setId($idStoreRequest);       
        
        $empresa = new CompanyRepository();
        $empresa->setOptions($empresa->getById(1));        
        
        $sucursal = new StoreRepository();     
        $sucursal->setOptions($sucursal->getById($wasteData['store_id']));
        
        //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 = new PDF();
        if($wasteData['status'] == '2'){
            $pdf->AddPage('','','  Cancelada');
        }else{
            $pdf->AddPage('','');
        }
        
        $pdf->SetTextColor(0); 
        $y = $pdf->GetY();
        $pdf->SetFont('Arial','B','12');
        $pdf->Cell(120,7, strtoupper($empresa->getName()), '0', 1, 'C');   
        $pdf->SetFont('Arial','B','12');
        $pdf->Cell(120,5, "FECHA DE MERMA: ".$wasteData['formatedDate'], '0', 1, 'C'); 
        $pdf->SetFont('Arial','','9');
        $pdf->Cell(120,5, "CREADO POR: ".strtoupper($wasteData['created_by']), '0', 1, 'C');    
        if(!is_null($wasteData['last_modification_by']) && $wasteData['last_modification_by'] !== ''){
            $pdf->Cell(120,5, "ULTIMA ACTUALIZACION POR : ".strtoupper($wasteData['last_modification_by'])." EN ".$wasteData['last_modification_at'], '0', 1, 'C');                        
        }
        
        $pdf->Cell(120,5, "FECHA DE IMPRESION : ".date('m/d/Y h:i:s A'), '0', 1, 'C');                        
        
        $pdf->SetY($y);
        $pdf->SetFont('Arial','','10');
        $pdf->Cell(120,5, "", '0', 0, 'C'); /*Margin left*/
        $pdf->Cell(70,5, "MERMA", '0', 1, 'R'); 
        $pdf->Cell(120,5, "", '0', 0, 'C'); /*Margin left*/
        $pdf->SetFont('Arial','B','16');
        $pdf->Cell(70,5, '#'.$wasteData['waste_num'], '0', 1, 'R'); 
        
        $pdf->Ln(2);
        $pdf->SetFont('Arial','','10');
        $pdf->Cell(120,5, "", '0', 0, 'C'); /*Margin left*/
        $pdf->Cell(70,5, "SUCURSAL", '0', 1, 'R'); 
        $pdf->Cell(120,5, "", '0', 0, 'C'); /*Margin left*/
        $pdf->SetFont('Arial','B','16');
        $pdf->Cell(70,5, strtoupper($sucursal->getName()), '0', 1, 'R');       
       
        $pdf->Ln(10);        
        $pdf->SetFont('Arial','B','10');
        
        $y = $pdf->GetY();
        $pdf->Cell(5, 7, "#", 'B', 0, 'L');
        $pdf->Cell(58, 7, "Producto", 'B', 0, 'L');
        $pdf->Cell(15, 7, "Cantidad", 'B', 0, 'R'); 
        $pdf->Cell(15, 7, "Costo", 'B', 0, 'R'); 
        $pdf->Cell(15, 7, "Precio", 'B', 0, 'R'); 
        $pdf->Cell(20, 7, "Total costo", 'B', 0, 'R'); 
        $pdf->Cell(20, 7, "Total venta", 'B', 1, 'R'); 
        
        $detalles = $waste->getWasteDetailsByWasteId($idStoreRequest);
        $totalProducts = 0;
        $totalCost = 0;
        $totalPrice = 0;
        $i = 1;
        
        foreach($detalles as $detalle){      
            $totalProducts += round($detalle['quantity'],2);
            $totalCost += round($detalle['total_cost'],2);
            $totalPrice += round($detalle['total_price'],2);
           
            $pdf->SetFont('Arial','','10');
            $pdf->Cell(5, 5, $i, 'B', 0, 'C');
            $pdf->Cell(58, 5, utf8_decode($detalle['product']), 'B', 0, 'L');
            $pdf->Cell(15, 5, number_format($detalle['quantity']), 'B', 0, 'R');            
            $pdf->Cell(15, 5, '$'.number_format($detalle['cost'],2), 'B', 0, 'R'); 
            $pdf->Cell(15, 5, '$'.number_format($detalle['price'],2), 'B', 0, 'R'); 
            $pdf->Cell(20, 5, '$'.number_format($detalle['total_cost'],2), 'B', 0, 'R');             
            $pdf->Cell(20, 5, '$'.number_format($detalle['total_price'],2), 'B', 1, 'R');            
            $i++;
        }       

        $pdf->SetFont('Arial','B','10');
        $pdf->Cell(63, 5, 'TOTAL', '', 0, 'L');
        $pdf->Cell(15, 5, number_format($totalProducts), '', 0, 'R');
        $pdf->Cell(15, 5,'', '', 0, 'R');
        $pdf->Cell(15, 5,'', '', 0, 'R');
        $pdf->Cell(20, 5, '$'.number_format($totalCost,2), '', 0, 'R');
        $pdf->Cell(20, 5, '$'.number_format($totalPrice,2), '', 1, 'R');
     
        
        $pdf->Ln(5);        
        $pdf->SetFont('Arial','B','10');
        $pdf->Cell(93, 7, "Notas de merma", 'B', 1, 'L');
        $pdf->Cell(93, 2, "", '0', 1, 'L');
        $pdf->SetFont('Arial','','10');
        $pdf->Multicell(93,3,  $wasteData['comments'], '0', 1, 'L');

        if($createFile){
            if(!is_dir(PATH_TEMP_DOCS."waste/")){
                mkdir(PATH_TEMP_DOCS."waste/",0777,true);
            }
            $pdf->Output(PATH_TEMP_DOCS."waste/WASTE-".$idStoreRequest.".pdf","F");
            $this->pathfFileCreated = PATH_TEMP_DOCS."waste/WASTE-".$idStoreRequest.".pdf";
        }else{
            $pdf->Output();            
        }
    }
    
    public function getPathFileCreated(){
        return $this->pathfFileCreated;
    }
}