$to = module_user::get_contacts(array('customer_id' => $invoice['customer_id'])); // hunt for 'accounts' extra field $field_to_find = strtolower(module_config::c('accounts_extra_field_name', 'Accounts')); foreach ($to as $contact) { $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $contact['user_id'])); foreach ($extras as $e) { if (strtolower($e['extra_key']) == $field_to_find) { // this is the accounts contact - woo! $to_select = $contact['email']; } } } if (!$to_select && $customer['primary_user_id']) { $primary = module_user::get_user($customer['primary_user_id']); if ($primary) { $to_select = $primary['email']; } } } } else { if ($invoice['member_id']) { $member = module_member::get_member($invoice['member_id']); $to = array($member); $replace['customer_name'] = $member['first_name']; } else { $to = array(); } } $template->assign_values($replace); module_email::print_compose(array('title' => _l('Email Invoice: %s', $invoice['name']), 'find_other_templates' => 'invoice_email', 'current_template' => $template_name, 'customer_id' => $invoice['customer_id'], 'company_id' => isset($invoice['company_id']) ? $invoice['company_id'] : 0, 'to' => $to, 'to_select' => $to_select, 'bcc' => module_config::c('admin_email_address', ''), 'content' => $template->render('html'), 'subject' => $template->replace_description(), 'success_url' => module_invoice::link_open($invoice_id), 'success_callback' => 'module_invoice::email_sent', 'success_callback_args' => array('invoice_id' => $invoice_id, 'template_name' => $template_name), 'invoice_id' => $invoice_id, 'cancel_url' => module_invoice::link_open($invoice_id), 'attachments' => array(array('path' => $pdf, 'name' => basename($pdf), 'preview' => module_invoice::link_generate($invoice_id, array('arguments' => array('go' => 1, 'print' => 1), 'page' => 'invoice_admin', 'full' => false))))));
public function process() { $errors = array(); if (isset($_REQUEST['butt_del']) && $_REQUEST['butt_del'] && $_REQUEST['job_id']) { $data = self::get_job($_REQUEST['job_id']); if (module_form::confirm_delete('job_id', _l("Really delete job: %s", $data['name']), self::link_open($_REQUEST['job_id']))) { $this->delete_job($_REQUEST['job_id']); set_message("job deleted successfully"); redirect_browser($this->link_open(false)); } } else { if ("ajax_job_list" == $_REQUEST['_process']) { $customer_id = isset($_REQUEST['customer_id']) ? (int) $_REQUEST['customer_id'] : 0; $res = module_job::get_jobs(array('customer_id' => $customer_id)); $options = array(); foreach ($res as $row) { $options[$row['job_id']] = $row['name']; } echo json_encode($options); exit; } else { if ("ajax_create_invoice" == $_REQUEST['_process']) { $job_id = (int) $_REQUEST['job_id']; $job = self::get_job($job_id, true); $job_tasks = self::get_tasks($job_id); if (!$job || $job['job_id'] != $job_id) { exit; } // no permissions. if (!module_invoice::can_i('create', 'Invoices')) { exit; } // no permissions ob_start(); ?> <p><?php _e('Please select which tasks to generate an invoice for:'); ?> </p> <ul> <?php foreach ($job['uninvoiced_task_ids'] as $task_id) { if (isset($job_tasks[$task_id])) { ?> <li> <input type="checkbox" id="invoice_create_task_<?php echo $task_id; ?> " data-taskid="<?php echo $task_id; ?> " class="invoice_create_task" name="invoice_task_id[<?php echo $task_id; ?> ]" value="1" <?php echo $job_tasks[$task_id]['fully_completed'] ? 'checked' : ''; ?> > <label for="invoice_create_task_<?php echo $task_id; ?> "> (#<?php echo $job_tasks[$task_id]['task_order']; ?> ) <?php echo htmlspecialchars($job_tasks[$task_id]['description']); ?> </label> </li> <?php } } ?> </ul> <?php $html = ob_get_clean(); echo $html; exit; } else { if ("ajax_task" == $_REQUEST['_process']) { // we are requesting editing a task. $job_id = (int) $_REQUEST['job_id']; $job = self::get_job($job_id, true); $job_tasks = self::get_tasks($job_id); if (!$job || $job['job_id'] != $job_id) { exit; } // no permissions. if (!self::can_i('edit', 'Job Tasks')) { exit; } // no permissions if (isset($_REQUEST['toggle_completed'])) { $task_id = (int) $_REQUEST['task_id']; $task_data = $job_tasks[$task_id]; $result = array(); if ($task_data && $task_data['job_id'] == $job_id) { if ($task_data['invoiced'] && $task_data['fully_completed']) { // dont allow to 'uncompleted' fully completed invoice tasks } else { // it is editable. $task_data['fully_completed_t'] = 1; $task_data['fully_completed'] = $task_data['fully_completed'] ? 0 : 1; // save a single job task $this->save_job_tasks($job_id, array('job_task' => array($task_id => $task_data))); $result['success'] = 1; $result['job_id'] = $job_id; $result['task_id'] = $task_id; $result['message'] = $task_data['fully_completed'] ? _l('Task marked as complete') : _l('Task marked as incomplete'); $email_status = self::send_job_task_email($job_id, $result['task_id'], 'toggle'); if ($email_status !== false) { $result['message'] .= is_array($email_status) && isset($email_status['message']) ? $email_status['message'] : _l(' and email sent to customer'); } } } echo json_encode($result); exit; } else { if (isset($_REQUEST['delete_task_log_id']) && (int) $_REQUEST['delete_task_log_id'] > 0) { $task_id = (int) $_REQUEST['task_id']; $task_log_id = (int) $_REQUEST['delete_task_log_id']; $sql = "DELETE FROM `" . _DB_PREFIX . "task_log` WHERE task_id = '{$task_id}' AND task_log_id = '{$task_log_id}' LIMIT 1"; query($sql); echo 'done'; } else { if (isset($_REQUEST['update_task_order'])) { // updating the task orders for this task.. $task_order = (array) $_REQUEST['task_order']; foreach ($task_order as $task_id => $new_order) { if ((int) $new_order > 0 && isset($job_tasks[$task_id])) { update_insert('task_id', $task_id, 'task', array('task_order' => (int) $new_order)); } } echo 'done'; } else { $task_id = (int) $_REQUEST['task_id']; $task_data = $job_tasks[$task_id]; $task_editable = !$task_data['invoiced']; $job_task_creation_permissions = module_job::get_job_task_creation_permissions(); // todo - load this select box in via javascript from existing one on page. $staff_members = module_user::get_staff_members(); $staff_member_rel = array(); foreach ($staff_members as $staff_member) { $staff_member_rel[$staff_member['user_id']] = $staff_member['name']; } // new different formats for job data. $task_data['manual_task_type_real'] = $task_data['manual_task_type']; if ((!isset($task_data['manual_task_type']) || $task_data['manual_task_type'] < 0) && isset($job['default_task_type'])) { // use the job task type $task_data['manual_task_type'] = $job['default_task_type']; } $percentage = self::get_percentage($task_data); if (isset($_REQUEST['get_preview'])) { $after_task_id = $task_id; // this will put it right back where it started. $previous_task_id = 0; $job_tasks = self::get_tasks($job_id); $show_hours_summary = false; foreach ($job_tasks as $k => $v) { if ($v['manual_task_type'] < 0) { $job_tasks[$k]['manual_task_type'] = $job['default_task_type']; } if ($job_tasks[$k]['manual_task_type'] == _TASK_TYPE_HOURS_AMOUNT) { $show_hours_summary = true; } } foreach ($job_tasks as $k => $v) { // find out where this new task position is! if ($k == $task_id) { $after_task_id = $previous_task_id; break; } $previous_task_id = $k; } $create_invoice_button = ''; //if($job['total_amount_invoicable'] > 0 && module_invoice::can_i('create','Invoices')){ if (count($job['uninvoiced_task_ids']) && module_invoice::can_i('create', 'Invoices')) { //href="'.module_invoice::link_generate('new',array('arguments'=>array( 'job_id' => $job_id, ))).'" $create_invoice_button = '<a class="submit_button save_button uibutton job_generate_invoice_button" onclick="return ucm.job.generate_invoice();">' . _l('Create Invoice') . '</a>'; } $result = array('task_id' => $task_id, 'after_task_id' => $after_task_id, 'html' => self::generate_task_preview($job_id, $job, $task_id, $task_data), 'summary_html' => self::generate_job_summary($job_id, $job, $show_hours_summary), 'create_invoice_button' => $create_invoice_button); echo json_encode($result); } else { $show_task_numbers = module_config::c('job_show_task_numbers', 1) && $job['auto_task_numbers'] != 2; ob_start(); include 'pages/ajax_task_edit.php'; $result = array('task_id' => $task_id, 'hours' => isset($_REQUEST['hours']) ? (double) $_REQUEST['hours'] : 0, 'html' => ob_get_clean()); echo json_encode($result); } } } } exit; } else { if ("save_job_tasks_ajax" == $_REQUEST['_process']) { // do everything via ajax. trickery! // dont bother saving the job. it's already created. $job_id = (int) $_REQUEST['job_id']; $job_data = self::get_job($job_id); if (!$job_id || !$job_data || $job_data['job_id'] != $job_id) { set_error('Permission denied'); exit; } $result = $this->save_job_tasks($job_id, $_POST); $job_data = self::get_job($job_id, false); //if(!$job_data || $job_data['job_id'] != $job_id) $new_status = self::update_job_completion_status($job_id); $new_status = addcslashes(htmlspecialchars($new_status), "'"); //module_cache::clear_cache(); $new_job_data = self::get_job($job_id, false); // we now have to edit the parent DOM to reflect these changes. // what were we doing? adding a new task? editing an existing task? switch ($result['status']) { case 'created': // we added a new task. // add a new task to the bottom (OR MID WAY!) through the task list. if ((int) $result['task_id'] > 0) { // support for job task completion email. $email_status = self::send_job_task_email($job_id, $result['task_id'], 'created'); ?> <script type="text/javascript"> parent.refresh_task_preview(<?php echo (int) $result['task_id']; ?> ); parent.clear_create_form(); parent.ucm.add_message('<?php _e('New task created successfully'); echo is_array($email_status) && isset($email_status['message']) ? $email_status['message'] : ($email_status ? _l(' and email sent to customer') : ''); ?> '); parent.ucm.display_messages(true); <?php if ($job_data['status'] != $new_status) { ?> parent.jQuery('#status').val('<?php echo $new_status; ?> ').change();<?php } ?> <?php if ($new_job_data['date_completed'] != $job_data['date_completed']) { ?> parent.jQuery('#date_completed').val('<?php echo print_date($new_job_data['date_completed']); ?> ').change();<?php } ?> </script> <?php } else { set_error('New task creation failed.'); ?> <script type="text/javascript"> top.location.href = '<?php echo $this->link_open($_REQUEST['job_id']); ?> &added=true'; </script> <?php } break; case 'deleted': // we deleted a task. set_message('Task removed successfully'); ?> <script type="text/javascript"> top.location.href = '<?php echo $this->link_open($_REQUEST['job_id']); ?> '; <?php if ($job_data['status'] != $new_status) { ?> parent.jQuery('#status').val('<?php echo $new_status; ?> ').change();<?php } ?> </script> <?php break; case 'error': set_error('Something happened while trying to save a task. Unknown error.'); // something happened, refresh the parent browser frame ?> <script type="text/javascript"> top.location.href = '<?php echo $this->link_open($_REQUEST['job_id']); ?> '; </script> <?php break; case 'edited': // we changed a task (ie: completed?); // update this task above. if ((int) $result['task_id'] > 0) { $email_status = self::send_job_task_email($job_id, $result['task_id'], 'edited'); ?> <script type="text/javascript"> parent.canceledittask(); //parent.refresh_task_preview(<?php echo (int) $result['task_id']; ?> ); parent.ucm.add_message('<?php _e('Task saved successfully'); echo is_array($email_status) && isset($email_status['message']) ? $email_status['message'] : ($email_status ? _l(' and email sent to customer') : ''); ?> '); parent.ucm.display_messages(true); <?php if ($job_data['status'] != $new_status) { ?> parent.jQuery('#status').val('<?php echo $new_status; ?> ').change();<?php } ?> <?php if ($new_job_data['date_completed'] != $job_data['date_completed']) { ?> parent.jQuery('#date_completed').val('<?php echo print_date($new_job_data['date_completed']); ?> ').change();<?php } ?> </script> <?php } else { ?> <script type="text/javascript"> parent.canceledittask(); parent.ucm.add_error('<?php _e('Unable to save task'); ?> '); parent.ucm.display_messages(true); <?php if ($job_data['status'] != $new_status) { ?> parent.jQuery('#status').val('<?php echo $new_status; ?> ').change();<?php } ?> </script> <?php } break; default: ?> <script type="text/javascript"> parent.ucm.add_error('<?php _e('Unable to save task. Please check required fields.'); ?> '); parent.ucm.display_messages(true); </script> <?php break; } exit; } else { if ("save_job" == $_REQUEST['_process']) { $job_id = (int) $_REQUEST['job_id']; if ((int) $job_id > 0) { $original_job_data = self::get_job($job_id, false); if (!$original_job_data || $original_job_data['job_id'] != $job_id) { $original_job_data = array(); $job_id = false; } } else { $original_job_data = array(); $job_id = false; } // check create permissions. if (!$job_id && !self::can_i('create', 'Jobs')) { // user not allowed to create jobs. set_error('Unable to create new Jobs'); redirect_browser(self::link_open(false)); } else { if ($job_id && !self::can_i('edit', 'Jobs')) { // user not allowed to create jobs. set_error('Unable to edit Jobs'); redirect_browser(self::link_open(false)); } } $job_id = $this->save_job($job_id, $_POST); // look for the new tasks flag. if (isset($_REQUEST['default_task_list_id']) && isset($_REQUEST['default_tasks_action'])) { switch ($_REQUEST['default_tasks_action']) { case 'insert_default': if ((int) $_REQUEST['default_task_list_id'] > 0) { $default = self::get_default_task($_REQUEST['default_task_list_id']); $task_data = $default['task_data']; $new_task_data = array('job_task' => array()); foreach ($task_data as $task) { $task['job_id'] = $job_id; if ($task['date_due'] && $task['date_due'] != '0000-00-00') { $diff_time = strtotime($task['date_due']) - $task['saved_time']; $task['date_due'] = date('Y-m-d', time() + $diff_time); } $new_task_data['job_task'][] = $task; } $this->save_job_tasks($job_id, $new_task_data); } break; case 'save_default': $new_default_name = trim($_REQUEST['default_task_list_id']); if ($new_default_name != '') { // time to save it! $task_data = self::get_tasks($job_id); $cached_task_data = array(); foreach ($task_data as $task) { unset($task['task_id']); unset($task['date_done']); unset($task['invoice_id']); unset($task['task_order']); unset($task['create_user_id']); unset($task['update_user_id']); unset($task['date_created']); unset($task['date_updated']); $task['saved_time'] = time(); $cached_task_data[] = $task; /*$cached_task_data[] = array( 'hours' => $task['hours'], 'amount' => $task['amount'], 'billable' => $task['billable'], 'fully_completed' => $task['fully_completed'], 'description' => $task['description'], 'long_description' => $task['long_description'], 'date_due' => $task['date_due'], 'user_id' => $task['user_id'], 'approval_required' => $task['approval_required'], 'task_order' => $task['task_order'], 'saved_time' => time(), );*/ } self::save_default_tasks((int) $_REQUEST['default_task_list_id'], $new_default_name, $cached_task_data); unset($task_data); } break; } } // check if we are generating any renewals if (isset($_REQUEST['generate_renewal']) && $_REQUEST['generate_renewal'] > 0) { $new_job_id = $this->renew_job($job_id); set_message("Job renewed successfully"); redirect_browser($this->link_open($new_job_id)); } if (isset($_REQUEST['butt_create_deposit']) && isset($_REQUEST['job_deposit']) && $_REQUEST['job_deposit'] > 0) { if (strpos($_REQUEST['job_deposit'], '%') !== false) { $job_data = module_job::get_job($job_id); $percent = (int) str_replace('%', '', $_REQUEST['job_deposit']); $_REQUEST['job_deposit'] = number_out($job_data['total_amount'] * ($percent / 100)); } // create an invoice for this job. $url = module_invoice::link_generate('new', array('arguments' => array('job_id' => $job_id, 'as_deposit' => 1, 'amount_due' => number_in($_REQUEST['job_deposit']), 'description' => str_replace('{JOB_NAME}', $_POST['name'], module_config::c('job_deposit_text', 'Deposit for job: {JOB_NAME}'))))); redirect_browser($url); } set_message("Job saved successfully"); redirect_browser(isset($_REQUEST['_redirect']) && !empty($_REQUEST['_redirect']) ? $_REQUEST['_redirect'] : $this->link_open($job_id)); } } } } } } if (!count($errors)) { redirect_browser($_REQUEST['_redirect']); exit; } print_error($errors, true); }
$(function(){ if(typeof ucm.job != 'undefined'){ ucm.job.ajax_task_url = '<?php echo module_job::link_ajax_task($job_id, false); ?> '; <?php if (module_invoice::can_i('create', 'Invoices')) { ?> ucm.job.create_invoice_popup_url = '<?php echo module_job::link_create_job_invoice($job_id, false); ?> '; ucm.job.create_invoice_url = '<?php echo module_invoice::link_generate('new', array('arguments' => array('job_id' => $job_id))); ?> '; <?php } ?> ucm.job.init(); } }); var completed_tasks_hidden = false; // set with session variable / cookie var editing_task_id = false; var loading_task_html = '<tr class="task_edit_loading"><td colspan="9" align="center"><?php _e('Loading...'); ?> </td></tr>';
foreach ($invoice_total + $invoice_total_due as $currency_id => $foo) { $currency = get_single('currency', 'currency_id', $currency_id); $footer_rows[] = array('invoice_number' => array('data' => '<strong>' . _l('%s Totals:', $currency && isset($currency['code']) ? $currency['code'] : '') . '</strong>', 'cell_colspan' => $colspan - 2, 'cell_class' => 'text-right'), 'c_invoice_total' => array('data' => '<strong>' . dollar(isset($invoice_total[$currency_id]) ? $invoice_total[$currency_id] : 0, true, $currency_id) . '</strong>'), 'c_invoice_total_due' => array('data' => '<strong>' . dollar(isset($invoice_total_due[$currency_id]) ? $invoice_total_due[$currency_id] : 0, true, $currency_id) . '</strong>'), 'row_bulk_action' => array('data' => ' ', 'cell_colspan' => $colspan2)); } $table_manager->set_footer_rows($footer_rows); } $table_manager->pagination = true; $table_manager->print_table(); ?> </form> <?php if (function_exists('convert_html2pdf') && get_display_mode() != 'mobile') { ?> <form action="<?php echo module_invoice::link_generate(false, array('arguments' => array('print' => 1))); ?> " method="post"> <input type="hidden" name="invoice_ids" value="<?php echo implode(",", $all_invoice_ids); ?> "> <input type="submit" name="butt_print" value="<?php echo _l('Export all results as PDF'); ?> " class="submit_button" /> </form> <?php }