Esempio n. 1
0
 public static function get_finance($finance_id, $full = true, $invoice_payment_id = false)
 {
     if (!$invoice_payment_id) {
         $invoice_payment_id = isset($_REQUEST['invoice_payment_id']) && (int) $_REQUEST['invoice_payment_id'] > 0 ? (int) $_REQUEST['invoice_payment_id'] : false;
     }
     $finance_id = (int) $finance_id;
     if ($finance_id > 0) {
         if (!$full) {
             return get_single("finance", "finance_id", $finance_id);
         }
         $sql = "SELECT f.* ";
         $sql .= " , fa.name AS account_name ";
         $sql .= " , GROUP_CONCAT(fc.`name` ORDER BY fc.`name` ASC SEPARATOR ', ') AS categories ";
         $sql .= " FROM `" . _DB_PREFIX . "finance` f ";
         $sql .= " LEFT JOIN `" . _DB_PREFIX . "finance_account` fa USING (finance_account_id) ";
         $sql .= " LEFT JOIN `" . _DB_PREFIX . "finance_category_rel` fcr ON f.finance_id = fcr.finance_id ";
         $sql .= " LEFT JOIN `" . _DB_PREFIX . "finance_category` fc ON fcr.finance_category_id = fc.finance_category_id ";
         $sql .= " WHERE f.finance_id = {$finance_id} ";
         $sql .= " GROUP BY f.finance_id ";
         $sql .= " ORDER BY f.transaction_date DESC ";
         $finance = qa1($sql);
         $finance_id = $finance['finance_id'];
         // get the categories.
         $finance['category_ids'] = get_multiple('finance_category_rel', array('finance_id' => $finance_id), 'finance_category_id');
         $finance['taxes'] = get_multiple('finance_tax', array('finance_id' => $finance_id), 'finance_tax_id', 'exact', 'order');
         // get any linked items.
         $linked_finances = $linked_invoice_payments = array();
         // find any child / linked transactions to this one.
         if ((int) $finance_id > 0 && isset($finance['parent_finance_id']) && $finance['parent_finance_id'] > 0) {
             // todo - this could cause problems!
             $foo = module_finance::get_finance($finance['parent_finance_id'], false);
             if ($foo['finance_id'] != $finance_id) {
                 // copied from get_finances() method
                 $foo['url'] = module_finance::link_open($foo['finance_id'], false);
                 $foo['credit'] = $foo['type'] == 'i' ? $foo['amount'] : 0;
                 $foo['debit'] = $foo['type'] == 'e' ? $foo['amount'] : 0;
                 if (!isset($foo['categories'])) {
                     $foo['categories'] = '';
                 }
                 if (!isset($foo['account_name'])) {
                     $foo['account_name'] = '';
                 }
                 $linked_finances[] = $foo;
             }
             // find any child finances that are also linked to this parent finance.
             foreach (module_finance::get_finances_simple(array('parent_finance_id' => $finance['parent_finance_id'])) as $foo) {
                 if ($foo['finance_id'] != $finance_id) {
                     // copied from get_finances() method
                     $foo['url'] = module_finance::link_open($foo['finance_id'], false);
                     $foo['credit'] = $foo['type'] == 'i' ? $foo['amount'] : 0;
                     $foo['debit'] = $foo['type'] == 'e' ? $foo['amount'] : 0;
                     if (!isset($foo['categories'])) {
                         $foo['categories'] = '';
                     }
                     if (!isset($foo['account_name'])) {
                         $foo['account_name'] = '';
                     }
                     $linked_finances[] = $foo;
                 }
             }
             // find any child invoice payments that are also linked to this parent finance
             foreach (get_multiple('invoice_payment', array('parent_finance_id' => $finance['parent_finance_id'])) as $invoice_payments) {
                 if ($invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_NORMAL || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_REFUND) {
                     $invoice_payments = module_invoice::get_invoice_payment($invoice_payments['invoice_payment_id']);
                     // copied from get_finances() method
                     $invoice_payments = self::_format_invoice_payment($invoice_payments, $finance);
                     $linked_invoice_payments[$invoice_payments['invoice_payment_id']] = $invoice_payments;
                 }
             }
         }
         if ((int) $finance_id > 0) {
             // find any child finances that are linked to this finance.
             foreach (module_finance::get_finances_simple(array('parent_finance_id' => $finance_id)) as $foo) {
                 if ($foo['finance_id'] != $finance_id) {
                     // copied from get_finances() method
                     $foo['url'] = module_finance::link_open($foo['finance_id'], false);
                     $foo['credit'] = $foo['type'] == 'i' ? $foo['amount'] : 0;
                     $foo['debit'] = $foo['type'] == 'e' ? $foo['amount'] : 0;
                     if (!isset($foo['categories'])) {
                         $foo['categories'] = '';
                     }
                     if (!isset($foo['account_name'])) {
                         $foo['account_name'] = '';
                     }
                     $linked_finances[] = $foo;
                 }
             }
             // find any child invoice payments that are also linked to this parent finance
             foreach (get_multiple('invoice_payment', array('parent_finance_id' => $finance_id)) as $invoice_payments) {
                 if ($invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_NORMAL || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_REFUND) {
                     // copied from get_finances() method
                     $invoice_payments = module_invoice::get_invoice_payment($invoice_payments['invoice_payment_id']);
                     $invoice_payments = self::_format_invoice_payment($invoice_payments, $finance);
                     // hack to pull tax information from a linked invoice payment to replace current items tax if none is defined
                     if (!$finance['taxes'] && count($invoice_payments['taxes']) && $invoice_payments['amount'] == $finance['amount']) {
                         $finance['taxes'] = $invoice_payments['taxes'];
                         $finance['taxable_amount'] = $invoice_payments['taxable_amount'];
                         $finance['sub_amount'] = $invoice_payments['sub_amount'];
                     }
                     $linked_invoice_payments[$invoice_payments['invoice_payment_id']] = $invoice_payments;
                 }
             }
             if (isset($finance['invoice_payment_id']) && $finance['invoice_payment_id'] > 0) {
                 $invoice_payments = module_invoice::get_invoice_payment($finance['invoice_payment_id']);
                 if ($invoice_payments && ($invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_NORMAL || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT || $invoice_payments['payment_type'] == _INVOICE_PAYMENT_TYPE_REFUND)) {
                     $invoice_payments = self::_format_invoice_payment($invoice_payments, $finance);
                     // hack to pull tax information from a linked invoice payment to replace current items tax if none is defined
                     if (!$finance['taxes'] && count($invoice_payments['taxes']) && $invoice_payments['amount'] == $finance['amount']) {
                         $finance['taxes'] = $invoice_payments['taxes'];
                         $finance['taxable_amount'] = $invoice_payments['taxable_amount'];
                         $finance['sub_amount'] = $invoice_payments['sub_amount'];
                     }
                     $linked_invoice_payments[$invoice_payments['invoice_payment_id']] = $invoice_payments;
                 } else {
                     if (!$invoice_payments) {
                         // todo: this shou;ldnt happen, fix!
                     }
                 }
             }
         }
         $finance['linked_invoice_payments'] = $linked_invoice_payments;
         $finance['linked_finances'] = $linked_finances;
     }
     if ($finance_id <= 0) {
         $finance = array('finance_id' => 0, 'parent_finance_id' => 0, 'transaction_date' => print_date(time()), 'name' => '', 'description' => '', 'type' => 'e', 'sub_amount' => 0, 'taxable_amount' => 0, 'tax_mode' => module_config::c('finance_default_tax_mode', 0), 'taxes' => array(), 'amount' => 0, 'currency_id' => module_config::c('default_currency_id', 1), 'category_ids' => array(), 'customer_id' => 0, 'job_id' => 0, 'invoice_id' => 0, 'job_staff_expense' => 0, 'user_id' => 0);
         if (isset($_REQUEST['from_job_id'])) {
             $job_data = module_job::get_job((int) $_REQUEST['from_job_id']);
             $finance['job_id'] = $job_data['job_id'];
             if ($job_data['customer_id']) {
                 $finance['customer_id'] = $job_data['customer_id'];
             }
             if (isset($_REQUEST['job_staff_expense']) && (int) $_REQUEST['job_staff_expense'] > 0) {
                 // we have a job staff expense, load up the job tasks for this staff member and find out the cost.
                 if (isset($job_data['staff_total_grouped'][$_REQUEST['job_staff_expense']])) {
                     $staff_member = module_user::get_user($_REQUEST['job_staff_expense']);
                     if ($staff_member && $staff_member['user_id'] == $_REQUEST['job_staff_expense']) {
                         // valid job found, load in the defaults.
                         $finance['name'] = $job_data['name'];
                         $finance['description'] = _l('Job Expense For Staff Member: %s', $staff_member['name'] . ' ' . $staff_member['last_name']);
                         $finance['type'] = 'e';
                         $finance['amount'] = $job_data['staff_total_grouped'][$_REQUEST['job_staff_expense']];
                         $finance['taxes'] = array();
                         $finance['job_staff_expense_id'] = $job_data['job_id'];
                         $finance['job_id'] = $job_data['job_id'];
                         $finance['currency_id'] = $job_data['currency_id'];
                         $finance['transaction_date'] = print_date($job_data['date_completed']);
                         $finance['user_id'] = $staff_member['user_id'];
                         $finance['job_staff_expense'] = $staff_member['user_id'];
                     }
                 }
             }
         }
         if (!$full) {
             return $finance;
         }
         if ($invoice_payment_id && $invoice_payment_id > 0) {
             $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id);
             if ($invoice_payment_data && $invoice_payment_data['invoice_id']) {
                 $finance = self::_format_invoice_payment($invoice_payment_data, $finance);
                 $finance['invoice_id'] = $invoice_payment_data['invoice_id'];
                 $finance['currency_id'] = $invoice_payment_data['currency_id'];
             }
         }
     }
     if (isset($finance['invoice_id']) && $finance['invoice_id']) {
         $new_finance = hook_handle_callback('finance_invoice_listing', $finance['invoice_id'], $finance);
         if (is_array($new_finance) && count($new_finance)) {
             foreach ($new_finance as $n) {
                 $finance = array_merge($finance, $n);
             }
         }
     }
     $finance['taxes'] = self::sanatise_taxes(isset($finance['taxes']) ? $finance['taxes'] : array(), isset($finance['taxable_amount']) ? $finance['taxable_amount'] : 0);
     return $finance;
 }