function task($VAR)
 {
     require_once PATH_MODULES . 'email_template/email_template.inc.php';
     require_once PATH_MODULES . 'invoice/invoice.inc.php';
     $invoice = new invoice();
     # get active net terms
     $db =& DB();
     $rs =& $db->Execute($sql = sqlSelect($db, "net_term", "*", "status=1"));
     if ($rs && $rs->RecordCount() > 0) {
         // loop through net terms
         while (!$rs->EOF) {
             $id = $rs->fields['id'];
             $last_interval = mktime(0, 0, 0, date('m'), date('d') - $rs->fields['terms'], date('Y'));
             $i =& $db->Execute($sql = sqlSelect($db, "invoice", "id,account_id,total_amt,billed_amt,due_date,net_term_date_last,net_term_intervals", "net_term_id = {$id} AND\n\t\t\t\t\t (suspend_billing = 0 OR suspend_billing IS NULL) AND\n\t\t\t\t\t (billing_status = 0 OR billing_status IS NULL) AND \n\t\t\t\t\t due_date <= {$last_interval} AND\n\t\t\t\t\t net_term_date_last <= {$last_interval}"));
             if ($i && $i->RecordCount() > 0) {
                 // loop through invoices
                 while (!$i->EOF) {
                     $terms = $rs->fields['terms'];
                     echo "<BR>" . ($start_interval = $i->fields['net_term_date_last']);
                     echo "<BR>" . ($stop_interval = $start_interval + 86400 * $terms);
                     echo "<BR>" . date(UNIX_DATE_FORMAT, $start_interval);
                     // charge or suspend?
                     if (!empty($i->fields['net_term_intervals']) && $rs->fields['suspend_intervals'] < $i->fields['net_term_intervals']) {
                         // suspend invoice
                         $arr['id'] = $i->fields['id'];
                         $na =& $invoice->voidInvoice($arr, $invoice);
                         // suspend billing status
                         $fields = array('suspend_billing' => 1);
                         $db->Execute($sql = sqlUpdate($db, "invoice", $fields, "id = {$i->fields['id']}"));
                         // send suspend e-mail
                         if ($rs->fields['enable_emails']) {
                             $email = new email_template();
                             $email->send('net_term_suspend', $i->fields['account_id'], $i->fields['id'], $rs->fields['suspend_intervals'], $i->fields['net_term_intervals']);
                         }
                     } else {
                         // calc late fee
                         if ($rs->fields['fee_type'] == 1) {
                             $fee = $rs->fields['fee'];
                         } else {
                             $fee = ($i->fields['total_amt'] - $i->fields['billed_amt']) * $rs->fields['fee'];
                         }
                         // create late charge
                         if ($fee > 0) {
                             $fields = array('date_orig' => time(), 'status' => 0, 'account_id' => $i->fields['account_id'], 'amount' => $fee, 'sweep_type' => $rs->fields['sweep_type'], 'taxable' => $this->taxable, 'quantity' => 1, 'attributes' => "Name=={$rs->fields['name']}\r\nInterval==" . date(UNIX_DATE_FORMAT, $start_interval) . " - " . date(UNIX_DATE_FORMAT, $stop_interval), 'description' => $rs->fields['sku']);
                             $db->Execute($sql = sqlInsert($db, "charge", $fields));
                             // update invoice
                             $_fields['net_term_intervals'] = $i->fields['net_term_intervals'] + 1;
                             $_fields['net_term_date_last'] = $stop_interval;
                             $db->Execute($sql = sqlUpdate($db, "invoice", $_fields, "id={$i->fields['id']}"));
                             echo "<BR><BR>{$sql}";
                         }
                         // send late fee/payment reminder e-mail:
                         if ($rs->fields['enable_emails']) {
                             $email = new email_template();
                             $email->send('net_term_late_notice', $i->fields['account_id'], $i->fields['id'], number_format($fee, 2), number_format($rs->fields['suspend_intervals'] - $i->fields['net_term_intervals']));
                         }
                     }
                     $i->MoveNext();
                 }
             }
             $rs->MoveNext();
         }
     }
 }
Exemple #2
0
 /**
  * Postback for Redirect Pay 
  */
 function postback($arr)
 {
     global $C_debug;
     if (empty($arr['invoice_id'])) {
         return false;
     }
     if (empty($arr['transaction_id'])) {
         return false;
     }
     if (empty($arr['amount'])) {
         return false;
     }
     if (eregi("MULTI-", $arr['invoice_id'])) {
         $this->postback_multiple($arr);
         return;
     }
     # Get the latest invoice info:
     $db =& DB();
     $sql1 = "";
     if (!empty($arr['subscription_id'])) {
         $sql1 = "checkout_plugin_data = " . $db->qstr(trim($arr['subscription_id'])) . "  OR ";
     }
     $q = "SELECT * FROM " . AGILE_DB_PREFIX . "invoice WHERE\n\t       \t\t\t( \n\t\t\t\t\t\t{$sql1}\n\t       \t\t\t\tparent_id = " . $db->qstr(@$arr['invoice_id']) . "\n\t\t\t\t\t\tOR\n\t\t\t\t\t\tid        = " . $db->qstr(@$arr['invoice_id']) . "  \n\t\t\t\t\t)  \n\t\t\t\t\tAND\n\t       \t\t\tbilling_status != 1\n\t\t\t\t\tAND \n\t       \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE) . "\n\t       \t\t\tORDER BY date_orig\n\t       \t\t\tLIMIT 0,1";
     $invoice = $db->Execute($q);
     if ($invoice === false || $invoice->RecordCount() == 0) {
         $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
     }
     if ($invoice->RecordCount() == 0) {
         return false;
     }
     $invoice_id = $invoice->fields['id'];
     # Validate the currency
     $billed_currency_id = $invoice->fields['billed_currency_id'];
     $total_amt = $invoice->fields['total_amt'];
     $billed_amt = $invoice->fields['billed_amt'];
     $actual_billed_amt = $invoice->fields['actual_billed_amt'];
     $currency_iso = @$arr['currency'];
     if (empty($currency_iso) || !$currency_iso) {
         # same as billed_currency_id
         $this->billed_amt = $arr['amount'] + $billed_amt;
         $this->actual_billed_amt = $arr['amount'] + $billed_amt;
         $this->actual_billed_currency_id = $billed_currency_id;
     } else {
         # Get the actual billed currency id currency info:
         $q = "SELECT * FROM " . AGILE_DB_PREFIX . "currency WHERE\n\t        \t\t\tthree_digit\t= " . $db->qstr($currency_iso) . " AND\n\t        \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE);
         $result = $db->Execute($q);
         if ($result === false) {
             $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
         }
         $actual_billed_currency_id = $result->fields['id'];
         if (is_string($result->fields["convert_array"])) {
             $convert = unserialize($result->fields["convert_array"]);
         } else {
             $convert = false;
         }
         $this->format_currency[$actual_billed_currency_id] = array('symbol' => $result->fields["symbol"], 'convert' => $convert, 'iso' => $result->fields["three_digit"]);
         if ($result->RecordCount() == 0 || $actual_billed_currency_id == $billed_currency_id) {
             # same as billed_currency_id
             $this->billed_amt = $arr['amount'] + $billed_amt;
             $this->actual_billed_amt = $arr['amount'] + $billed_amt;
             $this->actual_billed_currency_id = $actual_billed_currency_id;
         } else {
             # Get the billed currency id currency info:
             $q = "SELECT * FROM " . AGILE_DB_PREFIX . "currency WHERE\n\t\t        \t\t\tid   \t= " . $db->qstr($billed_currency_id) . " AND\n\t\t        \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE);
             $result = $db->Execute($q);
             if ($result === false) {
                 $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
             }
             $this->format_currency[$billed_currency_id] = array('symbol' => $result->fields["symbol"], 'convert' => unserialize($result->fields["convert_array"]), 'iso' => $result->fields["three_digit"]);
             # Convert the invoice amount to the actual billed currency amount
             $due_amount = $invoice->fields['total_amt'] - $invoice->fields['billed_amt'];
             $conversion = $this->format_currency[$billed_currency_id]["convert"][$actual_billed_currency_id]["rate"];
             $this->billed_amt = $billed_amt + ($arr['amount'] /= $conversion);
             $this->actual_billed_amt = $actual_billed_amt + $arr['amount'];
             $this->actual_billed_currency_id = $actual_billed_currency_id;
         }
     }
     # Check for any subscription_id
     if (!empty($arr['subscription_id'])) {
         $this->subscription_id = trim($arr['subscription_id']);
     } else {
         $this->subscription_id = trim($invoice->fields['checkout_plugin_data']);
     }
     # Check for the checkout_id
     if (!empty($arr['checkout_id'])) {
         $this->checkout_id = $arr['checkout_id'];
     } else {
         $this->checkout_id = $invoice->fields['checkout_plugin_id'];
     }
     # Check for the billing status:
     if ($this->billed_amt >= $invoice->fields['total_amt']) {
         $this->billing_status = '1';
     } else {
         $this->billing_status = '0';
     }
     # Check if this transaction_id has already been processed:
     $q = "SELECT id FROM " . AGILE_DB_PREFIX . "invoice_memo WHERE\n        \t\t\tinvoice_id \t= " . $db->qstr($invoice_id) . " AND\n        \t\t\ttype\t\t= " . $db->qstr('postback') . " AND\n        \t\t\tmemo\t\t= " . $db->qstr($arr['transaction_id']) . " AND\n        \t\t\tsite_id \t= " . $db->qstr(DEFAULT_SITE);
     $memo = $db->Execute($q);
     if ($memo === false) {
         $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
     }
     if ($memo->RecordCount() > 0) {
         # duplicate post:
         $C_debug->error('Duplicate Postback', 'checkout.inc.php :: postback()', "Duplicate postback for invoice {$arr['invoice_id']} & transaction id {$arr['transaction_id']}");
     } else {
         # Create the invoice memo:
         $memo_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_memo_id');
         $q = "INSERT INTO\n\t        \t\t\t" . AGILE_DB_PREFIX . "invoice_memo \n\t        \t      SET\n\t        \t\t\tid \t\t\t\t\t= " . $db->qstr($memo_id) . ",\n\t        \t\t\tsite_id \t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t        \t\t\tdate_orig \t\t\t= " . $db->qstr(time()) . ", \n\t        \t\t\tinvoice_id\t \t\t= " . $db->qstr($invoice_id) . ", \n\t        \t\t\taccount_id\t\t\t= " . $db->qstr(0) . ", \n\t        \t\t\ttype\t\t\t\t= " . $db->qstr('postback') . ", \n\t        \t\t\tmemo\t\t\t\t= " . $db->qstr($arr['transaction_id']);
         $memosql = $db->Execute($q);
         if ($memosql === false) {
             $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
         }
         # Update the invoice billing info:
         $q = "UPDATE\n\t        \t\t\t" . AGILE_DB_PREFIX . "invoice \n\t        \t      SET\n\t        \t\t\tdate_last \t\t\t= " . $db->qstr(time()) . ", \n\t        \t\t\tbilling_status \t\t= " . $db->qstr($this->billing_status) . ", \n\t        \t\t\tcheckout_plugin_id\t= " . $db->qstr($this->checkout_id) . ", \n\t        \t\t\tcheckout_plugin_data = " . $db->qstr($this->subscription_id) . ", \n\t        \t\t\tbilled_amt\t\t\t= " . $db->qstr($this->billed_amt) . ", \n\t        \t\t\tactual_billed_amt\t= " . $db->qstr($this->actual_billed_amt) . ", \n\t        \t\t\tactual_billed_currency_id = " . $db->qstr($this->actual_billed_currency_id) . "\n\t        \t\t   WHERE\n\t        \t\t\tid \t\t\t= " . $db->qstr($invoice_id) . " AND\n\t        \t\t\tsite_id \t= " . $db->qstr(DEFAULT_SITE);
         $memosql = $db->Execute($q);
         if ($memosql === false) {
             $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
         }
         # Update the invoice approval status
         $VAR['id'] = $invoice_id;
         include_once PATH_MODULES . 'invoice/invoice.inc.php';
         $inv = new invoice();
         if (!$arr['status']) {
             # void
             $inv->voidInvoice($VAR);
             # create a record of the viod in an invoice memo:
             $memo_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_memo_id');
             $q = "INSERT INTO\n\t\t        \t\t\t" . AGILE_DB_PREFIX . "invoice_memo \n\t\t        \t      SET\n\t\t        \t\t\tid \t\t\t\t\t= " . $db->qstr($memo_id) . ",\n\t\t        \t\t\tsite_id \t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t\t        \t\t\tdate_orig \t\t\t= " . $db->qstr(time()) . ", \n\t\t        \t\t\tinvoice_id\t \t\t= " . $db->qstr($invoice_id) . ", \n\t\t        \t\t\taccount_id\t\t\t= " . $db->qstr(0) . ", \n\t\t        \t\t\ttype\t\t\t\t= " . $db->qstr('void') . ", \n\t\t        \t\t\tmemo\t\t\t\t= " . $db->qstr("Voided due to postback: " . $arr['transaction_id']);
             $rsql = $db->Execute($q);
             if ($rsql === false) {
                 $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg());
             }
         } else {
             # approve
             $inv->autoApproveInvoice($invoice_id);
             # User invoice payment confirmation
             include_once PATH_MODULES . 'email_template/email_template.inc.php';
             $email = new email_template();
             $email->send('invoice_paid_user', $invoice->fields['account_id'], $invoice_id, DEFAULT_CURRENCY, '');
             # Admin alert of payment processed
             $email = new email_template();
             $email->send('admin->invoice_paid_admin', $invoice->fields['account_id'], $invoice_id, DEFAULT_CURRENCY, '');
         }
     }
     return true;
 }