Unsupported scripts and control panel web app for a hosting company
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

billing.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/php -q
  2. <?PHP
  3. chdir('/home/utd/control');
  4. require_once('lib/database.php');
  5. require_once('lib/log.php');
  6. require_once('lib/common.php');
  7. chdir('/home/utd/scripts');
  8. // First off, let's disable anything that's not been paid
  9. $sql = 'SELECT user_id, user_email, package_name, up_expires, up_cost, ';
  10. $sql .= 'up_id FROM userpackages NATURAL JOIN packages NATURAL JOIN ';
  11. $sql .= 'users WHERE up_active = 1 AND up_expires < '.time();
  12. $res = mysql_query($sql);
  13. while ($row = mysql_fetch_array($res)) {
  14. $user = $row['user_id'];
  15. $addr = $row['user_email'];
  16. $name = $row['package_name'];
  17. $date = $row['up_expires'];
  18. $upid = $row['up_id'];
  19. $cost = $row['up_cost'];
  20. $subj = 'UTD-Hosting package cancellation: '.$name;
  21. $body = 'This is an automatic notifcation. The "'.$name.'" package has now ';
  22. $body .= 'been disabled on your account. You will no longer have access to ';
  23. $body .= 'services provided as a part of this package. If you have no other ';
  24. $body .= 'active packages, your account will be automatically closed within ';
  25. $body .= '14 days.'."\n\n".'If you wish to retrieve data stored in your ';
  26. $body .= 'account, or wish to renew a package, or have any enquiries about ';
  27. $body .= 'this message, please e-mail support@utd-hosting.com.'."\n\n";
  28. $body .= ' -- UTD-Hosting support';
  29. $head = 'From: UTD-Hosting support <support@utd-hosting.com>';
  30. mail($addr, $subj, $body, $head);
  31. $sql = 'UPDATE userpackages SET up_active = 0 WHERE up_id = '.$upid;
  32. mysql_query($sql);
  33. logger::log("Package '$name' cancelled (no payment)", $user, logger::info);
  34. }
  35. // Now select anything that's outstanding and doesn't have an invoice
  36. $inv = array();
  37. $sql = 'SELECT up_id, package_id, user_id, up_expires, up_cost FROM ';
  38. $sql .= 'userpackages WHERE up_invoice = 1 AND up_active = 1 AND up_expires ';
  39. $sql .= '< '.strtotime('+1 month');
  40. $res = mysql_query($sql) or print(mysql_error()."\n".$sql);
  41. while ($row = mysql_fetch_assoc($res)) {
  42. $sql = 'SELECT bill_id FROM billitems NATURAL JOIN bills WHERE user_id = ';
  43. $sql .= $row['user_id'].' AND up_id = '.$row['up_id'].' AND bill_paid = 0';
  44. $re2 = mysql_query($sql);
  45. if (mysql_num_rows($re2) > 0) {
  46. continue;
  47. }
  48. if (!isset($inv[$row['user_id']])) {
  49. $inv[$row['user_id']] = array();
  50. }
  51. $inv[$row['user_id']][] = array($row['up_id'], $row['up_cost']);
  52. }
  53. // And now iterate through any invoices we need to make
  54. foreach ($inv as $user => $items) {
  55. $sql = 'SELECT user_email FROM users WHERE user_id = '.$user;
  56. $res = mysql_query($sql);
  57. $row = mysql_fetch_assoc($res);
  58. $tot = 0;
  59. foreach ($items as $data) {
  60. $tot += $data[1];
  61. }
  62. // Add it to the db
  63. $sql = 'INSERT INTO bills (user_id, bill_due, bill_generated, bill_total) ';
  64. $sql .= 'VALUES ('.$user.', '.strtotime('+1 month').', '.time().', '.$tot.')';
  65. $res = mysql_query($sql);
  66. $bil = mysql_insert_id();
  67. // And the items
  68. foreach ($items as $data) {
  69. list($pid, $cst) = $data;
  70. $sql = 'INSERT INTO billitems (bill_id, up_id, bi_cost) VALUES ('.$bil.', ';
  71. $sql .= $pid.', '.$cst.')';
  72. $res = mysql_query($sql);
  73. }
  74. $tot = sprintf('%01.2f', $tot/100);
  75. $pkg = count($items).' package'.(count($items) != 1 ? 's' : '');
  76. // And send them mail
  77. $to = $row['user_email'];
  78. $subj = 'UTD-Hosting invoice notification';
  79. $msg = 'This is an automatic notification. An invoice for £'.$tot.' has ';
  80. $msg .= 'been issued to you. This is for the extension of '.$pkg.' due to ';
  81. $msg .= 'expire within the next month.'."\n\n";
  82. $msg .= 'You may view and pay this invoice via the UTD-Hosting control panel';
  83. $msg .= ' at the following address: https://secure.utd-hosting.com/control/';
  84. $msg .= 'viewinvoice/'.$bil.' or you can log in normally and follow the ';
  85. $msg .= '"My Invoices" link in the main menu.'."\n\n";
  86. $msg .= 'If you have any queries about this invoice, please contact ';
  87. $msg .= 'sales@utd-hosting.com.'."\n\n".' -- UTD-Hosting';
  88. $head = 'From: UTD-Hosting accounts <sales@utd-hosting.com>';
  89. mail($to, $subj, $msg, $head);
  90. logger::log("Bill issued for £$tot", $user, logger::normal);
  91. }
  92. ?>