foreach ($invoice_total + $invoice_total_due as $currency_id => $foo) {
        $currency = get_single('currency', 'currency_id', $currency_id);
        $footer_rows[] = array('invoice_number' => array('data' => '<strong>' . _l('%s Totals:', $currency && isset($currency['code']) ? $currency['code'] : '') . '</strong>', 'cell_colspan' => $colspan - 2, 'cell_class' => 'text-right'), 'c_invoice_total' => array('data' => '<strong>' . dollar(isset($invoice_total[$currency_id]) ? $invoice_total[$currency_id] : 0, true, $currency_id) . '</strong>'), 'c_invoice_total_due' => array('data' => '<strong>' . dollar(isset($invoice_total_due[$currency_id]) ? $invoice_total_due[$currency_id] : 0, true, $currency_id) . '</strong>'), 'row_bulk_action' => array('data' => ' ', 'cell_colspan' => $colspan2));
    }
    $table_manager->set_footer_rows($footer_rows);
}
$table_manager->pagination = true;
$table_manager->print_table();
?>

</form>

<?php 
if (function_exists('convert_html2pdf') && get_display_mode() != 'mobile') {
    ?>

    <form action="<?php 
    echo module_invoice::link_generate(false, array('arguments' => array('print' => 1)));
    ?>
" method="post">
        <input type="hidden" name="invoice_ids" value="<?php 
    echo implode(",", $all_invoice_ids);
    ?>
">
        <input type="submit" name="butt_print" value="<?php 
    echo _l('Export all results as PDF');
    ?>
" class="submit_button" />
    </form>
<?php 
}
     $replace['title'] = '<h3>' . _l('Payment History:') . '</h3>';
 } else {
     $replace['title'] = '<strong>' . _l('Payment History:') . '</strong><br/>';
 }
 if (preg_match('#<tr[^>]+data-item-row="true">.*</tr>#imsU', $t->content, $matches)) {
     $item_row_html = $matches[0];
     $t->content = str_replace($item_row_html, '{ITEM_ROW_CONTENT}', $t->content);
 } else {
     set_error('Please ensure a TR with data-item-row="true" is in the invoice_payment_history template');
     $item_row_html = '';
 }
 $all_item_row_html = '';
 $item_count = 0;
 // changed from 1
 foreach ($payment_historyies as $invoice_payment_id => $invoice_payment_data) {
     $row_replace = array('ITEM_ODD_OR_EVEN' => $item_count++ % 2 ? 'odd' : 'even', 'ITEM_PAYMENT_DATE' => !trim($invoice_payment_data['date_paid']) || $invoice_payment_data['date_paid'] == '0000-00-00' ? _l('Pending on %s', print_date($invoice_payment_data['date_created'])) : print_date($invoice_payment_data['date_paid']), 'ITEM_PAYMENT_METHOD' => htmlspecialchars($invoice_payment_data['method']), 'ITEM_PAYMENT_DETAILS' => '', 'ITEM_PAYMENT_AMOUNT' => dollar($invoice_payment_data['amount'], true, $invoice_payment_data['currency_id']), 'ITEM_PAYMENT_RECEIPT_URL' => module_invoice::link_receipt($invoice_payment_data['invoice_payment_id']));
     if (isset($invoice_payment_data['data']) && $invoice_payment_data['data']) {
         $details = unserialize($invoice_payment_data['data']);
         if (isset($details['custom_notes'])) {
             $row_replace['ITEM_PAYMENT_DETAILS'] = htmlspecialchars($details['custom_notes']);
         }
     }
     $this_item_row_html = $item_row_html;
     $this_item_row_html = str_replace(' data-item-row="true"', '', $this_item_row_html);
     foreach ($row_replace as $key => $val) {
         $this_item_row_html = str_replace('{' . strtoupper($key) . '}', $val, $this_item_row_html);
     }
     $all_item_row_html .= $this_item_row_html;
 }
 $replace['ITEM_ROW_CONTENT'] = $all_item_row_html;
 $t->assign_values($replace);
Example #3
0
        unset($blank_invoices[$id]);
    }
}
if (count($blank_invoices)) {
    ?>
	<h2>Blank invoices found</h2>
	We found the following <?php 
    echo count($blank_invoices);
    ?>
 blank invoices that were created from a recent "Subscription" bug:
	<ul>
		<?php 
    foreach ($blank_invoices as $blank_invoice) {
        ?>
		<li><?php 
        echo module_invoice::link_open($blank_invoice['invoice_id'], true);
        ?>
 created on <?php 
        echo print_date($blank_invoice['date_created']);
        ?>
</li>
		<?php 
    }
    ?>
	</ul>
	You can remove all these invoices manually, or click the button below to remove them automatically.
	<form action="" method="post">
		<input type="hidden" name="remove_duplicates" value="yes">
		<input type="submit" value="Remove these <?php 
    echo count($blank_invoices);
    ?>
Example #4
0
" />
    <input type="hidden" name="quote_id" value="<?php 
echo (int) $job['quote_id'];
?>
" />


    <?php 
$fields = array('fields' => array('name' => 'Name'));
module_form::set_required($fields);
//module_form::set_default_field('task_desc_new');
module_form::set_default_field('job_name');
module_form::prevent_exit(array('valid_exits' => array('.submit_button', '.save_task', '.delete', '.task_defaults')));
hook_handle_callback('layout_column_half', 1, '35');
/**** JOB DETAILS ****/
$fieldset_data = array('heading' => array('type' => 'h3', 'title' => 'Job Details'), 'class' => 'tableclass tableclass_form tableclass_full', 'elements' => array('name' => array('title' => 'Job Title', 'field' => array('type' => 'text', 'name' => 'name', 'value' => $job['name'])), 'type' => array('title' => 'Type', 'field' => array('type' => 'select', 'name' => 'type', 'value' => $job['type'], 'blank' => false, 'options' => module_job::get_types(), 'allow_new' => true)), 'hourly_rate' => array('title' => 'Hourly Rate', 'ignore' => !module_invoice::can_i('view', 'Invoices'), 'field' => array('type' => 'currency', 'name' => 'hourly_rate', 'value' => number_out($job['hourly_rate']))), 'status' => array('title' => 'Status', 'field' => array('type' => 'select', 'name' => 'status', 'value' => $job['status'], 'blank' => false, 'options' => module_job::get_statuses(), 'allow_new' => true)), 'date_quote' => array('title' => 'Quote Date', 'ignore' => !module_config::c('job_allow_quotes', 0), 'field' => array('type' => 'date', 'name' => 'date_quote', 'value' => print_date($job['date_quote']), 'help' => 'This is the date the Job was quoted to the Customer. Once this Job Quote is approved, the Start Date will be set below.')), 'date_start' => array('title' => 'Start Date', 'field' => array('type' => 'date', 'name' => 'date_start', 'value' => print_date($job['date_start']), 'help' => 'This is the date the Job is scheduled to start work. This can be a date in the future. If you have the Calendar, this is the date that will be used for the Calendar event.')), 'time_start' => array('title' => 'Start Time', 'ignore' => !(class_exists('module_calendar', false) && module_config::c('job_show_times', 1)), 'field' => array('type' => 'time', 'name' => 'time_start', 'value' => isset($job['time_start']) ? $job['time_start'] : '', 'help' => 'This is the time the Job is scheduled to start.  If you have the Calendar, this is the time that will be used for the Calendar event.')), 'time_end' => array('title' => 'End Time', 'ignore' => !(class_exists('module_calendar', false) && module_config::c('job_show_times', 1)), 'field' => array('type' => 'time', 'name' => 'time_end', 'value' => isset($job['time_end']) ? $job['time_end'] : '', 'help' => 'This is the time the Job is scheduled to finish.  If you have the Calendar, this is the time that will be used for the Calendar event.')), 'date_due' => array('title' => 'Due Date', 'field' => array('type' => 'date', 'name' => 'date_due', 'value' => print_date($job['date_due']))), 'date_completed' => array('title' => 'Finished Date', 'field' => array('type' => 'date', 'name' => 'date_completed', 'value' => print_date($job['date_completed']))), 'user_id' => array('title' => 'Staff Member', 'ignore' => !module_config::c('job_allow_staff_assignment', 1), 'field' => array('type' => 'select', 'options' => $staff_member_rel, 'name' => 'user_id', 'value' => $job['user_id'], 'blank' => false, 'help' => 'Assign a staff member to this job. You can also assign individual tasks to different staff members. Staff members are users who have EDIT permissions on Job Tasks.'))), 'extra_settings' => array('owner_table' => 'job', 'owner_key' => 'job_id', 'owner_id' => $job['job_id'], 'layout' => 'table_row', 'allow_new' => module_job::can_i('create', 'Jobs'), 'allow_edit' => module_job::can_i('create', 'Jobs')));
$incrementing = false;
if (!isset($job['taxes']) || !count($job['taxes'])) {
    $job['taxes'][] = array();
    // at least have 1?
}
foreach ($job['taxes'] as $tax) {
    if (isset($tax['increment']) && $tax['increment']) {
        $incrementing = true;
    }
}
ob_start();
?>

    <span class="job_tax_increment">
        <input type="checkbox" name="tax_increment_checkbox" id="tax_increment_checkbox" value="1" <?php 
Example #5
0
        <?php 
if (class_exists('module_signature', false) && module_signature::signature_enabled($job_id)) {
    ?>
            <td> <?php 
    module_signature::signature_job_task_link($job_id, $task_id);
    ?>
 </td>
        <?php 
}
?>
        <td align="center" valign="top">
            <?php 
if ($task_data['invoiced'] && $task_data['invoice_id']) {
    if (module_invoice::can_i('view', 'Invoices')) {
        //$invoice = module_invoice::get_invoice($task_data['invoice_id']);
        echo module_invoice::link_open($task_data['invoice_id'], true);
    }
    /*echo " ";
      echo '<span class="';
      if($invoice['total_amount_due']>0){
          echo 'error_text';
      }else{
          echo 'success_text';
      }
      echo '">';
      if($invoice['total_amount_due']>0){
          echo dollar($invoice['total_amount_due'],true,$job['currency_id']);
          echo ' '._l('due');
      }else{
          echo _l('All paid');
      }
 public function external_hook($hook)
 {
     switch ($hook) {
         case 'event_ipn':
             $body = @file_get_contents('php://input');
             $event_json = json_decode($body);
             ob_start();
             echo "UCM coinbase DEBUG:<br><br>JSON: <br>\n";
             print_r($event_json);
             echo "<br><br>\n";
             $success = false;
             $bits = explode(':', isset($event_json->order->custom) ? $event_json->order->custom : '');
             if (count($bits) == 4) {
                 // we have our custom bits, invoice_id, invoice_payment_id and hash
                 // check they are right
                 $invoice_id = (int) $bits[0];
                 $invoice_payment_id = (int) $bits[1];
                 $invoice_payment_subscription_id = (int) $bits[2];
                 $hash = $bits[3];
                 $correct_hash = self::get_payment_key($invoice_id, $invoice_payment_id, $invoice_payment_subscription_id, true);
                 if ($invoice_id && $invoice_payment_id && $hash == $correct_hash) {
                     // This will send receipts on succesful invoices
                     // todo - coinbase doesnt sent this callback correctly just yet
                     if ($event_json && isset($event_json->recurring_payment) && $invoice_payment_subscription_id) {
                         // status changes on a recurring payment.
                         $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id);
                         if (!$invoice_payment_subscription['date_start'] || $invoice_payment_subscription['date_start'] == '0000-00-00') {
                             // no start date yet, set the start date now.
                             if ($event_json->recurring_payment->status == 'active') {
                                 update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d')));
                             }
                         }
                         if ($event_json->recurring_payment->status == 'paused' || $event_json->recurring_payment->status == 'canceled') {
                             update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_FAILED));
                         }
                     }
                     if ($event_json && isset($event_json->order->status) && $event_json->order->status == 'completed' && isset($event_json->order->total_native) && isset($event_json->order->custom)) {
                         // crab out the custom bits so we know what to deal with.
                         $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id);
                         $currency = module_config::get_currency($invoice_payment_data['currency_id']);
                         if ($invoice_payment_subscription_id) {
                             // this API result is for a subscription payment.
                             $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id);
                             if ($invoice_payment_subscription && $invoice_payment_subscription['invoice_payment_subscription_id'] == $invoice_payment_subscription_id && $currency['code'] == $event_json->order->total_native->currency_iso) {
                                 if (!$invoice_payment_subscription['date_start'] || $invoice_payment_subscription['date_start'] == '0000-00-00') {
                                     // no start date yet, set the start date now (this should really happen in the above callback, but coinbase isn't working right now)
                                     update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d')));
                                 }
                                 // we have a subscription payment. woo!
                                 // this gets a bit tricky, we have to work out if the invoice has been generated for this subscription yet.
                                 // if this invoice hasn't been generated yet then we have to generate it.
                                 // pass this back to the invoice class so we can reuse this feature in the future.
                                 $data = module_invoice::create_new_invoice_for_subscription_payment($invoice_id, $invoice_payment_id, $invoice_payment_subscription_id);
                                 if ($data && $data['invoice_id'] && $data['invoice_payment_id']) {
                                     $next_time = time();
                                     $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['days']) . ' days', $next_time);
                                     $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['months']) . ' months', $next_time);
                                     $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['years']) . ' years', $next_time);
                                     update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('date_last_pay' => date('Y-m-d'), 'date_next' => date('Y-m-d', $next_time)));
                                     update_insert("invoice_payment_id", $data['invoice_payment_id'], "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $event_json->order->total_native->cents / 100, 'method' => self::get_payment_method_name() . ' (Subscription)', 'invoice_payment_subscription_id' => $invoice_payment_subscription_id));
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Invoice Payment Subscription Received!");
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "API IP is " . $_SERVER['REMOTE_ADDR']);
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Received BTC: " . $event_json->order->total_btc->cents / 10000000);
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Received " . $event_json->order->total_native->currency_iso . ': ' . $event_json->order->total_native->cents / 100);
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Destination Address: " . $event_json->order->receive_address);
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Currency code matches, marking invoice as paid.");
                                     self::add_payment_data($data['invoice_payment_id'], 'log', "Raw Event Data: \n" . json_encode($event_json));
                                     module_invoice::save_invoice($data['invoice_id'], array());
                                     echo "Successful Subscription Payment!";
                                 } else {
                                     send_error("Coinbase Subscription Error (failed to generate new invoice!) " . var_export($data, true));
                                 }
                             } else {
                                 send_error('Currency code missmatch on coinbase subscription payment');
                             }
                         } else {
                             // this is a normal once off payment.
                             self::add_payment_data($invoice_payment_id, 'log', "API IP is " . $_SERVER['REMOTE_ADDR']);
                             self::add_payment_data($invoice_payment_id, 'log', "Received BTC: " . $event_json->order->total_btc->cents / 10000000);
                             self::add_payment_data($invoice_payment_id, 'log', "Received " . $event_json->order->total_native->currency_iso . ': ' . $event_json->order->total_native->cents / 100);
                             self::add_payment_data($invoice_payment_id, 'log', "Destination Address: " . $event_json->order->receive_address);
                             if ($currency['code'] == $event_json->order->total_native->currency_iso) {
                                 self::add_payment_data($invoice_payment_id, 'log', "Currency code matches, marking invoice as paid.");
                                 update_insert("invoice_payment_id", $invoice_payment_id, "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $event_json->order->total_native->cents / 100));
                                 module_invoice::save_invoice($invoice_id, array());
                                 echo "Successful Payment!";
                                 $success = true;
                             } else {
                                 self::add_payment_data($invoice_payment_id, 'log', "Currency code missmatch, please check settings!");
                             }
                             self::add_payment_data($invoice_payment_id, 'log', "Raw Event Data: \n" . json_encode($event_json));
                         }
                     }
                 }
             }
             $debug = ob_get_clean();
             if (module_config::c('coinbase_payment_debug', 0)) {
                 send_error("Coinbase Debug: {$debug}");
             }
             exit;
             break;
         case 'pay_subscription':
             $invoice_id = isset($_REQUEST['invoice_id']) ? $_REQUEST['invoice_id'] : false;
             $invoice_payment_id = isset($_REQUEST['invoice_payment_id']) ? $_REQUEST['invoice_payment_id'] : false;
             $invoice_payment_subscription_id = isset($_REQUEST['invoice_payment_subscription_id']) ? $_REQUEST['invoice_payment_subscription_id'] : false;
             $coinbase_plan_id = isset($_REQUEST['coinbase_plan_id']) ? $_REQUEST['coinbase_plan_id'] : false;
             $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : false;
             if ($invoice_id && $invoice_payment_id && $coinbase_plan_id && $invoice_payment_subscription_id && $user_id && isset($_POST['coinbaseToken'])) {
                 $user_data = module_user::get_user($user_id);
                 $email = isset($_REQUEST['coinbaseEmail']) && strlen($_REQUEST['coinbaseEmail']) ? $_REQUEST['coinbaseEmail'] : $user_data['email'];
                 if (!$email || !strpos($email, '@')) {
                     die('Please ensure your user account has a valid email address before paying with coinbase');
                 }
                 $invoice_payment = get_single('invoice_payment', 'invoice_payment_id', $invoice_payment_id);
                 $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id);
                 if (!$invoice_payment || !$invoice_payment_subscription || $invoice_payment['invoice_id'] != $invoice_id || $invoice_payment['invoice_payment_subscription_id'] != $invoice_payment_subscription_id) {
                     die('Invalid invoice payment subscription id');
                 }
                 $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id);
                 $invoice_data = module_invoice::get_invoice($invoice_id);
                 if ($invoice_payment_data && $invoice_data && $invoice_id == $invoice_data['invoice_id'] && $invoice_payment_data['invoice_id'] == $invoice_data['invoice_id']) {
                     $currency = module_config::get_currency($invoice_payment_data['currency_id']);
                     $currency_code = $currency['code'];
                     $description = isset($_REQUEST['description']) ? $_REQUEST['description'] : 'N/A';
                     $template = new module_template();
                     ob_start();
                     require_once 'includes/plugin_paymethod_coinbase/coinbase-php/lib/coinbase.php';
                     $coinbase = array("secret_key" => module_config::c('payment_method_coinbase_api_key'), "publishable_key" => module_config::c('payment_method_coinbase_secret_key'));
                     coinbase::setApiKey($coinbase['secret_key']);
                     try {
                         // todo- search for existing customer based on email address???
                         // todo: check if adding new plan to existing customer work??
                         $coinbase_customer = coinbase_Customer::create(array("card" => $_POST['coinbaseToken'], "email" => $email, 'metadata' => array('user_id' => $user_id)));
                         if ($coinbase_customer && $coinbase_customer->id) {
                             //} && $coinbase_customer->subscriptions){
                             $coinbase_subscription = $coinbase_customer->subscriptions->create(array('plan' => $coinbase_plan_id));
                             if ($coinbase_subscription && $coinbase_subscription->id) {
                                 update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'), 'coinbase_customer' => $coinbase_customer->id, 'coinbase_subscription' => $coinbase_subscription->id));
                                 module_paymethod_coinbase::add_payment_data($invoice_payment_id, 'log', "Started coinbase Subscription: " . var_export(array('customer.id' => $coinbase_customer->id, 'plan.id' => $coinbase_plan_id, 'subscription.id' => $coinbase_subscription->id), true));
                                 // success!
                                 // redirect to receipt page.
                                 redirect_browser(module_invoice::link_public_payment_complete($invoice_id));
                             } else {
                                 echo 'Failed to create subscription with coinbase';
                             }
                         }
                         $error = "Something went wrong during coinbase payment. Please confirm invoice payment went through: " . htmlspecialchars($description);
                         send_error($error);
                         echo $error;
                     } catch (coinbase_CardError $e) {
                         // The card has been declined
                         $body = $e->getJsonBody();
                         $err = $body['error'];
                         $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n";
                         $error .= $err['message'];
                         echo $error;
                         $error .= "\n\n\n" . var_export($err, true);
                         send_error($error);
                     } catch (Exception $e) {
                         $body = $e->getJsonBody();
                         $err = $body['error'];
                         $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n";
                         $error .= $err['message'];
                         echo $error;
                         $error .= "\n\n\n" . var_export($err, true);
                         send_error($error);
                     }
                     $template->content = ob_get_clean();
                     echo $template->render('pretty_html');
                     exit;
                 }
             }
             echo 'Error paying via coinbase';
             exit;
     }
 }
        $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))))));
Example #8
0
 public function run_cron($debug = false)
 {
     // check for payments.
     $sql = "SELECT * FROM `" . _DB_PREFIX . "invoice_payment` ip WHERE 1 ";
     $sql .= " AND  `method` = 'paynl' ";
     $sql .= " AND  `date_paid` = '0000-00-00' ";
     $sql .= " AND  `other_id` != '' ";
     foreach (qa($sql) as $payment) {
         // check api status:
         $strUrl = 'https://*****:*****@rest-api.pay.nl/v5/Transaction/info/json?';
         $arrArguments = array();
         $arrArguments['transactionId'] = $payment['other_id'];
         # Prepare and call API URL
         $strUrl .= http_build_query($arrArguments);
         if ($debug) {
             echo "Checking URL {$strUrl} <br>\n";
             $jsonResult = file_get_contents($strUrl);
         } else {
             $jsonResult = @file_get_contents($strUrl);
         }
         $json = @json_decode($jsonResult, true);
         if ($debug) {
             echo "Got result: <br>\n";
             print_r($json);
         }
         if ($json && isset($json['paymentDetails']) && isset($json['paymentDetails']['stateName']) && isset($json['paymentDetails']['amount'])) {
             module_paymethod_paynl::add_payment_data($payment['invoice_payment_id'], 'log', "PayNL Status " . $json['paymentDetails']['stateName'] . ": \n " . var_export($json, true));
             switch ($json['paymentDetails']['stateName']) {
                 case 'PENDING':
                     // defauly, still waiting for payment.
                     break;
                 case 'PAID':
                     update_insert("invoice_payment_id", $payment['invoice_payment_id'], "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $json['paymentDetails']['amount'] / 100, 'other_id' => ''));
                     module_invoice::save_invoice($payment['invoice_id'], array());
                     break;
                 case 'CANCEL':
                     update_insert("invoice_payment_id", $payment['invoice_payment_id'], "invoice_payment", array('other_id' => ''));
                     module_invoice::save_invoice($payment['invoice_id'], array());
                     send_error('PayNL payment cancelled for invoice: ' . module_invoice::link_open($payment['invoice_id'], true));
                     break;
             }
         } else {
             module_paymethod_paynl::add_payment_data($payment['invoice_payment_id'], 'log', "PayNL Status ERROR: \n " . $jsonResult);
         }
     }
 }
					<?php 
}
?>

					<?php 
if ((int) $invoice_id && module_invoice::can_i('delete', 'Invoices')) {
    ?>

					<input type="submit" name="butt_del" id="butt_del" value="<?php 
    echo _l('Delete');
    ?>
" class="submit_button delete_button" />
					<?php 
}
?>

					<input type="button" name="cancel" value="<?php 
echo _l('Cancel');
?>
" onclick="window.location.href='<?php 
echo module_invoice::link_open(false);
?>
';" class="submit_button" />
				</td>
			</tr>
		</tbody>
	</table>


</form>
    _e('Total Received');
    ?>
</th>
            <th><?php 
    _e('Total Unpaid');
    ?>
</th>
        </tr>
        </thead>
        <?php 
    foreach ($subscribed_owners as $subscribed_customer) {
        $history = module_subscription::get_subscription_history($subscription_id, $subscribed_customer['owner_table'], $subscribed_customer['owner_id']);
        $total_paid = $total_unpaid = array();
        foreach ($history as $h_id => $h) {
            if ($h['invoice_id']) {
                $invoice = module_invoice::get_invoice($h['invoice_id'], true);
                if ($invoice['date_cancel'] && $invoice['date_cancel'] != '0000-00-00') {
                    // invoice cancelled, ignore from listing
                    unset($history[$h_id]);
                    continue;
                }
                if ($h['paid_date'] && $h['paid_date'] != '0000-00-00') {
                    if (!isset($total_paid[$invoice['currency_id']])) {
                        $total_paid[$invoice['currency_id']] = 0;
                    }
                    $total_paid[$invoice['currency_id']] += $h['amount'];
                }
            }
            if (!$h['paid_date'] || $h['paid_date'] == '0000-00-00') {
                if (!isset($total_unpaid[$subscription['currency_id']])) {
                    $total_unpaid[$subscription['currency_id']] = 0;
            function customer_admin_email_generate_invoice_list($invoices, $customer_id)
            {
                ob_start();
                $colspan = 9;
                $colspan2 = 0;
                $invoice_total = array();
                $invoice_total_due = array();
                foreach ($invoices as $invoice) {
                    if (!isset($invoice_total[$invoice['currency_id']])) {
                        $invoice_total[$invoice['currency_id']] = 0;
                    }
                    if ($invoice['c_total_amount'] == 0) {
                        $invoice = module_invoice::get_invoice($invoice['invoice_id']);
                    }
                    $invoice_total[$invoice['currency_id']] += $invoice['c_total_amount'];
                    if (!isset($invoice_total_due[$invoice['currency_id']])) {
                        $invoice_total_due[$invoice['currency_id']] = 0;
                    }
                    $invoice_total_due[$invoice['currency_id']] += $invoice['c_total_amount_due'];
                }
                $table_manager = module_theme::new_table_manager();
                $columns = array();
                $columns['invoice_number'] = array('title' => 'Invoice Number', 'callback' => function ($invoice) {
                    //echo module_invoice::link_open($invoice['invoice_id'],true,$invoice);
                    echo '<a href="' . module_invoice::link_public($invoice['invoice_id']) . '">' . htmlspecialchars($invoice['name']) . '</a>';
                }, 'cell_class' => 'row_action');
                $columns['invoice_status'] = array('title' => 'Status', 'callback' => function ($invoice) {
                    echo htmlspecialchars($invoice['status']);
                });
                $columns['invoice_create_date'] = array('title' => 'Create Date', 'callback' => function ($invoice) {
                    if (!$invoice['date_create'] || $invoice['date_create'] == '0000-00-00') {
                        //echo print_date($invoice['date_created']);
                    } else {
                        echo print_date($invoice['date_create']);
                    }
                });
                $columns['invoice_due_date'] = array('title' => 'Due Date', 'callback' => function ($invoice) {
                    if ((!$invoice['date_paid'] || $invoice['date_paid'] == '0000-00-00') && strtotime($invoice['date_due']) < time()) {
                        echo '<span class="error_text">';
                        echo print_date($invoice['date_due']);
                        echo '</span>';
                    } else {
                        echo print_date($invoice['date_due']);
                    }
                });
                $columns['invoice_sent_date'] = array('title' => 'Sent Date', 'callback' => function ($invoice) {
                    if ($invoice['date_sent'] && $invoice['date_sent'] != '0000-00-00') {
                        ?>

				            <?php 
                        echo print_date($invoice['date_sent']);
                        ?>

				        <?php 
                    } else {
                        ?>

				            <span class="error_text"><?php 
                        _e('Not sent');
                        ?>
</span>
				        <?php 
                    }
                });
                $columns['invoice_paid_date'] = array('title' => 'Paid Date', 'callback' => function ($invoice) {
                    if ($invoice['date_paid'] && $invoice['date_paid'] != '0000-00-00') {
                        ?>

				            <?php 
                        echo print_date($invoice['date_paid']);
                        ?>

				        <?php 
                    } else {
                        if ($invoice['date_cancel'] && $invoice['date_cancel'] != '0000-00-00') {
                            ?>

				            <span class="error_text"><?php 
                            _e('Cancelled');
                            ?>
</span>
				        <?php 
                        } else {
                            if ($invoice['overdue']) {
                                ?>

				            <span class="error_text" style="font-weight: bold; text-decoration: underline;"><?php 
                                _e('Overdue');
                                ?>
</span>
				        <?php 
                            } else {
                                ?>

				            <span class="error_text"><?php 
                                _e('Not paid');
                                ?>
</span>
				        <?php 
                            }
                        }
                    }
                });
                if (class_exists('module_website', false) && module_website::is_plugin_enabled() && module_website::can_i('view', module_config::c('project_name_plural', 'Websites'))) {
                    $colspan++;
                    $columns['invoice_website'] = array('title' => module_config::c('project_name_single', 'Website'), 'callback' => function ($invoice) {
                        if (isset($invoice['website_ids'])) {
                            foreach ($invoice['website_ids'] as $website_id) {
                                if ((int) $website_id > 0) {
                                    echo module_website::link_open($website_id, true);
                                    echo '<br/>';
                                }
                            }
                        }
                    });
                }
                $columns['invoice_job'] = array('title' => 'Job', 'callback' => function ($invoice) {
                    foreach ($invoice['job_ids'] as $job_id) {
                        if ((int) $job_id > 0) {
                            //echo module_job::link_open($job_id,true);
                            $job_data = module_job::get_job($job_id);
                            echo '<a href="' . module_job::link_public($job_id) . '">' . htmlspecialchars($job_data['name']) . '</a>';
                            if ($job_data['date_start'] && $job_data['date_start'] != '0000-00-00' && $job_data['date_renew'] && $job_data['date_renew'] != '0000-00-00') {
                                _e(' (%s to %s)', print_date($job_data['date_start']), print_date(strtotime("-1 day", strtotime($job_data['date_renew']))));
                            }
                            echo "<br/>\n";
                        }
                    }
                    hook_handle_callback('invoice_admin_list_job', $invoice['invoice_id']);
                });
                if (!isset($_REQUEST['customer_id']) && module_customer::can_i('view', 'Customers')) {
                    $colspan++;
                    $columns['invoice_customer'] = array('title' => 'Customer', 'callback' => function ($invoice) {
                        echo module_customer::link_open($invoice['customer_id'], true);
                    });
                }
                $columns['c_invoice_total'] = array('title' => 'Invoice Total', 'callback' => function ($invoice) {
                    echo dollar($invoice['total_amount'], true, $invoice['currency_id']);
                });
                $columns['c_invoice_total_due'] = array('title' => 'Amount Due', 'callback' => function ($invoice) {
                    echo dollar($invoice['total_amount_due'], true, $invoice['currency_id']);
                    ?>

				        <?php 
                    if ($invoice['total_amount_credit'] > 0) {
                        ?>

				        <span class="success_text"><?php 
                        echo _l('Credit: %s', dollar($invoice['total_amount_credit'], true, $invoice['currency_id']));
                        ?>
</span>
				            <?php 
                    }
                });
                if (class_exists('module_extra', false)) {
                    ob_start();
                    $colspan2 += module_extra::print_table_header('invoice');
                    // used in the footer calc.
                    ob_end_clean();
                    $table_manager->display_extra('invoice', function ($invoice) {
                        module_extra::print_table_data('invoice', $invoice['invoice_id']);
                    });
                }
                $table_manager->set_columns($columns);
                $table_manager->row_callback = function ($row_data) {
                    // load the full vendor data before displaying each row so we have access to more details
                    if (isset($row_data['invoice_id']) && (int) $row_data['invoice_id'] > 0) {
                        return module_invoice::get_invoice($row_data['invoice_id']);
                    }
                    return array();
                };
                $table_manager->set_rows($invoices);
                if (module_config::c('invoice_list_show_totals', 1)) {
                    $footer_rows = array();
                    foreach ($invoice_total + $invoice_total_due as $currency_id => $foo) {
                        $currency = get_single('currency', 'currency_id', $currency_id);
                        $footer_rows[] = array('invoice_number' => array('data' => '<strong>' . _l('%s Totals:', $currency && isset($currency['code']) ? $currency['code'] : '') . '</strong>', 'cell_colspan' => $colspan - 2, 'cell_class' => 'text-right'), 'c_invoice_total' => array('data' => '<strong>' . dollar(isset($invoice_total[$currency_id]) ? $invoice_total[$currency_id] : 0, true, $currency_id) . '</strong>'), 'c_invoice_total_due' => array('data' => '<strong>' . dollar(isset($invoice_total_due[$currency_id]) ? $invoice_total_due[$currency_id] : 0, true, $currency_id) . '</strong>'), 'row_bulk_action' => array('data' => ' ', 'cell_colspan' => $colspan2));
                    }
                    $table_manager->set_footer_rows($footer_rows);
                }
                $table_manager->pagination = false;
                $table_manager->print_table();
                return ob_get_clean();
            }
Example #12
0
 public static function google_redirect($description, $amount, $user_id, $invoice_payment_id, $invoice_id, $currency_id)
 {
     chdir(dirname(__FILE__));
     //'includes/plugin_paymethod_google/');
     require_once 'library/googlecart.php';
     require_once 'library/googleitem.php';
     $server_type = self::is_sandbox() ? "sandbox" : '';
     $currency = module_config::get_currency($currency_id);
     self::add_payment_data($invoice_payment_id, 'log', 'Starting payment of ' . $server_type . ' in currency ' . $currency['code']);
     $cart = new GoogleCart(self::get_merchant_id(), self::get_merchant_key(), $server_type, $currency['code']);
     $total_count = 1;
     //  Check this URL for more info about the two types of digital Delivery
     //  http://code.google.com/apis/checkout/developer/Google_Checkout_Digital_Delivery.html
     //  Key/URL delivery
     self::add_payment_data($invoice_payment_id, 'log', 'Adding ' . $total_count . 'x ' . $description . ' (' . $amount . ' ' . $currency['code'] . ')');
     $item_1 = new GoogleItem($description, "", $total_count, $amount);
     // Unit price
     //$item_1->SetURLDigitalContent(module_invoice::link_receipt($invoice_payment_id),
     $item_1->SetURLDigitalContent(module_invoice::link_public_print($invoice_id), '', _l("Payment Receipt"));
     $cart->AddItem($item_1);
     $private_data = new MerchantPrivateData(array('invoice_id' => $invoice_id, 'amount' => $amount, 'currency_id' => $currency_id, 'invoice_payment_id' => $invoice_payment_id));
     $cart->SetMerchantPrivateData($private_data);
     // Specify <edit-cart-url>
     $cart->SetEditCartUrl(module_invoice::link_public($invoice_id));
     // Specify "Return to xyz" link
     $cart->SetContinueShoppingUrl(module_invoice::link_public($invoice_id));
     // Request buyer's phone number
     //$cart->SetRequestBuyerPhone(true);
     // This will do a server-2-server cart post and send an HTTP 302 redirect status
     // This is the best way to do it if implementing digital delivery
     // More info http://code.google.com/apis/checkout/developer/index.html#alternate_technique
     list($status, $error) = $cart->CheckoutServer2Server();
     // if i reach this point, something was wrong
     echo "An error had ocurred: <br />HTTP Status: " . $status . ":";
     echo "<br />Error message:<br />";
     echo $error;
     exit;
 }
Example #13
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 (module_invoice::can_i('view', 'Invoices')) {
    $rows = array();
    if ($job['staff_total_amount'] > 0 && !module_job::can_i('view', 'Job Split Pricing')) {
        $rows[] = array('label' => _l('Total:'), 'value' => '<span class="currency" style="text-decoration: underline; font-weight: bold;">' . dollar($job['staff_total_amount'], true, $job['currency_id']) . '</span>');
    } else {
        // we hide job tax if there is none
        $hide_tax = true;
        foreach ($job['taxes'] as $job_tax) {
            if (isset($job_tax['percent']) && $job_tax['percent'] > 0) {
                $hide_tax = false;
                break;
            }
        }
        if ($job['total_sub_amount_unbillable']) {
            $rows[] = array('label' => _l('Sub Total:'), 'value' => '<span class="currency">' . dollar($job['total_sub_amount'] + $job['total_sub_amount_unbillable'] + $job['discount_amount'], true, $job['currency_id']) . '</span>');
            $rows[] = array('label' => _l('Unbillable:'), 'value' => '<span class="currency">' . dollar($job['total_sub_amount_unbillable'], true, $job['currency_id']) . '</span>');
        }
        if ($job['discount_type'] == _DISCOUNT_TYPE_BEFORE_TAX) {
            $rows[] = array('label' => _l('Sub Total:'), 'value' => '<span class="currency">' . dollar($job['total_sub_amount'] + $job['discount_amount'], true, $job['currency_id']) . '</span>');
            if ($job['discount_amount'] > 0) {
Example #14
0
 public function external_hook($hook)
 {
     switch ($hook) {
         case 'public_signup_form':
             $signup_form = module_template::get_template_by_key('customer_signup_form_wrapper');
             $signup_form->page_title = $signup_form->description;
             $signup_form->assign_values(array('signup_form' => self::get_customer_signup_form_html()));
             echo $signup_form->render('pretty_html');
             exit;
         case 'public_signup':
             // sign out if testing.
             if (module_security::is_logged_in()) {
                 set_message('Logged out due to signup');
                 module_security::logout();
             }
             $result = array('messages' => array());
             function customer_signup_complete($result)
             {
                 if (isset($_REQUEST['via_ajax'])) {
                     echo json_encode($result);
                 } else {
                     echo implode('<br/>', $result['messages']);
                 }
                 exit;
             }
             if (!module_config::c('customer_signup_allowed', 0)) {
                 $result['error'] = 1;
                 $result['messages'][] = 'Customer signup disabled';
                 customer_signup_complete($result);
             }
             //recaptcha on signup form.
             if (module_config::c('captcha_on_signup_form', 0)) {
                 if (!module_captcha::check_captcha_form()) {
                     $result['error'] = 1;
                     $result['messages'][] = 'Captcha fail, please go back and enter correct captcha code.';
                     customer_signup_complete($result);
                 }
             }
             $customer = isset($_POST['customer']) && is_array($_POST['customer']) ? $_POST['customer'] : array();
             $contact = isset($_POST['contact']) && is_array($_POST['contact']) ? $_POST['contact'] : array();
             $contact_extra = isset($contact['extra']) && is_array($contact['extra']) ? $contact['extra'] : array();
             $contact_group = isset($contact['group_ids']) && is_array($contact['group_ids']) ? $contact['group_ids'] : array();
             $customer_extra = isset($customer['extra']) ? $customer['extra'] : array();
             $customer_group = isset($customer['group_ids']) && is_array($customer['group_ids']) ? $customer['group_ids'] : array();
             $address = isset($_POST['address']) ? $_POST['address'] : array();
             $website = isset($_POST['website']) ? $_POST['website'] : array();
             $website_extra = isset($website['extra']) ? $website['extra'] : array();
             $website_group = isset($website['group_ids']) && is_array($website['group_ids']) ? $website['group_ids'] : array();
             $job = isset($_POST['job']) ? $_POST['job'] : array();
             $job_extra = isset($job['extra']) ? $job['extra'] : array();
             $subscription = isset($_POST['subscription']) ? $_POST['subscription'] : array();
             // sanatise possibly problematic fields:
             // customer:
             $allowed = array('name', 'last_name', 'customer_name', 'email', 'phone', 'mobile', 'extra', 'type');
             foreach ($customer as $key => $val) {
                 if (!in_array($key, $allowed)) {
                     unset($customer[$key]);
                 }
             }
             if (isset($customer['type']) && $customer['type'] != _CUSTOMER_TYPE_NORMAL && $customer['type'] != _CUSTOMER_TYPE_LEAD) {
                 unset($customer['type']);
             }
             // added multiple contact support in the form of arrays.
             $contact_fields = array('name', 'last_name', 'email', 'phone');
             if (module_config::c('customer_signup_password', 0)) {
                 $contact_fields[] = 'password';
             }
             foreach ($contact_fields as $multi_value) {
                 if (isset($contact[$multi_value])) {
                     if (!is_array($contact[$multi_value])) {
                         $contact[$multi_value] = array($contact[$multi_value]);
                     }
                 } else {
                     if (isset($customer[$multi_value])) {
                         $contact[$multi_value] = array($customer[$multi_value]);
                     } else {
                         $contact[$multi_value] = array();
                     }
                 }
             }
             $valid_contact_email = false;
             $name_fallback = false;
             $primary_email = false;
             foreach ($contact['email'] as $contact_key => $email) {
                 if (!$name_fallback && isset($contact['name'][$contact_key])) {
                     $name_fallback = $contact['name'][$contact_key];
                 }
                 $contact['email'][$contact_key] = filter_var(strtolower(trim($email)), FILTER_VALIDATE_EMAIL);
                 if ($contact['email'][$contact_key]) {
                     $valid_contact_email = true;
                     if (!$primary_email) {
                         $primary_email = $contact['email'][$contact_key];
                         // set the primary contact details here by adding them to the master customer array
                         foreach ($contact_fields as $primary_contact_field) {
                             $customer[$primary_contact_field] = isset($contact[$primary_contact_field][$contact_key]) ? $contact[$primary_contact_field][$contact_key] : '';
                             unset($contact[$primary_contact_field][$contact_key]);
                         }
                     }
                 }
             }
             // start error checking / required fields
             if (!isset($customer['customer_name']) || !strlen($customer['customer_name'])) {
                 $customer['customer_name'] = $name_fallback;
             }
             if (!strlen($customer['customer_name'])) {
                 $result['error'] = 1;
                 $result['messages'][] = "Failed, please go back and provide a customer name.";
             }
             if (!$valid_contact_email || !$primary_email) {
                 $result['error'] = 1;
                 $result['messages'][] = "Failed, please go back and provide an email address.";
             }
             // check all posted required fields.
             function check_required($postdata, $messages = array())
             {
                 if (is_array($postdata)) {
                     foreach ($postdata as $key => $val) {
                         if (strpos($key, '_required') && strlen($val)) {
                             $required_key = str_replace('_required', '', $key);
                             if (!isset($postdata[$required_key]) || !$postdata[$required_key]) {
                                 $messages[] = 'Required field missing: ' . htmlspecialchars($val);
                             }
                         }
                         if (is_array($val)) {
                             $messages = check_required($val, $messages);
                         }
                     }
                 }
                 return $messages;
             }
             $messages = check_required($_POST);
             if (count($messages)) {
                 $result['error'] = 1;
                 $result['messages'] = array_merge($result['messages'], $messages);
             }
             if (isset($result['error'])) {
                 customer_signup_complete($result);
             }
             // end error checking / required fields.
             // check if this customer already exists in the system, based on email address
             $customer_id = false;
             $creating_new = true;
             $_REQUEST['user_id'] = 0;
             if (isset($customer['email']) && strlen($customer['email']) && !module_config::c('customer_signup_always_new', 0)) {
                 $users = module_user::get_contacts(array('email' => $customer['email']));
                 foreach ($users as $user) {
                     if (isset($user['customer_id']) && (int) $user['customer_id'] > 0) {
                         // this user exists as a customer! yey!
                         // add them to this listing.
                         $customer_id = $user['customer_id'];
                         $creating_new = false;
                         $_REQUEST['user_id'] = $user['user_id'];
                         // dont let signups update existing passwords.
                         if (isset($customer['password'])) {
                             unset($customer['password']);
                         }
                         if (isset($customer['new_password'])) {
                             unset($customer['new_password']);
                         }
                     }
                 }
             }
             $_REQUEST['extra_customer_field'] = array();
             $_REQUEST['extra_user_field'] = array();
             module_extra::$config['allow_new_keys'] = false;
             module_extra::$config['delete_existing_empties'] = false;
             // save customer extra fields.
             if (count($customer_extra)) {
                 // format the address so "save_customer" handles the save for us
                 foreach ($customer_extra as $key => $val) {
                     $_REQUEST['extra_customer_field'][] = array('key' => $key, 'val' => $val);
                 }
             }
             // save customer and customer contact details:
             $customer_id = $this->save_customer($customer_id, $customer);
             if (!$customer_id) {
                 $result['error'] = 1;
                 $result['messages'][] = 'System error: failed to create customer.';
                 customer_signup_complete($result);
             }
             $customer_data = module_customer::get_customer($customer_id);
             // todo - merge primary and secondary contact/extra/group saving into a single loop
             if (!$customer_data['primary_user_id']) {
                 $result['error'] = 1;
                 $result['messages'][] = 'System error: Failed to create customer contact.';
                 customer_signup_complete($result);
             } else {
                 $role_id = module_config::c('customer_signup_role', 0);
                 if ($role_id > 0) {
                     module_user::add_user_to_role($customer_data['primary_user_id'], $role_id);
                 }
                 // save contact extra data (repeated below for additional contacts)
                 if (isset($contact_extra[0]) && count($contact_extra[0])) {
                     $_REQUEST['extra_user_field'] = array();
                     foreach ($contact_extra[0] as $key => $val) {
                         $_REQUEST['extra_user_field'][] = array('key' => $key, 'val' => $val);
                     }
                     module_extra::save_extras('user', 'user_id', $customer_data['primary_user_id']);
                 }
                 // save contact groups
                 if (isset($contact_group[0]) && count($contact_group[0])) {
                     foreach ($contact_group[0] as $group_id => $tf) {
                         if ($tf) {
                             module_group::add_to_group($group_id, $customer_data['primary_user_id'], 'user');
                         }
                     }
                 }
             }
             foreach ($contact['email'] as $contact_key => $email) {
                 // add any additional contacts to the customer.
                 $users = module_user::get_contacts(array('email' => $email, 'customer_id' => $customer_id));
                 if (count($users)) {
                     // this contact already exists for this customer, dont update/change it.
                     continue;
                 }
                 $new_contact = array('customer_id' => $customer_id);
                 foreach ($contact_fields as $primary_contact_field) {
                     $new_contact[$primary_contact_field] = isset($contact[$primary_contact_field][$contact_key]) ? $contact[$primary_contact_field][$contact_key] : '';
                 }
                 // dont let additional contacts have passwords.
                 if (isset($new_contact['password'])) {
                     unset($new_contact['password']);
                 }
                 if (isset($new_contact['new_password'])) {
                     unset($new_contact['new_password']);
                 }
                 global $plugins;
                 $contact_user_id = $plugins['user']->create_user($new_contact, 'signup');
                 if ($contact_user_id) {
                     $role_id = module_config::c('customer_signup_role', 0);
                     if ($role_id > 0) {
                         module_user::add_user_to_role($contact_user_id, $role_id);
                     }
                     // save contact extra data  (repeated below for primary contacts)
                     if (isset($contact_extra[$contact_key]) && count($contact_extra[$contact_key])) {
                         $_REQUEST['extra_user_field'] = array();
                         foreach ($contact_extra[$contact_key] as $key => $val) {
                             $_REQUEST['extra_user_field'][] = array('key' => $key, 'val' => $val);
                         }
                         module_extra::save_extras('user', 'user_id', $contact_user_id);
                     }
                     // save contact groups
                     if (isset($contact_group[$contact_key]) && count($contact_group[$contact_key])) {
                         foreach ($contact_group[$contact_key] as $group_id => $tf) {
                             if ($tf) {
                                 module_group::add_to_group($group_id, $contact_user_id, 'user');
                             }
                         }
                     }
                 }
             }
             if (count($customer_group)) {
                 // format the address so "save_customer" handles the save for us
                 foreach ($customer_group as $group_id => $tf) {
                     if ($tf) {
                         module_group::add_to_group($group_id, $customer_id, 'customer');
                     }
                 }
             }
             $note_keys = array('customer', 'website', 'job', 'address', 'subscription');
             $note_text = _l('Customer signed up from Signup Form:');
             $note_text .= "\n\n";
             foreach ($note_keys as $note_key) {
                 $note_text .= "\n" . ucwords(_l($note_key)) . "\n";
                 if (isset($_POST[$note_key]) && is_array($_POST[$note_key])) {
                     foreach ($_POST[$note_key] as $post_key => $post_val) {
                         $note_text .= "\n - " . _l($post_key) . ": ";
                         if (is_array($post_val)) {
                             foreach ($post_val as $p => $v) {
                                 $note_text .= "\n  - - " . _l($p) . ': ' . $v;
                             }
                         } else {
                             $note_text .= $post_val;
                         }
                     }
                 }
             }
             $note_data = array('note_id' => false, 'owner_id' => $customer_id, 'owner_table' => 'customer', 'note_time' => time(), 'note' => $note_text, 'rel_data' => module_customer::link_open($customer_id), 'reminder' => 0, 'user_id' => 0);
             update_insert('note_id', false, 'note', $note_data);
             // save customer address fields.
             if (count($address)) {
                 $address_db = module_address::get_address($customer_id, 'customer', 'physical');
                 $address_id = $address_db && isset($address_db['address_id']) ? (int) $address_db['address_id'] : false;
                 $address['owner_id'] = $customer_id;
                 $address['owner_table'] = 'customer';
                 $address['address_type'] = 'physical';
                 // we have post data to save, write it to the table!!
                 module_address::save_address($address_id, $address);
             }
             // website:
             $allowed = array('url', 'name', 'extra', 'notes');
             foreach ($website as $key => $val) {
                 if (!in_array($key, $allowed)) {
                     unset($website[$key]);
                 }
             }
             $website['url'] = isset($website['url']) ? strtolower(trim($website['url'])) : '';
             $website_id = 0;
             if (count($website) && class_exists('module_website', false) && module_website::is_plugin_enabled()) {
                 if (strlen($website['url'])) {
                     // see if website already exists, don't create or update existing one for now.
                     $existing_websites = module_website::get_websites(array('customer_id' => $customer_id, 'url' => $website['url']));
                     foreach ($existing_websites as $existing_website) {
                         $website_id = $existing_website['website_id'];
                     }
                 }
                 //   echo $website_id;echo $website['url']; print_r($website_extra);exit;
                 if (!$website_id) {
                     $website_data = module_website::get_website($website_id);
                     $website_data['url'] = isset($website['url']) ? $website['url'] : 'N/A';
                     $website_data['name'] = isset($website['url']) ? $website['url'] : 'N/A';
                     $website_data['customer_id'] = $customer_id;
                     $website_id = update_insert('website_id', false, 'website', $website_data);
                     // save website extra data.
                     if ($website_id && count($website_extra)) {
                         $_REQUEST['extra_website_field'] = array();
                         foreach ($website_extra as $key => $val) {
                             $_REQUEST['extra_website_field'][] = array('key' => $key, 'val' => $val);
                         }
                         module_extra::save_extras('website', 'website_id', $website_id);
                     }
                     if ($website_id && isset($website['notes']) && strlen($website['notes'])) {
                         // add notes to this website.
                         $note_data = array('note_id' => false, 'owner_id' => $website_id, 'owner_table' => 'website', 'note_time' => time(), 'note' => $website['notes'], 'rel_data' => module_website::link_open($website_id), 'reminder' => 0, 'user_id' => $customer_data['primary_user_id']);
                         $note_id = update_insert('note_id', false, 'note', $note_data);
                     }
                 }
                 if ($website_id) {
                     if (count($website_group)) {
                         // format the address so "save_customer" handles the save for us
                         foreach ($website_group as $group_id => $tf) {
                             if ($tf) {
                                 module_group::add_to_group($group_id, $website_id, 'website');
                             }
                         }
                     }
                 }
             }
             // generate jobs for this customer.
             $job_created = array();
             if ($job && isset($job['type']) && is_array($job['type'])) {
                 if (module_config::c('customer_signup_any_job_type', 0)) {
                     foreach ($job['type'] as $type_name) {
                         // we have a match in our system. create the job.
                         $job_data = module_job::get_job(false);
                         $job_data['type'] = $type_name;
                         if (!$job_data['name']) {
                             $job_data['name'] = $type_name;
                         }
                         $job_data['website_id'] = $website_id;
                         $job_data['customer_id'] = $customer_id;
                         $job_id = update_insert('job_id', false, 'job', $job_data);
                         // todo: add default tasks for this job type.
                         $job_created[] = $job_id;
                     }
                 } else {
                     foreach (module_job::get_types() as $type_id => $type) {
                         foreach ($job['type'] as $type_name) {
                             if ($type_name == $type) {
                                 // we have a match in our system. create the job.
                                 $job_data = module_job::get_job(false);
                                 $job_data['type'] = $type;
                                 if (!$job_data['name']) {
                                     $job_data['name'] = $type;
                                 }
                                 $job_data['website_id'] = $website_id;
                                 $job_data['customer_id'] = $customer_id;
                                 $job_id = update_insert('job_id', false, 'job', $job_data);
                                 // todo: add default tasks for this job type.
                                 $job_created[] = $job_id;
                             }
                         }
                     }
                 }
                 if (count($job_created) && count($job_extra)) {
                     // save job extra data.
                     foreach ($job_created as $job_created_id) {
                         if ($job_created_id && count($job_extra)) {
                             $_REQUEST['extra_job_field'] = array();
                             foreach ($job_extra as $key => $val) {
                                 $_REQUEST['extra_job_field'][] = array('key' => $key, 'val' => $val);
                             }
                             module_extra::save_extras('job', 'job_id', $job_created_id);
                         }
                     }
                 }
             }
             // save files against customer
             $uploaded_files = array();
             if (isset($_FILES['customerfiles']) && isset($_FILES['customerfiles']['tmp_name'])) {
                 foreach ($_FILES['customerfiles']['tmp_name'] as $file_id => $tmp_file) {
                     if (is_uploaded_file($tmp_file)) {
                         // save to file module for this customer
                         $file_name = basename($_FILES['customerfiles']['name'][$file_id]);
                         if (strlen($file_name)) {
                             $file_path = 'includes/plugin_file/upload/' . md5(time() . $file_name);
                             if (move_uploaded_file($tmp_file, $file_path)) {
                                 // success! write to db.
                                 $file_data = array('customer_id' => $customer_id, 'job_id' => current($job_created), 'website_id' => $website_id, 'status' => module_config::c('file_default_status', 'Uploaded'), 'pointers' => false, 'description' => "Uploaded from Customer Signup form", 'file_time' => time(), 'file_name' => $file_name, 'file_path' => $file_path, 'file_url' => false);
                                 $file_id = update_insert('file_id', false, 'file', $file_data);
                                 $uploaded_files[] = $file_id;
                             }
                         }
                     }
                 }
             }
             // we create subscriptions for this customer/website (if none already exist)
             $subscription['subscription_name'] = array();
             $subscription['subscription_invoice'] = array();
             if (class_exists('module_subscription', false) && module_subscription::is_plugin_enabled() && isset($subscription['for']) && isset($subscription['subscriptions'])) {
                 if ($subscription['for'] == 'website' && $website_id > 0) {
                     $owner_table = 'website';
                     $owner_id = $website_id;
                 } else {
                     $owner_table = 'customer';
                     $owner_id = $customer_id;
                 }
                 $available_subscriptions = module_subscription::get_subscriptions();
                 $members_subscriptions = module_subscription::get_subscriptions_by($owner_table, $owner_id);
                 foreach ($subscription['subscriptions'] as $subscription_id => $tf) {
                     if (isset($available_subscriptions[$subscription_id])) {
                         if (isset($members_subscriptions[$subscription_id])) {
                             // we don't allow a member to sign up to the same subscription twice (just yet)
                         } else {
                             $subscription['subscription_name'][$subscription_id] = $available_subscriptions[$subscription_id]['name'];
                             $start_date = date('Y-m-d');
                             $start_modifications = module_config::c('customer_signup_subscription_start', '');
                             if ($start_modifications == 'hidden') {
                                 $start_modifications = isset($_REQUEST['customer_signup_subscription_start']) ? $_REQUEST['customer_signup_subscription_start'] : '';
                             }
                             if (!empty($start_modifications)) {
                                 $start_date = date('Y-m-d', strtotime($start_modifications));
                             }
                             $sql = "INSERT INTO `" . _DB_PREFIX . "subscription_owner` SET ";
                             $sql .= " owner_id = '" . (int) $owner_id . "'";
                             $sql .= ", owner_table = '" . mysql_real_escape_string($owner_table) . "'";
                             $sql .= ", subscription_id = '" . (int) $subscription_id . "'";
                             $sql .= ", start_date = '{$start_date}'";
                             query($sql);
                             module_subscription::update_next_due_date($subscription_id, $owner_table, $owner_id, true);
                             // and the same option here to send a subscription straight away upon signup
                             if (module_config::c('subscription_send_invoice_straight_away', 0)) {
                                 global $plugins;
                                 $plugins['subscription']->run_cron();
                                 // check if there are any invoices for this subscription
                                 $history = module_subscription::get_subscription_history($subscription_id, $owner_table, $owner_id);
                                 if (count($history) > 0) {
                                     foreach ($history as $h) {
                                         if ($h['invoice_id']) {
                                             $invoice_data = module_invoice::get_invoice($h['invoice_id']);
                                             if ($invoice_data['date_cancel'] != '0000-00-00') {
                                                 continue;
                                             }
                                             $subscription['subscription_invoice'][] = '<a href="' . module_invoice::link_public($h['invoice_id']) . '">' . _l('Invoice #%s for %s', htmlspecialchars($invoice_data['name']), dollar($invoice_data['total_amount'], true, $invoice_data['currency_id'])) . '</a>';
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if (!count($subscription['subscription_name'])) {
                 $subscription['subscription_name'][] = _l('N/A');
             }
             if (!count($subscription['subscription_invoice'])) {
                 $subscription['subscription_invoice'][] = _l('N/A');
             }
             $subscription['subscription_name'] = implode(', ', $subscription['subscription_name']);
             $subscription['subscription_invoice'] = implode(', ', $subscription['subscription_invoice']);
             // email the admin when a customer signs up.
             $values = array_merge($customer, $customer_extra, $website, $website_extra, $address, $subscription);
             $values['customer_name'] = $customer['customer_name'];
             $values['CUSTOMER_LINK'] = module_customer::link_open($customer_id);
             $values['CUSTOMER_NAME_LINK'] = module_customer::link_open($customer_id, true);
             if ($website_id) {
                 $values['WEBSITE_LINK'] = module_website::link_open($website_id);
                 $values['WEBSITE_NAME_LINK'] = module_website::link_open($website_id, true);
             } else {
                 $values['WEBSITE_LINK'] = _l('N/A');
                 $values['WEBSITE_NAME_LINK'] = _l('N/A');
             }
             $values['JOB_LINKS'] = '';
             if (count($job_created)) {
                 $values['JOB_LINKS'] .= 'The customer created ' . count($job_created) . ' jobs in the system: <br>';
                 foreach ($job_created as $job_created_id) {
                     $values['JOB_LINKS'] .= module_job::link_open($job_created_id, true) . "<br>\n";
                 }
             } else {
                 $values['JOB_LINKS'] = _l('N/A');
             }
             if (count($uploaded_files)) {
                 $values['uploaded_files'] = 'The customer uploaded ' . count($uploaded_files) . " files:<br>\n";
                 foreach ($uploaded_files as $uploaded_file) {
                     $values['uploaded_files'] .= module_file::link_open($uploaded_file, true) . "<br>\n";
                 }
             } else {
                 $values['uploaded_files'] = 'No files were uploaded';
             }
             $values['WEBSITE_NAME'] = isset($website['url']) ? $website['url'] : 'N/A';
             if (!$creating_new) {
                 $values['system_note'] = "Note: this signup updated the existing customer record in the system.";
             } else {
                 $values['system_note'] = "Note: this signup created a new customer record in the system.";
             }
             $customer_signup_template = module_config::c('customer_signup_email_admin_template', 'customer_signup_email_admin');
             if (isset($_REQUEST['customer_signup_email_admin_template'])) {
                 $customer_signup_template = $_REQUEST['customer_signup_email_admin_template'];
             }
             if ($customer_signup_template) {
                 $template = module_template::get_template_by_key($customer_signup_template);
                 if ($template->template_id) {
                     $template->assign_values($values);
                     $html = $template->render('html');
                     $email = module_email::new_email();
                     $email->replace_values = $values;
                     $email->set_subject($template->description);
                     $email->set_to_manual(module_config::c('customer_signup_admin_email', module_config::c('admin_email_address')));
                     // do we send images inline?
                     $email->set_html($html);
                     if ($email->send()) {
                         // it worked successfully!!
                     } else {
                         /// log err?
                     }
                 }
             }
             $customer_signup_template = module_config::c('customer_signup_email_welcome_template', 'customer_signup_email_welcome');
             if (isset($_REQUEST['customer_signup_email_welcome_template'])) {
                 $customer_signup_template = $_REQUEST['customer_signup_email_welcome_template'];
             }
             if ($customer_signup_template) {
                 $template = module_template::get_template_by_key($customer_signup_template);
                 if ($template->template_id) {
                     $template->assign_values($values);
                     $html = $template->render('html');
                     $email = module_email::new_email();
                     $email->customer_id = $customer_id;
                     $email->replace_values = $values;
                     $email->set_subject($template->description);
                     $email->set_to('user', $customer_data['primary_user_id']);
                     // do we send images inline?
                     $email->set_html($html);
                     if ($email->send()) {
                         // it worked successfully!!
                     } else {
                         /// log err?
                     }
                 }
             }
             //todo: optional redirect to url
             if (isset($_REQUEST['via_ajax'])) {
                 echo json_encode(array('success' => 1, 'customer_id' => $customer_id));
                 exit;
             }
             if (module_config::c('customer_signup_redirect', '')) {
                 redirect_browser(module_config::c('customer_signup_redirect', ''));
             }
             // load up the thank you template.
             $template = module_template::get_template_by_key('customer_signup_thank_you_page');
             $template->page_title = _l("Customer Signup");
             foreach ($values as $key => $val) {
                 if (!is_array($val)) {
                     $values[$key] = htmlspecialchars($val);
                 }
             }
             $template->assign_values($values);
             echo $template->render('pretty_html');
             exit;
             break;
     }
 }
Example #15
0
 public static function get_statistics_subscription($search)
 {
     $subscriptions = module_subscription::get_subscriptions();
     $return = array();
     foreach ($subscriptions as $subscription) {
         $return[$subscription['subscription_id']] = $subscription;
         $return[$subscription['subscription_id']]['total_received'] = 0;
         $return[$subscription['subscription_id']]['total_received_count'] = 0;
         $return[$subscription['subscription_id']]['total_unpaid'] = 0;
         $return[$subscription['subscription_id']]['total_unpaid_count'] = 0;
         $return[$subscription['subscription_id']]['members'] = array();
         $return[$subscription['subscription_id']]['customers'] = array();
         // find all subscription_history's between these days
         $sql = "SELECT * ";
         $sql .= " FROM `" . _DB_PREFIX . "invoice` i";
         $sql .= " RIGHT JOIN `" . _DB_PREFIX . "subscription_history` sh ON i.invoice_id = sh.invoice_id ";
         $sql .= " WHERE sh.subscription_id = " . (int) $subscription['subscription_id'];
         if (isset($search['date_from']) && $search['date_from']) {
             $sql .= " AND i.date_create >= '" . input_date($search['date_from']) . "'";
         }
         if (isset($search['date_to']) && $search['date_to']) {
             $sql .= " AND i.date_create <= '" . input_date($search['date_to']) . "'";
         }
         $res = qa($sql);
         // this is a list of invoices for these subscriptions from these date periods.
         //print_r($res); return;
         foreach ($res as $r) {
             $invoice = module_invoice::get_invoice($r['invoice_id']);
             $return[$subscription['subscription_id']]['total_received'] += $invoice['total_amount_paid'];
             if ($invoice['total_amount_paid'] > 0) {
                 $return[$subscription['subscription_id']]['total_received_count']++;
             }
             $return[$subscription['subscription_id']]['total_unpaid'] += $invoice['total_amount_due'];
             if ($invoice['total_amount_due'] > 0) {
                 $return[$subscription['subscription_id']]['total_unpaid_count']++;
             }
             if ($r['customer_id']) {
                 if (!isset($return[$subscription['subscription_id']]['customers'][$r['customer_id']])) {
                     $return[$subscription['subscription_id']]['customers'][$r['customer_id']] = array('customer_id' => $r['customer_id'], 'received_payments' => 0, 'unpaid_payments' => 0, 'received_total' => 0, 'unpaid_total' => 0);
                 }
                 if ($invoice['total_amount_paid'] > 0) {
                     $return[$subscription['subscription_id']]['customers'][$r['customer_id']]['received_payments']++;
                 }
                 if ($invoice['total_amount_due'] > 0) {
                     $return[$subscription['subscription_id']]['customers'][$r['customer_id']]['unpaid_payments']++;
                 }
                 $return[$subscription['subscription_id']]['customers'][$r['customer_id']]['received_total'] += $invoice['total_amount_paid'];
                 $return[$subscription['subscription_id']]['customers'][$r['customer_id']]['unpaid_total'] += $invoice['total_amount_due'];
             }
             if ($r['member_id']) {
                 if (!isset($return[$subscription['subscription_id']]['members'][$r['member_id']])) {
                     $return[$subscription['subscription_id']]['members'][$r['member_id']] = array('member_id' => $r['member_id'], 'received_payments' => 0, 'unpaid_payments' => 0, 'received_total' => 0, 'unpaid_total' => 0);
                 }
                 if ($invoice['total_amount_paid'] > 0) {
                     $return[$subscription['subscription_id']]['members'][$r['member_id']]['received_payments']++;
                 }
                 if ($invoice['total_amount_due'] > 0) {
                     $return[$subscription['subscription_id']]['members'][$r['member_id']]['unpaid_payments']++;
                 }
                 $return[$subscription['subscription_id']]['members'][$r['member_id']]['received_total'] += $invoice['total_amount_paid'];
                 $return[$subscription['subscription_id']]['members'][$r['member_id']]['unpaid_total'] += $invoice['total_amount_due'];
             }
         }
     }
     return $return;
 }
     if (isset($r['website_id']) && $r['website_id'] > 0) {
         echo module_website::link_open($r['website_id'], true);
     }
     ?>
             </td>
             <td>
                 <?php 
     if (isset($r['job_id']) && $r['job_id'] > 0) {
         echo module_job::link_open($r['job_id'], true);
     }
     ?>
             </td>
             <td>
                 <?php 
     if (isset($r['invoice_id']) && $r['invoice_id'] > 0) {
         echo module_invoice::link_open($r['invoice_id'], true);
     }
     ?>
             </td>
             <?php 
 } else {
     if (isset($r['finance_id'])) {
         ?>
             <td colspan="3">
                 <?php 
         echo module_finance::link_open($r['finance_id'], true);
         ?>
                 
                 <?php 
         echo htmlspecialchars($r['description']);
         ?>
Example #17
0
                                </td>
                            </tr>
                        <?php 
        }
        ?>

                    </tbody>
                </table>
                <?php 
    }
    ?>

                    </div>
                    <?php 
    $fieldset_data = array('heading' => array('title' => 'Job Invoices:', 'type' => 'h3'), 'elements_before' => ob_get_clean());
    if (module_invoice::can_i('create', 'Invoices')) {
        $fieldset_data['heading']['button'] = array('title' => _l('Create New Invoice'), 'url' => '#', 'id' => 'job_generate_invoice_button');
    }
    echo module_form::generate_fieldset($fieldset_data);
}
if (class_exists('module_finance', false) && module_finance::is_plugin_enabled() && module_finance::can_i('view', 'Finance') && (int) $job_id > 0 && module_finance::is_enabled() && is_file('includes/plugin_finance/pages/finance_job_edit.php')) {
    include 'includes/plugin_finance/pages/finance_job_edit.php';
}
?>


</form>
<?php 
// run the custom data hook to display items in this particular hook location
hook_handle_callback('custom_data_hook_location', _CUSTOM_DATA_HOOK_LOCATION_JOB_FOOTER, 'job', $job_id, $job);
hook_handle_callback('layout_column_half', 'end');
Example #18
0
            <a href="<?php 
    echo $finance['url'];
    ?>
"><?php 
    echo !trim($finance['name']) ? 'N/A' : htmlspecialchars($finance['name']);
    ?>
</a>
        </td>
	    <td><?php 
    echo $finance['customer_id'] ? module_customer::link_open($finance['customer_id'], true) : _l('N/A');
    ?>
</td>
	    <td><?php 
    if ($finance['invoice_id']) {
        $invoice_data = module_invoice::get_invoice($finance['invoice_id']);
        echo module_invoice::link_open($finance['invoice_id'], true, $invoice_data);
    } else {
        _e('N/A');
    }
    ?>
</td>
	    <td><?php 
    if (isset($finance['job_id']) && $finance['job_id']) {
        echo module_job::link_open($finance['job_id'], true);
    } else {
        if ($finance['invoice_id'] && count($invoice_data['job_ids'])) {
            foreach ($invoice_data['job_ids'] as $job_id) {
                echo module_job::link_open($job_id, true) . ' ';
            }
        } else {
            _e('N/A');
Example #19
0
} else {
    if ($task_data['billable']) {
        _e('Task is billable');
    } else {
        _e('Task not billable');
    }
    echo '<br/>';
    if ($task_data['taxable']) {
        _e('Task is taxable');
    } else {
        _e('Task not taxable');
    }
}
if ($task_data['invoiced'] && $task_data['invoice_id']) {
    echo '<br/>';
    echo _l('Invoice %s', module_invoice::link_open($task_data['invoice_id'], true));
}
?>
        </div>

        <div>

        <?php 
if (module_config::c('job_task_log_all_hours', 1) || $task_data['hours'] <= 0) {
    ?>
            <?php 
    if (!$task_data['fully_completed'] && $task_data['invoiced'] || $task_editable) {
        ?>
                <input type="hidden" name="job_task[<?php 
        echo $task_id;
        ?>
Example #20
0
 function handle_paypal_ipn()
 {
     ob_end_clean();
     if (!isset($_REQUEST['custom'])) {
         return;
     }
     $paypal_bits = explode("|", $_REQUEST['custom']);
     $user_id = (int) $paypal_bits[0];
     $payment_id = (int) $paypal_bits[1];
     $invoice_id = (int) $paypal_bits[2];
     $invoice_payment_subscription_id = false;
     if (count($paypal_bits) == 4) {
         // normal IPN, single payment.
     } else {
         if (count($paypal_bits) == 5) {
             // subscription IPN, with subscription id.
             $invoice_payment_subscription_id = (int) $paypal_bits[3];
             $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id);
         }
     }
     //send_error('bad?');
     if ($payment_id && $invoice_id) {
         $hash = $this->paypal_custom($user_id, $payment_id, $invoice_id, $invoice_payment_subscription_id);
         if ($hash != $_REQUEST['custom']) {
             send_error("PayPal IPN Error (incorrect hash) it should be " . $hash);
             exit;
         }
         /*$sql = "SELECT * FROM `"._DB_PREFIX."user` WHERE user_id = '$user_id' LIMIT 1";
                     $res = qa($sql);
                     if($res){
         
                         $user = array_shift($res);
                         if($user && $user['user_id'] == $user_id){*/
         // check for payment exists
         $payment = module_invoice::get_invoice_payment($payment_id);
         $invoice = module_invoice::get_invoice($invoice_id);
         if ($payment && $invoice) {
             /*if(isset($_REQUEST['fakepay'])){
                                         if($invoice_payment_subscription_id){
                                             // we have a subscription payment. woo!
                                             // this gets a bit tricky, we have to work out if the invoice has been generated for this subscription yet.
                                             // if this invoice hasn't been generated yet then we have to generate it.
                                             // pass this back to the invoice class so we can reuse this feature in the future.
                                             $data = module_invoice::create_new_invoice_for_subscription_payment($invoice_id, $payment_id, $invoice_payment_subscription_id);
                                             if($data && $data['invoice_id'] && $data['invoice_payment_id']){
             
                                                 $next_time = time();
                                                 $next_time = strtotime('+'.abs((int)$invoice_payment_subscription['days']).' days',$next_time);
                                                 $next_time = strtotime('+'.abs((int)$invoice_payment_subscription['months']).' months',$next_time);
                                                 $next_time = strtotime('+'.abs((int)$invoice_payment_subscription['years']).' years',$next_time);
                                                 update_insert('invoice_payment_subscription_id',$invoice_payment_subscription_id,'invoice_payment_subscription',array(
                                                     'date_last_pay' => date('Y-m-d'),
                                                     'date_next' => date('Y-m-d',$next_time),
                                                 ));
                                                 $new_payment_details = array(
                                                       'date_paid' => date('Y-m-d'),
                                                       'amount' => $_REQUEST['mc_gross'],
                                                       'method' => 'PayPal (Subscription)',
                                                       'invoice_payment_subscription_id' => $invoice_payment_subscription_id,
                                                  );
                                                 foreach(array('fee_percent','fee_amount','fee_description','fee_total') as $fee_field){
                                                     if(isset($payment[$fee_field])) {
                                                         $new_payment_details[ $fee_field ] = $payment[ $fee_field ];
                                                     }
                                                 }
                                                  update_insert("invoice_payment_id",$data['invoice_payment_id'],"invoice_payment",$new_payment_details);
             
                                                 module_invoice::save_invoice($data['invoice_id'],array());
             
                                                 echo "Successful Subscription Payment!";
             
                                             }else{
                                                 send_error("PayPal IPN Subscription Error (failed to generate new invoice!) ".var_export($result,true));
                                             }
             
                                         }else{
                                             // mark a normal payment as paid
             
                                             update_insert("invoice_payment_id",$payment_id,"invoice_payment",array(
                                                       'date_paid' => date('Y-m-d'),
                                                       'amount' => $_REQUEST['mc_gross'],
                                                       'method' => 'PayPal (IPN)',
                                              ));
             
                                             module_invoice::save_invoice($invoice_id,array());
             
                                             echo "Successful Payment!";
             
                                         }
                                         echo 'fakepay done';exit;
                                     }*/
             $invoice_currency = module_config::get_currency($invoice['currency_id']);
             $invoice_currency_code = $invoice_currency['code'];
             // check correct business
             if (!$_REQUEST['business'] && $_REQUEST['receiver_email']) {
                 $_REQUEST['business'] = $_REQUEST['receiver_email'];
             }
             if ($_REQUEST['business'] != module_config::c('payment_method_paypal_email', _ERROR_EMAIL)) {
                 send_error('PayPal error! Paid the wrong business name. ' . $_REQUEST['business'] . ' instead of ' . module_config::c('payment_method_paypal_email', _ERROR_EMAIL));
                 exit;
             }
             // check correct currency
             if ($invoice_currency_code && $_REQUEST['mc_currency'] != $invoice_currency_code) {
                 send_error('PayPal error! Paid the wrong currency code. ' . $_REQUEST['mc_currency'] . ' instead of ' . $invoice_currency_code);
                 exit;
             }
             switch ($_REQUEST['txn_type']) {
                 // handle subscriptions first.
                 // https://www.paypal.com/au/cgi-bin/webscr?cmd=p/acc/ipn-subscriptions-outside
                 case "subscr_signup":
                     // started! we update the start date of this one.
                     if ($invoice_payment_subscription_id) {
                         update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d')));
                     }
                     break;
                 case "subscr_cancel":
                 case "subscr_failed":
                 case "subscr_eot":
                     if ($invoice_payment_subscription_id) {
                         update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_FAILED));
                     }
                     break;
                     break;
                 case "subscr_payment":
                 case "web_accept":
                     if ($_REQUEST['payment_status'] == "Canceled_Reversal" || $_REQUEST['payment_status'] == "Refunded") {
                         // funky refund!! oh noes!!
                         // TODO: store this in the database as a negative payment... should be easy.
                         // populate $_REQUEST vars then do something like $payment_history_id = update_insert("payment_history_id","new","payment_history");
                         send_error("PayPal Error! The payment {$payment_id} has been refunded or reversed! BAD BAD! You have to follup up customer for money manually now.");
                     } else {
                         if ($_REQUEST['payment_status'] == "Completed") {
                             // payment is completed! yeye getting closer...
                             // running in paypal sandbox or not?
                             //$sandbox = (self::is_sandbox())?"sandbox.":'';
                             // quick check we're not getting a fake payment request.
                             $url = 'https://www.' . (self::is_sandbox() ? 'sandbox.' : '') . 'paypal.com/cgi-bin/webscr';
                             $result = self::fsockPost($url, $_POST);
                             //send_error('paypal sock post: '.$url."\n\n".var_export($result,true));
                             if (eregi("VERIFIED", $result)) {
                                 // finally have everything.
                                 // mark the payment as completed.
                                 if ($invoice_payment_subscription_id) {
                                     // we have a subscription payment. woo!
                                     // this gets a bit tricky, we have to work out if the invoice has been generated for this subscription yet.
                                     // if this invoice hasn't been generated yet then we have to generate it.
                                     // pass this back to the invoice class so we can reuse this feature in the future.
                                     $data = module_invoice::create_new_invoice_for_subscription_payment($invoice_id, $payment_id, $invoice_payment_subscription_id);
                                     if ($data && $data['invoice_id'] && $data['invoice_payment_id']) {
                                         $next_time = time();
                                         $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['days']) . ' days', $next_time);
                                         $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['months']) . ' months', $next_time);
                                         $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['years']) . ' years', $next_time);
                                         update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('date_last_pay' => date('Y-m-d'), 'date_next' => date('Y-m-d', $next_time)));
                                         $new_payment_details = array('date_paid' => date('Y-m-d'), 'amount' => $_REQUEST['mc_gross'], 'method' => 'PayPal (Subscription)', 'invoice_payment_subscription_id' => $invoice_payment_subscription_id);
                                         foreach (array('fee_percent', 'fee_amount', 'fee_description', 'fee_total') as $fee_field) {
                                             if (isset($payment[$fee_field])) {
                                                 $new_payment_details[$fee_field] = $payment[$fee_field];
                                             }
                                         }
                                         update_insert("invoice_payment_id", $data['invoice_payment_id'], "invoice_payment", $new_payment_details);
                                         module_invoice::save_invoice($data['invoice_id'], array());
                                         echo "Successful Subscription Payment!";
                                     } else {
                                         send_error("PayPal IPN Subscription Error (failed to generate new invoice!) " . var_export($result, true));
                                     }
                                 } else {
                                     // mark a normal payment as paid
                                     update_insert("invoice_payment_id", $payment_id, "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $_REQUEST['mc_gross'], 'method' => 'PayPal (IPN)'));
                                     module_invoice::save_invoice($invoice_id, array());
                                     echo "Successful Payment!";
                                 }
                                 /*// send customer an email thanking them for their payment.
                                                                         $sql = "SELECT * FROM "._DB_PREFIX."users WHERE user_id = '"._ADMIN_USER_ID."'";
                                                                         $res = qa($sql);
                                                                         $admin = array_shift($res);
                                                                         $from_email = $admin['email'];
                                                                         $from_name = $admin['real_name'];
                                                                         $mail_content = "Dear ".$user['real_name'].", \n\n";
                                                                         $mail_content .= "Your ".dollar($payment['outstanding'])." payment for '".$payment['description']."' has been processed. \n\n";
                                                                         $mail_content .= "We have successfully recorded your ".dollar($_REQUEST['mc_gross'])." payment in our system.\n\n";
                                                                         $mail_content .= "You will receive another email shortly from PayPal with details of the transaction.\n\n";
                                                                         $mail_content .= "Kind Regards,\n\n";
                                                                         $mail_content .= $from_name."\n".$from_email;
                                 
                                                                         send_error("PayPal SUCCESS!! User has paid you ".$_REQUEST['mc_gross']." we have recorded this against the payment and sent them an email");
                                                                         //$this->send_email( $payment_id, $user['email'], $mail_content, "Payment Successful", $from_email, $from_name );
                                                                         send_email($user['email'], "Payment Successful", $mail_content, array("FROM"=>$from_email,"FROM_NAME"=>$from_name));
                                                                         */
                                 // check if it's been paid in full..
                             } else {
                                 send_error("PayPal IPN Error (paypal rejected the payment!) " . var_export($result, true));
                             }
                         } else {
                             send_error("PayPal info: This payment is not yet completed, this usually means it's an e-cheque, follow it up in a few days if you dont hear anything. This also means you may have to login to paypal and 'Accept' the payment. So check there first.");
                         }
                     }
                     break;
                 default:
                     send_error("PayPal IPN Error (unknown transaction t ype!) ");
                     break;
             }
         } else {
             send_error("PayPal IPN Error (no payment found in database!)");
         }
         /*}else{
                   send_error("PayPal IPN Error (error with user that was found in database..)");
               }
           }else{
               send_error("PayPal IPN Error (no user found in database #1)");
           }*/
     } else {
         send_error("PayPal IPN Error (no payment or invoice id found)");
     }
     exit;
 }
                <strong><?php 
            echo $payment_methods_of['name'];
            ?>
</strong><br/>
                <?php 
            echo $payment_methods_of['description'];
            ?>
            </li>
            <?php 
        }
        ?>
        </ul>
        <?php 
    }
    $payment_methods_offline_html = ob_get_clean();
    $template_invoice_payment_methods = module_template::get_template_by_key('invoice_payment_methods');
    $template_invoice_payment_methods->assign_values(array('PAYMENT_METHODS_ONLINE' => $payment_methods_online_html, 'PAYMENT_METHODS_OFFLINE' => $payment_methods_offline_html));
    $template_invoice_payment_methods->assign_values(module_invoice::get_replace_fields($invoice_id, $invoice_data));
    echo $template_invoice_payment_methods->replace_content();
} else {
    ?>

    <p align="center">
    <?php 
    $template_print = module_template::get_template_by_key('invoice_payment_in_full');
    echo $template_print->content;
    ?>
	</p>
    
<?php 
}
Example #22
0
 public static function customer_id_changed($old_customer_id, $new_customer_id)
 {
     $old_customer_id = (int) $old_customer_id;
     $new_customer_id = (int) $new_customer_id;
     if ($old_customer_id > 0 && $new_customer_id > 0) {
         $sql = "UPDATE `" . _DB_PREFIX . "quote` SET customer_id = " . $new_customer_id . " WHERE customer_id = " . $old_customer_id;
         query($sql);
         module_invoice::customer_id_changed($old_customer_id, $new_customer_id);
         module_file::customer_id_changed($old_customer_id, $new_customer_id);
     }
 }
Example #23
0
 public static function is_automatic_paying_invoice($invoice_id)
 {
     $invoice_payments = module_invoice::get_invoice_payments($invoice_id);
     foreach ($invoice_payments as $payment) {
         if (isset($payment['invoice_payment_subscription_id']) && $payment['invoice_payment_subscription_id']) {
             return true;
         }
     }
     // check if this is part of a subscription, and if the previous subscription
     if (class_exists('module_subscription', false)) {
         // THIS CODE EXISTS
         // check if this invoice is part of a subscription.
         // if it is we hunt through the subscription history until we find a recent unpaid invoice
         $subscription_history_item = get_single('subscription_history', 'invoice_id', $invoice_id);
         if ($subscription_history_item && $subscription_history_item['subscription_owner_id']) {
             // we have an invoice that is on a subscription!
             $subscription_owner = module_subscription::get_subscription_owner($subscription_history_item['subscription_owner_id']);
             // check if there are unpaid invoices that were generated after this invoice.
             if ($subscription_owner['subscription_owner_id'] == $subscription_history_item['subscription_owner_id']) {
                 $subscription_history = get_multiple('subscription_history', array('subscription_owner_id' => $subscription_owner['subscription_owner_id']));
                 foreach ($subscription_history as $h) {
                     $invoice_payments = module_invoice::get_invoice_payments($h['invoice_id']);
                     foreach ($invoice_payments as $payment) {
                         if (isset($payment['invoice_payment_subscription_id']) && $payment['invoice_payment_subscription_id']) {
                             $payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $payment['invoice_payment_subscription_id']);
                             if ($payment_subscription && $payment_subscription['status'] == _INVOICE_SUBSCRIPTION_ACTIVE) {
                                 //} || $payment_subscription['status'] == _INVOICE_SUBSCRIPTION_PENDING)){
                                 return true;
                             }
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
Example #24
0
 public 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 invoice operations until after {$after_time}:00 - it is currently {$time_of_day}:" . date('i') . "<br>\n";
         }
         return;
     }
     // find automatic job renewals
     $sql = "SELECT p.* FROM `" . _DB_PREFIX . "job` p ";
     $sql .= " WHERE p.date_renew != '0000-00-00'";
     $sql .= " AND p.date_start != '0000-00-00'";
     $sql .= " AND p.date_renew <= '" . date('Y-m-d') . "'";
     $sql .= " AND (p.renew_job_id IS NULL OR p.renew_job_id = 0)";
     $sql .= " AND (p.renew_auto = 1)";
     $renew_jobs = qa($sql);
     foreach ($renew_jobs as $renew_job) {
         // time to automatically renew this job! woo!
         if ($debug) {
             echo "Automatically Renewing Job " . module_job::link_open($renew_job['job_id'], true) . "<br>\n";
         }
         //$job_details = $this->get_job($renew_job['job_id']);
         $job_invoices = module_invoice::get_invoices(array('job_id' => $renew_job['job_id']));
         $unpaid_invoice = false;
         foreach ($job_invoices as $job_invoice) {
             $job_invoice = module_invoice::get_invoice($job_invoice['invoice_id']);
             if ($job_invoice['total_amount_due'] > 0) {
                 $unpaid_invoice = true;
             }
         }
         if (module_config::c('invoice_auto_renew_only_paid_invoices', 1) && $unpaid_invoice) {
             if ($debug) {
                 echo "Not automatically renewing this job because it has unpaid invoices. <br>\n";
             }
         } else {
             $new_job_id = $this->renew_job($renew_job['job_id'], true);
             if ($new_job_id) {
                 //module_cache::clear_cache();
                 if ($debug) {
                     echo "Job Automatically Renewed: " . module_job::link_open($new_job_id, true) . "<br>\n";
                 }
                 if ($renew_job['renew_invoice']) {
                     // we want to tick all these tasks off and invoice this job, then send this invoice to the customer.
                     $job_tasks = module_job::get_tasks($new_job_id);
                     foreach ($job_tasks as $job_task_id => $job_task) {
                         $job_tasks[$job_task_id]['fully_completed_t'] = 1;
                         $job_tasks[$job_task_id]['fully_completed'] = 1;
                     }
                     $this->save_job_tasks($new_job_id, array('job_task' => $job_tasks));
                     //module_cache::clear_cache();
                     // generate an invoice for this job.
                     $_REQUEST['job_id'] = $new_job_id;
                     $new_invoice = module_invoice::get_invoice('new');
                     $new_invoice['date_create'] = $renew_job['date_renew'];
                     $new_invoice['invoice_invoice_item'] = module_invoice::get_invoice_items('new', $new_invoice);
                     $new_invoice_id = module_invoice::save_invoice('new', $new_invoice);
                     //module_cache::clear_cache();
                     if ($debug) {
                         echo "Generated new invoice for renewed job: " . module_invoice::link_open($new_invoice_id, true) . "<br/>";
                     }
                     if ($debug) {
                         echo "Emailing invoice to customer...";
                     }
                     if (module_invoice::email_invoice_to_customer($new_invoice_id)) {
                         if ($debug) {
                             echo "send successfully";
                         }
                     } else {
                         if ($debug) {
                             echo "send failed";
                         }
                     }
                     if ($debug) {
                         echo "<br>\n";
                     }
                 }
             }
         }
     }
 }
Example #25
0
 private static function _format_invoice_payment($invoice_payment_data, $finance_data)
 {
     if (isset($invoice_payment_data['invoice_payment_id']) && $invoice_payment_data['invoice_payment_id'] > 0 && isset($invoice_payment_data['invoice_id']) && $invoice_payment_data['invoice_id'] > 0) {
         $invoice_data = module_invoice::get_invoice($invoice_payment_data['invoice_id']);
         $invoice_payment_data['url'] = module_finance::link_open('new', false) . '&invoice_payment_id=' . $invoice_payment_data['invoice_payment_id'];
         if ($invoice_payment_data['amount'] < 0 && (isset($invoice_payment_data['payment_type']) && $invoice_payment_data['payment_type'] == _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT)) {
             $invoice_payment_data['name'] = !isset($invoice_payment_data['name']) ? _l('Assigning Credit') : $invoice_payment_data['name'];
             $invoice_payment_data['description'] = !isset($invoice_payment_data['description']) ? _l('Assigning Overpayment Credit from invoice <a href="%s">#%s</a>', module_invoice::link_open($invoice_payment_data['invoice_id'], false), $invoice_data['name'], $invoice_payment_data['method']) : $invoice_payment_data['description'];
             // refund
             $invoice_payment_data['amount'] = abs($invoice_payment_data['amount']);
             $invoice_payment_data['debit'] = abs($invoice_payment_data['amount']);
             $invoice_payment_data['sub_amount'] = abs($invoice_payment_data['amount']);
             $invoice_payment_data['taxable_amount'] = abs($invoice_payment_data['amount']);
             $invoice_payment_data['credit'] = 0;
             $invoice_payment_data['type'] = 'e';
         } else {
             if ($invoice_payment_data['amount'] < 0 || isset($invoice_payment_data['payment_type']) && $invoice_payment_data['payment_type'] == _INVOICE_PAYMENT_TYPE_REFUND) {
                 $invoice_payment_data['name'] = !isset($invoice_payment_data['name']) ? _l('Invoice Refund') : $invoice_payment_data['name'];
                 $invoice_payment_data['description'] = !isset($invoice_payment_data['description']) ? _l('Refund against invoice <a href="%s">#%s</a> via "%s" method', module_invoice::link_open($invoice_payment_data['invoice_id'], false), $invoice_data['name'], $invoice_payment_data['method']) : $invoice_payment_data['description'];
                 // refund
                 $invoice_payment_data['amount'] = abs($invoice_payment_data['amount']);
                 $invoice_payment_data['debit'] = abs($invoice_payment_data['amount']);
                 $invoice_payment_data['sub_amount'] = abs($invoice_payment_data['amount']);
                 $invoice_payment_data['taxable_amount'] = abs($invoice_payment_data['amount']);
                 $invoice_payment_data['credit'] = 0;
                 $invoice_payment_data['type'] = 'e';
             } else {
                 $invoice_payment_data['name'] = !isset($invoice_payment_data['name']) ? _l('Invoice Payment') : $invoice_payment_data['name'];
                 $invoice_payment_data['description'] = !isset($invoice_payment_data['description']) ? _l('Payment against invoice <a href="%s">#%s</a> via "%s" method', module_invoice::link_open($invoice_payment_data['invoice_id'], false), $invoice_data['name'], $invoice_payment_data['method']) : $invoice_payment_data['description'];
                 $invoice_payment_data['credit'] = $invoice_payment_data['amount'];
                 $invoice_payment_data['sub_amount'] = $invoice_payment_data['amount'];
                 $invoice_payment_data['taxable_amount'] = $invoice_payment_data['amount'];
                 $invoice_payment_data['debit'] = 0;
                 $invoice_payment_data['type'] = 'i';
             }
         }
         if (isset($invoice_payment_data['data']) && strlen($invoice_payment_data['data'])) {
             $details = @unserialize($invoice_payment_data['data']);
             if ($details && isset($details['custom_notes']) && strlen($details['custom_notes'])) {
                 $invoice_payment_data['description'] .= " \n(" . $details['custom_notes'] . ')';
             }
         }
         $invoice_payment_data['account_name'] = '';
         $invoice_payment_data['categories'] = '';
         if (module_config::c('finance_date_type', 'payment') == 'invoice') {
             $invoice_payment_data['transaction_date'] = $invoice_data['date_create'];
         } else {
             $invoice_payment_data['transaction_date'] = $invoice_payment_data['date_paid'];
         }
         $invoice_payment_data['invoice_name'] = $invoice_data['name'];
         $invoice_payment_data['taxes'] = $invoice_data['taxes'];
         // calculate the sub amount based on taxes.
         if ($invoice_payment_data['amount'] >= $invoice_data['total_amount']) {
             // user overpaid this invoice amount.
             // check if there hasn't been any refunds or anything or assigning deposits.
         } else {
             if ($invoice_payment_data['amount'] == $invoice_data['total_amount']) {
                 // then we can work out any sub non taxable items.
                 if ($invoice_data['total_tax'] > 0) {
                     //$finance['sub_amount'] = $finance['amount'] - $invoice_data['total_tax'];
                     // todo: cache these and do a get_invoice basic above so we don't calculate each time.
                     $invoice_payment_data['sub_amount'] = $invoice_data['total_sub_amount'];
                     $invoice_payment_data['taxable_amount'] = $invoice_data['total_sub_amount_taxable'];
                 }
             } else {
                 // todo: average out the difference between invoice payments and the total amount? spread the tax over all payments maybe?
                 if (count($invoice_payment_data['taxes'])) {
                     $tax_percents = 0;
                     $increment = false;
                     foreach ($invoice_payment_data['taxes'] as $tax_id => $tax) {
                         if ($tax['increment']) {
                             $increment = true;
                         }
                     }
                     foreach ($invoice_payment_data['taxes'] as $tax_id => $tax) {
                         // the 'amount' of tax here will be incorrect, because this is a part payment against an invoice
                         // the 'amount' in here is the FULL amount of tax that has been charged against the invoice
                         $invoice_payment_data['taxes'][$tax_id]['amount'] = 0;
                         if ($increment) {
                             $invoice_payment_data['taxable_amount'] = $invoice_payment_data['taxable_amount'] / (1 + $tax['percent'] / 100);
                         } else {
                             $tax_percents += $tax['percent'] / 100;
                         }
                     }
                     $invoice_payment_data['taxable_amount'] = round($invoice_payment_data['taxable_amount'] / (1 + $tax_percents) * 100, 2) / 100;
                     $invoice_payment_data['sub_amount'] = $invoice_payment_data['taxable_amount'];
                 }
             }
         }
         $new_finance = hook_handle_callback('finance_invoice_listing', $invoice_payment_data['invoice_id'], $finance_data);
         if (is_array($new_finance) && count($new_finance)) {
             foreach ($new_finance as $n) {
                 $invoice_payment_data = array_merge($invoice_payment_data, $n);
             }
         }
     }
     return $invoice_payment_data;
 }
Example #26
0
 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";
                     }
                 }
             }
         }
     }
 }
Example #27
0
        include 'includes/plugin_finance/pages/finance_invoice_edit.php';
    }
}
// invoice_id check
hook_handle_callback('layout_column_half', 'end');
$form_actions = array('class' => 'action_bar action_bar_left', 'elements' => array(array('type' => 'save_button', 'name' => 'butt_save', 'onclick' => "\$('#form_redirect').val('" . (!$invoice_id && isset($_REQUEST['job_id']) && (int) $_REQUEST['job_id'] > 0 ? module_job::link_open($_REQUEST['job_id']) : module_invoice::link_open(false)) . "');", 'value' => _l('Save and Return')), array('type' => 'save_button', 'name' => 'butt_save', 'value' => _l('Save'))));
if ((int) $invoice_id) {
    if ($invoice['date_paid'] && $invoice['date_paid'] != '0000-00-00') {
        $form_actions['elements'][] = array('type' => 'save_button', 'class' => 'submit_button', 'name' => 'butt_email', 'value' => _l('Email Receipt'));
    } else {
        $form_actions['elements'][] = array('type' => 'submit', 'class' => 'submit_button', 'name' => 'butt_email', 'value' => _l('Email Invoice'));
    }
    if (function_exists('convert_html2pdf')) {
        if (!module_invoice::can_i('edit', 'Invoices')) {
            $form_actions['elements'][] = array('type' => 'button', 'class' => 'submit_button no_permissions', 'name' => 'butt_print', 'value' => _l('Print PDF'), 'onclick' => "window.location.href='" . module_invoice::link_public_print($invoice_id) . "';");
        } else {
            $form_actions['elements'][] = array('type' => 'submit', 'class' => 'submit_button', 'name' => 'butt_print', 'value' => _l('Print PDF'));
        }
    }
}
if ((int) $invoice_id && module_invoice::can_i('delete', 'Invoices')) {
    $form_actions['elements'][] = array('type' => 'delete_button', 'name' => 'butt_del', 'value' => _l('Delete'));
}
$form_actions['elements'][] = array('type' => 'button', 'name' => 'cancel', 'value' => _l('Cancel'), 'class' => 'submit_button', 'onclick' => "window.location.href='" . module_invoice::link_open(false) . "';");
echo module_form::generate_form_actions($form_actions);
?>



</form>
    });
}
if (class_exists('module_invoice', false) && module_invoice::can_i('view', 'Invoices') && module_config::c('customer_list_show_invoices', 1)) {
    $columns['customer_invoices'] = array('title' => 'Invoices', 'callback' => function ($customer) {
        $invoices = module_invoice::get_invoices(array('customer_id' => $customer['customer_id']));
        if (count($invoices)) {
            $total_due = 0;
            $total_paid = 0;
            foreach ($invoices as $invoice) {
                $invoice = module_invoice::get_invoice($invoice['invoice_id']);
                $total_due += $invoice['total_amount_due'];
                $total_paid += $invoice['total_amount_paid'];
            }
            $old_customer_id = isset($_REQUEST['customer_id']) ? $_REQUEST['customer_id'] : false;
            $_REQUEST['customer_id'] = $customer['customer_id'];
            echo '<a href="' . module_invoice::link_open(false) . '">' . _l('%s invoice%s: %s', count($invoices), count($invoices) > 1 ? 's' : '', ($total_due > 0 ? '<span class="error_text">' . _l('%s due', dollar($total_due, true, $invoice['currency_id'])) . ' </span>' : '') . ($total_paid > 0 ? '<span class="success_text">' . _l('%s paid', dollar($total_paid, true, $invoice['currency_id'])) . ' </span>' : '')) . '</a>';
            if ($old_customer_id) {
                $_REQUEST['customer_id'] = $old_customer_id;
            } else {
                unset($_REQUEST['customer_id']);
            }
        }
    });
}
if (class_exists('module_extra', false)) {
    // do extra before "table sorting" so that it can hook in with the table sort call
    $table_manager->display_extra('customer', function ($customer) {
        module_extra::print_table_data('customer', $customer['customer_id']);
    }, 'customer_id');
    $table_manager->display_extra('user', function ($customer) {
        module_extra::print_table_data('user', $customer['primary_user_id']);
Example #29
0
 function handle_hook($hook, $calling_module = false, $owner_table = false, $key_name = false, $key_value = false, $rel_data = false)
 {
     switch ($hook) {
         case "home_alerts":
             $alerts = array();
             if (module_config::c('allow_note_reminders', 1)) {
                 // find any jobs that are past the due date and dont have a finished date.
                 $key = _l('Note Reminder');
                 if (class_exists('module_dashboard', false)) {
                     module_dashboard::register_group($key, array('columns' => array('name' => _l('Reminder'), 'type' => _l('Type'), 'full_link' => _l('Link'), 'date' => _l('Date'), 'days' => _l('Date'))));
                 }
                 $sql = "SELECT * FROM `" . _DB_PREFIX . "note` n ";
                 $sql .= " WHERE n.`reminder` = 1 AND n.note_time < " . (int) strtotime('+' . module_config::c('alert_days_in_future', 5) . ' days') . "";
                 $sql .= " AND ( n.`user_id` = 0 OR n.`user_id` = " . module_security::get_loggedin_id() . ")";
                 $sql .= " ORDER BY n.note_time ASC";
                 $tasks = qa($sql);
                 foreach ($tasks as $task) {
                     $alert_res = process_alert(date('Y-m-d', $task['note_time']), $key);
                     if ($alert_res) {
                         $alert_res['link'] = $task['rel_data'];
                         // fix for linking when changing folder.
                         $alert_res['type'] = _l(ucwords($task['owner_table']));
                         switch ($task['owner_table']) {
                             case 'user':
                                 $user = module_user::get_user($task['owner_id']);
                                 if ($user['customer_id'] || $user['vendor_id']) {
                                     $alert_res['link'] = module_user::link_open_contact($task['owner_id'], false, $user);
                                     $alert_res['full_link'] = module_user::link_open_contact($task['owner_id'], true, $user);
                                     $alert_res['type'] = _l('Contact');
                                 } else {
                                     $alert_res['link'] = module_user::link_open($task['owner_id'], false, $user);
                                     $alert_res['full_link'] = module_user::link_open($task['owner_id'], true, $user);
                                 }
                                 break;
                             case 'invoice':
                                 $invoice_data = module_invoice::get_invoice($task['owner_id'], true);
                                 if (!$invoice_data || !isset($invoice_data['invoice_id']) || $invoice_data['invoice_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_invoice::link_open($task['owner_id'], false, $invoice_data);
                                 $alert_res['full_link'] = module_invoice::link_open($task['owner_id'], true, $invoice_data);
                                 break;
                             case 'quote':
                                 $quote_data = module_quote::get_quote($task['owner_id'], true);
                                 if (!$quote_data || !isset($quote_data['quote_id']) || $quote_data['quote_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_quote::link_open($task['owner_id'], false, $quote_data);
                                 $alert_res['full_link'] = module_quote::link_open($task['owner_id'], true, $quote_data);
                                 break;
                             case 'website':
                                 $website_data = module_website::get_website($task['owner_id']);
                                 if (!$website_data || !isset($website_data['website_id']) || $website_data['website_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_website::link_open($task['owner_id'], false);
                                 $alert_res['full_link'] = module_website::link_open($task['owner_id'], true);
                                 break;
                             case 'customer':
                                 $customer_data = module_customer::get_customer($task['owner_id']);
                                 if (!$customer_data || !isset($customer_data['customer_id']) || $customer_data['customer_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_customer::link_open($task['owner_id'], false, $customer_data);
                                 $alert_res['full_link'] = module_customer::link_open($task['owner_id'], true, $customer_data);
                                 break;
                             case 'vendor':
                                 $vendor_data = module_vendor::get_vendor($task['owner_id']);
                                 if (!$vendor_data || !isset($vendor_data['vendor_id']) || $vendor_data['vendor_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_vendor::link_open($task['owner_id'], false, $vendor_data);
                                 $alert_res['full_link'] = module_vendor::link_open($task['owner_id'], true, $vendor_data);
                                 break;
                             case 'job':
                                 $job_data = module_job::get_job($task['owner_id']);
                                 if (!$job_data || !isset($job_data['job_id']) || $job_data['job_id'] != $task['owner_id']) {
                                     continue 2;
                                 }
                                 $alert_res['link'] = module_job::link_open($task['owner_id'], false, $job_data);
                                 $alert_res['full_link'] = module_job::link_open($task['owner_id'], true, $job_data);
                                 break;
                                 // todo - add others.
                         }
                         $alert_res['name'] = $task['note'];
                         $alert_res['date'] = print_date($alert_res['date']);
                         $alert_res['time'] = $task['note_time'];
                         $alerts[] = $alert_res;
                     }
                 }
             }
             return $alerts;
             break;
             /*case "note_list":
             				if($owner_id && $owner_id != 'new'){
             
             					$note_items = $this->get_notes(array("owner_table"=>$owner_table,"owner_id"=>$owner_id));
             					foreach($note_items as &$note_item){
             						// do it in loop here because of $this issues in static method below.
             						// instead of include file below.
             						$note_item['html'] = $this->print_note($note_item['note_id']);
             					}
             					include("pages/note_list.php");
             				}else{
             					echo 'Please save first before creating notes.';
             				}
             				break;*/
         /*case "note_list":
         				if($owner_id && $owner_id != 'new'){
         
         					$note_items = $this->get_notes(array("owner_table"=>$owner_table,"owner_id"=>$owner_id));
         					foreach($note_items as &$note_item){
         						// do it in loop here because of $this issues in static method below.
         						// instead of include file below.
         						$note_item['html'] = $this->print_note($note_item['note_id']);
         					}
         					include("pages/note_list.php");
         				}else{
         					echo 'Please save first before creating notes.';
         				}
         				break;*/
         case "note_delete":
             // find the key we are saving this address against.
             $owner_id = (int) $key_value;
             if (!$owner_id || $owner_id == 'new') {
                 // find one in the post data.
                 if (isset($_REQUEST[$key_name])) {
                     $owner_id = $_REQUEST[$key_name];
                 }
             }
             $note_hash = md5($owner_id . '|' . $owner_table);
             // just for posting unique arrays.
             if ($owner_table && $owner_id) {
                 $this->note_delete($owner_table, $owner_id);
             }
             break;
     }
 }
Example #30
0
        </td>
    </tr>
<?php 
}
$replace['invoice_summary'] = ob_get_clean();
/* START INVOICE LINE ITEMS */
$task_decimal_places = module_config::c('task_amount_decimal_places', -1);
if ($task_decimal_places < 0) {
    $task_decimal_places = false;
    // use default currency dec places.
}
$task_decimal_places_trim = module_config::c('task_amount_decimal_places_trim', 0);
$all_item_row_html = '';
$item_count = 0;
// changed from 1
foreach (module_invoice::get_invoice_items($invoice_id, $invoice) as $invoice_item_id => $invoice_item_data) {
    $row_replace = $invoice_item_data;
    $row_replace['item_odd_or_even'] = $item_count++ % 2 ? 'odd' : 'even';
    $row_replace['item_number'] = '';
    $row_replace['item_description'] = '';
    $row_replace['item_date'] = '';
    $row_replace['item_tax'] = 0;
    $row_replace['item_tax_rate'] = '';
    if (isset($invoice_item_data['custom_task_order']) && (int) $invoice_item_data['custom_task_order'] > 0) {
        $row_replace['item_number'] = $invoice_item_data['custom_task_order'];
    } else {
        if (isset($invoice_item_data['task_order']) && $invoice_item_data['task_order'] > 0) {
            $row_replace['item_number'] = $invoice_item_data['task_order'];
        } else {
            $row_replace['item_number'] = $item_count;
        }