move_invoice_due_date.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. // this hook offsets the due date of an invoice using order grace days
  3. if (!defined("WHMCS"))
  4. die("This file cannot be accessed directly");
  5. use WHMCS\Database\Capsule;
  6. function get_grace() {
  7. $grace = Capsule::table('tblconfiguration')->where('setting', 'OrderDaysGrace')->first();
  8. return $grace->value;
  9. }
  10. add_hook('InvoiceCreation', 1, function($vars) {
  11. $invoice = localAPI('GetInvoice', ['invoiceid' => $vars['invoiceid']]);
  12. $dueDate = date_create($invoice['duedate']);
  13. $invoiceDate = date_create($invoice['date']);
  14. if($dueDate <= $invoiceDate) {
  15. $grace = get_grace();
  16. array_shift($invoice);
  17. date_add($invoiceDate,date_interval_create_from_date_string("$grace days"));
  18. $invoice['duedate'] = date_format($invoiceDate, 'Y-m-d');
  19. $result = localAPI('UpdateInvoice', $invoice);
  20. if($result['result'] == 'success') {
  21. return true;
  22. }
  23. logModuleCall(
  24. 'InvoiceCreation',
  25. __FUNCTION__,
  26. $result,
  27. "Hook Error",
  28. $invoice
  29. );
  30. return false;
  31. }
  32. return true;
  33. });
  34. ?>