public static function get_finances($search = array()) { // we have to search for recent transactions. this involves combining the "finance" table with the "invoice_payment" table // then sort the results by date $hide_invoice_payments = false; $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 "; $where = " WHERE 1 "; if (isset($search['finance_account_id']) && is_array($search['finance_account_id'])) { $fo = array(); foreach ($search['finance_account_id'] as $val) { if ((int) $val > 0) { $fo[(int) $val] = true; } } if (count($fo) > 0) { $where .= " AND ( "; foreach ($fo as $f => $ff) { $where .= " f.finance_account_id = " . $f . ' OR'; } $where = rtrim($where, 'OR'); $where .= ' )'; $hide_invoice_payments = true; } } if (isset($search['finance_recurring_id']) && $search['finance_recurring_id']) { $where .= " AND f.finance_recurring_id = '" . (int) $search['finance_recurring_id'] . "'"; $hide_invoice_payments = true; } if (isset($search['finance_category_id']) && is_array($search['finance_category_id'])) { $fo = array(); foreach ($search['finance_category_id'] as $val) { if ((int) $val > 0) { $fo[(int) $val] = true; } } if (count($fo) > 0) { $where .= " AND EXISTS ( SELECT * FROM `" . _DB_PREFIX . "finance_category_rel` fcr2 WHERE fcr2.finance_id = f.finance_id AND ( "; foreach ($fo as $f => $ff) { $where .= " fcr2.finance_category_id = " . $f . ' OR'; } $where = rtrim($where, 'OR'); $where .= ' )'; $where .= ' )'; $hide_invoice_payments = true; } } if (isset($search['invoice_payment_id']) && $search['invoice_payment_id']) { $where .= " AND f.invoice_payment_id = '" . (int) $search['invoice_payment_id'] . "'"; $hide_invoice_payments = true; } // below 6 searches are repeated again below in invoice payments if (isset($search['job_id']) && (int) $search['job_id'] > 0) { $where .= " AND f.`job_id` = " . (int) $search['job_id']; } if (isset($search['invoice_id']) && (int) $search['invoice_id'] > 0) { $where .= " AND f.`invoice_id` = " . (int) $search['invoice_id']; } if (isset($search['customer_id']) && (int) $search['customer_id'] > 0) { $where .= " AND f.`customer_id` = " . (int) $search['customer_id']; } if (isset($search['company_id']) && (int) $search['company_id'] > 0) { // check this user can view this company id or not if (class_exists('module_company', false) && module_company::can_i('view', 'Company') && module_company::is_enabled()) { $companys = module_company::get_companys(); if (isset($companys[$search['company_id']])) { $sql .= " LEFT JOIN `" . _DB_PREFIX . "company_customer` cc ON f.customer_id = cc.customer_id "; $where .= " AND ( cc.`company_id` = " . (int) $search['company_id'] . " OR f.`company_id` = " . (int) $search['company_id'] . " )"; } } } if (isset($search['generic']) && strlen(trim($search['generic']))) { $name = mysql_real_escape_string(trim($search['generic'])); $where .= " AND (f.`name` LIKE '%{$name}%' OR f.description LIKE '%{$name}%' )"; } if (isset($search['date_from']) && $search['date_from'] != '') { $where .= " AND f.transaction_date >= '" . input_date($search['date_from']) . "'"; } if (isset($search['date_to']) && $search['date_to'] != '') { $where .= " AND f.transaction_date <= '" . input_date($search['date_to']) . "'"; } if (isset($search['amount_from']) && $search['amount_from'] != '') { $where .= " AND f.amount >= '" . mysql_real_escape_string($search['amount_from']) . "'"; } if (isset($search['amount_to']) && $search['amount_to'] != '') { $where .= " AND f.amount <= '" . mysql_real_escape_string($search['amount_to']) . "'"; } if (isset($search['type']) && $search['type'] != '' && $search['type'] != 'ie') { $where .= " AND f.type = '" . mysql_real_escape_string($search['type']) . "'"; } // permissions from job module. /*switch(module_job::get_job_access_permissions()){ case _JOB_ACCESS_ALL: break; case _JOB_ACCESS_ASSIGNED: // only assigned jobs! //$from .= " LEFT JOIN `"._DB_PREFIX."task` t ON u.job_id = t.job_id "; //u.user_id = ".(int)module_security::get_loggedin_id()." OR $where .= " AND (t.user_id = ".(int)module_security::get_loggedin_id().")"; break; case _JOB_ACCESS_CUSTOMER: break; }*/ // permissions from customer module. // tie in with customer permissions to only get jobs from customers we can access. switch (module_customer::get_customer_data_access()) { case _CUSTOMER_ACCESS_ALL: // all customers! so this means all jobs! break; case _CUSTOMER_ACCESS_ALL_COMPANY: case _CUSTOMER_ACCESS_CONTACTS: case _CUSTOMER_ACCESS_TASKS: case _CUSTOMER_ACCESS_STAFF: $valid_customer_ids = module_security::get_customer_restrictions(); if (count($valid_customer_ids)) { $where .= " AND f.customer_id IN ( "; foreach ($valid_customer_ids as $valid_customer_id) { $where .= (int) $valid_customer_id . ", "; } $where = rtrim($where, ', '); $where .= " )"; } } $where .= " GROUP BY f.finance_id "; $where .= " ORDER BY f.transaction_date DESC "; $sql .= $where; $finances_from_finance_db_table = qa($sql); // invoice payments: $finance_from_invoice_payments = array(); $finance_from_job_staff_expenses = array(); if (!$hide_invoice_payments && (!isset($search['invoice_id']) || !(int) $search['invoice_id'] > 0)) { $sql = "SELECT j.*, f.finance_id AS existing_finance_id "; $sql .= " FROM `" . _DB_PREFIX . "job` j "; $sql .= " LEFT JOIN `" . _DB_PREFIX . "finance` f ON j.job_id = f.job_id AND f.job_staff_expense > 0 "; $where = " WHERE 1 "; //j.date_completed != '0000-00-00' "; $where .= " AND j.`c_staff_total_amount` > 0 "; if (isset($search['job_id']) && (int) $search['job_id'] > 0) { $where .= " AND (j.`job_id` = " . (int) $search['job_id'] . " ) "; } if (isset($search['customer_id']) && (int) $search['customer_id'] > 0) { $where .= " AND j.`customer_id` = " . (int) $search['customer_id']; } /*if(isset($search['generic']) && strlen(trim($search['generic']))){ $name = mysql_real_escape_string(trim($search['generic'])); $where .= " AND (i.`name` LIKE '%$name%' OR p.method LIKE '%$name%' )"; }*/ if (isset($search['company_id']) && (int) $search['company_id'] > 0) { // check this user can view this company id or not if (class_exists('module_company', false) && module_company::can_i('view', 'Company') && module_company::is_enabled()) { $companys = module_company::get_companys(); if (isset($companys[$search['company_id']])) { $sql .= " LEFT JOIN `" . _DB_PREFIX . "company_customer` cc ON j.customer_id = cc.customer_id "; $where .= " AND cc.`company_id` = " . (int) $search['company_id']; } } } if (isset($search['date_from']) && $search['date_from'] != '') { $where .= " AND j.date_completed >= '" . input_date($search['date_from']) . "'"; } if (isset($search['date_to']) && $search['date_to'] != '') { $where .= " AND j.date_completed <= '" . input_date($search['date_to']) . "'"; } if (isset($search['amount_from']) && $search['amount_from'] != '') { $where .= " AND j.c_staff_total_amount >= '" . mysql_real_escape_string($search['amount_from']) . "'"; } if (isset($search['amount_to']) && $search['amount_to'] != '') { $where .= " AND j.c_staff_total_amount <= '" . mysql_real_escape_string($search['amount_to']) . "'"; } switch (module_job::get_job_access_permissions()) { case _JOB_ACCESS_ALL: break; case _JOB_ACCESS_ASSIGNED: // only assigned jobs! $sql .= " LEFT JOIN `" . _DB_PREFIX . "task` t ON j.job_id = t.job_id "; $where .= " AND (j.user_id = " . (int) module_security::get_loggedin_id() . " OR t.user_id = " . (int) module_security::get_loggedin_id() . ")"; break; case _JOB_ACCESS_CUSTOMER: // tie in with customer permissions to only get jobs from customers we can access. $valid_customer_ids = module_security::get_customer_restrictions(); if (count($valid_customer_ids)) { $where .= " AND j.customer_id IN ( "; foreach ($valid_customer_ids as $valid_customer_id) { $where .= (int) $valid_customer_id . ", "; } $where = rtrim($where, ', '); $where .= " )"; } break; } switch (module_customer::get_customer_data_access()) { case _CUSTOMER_ACCESS_ALL: // all customers! so this means all jobs! break; case _CUSTOMER_ACCESS_ALL_COMPANY: case _CUSTOMER_ACCESS_CONTACTS: case _CUSTOMER_ACCESS_TASKS: case _CUSTOMER_ACCESS_STAFF: $valid_customer_ids = module_security::get_customer_restrictions(); if (count($valid_customer_ids)) { $where .= " AND j.customer_id IN ( "; foreach ($valid_customer_ids as $valid_customer_id) { $where .= (int) $valid_customer_id . ", "; } $where = rtrim($where, ', '); $where .= " )"; } } $sql .= $where . " GROUP BY j.job_id ORDER BY j.date_completed DESC "; //echo $sql; $finance_from_job_staff_expenses = array(); $res = qa($sql); foreach ($res as $finance) { // we have a job with staff expenses. split this up into gruops based on staff members. $staff_total_grouped = false; if (isset($finance['c_staff_total_grouped']) && strlen($finance['c_staff_total_grouped'])) { $staff_total_grouped = @unserialize($finance['c_staff_total_grouped']); } if ($staff_total_grouped === false) { // echo 'here: '; // var_dump($finance); // var_dump($staff_total_grouped); $job_data = module_job::get_job($finance['job_id']); $staff_total_grouped = $job_data['staff_total_grouped']; } if (is_array($staff_total_grouped)) { foreach ($staff_total_grouped as $staff_id => $staff_total) { $staff_member = module_user::get_user($staff_id); if ($staff_member && $staff_member['user_id'] == $staff_id) { // make sure this entry doesn't already exist in the database table for this job // there MAY be an existing entry if 'existing_finance_id' is set if ($finance['existing_finance_id'] > 0) { // check if it exists for this staff member. $existing = get_single('finance', array('job_id', 'job_staff_expense', 'amount'), array($finance['job_id'], $staff_id, $staff_total)); if ($existing) { // match exists already, skip adding this one to the list. continue; } } //$finance = self::_format_invoice_payment($finance, $finance); //$finance['url'] = module_job::link_open($finance['job_id'],false,$finance); $finance['url'] = module_finance::link_open('new', false) . '&job_staff_expense=' . $staff_id . '&from_job_id=' . $finance['job_id']; $finance['transaction_date'] = $finance['date_completed']; $finance['description'] = _l('Job Expense For Staff Member: %s', $staff_member['name'] . ' ' . $staff_member['last_name']); //"Exiting: ".$finance['existing_finance_id'].": ". $finance['amount'] = $staff_total; $finance['debit'] = $staff_total; $finance['sub_amount'] = $staff_total; $finance['taxable_amount'] = $staff_total; $finance['credit'] = 0; $finance['type'] = 'e'; $finance_from_job_staff_expenses[] = $finance; } } } } } if (!$hide_invoice_payments) { $sql = "SELECT p.*, i.customer_id "; if (module_config::c('finance_date_type', 'payment') == 'invoice') { // show entries by invoice create date, not payment date. $sql .= " , i.date_create AS transaction_date "; } else { // default, show by paid date. $sql .= " , p.date_paid AS transaction_date "; } $sql .= " FROM `" . _DB_PREFIX . "invoice_payment` p "; $sql .= " LEFT JOIN `" . _DB_PREFIX . "invoice` i ON p.invoice_id = i.invoice_id "; $where = " WHERE p.date_paid != '0000-00-00' "; $where .= " AND p.`amount` != 0 "; $where .= " AND ( p.`payment_type` = " . _INVOICE_PAYMENT_TYPE_NORMAL . " OR p.`payment_type` = " . _INVOICE_PAYMENT_TYPE_REFUND . ' OR p.`payment_type` = ' . _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT . ' OR p.`payment_type` = ' . _INVOICE_PAYMENT_TYPE_CREDIT . ')'; if (isset($search['job_id']) && (int) $search['job_id'] > 0) { $sql .= " LEFT JOIN `" . _DB_PREFIX . "invoice_item` ii ON i.invoice_id = ii.invoice_id"; $sql .= " LEFT JOIN `" . _DB_PREFIX . "task` t ON ii.task_id = t.task_id"; $where .= " AND (t.`job_id` = " . (int) $search['job_id'] . " OR i.`deposit_job_id` = " . (int) $search['job_id'] . " ) "; } if (isset($search['invoice_id']) && (int) $search['invoice_id'] > 0) { $where .= " AND p.`invoice_id` = " . (int) $search['invoice_id']; } if (isset($search['customer_id']) && (int) $search['customer_id'] > 0) { $where .= " AND i.`customer_id` = " . (int) $search['customer_id']; } /*if(isset($search['generic']) && strlen(trim($search['generic']))){ $name = mysql_real_escape_string(trim($search['generic'])); $where .= " AND (i.`name` LIKE '%$name%' OR p.method LIKE '%$name%' )"; }*/ if (isset($search['company_id']) && (int) $search['company_id'] > 0) { // check this user can view this company id or not if (class_exists('module_company', false) && module_company::can_i('view', 'Company') && module_company::is_enabled()) { $companys = module_company::get_companys(); if (isset($companys[$search['company_id']])) { $sql .= " LEFT JOIN `" . _DB_PREFIX . "company_customer` cc ON i.customer_id = cc.customer_id "; $where .= " AND cc.`company_id` = " . (int) $search['company_id']; } } } if (isset($search['date_from']) && $search['date_from'] != '') { if (module_config::c('finance_date_type', 'payment') == 'invoice') { $where .= " AND i.date_create >= '" . input_date($search['date_from']) . "'"; } else { $where .= " AND p.date_paid >= '" . input_date($search['date_from']) . "'"; } } if (isset($search['date_to']) && $search['date_to'] != '') { if (module_config::c('finance_date_type', 'payment') == 'invoice') { $where .= " AND i.date_create <= '" . input_date($search['date_to']) . "'"; } else { $where .= " AND p.date_paid <= '" . input_date($search['date_to']) . "'"; } } if (isset($search['amount_from']) && $search['amount_from'] != '') { $where .= " AND p.amount >= '" . mysql_real_escape_string($search['amount_from']) . "'"; } if (isset($search['amount_to']) && $search['amount_to'] != '') { $where .= " AND p.amount <= '" . mysql_real_escape_string($search['amount_to']) . "'"; } if (isset($search['type']) && $search['type'] != '' && $search['type'] != 'ie') { if ($search['type'] == 'i') { $where .= " AND p.amount > 0"; } else { if ($search['type'] == 'e') { $where .= " AND p.amount < 0"; } } } switch (module_customer::get_customer_data_access()) { case _CUSTOMER_ACCESS_ALL: // all customers! so this means all jobs! break; case _CUSTOMER_ACCESS_ALL_COMPANY: case _CUSTOMER_ACCESS_CONTACTS: case _CUSTOMER_ACCESS_TASKS: case _CUSTOMER_ACCESS_STAFF: $valid_customer_ids = module_security::get_customer_restrictions(); if (count($valid_customer_ids)) { $where .= " AND i.customer_id IN ( "; foreach ($valid_customer_ids as $valid_customer_id) { $where .= (int) $valid_customer_id . ", "; } $where = rtrim($where, ', '); $where .= " )"; } } $sql .= $where . " ORDER BY p.date_paid DESC "; //echo $sql; $finance_from_invoice_payments = qa($sql); foreach ($finance_from_invoice_payments as $finance_id => $finance) { // doesn't have an finance / account reference just yet. // but they can create one and this will become a child entry to it. $finance = self::_format_invoice_payment($finance, $finance); /*if(!isset($finance['customer_id']) || !$finance['customer_id']){ $invoice_data = module_invoice::get_invoice($finance['invoice_id'],2); $finance['customer_id'] = $invoice_data['customer_id']; }*/ // grab a new name/descriptino/etc.. from other plugins (at the moment only subscription) /*$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_from_invoice_payments[$finance_id] = $finance; } if (isset($search['generic']) && strlen(trim($search['generic']))) { $name = mysql_real_escape_string(trim($search['generic'])); // $where .= " AND (i.`name` LIKE '%$name%' OR p.method LIKE '%$name%' )"; // we have to do a PHP search here because foreach ($finance_from_invoice_payments as $finance_id => $finance) { if (stripos($finance['name'], $name) === false && stripos($finance['description'], $name) === false) { unset($finance_from_invoice_payments[$finance_id]); } } } } $finances = array_merge($finances_from_finance_db_table, $finance_from_invoice_payments, $finance_from_job_staff_expenses); unset($finances_from_finance_db_table); unset($finance_from_invoice_payments); unset($finance_from_job_staff_expenses); // sort this if (!function_exists('sort_finance')) { function sort_finance($a, $b) { $t1 = strtotime($a['transaction_date']); $t2 = strtotime($b['transaction_date']); if ($t1 == $t2) { // sort by finance id, putting ones with a finance id first before others. then amount. if (isset($a['finance_id']) && !isset($b['finance_id'])) { // put $a before $b return -1; } else { if (!isset($a['finance_id']) && isset($b['finance_id'])) { // put $b before $a return 1; } else { return $a['amount'] > $b['amount']; } } } else { return $t1 < $t2; } } } uasort($finances, 'sort_finance'); foreach ($finances as $finance_id => $finance) { // we load each of these transactions // transaction can be a "transaction" or an "invoice_payment" // find out if this transaction is a child transaction to another transaction. // if it is a child transaction and we haven't already dispayed it in this listing // then we find the parent transaction and display it along with all it's children in this place. // this wont be perfect all the time but will be awesome in 99% of cases. if (isset($finance['finance_id']) && $finance['finance_id']) { // displayed before already? if (isset($displayed_finance_ids[$finance['finance_id']])) { $finances[$displayed_finance_ids[$finance['finance_id']]]['link_count']++; unset($finances[$finance_id]); continue; } $displayed_finance_ids[$finance['finance_id']] = $finance_id; if (isset($finance['invoice_payment_id']) && $finance['invoice_payment_id']) { $displayed_invoice_payment_ids[$finance['invoice_payment_id']] = $finance_id; // so we dont display again. } } else { if (isset($finance['invoice_payment_id']) && $finance['invoice_payment_id'] && isset($finance['invoice_id']) && $finance['invoice_id']) { // this is an invoice payment (incoming payment) // displayed before already? if (isset($displayed_invoice_payment_ids[$finance['invoice_payment_id']])) { $finances[$displayed_invoice_payment_ids[$finance['invoice_payment_id']]] = array_merge($finance, $finances[$displayed_invoice_payment_ids[$finance['invoice_payment_id']]]); $finances[$displayed_invoice_payment_ids[$finance['invoice_payment_id']]]['link_count']++; unset($finances[$finance_id]); continue; } $displayed_invoice_payment_ids[$finance['invoice_payment_id']] = $finance_id; // so we dont display again. } else { if (isset($finance['c_staff_total_amount'])) { // staff expense. } else { // nfi? unset($finances[$finance_id]); continue; } } } if (isset($finance['parent_finance_id']) && $finance['parent_finance_id']) { // check if it's parent finance id has been displayed already somewhere. if (isset($displayed_finance_ids[$finance['parent_finance_id']])) { $finances[$displayed_finance_ids[$finance['parent_finance_id']]]['link_count']++; unset($finances[$finance_id]); continue; // already done it on this page. } $displayed_finance_ids[$finance['parent_finance_id']] = $finance_id; // we haven't displayed the parent one yet. // display the parent one in this listing. $finance = self::get_finance($finance['parent_finance_id']); } /*if(isset($finance['invoice_payment_id']) && $finance['invoice_payment_id'] && isset($finance['invoice_id']) && $finance['invoice_id']){ // moved to above. }else*/ if (isset($finance['finance_id']) && $finance['finance_id']) { $finance['url'] = self::link_open($finance['finance_id'], false); $finance['credit'] = $finance['type'] == 'i' ? $finance['amount'] : 0; $finance['debit'] = $finance['type'] == 'e' ? $finance['amount'] : 0; if (!isset($finance['categories'])) { $finance['categories'] = ''; } if (!isset($finance['account_name'])) { $finance['account_name'] = ''; } } if (isset($finance['taxes']) && !isset($finance['sub_amount'])) { $finance['sub_amount'] = $finance['amount']; foreach ($finance['taxes'] as $tax) { if (isset($tax['amount'])) { $finance['sub_amount'] -= $tax['amount']; } } } $finance['link_count'] = 0; $finances[$finance_id] = $finance; } return $finances; }
<tbody> <?php if (class_exists('module_company', false) && defined('COMPANY_UNIQUE_CONFIG') && COMPANY_UNIQUE_CONFIG && (int) $template_id > 0 && module_company::can_i('view', 'Company') && module_company::is_enabled()) { ?> <tr> <th class="width2"> <?php echo _l('Choose Company'); ?> </th> <td> <?php $company_template_rel = array(); foreach (module_company::get_companys() as $company) { // does this one have a custom template yet? $custom_template = module_company::template_get_company($template_id, $template, $company['company_id']); if ($custom_template) { $company_template_rel[$company['company_id']] = $company['name'] . ' ' . _l('(custom)'); } else { $company_template_rel[$company['company_id']] = $company['name']; } } echo print_select_box($company_template_rel, 'company_id', $company_id, '', _l('Default')); ?> <script type="text/javascript"> $(function(){ $('#company_id').change(function(){ change_detected = false;
print_heading(array('main' => true, 'type' => 'h2', 'title' => $page_type, 'button' => $header_buttons)); ?> <form action="" method="post"> <?php $search_bar = array('elements' => array('name' => array('title' => _l('Names, Phone or Email:'), 'field' => array('type' => 'text', 'name' => 'search[generic]', 'value' => isset($search['generic']) ? $search['generic'] : '', 'size' => 15)), 'address' => array('title' => _l('Address:'), 'field' => array('type' => 'text', 'name' => 'search[address]', 'value' => isset($search['address']) ? $search['address'] : '', 'size' => 15)))); if (class_exists('module_extra', false)) { $search_bar['extra_fields'] = 'customer'; } if (class_exists('module_group', false) && module_customer::can_i('view', $page_type_single . ' Groups')) { $search_bar['elements']['group_id'] = array('title' => false, 'field' => array('type' => 'select', 'name' => 'search[group_id]', 'value' => isset($search['group_id']) ? $search['group_id'] : '', 'options' => module_group::get_groups('customer'), 'options_array_id' => 'name', 'blank' => _l(' Industry - '))); } if (class_exists('module_company', false) && module_company::can_i('view', 'Company') && module_company::is_enabled()) { $companys = module_company::get_companys(); $companys_rel = array(); foreach ($companys as $company) { $companys_rel[$company['company_id']] = $company['name']; } $search_bar['elements']['company'] = array('title' => false, 'field' => array('type' => 'select', 'name' => 'search[company_id]', 'value' => isset($search['company_id']) ? $search['company_id'] : '', 'options' => $companys_rel, 'blank' => _l(' - Company - '))); } echo module_form::search_bar($search_bar); /** START TABLE LAYOUT **/ $table_manager = module_theme::new_table_manager(); $columns = array(); if (class_exists('module_company', false) && module_company::can_i('view', 'Company') && module_company::is_enabled()) { $columns['company_name'] = array('title' => 'Company', 'callback' => function ($customer) { if (isset($customer['company_ids']) && is_array($customer['company_ids']) && count($customer['company_ids'])) { foreach ($customer['company_ids'] as $company_id => $company_name) { ?>