<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_COMPLETE_STOCK_COUNT';
$path_to_root = "..";

include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_controls.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/complete_stock_count_ui.inc");
include_once($path_to_root . "/inventory/includes/db/complete_stock_count_db.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/errors.inc");
include_once($path_to_root . "/includes/types.inc");
include_once($path_to_root . "/inventory/includes/process_stock_count.inc");
include_once($path_to_root . "/jobq/includes/jobq_requests.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();


//------------
if (isset($_GET['ProcessStockCountNo']) && is_numeric($_GET['ProcessStockCountNo'])) {
    
    $_SESSION['page_title'] = sprintf( _("Process Stock Count # %d"), $_GET['ProcessStockCountNo']);
    create_cart(ST_STOCK_COUNT, $_GET['ProcessStockCountNo']);
}

if (isset($_GET['CompleteStockCountNo']) && is_numeric($_GET['CompleteStockCountNo'])) {

        $_SESSION['page_title'] = sprintf( _("Complete Stock Count # %d"), $_GET['CompleteStockCountNo']);
	create_cart(ST_STOCK_COUNT, $_GET['CompleteStockCountNo']);
        
    }
   

page($_SESSION['page_title'], false, false, "", $js);
//-----------------------------------------------------------------------------------------------

check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));

check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));

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

if (isset($_GET['CompletedID'])) 
{
	$trans_no = $_GET['CompletedID'];
	$trans_type = ST_STOCK_COUNT;

	display_notification_centered(_("Stock Count ".$trans_no." has been completed"));
        
	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Stock Count")), 0, 1);

	//display_note(print_document_link($trans_no, _("&Print This Stock Count"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/inventory/stock_count.php?NewStockCount=Yes", _("Enter &Another Stock Count"));
        
	display_footer_exit();
}

if (isset($_GET['ProcessedID'])) 
{
	$trans_no = $_GET['ProcessedID'];
	$trans_type = ST_STOCK_COUNT;

	display_notification_centered(_("Stock Count ".$trans_no." has been submitted to job queue for processing"));
        
	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Stock Count")), 0, 1);

	display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Stock count")), 0, 1);

	//display_note(print_document_link($trans_no, _("&Print This Stock Count"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/inventory/stock_count.php?NewStockCount=Yes", _("Enter &Another Stock Count"));
        
	display_footer_exit();
}

if (isset($_POST['Complete'])){
  
        $msg_csc = complete_stock_count ($_SESSION['stock_counts']);
        if($msg_csc['passed'] == true){
                $trans_no = $_SESSION['stock_counts']->order_no;
                unset($_SESSION['stock_counts']);
                meta_forward($_SERVER['PHP_SELF'], "CompletedID=$trans_no");
        }
        else{
                display_error($psc_msg['message']);
        }
}

if (isset($_POST['Process'])){
    
        $stock_count_no = $_SESSION['stock_counts']->order_no;
        $parameters[] = array('stock_count_no' => $stock_count_no, 'stock_count_processing_type' => 'PROCESS');
        $parameters_json = json_encode($parameters);
        
        $submitted_by = $_SESSION['wa_current_user']->user;

        $msg_cjr = create_jobq_request(ST_STOCK_COUNT, $submitted_by, $parameters_json);
        if($msg_cjr['passed'] == true){
                update_stock_count_joq_request_id($stock_count_no, (int)$msg_cjr['message']);
                $trans_no = $stock_count_no;
                unset($_SESSION['stock_counts']);
                meta_forward($_SERVER['PHP_SELF'], "ProcessedID=$trans_no");
        }
        else{
            display_error($msg_cjr['message']);
        }
 
}

function create_cart($trans_type, $stock_count_id)
{ 
	global $Refs;

	if (!$_SESSION['SysPrefs']->db_ok) // create_cart is called before page() where the check is done
		return;
        
        unset ($_SESSION['stock_counts']);
		$_SESSION['stock_counts'] = new items_cart(ST_STOCK_COUNT);
    
        if ($stock_count_id) {
            read_stock_count($stock_count_id, $_SESSION['stock_counts'], $trans_type);
		$_SESSION['stock_counts']->order_no = $stock_count_id;
	} else{
                $_SESSION['stock_counts']->order_no = $stock_count_id;
                $_SESSION['stock_counts']->trans_type = $trans_type;

        } 
	//copy_from_cart();
}


function read_stock_count($stock_count_id, &$order, $trans_type)
{   

      $stock_count_header = get_stock_count_header($stock_count_id);
      
      copy_to_cart($stock_count_header, $stock_count_id);

        $order->from_loc = $stock_count_header["loc_code"];     
        $order->status_id = $stock_count_header["status_id"];
        $order->tran_date = sql2date($stock_count_header["created_on"]);
        $order->reference = $stock_count_header["reference"];
        $order->project_description = $stock_count_header["project_description"];     
        $order->task_description = $stock_count_header["task_description"];
        $order->approval_path_description = $stock_count_header["approval_path_description"];
      
      $result = get_stock_count_details($stock_count_id);
      if (db_num_rows($result) > 0){
        $line_no=0;
            while ($stock_count_lines = db_fetch($result)){
                add_stock_order($order, $stock_count_lines["stock_id"],
                    $stock_count_lines["counted_qty"],$stock_count_header["location_name"],
                    $stock_count_lines["description"], $stock_count_lines["status_id"], $stock_count_header["created_on"], $stock_count_lines["id"], $stock_count_lines["standard_cost"]);
                $order->line_items[$line_no]->row_id = $stock_count_lines["id"] ;
             $line_no++;       
            } 
      }
  
}

function copy_to_cart($stock_count_header, $stock_count_id)
{
	$cart = &$_SESSION['stock_counts'];
        $cart->location = $stock_count_header["location_name"];
        $cart->status_id = $stock_count_header["status_id"];
        $cart->reference = $stock_count_header["reference"];
        $cart->created_on = $stock_count_header["created_on"];
        $cart->updated_on = $stock_count_header["updated_on"];
        $cart->tran_date = $stock_count_header["tran_date"];
        $cart->system_qty = $stock_count_header["system_qty"];
        $cart->trans_type = ST_STOCK_COUNT;
        $cart->trans_no = $stock_count_id;	
}


function copy_from_cart()
{
//
//
      $cart = &$_SESSION['stock_counts'];
        //$_POST['comment'] = $cart->comment;
        //$_POST['AdjDate'] = $cart->AdjDate ;
	//$_POST['plant_code'] = $cart->plant_code;
        //$_POST['job_code'] = $cart->job_code;
        //$price = $cart->price;
        //$_POST['qty'] = $cart->quantity;
        //$_POST['description'] = $cart->description;
       // $_POST['sales_type'] = $cart->sales_type;
//	$cart = &$_SESSION['Items'];
//	$_POST['ref'] = $cart->reference;
//	$_POST['Comments'] = $cart->Comments;
//         $_POST['comment'] = $cart->comment ;
//
//	
}

//-----------------------------------------------------------------------------------------------
start_form();

display_order_header($_SESSION['stock_counts']);



display_order_summary(_("Stock Items"), $_SESSION['stock_counts'], true);




    if(isset($_GET['ProcessStockCountNo']) && $_SESSION['stock_counts']->status_id != 35){
        submit_center_last('Process', _("Process Stock Count"), '', 'default');
    }elseif (isset($_GET['CompleteStockCountNo'])) {
        submit_center_first('Complete', _("Complete Stock Count"), '', 'default');
    }
    else {}
    

end_form();
end_page();

?>



