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 'de':
  47.                 $taxrate = 18;
  48.                 break;
  49.         }
  50.  
  51.         if (!$row['paid_startup_fee']) {
  52.             $total += 25;
  53.         }
  54.  
  55.         $row['total'] = $total * (1 + ($taxrate / 100));
  56.         $invoice = file_get_contents('invoice.tpl');
  57.         $invoice = Twig_Template::createFromString($invoice, $row);
  58.  
  59.         $ch = curl_init();
  60.         curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/create_pdf");
  61.         curl_setopt($ch, CURLOPT_POST, 1);
  62.         curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);
  63.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  64.         $pdf = curl_exec($ch);
  65.  
  66.         $mail = new PHPMailer('smtp1.mailer.com', 'info', 'secret');
  67.         $mail->setAuth('smtp1.mailer.com', 'info', 'secret');
  68.         $mail->openConnection();
  69.  
  70.         $email = file_get_contents('email.tpl');
  71.         $email = Twig_Template::createFromString($email, $row);
  72.  
  73.         $mail->setFrom('[email protected]', 'Lightspeed HQ');
  74.         $mail->addAddress($row['email'], $row['name']);
  75.         $mail->addAttachment($pdf, 'invoice.pdf');
  76.         $mail->body = $email;
  77.         $mail->send();
  78.     }
  79. }
  80.  
File Description
  • invoice test
  • PHP Code
  • 01 Apr-2021
  • 2.32 Kb
You can Share it: