invoices - 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 invoices.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.         $query2 = mysql_query("SELECT * FROM products WHERE account_id = $id");
  33.         while($row2 = mysql_fetch_assoc($query2)) {
  34.             $total += $row2['price'];
  35.         }
  36.  
  37.         switch ($row['country']) {
  38.             case 'us':
  39.                 $taxrate = 6;
  40.                 break;
  41.             case 'ca':
  42.                 $taxrate = 9.9975;
  43.                 break;
  44.             case 'nl':
  45.                 $taxrate = 21;
  46.                 break;
  47.             case 'de':
  48.                 $taxrate = 18;
  49.                 break;
  50.             case 'uk':
  51.                 $taxrate = 7;
  52.                 break;
  53.         }
  54.  
  55.         if ($row['coupon']) {
  56.             $query3 = mysql_query("SELECT * FROM coupons WHERE code = " . $row['coupon']);
  57.             $row3 = mysql_fetch_row($row);
  58.             $total = $row3['discount'];
  59.         }
  60.  
  61.         if (!$row['paid_startup_fee']) {
  62.             $total += 25;
  63.         }
  64.  
  65.         $query4 = mysql_query("SELECT * FROM modules WHERE account = $id is_cancelled = 0");
  66.         while ($row4 = mysql_fetch_assoc($query4)) {
  67.             if ($row4['type'] === 'domain_name') {
  68.                 if ($row4['cancel_at_next_payment']) {
  69.                     $ch = curl_init();
  70.                     curl_setopt($ch, CURLOPT_URL,"http://api.godaddy.com/revoke_domain");
  71.                     curl_setopt($ch, CURLOPT_POST, 1);
  72.                     curl_setopt($ch, CURLOPT_POSTFIELDS, $row4['domain_name']);
  73.                     curl_exec($ch);
  74.                 }
  75.             }
  76.             $total += $row4['price'];
  77.         }
  78.  
  79.         $row['total'] = $total * (1 + ($taxrate / 100));
  80.         $invoice = file_get_contents('invoice.tpl');
  81.         $invoice = Twig_Template::createFromString($invoice, $row);
  82.  
  83.         $ch = curl_init();
  84.         curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/create_pdf");
  85.         curl_setopt($ch, CURLOPT_POST, 1);
  86.         curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);
  87.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  88.         $pdf = curl_exec($ch);
  89.  
  90.         $mail = new PHPMailer('smtp1.mailer.com', 'info', 'secret');
  91.         $mail->setAuth('smtp1.mailer.com', 'info', 'secret');
  92.         $mail->openConnection();
  93.  
  94.         $email = file_get_contents('email.tpl');
  95.         $email = Twig_Template::createFromString($email, $row);
  96.  
  97.         $mail->setFrom('[email protected]', 'Lightspeed HQ');
  98.         $mail->addAddress($row['email'], $row['name']);
  99.         $mail->addAttachment($pdf, 'invoice.pdf');
  100.         $mail->isHtml(true);
  101.         $mail->body = $email;
  102.         $mail->send();
  103.     }
  104. }
  105.  
File Description
  • invoices
  • PHP Code
  • 18 Feb-2021
  • 3.27 Kb
You can Share it: