public static function generate_subscription_invoice($subscription_id, $owner_table, $owner_id, $date, $amount) { $subscription = self::get_subscription($subscription_id); if (!$subscription || $subscription['subscription_id'] != $subscription_id) { return false; } $members_subscriptions = module_subscription::get_subscriptions_by($owner_table, $owner_id); /*if($customer_hack){ $members_subscriptions = module_subscription::get_subscriptions_by_customer($member_id); }else{ $members_subscriptions = module_subscription::get_subscriptions_by_member($member_id); }*/ // we have an ammount! create an invoice for this amount/ // assign it to a subscription (but not necessary!) if ($subscription_id && !isset($members_subscriptions[$subscription_id])) { die('Shouldnt happen'); } $history = module_subscription::get_subscription_history($subscription_id, $owner_table, $owner_id); // we grab the history of this subscription. if this is the first subscription for this member and the $date is in the past then we update the date to today. if (strtotime($date) < strtotime(date('Y-m-d'))) { $has_history = false; foreach ($history as $h) { if (!$h['invoice_id']) { } else { $invoice_data = module_invoice::get_invoice($h['invoice_id']); if ($invoice_data['date_cancel'] != '0000-00-00') { continue; } $has_history = true; break; } } if (!$has_history) { $date = date('Y-m-d'); } } // does this one have a discount/trial ? $number_of_past_invoices = 0; foreach ($history as $h) { if (!$h['invoice_id']) { } else { $invoice_data = module_invoice::get_invoice($h['invoice_id']); if ($invoice_data['date_cancel'] != '0000-00-00') { continue; } $number_of_past_invoices++; } } //if(isset($subscription['settings']) && isset($subscription['settings']['trial_period']) && $subscription['settings']['trial_period'] > 0 && $number_of_past_invoices < $subscription['settings']['trial_period']){ //echo $number_of_past_invoices;print_r($subscription['settings']);print_r($history);exit; if ($number_of_past_invoices <= 0 && isset($subscription['settings']['trial_price_adjust']) && $subscription['settings']['trial_price_adjust'] != 0) { $amount += $subscription['settings']['trial_price_adjust']; } //} //$next_time = self::_calculate_next_time(strtotime($date), $subscription); if (isset($members_subscriptions[$subscription_id]['next_due_date']) && $members_subscriptions[$subscription_id]['next_due_date'] != '0000-00-00' && $members_subscriptions[$subscription_id]['next_due_date'] != $date) { $time_period = self::_calculate_next_time(strtotime($members_subscriptions[$subscription_id]['next_due_date']), $subscription, true); } else { $time_period = self::_calculate_next_time(strtotime($date), $subscription, true); } $subscription_owner_id = $members_subscriptions[$subscription_id]['subscription_owner_id']; $amount_currency = $subscription['currency_id']; //module_config::c('subscription_currency',1); $data = array('subscription_id' => $subscription_id, 'subscription_owner_id' => $subscription_owner_id, 'amount' => $amount, 'currency_id' => $amount_currency, 'invoice_id' => 0, 'from_next_due_date' => $members_subscriptions[$subscription_id]['next_due_date']); /*if($customer_hack){ unset($data['member_id']); $data['customer_id'] = $member_id; }*/ $subscription_history_id = update_insert('subscription_history_id', 0, 'subscription_history', $data); $customer_id = 0; switch ($owner_table) { case 'website': $website_data = module_website::get_website($owner_id); $customer_id = $website_data['customer_id']; break; case 'customer': $customer_id = $owner_id; break; } module_invoice::$new_invoice_number_date = $date; // we have to seed the customer id if it exists. $_REQUEST['customer_id'] = $customer_id; $invoice_data = module_invoice::get_invoice('new', true); $invoice_data['customer_id'] = $customer_id; // customer_id, website_id, member_id $invoice_data[$owner_table . '_id'] = $owner_id; /*if($customer_hack){ $invoice_data['member_id'] = 0; $invoice_data['customer_id'] = $member_id; }else{ $invoice_data['member_id'] = $member_id; // added in version 2.31 for invoice integration. eg: emailing invoice $invoice_data['customer_id'] = 0; }*/ $invoice_data['user_id'] = 0; $invoice_data['currency_id'] = $amount_currency; $invoice_data['date_sent'] = '0000-00-00'; $invoice_data['date_cancel'] = '0000-00-00'; $invoice_data['date_create'] = $date; $invoice_data['default_task_type'] = _TASK_TYPE_AMOUNT_ONLY; // todo - option this out to the subscription settings area. $invoice_data['date_due'] = date('Y-m-d', strtotime("+" . module_config::c('subscription_invoice_due_date', 0) . " days", strtotime($date))); if (strtotime($invoice_data['date_due']) < time()) { // due date in the past? hmm, update it from today instead. $invoice_data['date_due'] = date('Y-m-d', strtotime("+" . module_config::c('subscription_invoice_due_date', 0) . " days", time())); } $invoice_data['name'] = !$invoice_data['name'] || module_config::c('subscription_invoice_numeric', 0) ? 'S' . str_pad($subscription_history_id, 6, '0', STR_PAD_LEFT) : $invoice_data['name']; // pick a tax rate for this automatic invoice. $invoice_data['total_tax_name'] = isset($subscription['settings']) && isset($subscription['settings']['tax_name']) ? $subscription['settings']['tax_name'] : ''; $invoice_data['total_tax_rate'] = isset($subscription['settings']) && isset($subscription['settings']['tax_amount']) ? $subscription['settings']['tax_amount'] : ''; $invoice_data['tax_type'] = isset($subscription['settings']) && isset($subscription['settings']['tax_type']) ? $subscription['settings']['tax_type'] : module_config::c('invoice_tax_type', 0); $invoice_data['invoice_template_email'] = isset($subscription['settings']) && isset($subscription['settings']['invoice_template_email']) ? $subscription['settings']['invoice_template_email'] : ''; $invoice_data['invoice_template_print'] = isset($subscription['settings']) && isset($subscription['settings']['invoice_template_print']) ? $subscription['settings']['invoice_template_print'] : ''; $invoice_data['invoice_invoice_item'] = array('new' => array('description' => $members_subscriptions[$subscription_id]['name'] . $time_period, 'hourly_rate' => $amount, 'completed' => 1, 'manual_task_type' => _TASK_TYPE_AMOUNT_ONLY, 'date_done' => $date)); $invoice_id = module_invoice::save_invoice('new', $invoice_data); if ($invoice_id) { // limit payment methods if this has been set in the options area: $payment_methods = handle_hook('get_payment_methods'); foreach ($payment_methods as &$payment_method) { if ($payment_method->is_enabled()) { $enabled = isset($subscription['settings']['payment_methods'][$payment_method->module_name]) && $subscription['settings']['payment_methods'][$payment_method->module_name] ? true : (isset($subscription['settings']['payment_methods']) ? false : true); if ($enabled) { $payment_method->set_allowed_for_invoice($invoice_id, 1); } else { $payment_method->set_allowed_for_invoice($invoice_id, 0); } } } update_insert('subscription_history_id', $subscription_history_id, 'subscription_history', array('invoice_id' => $invoice_id)); module_invoice::add_history($invoice_id, 'Created invoice from subscription #' . str_pad($subscription_history_id, 6, '0', STR_PAD_LEFT) . ' from ' . $owner_table . ' ID# ' . $owner_id); self::update_next_due_date($subscription_id, $owner_table, $owner_id); } else { set_error('failed to create subscription invoice'); } return $invoice_id; }
public function generate_priority_invoice($ticket_id) { // call the invoice module and create an invoice for this ticket. // once this invoice is paid it will do a callback to the ticket. $ticket_data = $this->get_ticket($ticket_id); // check if no invoice exists. if (!$ticket_data['invoice_id']) { $task_name = module_config::c('ticket_priority_invoice_task', 'Priority Support Ticket'); $task_cost = module_config::c('ticket_priority_cost', 10); $task_currency = module_config::c('ticket_priority_currency', 1); // we do this hack so that the customer can have different invoice templates for support tickets. $old_customer_id = isset($_REQUEST['customer_id']) ? $_REQUEST['customer_id'] : false; $_REQUEST['customer_id'] = $ticket_data['customer_id']; $invoice_data = module_invoice::get_invoice('new', true); $_REQUEST['customer_id'] = $old_customer_id; // todo - if the ticket customer_id changes (a feature for later on) then we have to update any of these invoices. // maybe it's best we don't have a customer_id here? hmmmmmmmmmmmmmmmmmm // the user will have to enter their own invoice details anyway. // maybe we can read the customer_id from the user table if there is no customer_id in the invoice table? that might fix some things. $invoice_data['customer_id'] = $ticket_data['customer_id']; $invoice_data['user_id'] = $ticket_data['user_id']; $invoice_data['currency_id'] = $task_currency; $invoice_data['date_sent'] = date('Y-m-d'); $invoice_data['name'] = 'T' . $this->ticket_number($ticket_id); // don't set an automatic reminder on invoices $invoice_data['overdue_email_auto'] = module_config::c('ticket_priority_auto_overdue_email', 0); // pick a tax rate for this automatic invoice. //if(module_config::c('ticket_priority_tax_name','')){ $invoice_data['total_tax_name'] = module_config::c('ticket_priority_tax_name', ''); //} //if(module_config::c('ticket_priority_tax_rate','')){ $invoice_data['total_tax_rate'] = module_config::c('ticket_priority_tax_rate', ''); //} $invoice_data['invoice_invoice_item'] = array('new' => array('description' => $task_name . ' - ' . _l('Ticket #' . $this->ticket_number($ticket_id)), 'hourly_rate' => $task_cost, 'manual_task_type' => _TASK_TYPE_AMOUNT_ONLY, 'completed' => 1)); $invoice_id = module_invoice::save_invoice('new', $invoice_data); update_insert('ticket_id', $ticket_id, 'ticket', array('invoice_id' => $invoice_id)); module_invoice::add_history($invoice_id, 'Created invoice from support ticket #' . $this->ticket_number($ticket_id)); return $invoice_id; } return $ticket_data['invoice_id']; }