public function test_convertExVatToIncVat()
 {
     $this->assertEquals(10.0, WebServiceRowFormatter::convertExVatToIncVat(8.0, 25));
     $this->assertEquals(69.98999999999999 * 1.25, WebServiceRowFormatter::convertExVatToIncVat(69.98999999999999, 25));
     $this->assertEquals(0, WebServiceRowFormatter::convertExVatToIncVat(0, 0));
     $this->assertEquals(1, WebServiceRowFormatter::convertExVatToIncVat(1, 0));
     $this->assertEquals(0, WebServiceRowFormatter::convertExVatToIncVat(0, 25));
     $this->assertEquals(100.0 * 1.06, WebServiceRowFormatter::convertExVatToIncVat(100.0, 6));
     $this->assertEquals(\Svea\Helper::bround(100.0 * 1.0825, 2), WebServiceRowFormatter::convertExVatToIncVat(100.0, 8.25));
 }
 public function test_incvat_order_with_fixedDiscount_as_incvat_only_has_priceIncludingVat_true()
 {
     $order = FixedDiscountRowsIntegrationTest::create_only_incvat_order_and_fee_rows_order();
     $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(10.0)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 10i"));
     $request = $order->useInvoicePayment()->prepareRequest();
     // all order rows
     $this->assertEquals(72.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit);
     $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat);
     $this->assertEquals(33.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat);
     // all invoice fee rows
     $this->assertEquals(8.800000000000001, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat);
     // all shipping fee rows
     $this->assertEquals(17.6, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat);
     // all discount rows
     // expected: fixedDiscount: 10 incvat => split across 10i *(72/72+33) @20% + 10i *(33/72+33) @10% => 6.8571i @20% + 3.1428i @10% =>5.71 + 2.86
     $this->assertEquals(-6.86, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit), 2, PHP_ROUND_HALF_UP);
     //=WS
     $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat);
     $this->assertEquals(-3.14, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PricePerUnit), 2, PHP_ROUND_HALF_UP);
     //=WS
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PriceIncludingVat);
     // check that service accepts order
     $response = $order->useInvoicePayment()->doRequest();
     $this->assertEquals(true, $response->accepted);
     // r() is round($val, 2, PHP_ROUND_HALF_EVEN), i.e. bankers rounding
     // r(72*1) + r(33*1) + r(17.60*1) + r(8.80*1) + r(-5.72*1.20*1) + r(-2.85*1.10*1) => 72.00+33.00+17.60+8.80-6.86-3.14 => 121.40
     $this->assertEquals("121.40", $response->amount);
 }
 public function formatRelativeDiscountRows($row)
 {
     $tempRow = new HostedOrderRowBuilder();
     if (isset($row->name)) {
         $tempRow->setName($row->name);
     }
     if (isset($row->description)) {
         $tempRow->setDescription($row->description);
     }
     if (isset($row->discountId)) {
         $tempRow->setSku($row->discountId);
     }
     if (isset($row->unit)) {
         $tempRow->setUnit($row->unit);
     }
     $rawAmount = $this->rawAmount / 100 * $row->discountPercent / 100;
     $rawVat = $this->rawVat / 100 * $row->discountPercent / 100;
     $tempRow->setAmount(-\Svea\Helper::bround($rawAmount, 2) * 100);
     $tempRow->setVat(-\Svea\Helper::bround($rawVat, 2) * 100);
     $tempRow->setQuantity(1);
     $this->totalAmount += $tempRow->amount;
     $this->totalVat += $tempRow->vat;
     $this->newRows[] = $tempRow;
     $this->discountAmount += $tempRow->amount;
     $this->discountVat += $tempRow->vat;
 }
 public function test_getOrderTotals_has_same_amounts_as_service_when_order_sent_priceIncludingVat_true()
 {
     $order = GetRequestTotalsTest::create_only_incvat_order_and_fee_rows_order();
     $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(8.0)->setVatPercent(20)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 8.00i@20%"))->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(3.67)->setVatPercent(10)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 3.67ie@10%"));
     $request = $order->useInvoicePayment()->prepareRequest();
     // all order rows
     $this->assertEquals(72.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit);
     $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat);
     $this->assertEquals(33.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat);
     // all invoice fee rows
     $this->assertEquals(8.800000000000001, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat);
     // all shipping fee rows
     $this->assertEquals(17.6, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit);
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat);
     // all discount rows
     // expected: fixedDiscount: 10 exvat => split across 10e *(60/60+30) @20% + 10e *(30/60+30) @10% => 6.6666e @20% + 3.3333e @10% => 8.00i + 3.67i
     $this->assertEquals(-8.0, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit), 2, PHP_ROUND_HALF_UP);
     //=WS
     $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat);
     $this->assertEquals(-3.67, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PricePerUnit), 2, PHP_ROUND_HALF_UP);
     //=WS
     $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->VatPercent);
     $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PriceIncludingVat);
     // check that service accepts order
     //$response = $order->useInvoicePayment()->doRequest();
     //$this->assertEquals( true, $response->accepted );
     //// r() is round($val, 2, PHP_ROUND_HALF_EVEN), i.e. bankers rounding
     //// r(72.00*1) + r(33.00*1) + r(17.60*1) + r(8.80*1) + r(-8.00*1) + r(-3.66*1) => 72.00+33.00+17.60+8.80-8.00-3.67 => 119.73
     ////$this->assertEquals( "119.73", $response->amount );     // TODO check that this is the amount in S1 invoice, vs 119.74 w/PriceIncludingVat = false
     //$this->assertEquals( "119.73", $response->amount );     // jfr vs 119.73 w/PriceIncludingVat = true
     // verify that getRequestTotals() got the same amount as the service
     $preview = $order->useInvoicePayment()->getRequestTotals();
     $this->assertEquals("119.73", $preview['total_incvat']);
     //$this->assertEquals( $preview['total_incvat'], $response->amount );
 }
 private function formatFixedDiscountRows($row)
 {
     // only amountIncVat (i.e. amount) was specified:
     if (isset($row->amount) && !isset($row->vatPercent) && !isset($row->amountExVat)) {
         $this->newRows = array_merge($this->newRows, $this->formatFixedDiscountSpecifiedAsAmountIncVatOnly($row));
     }
     // only amountExVat was specified:
     if (!isset($row->amount) && !isset($row->vatPercent) && isset($row->amountExVat)) {
         $this->newRows = array_merge($this->newRows, $this->formatFixedDiscountSpecifiedAsAmountExVatOnly($row));
     }
     // amountIncVat (i.e. amount) and vatPercent is set, so we use that vatPercent:
     if (isset($row->amount) && isset($row->vatPercent) && !isset($row->amountExVat)) {
         $orderRow = new WebServiceSoap\SveaOrderRow();
         if (isset($row->discountId)) {
             $orderRow->ArticleNumber = $row->discountId;
         }
         $orderRow->Description = $this->formatRowNameAndDescription($row);
         if (isset($row->unit)) {
             $orderRow->Unit = $row->unit;
         }
         $orderRow->DiscountPercent = 0;
         //no discount on discount
         $orderRow->NumberOfUnits = 1;
         //only one discount per row
         //calculate discount
         $vatRate = $row->vatPercent;
         $discountAtThisVatRateIncVat = $row->amount;
         //                    $discountAtThisVatRateExVat =
         //                            WebServiceRowFormatter::convertIncVatToExVat( $discountAtThisVatRateIncVat, $vatRate );
         $orderRow->PricePerUnit = -1 * ($this->priceIncludingVat ? $discountAtThisVatRateIncVat : WebServiceRowFormatter::convertIncVatToExVat($row->amount, \Svea\Helper::bround($row->vatPercent)));
         $orderRow->VatPercent = $vatRate;
         $orderRow->PriceIncludingVat = $this->priceIncludingVat ? TRUE : FALSE;
         $this->newRows[] = $orderRow;
     }
     // amountExVat (i.e. amount) and vatPercent is set, so we use that vatPercent:
     if (!isset($row->amount) && isset($row->vatPercent) && isset($row->amountExVat)) {
         $orderRow = new WebServiceSoap\SveaOrderRow();
         if (isset($row->discountId)) {
             $orderRow->ArticleNumber = $row->discountId;
         }
         $orderRow->Description = $this->formatRowNameAndDescription($row);
         if (isset($row->unit)) {
             $orderRow->Unit = $row->unit;
         }
         $orderRow->DiscountPercent = 0;
         //no discount on discount
         $orderRow->NumberOfUnits = 1;
         //only one discount per row
         //calculate discount
         $vatRate = $row->vatPercent;
         $discountAtThisVatRate = $this->priceIncludingVat ? WebServiceRowFormatter::convertExVatToIncVat($row->amountExVat, \Svea\Helper::bround($row->vatPercent)) : $row->amountExVat;
         $orderRow->PricePerUnit = -1 * $discountAtThisVatRate;
         $orderRow->VatPercent = $vatRate;
         $orderRow->PriceIncludingVat = $this->priceIncludingVat ? TRUE : FALSE;
         $this->newRows[] = $orderRow;
     }
 }
 private function serializeOrderRow($orderRow)
 {
     $this->XMLWriter->startElement("row");
     if (!empty($orderRow->sku) && $orderRow->sku != null) {
         $this->XMLWriter->writeElement("sku", $orderRow->sku);
     } else {
         $this->XMLWriter->writeElement("sku", "");
     }
     if (!empty($orderRow->name) && $orderRow->name != null) {
         $this->XMLWriter->writeElement("name", $orderRow->name);
     } else {
         $this->XMLWriter->writeElement("name", "");
     }
     if (!empty($orderRow->description) && $orderRow->description != null) {
         $this->XMLWriter->writeElement("description", $orderRow->description);
     } else {
         $this->XMLWriter->writeElement("description", "");
     }
     if (!empty($orderRow->amount) && $orderRow->amount != null) {
         $this->XMLWriter->writeElement("amount", \Svea\Helper::bround($orderRow->amount));
     } else {
         $this->XMLWriter->writeElement("amount", "0");
     }
     if (!empty($orderRow->vat) && $orderRow->vat != null) {
         $this->XMLWriter->writeElement("vat", \Svea\Helper::bround($orderRow->vat));
     } else {
         $this->XMLWriter->writeElement("vat", "0");
     }
     if (!empty($orderRow->quantity) && $orderRow->quantity != null) {
         $this->XMLWriter->writeElement("quantity", $orderRow->quantity);
     }
     if (!empty($orderRow->unit) && $orderRow->unit != null) {
         $this->XMLWriter->writeElement("unit", $orderRow->unit);
     }
     $this->XMLWriter->endElement();
 }
 function calculateVatPercentFromVatAndAmount($vat, $amount)
 {
     $amountExVat = $amount - $vat;
     $unroundedVatPercent = $amountExVat > 0 ? $vat / $amountExVat : 0.0;
     // catch potential divide by zero
     $vatPercent = \Svea\Helper::bround($unroundedVatPercent, 2) * 100;
     // OrderRow has vatpercent as int.
     return $vatPercent;
 }
 private function calculateTotalVatSumOfRows($row)
 {
     //if amount inc vat
     $sum = 0;
     //calculate the exvat sum
     if ($row->PriceIncludingVat == true) {
         $rowsum_incvat = $this->getRowAmount($row);
         $exvat = $this->convertIncVatToExVat($row, $rowsum_incvat);
         $vat = \Svea\Helper::bround($rowsum_incvat, 2) - \Svea\Helper::bround($exvat, 2);
         $sum += $vat;
     } else {
         $exvat = $this->getRowAmount($row);
         $vat = \Svea\Helper::bround($exvat, 2) * ($row->VatPercent / 100);
         $sum += \Svea\Helper::bround($vat, 2);
     }
     //        $vat = \Svea\Helper::bround($exvat,2) * ($row->VatPercent / 100 );
     //        $sum += intval(100.00 * $vat) / 100.00; //php for .NET Math.Truncate -- round to nearest integer towards zero
     return $sum;
 }