Skip to content

Instantly share code, notes, and snippets.

@anish-dcruz
Last active June 14, 2023 19:15
Show Gist options
  • Save anish-dcruz/8be34c1c44d569b2966560d7f875fcc3 to your computer and use it in GitHub Desktop.
Save anish-dcruz/8be34c1c44d569b2966560d7f875fcc3 to your computer and use it in GitHub Desktop.
Advanced Laravel 1

Advanced Laravel 1

Instructions

You are given two files: 1. LineItemTest.php 2. LineItem.php

Complete the 'LineItem.php' class using 'LineItemTest.php' as a reference, ensuring that all tests pass.

No test cases should be removed, but additional tests may be added if necessary.

Please refer to the link below to obtain the formulas necessary to complete this exercise.

https://support.avaza.com/what-difference-does-the-tax-inclusive-tax-exclusive-setting-make/

Methods to implement:

1. `__construct`
	pass in the input and other information necessary, see all the test cases
2. `subTotal`
	which which equals unit price * qty
3. `netPrice`
	also known as "Amount Before Tax," see the link for formulas for both tax inclusive and exclusive
4. `discountAmount`
	refer to the link for the formula; simply return 'discount value' if the 'discount type' is not percent.
5. `taxAmount`
	see the link to find the formula
6. `totalAmount
	see the link to find the formula

Notes:

1. Discounts are offered in two ways.
	- percent
	- value (amount)
2. Taxes are classified into two categories: inclusive and exclusive.
3. By default the "line item" is tax exclusive (test cases 1 and 2)

Ensure that you handle all the cases.

Tips:

1. To obtain accurate results, netPrice and taxAmount are rounded to two decimal places (verify yourself)
  1. (Discount %) = ($discount_value / 100)
  2. (Tax %) = ($tax_percent / 100)
  3. Include brackets wherever necessary to obtain the correct calculation.

Finally, submit 'LineItem.php' privately (Make sure your answer is not public)

<?php
class LineItem {
}
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class LineItemTest extends TestCase
{
public function test_line_item_default()
{
$inputLine = [
'item_id' => 1,
'description' => 'desc_1',
'unit_price' => 12,
'qty' => 1,
'discount_type' => 'percent',
'discount_value' => 20,
'tax_percent' => 20
];
$line = new LineItem($inputLine);
$this->assertEquals(12, $line->subTotal());
$this->assertEquals(2.4, $line->discountAmount());
$this->assertEquals(9.6, $line->netPrice());
$this->assertEquals(1.92, $line->taxAmount());
$this->assertEquals(11.52, $line->totalAmount());
}
public function test_line_item_default_2()
{
$inputLine = [
'item_id' => 1,
'description' => 'desc_1',
'unit_price' => 10,
'qty' => 1,
'discount_type' => 'percent',
'discount_value' => 20,
'tax_percent' => 20
];
$line = new LineItem($inputLine);
$this->assertEquals(10, $line->subTotal());
$this->assertEquals(2, $line->discountAmount());
$this->assertEquals(8, $line->netPrice());
$this->assertEquals(1.6, $line->taxAmount());
$this->assertEquals(9.6, $line->totalAmount());
}
public function test_line_item_tax_type_exclusive_discount_value()
{
$inputLine = [
'item_id' => 1,
'description' => 'desc_1',
'unit_price' => 100,
'qty' => 1,
'discount_type' => 'value',
'discount_value' => 5,
'tax_percent' => 10
];
$document = [
'tax_type' => 'exclusive'
];
$line = new LineItem($inputLine, $document);
$this->assertEquals(100, $line->subTotal());
$this->assertEquals(5, $line->discountAmount());
$this->assertEquals(95, $line->netPrice());
$this->assertEquals(9.5, $line->taxAmount());
$this->assertEquals(104.5, $line->totalAmount());
}
public function test_line_item_tax_type_exclusive()
{
$inputLine = [
'item_id' => 1,
'description' => 'desc_1',
'unit_price' => 100,
'qty' => 1,
'discount_type' => 'percent',
'discount_value' => 5,
'tax_percent' => 10
];
$document = [
'tax_type' => 'exclusive'
];
$line = new LineItem($inputLine, $document);
$this->assertEquals(100, $line->subTotal());
$this->assertEquals(5, $line->discountAmount());
$this->assertEquals(95, $line->netPrice());
$this->assertEquals(9.5, $line->taxAmount());
$this->assertEquals(104.5, $line->totalAmount());
}
public function test_line_item_tax_type_inclusive()
{
$inputLine = [
'item_id' => 1,
'description' => 'desc_1',
'unit_price' => 100,
'qty' => 1,
'discount_type' => 'percent',
'discount_value' => 5,
'tax_percent' => 10
];
$document = [
'tax_type' => 'inclusive'
];
$line = new LineItem($inputLine, $document);
$this->assertEquals(100, $line->subTotal());
$this->assertEquals(5, $line->discountAmount());
$this->assertEquals(86.36, $line->netPrice());
$this->assertEquals(8.64, $line->taxAmount());
$this->assertEquals(95, $line->totalAmount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment