public function process() { $errors = array(); if ($_REQUEST['_process'] == 'make_payment') { $this->handle_payment(); } else { if (isset($_REQUEST['butt_del']) && $_REQUEST['butt_del'] && $_REQUEST['invoice_id'] && module_invoice::can_i('delete', 'Invoices')) { $data = self::get_invoice($_REQUEST['invoice_id']); if (module_form::confirm_delete('invoice_id', _l("Really delete invoice: %s", htmlspecialchars($data['name'])), self::link_open($_REQUEST['invoice_id']))) { $invoice_data = self::get_invoice($_REQUEST['invoice_id'], true); $this->delete_invoice($_REQUEST['invoice_id']); set_message("Invoice deleted successfully"); if (isset($invoice_data['job_ids']) && $invoice_data['job_ids']) { redirect_browser(module_job::link_open(current($invoice_data['job_ids']))); } else { redirect_browser(self::link_open(false)); } } } else { if ("assign_credit_to_customer" == $_REQUEST['_process']) { $invoice_id = (int) $_REQUEST['invoice_id']; if ($invoice_id > 0) { $invoice_data = $this->get_invoice($invoice_id); $credit = $invoice_data['total_amount_credit']; if ($credit > 0) { if ($invoice_data['customer_id']) { // assign to customer. module_customer::add_credit($invoice_data['customer_id'], $credit); // assign this as a negative payment, and also give it to the customer account. $this->add_history($invoice_id, 'Added ' . dollar($credit) . ' credit to customers account from this invoice overpayment'); update_insert('invoice_payment_id', 'new', 'invoice_payment', array('invoice_id' => $invoice_id, 'amount' => -$credit, 'payment_type' => _INVOICE_PAYMENT_TYPE_OVERPAYMENT_CREDIT, 'currency_id' => $invoice_data['currency_id'], 'method' => _l('Assigning Credit'), 'date_paid' => date('Y-m-d'))); module_cache::clear('invoice'); } } redirect_browser($this->link_open($invoice_id)); } } else { if ("save_invoice" == $_REQUEST['_process']) { $invoice_id = isset($_REQUEST['invoice_id']) ? (int) $_REQUEST['invoice_id'] : false; // check the user has permissions to edit this page. if ($invoice_id > 0) { $invoice = $this->get_invoice($invoice_id); if (!module_security::can_access_data('invoice', $invoice, $invoice_id)) { echo 'Data access denied. Sorry.'; exit; } } if (!$this->can_i('edit', 'Invoices')) { // bug fix, customer making a payment displays this edit access denied. if (isset($_REQUEST['butt_makepayment']) && $_REQUEST['butt_makepayment'] == 'yes') { //redirect_browser(self::link_public_pay($invoice_id)); self::handle_payment(); return; } else { echo 'Edit access denied. Sorry.'; exit; } } $data = $_POST; if (isset($data['default_renew_auto']) && !isset($data['renew_auto'])) { $data['renew_auto'] = 0; } if (isset($data['default_renew_email']) && !isset($data['renew_email'])) { $data['renew_email'] = 0; } if (isset($data['default_overdue_email_auto']) && !isset($data['overdue_email_auto'])) { $data['overdue_email_auto'] = 0; } if (isset($data['set_manual_company_id'])) { $data['company_id'] = $data['set_manual_company_id']; } if (isset($data['customer_id']) && $data['customer_id'] && (!isset($data['user_id']) || !$data['user_id'])) { // find the primary contact for this invoice and set that there? // no - we don't! we leave it as blank so we can update the customer primary contact when needed. /* $customer_data = module_customer::get_customer($data['customer_id']); if($customer_data && $customer_data['customer_id'] == $data['customer_id']){ if($customer_data['primary_user_id']){ $data['user_id'] = $customer_data['primary_user_id']; }else{ $customer_contacts = module_user::get_contacts(array('customer_id'=>$data['customer_id'])); foreach($customer_contacts as $contact){ // todo - search roles or something to find the accountant. $data['user_id'] = $contact['user_id']; break; } } }*/ } // check for credit assessment. if (isset($_POST['apply_credit_from_customer']) && $_POST['apply_credit_from_customer'] == 'do') { $invoice_data = $this->get_invoice($invoice_id); $customer_data = module_customer::get_customer($invoice_data['customer_id']); if ($customer_data['credit'] > 0) { $apply_credit = min($invoice_data['total_amount_due'], $customer_data['credit']); //$invoice_data['discount_amount'] += $customer_data['credit']; //$this->save_invoice($invoice_id,array('discount_amount'=>$invoice_data['discount_amount'],'discount_description'=>_l('Credit:'))); update_insert('invoice_payment_id', false, 'invoice_payment', array('invoice_id' => $invoice_id, 'payment_type' => _INVOICE_PAYMENT_TYPE_CREDIT, 'method' => _l('Credit'), 'amount' => $apply_credit, 'currency_id' => $invoice_data['currency_id'], 'other_id' => $invoice_data['customer_id'], 'date_paid' => date('Y-m-d'))); $this->add_history($invoice_id, _l('Applying %s customer credit to this invoice.', dollar($apply_credit))); module_cache::clear('invoice'); module_customer::remove_credit($customer_data['customer_id'], $apply_credit); } } // check for subsscription credit assessment. if (isset($_POST['apply_credit_from_subscription_bucket']) && $_POST['apply_credit_from_subscription_bucket'] == 'do' && (int) $_POST['apply_credit_from_subscription_id'] > 0) { $invoice_data = $this->get_invoice($invoice_id); $subscription_owner = module_subscription::get_subscription_owner($_POST['apply_credit_from_subscription_id']); $available_subscription_credit = module_subscription::get_available_credit($subscription_owner['owner_table'], $subscription_owner['owner_id']); //print_r($subscription_owner); print_r($available_subscription_credit);exit; if ($subscription_owner['subscription_owner_id'] && $available_subscription_credit[$subscription_owner['subscription_id']]['remain'] > 0) { $apply_credit = min($invoice_data['total_amount_due'], $available_subscription_credit[$subscription_owner['subscription_id']]['remain']); //$invoice_data['discount_amount'] += $customer_data['credit']; //$this->save_invoice($invoice_id,array('discount_amount'=>$invoice_data['discount_amount'],'discount_description'=>_l('Credit:'))); update_insert('invoice_payment_id', false, 'invoice_payment', array('invoice_id' => $invoice_id, 'payment_type' => _INVOICE_PAYMENT_TYPE_SUBSCRIPTION_CREDIT, 'method' => _l('Credit'), 'amount' => $apply_credit, 'currency_id' => $invoice_data['currency_id'], 'other_id' => $subscription_owner['subscription_owner_id'], 'date_paid' => date('Y-m-d'))); $this->add_history($invoice_id, _l('Applying %s subscription credit to this invoice.', dollar($apply_credit))); module_cache::clear('invoice'); } } $invoice_id = $this->save_invoice($invoice_id, $data); if (isset($_REQUEST['allowed_payment_method_check'])) { // todo - ability to disable ALL payment methods. - array wont be set if none are ticked $payment_methods = handle_hook('get_payment_methods'); foreach ($payment_methods as &$payment_method) { if ($payment_method->is_enabled()) { // is this one already enabled for this invoice? //$is_already_allowed = $payment_method->is_allowed_for_invoice($invoice_id); if (isset($_REQUEST['allowed_payment_method']) && isset($_REQUEST['allowed_payment_method'][$payment_method->module_name])) { $payment_method->set_allowed_for_invoice($invoice_id, 1); } else { $payment_method->set_allowed_for_invoice($invoice_id, 0); } } } } // check if we are generating any renewals if (isset($_REQUEST['generate_renewal']) && $_REQUEST['generate_renewal'] > 0) { $new_invoice_id = $this->renew_invoice($invoice_id); if ($new_invoice_id) { set_message("Invoice renewed successfully"); redirect_browser(module_invoice::link_open($new_invoice_id, false)); } else { } } if (isset($_REQUEST['butt_makepayment']) && $_REQUEST['butt_makepayment'] == 'yes') { //redirect_browser(self::link_public_pay($invoice_id)); self::handle_payment(); } else { if (isset($_REQUEST['butt_print']) && $_REQUEST['butt_print']) { $_REQUEST['_redirect'] = self::link_generate($invoice_id, array('arguments' => array('print' => 1))); } else { if (isset($_REQUEST['butt_merge']) && $_REQUEST['butt_merge'] && isset($_REQUEST['merge_invoice']) && is_array($_REQUEST['merge_invoice'])) { $merge_invoice_ids = self::check_invoice_merge($invoice_id); foreach ($merge_invoice_ids as $merge_invoice) { if (isset($_REQUEST['merge_invoice'][$merge_invoice['invoice_id']])) { // copy all the tasks from that invoice over to this invoice. $sql = "UPDATE `" . _DB_PREFIX . "invoice_item` SET invoice_id = " . (int) $invoice_id . " WHERE invoice_id = " . (int) $merge_invoice['invoice_id'] . " "; query($sql); $this->delete_invoice($merge_invoice['invoice_id']); } } $_REQUEST['_redirect'] = $this->link_open($invoice_id); set_message('Invoices merged successfully'); } else { if (isset($_REQUEST['butt_email']) && $_REQUEST['butt_email']) { $_REQUEST['_redirect'] = self::link_generate($invoice_id, array('arguments' => array('email' => 1))); } else { if (isset($_REQUEST['butt_generate_credit']) && $_REQUEST['butt_generate_credit']) { // generate a credit note against this invioce. // to do this we duplicate the invoice, remove the cancel date, remove the sent date, // set a new create date, set the credit_note_id variable, remove the paid date, // (copied from the generate renewal code above) $invoice = $this->get_invoice($invoice_id); unset($invoice['invoice_id']); unset($invoice['date_renew']); unset($invoice['date_sent']); unset($invoice['date_paid']); unset($invoice['date_cancel']); unset($invoice['renew_invoice_id']); unset($invoice['deposit_job_id']); $invoice['name'] = self::new_invoice_number($invoice['customer_id']); $invoice['credit_note_id'] = $invoice_id; $invoice['date_create'] = date('Y-m-d'); $invoice['discount_amount'] = 0; $invoice['discount_description'] = _l('Discount:'); $invoice['discount_type'] = module_config::c('invoice_discount_type', _DISCOUNT_TYPE_BEFORE_TAX); // 1 = After Tax $invoice['tax_type'] = module_config::c('invoice_tax_type', 0); $invoice['date_due'] = false; $invoice['status'] = module_config::s('invoice_status_default', 'New'); $new_invoice_id = $this->save_invoice('new', $invoice); if ($new_invoice_id) { // now we create the tasks $tasks = $this->get_invoice_items($invoice_id, $invoice); foreach ($tasks as $task) { unset($task['invoice_item_id']); if ($task['custom_description']) { $task['description'] = $task['custom_description']; } if ($task['custom_long_description']) { $task['long_description'] = $task['custom_long_description']; } $task['invoice_id'] = $new_invoice_id; $task['date_done'] = $invoice['date_create']; update_insert('invoice_item_id', 'new', 'invoice_item', $task); } set_message("Credit note generated successfully"); module_cache::clear('invoice'); redirect_browser($this->link_open($new_invoice_id)); } else { set_error('Generating credit note failed'); redirect_browser($this->link_open($invoice_id)); } } else { $_REQUEST['_redirect'] = isset($_REQUEST['_redirect']) && !empty($_REQUEST['_redirect']) ? $_REQUEST['_redirect'] : $this->link_open($invoice_id); set_message("Invoice saved successfully"); } } } } } } } } } if (!count($errors)) { redirect_browser($_REQUEST['_redirect']); exit; } print_error($errors, true); }