예제 #1
0
 public static function email_invoice_to_customer($invoice_id, $debug = false)
 {
     // this is a copy of some of the code in invoie_admin_email.php
     // used in the CRON job when sending out automated emails.
     $invoice = module_invoice::get_invoice($invoice_id);
     // template for sending emails.
     // are we sending the paid one? or the dueone.
     $template_name = '';
     $template_prefix = isset($invoice['invoice_template_email']) && strlen($invoice['invoice_template_email']) ? $invoice['invoice_template_email'] : 'invoice_email';
     if (isset($invoice['credit_note_id']) && $invoice['credit_note_id']) {
         $template_name = 'credit_note_email';
     } else {
         if ($invoice['date_paid'] && $invoice['date_paid'] != '0000-00-00') {
             $template_name = $template_prefix . '_paid';
         } else {
             if ($invoice['overdue'] && $invoice['date_sent'] && $invoice['date_sent'] != '0000-00-00') {
                 $template_name = $template_prefix . '_overdue';
             } else {
                 $template_name = $template_prefix . '_due';
             }
         }
     }
     $template_name = hook_filter_var('invoice_email_template', $template_name, $invoice_id, $invoice);
     if (class_exists('module_company', false) && isset($invoice_data['company_id']) && (int) $invoice_data['company_id'] > 0) {
         module_company::set_current_company_id($invoice_data['company_id']);
     }
     $template = module_template::get_template_by_key($template_name);
     if (!$template || $template->template_key != $template_name) {
         // backup default templates incase someone has chosen a template that doesn't exist (eg: created invoice_email_MINE_due but not invoice_email_MINE_paid )
         $template_prefix = 'invoice_email';
         if ($invoice['date_paid'] && $invoice['date_paid'] != '0000-00-00') {
             $template_name = $template_prefix . '_paid';
         } else {
             if ($invoice['overdue'] && $invoice['date_sent'] && $invoice['date_sent'] != '0000-00-00') {
                 $template_name = $template_prefix . '_overdue';
             } else {
                 $template_name = $template_prefix . '_due';
             }
         }
     }
     $replace = module_invoice::get_replace_fields($invoice_id, $invoice);
     if (defined('_BLOCK_EMAILS') && _BLOCK_EMAILS) {
         $pdf = false;
     } else {
         $pdf = module_invoice::generate_pdf($invoice_id);
     }
     $send_email_to = array();
     $to = array();
     if ($invoice['customer_id']) {
         $customer = module_customer::get_customer($invoice['customer_id']);
         $replace['customer_name'] = $customer['customer_name'];
         if ($invoice['user_id']) {
             // this invoice has a manually assigned user, only send the invoice to this user.
             // todo: should we also send to accounts? not sure - see if peopel complain
             $primary = module_user::get_user($invoice['user_id']);
             if ($primary) {
                 $send_email_to[] = $primary;
             }
         } else {
             $to = module_user::get_contacts(array('customer_id' => $invoice['customer_id']));
             // hunt for 'accounts' extra field
             $field_to_find = strtolower(module_config::c('accounts_extra_field_name', 'Accounts'));
             foreach ($to as $contact) {
                 $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $contact['user_id']));
                 foreach ($extras as $e) {
                     if (strtolower($e['extra_key']) == $field_to_find) {
                         // this is the accounts contact - woo!
                         $send_email_to[] = $contact;
                     }
                 }
             }
             if (!count($send_email_to) && $customer['primary_user_id']) {
                 $primary = module_user::get_user($customer['primary_user_id']);
                 if ($primary) {
                     $send_email_to[] = $primary;
                 }
             }
         }
     } else {
         if ($invoice['member_id']) {
             $member = module_member::get_member($invoice['member_id']);
             $to = array($member);
             $replace['customer_name'] = $member['first_name'];
         } else {
             $to = array();
         }
     }
     $template->assign_values($replace);
     $html = $template->render('html');
     // send an email to this user.
     $email = module_email::new_email();
     $email->replace_values = $replace;
     // todo: send to all customer contacts ?
     if ($send_email_to) {
         foreach ($send_email_to as $send_email_t) {
             if (!empty($send_email_t['user_id'])) {
                 $email->set_to('user', $send_email_t['user_id']);
             } else {
                 if (!empty($send_email_t['email'])) {
                     $email->set_to_manual($send_email_t['email']);
                 }
             }
         }
     } else {
         foreach ($to as $t) {
             if (!empty($t['user_id'])) {
                 $email->set_to('user', $t['user_id']);
             } else {
                 if (!empty($t['email'])) {
                     $email->set_to_manual($t['email']);
                 }
             }
             break;
             // only 1? todo: all?
         }
     }
     $email->set_bcc_manual(module_config::c('admin_email_address', ''), '');
     //$email->set_from('user',); // nfi
     $email->set_subject($template->description);
     // do we send images inline?
     $email->set_html($html);
     if ($pdf) {
         $email->add_attachment($pdf);
     }
     $email->invoice_id = $invoice_id;
     $email->customer_id = $invoice['customer_id'];
     $email->prevent_duplicates = true;
     if ($email->send($debug)) {
         // it worked successfully!!
         // record a log on the invoice when it's done.
         self::email_sent(array('invoice_id' => $invoice_id, 'template_name' => $template_name));
         return true;
     } else {
         /// log err?
         return false;
     }
 }
예제 #2
0
 public function handle_hook($hook)
 {
     switch ($hook) {
         case "invoice_paid":
             $foo = func_get_args();
             $invoice_id = (int) $foo[1];
             if ($invoice_id > 0) {
                 // see if any subscriptions match this invoice.
                 //module_cache::clear_cache();
                 $invoice = module_invoice::get_invoice($invoice_id);
                 $subscription = get_single('subscription_history', 'invoice_id', $invoice_id);
                 if ($subscription) {
                     // mark subscription as paid and move onto the next date.
                     update_insert('subscription_history_id', $subscription['subscription_history_id'], 'subscription_history', array('paid_date' => $invoice['date_paid']));
                     $subscription_owner = get_single('subscription_owner', 'subscription_owner_id', $subscription['subscription_owner_id']);
                     $this->update_next_due_date($subscription['subscription_id'], $subscription_owner['owner_table'], $subscription_owner['owner_id']);
                     /*if($subscription['customer_id']){
                           $this->update_next_due_date($subscription['subscription_id'],$subscription['customer_id'],true);
                       }else{
                           $this->update_next_due_date($subscription['subscription_id'],$subscription['member_id'],false);
                       }*/
                 }
             }
             break;
         case "home_alerts":
             $alerts = array();
             if (module_config::c('subscription_alerts', 1) && self::can_i('view', 'Subscriptions')) {
                 // find renewals due in a certain time.
                 $time = date('Y-m-d', strtotime('+' . module_config::c('alert_days_in_future', 5) . ' days'));
                 $key = _l('Subscription Due');
                 if (class_exists('module_dashboard', false)) {
                     module_dashboard::register_group($key, array('columns' => array('full_link' => _l('Name'), 'type' => _l('Type'), 'subscription_name' => _l('Subscription'), 'automatic_renew' => _l('Automatic Renew'), 'automatic_email' => _l('Automatic Email'), 'next_due_date' => _l('Next Due Date'), 'days' => _l('Day Count'))));
                 }
                 $db_fields = get_fields('subscription');
                 $sql = "SELECT s.*, so.* ";
                 if (isset($db_fields['invoice_prior_days'])) {
                     $sql .= ", DATE_SUB(so.next_due_date, INTERVAL `invoice_prior_days` DAY) AS next_generation_date ";
                 }
                 $sql .= " FROM `" . _DB_PREFIX . "subscription_owner` so ";
                 $sql .= " LEFT JOIN `" . _DB_PREFIX . "subscription` s USING (subscription_id)";
                 if (isset($db_fields['invoice_prior_days'])) {
                     $sql .= " WHERE DATE_SUB(so.next_due_date, INTERVAL `invoice_prior_days` DAY) <= '" . $time . "'";
                 } else {
                     $sql .= " WHERE so.next_due_date <= '" . $time . "'";
                 }
                 $sql .= " AND so.`deleted` = 0";
                 //                    echo $sql;
                 $items = qa($sql);
                 foreach ($items as $item) {
                     //                        echo '<hr>';print_r($item);echo '<hr>';
                     $alert_res = process_alert(isset($item['next_generation_date']) ? $item['next_generation_date'] : $item['next_due_date'], $key);
                     if ($alert_res) {
                         switch ($item['owner_table']) {
                             case 'member':
                                 $permission_check = module_member::get_member($item['owner_id']);
                                 if (!$permission_check || $permission_check['member_id'] != $item['owner_id'] || !module_member::can_i('view', 'Members')) {
                                     continue 2;
                                 }
                                 $alert_res['full_link'] = module_member::link_open($item['owner_id'], true);
                                 break;
                             case 'website':
                                 $permission_check = module_website::get_website($item['owner_id']);
                                 if (!$permission_check || $permission_check['website_id'] != $item['owner_id'] || !module_website::can_i('view', 'Websites')) {
                                     continue 2;
                                 }
                                 $alert_res['full_link'] = module_website::link_open($item['owner_id'], true);
                                 break;
                             case 'customer':
                                 $permission_check = module_customer::get_customer($item['owner_id']);
                                 if (!$permission_check || $permission_check['customer_id'] != $item['owner_id'] || !module_customer::can_i('view', 'Customers')) {
                                     continue 2;
                                 }
                                 $alert_res['full_link'] = module_customer::link_open($item['owner_id'], true);
                                 break;
                         }
                         $alert_res['name'] = $item['name'];
                         $alert_res['link'] = '#';
                         if (preg_match('@href="([^"]+)"@', $alert_res['full_link'], $link_match)) {
                             $alert_res['link'] = $link_match[1];
                         }
                         $alert_res['type'] = $item['owner_table'];
                         $alert_res['subscription_name'] = module_subscription::link_open($item['subscription_id'], true);
                         $alert_res['next_due_date'] = isset($item['next_generation_date']) ? print_date($item['next_generation_date']) : print_date($item['next_due_date']);
                         $alert_res['automatic_renew'] = $item['automatic_renew'] ? _l('Yes') : _l('No');
                         $alert_res['automatic_email'] = $item['automatic_email'] ? _l('Yes') : _l('No');
                         $alerts[] = $alert_res;
                     }
                 }
             }
             return $alerts;
             break;
     }
 }
예제 #3
0
    } else {
        $to = module_user::get_contacts(array('customer_id' => $invoice['customer_id']));
        // hunt for 'accounts' extra field
        $field_to_find = strtolower(module_config::c('accounts_extra_field_name', 'Accounts'));
        foreach ($to as $contact) {
            $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $contact['user_id']));
            foreach ($extras as $e) {
                if (strtolower($e['extra_key']) == $field_to_find) {
                    // this is the accounts contact - woo!
                    $to_select = $contact['email'];
                }
            }
        }
        if (!$to_select && $customer['primary_user_id']) {
            $primary = module_user::get_user($customer['primary_user_id']);
            if ($primary) {
                $to_select = $primary['email'];
            }
        }
    }
} else {
    if ($invoice['member_id']) {
        $member = module_member::get_member($invoice['member_id']);
        $to = array($member);
        $replace['customer_name'] = $member['first_name'];
    } else {
        $to = array();
    }
}
$template->assign_values($replace);
module_email::print_compose(array('title' => _l('Email Invoice: %s', $invoice['name']), 'find_other_templates' => 'invoice_email', 'current_template' => $template_name, 'customer_id' => $invoice['customer_id'], 'company_id' => isset($invoice['company_id']) ? $invoice['company_id'] : 0, 'to' => $to, 'to_select' => $to_select, 'bcc' => module_config::c('admin_email_address', ''), 'content' => $template->render('html'), 'subject' => $template->replace_description(), 'success_url' => module_invoice::link_open($invoice_id), 'success_callback' => 'module_invoice::email_sent', 'success_callback_args' => array('invoice_id' => $invoice_id, 'template_name' => $template_name), 'invoice_id' => $invoice_id, 'cancel_url' => module_invoice::link_open($invoice_id), 'attachments' => array(array('path' => $pdf, 'name' => basename($pdf), 'preview' => module_invoice::link_generate($invoice_id, array('arguments' => array('go' => 1, 'print' => 1), 'page' => 'invoice_admin', 'full' => false))))));
예제 #4
0
<?php

/** 
 * Copyright: dtbaker 2012
 * Licence: Please check CodeCanyon.net for licence details. 
 * More licence clarification available here:  http://codecanyon.net/wiki/support/legal-terms/licensing-terms/ 
 * Deploy: 9809 f200f46c2a19bb98d112f2d32a8de0c4
 * Envato: 4ffca17e-861e-4921-86c3-8931978c40ca
 * Package Date: 2015-11-25 02:55:20 
 * IP Address: 67.79.165.254
 */
if (isset($_REQUEST['member_id'])) {
    $links = array();
    $member_id = $_REQUEST['member_id'];
    if ($member_id && $member_id != 'new') {
        $member = module_member::get_member($member_id);
        // we have to load the menu here for the sub plugins under member
        // set default links to show in the bottom holder area.
        array_unshift($links, array("name" => _l('Member:') . ' <strong>' . htmlspecialchars($member['first_name'] . ' ' . $member['last_name']) . '</strong>', "icon" => "images/icon_arrow_down.png", 'm' => 'member', 'p' => 'member_admin', 'default_page' => 'member_admin_edit', 'order' => 1, 'menu_include_parent' => 0));
    } else {
        $member = array('name' => 'New Member');
        array_unshift($links, array("name" => "New Member Details", "icon" => "images/icon_arrow_down.png", 'm' => 'member', 'p' => 'member_admin', 'default_page' => 'member_admin_edit', 'order' => 1, 'menu_include_parent' => 0));
    }
} else {
    include 'member_admin_list.php';
}