<?php
require_once($global['root-model']."Crud.php");
$obj_crud = new Crud();

require_once($global['root-model']."PO.php");
$obj_po = new PO();

require_once($global['root-model']."Activity.php");
$obj_activity = new Activity();

require_once($global['root-model']."Access.php");
$obj_access = new Access();

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

    $po_id = isset($_GET['po']) ? $_GET['po'] : "";

    $data = $obj_po->get_detail_po($obj_crud,$po_id);
    $items = $obj_po->get_atpo_item($obj_crud,$po_id);
    $activitys = $obj_activity->get_data($obj_crud, $po_id);

    $acsp_edit = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'edit_po');
    $acsp_print = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'print_po');
    $acsp_viewact = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'view_activity');
    $acsp_createact = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'create_activity');
    $acsp_document = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'upload_document');
    $acsp_prepared = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_prepare');
    $acsp_progress = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_progress');
    $acsp_complete = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_complete');
    $acsp_cancel = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_cancel');
    $acsp_paid = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_paid');
    $acsp_invoice = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'update_invoice');
    $acsp_delete = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'delete_po');
    $acsp_hide_amount = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'hide_amount');
    $acsp_create_notes = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'create_notes');

    if(isset($_SESSION['vmessage'])){
        foreach($_SESSION['vmessage'] as $svm){
            $vmessage[] = $svm;
        }
        unset($_SESSION['vmessage']);
    } else {
        $vmessage = "";
    }

    if(isset($_SESSION['valert'])){
        foreach($_SESSION['valert'] as $vlr){
            $valert[] = $vlr;
        }
        unset($_SESSION['valert']);
    } else {
        $valert = "";
    }
} else {
    if($_GET['action'] == "notes"){

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $act_notes = $obj_crud->escape_string(check_input($_POST['note']));
        $act_type = "notes";
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $result = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
        if($result > 0){
            $_SESSION['vmessage'][] = "Successfully create notes in PO ".$po_id.".";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed to create new notes!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-detail'].$po_id."/");

    } else if($_GET['action'] == "changeStat"){
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $po_id = $obj_crud->escape_string(check_input($_GET['poid']));
        $stat = $obj_crud->escape_string(check_input($_GET['stat']));

        $result = $obj_po->change_stat($obj_crud, $po_id, $stat);
        if($result != 0){

            //save activity
            $act_type = $stat;
            $act_notes = "Change status to ".$stat;
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            $_SESSION['vmessage'][] = "Successfully change status PO ".$po_id." to ".$stat.".";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed change status!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-detail'].$po_id."/");
    } else if($_GET['action'] == "changePaid"){
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $po_id = $obj_crud->escape_string(check_input($_GET['poid']));
        $stat = $obj_crud->escape_string(check_input($_GET['stat']));

        $result = $obj_po->change_paid($obj_crud, $po_id, $stat);
        if($result != 0){

            //save activity
            if($stat == 'yes'){
                $act_type = "paid";
                $act_notes = "Set purchase order already paid";
            } else {
                $act_type = "notpaid";
                $act_notes = "Set purchase order to not paid";
            }

            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            if($stat == 'yes'){
                $_SESSION['vmessage'][] = "Successfully set purchase order to Paid.";
            } else {
                $_SESSION['vmessage'][] = "Successfully set purchase order to Not Paid.";
            }
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed change paid status!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-detail'].$po_id."/");
    } else if($_GET['action'] == "invoice"){

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $invoice = $obj_crud->escape_string(check_input($_POST['invoice']));

        $act_type = "invoice";
        $act_notes = "Update Invoice No";
        
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $result = $obj_po->update_invoice($obj_crud, $po_id, $invoice);
        if($result > 0){
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
            $_SESSION['vmessage'][] = "Successfully update invoice no.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed to update invoice no!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-detail'].$po_id."/");

    } else if($_GET['action'] == "delete"){
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $po_id = $obj_crud->escape_string(check_input($_GET['poid']));

        $result = $obj_po->delete_po($obj_crud, $po_id);
        if($result != 0){

            //save activity
            $act_type = "delete";
            $act_notes = $N_user_name." Delete PO ".$po_id;
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            $_SESSION['vmessage'][] = "Successfully Delete PO ".$po_id.".";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed to Delete PO ".$po_id."!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po']."");
    } else if($_GET['action'] == "ponotes"){

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $ponote = $obj_crud->escape_string(check_input($_POST['ponote']));

        $act_type = "notes";
        $act_notes = "Update Purchase Order Notes";
        
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $result = $obj_po->update_ponotes($obj_crud, $po_id, $ponote);
        if($result > 0){
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
            $_SESSION['vmessage'][] = "Successfully update purchase order notes.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed to update purchase order notes!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-detail'].$po_id."/");

    }
}
?>