Example #1
0
 /**
  * Receives the Invoice form data and calculates each row total amount and
  * full invoice totals. Returns a json
  *
  * @param 'invoice' from Request
  * @return JSON through Response
  */
 public function executeCalculate(sfWebRequest $request)
 {
     $currency = PropertyTable::get('currency');
     $format = new sfNumberFormat($this->culture);
     $data = $request->getParameter('invoice');
     $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
     $invoice = new Invoice();
     $items = array();
     $totals = array();
     if (isset($data['Items'])) {
         foreach ((array) $data['Items'] as $itemId => $itemData) {
             if ($itemData['remove']) {
                 continue;
             }
             $item = new Item();
             $item->setUnitaryCost($itemData['unitary_cost']);
             $item->setQuantity($itemData['quantity']);
             $item->setDiscount($itemData['discount']);
             if (isset($itemData['taxes_list'])) {
                 $taxes = Doctrine::getTable('Tax')->createQuery()->whereIn('id', $itemData['taxes_list'])->execute();
                 $item->Taxes = $taxes;
             }
             $items[$itemId] = $format->format($item->getGrossAmount(), 'c', $currency);
             $invoice->Items[] = $item;
         }
         $totals['base'] = $format->format($invoice->calculate('base_amount', true), 'c', $currency);
         $totals['discount'] = $format->format($invoice->calculate('discount_amount', true), 'c', $currency);
         $totals['net'] = $format->format($invoice->calculate('net_amount', true), 'c', $currency);
         $totals['taxes'] = $format->format($invoice->calculate('tax_amount', true), 'c', $currency);
         $totals['gross'] = $format->format($invoice->calculate('gross_amount', true), 'c', $currency);
     } else {
         $zero = $format->format(0, 'c', $currency);
         $totals['base'] = $zero;
         $totals['discount'] = $zero;
         $totals['net'] = $zero;
         $totals['taxes'] = $zero;
         $totals['gross'] = $zero;
     }
     return $this->renderText(json_encode(array('items' => $items, 'totals' => $totals)));
 }
 protected function generateInvoiceItems()
 {
     for ($j = 0; $j < mt_rand(1, 10); $j++) {
         $item = new Item();
         $item->setDescription($this->items[array_rand($this->items)]);
         $item->setUnitaryCost(mt_rand(100, 100000) / 100);
         $item->setQuantity(mt_rand(1, 10));
         if (mt_rand(1, 15) == 1) {
             $item->setDiscount(mt_rand(1, 70));
         }
         $max_tax = mt_rand(1, 10) == 1 ? mt_rand(1, 3) : 1;
         for ($kk = 0; $kk < $max_tax; $kk++) {
             $item->Taxes[] = $this->getRandomTax();
         }
         $this->inv->Items[] = $item;
     }
 }
Example #3
0
<?php

include dirname(__FILE__) . '/../../bootstrap/Doctrine.php';
$t = new lime_test(6, new lime_output_color());
$t->diag('Item class tests');
$item = new Item();
$item->setUnitaryCost(1234.214);
$item->setQuantity(3);
$item->setDiscount(13);
$tax1 = new Tax();
$tax2 = new Tax();
$tax1->setValue(16);
$tax2->setValue(4);
$item->Taxes[] = $tax1;
$item->Taxes[] = $tax2;
$base = 1234.214 * 3;
$discount = $base * 13 / 100;
$taxAmount = ($base - $discount) * ($tax1->value + $tax2->value) / 100;
$t->is($item->getBaseAmount(), $base, 'getBaseAmount()');
$t->is($item->getNetAmount(), $base - $discount, 'getNetAmount()');
$t->is($item->getDiscountAmount(), $discount, 'getDiscountAmount()');
$t->is($item->getTaxAmount(), $taxAmount, 'getTaxAmount()');
$t->is($item->getGrossAmount(), $base - $discount + $taxAmount, 'getGrossAmount()');
$t->is($item->getTaxesPercent(), $tax1->value + $tax2->value, 'getTaxesPercent()');