<?php 
$controller = 'Purchase';
$action = '';
if(isset($_POST['action'])){
    $action = $_POST['action'];
    if (isset($_POST['id'])) {$id = $_POST['id'];}
}elseif(isset($_GET['action'])){
    $action = $_GET['action'];
    if (isset($_GET['id'])) {$id = $_GET['id'];}
}

include $_SERVER["DOCUMENT_ROOT"].'/app/include/bootstrap.php';

$_form = new PurchaseForm();
$_purchase = new PurchaseRepository();

switch($action){
    case 'insert':        
        $_form->setTokenForm($_POST['token_form']);
        $_form->populate($_POST);  
        $_POST['attachments'] = $_FILES;
        $_purchase->setOptions($_POST);
        
        if ($_form->isValid()) {             
            if ($_purchase->save($_purchase->getOptions())) {                
                $_purchaseId = $_purchase->getLastInsertId();
                $_purchaseNo = $_purchase->getPurchaseNumberById($_purchaseId);
                $printPurchase = "<a href=\"\" onclick=\"javascript: void window.open('/Controller/Purchase.php?action=export&format=pdf&flag=in&id=$_purchaseId','','width=700,height=500,status=1,scrollbars=1,resizable=1')\">".$_translator->_getTranslation('Imprimir')."</a>";  
                $flashmessenger->addMessage(array(
                    "success"=>$_translator->_getTranslation("Compra")." #$_purchaseNo ".$_translator->_getTranslation("se ha registrado exitosamente.")));
                
                header("Location: Purchase.php?action=edit&id=$_purchaseId");
            }else{                
                $vista = 'Purchase.php';
                include $root . '/View/Template.php';                
            }
        } else {
            $vista = 'Purchase.php';
            include $root . '/View/Template.php';
        }
        break;
        
    case 'list':          
        $searchFilter = null;
        if(isset($_POST['search'])){$searchFilter = $_POST;}
        $_listPurchases = $_purchase->getListPurchase($searchFilter);
        
        $vista = 'PurchaseList.php';
        include $root . '/View/Template.php';
    break;
        
    case 'export':
        switch($_GET['format']){            
            case 'pdf':
                switch($_GET['flag']){
                    case 'po':
                        $pdf = new PurchasePDF($id);
                        break;
                    }   
                break;
        }
         
        break;
    
    case 'edit': 
        if($_GET){
            $_purchaseData = $_purchase->getById($id);
            $_purchase->crearTablaDetallesForUser();
            $_purchase->setPurchaseDetailsById($id,$_form->getTokenForm());     /*No exist esta funcion */       
        }
        
        if($_POST){            
            $_form->setTokenForm($_POST['token_form']);
            $_POST['attachments'] = $_FILES;
            $_purchaseData = $_POST;
        }
        
        $_form->setActionController('edit');
        $_form->setId($id);
        $_form->populate($_purchaseData);        
        
        $_disabled = null;
        
        if($_purchaseData['status'] == '3'){
            $_disabled = true;
            $_form->disabledAllElements();
            $_form->hideElement(array('terminar'));               
        }
        
        $_purchase->setOptions($_purchaseData);

        if(isset($_POST['id']) && $_purchaseData['status'] != '3'){
            if($_form->isValid()){       
                $result = $_purchase->update($id,$_purchase->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Compra se actualizo exitosamente.'));
                    header("Location: Purchase.php?action=edit&id=$id");
                }else{
                    $vista = 'Purchase.php';
                    include $root . '/View/Template.php';                    
                }       
            }else{
                $vista = 'Purchase.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'Purchase.php';
            include $root . '/View/Template.php';
        }        
        break;
    
    case 'delete':
        if(!$_purchase->isUsedInRecord($id)){
            if($_purchase->delete($id)){
                $flashmessenger->addMessage(array('success'=>'Compra se cancelo exitosamente.'));
            }
        }
        
        header('Location: Purchase.php?action=list');
        break;
        
    case 'ajax':
        $ajaxPurchase = new PurchaseAjax();
        $json = $ajaxPurchase->getResponse($_POST['request'],$_POST);
        
        echo json_encode($json);
        break;       
    
    default:      
        $_purchase->crearTablaDetallesForUser();
        
        //$_form->populate(array('tipo'=>$_tipo));
        $vista = 'Purchase.php';
        include $root.'/View/Template.php';
        break;
}