function JobsFailure()
{
    global $sugar_config;
    $gb = new Global_Functions();
    $GLOBALS['log']->error("JOBS: JobsFailure: Started.");
    $bean = BeanFactory::getBean('E_Enrolments');
    $j = 0;
    $rstSQL = $bean->db->query("SELECT name, (execute_time + interval 11 hour) as exec_time, `status`, resolution, message FROM job_queue\n                                where resolution = 'failure' and execute_time > (now() - interval 12 hour) order by execute_time DESC;");
    $jobs = '<table border = 1><tbody><tr><td><b>Job Name</b></td><td><b>Exec Date</b></td><td><b>Status</b></td><td><b>Result</b></td><td><b>Message</b></td></tr>';
    // Failure JOBS
    while ($row = $bean->db->fetchByAssoc($rstSQL)) {
        if ($j == 0) {
            $j = 1;
        }
        $jobs .= "<tr><td>" . $row['name'] . "</td><td>" . $row['exec_time'] . "</td><td>" . $row['status'] . "</td><td>" . $row['resolution'] . "</td><td>" . $row['message'] . "</td></tr>";
    }
    //Hangs Up Jobs
    $rstSQL = $bean->db->query("SELECT name, (execute_time + interval 11 hour) as exec_time, `status`, resolution, message FROM job_queue\n                                where status = 'running' and (execute_time + interval 11 hour) < (now() - interval 1 hour) order by execute_time DESC");
    while ($row = $bean->db->fetchByAssoc($rstSQL)) {
        if ($j == 0) {
            $j = 1;
        }
        $jobs .= "<tr><td>" . $row['name'] . "</td><td>" . $row['exec_time'] . "</td><td>" . $row['status'] . "</td><td>" . $row['resolution'] . "</td><td>" . $row['message'] . "</td></tr>";
    }
    $jobs .= "</tbody></table>";
    if ($j == 1) {
        $strEmailBody = "<html><body><strong>Alarm notification about failure Jobs in CRM</strong><br>" . $jobs . "<br><br> Site: <a href='http://pumpkin.crm/'>Pumpkin CRM</a></body></html>";
        $emailObj = new Email();
        $defaults = $emailObj->getSystemDefaultEmail();
        $toAddresses = array();
        $toAddresses['Techsupport'] = $sugar_config['alarm_it_address'];
        //SEND EMAIL
        if ($gb->_Send_Email($defaults['email'], $defaults['name'], $toAddresses, 'CRM: Failure or Hangs up JOBS Alarm.', '', '', html_entity_decode($strEmailBody), array(), false)) {
            $GLOBALS['log']->error("JobsFailure: List of failure Jobs has been sent to IT.");
            return true;
        } else {
            $GLOBALS['log']->fatal("JobsFailure: Error send Email.");
            return false;
        }
    }
    $GLOBALS['log']->error("JOBS: JobsFailure: Finished.");
    return true;
}
 function sendInvoice($api, $args)
 {
     $global = new Global_Functions();
     //$GLOBALS['log']->error("PT_Dispatch: Args: ".print_r($args,true));
     if (isset($args['invoice']) && !empty($args['invoice'])) {
         $InvObj = BeanFactory::getBean('TRANN_Trans', $args['invoice']);
         if ($InvObj->trans_type == 'Invoice') {
             require_once "custom/CustomHandlers/Invoice_PDF.php";
         } elseif ($InvObj->trans_type == 'CreditMemo') {
             require_once "custom/CustomHandlers/CreditMemo_PDF.php";
         }
         $InvObj->load_relationship('contacts_trann_trans_1');
         $clientObj = current($InvObj->contacts_trann_trans_1->getBeans());
         // if(!empty($clients))                $clientObj = current($clients);
     } else {
         return false;
     }
     /*        $clientId = $global->_get_related_id($this->bean,'contacts_trann_trans_1contacts_ida','contacts_trann_trans_1');
             $clientObj = BeanFactory::getBean('Contacts');
             $clientObj->retrieve($clientId);*/
     if ($InvObj->trans_type == 'Invoice') {
         $PDFObj = new Invoice();
     } elseif ($InvObj->trans_type == 'CreditMemo') {
         $PDFObj = new CreditMemo();
     }
     //GET INVOICE PRODUCT ITEMS AND PRICE
     $items = array();
     for ($k = 1; $k < 5; $k++) {
         $itemID = 'trann_items_id' . $k . '_c';
         $price = 'amount_' . $k;
         if ($InvObj->{$itemID} != '' && $InvObj->{$price} != 0) {
             $itemObj = BeanFactory::getBean('TRANN_Items', $InvObj->{$itemID});
             $taxObj = BeanFactory::getBean('TAXRA_Tax', $itemObj->taxra_tax_id_c);
             $items[$k]['name'] = $itemObj->display_name;
             $items[$k]['price'] = number_format($InvObj->{$price}, 2);
             $amount_net = round((double) $InvObj->{$price} / (1 + (double) $taxObj->rate / 100), 2);
             $amount_tax = round((double) $InvObj->{$price} - (double) $amount_net, 2);
             $items[$k]['amount_net'] = number_format($amount_net, 2);
             $items[$k]['amount_tax'] = number_format($amount_tax, 2);
         }
     }
     //GET PAYMENTS FOR THIS INVOICE
     $query = "SELECT m.name as method,pay.* FROM trann_trans_trann_payments_1_c as link, trann_payments as pay,trann_payment_method as m WHERE link.trann_trans_trann_payments_1trann_trans_ida = '" . $InvObj->id . "' AND link.deleted=0 AND pay.id=link.trann_trans_trann_payments_1trann_payments_idb AND pay.deleted=0 AND m.id=pay.trann_payment_method_id_c AND m.deleted=0 ORDER BY pay.date_entered DESC";
     $result = $InvObj->db->query($query, true);
     $payments = array();
     $j = 0;
     while ($row = $InvObj->db->fetchByAssoc($result)) {
         $payments[$j]['number'] = $row['name'];
         $payments[$j]['method'] = $row['method'];
         $payments[$j]['date'] = $row['payment_date'];
         if ($row['trann_trans_id1_c'] == $InvObj->id) {
             $payments[$j]['amount'] = number_format($row['amount_applied_1'], 2);
         }
         if ($row['trann_trans_id2_c'] == $InvObj->id) {
             $payments[$j]['amount'] = number_format($row['amount_applied_2'], 2);
         }
         if ($row['trann_trans_id3_c'] == $InvObj->id) {
             $payments[$j]['amount'] = number_format($row['amount_applied_3'], 2);
         }
         $j++;
     }
     $InvObj->amount_remaining = number_format($InvObj->amount_remaining, 2);
     $InvObj->amount_total = number_format($InvObj->amount_total, 2);
     //CREATE INVOICE
     $PDFObj->Generate_PDF($InvObj, $clientObj, $payments, $items);
     //GET EMAIL TEMPALTE FOR INVOICE
     $emailTemp = BeanFactory::getBean('EmailTemplates');
     if ($InvObj->trans_type == 'Invoice') {
         $emailTemp->retrieve($global->INVOICE_TEMPLATE);
     } elseif ($InvObj->trans_type == 'CreditMemo') {
         $emailTemp->retrieve($global->CREDIT_MEMO_TEMPLATE);
     }
     $emailObj = new Email();
     $defaults = $emailObj->getSystemDefaultEmail();
     //SET TO ADDRESSES
     $toAddresses = array();
     $toAddresses[$clientObj->first_name . ' ' . $clientObj->last_name] = $clientObj->email1;
     if ($InvObj->email_address != '') {
         $extraEmails = explode(',', $InvObj->email_address);
         foreach ($extraEmails as $email) {
             $toAddresses[$email] = $email;
         }
     }
     //REPLACE VALUES FOR KEYS IN EMAIL TEMPLATE
     $data = array($clientObj->first_name . ' ' . $clientObj->last_name, $clientObj->first_name, $items[1]['name']);
     $keys = array('$contact_name', '$contact_first_name', '$product');
     $body = str_replace($keys, $data, $emailTemp->body_html);
     $subject = str_replace($keys, $data, $emailTemp->subject);
     //ATTACH INVOICE PDF
     $attachedFiles = array(array('file_location' => "cache/invoices/", 'filename' => $InvObj->name . ".pdf"));
     //SEND EMAIL
     if (!$global->_Send_Email($defaults['email'], $defaults['name'], $toAddresses, $subject . ' - ' . $InvObj->name, 'Contacts', $clientObj->id, $body, $attachedFiles, true)) {
         return false;
     } else {
         return true;
     }
 }