Invoice test - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Invoice test.php

  1. <?php
  2.  
  3. /**
  4.  * Question:
  5.  * What would you change to make this code more maintainable, testable and developer-friendly?
  6.  *
  7.  * Context:
  8.  * This script runs every night and creates about 2.000 invoices.
  9.  * It takes very long to run and has become impossible to maintain.
  10.  *
  11.  **/
  12.  
  13. require_once 'PdfCreator.php';
  14. require_once 'PHPMailer.php';
  15.  
  16. set_time_limit(60 * 60);
  17. ini_set('memory_limit', '750M');
  18.  
  19. // Daily invoice creation
  20. function create_invoices()
  21. {
  22.     $thisMonth = new DateTime('today');
  23.     $thisMonth->modify('-1 day');
  24.     $query = mysql_query("SELECT * FROM accounts WHERE is_live=1 AND DAYOFMONTH(start_date) = " . $thisMonth->format('j'));
  25.  
  26.     // Create about ~2000 invoices
  27.     while ($row = mysql_fetch_assoc($query))
  28.     {
  29.         $id = $row['id'];
  30.         $total = $row['price'];
  31.  
  32.         //get products total
  33.         $query2 = mysql_query("SELECT * FROM products WHERE account_id = $id");
  34.         while($row2 = mysql_fetch_assoc($query2)) {
  35.             $total += $row2['price'];
  36.         }
  37.  
  38.         //get tax rate for country
  39.         switch ($row['country']) {
  40.             case 'us':
  41.                 $taxrate = 6;
  42.                 break;
  43.             case 'ca':
  44.                 $taxrate = 9.9975;
  45.                 break;
  46.             case 'nl':
  47.                 $taxrate = 21;
  48.                 break;
  49.             case 'de':
  50.                 $taxrate = 18;
  51.                 break;
  52.             case 'uk':
  53.                 $taxrate = 7;
  54.                 break;
  55.         }
  56.  
  57.         if ($row['coupon']) {
  58.             $query3 = mysql_query("SELECT * FROM coupons WHERE code = " . $row['coupon']);
  59.             $row3 = mysql_fetch_row($row);
  60.             $total = $row3['discount'];
  61.         }
  62.  
  63.         if (!$row['paid_startup_fee']) {
  64.             $total += 25;
  65.         }
  66.  
  67.         $query4 = mysql_query("SELECT * FROM modules WHERE account = $id is_cancelled = 0");
  68.         while ($row4 = mysql_fetch_assoc($query4)) {
  69.             if ($row4['type'] === 'domain_name') {
  70.                 if ($row4['cancel_at_next_payment']) {
  71.                     $ch = curl_init();
  72.                     curl_setopt($ch, CURLOPT_URL,"http://api.godaddy.com/revoke_domain");
  73.                     curl_setopt($ch, CURLOPT_POST, 1);
  74.                     curl_setopt($ch, CURLOPT_POSTFIELDS, $row4['domain_name']);
  75.                     curl_exec($ch);
  76.                 }
  77.             }
  78.             $total += $row4['price'];
  79.         }
  80.  
  81.         $row['total'] = $total * (1 + ($taxrate / 100));
  82.         $invoice = file_get_contents('invoice.tpl');
  83.         $invoice = Twig_Template::createFromString($invoice, $row);
  84.  
  85.         $ch = curl_init();
  86.         curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/create_pdf");
  87.         curl_setopt($ch, CURLOPT_POST, 1);
  88.         curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);
  89.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90.         $pdf = curl_exec($ch);
  91.  
  92.         $mail = new PHPMailer('smtp1.mailer.com', 'info', 'secret');
  93.         $mail->setAuth('smtp1.mailer.com', 'info', 'secret');
  94.         $mail->openConnection();
  95.  
  96.         $email = file_get_contents('email.tpl');
  97.         $email = Twig_Template::createFromString($email, $row);
  98.  
  99.         $mail->setFrom('[email protected]', 'Lightspeed HQ');
  100.         $mail->addAddress($row['email'], $row['name']);
  101.         $mail->addAttachment($pdf, 'invoice.pdf');
  102.         $mail->isHtml(true);
  103.         $mail->body = $email;
  104.         $mail->send();
  105.     }
  106. }
  107.  
File Description
  • Invoice test
  • PHP Code
  • 31 Mar-2021
  • 3.33 Kb
You can Share it: