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

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

function approve_po(&$po_obj)
{
	begin_transaction();
	hook_db_prewrite($po_obj, ST_PURCHORDER);
        
        $id_user = $_SESSION['wa_current_user']->user;
	$datetime = date('Y-m-d H:i:s');

    /*Update the purchase order header with any changes */
    $sql = "UPDATE ".TB_PREF."purch_orders SET Comments=" . db_escape($po_obj->Comments) . ",
		requisition_no= ". db_escape( $po_obj->supp_ref). ",
		into_stock_location=" . db_escape($po_obj->Location). ",
		ord_date='" . date2sql($po_obj->orig_order_date) . "',
		delivery_address=" . db_escape($po_obj->delivery_address).",
		total=". db_escape($po_obj->get_trans_total()).",
		tax_included=". db_escape($po_obj->tax_included).", 
                approved_by=". db_escape($id_user).",
                approved_on=". db_escape($datetime).",
                approval_status= ". db_escape($po_obj->po_approval_status) ;
                
    $sql .= " WHERE order_no = " . $po_obj->order_no;

	db_query($sql, "The purchase order could not be updated");

	$sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no="
		.db_escape($po_obj->order_no);
	db_query($sql, "could not delete old purch order details");

    /*Now Update the purchase order detail records */
    foreach ($po_obj->line_items as $po_line)
    {
    	$sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code, 
    		description, delivery_date, unit_price, quantity_ordered, quantity_received, original_order_qty, 
                order_unit, order_unit_qty, order_unit_conv_factor) VALUES ("
			.db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). ","
			.$po_obj->order_no . ","
			.db_escape($po_line->stock_id). ","
			.db_escape($po_line->item_description). ",'"
			.date2sql($po_line->req_del_date) . "',"
			.db_escape($po_line->price) . ", "
			.db_escape($po_line->quantity) . ", "
			.db_escape($po_line->qty_received) . ", "
                        .db_escape($po_line->quantity) . ", "
                        .db_escape($po_line->tran_unit). " , " 
                        .db_escape($po_line->tran_qty). " , " 
                        .db_escape($po_line->convertion_factor). ")";
		
		db_query($sql, "One of the purchase order detail records could not be updated");
    }

	// add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
        
//        update_pr_history($po_obj->order_no, $po_obj->reference, $po_obj->approval_hierarchy, 
//                    $id_user, $datetime, $po_obj->pr_status, $po_obj->Comments);
        
	add_audit_trail($po_obj->trans_type, $po_obj->order_no, Today(), _("Updated."));
	hook_db_postwrite($po_obj, ST_PURCHORDER);
	commit_transaction();

	return $po_obj->order_no;
}

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


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


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

function get_sql_for_po_approve_search($current_user_id)
{
    
	$sql = "SELECT 
                porder.order_no, 
                porder.reference,
                supplier.supp_name, 
                location.location_name,
                porder.requisition_no, 
                porder.ord_date,
                creu.real_name,
                supplier.curr_code 

            FROM 
                ".TB_PREF."purch_orders as porder, 
                ".TB_PREF."projects_master as p, 
                ".TB_PREF."suppliers as supplier, 
                ".TB_PREF."locations as location, 
                ".TB_PREF."approvers as apr,
                ".TB_PREF."user_data_access_groups as udag,
                ".TB_PREF."users creu  
            WHERE porder.project_id = p.project_id AND 
                porder.supplier_id = supplier.supplier_id AND 
                location.loc_code = porder.into_stock_location AND 
                (porder.approval_status = ".AS_AWAITING_APPROVAL." 
                    OR porder.approval_status = ".AS_WITH_THE_APPROVER." 
                    OR porder.approval_status = ".AS_BACK_TO_ORIGINATOR." 
                    OR porder.approval_status = ".AS_REOPENED.") AND 
                porder.next_approval_group_id = apr.approval_group_id AND 
                p.dag_id = udag.dag_id AND 
                udag.user_id = apr.user_id AND  
                porder.created_by = creu.id AND 
                apr.user_id = ".$current_user_id; 


	return $sql;
}



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

?>