<?php

# ---------------------------------------------------------
# ADD NEW STATUS FIELD TO THE RECURRINGFEE TABLE
# ---------------------------------------------------------
$query = "ALTER TABLE `recurringfee` "
        ."ADD `status` "
        ."TINYINT( 1 ) "
        ."NOT NULL "
        ."DEFAULT '1' "
        ."AFTER `monthlyusage` ";
$result = mysql_query($query) or die("ERROR in query: $query");


# ---------------------------------------------------------
# SEARCH ALL THE RECURRING PRODUCTS THAT DOES NOT HAVE A RECURRING FEE
# ---------------------------------------------------------
$query2 = "SELECT d.id, "
         ."d.nextbilldate, "
         ."d.paymentterm, "
         ."d.CustomerID "
         ."FROM domains d "
         ."LEFT JOIN recurringfee rf "
         ."ON rf.appliestoid = d.id "
         ."AND rf.billingtypeid = -1 "
         ."WHERE d.recurring != 0 "
         ."AND rf.id IS NULL ";
$result2 = mysql_query($query2) or die("ERROR in query: $query2");

# ---------------------------------------------------------
# CREATE THE RECURRING FEE FOR ALL THE RECURRING PRODUCTS THAT DOES NOT HAVE IT, BUT SET STATUS TO ZERO
# ---------------------------------------------------------
while(list($d_id, $d_nextbilldate, $d_paymentterm, $d_CustomerID) = mysql_fetch_row($result2)){
    $query3 = "INSERT INTO recurringfee "
             ."(customerid, "
             ."appliestoid, "
             ."billingtypeid, "
             ."nextbilldate, "
             ."paymentterm, "
             ."status) "
             ."VALUES ('".$d_CustomerID."', "
             ."'".$d_id."', "
             ."-1, "
             ."'".$d_nextbilldate."', "
             ."'".$d_paymentterm."', "
             ."0) ";
    $result3 = mysql_query($query3) or die("ERROR in query: $query3");
}

# ---------------------------------------------------------
# DROPS DEPRECATED FIELDS FROM THE DOMAINS TABLE
# ---------------------------------------------------------
$query4 = "ALTER TABLE `domains` "
         ."DROP `nextbilldate` ";
$result4 = mysql_query($query4) or die("ERROR in query: $query4");

$query5 = "ALTER TABLE `domains` "
         ."DROP `paymentterm` ";
$result5 = mysql_query($query5) or die("ERROR in query: $query5");

?>
