Ejemplo n.º 1
0
 function approveInvoice($VAR)
 {
     # Get the invoice details:
     $db =& DB();
     $q = "SELECT * FROM " . AGILE_DB_PREFIX . "invoice WHERE\n\t        \t\tid = " . $db->qstr($VAR['id']) . " AND\n\t        \t\tsite_id = " . $db->qstr(DEFAULT_SITE);
     $invoice = $db->Execute($q);
     if ($invoice === false) {
         global $C_debug;
         $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
         return false;
     }
     # Validate invoice exists & needs approval:
     if ($invoice->fields['id'] != $VAR['id'] || $invoice->fields['process_status'] == '1') {
         return false;
     }
     # Update the invoice approval status:
     $q = "UPDATE " . AGILE_DB_PREFIX . "invoice SET\n\t        \t\tdate_last \t\t= " . $db->qstr(time()) . ",\n\t        \t\tprocess_status \t= " . $db->qstr('1') . " WHERE\n\t        \t\tid\t\t \t\t= " . $db->qstr($VAR['id']) . " AND\n\t        \t\tsite_id = " . $db->qstr(DEFAULT_SITE);
     $result = $db->Execute($q);
     if ($result === false) {
         global $C_debug;
         $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
         return false;
     }
     # Send approval notice to user:
     include_once PATH_MODULES . 'email_template/email_template.inc.php';
     $mail = new email_template();
     $mail->send('invoice_approved_user', $invoice->fields['account_id'], $VAR['id'], '', '');
     # Include the service class
     include_once PATH_MODULES . 'service/service.inc.php';
     $srvc = new service();
     # Determine if services have already been created for this invoice:
     if ($invoice->fields['type'] != 1) {
         $q = "SELECT id FROM " . AGILE_DB_PREFIX . "service WHERE\n    \t        \t\tinvoice_id = " . $db->qstr($VAR['id']) . " AND\n    \t        \t\tsite_id = " . $db->qstr(DEFAULT_SITE);
         $service = $db->Execute($q);
         if ($service === false) {
             global $C_debug;
             $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
             return false;
         }
         if ($service->RecordCount() > 0) {
             # Update services to approved status:
             while (!$service->EOF) {
                 $srvc->approveService($service->fields['id']);
                 $service->MoveNext();
             }
             return true;
         }
         # Get the parent items in this invoice :
         $q = "SELECT * FROM " . AGILE_DB_PREFIX . "invoice_item WHERE\n    \t        \t  ( parent_id = 0 OR parent_id IS NULL OR parent_id = '') AND\n    \t        \t  invoice_id =  " . $db->qstr($VAR['id']) . " AND\n    \t        \t  site_id \t =  " . $db->qstr(DEFAULT_SITE);
         $ii = $db->Execute($q);
         if ($ii === false) {
             global $C_debug;
             $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
             return false;
         }
         while (!$ii->EOF) {
             if (empty($ii->fields['service_id'])) {
                 # Add the service
                 $srvc->invoiceItemToService($ii->fields['id'], $invoice);
                 # Check for any children items in this invoice:
                 $q = "SELECT * FROM " . AGILE_DB_PREFIX . "invoice_item WHERE\n\t    \t\t        \t  parent_id  =  " . $db->qstr($ii->fields['id']) . " AND\n\t    \t\t        \t  invoice_id =  " . $db->qstr($VAR['id']) . " AND\n\t    \t\t        \t  site_id \t =  " . $db->qstr(DEFAULT_SITE);
                 $iii = $db->Execute($q);
                 if ($iii === false) {
                     global $C_debug;
                     $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
                     return false;
                 }
                 while (!$iii->EOF) {
                     # Add the service
                     $srvc->invoiceItemToService($ii->fields['id'], $invoice);
                     $iii->MoveNext();
                 }
             } else {
                 $srvc = new service();
                 if ($ii->fields['item_type'] == 2 && $ii->fields['domain_type'] == 'renew') {
                     # this is a domain renewal
                     $srvc->renewDomain($ii, $invoice->fields['account_billing_id']);
                 } else {
                     # this is an upgrade for an existing service
                     $srvc->modifyService($ii, $invoice->fields['account_billing_id']);
                 }
             }
             $ii->MoveNext();
         }
     } elseif ($invoice->fields['type'] == 1) {
         # recurring invoice, just update assoc services
         # Loop through invoice items & approve assoc services
         $q = "SELECT service_id FROM " . AGILE_DB_PREFIX . "invoice_item WHERE\n                        invoice_id = " . $db->qstr($VAR['id']) . " AND\n                        site_id = " . $db->qstr(DEFAULT_SITE);
         $service = $db->Execute($q);
         if ($service === false) {
             global $C_debug;
             $C_debug->error('invoice.inc.php', 'voidInvoice', $db->ErrorMsg());
             return false;
         }
         if ($service->RecordCount() > 0) {
             # Include the service class
             include_once PATH_MODULES . 'service/service.inc.php';
             $srvc = new service();
             # Update services to inactive status:
             while (!$service->EOF) {
                 $srvc->approveService($service->fields['service_id']);
                 $service->MoveNext();
             }
         }
     }
     # get account id
     if (defined("SESS_ACCOUNT")) {
         $account_id = SESS_ACCOUNT;
     } else {
         $account_id = 0;
     }
     # if approved, create a memo
     $id = $db->GenID(AGILE_DB_PREFIX . 'invoice_memo_id');
     $q = "INSERT INTO " . AGILE_DB_PREFIX . "invoice_memo\n\t\t    \t\tSET\n\t\t    \t\tid \t\t= " . $db->qstr($id) . ",\n\t\t    \t\tsite_id \t= " . $db->qstr(DEFAULT_SITE) . ",\n\t\t    \t\tdate_orig \t= " . $db->qstr(time()) . ",\n\t\t    \t\tinvoice_id\t= " . $db->qstr($VAR['id']) . ",\n\t\t    \t\taccount_id\t= " . $db->qstr($account_id) . ",\n\t\t    \t\ttype\t\t= " . $db->qstr('approval') . ",\n\t\t    \t\tmemo\t\t= " . $db->qstr('NA');
     $memo = $db->Execute($q);
     if ($memo === false) {
         global $C_debug;
         $C_debug->error('invoice.inc.php', 'approveInvoice', $db->ErrorMsg());
         return false;
     }
     return true;
 }