<?php

class WasteAjax extends WasteRepository {

    

    public $flashmessenger = null;

    

    public function __construct() {

        if(!$this->flashmessenger instanceof FlashMessenger){

            $this->flashmessenger = new FlashMessenger();

        }

    }

    

    public function getResponse($request, $options) {

        return $this->$request($options);

    }

    

    public function _getTranslation($text){

        $translator = new Translator();

        return $translator->_getTranslation($text);

    }

    

    public function getTranslation($options){

        $msj = $options['msj'];

        

        return array(

            'response'=>true,

            'translation'=>$this->_getTranslation($msj)

                );

    }    

    

    public function addDetail($options = null,$uuid = null){

        $tools = new Tools();          

        //UG add categoria pos 30.08.24

        $extenseCatRepo = new PosCategoriaRepository();

        $categoriaArray = $extenseCatRepo->getListaSelectCategorias();        

        $categoriaArray = array('' => 'Seleccionar una opccion...') + $categoriaArray;
 

        $expenseRepo = new PosProductoRepository();

        $productsArray = $expenseRepo->getListSelectProductosAndPrice();  

        $productsArray = array('' => 'Seleccionar una opccion...') + $productsArray;
      
        

        if(is_null($uuid)){

            /*Cuando Registro general es nuevo*/                 

            $onlyUuid = $tools->uniqidReal(6);

            $uuid = $onlyUuid.'x_details';            

        }else{

            /*Cuando Registro general se esta editando*/

            $onlyUuid = $uuid;

            $uuid = $uuid.'_details';         

        }

        

        $form = new Form();

        $fields = array(          

            'product_id'=>null,

            'quantity'=>null,

            'cost'=>null,

            'price'=>null,

            'total_cost'=>null,

            'total_price'=>null,

            'categoria'=>null

        );      

        

        if(!isset($options['action'])){            

            foreach($fields as $key => $value){

                $fields[$key] = $options[$key];

            }

        }        
      
        $form->addElement(array('type'=>'select','name'=>$uuid.'[categoria]','class'=>$uuid." {$onlyUuid} categoria required",'multiOptions'=>$categoriaArray,'value'=>"{$fields['categoria']}",'optionals'=>array('style'=>'width:100%','data-uuid'=>$onlyUuid,'readonly'=>'true')));         

        $form->addElement(array('type'=>'select','name'=>$uuid.'[product_id]','class'=>$uuid." {$onlyUuid} product_id required",'multiOptions'=>$productsArray,'value'=>"{$fields['product_id']}",'optionals'=>array('style'=>'width:100%','data-uuid'=>$onlyUuid,'readonly'=>'true')));         

        $form->addElement(array('type'=>'text','name'=>$uuid.'[quantity]','class'=>$uuid." {$onlyUuid} quantity mg_decimal required text-right",'value'=>"{$fields['quantity']}",'optionals'=>array('data-uuid'=>$onlyUuid)));            

        $form->addElement(array('type'=>'text','name'=>$uuid.'[cost]','class'=>$uuid." {$onlyUuid} cost mg_decimal required text-right",'value'=>"{$fields['cost']}",'optionals'=>array('data-uuid'=>$onlyUuid,'readonly'=>true)));            

        $form->addElement(array('type'=>'text','name'=>$uuid.'[total_cost]','class'=>$uuid." {$onlyUuid} total_cost mg_decimal required text-right",'value'=>"{$fields['total_cost']}",'optionals'=>array('data-uuid'=>$onlyUuid,'readonly'=>true)));            

        $form->addElement(array('type'=>'text','name'=>$uuid.'[price]','class'=>$uuid." {$onlyUuid} price mg_decimal required text-right",'value'=>"{$fields['price']}",'optionals'=>array('data-uuid'=>$onlyUuid,'readonly'=>true)));            

        $form->addElement(array('type'=>'text','name'=>$uuid.'[total_price]','class'=>$uuid." {$onlyUuid} total_price mg_decimal required text-right",'value'=>"{$fields['total_price']}",'optionals'=>array('data-uuid'=>$onlyUuid,'readonly'=>true)));            
      
      
       $expenseString = "<tr>"                              

                ."<td class='col-md-1 text-center'>"

                    . "<span class='btn btn-default _edit' data-uuid='$uuid'><i class='fa fa-pencil'></i></span> "

                    . "<span class='btn btn-danger _delete' data-uuid='$uuid'><i class='fa fa-trash'></i></span> "                    

                . "</td>"                    
                
                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[categoria]'))}</td>"   

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[product_id]'))}</td>"    

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[quantity]'))}</td>"      

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[cost]'))}</td>"    

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[price]'))}</td>"   

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[total_cost]'))}</td>"                      

                ."<td class='text-center'>{$form->createElementString($form->getElement($uuid.'[total_price]'))}</td>"   

                ."</tr>";

                        

        return array(

            'response'=>true,

            'detail_string'=>$expenseString,

            'uuid'=>$uuid

        );

    }

    

    public function getDataProduct($options){

        $productRepo = new PosProductoRepository();

        $data = $productRepo->getById($options['product_id']);


        return array(

            'response'=>true,

            'cost'=>$data['costo'],

            'price'=>$data['punitarioventa']

        );

    }

    //UG add function filter by category_id 
    public function getDataByCategory($options){

        $productRepo = new PosProductoRepository();

        $data = $productRepo->getListSelectProductosByCategory($options['category_id']);

        
        $list = "<option value=''>".$this->_getTranslation('Seleccionar una opcion...')."</option>";
        foreach($data as $key => $value){
         
            $list .= "<option value='$key' >$value</option>";
        }

        return array(

            'response'=>true,

            'producto'=>$list,

        );

    }

}