|
| 1 | +<?php |
| 2 | +use josemmo\Facturae\Facturae; |
| 3 | +use josemmo\Facturae\FacturaeItem; |
| 4 | +use josemmo\Facturae\FacturaeParty; |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +final class DiscountsTest extends TestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Get base invoice |
| 11 | + * @return Facturae Base invoice |
| 12 | + */ |
| 13 | + private function _getBaseInvoice() { |
| 14 | + $fac = new Facturae(); |
| 15 | + $fac->setNumber('EMP201712', '0003'); |
| 16 | + $fac->setIssueDate('2017-12-01'); |
| 17 | + $fac->setSeller(new FacturaeParty([ |
| 18 | + "taxNumber" => "A00000000", |
| 19 | + "name" => "Perico el de los Palotes S.A.", |
| 20 | + "address" => "C/ Falsa, 123", |
| 21 | + "postCode" => "12345", |
| 22 | + "town" => "Madrid", |
| 23 | + "province" => "Madrid" |
| 24 | + ])); |
| 25 | + $fac->setBuyer(new FacturaeParty([ |
| 26 | + "isLegalEntity" => false, |
| 27 | + "taxNumber" => "00000000A", |
| 28 | + "name" => "Antonio", |
| 29 | + "firstSurname" => "García", |
| 30 | + "lastSurname" => "Pérez", |
| 31 | + "address" => "Avda. Mayor, 7", |
| 32 | + "postCode" => "54321", |
| 33 | + "town" => "Madrid", |
| 34 | + "province" => "Madrid" |
| 35 | + ])); |
| 36 | + return $fac; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Test invoice item discounts |
| 42 | + */ |
| 43 | + public function testItemDiscounts() { |
| 44 | + $fac = $this->_getBaseInvoice(); |
| 45 | + $expectedGrossAmounts = []; |
| 46 | + |
| 47 | + // Add first item |
| 48 | + $fac->addItem(new FacturaeItem([ |
| 49 | + "name" => "First item", |
| 50 | + "unitPriceWithoutTax" => 100, |
| 51 | + "discounts" => [ |
| 52 | + ["reason"=>"Half price", "rate"=>50] |
| 53 | + ], |
| 54 | + "taxes" => [Facturae::TAX_IVA => 10] |
| 55 | + ])); |
| 56 | + $expectedGrossAmounts[] = 50; |
| 57 | + |
| 58 | + // Add second item |
| 59 | + $fac->addItem(new FacturaeItem([ |
| 60 | + "name" => "Second item", |
| 61 | + "unitPriceWithoutTax" => 100, |
| 62 | + "discounts" => [ |
| 63 | + ["reason"=>"Half price", "rate"=>50], |
| 64 | + ["reason"=>"5€ off", "amount"=>5] |
| 65 | + ], |
| 66 | + "charges" => [ |
| 67 | + ["reason"=>"Twice as much", "rate"=>50] |
| 68 | + ], |
| 69 | + "taxes" => [Facturae::TAX_IVA => 10] |
| 70 | + ])); |
| 71 | + $expectedGrossAmounts[] = 95; |
| 72 | + |
| 73 | + // Add third item |
| 74 | + $fac->addItem(new FacturaeItem([ |
| 75 | + "name" => "Third item", |
| 76 | + "quantity" => 2, |
| 77 | + "unitPrice" => 100, |
| 78 | + "discounts" => [ |
| 79 | + ["reason"=>"Half price", "rate"=>50] |
| 80 | + ], |
| 81 | + "taxes" => [Facturae::TAX_IVA => 0] |
| 82 | + ])); |
| 83 | + $expectedGrossAmounts[] = 100; |
| 84 | + |
| 85 | + // Add fourth item |
| 86 | + $fac->addItem(new FacturaeItem([ |
| 87 | + "name" => "Fourth item", |
| 88 | + "unitPrice" => 100, |
| 89 | + "discounts" => [ |
| 90 | + ["reason"=>"Half price", "rate"=>50] |
| 91 | + ], |
| 92 | + "charges" => [ |
| 93 | + ["reason"=>"Extra €5 (tax. included)", "amount"=>5], |
| 94 | + ["reason"=>"Extra €10", "amount"=>10, "hasTaxes"=>false], |
| 95 | + ], |
| 96 | + "taxes" => [Facturae::TAX_IVA => 25] |
| 97 | + ])); |
| 98 | + $expectedGrossAmounts[] = (100 / 1.25)*0.5 + (5 / 1.25) + 10; |
| 99 | + |
| 100 | + // Generate invoice and validate output |
| 101 | + $invoiceXml = new \SimpleXMLElement($fac->export()); |
| 102 | + $invoiceXml = $invoiceXml->Invoices->Invoice[0]; |
| 103 | + foreach ($invoiceXml->Items->InvoiceLine as $item) { |
| 104 | + $itemGross = floatval($item->GrossAmount); |
| 105 | + $expectedGross = array_shift($expectedGrossAmounts); |
| 106 | + $this->assertEquals($itemGross, $expectedGross, '', 0.00001); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments