/** * Issue invoice * * @param void * @return null */ function issue() { $this->wireframe->print_button = false; if ($this->active_invoice->isNew()) { $this->httpError(HTTP_ERR_NOT_FOUND); } // if if (!$this->active_invoice->canIssue($this->logged_user)) { $this->httpError(HTTP_ERR_FORBIDDEN); } // if $company = $this->active_invoice->getCompany(); if (!instance_of($company, 'Company')) { $this->httpError(HTTP_ERR_CONFLICT); } // if $issue_data = $this->request->post('issue'); if (!is_array($issue_data)) { $issue_data = array('issued_on' => new DateValue(), 'due_on' => new DateValue('+7 days')); } // if $company_managers = Users::findCompanyManagers($company); if (empty($company_managers)) { $this->wireframe->addPageMessage(lang('Sorry, there\'s nobody in :company_name to whom we can email the invoice', array('company_name' => $company->getName())), 'warning'); } // if $this->smarty->assign(array('company_managers' => $company_managers, 'users' => $company->getUsers(), 'company' => $company, 'issue_data' => $issue_data)); if ($this->request->isSubmitted()) { db_begin_work(); $issued_on = isset($issue_data['issued_on']) ? new DateValue($issue_data['issued_on']) : new DateValue(); $due_on = isset($issue_data['due_on']) ? new DateValue($issue_data['due_on']) : null; $this->active_invoice->setStatus(INVOICE_STATUS_ISSUED, $this->logged_user, $issued_on); if ($due_on) { $this->active_invoice->setDueOn($due_on); } // if $issue_to = null; $user_id = array_var($issue_data, 'user_id'); if ($user_id) { $user = Users::findById($user_id); if (instance_of($user, 'User')) { $this->active_invoice->setIssuedToId($user->getId()); $issue_to = array($user); if ($user->getId() != $this->logged_user->getId()) { $issue_to[] = $this->logged_user; } // if } // if } // if $autogenerated = false; if (!$this->active_invoice->getNumber()) { $autogenerated = true; $this->active_invoice->setNumber($this->active_invoice->generateInvoiceId()); } // if $save = $this->active_invoice->save(); if ($save && !is_error($save)) { if ($autogenerated) { Invoices::incrementDateInvoiceCounters(); } // if if (isset($issue_data['send_emails']) && $issue_data['send_emails']) { if ($issue_to) { $filename_name = 'invoice_' . $this->active_invoice->getId() . '.pdf'; $filename = WORK_PATH . '/' . $filename_name; require_once INVOICING_MODULE_PATH . '/models/InvoicePdf.class.php'; InvoicePDF::save($this->active_invoice, $filename); ApplicationMailer::send($issue_to, 'invoicing/issue', array('issued_by_name' => $this->logged_user->getDisplayName(), 'issued_by_url' => $this->logged_user->getViewUrl(), 'invoice_number' => $this->active_invoice->getNumber(), 'invoice_url' => $this->active_invoice->getCompanyViewUrl(), 'pdf_url' => $this->active_invoice->getCompanyPdfUrl()), null, array(array('path' => $filename))); @unlink($filename); } // if } // if db_commit(); flash_success('Invoice has been issued'); $this->redirectToUrl($this->active_invoice->getViewUrl()); } else { db_rollback(); $this->smarty->assign('errors', $issue); } // if } // if }