<?php
class ShipmentStoreRequestPDF
{
    private $pathfFileCreated = null;
    public function __construct($idShipment, $createFile = null)
    {
        $shipment = new ShipmentStoreRequestRepository();
        $shipmentData = $shipment->getById($idShipment);
        $shipment->setOptions($shipmentData);
        $shipment->setId($idShipment);

        $empresa = new CompanyRepository();
        $empresa->setOptions($empresa->getById(1));

        //UG PARA PODER CONTROLAR EL MONTO DEL ENVIO
        $settings = new SettingsRepository();
        $shipping_fee = $settings->_get('shipping_fee');

        //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();

        $detalles = $shipment->getShipmentDetailsSaved($idShipment, true);
        $totalEnviado = 0;
        foreach ($detalles as $detalle) {
            $totalEnviado += $detalle['quantity'];
        }
        $etiquetas = ceil($totalEnviado / 20);
        //Yo cambie la variable $reptir ya no se ocupa solo quieren una sola hoja.
        //@andres el dia Nov/29/2022
        for ($repetir = 1; $repetir <= 1; $repetir++) {
            if ($shipment->getStatus() == '4') {
                $pdf->AddPage('', '', 0, 'Cancelado');
            } else {
                $pdf->AddPage('', '');
            }


            $pdf->SetTextColor(0);
            $y = $pdf->GetY();
            $pdf->SetFont('Arial', 'B', '36');
            $pdf->Cell(95, 20, strtoupper($shipmentData['to_store_name']), '0', 1, 'C');
            $pdf->SetFont('Arial', 'B', '12');
            $pdf->Cell(95, 5, "FECHA DE ENTREGA: " . $shipmentData['deliveryDateFormated'], '0', 1, 'C');
         
            $pdf->SetY($y);
            $pdf->SetFont('Arial', '', '10');
            $pdf->Cell(95, 5, "", '0', 0, 'C'); /*Margin left*/
            $pdf->Cell(95, 5, "ENVIO", '0', 1, 'R');
            $pdf->Cell(95, 5, "", '0', 0, 'C'); /*Margin left*/
            $pdf->SetFont('Arial', 'B', '16');
            $pdf->Cell(95, 5, '#' . $idShipment, '0', 1, 'R');

            $barCode = new BarCode();
            $barCode->setBarCode(array(
                'filepath' => PATH_TEMP_DOCS . '/barcodes/' . $shipment->getNumShipment() . '.png',
                'text' => $shipment->getNumShipment(),
                'print' => false
            ));
            $pdf->Image(PATH_TEMP_DOCS . 'barcodes/' . $shipment->getNumShipment() . '.png', 163, 20, 40, 0);

            $pdf->Ln(15);
            $pdf->SetFont('Arial', 'B', '11');
            $y = $pdf->GetY();
            $pdf->Cell(6, 7, "#", 'B', 0, 'L');
            $pdf->Cell(63, 7, "Descripcion", 'B', 0, 'L'); // Reducido de 58 a 48

            $pdf->Cell(15, 7, "PzxCh", 'B', 0, 'R'); // Aumentado de 15 a 25 //UG New colum, unidades_por_charola
            $pdf->Cell(15, 7, "CxPz", 'B', 0, 'R');
            $pdf->Cell(15, 7, "Pedido", 'B', 0, 'R');
            $pdf->Cell(15, 7, "Enviado", 'B', 0, 'R');
            $pdf->Cell(15, 7, "Piezas", 'B', 0, 'R'); // Aumentado de 15 a 25 //UG New en el excel


            //$pdf->Cell(15, 7, "Pedido", 'B', 0, 'R');//No viene en el excel, por lo tanto se borra
            // $pdf->Cell(15, 7, "CxCh", 'B', 0, 'R'); // Aumentado de 15 a 25 //UG New name


            $pdf->Cell(20, 7, "Ctotal", 'B', 0, 'R'); // Aumentado de 15 a 25

            $pdf->Cell(30, 7, "VEstimada", 'B', 1, 'R'); // Aumentado de 15 a 25

            $existeSegundaColumna = null;
            $totalEnviado = 0;
            $totalRecibido = 0;
            $totalPzas = 0;
            $onlysale_price_total = 0;
            $totalVEstimada = 0;
            $piezas = 0;
            $VEstimada = 0;
            $Ctotal = 0;
            $CxPz = 0;
            $i = 1;
            $j = 1;
            $k = 1;
            $yForComments = $pdf->GetY();
            $totalsale_price = 0;
            foreach ($detalles as $detalle) {
                
                //UG validamos si existe datos en el campo de extra_pzas //PARA HACER EL CALCULO, NECESITA EXISTIR UNIDADES POR CHAROLA
                if($detalle['extra_pzas'] !== null && $detalle['extra_pzas'] !== '0' && $detalle['unidades_por_charola'] != 0){

                    //Calculamos el porcentaje de extra_pzas Formula = extra_pzas / unidades_por_charola
                    $porcentaje = $detalle['extra_pzas'] / $detalle['unidades_por_charola'];
                    $detalle['quantity'] =  $detalle['quantity'] + $porcentaje;

                }

                if ($detalle['unidades_por_charola'] != 0 && $detalle['sale_price'] != 0) {
                    $CxPz = $detalle['sale_price'] / $detalle['unidades_por_charola'];
                } else {
                    $CxPz = 0;
                }
                $totalEnviado += $detalle['real_stock_in_store']; //ccambie esto tenia real stock... pudin //UG Regresa al campo original real_stock_in_store antes quantity
                $totalRecibido += $detalle['quantity']; //cambie esto, estaba quantity ... pudin // UG lo regreso a real_stock_in_store antes estaba received //UG regresa al campo original quantity antes real_stock_in_store
                 
                $totalsale_price += $detalle['quantity'] * $detalle['sale_price'];
                $subtotal_sale_price = $detalle['quantity'] * $detalle['sale_price'];

                if($detalle['quantity'] == 0){
                    $piezas = 0;
                    $VEstimada = 0;
                    $Ctotal = 0;
                }else{
                    $piezas = $detalle['quantity'] * $detalle['unidades_por_charola'];

                    $VEstimada =  $detalle['wholesale_price'] * $piezas ;
                    $Ctotal = $detalle['quantity'] * $CxPz * $detalle['unidades_por_charola'];//UG NEW calculation para PzxCh
                }
               
                
                $totalPzas += $piezas;
                $totalVEstimada += $VEstimada;
                $onlysale_price_total += $detalle['sale_price'];
                
            
                $pdf->SetFont('Arial', '', '12');
                $pdf->Cell(6, 5, $i, 'B', 0, 'C');
                $pdf->Cell(63, 5, utf8_decode($detalle['description']), 'B', 0, 'L'); // Ajustado de 58 a 48

                $pdf->Cell(15, 5, number_format($detalle['unidades_por_charola'], 2), 'B', 0, 'R'); // PzxCh
                $pdf->Cell(15, 5, number_format($CxPz, 2), 'B', 0, 'R'); // Pza x Charola * sale_price
                $pdf->Cell(15, 5, number_format($detalle['real_stock_in_store']), 'B', 0, 'R'); //ccambie esto tenia real stock... pudin //UG Regresa al campo original real_stock_in_store antes quantity
                $pdf->Cell(15, 5, number_format($detalle['quantity']), 'B', 0, 'R'); //cambie esto, estaba quantity ... pudin // UG lo regreso a real_stock_in_store antes estaba received //UG regresa al campo original quantity antes real_stock_in_store
                $pdf->Cell(15, 5, number_format($piezas), 'B', 0, 'R'); 
                //$pdf->Cell(15, 5, number_format($detalle['quantity']), 'B', 0, 'R'); ////No viene en el excel, por lo tanto se borra

                // $pdf->Cell(15, 5, number_format($detalle['sale_price'], 2), 'B', 0, 'R'); // Aumentado de 15 a 25

                


                $pdf->Cell(20, 5, number_format($Ctotal, 2), 'B', 0, 'R'); // Aumentado de 15 a 25

                $pdf->Cell(30, 5, number_format($VEstimada, 2), 'B', 1, 'R'); // Aumentado de 15 a 25

                $yForComments = $pdf->GetY();
                $i++;
                $j++;
                $k++;
            }
            //UG asignamos monto de envio a la tienda
            if($shipping_fee != null){
                $totalsale_price += $shipping_fee;
            }else{
                $shipping_fee = 0;
            }


            // /*TOTAL CHAROLAS*/
            // if (is_null($existeSegundaColumna)) {
            //     // $pdf->SetFont('Arial', 'B', '12');
            //     // $pdf->Cell(5, 5, '', '', 0, 'C');
            //     // $pdf->Cell(58, 5, 'TOTAL', '', 0, 'L');
            //     // $pdf->Cell(15, 5, number_format($totalEnviado), '', 0, 'R');
            //     // $pdf->Cell(15, 5, number_format($totalRecibido), '', 1, 'R');

            //     // Agregar fila para Shipping Fee
            //     $pdf->SetFont('Arial', 'B', '12');
            //     $pdf->Cell(5, 5, '', '', 0, 'C');
            //     $pdf->Cell(30, 5, 'Shipping Fee', '', 0, 'L');
            //     $pdf->Cell(13, 5, '', '', 0, 'R');
            //     $pdf->Cell(13, 5, '', '', 0, 'R');
            //     $pdf->Cell(15, 5, '', '', 0, 'R');
            //     $pdf->Cell(20, 5, number_format($shipping_fee, 2), '', 1, 'R');
            //     $pdf->Ln(2);
            //     $pdf->SetFont('Arial', 'B', '12');
            //     $pdf->Cell(5, 5, '', '', 0, 'C');
            //     $pdf->Cell(30, 5, 'TOTAL', '', 0, 'L'); // Reducido de 58 a 48
            //     $pdf->Cell(13, 5, number_format($totalEnviado), '', 0, 'R');
            //     $pdf->Cell(13, 5, number_format($totalRecibido), '', 0, 'R');
            //     $pdf->Cell(15, 5, '', '', 0, 'R');

            //     $pdf->Cell(20, 5, number_format($totalsale_price, 2), '', 1, 'R'); // Aumentado de 15 a 25

            // } else {

            //     $pdf->SetFont('Arial', 'B', '12');
            //     $pdf->Cell(98, 7, "", '', 0, 'L'); /*Margin left*/
            //     // Agregar fila para Shipping Fee
            //     $pdf->Cell(5, 5, '', '', 0, 'C');
            //     $pdf->Cell(30, 5, 'Shipping Fee', '', 0, 'L');
            //     $pdf->Cell(13, 5, '', '', 0, 'R');
            //     $pdf->Cell(13, 5, '', '', 0, 'R');
            //     $pdf->Cell(15, 5, '', '', 0, 'R');
            //     $pdf->Cell(20, 5, number_format($shipping_fee, 2), '', 1, 'R');
            //     //$pdf->Ln(40);
            //     // $pdf->Cell(5, 5, '', '', 0, 'C');
            //     // $pdf->Cell(58, 5, 'TOTAL', '', 0, 'C');
            //     // $pdf->Cell(15, 5, number_format($totalEnviado), '', 0, 'R');
            //     // $pdf->Cell(15, 5, number_format($totalRecibido), '', 1, 'R');
            //    // $pdf->SetFont('Arial', 'B', '12');

            //    $pdf->Cell(98, 7, "", '', 0, 'L'); /*Margin left*/
            //    // Agregar fila para Shipping Fee
            //    $pdf->Cell(5, 5, '', '', 0, 'C');
            //    $pdf->Cell(30, 5, 'TOTAL', '', 0, 'L');
            //    $pdf->Cell(13, 5, number_format($totalEnviado), '', 0, 'R');
            //    $pdf->Cell(13, 5, number_format($totalRecibido), '', 0, 'R');
            //    $pdf->Cell(15, 5, '', '', 0, 'R');
            //    $pdf->Cell(20, 5, number_format($totalsale_price, 2), '', 1, 'R');


            //     // $pdf->Cell(5, 5, '', '', 0, 'C');
            //     // $pdf->Cell(30, 5, 'TOTAL', '', 0, 'L'); // Reducido de 58 a 48
            //     // $pdf->Cell(13, 5, number_format($totalEnviado), '', 0, 'R');
            //     // $pdf->Cell(13, 5, number_format($totalRecibido), '', 0, 'R');
            //     // $pdf->Cell(15, 5, '', '', 0, 'R');

            //     // $pdf->Cell(20, 5, number_format($totalsale_price, 2), '', 1, 'R'); // Aumentado de 15 a 25

            // }
            //$pdf->Ln(20);
            $pdf->SetFont('Arial', 'B', '12');
            // $pdf->Cell(98, 7, "", '', 0, 'L'); /*Margin left*/
            // Agregar fila para Shipping Fee
            $pdf->Cell(6, 5, '', '', 0, 'C');
            //$pdf->Cell(63, 5, 'Shipping Fee', '', 0, 'L');
            $pdf->Cell(15, 5, '', '', 0, 'R');
            $pdf->Cell(15, 5, '', '', 0, 'R');
            // $pdf->Cell(15, 5, '', '', 0, 'R');
            $pdf->Cell(15, 5, '', '', 0, 'R');

            $pdf->Cell(15, 5, '', '', 0, 'R');
            //$pdf->Cell(20, 5, number_format($shipping_fee, 2), '', 0, 'R');
            $pdf->Cell(30, 5, '', '', 1, 'R');

            //$pdf->Ln(40);
            // $pdf->Cell(5, 5, '', '', 0, 'C');
            // $pdf->Cell(58, 5, 'TOTAL', '', 0, 'C');
            // $pdf->Cell(15, 5, number_format($totalEnviado), '', 0, 'R');
            // $pdf->Cell(15, 5, number_format($totalRecibido), '', 1, 'R');
           // $pdf->SetFont('Arial', 'B', '12');

        //    $pdf->Cell(98, 7, "", '', 0, 'L'); /*Margin left*/
           // Agregar fila para Shipping Fee
           $pdf->Cell(6, 5, '', '', 0, 'C');
           $pdf->Cell(63, 5, 'TOTAL', '', 0, 'L');
           $pdf->Cell(15, 5, '', '', 0, 'R'); //number_format($totalEnviado)
           
           $pdf->Cell(15, 5,'' , '', 0, 'R');
           $pdf->Cell(15, 5,number_format($totalEnviado) , '', 0, 'R'); 

          
           $pdf->Cell(15, 5, number_format($totalRecibido), '', 0, 'R');
           $pdf->Cell(15, 5, number_format($totalPzas), '', 0, 'R');

           $pdf->Cell(20, 5, number_format($totalsale_price, 2), '', 0, 'R');
           $pdf->Cell(30, 5, number_format($totalVEstimada,2), '', 1, 'R');


            // $pdf->Cell(5, 5, '', '', 0, 'C');
            // $pdf->Cell(30, 5, 'TOTAL', '', 0, 'L'); // Reducido de 58 a 48
            // $pdf->Cell(13, 5, number_format($totalEnviado), '', 0, 'R');
            // $pdf->Cell(13, 5, number_format($totalRecibido), '', 0, 'R');
            // $pdf->Cell(15, 5, '', '', 0, 'R');

            // $pdf->Cell(20, 5, number_format($totalsale_price, 2), '', 1, 'R'); // Aumentado de 15 a 25

            $pdf->Ln(20);
            $pdf->SetY($yForComments);
            $pdf->Ln(20);

            $pdf->SetFont('Arial', 'B', '8');
            $pdf->Cell(85, 7, "Notas envio", 'B', 1, 'L');
            $pdf->SetFont('Arial', '', '8');
            $pdf->Multicell(85, 4, utf8_decode($shipment->getComments()));

            $pdf->SetY($yForComments);
            $pdf->Ln(20);

            $pdf->SetFont('Arial', 'B', '8');
            $pdf->Cell(90, 7, "", '0', 0, 'L');
            $pdf->Cell(85, 7, "Notas pedido", 'B', 1, 'L');

            $pdf->SetFont('Arial', '', '8');
            $pdf->Cell(90, 7, "", '0', 0, 'L');
            $pdf->Multicell(85, 4, utf8_decode($shipmentData['store_request_comments']), '0', 1, 'L');

            $pdf->SetY(260);
            $pdf->Cell(10, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Enviado por", 'T', 0, 'C');
            $pdf->Cell(20, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Recibido por", 'T', 00, 'C');
            $pdf->Cell(20, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Entregado por", 'T', 1, 'C');

            $pdf->Cell(10, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Nombre y firma", '0', 0, 'C');
            $pdf->Cell(20, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Nombre y firma", '0', 0, 'C');
            $pdf->Cell(20, 4, "", '', 0, 'C');
            $pdf->Cell(45, 4, "Nombre y firma", '0', 1, 'C');
        }


        if ($createFile) {
            if (!is_dir(PATH_TEMP_DOCS . "requisitions/")) {
                mkdir(PATH_TEMP_DOCS . "requisitions/", 0777, true);
            }
            $pdf->Output(PATH_TEMP_DOCS . "requisitions/REQ-" . $idShipment . ".pdf", "F");
            $this->pathfFileCreated = PATH_TEMP_DOCS . "requisitions/REQ-" . $idShipment . ".pdf";
        } else {
            $pdf->Output();
            unlink(ROOT . 'app/resources/docs/temp/barcodes/' . $shipment->getNumShipment() . '.png');
        }
    }

    public function getPathFileCreated()
    {
        return $this->pathfFileCreated;
    }
}
