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

function add_laundry_order($description, $order_no, $debtor_no, $group_no, $days, $monthly,
	$begin, $end)
{
	return;
        
        $sql = "INSERT INTO ".TB_PREF."recurrent_invoices (description, order_no, debtor_no,
		group_no, days, monthly, begin, end, last_sent) VALUES (".db_escape($description) . ", "
		.db_escape($order_no).", ".db_escape($debtor_no).", "
		.db_escape($group_no).", ".$days.", ".$monthly.", '"
		.date2sql($begin)."', '".date2sql($end)."', '0000-00-00')";
   	db_query($sql,"The recurrent invoice could not be added");
}

function update_laundry_order($selected_id, $description, $order_no, $debtor_no, $group_no, $days, $monthly,
	$begin, $end)
{
	return;
        
        $sql = "UPDATE ".TB_PREF."recurrent_invoices SET 
		description=".db_escape($description).", 
		order_no=".db_escape($order_no).", 
		debtor_no=".db_escape($debtor_no).", 
		group_no=".db_escape($group_no).", 
		days=".$days.", 
		monthly=".$monthly.", 
		begin='".date2sql($begin)."', 
		end='".date2sql($end)."' 
		WHERE id = ".db_escape($selected_id);
   	db_query($sql,"The recurrent invoice could not be updated");
}

function update_pos_invoice_status($id, $status)
{
	$updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
	$sql = "UPDATE ".TB_PREF."pos_invoices SET status = ".db_escape($status)." WHERE id=".db_escape($id);
        
   	db_query($sql,"Pos invoice could not be updated");
}

function add_pos_invoice($invoice)
{
	$updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        if($invoice['trans_no'] <=0)
            return;
        
        $sql = "SELECT id FROM ".TB_PREF."pos_invoices 
                WHERE debtor_tran_no = '".$invoice['trans_no']."' AND 
                        debtor_tran_no <> 0";
        $result = db_query($sql,"Pos invoice id could not be retrieved");
        $my_row = db_fetch($result);
        $id = $my_row['id'];
        
        $status = "";
        if($invoice['application_id'] == 2){
            if($invoice['delivered'] == 1){
                $status = "D";
            }
            else{
                $status = "C";
            }
        }

        if($my_row != null){
            if($invoice['delivered'] == 1){
                update_pos_invoice_status($id, $status);
            }
            return;
        }
            
        $tran_date = change_date_format("YYYYMMDD", $invoice['order_date'], "YYYYMMDD");
        
        $sql = "INSERT INTO ".TB_PREF."pos_invoices (
                            debtor_tran_no, reference, app_id, 
                            terminal_id, pos_user_id,
                            location, tran_date, status, 
                            updated_by, updated_on)
                    VALUES (".db_escape($invoice['trans_no']).", ".db_escape($invoice['reference']).", ".db_escape($invoice['application_id']).", "
                            .db_escape($invoice['terminal_id']).", ".db_escape($invoice['pos_user_name']).", "
                            .db_escape($invoice['location']).", ".db_escape($tran_date).", ".db_escape($status).", "
                            .db_escape($updated_by).", ".db_escape($updated_on).")";

        db_query($sql,"Pos invoice details could not be added");

}

function get_pos_invoices($app_id, $from_date, $to_date,
                            $filter_Location, $filter_status)
{
	//$date = date2sql($date);
	$sql = "SELECT pi.*, u.real_name, l.location_name   
                FROM ".TB_PREF."pos_invoices pi, "
                        .TB_PREF."users u, "
                        .TB_PREF."locations l     
                WHERE pi.updated_by = u.id AND 
                        l.loc_code = pi.location AND 
                        pi.tran_date >= ".db_escape(date2sql($from_date))." AND 
                        pi.tran_date <= ".db_escape(date2sql($to_date));
        
        if($app_id != null || $app_id != ""){
            $sql .= " AND pi.app_id = ".db_escape($app_id);
        }
            
        if($filter_Location != null || $filter_Location != ""){
            $sql .= " AND pi.location = ".db_escape($filter_Location);
        }
        
        if($filter_status != "-1"){
            $sql .= " AND pi.status = ".db_escape($filter_status);
        }
   	
        $result = db_query($sql,"Pos invoices could not be retrieved");
        
        return $result;
}

?>