<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_PROJECT_SALES_ORDER';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/db/project_sales_orders_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc"); 
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc"); 
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/taxes/db/tax_groups_db.inc");

//include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc"); 

        
$js = '';
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}
page(_($help_context = "Project (Customer) Purchase Order"));

//------------------------------------------------------------------------------------------------------
simple_page_mode(true);

//$pso_no = get_session_variable('project_invoice_no'); 
//if(!($pso_no > 0)){
//    set_session_variable('project_invoice_no', -1);
////    $pso_no = -1;
//}

$pso_no = -1;
 
if (isset($_GET['PSONo']))
{
    $_POST['PSONo'] = $_GET['PSONo'];
    set_session_variable("ST_SALES_ORDERS_so_no", $_POST['PSONo']);
}

if (isset($_GET['NewOrder']))
{
    $_POST['PSONo'] = -1;
    set_session_variable("ST_SALES_ORDERS_so_no", $_POST['PSONo']);
}        
        
if (isset($_POST['PSONo']) && $_POST['PSONo'] > 0){
    $pso_no = $_POST['PSONo'];
}
else{
    $pso_no = get_session_variable("ST_SALES_ORDERS_so_no");
    
}

if (isset($_GET['Released_ID'])) 
{
    //return;
        
    $trans_no = $_GET['Released_ID'];
	$trans_type = ST_PROJECT_INVOICE;
    $message = get_session_variable('project_invoice_messages');
    clear_session_variable('project_invoice_messages');
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this Project Invoice"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/projects/project_invoice.php", _("Create another &Project Invoice"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}



function can_process_pso_header($pso_no)
{    

//    if($pso_no > 0){
//        $psoh = get_project_sales_order_header($pso_no);
//        if(!($psoh['approval_status'] == AS_NEW || $psoh['approval_status'] == AS_WITH_THE_APPROVER || 
//                $psoh['approval_status'] == AS_AWAITING_APPROVAL || $psoh['approval_status'] == AS_BACK_TO_ORIGINATOR)){
//            $message = ("This document status does not allow further updating.");
//            return array("success" => false, "focus_to" => '', "message" => $message);
//        }
//        
//        $current_user_id = $_SESSION['wa_current_user']->user;
//        $ced_msg = can_edit_document(ST_UTILITY_BILL_CREDIT_NOTE, $psoh['project_id'], $psoh['next_approval_group_id'] , $current_user_id, $psoh['created_by'], $pso_no);
//        if($ced_msg['success'] !== true){
//            return array("success" => false, "focus_to" => '', "message" => $ced_msg['message']);
//        }
//        
//    }
    
    if (!($_POST['project_id'] > 0))
    {
        $message = "Please select a Project.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if (!($_POST['debtor_no'] > 0))
    {
        $message = "Please select a Project.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if(!($_POST['branch_code'] > 0))
    {
        $message = "Please select a customer branch.";
        $set_focus = 'branch_code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }      

    if(!is_date($_POST['ord_date']))
    {
        $message = "Date entered is not a valid date.";
        $set_focus = 'ord_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }
      
    if (!($_POST['tax_group_id'] > 0))
    {
        $message = "Please select a Tax group.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    return array("success" => true, "message" => "");    
}

function save_pso_header(&$pso_no)
{
    $validate = can_process_pso_header($pso_no);   
    
    if($validate["success"] !== true) 
    {          
        if(isset($validate["focus_to"])){
            set_focus($validate["focus_to"]);
        }
        return array("success" => false, "message" => $validate["message"]);
    }
    
    $due_date = get_invoice_duedate($_POST['payment_terms'], $_POST['ord_date']);

    $pi_taxes = array();
    $taxes_total = 0;
    $tax_group_id = get_post('tax_group_id');
    $tax_types_array = get_tax_group_items_as_array($tax_group_id);
    foreach ($tax_types_array as $tax_type){
        if(isset($_POST[$tax_type['tax_type_id'].'_mpi_tax']) && input_num($tax_type['tax_type_id'].'_mpi_tax') > 0){
            $tax_amount = input_num($tax_type['tax_type_id'].'_mpi_tax');
            $a = array('tax_type_id' => $tax_type['tax_type_id'], 'tax_amount' => $tax_amount);
            $pi_taxes[] = $a;
            $taxes_total += $tax_amount;
        }
    }
    
    $pso_no = write_project_sales_order_header($pso_no, $_POST['project_id'], $_POST['debtor_no'],
                                        $_POST['branch_code'], $_POST['reference'], $_POST['payment_terms'],
                                        $_POST['ord_date'], $_POST['salesman_code'], 
                                        input_num('total'), $taxes_total, $_POST['rate_include_taxes'],                                 
                                        get_post('po_reference'), $_POST['site_id'],
                                        $due_date, '', get_post('tax_group_id'), $pi_taxes);   
    
    if($pso_no > 0){
        set_session_variable("ST_SALES_ORDERS_so_no", $pso_no);
        return array("success" => true, 'trans_no' => $pso_no, "message" => "Project Invoice $pso_no has been saved.");
    }
    else{
        return array("success" => false, 'trans_no' => '', "message" => "Could not save Project Invoice.");
    }
}


function save_pso_detail(&$pso_no, $selected_id)        
{
    $item_id = get_post('item_id');
    $order_unit_qty = input_num('order_unit_qty');
    $order_unit = $_POST['order_unit'];
    $conversion_factor = 1;
    
    switch ($_POST['type']) {
        case PSOIT_STOCK_ITEM:
            get_convertion_factor2(get_post('item_id'), $order_unit, $conversion_factor);
            break;
        
        case PSOIT_BOQ_ITEM:

            break;

        case PSOIT_TEXT:
        default:

            break;
    }
    
    $quantity = $order_unit_qty * $conversion_factor;
            
    write_project_sales_order_line($pso_no, $selected_id, $_POST['type'], get_post('description'), 
                                    $item_id, $order_unit, $order_unit_qty, $conversion_factor, 
                                    input_num('order_unit_rate'), $quantity, get_post('line_comment'), get_post('tax_group_id'), 
                                    get_post('rate_include_taxes'));
}

function save_pso(&$pso_no, $selected_id)
{
    $msg = save_pso_header($pso_no);
    if($msg["success"] !== true){
        return $msg;
    }

    save_pso_detail($pso_no, $selected_id);

    return array("success" => true, 'trans_no' => $pso_no, "message" => $msg["message"]);

}

function display_pso_header($pso_no)
{
    if($pso_no>0){
        $pso_header = get_project_sales_order_header($pso_no);
        $_POST['project_id'] = $pso_header['project_id'];
        $_POST['debtor_no'] = $pso_header['debtor_no'];
        
        if(!isset($_POST['customer_branch'])){
            $_POST['customer_branch'] = $pso_header['branch_code'];
        }
        if(!isset($_POST['reference'])){
            $_POST['reference'] = $pso_header['reference'];
        }
        if(!isset($_POST['payment_terms'])){
            $_POST['payment_terms'] = $pso_header['payment_terms'];
        }
        if(!isset($_POST['ord_date'])){
            $_POST['ord_date'] = sql2date($pso_header['ord_date']);
        }
        if(!isset($_POST['salesman_code'])){
            $_POST['salesman_code'] = $pso_header['salesman_code'];
        }
        if(!isset($_POST['total'])){
            $_POST['total'] = $pso_header['total'];
        }
        if(!isset($_POST['taxes'])){
            $_POST['taxes'] = $pso_header['taxes'];
        }
        if(!isset($_POST['project_description'])){
            $_POST['project_description'] = $pso_header['project_description'];
        }
        if(!isset($_POST['tax_group_name'])){
            $_POST['tax_group_name'] = $pso_header['tax_group_name'];
        }
        if(!isset($_POST['payment_certificate_amount'])){
            $_POST['payment_certificate_amount'] = $pso_header['payment_certificate_amount'];
        }
        if(!isset($_POST['tax_group_id'])){
            $_POST['tax_group_id'] = $pso_header['tax_group_id'];
        }
        if(!isset($_POST['po_reference'])){
            $_POST['po_reference'] = $pso_header['po_reference'];
        }
        if(!isset($_POST['rate_include_taxes'])){
            $_POST['rate_include_taxes'] = $pso_header['rate_include_taxes'];
        }
        
        if(!isset($_POST['site_id'])){
            $_POST['site_id'] = $pso_header['site_id'];
        }
    }
    else{
        if(!isset($_POST['rate_include_taxes'])){
            $_POST['rate_include_taxes'] = 0;
        }
    }
        
    start_outer_table(TABLESTYLE2, "width=50%"); // outer table
    start_table(TABLESTYLE2);  
        table_section(1, "50%");
            text_row("Reference:", 'reference', null, null, null);   
        
            if($pso_no>0){
                label_row("Project:", $_POST['project_description']);   
                hidden('project_id',$_POST['project_id']);
            }
            else{
                project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true); 
            }
            
            $_POST['customer_name'] = $_POST['customer_id'] = $rate_include_taxes = '';
            $project = get_project($_POST['project_id']);
            if (isset($project['project_id']) && $project['project_id'] > 0) {
                $_POST['customer_name'] = $project['customer_name']; 
                $_POST['customer_id'] = $project['customer_id']; 
                $rate_include_taxes = get_yes_no_name($project['rate_include_taxes']);
            }

            label_row("Customer:", $_POST['customer_name']);    
            hidden('debtor_no', $_POST['customer_id']);

            customer_branches_list_row("Branch:", $_POST['customer_id'], 'branch_code', null, false, false, false);        
            
            text_row("PO Reference:", 'po_reference', null, null, null);  
            
            date_row("Order Date:", 'ord_date');

        table_section(2, "50%");           
            
            start_row();
            sale_payment_list_cells("Payment Terms:", 'payment_terms', null, null);  
            end_row();     
            sales_persons_list_row("Sales Person:", 'salesman_code', null);
            
            
            label_row("Rate Include Taxes:", $rate_include_taxes);    
            hidden('rate_include_taxes',$_POST['rate_include_taxes']);
            
            if($pso_no>0){
                label_row("Tax Group:",  $_POST['tax_group_name']);
                hidden('tax_group_id',$_POST['tax_group_id']);
            }
            else{
                tax_groups_list_row("Tax Group:", 'tax_group_id');                
            }

            project_sites_list_row("Job:", "site_id", $_POST['project_id'], null, "Select a job" );
            
//    end_table(1);

    end_outer_table(1); // outer table
//    hidden('PSONo', $pso_no);  

}

function display_selected_tax_types($pso_no, $tax_group_id)
{
    $tax_types_array = get_tax_group_items_as_array($tax_group_id);
    foreach ($tax_types_array as $tax_type) {
        if($tax_type['rate'] > 0){
            $tax_amount = get_manual_project_invoice_tax_type_amount($pso_no, $tax_type['tax_type_id']);
            $_POST[$tax_type['tax_type_id'].'_mpi_tax'] = $tax_amount;
            amount_row($tax_type['tax_type_name'], $tax_type['tax_type_id'].'_mpi_tax');   
        }
    }
}

function display_pso_details($pso_no)
{  
    global $project_invoice_line_types;    
          
    $sub_total = 0;
    
    $result = get_project_sales_order_details($pso_no);      
                              
    start_table(TABLESTYLE, "width=80%");
    $th = array(_('Type'),_('Details'),
                _('Unit'), _('Rate'),
                _("Qty"), "Total", "", "");              

    table_header($th);
    $k = 0; //row colour counter
   // $total = 0;
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cell($myrow["id"], "align=right");

        if($myrow["type"] == 0){
            $type_name = "Comment";
        }
        elseif($myrow["type"] == 1){
            $type_name = "BOQ Item";
        }else{
            $type_name = "Item";
        }
             
        label_cell($type_name);  
        
        switch ($myrow["type"]){
            case 1:
                $boq_item = get_project_boq_item_details($myrow["item_id"]);
                $item_details = $boq_item['code']." - ".$boq_item['description'];
                break;
            case 2:
                $stock_item = get_stock_item_edit_info($myrow["item_id"]);
                $item_details = $stock_item['stock_id']." - ".$stock_item['description'];
                break;
            default:
                $item_details = $myrow["description"];
                break;
        }
        label_cell($item_details);
 
        label_cell($myrow["unit_name"]);           
        amount_decimal_cell($myrow["order_unit_rate"]);  
        label_cell($myrow["order_unit_qty"], "align=right"); 
        
        $line_total = $myrow["order_unit_rate"] * $myrow["order_unit_qty"];
        amount_decimal_cell($line_total);  
        $sub_total += $line_total;
        
        if(strlen($myrow["line_comment"]) > 0){
            label_cell(""); label_cell("");
            end_row();
            $t= $k+1;
            alt_table_row_color($t);
            label_cell("");
//            $s = textarea_cells(null, '$name', $myrow["line_comment"], 100, 3, null, "colspan=4");
            label_cell($myrow["line_comment"], "colspan=5");
            
        }

        edit_button_cell("Edit".$myrow["id"], _("Edit"));    
        delete_button_cell("Delete".$myrow["id"], _("Delete"));  

        end_row();        
      
        }

        start_row();
        label_cell("Sub-total", "colspan=5 align=right");
        amount_decimal_cell($sub_total);
        label_cell("");
        label_cell("");
        end_row();
       
        $tax_included = $_POST['rate_include_taxes'];
        $taxes = get_project_sales_order_taxes($pso_no, 0);
        display_edit_tax_items($taxes, 5, $tax_included, 2, false);

        if($tax_included){
            start_row();
            label_cell("Amount Total", "colspan=5 align=right");
            amount_decimal_cell($sub_total);
            label_cell("");
            label_cell("");
            end_row();
        }
        else{
            foreach ($taxes as $tax) {
                $sub_total += $tax['Value'];
            }
            start_row();
            label_cell("Amount Total", "colspan=5 align=right");
            amount_decimal_cell($sub_total);
            label_cell("");
            label_cell("");
            end_row();
        }
}

function display_pso_edit_controls($selected_id, &$Mode)
{
    global $Ajax;
    
    $Ajax->activate("billing_items");
    
    if ($Mode =='Edit')   
    {
        $myrow = get_project_sales_order_line($selected_id); 

        $_POST['type'] = $myrow["type"];
        $_POST['item_id'] = $myrow["item_id"];    
        $_POST['description'] = $myrow["description"];    
        $_POST['order_unit'] = $myrow["order_unit"];            
        $_POST['line_comment'] = $myrow["line_comment"];  
        $_POST['order_unit_qty'] = $myrow["order_unit_qty"];           
        $_POST['order_unit_rate'] = $myrow["order_unit_rate"];   
    }
    else{
   
    }

    hidden('selected_id', $selected_id );
    br();

    div_start('billing_items');
    start_table(TABLESTYLE2);
 
//    three_value_list_row("Type :", "type", null, "Comment", 0, "BOQ Item", 1, "Item", 2, true);
    project_sales_order_item_types_list_row("Type :", "type", null, false, true);
            
    switch ($_POST['type']) {
        case PSOIT_STOCK_ITEM:
            $Ajax->activate('wo_material_stock_id');
            start_row();
            label_cell(_("Stock Item :"), "class='label'");

            echo "<td>";
                echo stock_items_list('item_id', null, false, true);
            echo "</td>";
            end_row();
            units_list_row("Unit :", "order_unit", get_post('item_id'));
            amount_row("Rate :", 'order_unit_rate');
            
            hidden("description", '');
            break;
        case PSOIT_BOQ_ITEM:
            project_boq_item_groups_list_row("BOQ Group :", "boq_group_id", get_post('project_id'), null, "Select a BOQ Item Group", true);
            group_boq_items_list_row("BOQ Item :", "item_id", get_post('project_id'), $_POST['boq_group_id'], null, false, true);

            $rate = $unit = $unit_name = null;
            $boq_item = get_project_boq_item_details($_POST['item_id']);
            if (isset($boq_item['id']) && $boq_item['id'] > 0) {
                $rate = $boq_item['rate'];
                $unit = $boq_item['unit'];
                $unit_name = $boq_item['unit_name'];
            }
            label_row("Unit :", $unit_name);
            label_row("Rate :", $rate);

            hidden("order_unit", $unit);
            hidden("order_unit_rate", $rate);
            hidden("description", '');

            break;

        case PSOIT_TEXT:
        default:
            text_row("Description", "description", null, 50, 500);
            stock_units_list_row(_("Units:"), 'order_unit', null);
            amount_row("Rate :", 'order_unit_rate');
            break;
    }
         
    amount_row("Quantity :", 'order_unit_qty');
    textarea_row("Details:", 'line_comment', null, 50, 4);
      

    end_table(1);
    div_end();

}


function release_for_approval($pso_no)
{
//    $msg = can_process_pi_header($pso_no);
    $msg = save_manual_project_invoice_header($pso_no);
    if($msg['success'] !== true){
        return $msg;
    }
    

    
    begin_transaction();
        
//    update_project_invoice_header($pso_no, $_POST['project_id'], $_POST['customer_id'], $_POST['customer_branch'], 
//            $_POST['payment_terms'], $_POST['order_date'], $due_date, $_POST['sales_person']);

//    $pih = get_project_invoice_header($pso_no);
    $pih = get_manual_project_invoice_header($pso_no);
    $items_added_tax = 0;

    if($pih['rate_include_taxes']){
        $pi_total = $pih['total'];
    }
    else{
        $pi_total = $pih['total'] + $pih['taxes'];
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;


//    $row_state = db_new_row_state($pih['row_state']);
    
    $info = approve_document(ST_PROJECT_INVOICE, $pso_no, $pih['project_id'], 
                        $pi_total, $pih['next_approval_group_id'], 
                        $current_user_id, $pih['row_state'], $pih['created_by']);

    if($info['success'] !== true){
        cancel_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }

    if(exists_customer_trans(ST_PROJECT_INVOICE, $pso_no)){
        cancel_transaction();
        return array("success" => false, "message" => "Project Invoice $pso_no is already in the customer's transactions list.");
    }
    
    if($info['approval_status'] == AS_APPROVED){
        add_customer_transaction(ST_PROJECT_INVOICE, $pso_no, $pih['debtor_no'], $pih['branch_code'], 
                                        sql2date($pih['ord_date']), $pih['reference'], $pih['total'], 0,
                                        $pih['taxes'], 0, 0, 0, 
                                        0, 0, sql2date($pih['due_date']), 0, 
                                        0, 0, 0, $pih['payment_terms'], 
                                        $pih['project_id']); 
    }
    
    commit_transaction();

    
    return array("success" => true, "message" => $info['message']);
            
        
}

function send_pi_back_to_originator($pso_no)
{
   $msg = can_process_pso_header($pso_no);
    if($msg['success'] !== true){
        return $msg;
    }
    
    begin_transaction();
    $pih = get_manual_project_invoice_header($pso_no);

    if($pih['rate_include_taxes']){
        $pi_total = $pih['total'];
    }
    else{
        $pi_total = $pih['total'] + $pih['taxes'];
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;

    $sbdo_msg = send_back_to_document_originator(ST_PROJECT_INVOICE, $pso_no, $pih['project_id'],
                                    $pih['next_approval_group_id'], $current_user_id, $pih['row_state'], 
                                    $pih['created_by']);
    
    if($sbdo_msg['success'] !== true){
        cancel_transaction();
        return $sbdo_msg;
    }
    else{
        commit_transaction();
        return $sbdo_msg;
    }
}

function reject_pi($pso_no)
{
    $msg = can_process_pso_header($pso_no);
    if($msg['success'] !== true){
        return $msg;
    }

    begin_transaction();
    $pih = get_manual_project_invoice_header($pso_no);

    if($pih['rate_include_taxes']){
        $pi_total = $pih['total'];
    }
    else{
        $pi_total = $pih['total'] + $pih['taxes'];
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $rj_msg = reject_document(ST_PROJECT_INVOICE, $pso_no, $pih['project_id'],
                    $pih['next_approval_group_id'], $current_user_id, $pih['row_state']);
    
    if($rj_msg['success'] !== true){
        cancel_transaction();
        return $rj_msg;
    }
    else{
        commit_transaction();
        return $rj_msg;
    }

}


//-------------------------------------------------------------------------------------------------------------


if (isset($_POST['Release'])){

    $release_msg = release_for_approval($pso_no);
    if($release_msg['success'] !== true){
        display_error($sbdo_msg['message']);
    }
    else{
        new_doc_date($_POST['order_date']);
        set_session_variable('project_invoice_messages', $release_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Released_ID=$pso_no");
    }
        
} 

if (isset($_POST['Back_To_Originator'])){

    $sent_back_msg = send_pi_back_to_originator($pso_no);
    if($sent_back_msg['success'] !== true){
        display_error($sent_back_msg['message']);
    }
    else{
        new_doc_date($_POST['order_date']);
        set_session_variable('project_invoice_messages', $sent_back_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Sent_Back_ID=$pso_no");
    }
        
} 


if (isset($_POST['Reject_PI'])){

    $reject_msg = reject_pi($pso_no);
        if($reject_msg['success'] !== true){
        display_error($reject_msg['message']);
    }
    else{
        new_doc_date($_POST['order_date']);
        set_session_variable('project_invoice_messages', $reject_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Rejected_ID=$pso_no");
    }
        
} 


if(isset($_POST['Save_Project_Sales_Order_Header'])){
        
    $msg = save_pso_header($pso_no);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['PSONo'] = $msg['trans_no'];
        display_notification($msg['message']);
        $Mode = 'RESET';
    }

}


if(isset($_POST['Add_Project_CPO']) || isset($_POST['Update_Project_CPO'])){
        
    $msg = save_pso($pso_no,$selected_id);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $pso_no = $msg['trans_no'];
        hidden('PSONo', $msg['trans_no']);
        display_notification($msg['message']);
        $Mode = 'RESET';
    }

}


if (isset($_POST['cancel'])){
    $Mode = 'RESET';
    $selected_id = -1;
}  
//--------------------------------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
        $recs = delete_project_sales_order_item($selected_id);
        
        if($recs == 1){
                display_notification(_("Project Sales Order item has been deleted."));
        }
        else{
                display_error(_("Project Sales Order item could not be deleted."));
        }
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('billing_items');
//--------------------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
start_form();

$pi_header = display_pso_header($pso_no);   

//br();

if($pso_no > 0){
    submit_center_first('Save_Project_Sales_Order_Header', _("Save"), '', 'default');
}
//if($pso_no>0 && !($selected_id > 0)){
//    br(2); 
//    submit_center_first('Release', "Release for Approval", '', 'default');
//    submit('Back_To_Originator', 'Back To Originator', true, '', 'default');
//    submit_center_last('Reject_PI', "Reject", '', 'default');	
//}

br(2);

//------------------------------------------------------------------------------------------------------------
         
display_pso_details($pso_no);

//hidden('PSONo',$pso_no);
//inactive_control_row($th);
end_table(1);
//-----------------------------------------------------------------------------------------------------------

display_pso_edit_controls($selected_id, $Mode);

if($selected_id > 0){
    submit_center_first('Update_Project_CPO', _("Update"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}
else{        
    submit_center_first('Add_Project_CPO', _("ADD"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}

br();


if (!@$_GET['popup'])
{
    end_form();
    end_page(@$_GET['popup'], false, false);        
}


?>
