コード例 #1
0
ファイル: InvoiceItemsTest.php プロジェクト: pscheit/psc-cms
 public function testAcceptance()
 {
     $invoiceItems = new InvoiceItems();
     $invoiceItems->addItem($bananen = new SimpleInvoiceItem('mehrere Bananen (pauschalpreis)', new Price(2.5, Price::NETTO, 0.19)));
     $this->assertEquals(2, $invoiceItems->getNextPosition());
     // does not increase
     $invoiceItems->addItem($aepfel = new SimpleInvoiceItem('Äpfel (pauschalpreis)', new Price(7.5, Price::BRUTTO, 0.19)));
     $this->assertCount(2, $invoiceItems->unwrap());
     $this->assertEquals(1, $bananen->getPos());
     $this->assertEquals(2, $aepfel->getPos());
     $this->assertEquals(array(1 => $bananen, 2 => $aepfel), $invoiceItems->toArray());
 }
コード例 #2
0
 /**
  * Delete all items for a invoice
  *
  * @param Invoice $invoice
  * @return null
  */
 function deleteByInvoice($invoice)
 {
     db_begin_work();
     $execute = db_execute('DELETE FROM ' . TABLE_PREFIX . 'invoice_time_records WHERE invoice_id = ?', $invoice->getId());
     if ($execute && !is_error($execute)) {
         $delete = InvoiceItems::delete(array('invoice_id = ?', $invoice->getId()));
         if ($delete && !is_error($delete)) {
             db_commit();
         } else {
             db_rollback();
         }
         // if
         return $delete;
     } else {
         db_rollback();
         return $execute;
     }
     // if
 }
コード例 #3
0
 /**
  * Delete existing invoice from database
  *
  * @param void
  * @return boolean
  */
 function delete()
 {
     db_begin_work();
     $delete = parent::delete();
     if ($delete && !is_error($delete)) {
         InvoiceItems::deleteByInvoice($this);
         InvoicePayments::deleteByInvoice($this);
         db_commit();
         return true;
     } else {
         db_rollback();
         return $delete;
     }
     // if
 }
コード例 #4
0
 /**
  * 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
 }
コード例 #5
0
 /**
  * Returns true if $user can delete this tax rate
  *
  * @param User $user
  * @return boolean
  */
 function canDelete($user)
 {
     return $user->isAdministrator() && InvoiceItems::countByTaxRate($this) == 0;
 }