<?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']."Region.php");
$obj_region = new Region();

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

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

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

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

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

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

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

    $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_admin_region = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'show_admin_region');
    
    if($acsp_edit == 1){
        if($po_id != ""){
            $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);

            $data_region = $obj_region->get_data_select($obj_crud);
            $data_unit = $obj_unit->get_data_select($obj_crud);
            $data_ppn = $obj_ppn->get_ppn($obj_crud);
            $data_admin = $obj_admin->get_admin_pic($obj_crud);
            $data_admin_client = $obj_admin->get_admin_pic_client($obj_crud);
        } else {
            header("Location:".$path['dashboard']);
        }
    } else {
        header("Location:".$path['dashboard']);
    }
    

    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'] == "editItem"){
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $atpo_id = $obj_crud->escape_string(check_input($_POST['atpo_id']));
        $po_ppn = $obj_crud->escape_string(check_input($_POST['po_ppn']));
        $edesc = $obj_crud->escape_string(check_input($_POST['edesc']));
        $eproduct = $obj_crud->escape_string(check_input($_POST['eproduct']));
        $eqty = $obj_crud->escape_string(check_input($_POST['eqty']));
        $eunit = $obj_crud->escape_string(check_input($_POST['eunit']));
        $eunittxt = $obj_crud->escape_string(check_input($_POST['eunittxt']));
        $eprice = $obj_crud->escape_string(check_input($_POST['eprice']));

        $result = $obj_po->update_atpo($obj_crud,$atpo_id,$edesc,$eunit,$eunittxt,$eprice,$eproduct);
        if($result != 0){
            //recount total and price
            $items = $obj_po->get_atpo_item($obj_crud,$po_id);
            $total_item = count($items);
            $subtotal = 0;
            foreach($items as $item){
                $subtotal+= $item['atpo_price'];
            }
            $ppn_amount = ($po_ppn / 100) * $subtotal;
            $total = $subtotal + $ppn_amount;
            $result = $obj_po->update_price_po($obj_crud,$po_id,$subtotal,$ppn_amount,$total,$total_item);

            //save activity
            $act_type = "edit";
            $act_notes = "Editing item on the list";
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            $_SESSION['vmessage'][] = "Successfully edit item on the list.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed edit item on the list!"; 
            $_SESSION['valert'][] = "error";
        }

        header("Location:".$path['po-edit'].$po_id."/");

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

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $po_ppn = $obj_crud->escape_string(check_input($_POST['po_ppn']));
        $adesc = $obj_crud->escape_string(check_input($_POST['adesc']));
        $aproduct = $obj_crud->escape_string(check_input($_POST['aproduct']));
        $aqty = $obj_crud->escape_string(check_input($_POST['aqty']));
        $aunit = $obj_crud->escape_string(check_input($_POST['aunit']));
        $aunittxt = $obj_crud->escape_string(check_input($_POST['aunittxt']));
        $aprice = $obj_crud->escape_string(check_input($_POST['aprice']));

        $result = $obj_po->insert_at_po($obj_crud, $po_id, $adesc, $aqty, $aunit, $aunittxt, $aprice, $aproduct);
        if($result != 0){
            //recount total and price
            $items = $obj_po->get_atpo_item($obj_crud,$po_id);
            $total_item = count($items);
            $subtotal = 0;
            foreach($items as $item){
                $subtotal+= $item['atpo_price'];
            }
            $ppn_amount = ($po_ppn / 100) * $subtotal;
            $total = $subtotal + $ppn_amount;
            $result = $obj_po->update_price_po($obj_crud,$po_id,$subtotal,$ppn_amount,$total,$total_item);

            //save activity
            $act_type = "add";
            $act_notes = "Adding new item on the list";
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            $_SESSION['vmessage'][] = "Successfully add item on the list.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed add item on the list!"; 
            $_SESSION['valert'][] = "error";
        }

        header("Location:".$path['po-edit'].$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']));
        $atpoid = $obj_crud->escape_string(check_input($_GET['atpoid']));
        $po_ppn = $obj_crud->escape_string(check_input($_GET['poppn']));

        $result = $obj_po->delete_atpo($obj_crud, $atpoid);
        if($result != 0){
            //recount total and price
            $items = $obj_po->get_atpo_item($obj_crud,$po_id);
            $total_item = count($items);
            $subtotal = 0;
            foreach($items as $item){
                $subtotal+= $item['atpo_price'];
            }
            $ppn_amount = ($po_ppn / 100) * $subtotal;
            $total = $subtotal + $ppn_amount;
            $result = $obj_po->update_price_po($obj_crud,$po_id,$subtotal,$ppn_amount,$total,$total_item);

            //save activity
            $act_type = "delete";
            $act_notes = "Delete item on the list";
            $save_activity = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);

            $_SESSION['vmessage'][] = "Successfully delete item on the list.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed add delete on the list!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-edit'].$po_id."/");
    } else if($_GET['action'] == "update"){
        date_default_timezone_set("Asia/Jakarta");
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $po_id = $obj_crud->escape_string(check_input($_POST['poid']));
        $ponumber = $obj_crud->escape_string(check_input($_POST['ponumber']));
        $ponumberold = $obj_crud->escape_string(check_input($_POST['ponumberold']));
        $region = $obj_crud->escape_string(check_input($_POST['region']));
        $pic = $obj_crud->escape_string(check_input($_POST['pic']));
        $document_date = $obj_crud->escape_string(check_input($_POST['document_date']));
        $date_format = date("Y-m-d", strtotime($document_date));
        $total_item = $obj_crud->escape_string(check_input($_POST['total_item']));
        $cregion = "0";
        $csub = $obj_crud->escape_string(check_input($_POST['csub']));
        $picc = $obj_crud->escape_string(check_input($_POST['picc']));

        //client
        $clientid = $obj_crud->escape_string(check_input($_POST['clientid']));
        if($clientid == ""){
            $data_client = $obj_client->check_client_email($obj_crud,$cemail);
            if(is_array($data_client)){
                $clientid = $data_client[0]['client_id'];
                $cname = $data_client[0]['client_name'];
                $cemail = $data_client[0]['client_email'];
                $cphone = $data_client[0]['client_phone'];
                $caddress = $data_client[0]['client_address'];
            } else {
                $cname = $obj_crud->escape_string(check_input($_POST['cname']));
                $cemail = $obj_crud->escape_string(check_input($_POST['cemail']));
                $cphone = $obj_crud->escape_string(check_input($_POST['cphone']));
                $caddress = $obj_crud->escape_string(check_input($_POST['caddress']));
                $create_client = $obj_client->create_client($obj_crud,$cname,$cemail,$cphone,$cregion,$caddress);
                $clientid = $create_client;
            }
        } else {
            $cname = $obj_crud->escape_string(check_input($_POST['cname']));
            $cemail = $obj_crud->escape_string(check_input($_POST['cemail']));
            $cphone = $obj_crud->escape_string(check_input($_POST['cphone']));
            $caddress = $obj_crud->escape_string(check_input($_POST['caddress']));
        }
        if($ponumber == $ponumberold){
            $result = $obj_po->update_po($obj_crud,$po_id,$ponumber,$region,$pic,$date_format,$clientid,$cname,$cemail,$cphone,$caddress,$total_item,$csub,$picc);
            if($result != 0){
                //save activity
                $act_type = "edit";
                $act_notes = "Editing detail info purchase order";
                $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 detail.";
                $_SESSION['valert'][] = "success";
            } else {
                $_SESSION['vmessage'][]= "Failed update purchase order detail!"; 
                $_SESSION['valert'][] = "error";
            }
        } else {
            $check_po_num = $obj_po->check_po_number($obj_crud,$ponumber);
            if($check_po_num == 0){
                $result = $obj_po->update_po($obj_crud,$po_id,$ponumber,$region,$pic,$date_format,$clientid,$cname,$cemail,$cphone,$caddress,$total_item,$csub,$picc);
                if($result != 0){
                    //save activity
                    $act_type = "edit";
                    $act_notes = "Editing detail info purchase order";
                    $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 detail.";
                    $_SESSION['valert'][] = "success";
                } else {
                    $_SESSION['vmessage'][]= "Failed update purchase order detail!"; 
                    $_SESSION['valert'][] = "error";
                }
            } else {
                $_SESSION['vmessage'][]= "Failed to edit po! purchase order number ".$ponumber." already register!"; 
                $_SESSION['valert'][] = "error";
            }
        }
        header("Location:".$path['po-edit'].$po_id."/");
    }
}
?>