public static function run_cron($debug = false) { // we only want to perform these cron actions if we're after a certain time of day // because we dont want to be generating these renewals and sending them at midnight, can get confusing $after_time = module_config::c('invoice_automatic_after_time', 7); $time_of_day = date('G'); if ($time_of_day < $after_time) { if ($debug) { echo "Not performing automatic subscription operations until after {$after_time}:00 - it is currently {$time_of_day}:" . date('i') . "<br>\n"; } return; } // find all automatic subscriptions and renew them (if applicable) $sql = "SELECT * FROM `" . _DB_PREFIX . "subscription` s "; $sql .= " WHERE s.automatic_renew = 1"; $subscriptions = qa($sql); foreach ($subscriptions as $subscription) { if ($subscription['automatic_renew']) { if ($debug) { echo "<br>\nProcessing subscription renewals for subscription " . module_subscription::link_open($subscription['subscription_id'], true) . "<br>\n<br>\n"; } // find all the members/customers from this subscription //$members = module_subscription::get_subscribed_members($subscription['subscription_id']); //$customers = module_subscription::get_subscribed_customers($subscription['subscription_id']); $owners = module_subscription::get_subscribed_owners($subscription['subscription_id']); foreach ($owners as $member) { if (!$member['next_generation_date'] || $member['next_generation_date'] == '0000-00-00') { continue; } if (!$member['next_due_date'] || $member['next_due_date'] == '0000-00-00') { continue; } if ($debug) { echo "Doing: " . $member['owner_table'] . " " . $member['owner_id'] . "<br>\n"; } // check permissions for logged in users, dont want the cron to run when someone is logged in and no access to this account. if (module_security::is_logged_in()) { switch ($member['owner_table']) { case 'website': $website_perm_check = module_website::get_website($member['owner_id']); if (!$website_perm_check || $website_perm_check['website_id'] != $member['owner_id']) { continue 2; } if ($debug) { echo "permission pass for website: " . $website_perm_check['website_id']; } break; case 'customer': $customer_perm_check = module_customer::get_customer($member['owner_id']); if (!$customer_perm_check || $customer_perm_check['customer_id'] != $member['owner_id']) { continue 2; } if ($debug) { echo "permission pass for customer: " . $customer_perm_check['customer_id']; } break; } } // is the last invoice unpaid? $history = self::get_subscription_history($subscription['subscription_id'], $member['owner_table'], $member['owner_id']); $next_due_time_invoice_created = false; $invoice_unpaid = false; if (isset($member['recur_limit']) && (int) $member['recur_limit'] > 0 && count($history) >= (int) $member['recur_limit']) { if ($debug) { echo " - not renewing this one because it has hit our recur limit of " . $member['recur_limit'] . "<br>\n"; } continue; } foreach ($history as $h) { $last_invoice = module_invoice::get_invoice($h['invoice_id']); if (!$last_invoice || $last_invoice['date_cancel'] != '0000-00-00') { continue; } // check the new 'next_due_date' entry in the db table if (isset($h['from_next_due_date']) && $h['from_next_due_date'] && $h['from_next_due_date'] != '0000-00-00') { // we're using the new method of checking when an invoice was generated, rather than the confusing invoice 'date_create' check below if ($debug) { echo " - checking if next_due_date " . print_date($member['next_due_date']) . " matches subscription history from_next_due_date for invoice " . module_invoice::link_open($h['invoice_id'], true, $last_invoice) . " from_next_due_date: " . print_date($h['from_next_due_date']) . " (invoice create_date: " . print_date($last_invoice['date_create']) . ")<br>\n"; } if (print_date($member['next_due_date']) == print_date($h['from_next_due_date'])) { //print_date($last_invoice['date_create'])){ // this invoice is for the next due date. $next_due_time_invoice_created = $last_invoice; } } else { if ($debug) { echo " - checking if next_generation_date (" . print_date($member['next_generation_date']) . ") or next_due_date (" . print_date($member['next_due_date']) . ") matches invoice " . module_invoice::link_open($h['invoice_id'], true, $last_invoice) . " created date (" . print_date($last_invoice['date_create']) . ") <br>\n"; } if (print_date($member['next_generation_date']) == print_date($last_invoice['date_create']) || print_date($member['next_due_date']) == print_date($last_invoice['date_create'])) { //print_date($last_invoice['date_create'])){ // this invoice is for the next due date. $next_due_time_invoice_created = $last_invoice; } } if ($last_invoice['total_amount_due'] > 0) { $invoice_unpaid = true; } } //self::generate_subscription_invoice($subscription_id, $customer_hack, $member_id, $date, $amount) $next_due_time = strtotime($member['next_generation_date']); if ($debug) { echo " - next subscription time is " . $member['next_generation_date'] . " <br>\n"; } if ($next_due_time <= strtotime(date('Y-m-d')) && !$next_due_time_invoice_created) { if ($debug) { echo " - Yes its time to generate an invoice!<br>\n"; } if (module_config::c('invoice_auto_renew_only_paid_invoices', 1) && $invoice_unpaid) { if ($debug) { echo " - skipping generating renewal for " . $member['owner_table'] . " " . $member['owner_id'] . " because a previous subscription is unpaid <br>\n"; } continue; } // time to generate! woo! if ($debug) { echo " - generating subscription renewal for " . $member['owner_table'] . " " . $member['owner_id'] . "<br>\n"; } $invoice_id = self::generate_subscription_invoice($subscription['subscription_id'], $member['owner_table'], $member['owner_id'], $member['next_generation_date'], $subscription['amount']); if ($debug) { echo " - generated invoice " . module_invoice::link_open($invoice_id, true) . " for subscription <br>\n"; } if ($subscription['automatic_email']) { if ($debug) { echo " - emailing invoice to " . $member['owner_table'] . "... <br>\n"; } if (module_invoice::email_invoice_to_customer($invoice_id, $debug)) { if ($debug) { echo "send successfully <br>\n"; } } else { echo " - failed to send invoice " . module_invoice::link_open($invoice_id, true) . " to " . $member['owner_table'] . " <br>\n"; } } } else { if ($debug) { echo " - skipping generating renewal for " . $member['owner_table'] . " " . $member['owner_id'] . " because the due date has already been generated <br>\n"; } } } } } }
echo module_form::generate_form_actions($form_actions); ?> </form> <?php hook_handle_callback('layout_column_half', 2); ?> <?php if ((int) $subscription_id > 0) { // show subscribed members / customers print_heading(array('type' => 'h3', 'title' => 'Subscribers')); $subscribed_owners = module_subscription::get_subscribed_owners($subscription_id); ?> <table class="tableclass tableclass_rows tableclass_full"> <thead> <tr> <th><?php _e('Name'); ?> </th> <th><?php _e('Start Date'); ?> </th> <th><?php