<?php
     //shared ip check
     //get the domains that are using their servers default ip
     $query = "SELECT d.id FROM domains d, server s, domain_customdomainfields cd WHERE d.serverid=s.id AND s.sharedip=cd.value AND cd.customid=1 AND cd.userid=d.id";
     $result = mysql_query($query) or die("ERROR:".$query);
     while(list($domainid) = mysql_fetch_row($result))
     {
      	$query2 = "update domains set shared=1 where id=$domainid";
        mysql_query($query2) or die("ERROR:".$query2);
     }

    //set the recurringfee's nextinvoice date, based on the user's net invoice date
     $query = "SELECT r.id,u.nextbilldate,u.paymentterm FROM users u, recurringfee r WHERE r.customerid=u.id";
     $result = mysql_query($query) or die("ERROR:".$query);
     while(list($recurringid,$nextbilldate,$paymentterm) = mysql_fetch_row($result))
     {
          $query2 = "UPDATE recurringfee SET nextbilldate='$nextbilldate',paymentterm=$paymentterm WHERE id=$recurringid";
          mysql_query($query2) or die("ERROR:".$query2);
     }

	 //set the domain's next invoice date, based on the user's next invoice date
     $query = "SELECT d.id,u.nextbilldate,u.paymentterm FROM users u, domains d WHERE d.customerid=u.id";
     $result = mysql_query($query) or die("ERROR:".$query);
     while(list($domainid,$nextbilldate,$paymentterm) = mysql_fetch_row($result))
     {
          $query2 = "UPDATE domains SET nextbilldate='$nextbilldate',paymentterm=$paymentterm WHERE id=$domainid";
          mysql_query($query2) or die("ERROR:".$query2);
     }

     //I need to create a recurring fee for every domain that is recurring
     $query = "SELECT id,nextbilldate,customerid,paymentterm FROM domains WHERE recurring=1";
     $result = mysql_query($query) or die("ERROR:".$query);
     while(list($domainid,$nextbilldate,$customerid,$paymentterm) = mysql_fetch_row($result))
     {
      	  //ensure I'm not adding a recurring fee for a domain that already has one
          $query2 = "SELECT customerid FROM recurringfee WHERE customerid=$customerid AND appliestoid=$domainid AND billingtypeid=-1";
          $result2 = mysql_query($query2) or die("ERROR:".$query2);
          $num = mysql_numrows($result2);

          //if recurring fee does not exist add it
          if ($num==0){
             $query3 = "INSERT INTO recurringfee(customerid,nextbilldate,appliestoid,billingtypeid,paymentterm) VALUES($customerid,'$nextbilldate',$domainid,-1,$paymentterm)";
             mysql_query($query3) or die("ERROR:".$query3);
          }
     }
?>
