Jump to content

Cancel invoices for domains


hostingarg

Recommended Posts

Hello.

I used a script via cronjob to cancel invoices for expired domains.

The script works correctly on WHMCS 6 with php 5.6

 

The script is as follows:

 


<?
$emailheader = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN''http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<title>Documento sin tulo</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<style type='text/css'>
<!--
.Estilo2 {font-family: Calibri, Verdana, Arial, Helvetica, sans-serif}
.Estilo3 {
  color: #333333;
  font-size: 10px;
  font-family: Calibri, Verdana, Arial, Helvetica, sans-serif;
}
a:link {
  color: #333333;
}
a:hover {
  color: #999999;
}
a:visited {
  color: #333333;
}
a:active {
  color: #333333;
}
.Estilo8 {
  font-family: Calibri, Verdana, Arial, Helvetica, sans-serif;
  font-size: 10px;
}
.Estilo10 {font-size: 10px; color: #333333;}
.Estilo11 {font-weight: bold; color: #FF0000;}
-->
</style>
</head>

<body>";

echo "<html>
<body>
<table width='80%'>
  <tr>
     <th align='left'>Cliente</th><th align='left'>Dominio</th><th align='left'>Estado</th><th align='left'>Expiry Date</th><th align='left'>Fecha Hoy</th><th align='left'>Caducado?</th>
  </tr>";

# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);

require_once '........./clients/init.php';

$query = "SELECT * FROM tbldomains ORDER BY domain ASC";
$result = mysql_query($query);

for ($c=0; $c < mysql_num_rows($result);$c++){
  $domain = mysql_result($result,$c,"domain");
  $status = mysql_result($result,$c,"status");
  $whmcsexpirydate = mysql_result($result,$c,"expirydate");
  $userid = mysql_result($result,$c,"userid");
  $recurringamount = mysql_result($result,$c,"recurringamount");

  // get client information

  $query2 = "SELECT * FROM tblclients WHERE id = $userid";
  $result2 = mysql_query($query2);

  $clientemail = mysql_result($result2,0,"email");
  $firstname = mysql_result($result2,0,"firstname");
  $lastname = mysql_result($result2,0,"lastname");
  $companyname = mysql_result($result2,0,"companyname");

  // get today's date

  $hoy = date("Y-m-d");
  $hoy_formateado = date("d/m/Y");

  if ($whmcsexpirydate == $hoy){
     $correo = $emailheader."<img src = 'logo.png'><br>
     <br>
     Estimado/a $firstname $lastname ($companyname),<br>
     <br>
     <font color = '#990000'>Su dominio <b>$domain</b> ha caducado hoy, dia $hoy_formateado.</font><br>
     <br>
     <b>Le recordamos que si usted decide no renovar este dominio, corre el riesgo de perder este dominio para siempre.</b><br>
     <br>
     Si no era su intencion dejar que este dominio caducase, o si cree que ha recibido este comunicado por error, por favor abra un ticket de soporte inmediatamente para que podamos solucionar cualquier incidencia que haya podido ocurrir.<br>
     <br>

     <font size ='1' color = '#aaaaaa'>Numero de Cliente $userid</font><br>
     <br>
     </body>
     </html>";

     $correo_stripped = "Estimado $firstname $lastname ($companyname),<br>
     <br>
     <font color = #990000>Su dominio <b>$domain</b> ha caducado hoy, dia $hoy_formateado.</font><br>
     <br>
     Le recordamos que si usted decide no restaurar este dominio, corre el riesgo de perder este dominio para siempre.<br>
     <br>
     Si no era su intencion dejar que este dominio caducase, o si cree que ha recibido este comunicado por error, por favor abra un Ticket de Soporte con el Departamento tecnico inmediatamente para que podamos solucionar cualquier incidencia que haya podido ocurrir.<br>
     <br>
     <font size =1 color = #aaaaaa>Numero de Cliente $userid<br>
     <br>";

     echo "<tr>
           <td align='left'>$clientemail</td><td align='left'>$domain</td><td align='left'>$status</td><td align='left'>$whmcsexpirydate</td><td align='left'>$hoy_formateado</td>
           <td align='left'><font color=#990000>CADUCADO HOY</font></td>";

     mail("micorreo@correo.com", "Su dominio $domain ha caducado", $correo, "From: Yo <micorreo@correo.com>\n"."MIME-Version: 1.0\n" .   "Content-type: text/html; charset=iso-8859-1");

     mail($clientemail, "Su dominio $domain ha caducado", $correo, "From: Yo <micorreo@correo.com>\n"."MIME-Version: 1.0\n" .   "Content-type: text/html; charset=iso-8859-1");

// ---------------------------------------------- SCRIPT CORE ------------------------------------------------------

       $clientquery2 = "INSERT INTO tblemails (userid,subject,message,`date`,`to`) VALUES ('$userid','Su dominio $domain ha caducado','$correo_stripped','$hoy','$clientemail') ";
       $clientresult2= mysql_query($clientquery2) or die (mysql_error());

       // eliminar articulo de albaran o cancelar albaran si era lo unico en factura

       $invoiceitemquery = "SELECT * FROM tblinvoiceitems WHERE userid = '$userid' AND type = 'Domain' AND description LIKE '%$domain%'";
       $invoiceitemresult= mysql_query($invoiceitemquery) or die (mysql_error());

        // cycle thru invoice items

        for ($inv=0; $inv < mysql_num_rows($invoiceitemresult);$inv++){

           $invoiceitemid = mysql_result($invoiceitemresult,$inv,"id");
           $invoiceid = mysql_result($invoiceitemresult,$inv,"invoiceid");

           $invoicequery = "SELECT * FROM tblinvoices WHERE id = '$invoiceid' ";
           $invoiceresult = mysql_query($invoicequery) or die (mysql_error());
           $subtotal = mysql_result($invoiceresult,0,"subtotal");
           $totalviejo = mysql_result($invoiceresult,0,"total");
           $tax = mysql_result($invoiceresult,0,"tax");
           $taxrate = mysql_result($invoiceresult,0,"taxrate");
           $invstatus = mysql_result($invoiceresult,0,"status");

           // ----------------- albaranes pendientes de pago. filtramos aqui-------
           if ($invstatus == "Unpaid"){
               // asegurarnos que era el unico articulo antes de cancelar el albaran
               $invoicecheckquery = "SELECT * FROM tblinvoiceitems WHERE invoiceid = '$invoiceid' ";
               $invoicecheckresult = mysql_query($invoicecheckquery) or die (mysql_error());

               if (mysql_num_rows($invoicecheckresult) == 1) { // si era el unico articulo y el total del albaran coincide con el total del valor recurrente del dominio SOLO cancelar albaran completamente, sin eliminar articulos
                   $invoiceupdatequery = "UPDATE tblinvoices SET status = 'Cancelled' WHERE id = '$invoiceid' ";
                   $invoiceupdateresult= mysql_query($invoiceupdatequery) or die (mysql_error());
                   $acciones.="<br>Se cancela la factura $invoiceid. Era el unico articulo existente.";
               }
               else if (mysql_num_rows($invoicecheckresult) > 1){ // si no era el unico articulo, eliminar ese articulo de albaran solamente y restar cantidad del subtotal

                   $invoiceupdatequery = "UPDATE tblinvoices SET status = 'Cancelled' WHERE id = '$invoiceid' ";
                   $invoiceupdateresult= mysql_query($invoiceupdatequery) or die (mysql_error());
                   $acciones.="<br>Se cancela la factura $invoiceid. HAY MAS ITEMS EN ESTA FACTURA.";


             }
               else $acciones.="<br>Hubo un fallo.";
           } // -------------------------------- EOF Unpaid filter ----------------------------

       } // EOF cycle thru invoice items

   } // EOF procesar solo domiios que caducan hoy
   echo "</tr>"; // close row

} // EOF cycle thru domains

// ---------------------------------------------- EOF SCRIPT CORE ------------------------------------------------------

mail("micorreo@correo.com", "Acciones dominios caducados $hoy_formateado", $acciones, "From: Yo <micorreo@correo.com>\n"."MIME-Version: 1.0\n" .   "Content-type: text/html; charset=iso-8859-1");

echo "</table></body></html>";

?>

 

I run this script by cronjob once a day.

 

Well, now i want convert this script to Hook, to run with cron of whmcs.

 

Is there anyone who can help me with this?

I currently use WHMCS 7, php 7 and the script stopped working with this new configuration.

 

Thanks

Link to comment
Share on other sites

  • 3 weeks later...

Hi.

 

I did not review the script but since version 6 was deprecated the select_query (), update_query (), insert_query () and full_query ()

You should use Capsule now.

 

You can use AfterCronJob or PreCronJob Hooks.

Crete a php file into /includes/hooks directory with the script and the hook declaration.

If you use hooks you will not be able to add content to the report that is sent by email. If you need to send the result by mail you will have to deal with the code generation to send the email.

 

Docs:

http://docs.whmcs.com/Hooks

http://docs.whmcs.com/Hooks:PreCronJob

http://docs.whmcs.com/Hooks:AfterCronJob

http://docs.whmcs.com/Interacting_With_The_Database#Deprecated_functionality

Link to comment
Share on other sites

Hi.

 

I did not review the script but since version 6 was deprecated the select_query (), update_query (), insert_query () and full_query ()

You should use Capsule now.

 

mysql_query is way back of php 7. That's why it stopped working, along with version 6 deprecated queries. Hooks with Capsule works smoothly.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated