/**
  * Find all predefined invoice items and sort them by position
  *
  */
 function findAll()
 {
     return InvoiceItemTemplates::find(array('order' => 'ISNULL(position) ASC, position, id ASC'));
 }
 /**
  * Predefined items main page
  * 
  * @param void
  * @return null
  */
 function index()
 {
     $this->wireframe->addPageAction(lang('New Invoice Item'), assemble_url('admin_invoicing_item_add'));
     $this->smarty->assign(array('invoice_item_templates' => InvoiceItemTemplates::findAll(), 'reorder_item_templates_url' => assemble_url('admin_invoicing_items_reorder')));
 }
 /**
  * Update existing invoice
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_invoice->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_invoice->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $invoice_data = $this->request->post('invoice');
     if (!is_array($invoice_data)) {
         $invoice_data = array('number' => $this->active_invoice->getNumber(), 'due_on' => $this->active_invoice->getDueOn(), 'issued_on' => $this->active_invoice->getIssuedOn(), 'currency_id' => $this->active_invoice->getCurrencyId(), 'comment' => $this->active_invoice->getComment(), 'company_id' => $this->active_invoice->getCompanyId(), 'company_address' => $this->active_invoice->getCompanyAddress(), 'project_id' => $this->active_invoice->getProjectId(), 'note' => $this->active_invoice->getNote(), 'language_id' => $this->active_invoice->getLanguageId());
         if (is_foreachable($this->active_invoice->getItems())) {
             $invoice_data['items'] = array();
             foreach ($this->active_invoice->getItems() as $item) {
                 $invoice_data['items'][] = array('description' => $item->getDescription(), 'unit_cost' => $item->getUnitCost(), 'quantity' => $item->getQuantity(), 'tax_rate_id' => $item->getTaxRateId(), 'total' => $item->getTotal(), 'subtotal' => $item->getSubtotal(), 'time_record_ids' => $item->getTimeRecordIds());
             }
             // foreach
         }
         // if
     }
     // if
     $invoice_notes = InvoiceNoteTemplates::findAll();
     $invoice_item_templates = InvoiceItemTemplates::findAll();
     $this->smarty->assign(array('invoice_data' => $invoice_data, 'invoice_item_templates' => $invoice_item_templates, 'tax_rates' => TaxRates::findAll(), 'invoice_notes' => $invoice_notes, 'original_note' => $this->active_invoice->getNote()));
     $cleaned_notes = array();
     if (is_foreachable($invoice_notes)) {
         foreach ($invoice_notes as $invoice_note) {
             $cleaned_notes[$invoice_note->getId()] = $invoice_note->getContent();
         }
         // foreach
     }
     // if
     js_assign('invoice_notes', $cleaned_notes);
     js_assign('original_note', $this->active_invoice->getNote());
     $cleaned_item_templates = array();
     if (is_foreachable($invoice_item_templates)) {
         foreach ($invoice_item_templates as $invoice_item_template) {
             $cleaned_item_templates[$invoice_item_template->getId()] = array('description' => $invoice_item_template->getDescription(), 'unit_cost' => $invoice_item_template->getUnitCost(), 'quantity' => $invoice_item_template->getQuantity(), 'tax_rate_id' => $invoice_item_template->getTaxRateId());
         }
         // foreach
     }
     // if
     js_assign('invoice_item_templates', $cleaned_item_templates);
     js_assign('company_details_url', assemble_url('invoice_company_details'));
     js_assign('move_icon_url', get_image_url('move.gif'));
     if ($this->request->isSubmitted()) {
         $this->active_invoice->setAttributes($invoice_data);
         $invoice_company = Companies::findById(array_var($invoice_data, 'company_id', null));
         $this->active_invoice->setCompanyName($invoice_company->getName());
         $save = $this->active_invoice->save();
         if ($save && !is_error($save)) {
             InvoiceItems::deleteByInvoice($this->active_invoice);
             $counter = 1;
             if (is_foreachable($invoice_data['items'])) {
                 foreach ($invoice_data['items'] as $invoice_item_data) {
                     $invoice_item = new InvoiceItem();
                     $invoice_item->setAttributes($invoice_item_data);
                     $invoice_item->setInvoiceId($this->active_invoice->getId());
                     $invoice_item->setPosition($counter);
                     $item_save = $invoice_item->save();
                     if ($item_save && !is_error($item_save)) {
                         $invoice_item->setTimeRecordIds(array_var($invoice_item_data, 'time_record_ids', null));
                         $counter++;
                     } else {
                         $this->smarty->assign('errors', new ValidationErrors(array('items' => lang('Invoice items data is not valid. All descriptions are required and there need to be at least one unit with cost set per item!'))));
                     }
                     // if
                 }
                 // foreach
                 flash_success('":number" has been updated', array('number' => $this->active_invoice->getName()));
                 if ($this->active_invoice->isIssued()) {
                     $invoice_company = $this->active_invoice->getCompany();
                     if (instance_of($invoice_company, 'Company') && $invoice_company->hasManagers()) {
                         $this->redirectTo('invoice_notify', array('invoice_id' => $this->active_invoice->getId()));
                     }
                     // if
                 }
                 // if
                 $this->redirectToUrl($this->active_invoice->getViewUrl());
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     }
     // if
 }