<?php
/**********************************************************************
***********************************************************************/
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/ui/items_cart.inc");

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

function display_purchase_requisition_header(&$order, $po_no)
{
    global $Refs, $approval_statuses, $Ajax;
    
    $project_id = $order->project_id;
    $user_id = $_SESSION['wa_current_user']->user;
    $cap = can_access_project($project_id, $user_id);
    if($cap['success'] !== true) 
    {
        display_error($cap["message"]);
        set_focus('project_id');
        display_footer_exit();
        exit;
    }

    if(!(isset($po_no) && $po_no > 0))// No PO
    {
        if(!$_SESSION["wa_current_user"]->can_access('SA_PR_ISSUES_WITHOUT_POS')){ // No access to issue without a PO
            $msg = "You are not allowed to issue items without a purchase order.";
            display_error($msg);
            set_focus('project_id');
            display_footer_exit();
            exit;
        }
    }
    
    $Ajax->activate('pr_header');
    div_start('pr_header');
    
    start_outer_table(TABLESTYLE2, "width=65%");

    table_section(1, "30%");
    label_row(_("Requesting Location"), $order->location_name, "class='tableheader2'");
    label_row(_("Project"), $order->project_description, "class='tableheader2'");
    //label_row(_("Task"), $order->task_description, "class='tableheader2'");
    label_row(_("Approval Path"), $order->approval_path_description, "class='tableheader2'");
    


    table_section(2, "30%");
    //label_row(_("Requesting Location:"), $order->receiving_location, "class='tableheader2'");
    $pr_link = get_trans_view_str(ST_PURCH_REQ, $order->order_id);
    label_row(_("Requisition No"), $pr_link, "class='tableheader2'");
    label_row(_("Requisition Reference"), $order->reference, "class='tableheader2'");
    label_row(_("Required Date"), $order->required_date, "class='tableheader2'");
    
    table_section(3, "30%");
//    if($order->use_future_budget == 0)
//    {
//        label_row(_("Use Future Budget"), "No", "class='tableheader2'");
//    }
//    else 
//    {
//        label_row(_("Use Future Budget"), "Yes", "class='tableheader2'");
//    }
    label_row(_("Requestor"), $order->requestor_name, "class='tableheader2'");
    $s = get_project_trans_view_str(98, $order->order_id."&source_type_id=".ST_PURCH_REQ, "Approval Status");
    label_row($s,$approval_statuses[$order->approval_status], "class='tableheader2'");
//    label_row(_("Requisition Comment"), $order->memo_, "class='tableheader2'");
    
    end_outer_table(1);


    start_outer_table(TABLESTYLE2, "width=70%"); // outer table
        table_section(1, "30%");
            locations_list_row("Issuing Location:", "issuing_location", null, '',false);
            ref_row(_("Reference:"), 'pri_reference', '', $Refs->get_next(ST_PR_ISSUE));
            date_row("Date", 'pri_date');
            
        table_section(2, "30%");
            group_companies_list_row("Issuing To Employee Company: ", "employee_company_code", null, false, true);
            group_company_employees_simple_list_row("Issuing To Employee:", "employee_code", get_post('employee_company_code'), null, '');

        
        table_section(3, "40%");
            movement_types_list_row(_("Movement Type:"), 'movement_type', ST_PR_ISSUE, null, '', true);
            
            $_POST['group_company_code'] = get_default_group_company_code();
            hidden("group_company_code", $_POST['group_company_code']);
    
            set_post_not_set_numeric('movement_type', $_POST['movement_type']);
            
            $movement_type = get_movement_type($_POST['movement_type']);
            if (list_updated('movement_type')) {
                unset($_POST['linked_field_id']);
            }
            
            $linked_field = 0;
            if (isset($movement_type['linked_field'])) {
                $linked_field = $movement_type['linked_field'];
            }
            
            switch ($linked_field) {
                case IMLF_CUSTOMER:
                    customer_list_row(_("Customer:"), 'linked_field_id', null, '');
                    break;
                case IMLF_SUPPLIER:
                    supplier_list_row(_("Supplier:"), 'linked_field_id', null, '');
                    break;
                case IMLF_EMPLOYEE:
                    employees_simple_list_row("Employee:", 'linked_field_id', null, '');
                    break;
                case IMLF_VEHICLE:
                    vehicles_list_row('Vehicle:', 'linked_field_id', null, '', true);
                    break;
                case IMLF_ASSET:
                    label_row(_("Asset:"), asset_list('linked_field_id', null, ''));
                    break;
                default:
                    hidden('linked_field_id', 0);
                    break;
            }
            
            hidden('linked_field_type', $linked_field);
            
    //    table_section(3, "40%");
        //yesno_list_row("Make Requisition Complete", "pr_complete");
    
    end_outer_table(1); // outer table
    
    div_end();
}

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

function display_purchase_requisition_items($title, &$order, $po_no=0)
{
	global $path_to_root, $Ajax;

    $Ajax->activate('items_table');
    
	display_heading($title);
    div_start('items_table');
    start_table(TABLESTYLE, "width=100%");
    
    if($order->po_no > 0){
        $th = array("PR Line","Allocs","PO Line",_("Item Code"), _("Item Description"), _("Req. Unit"), _("Req. Qty"),  _("Qty Issued"),
                _("PO Qty"),  _("PO Received Qty"),
                _("Issu. Unit"),_("Issu. Qty"), _("Required Date"),_("Comment"), _("Site"), "");        
    }
    else{
        $th = array("PR Line",_("Item Code"), _("Item Description"), _("Req. Unit"), _("Req. Qty"), 
                _("Qty Issued"), _("Issu. Unit"),_("Issu. Qty"), _("Required Date"),_("Comment"), _("Site"), "");
    }
	//if ( count($order->line_items)) $th[] = '';

	table_header($th);
	$total = 0;
	$k = 0;  //row colour counter

	$id = find_submit('Edit');
	foreach ($order->line_items as $line_no=>$stock_item)
	{
        $prefix = $stock_item->row_id."_".$stock_item->pr_po_line_id."_".$stock_item->po_line_id;

		$total += ($stock_item->standard_cost * $stock_item->quantity);

		if ($id != $line_no)
		{
                    alt_table_row_color($k);
                    
                    if($order->po_no > 0){
                        $ids_text = get_trans_view_str(ST_PURCH_REQ, $order->order_id, $stock_item->row_id);
                        $pr_allocs_link = get_pr_allocation_ids_string($stock_item->po_line_id, $stock_item->pr_po_line_id);
                        $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $order->po_no, $stock_item->po_line_id."(".$order->po_no.")");
//                        $ids_text = $stock_item->row_id." ".$stock_item->pr_po_line_id." ".$stock_item->po_line_id;
//                        $ids_text = $stock_item->row_id." ".$pr_allocs_link." ".$po_link;
                        label_cell($ids_text, 'align=center');
                        label_cell($pr_allocs_link, 'align=center');
                        label_cell($po_link, 'align=center');
                    }
                    else{
//                        $ids_text = $stock_item->row_id;
                        $ids_text = get_trans_view_str(ST_PURCH_REQ, $order->order_id, $stock_item->row_id);
                        label_cell($ids_text, 'align=center');
                    }
                    

                    view_stock_status_cell($stock_item->stock_id);
                    label_cell($stock_item->item_description);
                    
                    label_cell($stock_item->alternate_unit_name);
                    
                    $dec = get_qty_dec($stock_item->stock_id);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
                    qty_cell($stock_item->requested_unit_qty, false, $dec);
                    
                    
                    $tran_unit_issued_qty = $stock_item->issued_qty / $stock_item->convertion_factor;
                    qty_cell($tran_unit_issued_qty, false, $dec);
                    
                    if($order->po_no > 0){
                        qty_cell($stock_item->po_quantity/$stock_item->convertion_factor, false, $dec);

                        $tran_unit_issued_qty = $stock_item->po_quantity_received / $stock_item->convertion_factor;
                        qty_cell($tran_unit_issued_qty, false, $dec);
                    }
                    
                    
                    if(get_post($prefix.'_tran_unit') != ''){
                        $current_tran_unit = get_post($prefix.'_tran_unit');
                    }
                    else{
                        $current_tran_unit = $stock_item->tran_unit;
                    }
                    
                    
                    $alternate_unit = get_alternate_unit_info($stock_item->stock_id, $current_tran_unit);
                    if($alternate_unit == false){
                        $convertion_factor = 1;
                    }
                    else{
                        $convertion_factor = $alternate_unit['convertion_factor'];
                    } 
                    units_list_cells(null, $prefix.'_tran_unit', $stock_item->stock_id, $alternate_unit['alternate_unit'], false, true);
                    $unit_of_parent = get_post($prefix.'_tran_unit');
                    
                    if(list_updated($prefix.'_tran_unit') || !isset($_POST[$prefix."_tran_qty"] )){
                        $base_unit_req_qty_pr = $stock_item->requested_unit_qty * $stock_item->convertion_factor;
                        $suggested_issu_qty_pr = ( $base_unit_req_qty_pr - $stock_item->issued_qty) / $convertion_factor;
                        if($suggested_issu_qty_pr < 0){
                            $suggested_issu_qty_pr = 0;
                        }
                        
                        $base_unit_req_qty_po = $stock_item->po_quantity_received;
                        $suggested_issu_qty_po = ($base_unit_req_qty_po - $stock_item->issued_qty) / $convertion_factor;
                        if($suggested_issu_qty_po < 0){
                            $suggested_issu_qty_po = 0;
                        }
                        
                        $suggested_issu_qty = $suggested_issu_qty_pr;
                        if($po_no > 0 && $suggested_issu_qty_pr > $suggested_issu_qty_po){
                            $suggested_issu_qty = $suggested_issu_qty_po;                            
                        }
                        
                        $_POST[$prefix."_tran_qty"] = number_format($suggested_issu_qty,$dec);
                    }
                    else{
                        $suggested_issu_qty = $_POST[$prefix."_tran_qty"];
                    }
                    
                    if($order->pr_complete){
                    //qty_cells(null, $stock_item->row_id."_qty", $suggested_issu_qty, null, null, $dec);
                        $_POST[$prefix.'_'.'tran_qty'] = 0;
                        amount_decimal_cell($_POST[$prefix.'_'.'tran_qty']);
                        hidden($prefix.'_'.'tran_qty', 0.00);
                    }
                    else{
                        display_quantity_cells(get_post('guid'), $order->trans_type, $stock_item->line_ref, $stock_item->stock_id, 
                                                $stock_item->stock_control_method, $prefix.'_', $suggested_issu_qty, $unit_of_parent, 
                                                $dec = 2, "items_table");
                    }
                        
                    label_cell($stock_item->required_date);
                    label_cell($stock_item->line_comment);
                    
                    project_sites_list_cells(null, $prefix.'_project_site_id', $order->project_id, $stock_item->project_site_id,'');
                    
                    /*
                    edit_button_cell("Edit$line_no", _("Edit"),
                            _('Edit document line'));
                    delete_button_cell("Delete$line_no", _("Delete"),
                            _('Remove line from document'));
                     * 
                     */
                    
                    if($order->pr_complete){
                        label_cell('');
                    }
                    else{
                        check_cells(null, $prefix.'_check_pr_issue_item');
                    }
            
                    end_row();
                    
                    $dec = 0;
		}
		else
		{
			//mio_edit_item_controls($order, $line_no);
		}
	}

	if ($id == -1)
		//mio_edit_item_controls($order);

	//label_row(_("Total"), number_format2($total,user_price_dec()), "align=right colspan=5", "align=right", 2);

    end_table();
	div_end();
}

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

function pr_options_controls()
{
	  echo "<br>";
	  start_table();

	  textarea_row(_("PR Issue Comment:"), 'memo_', null, 50, 3);

	  end_table(1);
}


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


function can_commit_pr_issue($issuing_location, $pri_reference, $pri_date_, $movement_type, $linked_field_id)
{
    global $Refs;
        
    $total_qty = 0;
    if(!(isset($_SESSION['pr_items']->po_no) && $_SESSION['pr_items']->po_no > 0))// No PO
    {
        if(!$_SESSION["wa_current_user"]->can_access('SA_PR_ISSUES_WITHOUT_POS')){ // No access to issue without a PO
            $message = ("You are not allowed to issue items without a purchase order.");
            $set_focus = "";
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
    }
    
    if(!($_SESSION['pr_items']->approval_status == AS_APPROVED || $_SESSION['pr_items']->approval_status == AS_ISSUED))
    {
        $message = ("Status of this Purchase Requisition does not allow item issues.");
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if($_SESSION['pr_items']->pr_complete == 1)
    {
        $message = ("This Purchase Requisition has been generated from a Material Requisition or a Sales Order. No material issuing allowed from this Purchase Requisition.");
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!$issuing_location > 0) 
    {
        $message = ( _("Please select an issuing location."));
        $set_focus = ('issuing_location');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!$Refs->is_valid($pri_reference)) 
    {
        $message = ( _("You must enter a reference."));
        $set_focus = ('pri_reference');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $ref_msg = reference_exists($pri_reference, ST_PR_ISSUE);
    if($ref_msg){
        $message = ($ref_msg);
        $set_focus = ('pri_reference');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!(is_date($pri_date_))) 
    {
        $message = ( _("Entered date is not a valid date."));
        $set_focus = ('pri_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!is_date_in_fiscalyear($pri_date_)) {
        $message = (_("The entered date is not in fiscal year."));
        $set_focus = ('pri_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $msg = is_accounting_period_open($pri_date_);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('pri_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!(strlen($_POST['employee_code']) > 2)) 
    {
        $message = ( _("Please select an issuing to employee."));
        $set_focus = ('employee_code');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!($movement_type > 0)) {
        $input_error = 1;
        $message = ( _('Select a movement type.'));
        $set_focus = ('movement_type');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    } else {
        $mv_tp = get_movement_type($movement_type);
            if ($mv_tp['linked_field'] != IMLF_NOT_LINKED) {

                global $inventory_movement_type_linked_fields;

                if ($mv_tp['linked_field'] == IMLF_EMPLOYEE && ($linked_field_id == 0)) {
                    $input_error = 1;
                    $message = ( _('Select an '.$inventory_movement_type_linked_fields[$mv_tp['linked_field']].'.'));
                    $set_focus = ('linked_field_id');
                    return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
                    
                } elseif ($mv_tp['linked_field'] == IMLF_ASSET && ! ($linked_field_id > 0)) {
                    $input_error = 1;
                    $message = ( _('Select an '.$inventory_movement_type_linked_fields[$mv_tp['linked_field']].'.'));
                    $set_focus = ('linked_field_id');
                    return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
                    
                } elseif (! ($_POST['linked_field_id'] > 0)) {
                    $input_error = 1;
                    $message = ( _('Select a '.$inventory_movement_type_linked_fields[$mv_tp['linked_field']].'.'));
                    $set_focus = ('linked_field_id');
                    return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
                    
                }

                if ($mv_tp['linked_field'] == IMLF_ASSET) {
                    $asset_row = get_asset($linked_field_id);
                        if ($asset_row['type'] == AT_VEHICLE) {
                            $message = ("Selected asset is a Vehicle. Only the assets of types Fixed Asset and Tool are allowed under this Movement Type.");
                            $set_focus = ('linked_field_id');
                            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
                        }
                }
            }
    }
    
    foreach ($_SESSION['pr_items']->line_items as $line_item)
    {
        $prefix = $line_item->row_id."_".$line_item->pr_po_line_id."_".$line_item->po_line_id;
        
        if (!check_num($prefix.'_tran_qty',0))
        {
            $message = (_("Line Id ".$line_item->row_id." : quantity entered for item [".$line_item->stock_id."] is negative or invalid."));
            $set_focus = ($prefix."_tran_qty");
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        $total_qty += $_POST[$prefix."_tran_qty"];

    } 
        
        
    if(!(isset($_SESSION['pr_items']->po_no) && $_SESSION['pr_items']->po_no > 0)){// No PO
        // Validate for over quantity when there is no PO
        foreach ($_SESSION['pr_items']->line_items as $line_item) 
        {
            if($line_item->item_selected == false){
                continue;
            }

            $prefix = $line_item->row_id."_".$line_item->pr_po_line_id."_".$line_item->po_line_id;
        
    //        $alternate_unit = get_convertion_factor($_POST[$prefix."_tran_unit"], $line_item->stock_id);
    //        $convesion_factor = 1;
    //        $alternate_unit = $_POST[$prefix."_tran_unit"];
    //        get_convertion_factor2($line_item->stock_id, $alternate_unit, $convesion_factor);

            $tran_unit = $line_item->tran_unit;
    //        $pr_line = get_pr_detail_record($line_item->row_id);
            $total_issued_qty = get_pr_item_total_issued_qty($line_item->row_id);
            $base_unit_issued_qty = $total_issued_qty;
            get_convertion_factor2($line_item->stock_id, $tran_unit, $tran_unit_convesion_factor);
            $tran_unit_issued_qty = $base_unit_issued_qty / $tran_unit_convesion_factor;

            $base_unit_issuing_qty = $line_item->quantity;
            $tran_unit_issuing_qty = $line_item->tran_qty;

    //        $tran_unit_requested_qty = $line_item->requested_qty / $line_item->convertion_factor;
            $tran_unit_requested_qty = $line_item->requested_qty / $tran_unit_convesion_factor;
    //        if($base_unit_issuing_qty + $base_unit_issued_qty > $line_item->requested_qty + 0.0000001)
            if($tran_unit_issued_qty + $tran_unit_issuing_qty > $tran_unit_requested_qty + 0.0000001)
            {
                $message = (_("Line Id ".$line_item->row_id  
                        ." - Item ".$line_item->stock_id." - ".$line_item->item_description
                        ."<br> Issuing Unit : ". get_unit_descr($line_item->tran_unit) 
                        .", Issuing quantity (".number_format($tran_unit_issuing_qty, 3)
                        .") + already issued qty (".number_format($tran_unit_issued_qty, 3)
                        .") can not be greater than the requested quantity (".number_format($tran_unit_requested_qty,3).")."));
                $set_focus = ($line_item->row_id.'_tran_qty');
                return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
            }
        }
    } 
    
    $vtqd_msg = validate_transaction_quantity_details($_SESSION['pr_items']);		
    if ($vtqd_msg['success'] !== true) 		
    {
        $message = ($vtqd_msg['message']);
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    return array("success"=>true, "set_focus"=>"", "message"=>"");
}

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

//function view_requisition_transfer_header($mrt_no, $suppress_view_link=false)
//{
//    $myrow = get_material_issue_header($mrt_no);
//
//    if (strlen($myrow[0]) == 0)
//    {
//    	display_note(_("The material requisition transfer sent is not valid."));
//    	exit;
//    }
//
//   	start_table(TABLESTYLE, "width=80%");
//
//	$th = array(_("#"), _("Reference"), _("Issuing Location"), _("Receiving Location"),
//			_("Source MR No"), _("Date"), _("Issued By"));
//
//	table_header($th);
//	start_row();
//	if ($suppress_view_link)
//		label_cell($myrow["mrt_no"]);
//	else
//		//label_cell(get_trans_view_str(ST_MATERIAL_ISSUE, $myrow["mrt_no"]));
//            label_cell(get_inventory_trans_view_str(ST_MATERIAL_ISSUE, $myrow["mrt_no"]), "class='tableheader2'");
//        
//	label_cell($myrow["reference"]);
//	label_cell($myrow["issuing_location_name"]);
//	//view_stock_status_cell($myrow["stock_id"], $myrow["StockItemName"]);
//	label_cell($myrow["receiving_location_name"]);
//        label_cell(get_trans_view_str(ST_MATERIAL_REQ, $myrow["source_mtr_no"]));
//	label_cell(sql2date($myrow["created_on"]));
//        label_cell($myrow["created_user_name"]);
//
//	end_row();
//
//	comments_display_row(ST_MATERIAL_ISSUE, $mrt_no);
//
//	end_table();
//
//}
//
////---------------------------------------------------------------------------------
//
//function view_material_requisition_transfer_details($mrt_no, $suppress_view_link=false)
//{
//    $result = get_material_issue_details($mrt_no, ST_REQTRANSFER);
//
//   	start_table(TABLESTYLE, "width=80%");
//
//	$th = array(_("Itemcode"), _("Item Description"), _("Qualtity"));
//
//	table_header($th);
//        
//        while ($myrow = db_fetch($result)){
//            if($myrow["qty"] > 0){
//                start_row();
//
//                label_cell($myrow["stock_id"]);
//                view_stock_status_cell($myrow["stock_id"], $myrow["description"]);
//
//                $quantity = $myrow["qty"];
//
//                $dec = get_qty_dec($myrow["stock_id"]);
//                qty_cell($quantity, false, $dec);
//
//
//                end_row();
//            }
//        }
//
//
//	end_table();
//
//}
//

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


function populate_pr_cart_from_database($cart_name, $pr_no, $po_no=0)
{
        $myrow = get_pr_header($pr_no);
        
        $pr_cart = $_SESSION[$cart_name];
        
        $pr_cart->order_id = $myrow["pr_no"];
        $pr_cart->requesting_location = $myrow["location"];
        $pr_cart->location_name  = $myrow["issuing_location_name"];
        $pr_cart->reference = $myrow["reference"];
        $pr_cart->project_id = $myrow["project_id"];
        $pr_cart->project_description  = $myrow["project_description"];        
        //$pr_cart->approval_path_description  = $myrow["approval_path_description"];
        $pr_cart->pr_date = $myrow["pr_date"];
        $pr_cart->required_date  = $myrow["pr_required_date"];
        $pr_cart->pr_complete = $myrow["pr_complete"];
        $pr_cart->approval_status = $myrow["approval_status"];
        $pr_cart->requestor_name = $myrow["requestor_name"];
        $pr_cart->next_approval_group_id  = $myrow["next_approval_group_id"];
        $pr_cart->row_state  = $myrow["row_state"];
        $pr_cart->po_no  = $po_no;
        $pr_cart->total  = $myrow["total"];
        
        if($po_no > 0){
            $poh = get_po_record($po_no);
            $pr_cart->tax_group_id = $poh['tax_group_id'];
            $pr_cart->tax_included  = $poh['tax_included'];
        }
        else{
            $pr_cart->tax_group_id = 0;
            $pr_cart->tax_included = 0;
        }
        
  
        $pr_details = get_pr_details($pr_cart->order_id, $po_no);

//        get_po_details_array($pod_array, $pr_cart->order_id, $po_no);
        
        $element_no = 0;
        while ($prd_row = db_fetch($pr_details)) 
        {
            $total_issued_qty = get_pr_item_total_issued_qty($prd_row["id"]);
            
            $pr_line = new line_item($prd_row["item_code"], 0, $standard_cost=null, 
                    $prd_row["description"],sql2date($prd_row["required_date"]), $prd_row["line_comment"],
                    null, $prd_row["id"], $total_issued_qty, $prd_row["quantity"], 0, 0, null, 0, 
                    null, null, null, null, null, $prd_row["requested_unit"], $prd_row["requested_unit_qty"], 
                    $prd_row["req_unit_conv_factor"]);

            $pr_line->row_id = $prd_row["id"];
            $pr_line->pr_line_id = $pr_line->row_id;
            $pr_line->stock_control_method = $prd_row["stock_control_method"];
            $pr_line->created_by = $prd_row["created_by"];
            $pr_line->created_on = $prd_row["created_on"];
            $pr_line->category_id = $prd_row["category_id"];
//            $pr_line->po_line_id = $prd_row["po_line_id"];
            $pr_line->quantity = $prd_row["quantity"];
//            $pr_line->order_price = $prd_row["order_price"];
            $pr_line->requested_unit = $prd_row["requested_unit"];
            $pr_line->requested_unit_qty = $prd_row["requested_unit_qty"];
            $pr_line->req_unit_conv_factor = $prd_row["req_unit_conv_factor"];
            $pr_line->estimate_price = $prd_row["estimate_price"];
            $pr_line->task_id = $prd_row["task_id"];
            $pr_line->project_site_id = $prd_row["project_site_id"];
            $pr_line->line_ref = $pr_line->row_id;
            
            $pr_line->po_quantity = $prd_row["po_ordered_qty"];            
            $pr_line->po_quantity_received = $prd_row["po_received_qty"];
            $pr_line->po_line_id = $prd_row["po_detail_id"];
            $pr_line->requisition_id = $prd_row["requisition_id"];
            $pr_line->pr_po_line_id = $prd_row["pr_po_line_id"];
                    
            

            if($po_no>0){
                $po_unit_price = get_tax_free_price_for_item($prd_row["item_code"], $prd_row["unit_price"], $pr_cart->tax_group_id, $pr_cart->tax_included);
                $pr_line->charging_cost = $po_unit_price;
                $pr_line->cost_source = "PO line #".$pr_line->po_line_id;
            }
            else{
                $estimated_unit_price = $pr_line->estimate_price/$pr_line->req_unit_conv_factor;
                $calculated_charging_cost = get_pr_item_calculated_charging_cost($prd_row, $estimated_unit_price);
                
                $pr_line->charging_cost = $calculated_charging_cost['charging_cost'];
                $pr_line->cost_source = $calculated_charging_cost['charging_cost'];
            }
            
                                    
            $pr_cart->line_items[$element_no] = $pr_line;
            $element_no++;
        }

}