function Create_Invoice_Pdf($bean, $event, $arguments)
 {
     if (($bean->trans_type == 'CreditMemo' || $bean->trans_type == 'Invoice') && $bean->send_invoice_email == 1) {
         if ($bean->trans_type == 'Invoice') {
             require_once "custom/CustomHandlers/Invoice_PDF.php";
         } elseif ($bean->trans_type == 'CreditMemo') {
             require_once "custom/CustomHandlers/CreditMemo_PDF.php";
         }
         $clientId = $this->_get_related_id($bean, 'contacts_trann_trans_1contacts_ida', 'contacts_trann_trans_1');
         $clientObj = BeanFactory::getBean('Contacts');
         $clientObj->retrieve($clientId);
         if ($bean->trans_type == 'Invoice') {
             $PDFObj = new Invoice();
         } elseif ($bean->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 ($bean->{$itemID} != '' && $bean->{$price} != 0) {
                 $itemObj = BeanFactory::getBean('TRANN_Items');
                 $itemObj->retrieve($bean->{$itemID});
                 $taxObj = BeanFactory::getBean('TAXRA_Tax');
                 $taxObj->retrieve($itemObj->taxra_tax_id_c);
                 $items[$k]['name'] = $itemObj->display_name;
                 $items[$k]['price'] = number_format($bean->{$price}, 2);
                 $amount_net = round((double) $bean->{$price} / (1 + (double) $taxObj->rate / 100), 2);
                 $amount_tax = round((double) $bean->{$price} - (double) $amount_net, 2);
                 $items[$k]['amount_net'] = number_format($amount_net, 2);
                 $items[$k]['amount_tax'] = number_format($amount_tax, 2);
             }
         }
         $bean->load_relationship('trann_trans_trann_payments_1');
         $PayList = $bean->trann_trans_trann_payments_1->getBeans();
         $j = 0;
         $payments = array();
         foreach ($PayList as $pay) {
             $payMethod = BeanFactory::getBean('TRANN_Payment_Method', $pay->trann_payment_method_id_c);
             $payments[$j]['number'] = $pay->name;
             $payments[$j]['method'] = $payMethod->name;
             $payments[$j]['date'] = $pay->payment_date;
             if ($pay->trann_trans_id1_c == $bean->id) {
                 $payments[$j]['amount'] = number_format($pay->amount_applied_1, 2);
             }
             if ($pay->trann_trans_id2_c == $bean->id) {
                 $payments[$j]['amount'] = number_format($pay->amount_applied_2, 2);
             }
             if ($pay->trann_trans_id3_c == $bean->id) {
                 $payments[$j]['amount'] = number_format($pay->amount_applied_3, 2);
             }
             $j++;
         }
         $bean->amount_remaining = number_format($bean->amount_remaining, 2);
         $bean->amount_total = number_format($bean->amount_total, 2);
         //CREATE INVOICE
         $PDFObj->Generate_PDF($bean, $clientObj, $payments, $items);
     }
 }
 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;
     }
 }