<?php

function build_invoice_reference(&$invoice)
{
    $reference = change_date_format("YYYYMMDD", $invoice['order_date'], "YYYYMMDD");
    $reference = str_replace("/", "", $reference);
    $reference = str_replace("-", "", $reference);
    $reference = str_replace("'\'", "", $reference);
    
    $reference .= "_".substr("00000".$invoice['ticket_no'],-6);
    $reference .= "_".$invoice['application_id'].$invoice['terminal_id'];
    
    $invoice['reference'] = $reference;
}

//function change_date_format($current_date_format,$date, $new_date_format)
//{
//    $day = $month = $year ="";
//    
//    $date = trim($date);
//    $date = str_replace("/", "", $date);
//    $date = str_replace("-", "", $date);
//    
//    switch ($current_date_format) {
//	default:
//	case 'DDMMYYYY':
//            $day = substr($date,0,2);
//            $month = substr($date,2,2);
//            $year = substr($date,4,4);
//            break;
//	case 'MMDDYYYY':
//            $day = substr($date,2,2);
//            $month = substr($date,0,2);
//            $year = substr($date,4,4);
//            break;
//        case 'YYYYMMDD':
//            $day = substr($date,6,2);
//            $month = substr($date,4,2);
//            $year = substr($date,0,4);
//            break;
//    }
//    
//    global $dateseps;
//    $date_separator = $dateseps[user_date_sep()];;
//    
//    switch ($new_date_format) {
//	default:
//	case 'DDMMYYYY':
//            $new_date = $day.$date_separator.$month.$date_separator.$year;
//            break;
//	case 'MMDDYYYY':
//            $new_date = $month.$date_separator.$day.$date_separator.$year;
//            break;
//        case 'YYYYMMDD':
//            $new_date = $year.$date_separator.$month.$date_separator.$day;
//    }
//    
//    return $new_date;
//}

function convert_delivery_date($date, $new_date_format)
{
    global $dateseps;
    
    $day = $month = $year ="";
    
    $date_separator = $dateseps[user_date_sep()];
    
    $date = trim($date);
    $date = str_replace("/", "", $date);
    $date = str_replace("-", "", $date);
    $date = str_replace($date_separator, "", $date);
    
    $delivery_month = substr($date,0,3);
    
    $month = date('m', strtotime($delivery_month . '01'));
    $day = substr($date,3,2);
    
    $current_month = date('M');
    
    if(($current_month == "Nov" || $current_month == "Dec") && ($delivery_month == "Jan" || $delivery_month == "Feb")){
        $year = date('Y', strtotime('+1 year'));
    }
    else{
        $year = date("Y");
    }
    
        
    
    
    switch ($new_date_format) {
	default:
	case 'DDMMYYYY':
            $new_date = $day.$date_separator.$month.$date_separator.$year;
            break;
	case 'MMDDYYYY':
            $new_date = $month.$date_separator.$day.$date_separator.$year;
            break;
        case 'YYYYMMDD':
            $new_date = $year.$date_separator.$month.$date_separator.$day;
    }
    
    return $new_date;
}

function get_application_id($reference)
{
    $application_id = substr($reference, 16, 1);
    
    return $application_id;

}

?>