Invoice test - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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.
Result of php executing
Full code of Invoice test.php
- <?php
- /**
- * Question:
- * What would you change to make this code more maintainable, testable and developer-friendly?
- *
- * Context:
- * This script runs every night and creates about 2.000 invoices.
- * It takes very long to run and has become impossible to maintain.
- *
- **/
- require_once 'PdfCreator.php';
- require_once 'PHPMailer.php';
- set_time_limit(60 * 60);
- ini_set('memory_limit', '750M');
- // Daily invoice creation
- function create_invoices()
- {
- $thisMonth = new DateTime('today');
- $thisMonth->modify('-1 day');
- $query = mysql_query("SELECT * FROM accounts WHERE is_live=1 AND DAYOFMONTH(start_date) = " . $thisMonth->format('j'));
- // Create about ~2000 invoices
- while ($row = mysql_fetch_assoc($query))
- {
- $id = $row['id'];
- $total = $row['price'];
- //get products total
- $query2 = mysql_query("SELECT * FROM products WHERE account_id = $id");
- while($row2 = mysql_fetch_assoc($query2)) {
- $total += $row2['price'];
- }
- //get tax rate for country
- switch ($row['country']) {
- case 'us':
- $taxrate = 6;
- break;
- case 'ca':
- $taxrate = 9.9975;
- break;
- case 'nl':
- $taxrate = 21;
- break;
- case 'de':
- $taxrate = 18;
- break;
- case 'uk':
- $taxrate = 7;
- break;
- }
- if ($row['coupon']) {
- $query3 = mysql_query("SELECT * FROM coupons WHERE code = " . $row['coupon']);
- $row3 = mysql_fetch_row($row);
- $total = $row3['discount'];
- }
- if (!$row['paid_startup_fee']) {
- $total += 25;
- }
- $query4 = mysql_query("SELECT * FROM modules WHERE account = $id is_cancelled = 0");
- while ($row4 = mysql_fetch_assoc($query4)) {
- if ($row4['type'] === 'domain_name') {
- if ($row4['cancel_at_next_payment']) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,"http://api.godaddy.com/revoke_domain");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $row4['domain_name']);
- curl_exec($ch);
- }
- }
- $total += $row4['price'];
- }
- $row['total'] = $total * (1 + ($taxrate / 100));
- $invoice = file_get_contents('invoice.tpl');
- $invoice = Twig_Template::createFromString($invoice, $row);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/create_pdf");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $invoice);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $pdf = curl_exec($ch);
- $mail = new PHPMailer('smtp1.mailer.com', 'info', 'secret');
- $mail->setAuth('smtp1.mailer.com', 'info', 'secret');
- $mail->openConnection();
- $email = file_get_contents('email.tpl');
- $email = Twig_Template::createFromString($email, $row);
- $mail->addAddress($row['email'], $row['name']);
- $mail->addAttachment($pdf, 'invoice.pdf');
- $mail->isHtml(true);
- $mail->body = $email;
- $mail->send();
- }
- }