Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to Add Discounts and Taxes to Transactions #29

Merged
merged 16 commits into from
Feb 23, 2023

Conversation

risangbaskoro
Copy link
Collaborator

@risangbaskoro risangbaskoro commented Feb 22, 2023

This PR should close #16.

The ability to assign discounts and taxes in Kasir object.

Discounts

To add a discount, call thediscount() method and pass the parameters to create a discount, This method will also automatically calculate the gross_amount.

The available parameters for discount are:

  • amount: int required The amount of the discount, in fixed amount or percentage of the gross_amount.
  • percentage: bool Determines if the amount is in percentage.
  • name: string The name of the discount. This will be displayed in the Midtrans Merchant Account Portal.
  • id: string The ID of the discount. This will be displayed in the Midtrans Merchant Account Portal.

Adding a Discount

$kasir = Kasir::make()
    ->itemDetails($items)
    ->discount(10, true, 'Voucher GoPay', 'voucher-gopay');

Note
The example above will assign an array of discount to the discounts property and will automatically calculate the gross_amount.

Adding Multiple Discounts

To add multiple discounts, chain the discount() method:

$kasir = Kasir::make()
    ->itemDetails($items)
    ->discount(10, true, 'Voucher GoPay', 'voucher-gopay')
    ->discount(2, true, 'Promo Pengguna Baru', 'new-user');

Note
The example above will create two discounts; first is a 10% discount, and second is a 2% discount calculated after the first discount is applied.

Warning
The gross_amount will be calculated in the order in which the discount is called.

You can also add multiple discounts by using discounts() method and passing an array of discounts with each discount parameters as the keys.

$kasir = Kasir::make()
    ->itemDetails($items)
    ->discounts([
        [
            'id' => 'voucher-gopay',
            'name' => 'Voucher GoPay',
            'amount' => 10,
            'percentage' => true,
        ], [
            'id' => 'new-user',
            'name' => 'Promo Pengguna Baru',
            'amount' => 2,
            'percentage' => true,
        ]
    ]);

or pass an anonymous function:

$kasir = Kasir::make()
    ->itemDetails($items)
    ->discounts(function() {
        return [
            [
                'id' => 'voucher-gopay',
                'name' => 'Voucher GoPay',
                'amount' => 10,
                'percentage' => true,
            ], [
                'id' => 'new-user',
                'name' => 'Promo Pengguna Baru',
                'amount' => 2,
                'percentage' => true,
            ]
        ];
    });

Taxes

Similar to adding discounts, adding taxes is done by calling tax() or taxes() method and passing the parameters:

$kasir = Kasir::make()
    ->itemDetails($items)
    ->tax(1000, false, 'Biaya Metode Pembayaran', 'snap-fee')
    ->tax(11, true, 'PPN 11%', 'ppn');

or using an array of taxes:

$kasir = Kasir::make()
    ->itemDetails($items)
    ->taxes([
        [
            'id' => 'snap-fee',
            'name' => 'Biaya Metode Pembayaran',
            'amount' => 1000,
            'percentage' => false,
        ], [
            'id' => 'ppn',
            'name' => 'PPN 11%',
            'amount' => 11,
            'percentage' => true,
        ],
    ]);

or using an anonymous function:

$kasir = Kasir::make()
    ->itemDetails($items)
    ->taxes(function () {
        return [
            [
                'id' => 'snap-fee',
                'name' => 'Biaya Metode Pembayaran',
                'amount' => 1000,
                'percentage' => false,
            ], [
                'id' => 'ppn',
                'name' => 'PPN 11%',
                'amount' => 11,
                'percentage' => true,
            ],
        ];
    });

Default Behavior

By default, the gross_amount will be calculated from the discounts first, then the taxes. This behavior is to avoid unrealistic tax charges.

Warning
There are currently no method for overriding this behavior.

Unverified

This user has not yet uploaded their public signing key.

Unverified

This user has not yet uploaded their public signing key.

Unverified

This user has not yet uploaded their public signing key.
@risangbaskoro risangbaskoro added the feature New feature or request label Feb 22, 2023
@risangbaskoro risangbaskoro linked an issue Feb 22, 2023 that may be closed by this pull request
6 tasks
@risangbaskoro
Copy link
Collaborator Author

Didn't use this #16 (comment) for recalculating taxes and discounts. Instead, recalculate taxes and discounts from gross_amount for each taxes and discounts added.

Unverified

This user has not yet uploaded their public signing key.

Unverified

This user has not yet uploaded their public signing key.
@risangbaskoro risangbaskoro changed the title Tax and Discount feature Add Taxes and Discounts to a Transaction Feb 22, 2023
@risangbaskoro risangbaskoro changed the title Add Taxes and Discounts to a Transaction Add Taxes and Discounts to Transactions Feb 22, 2023

Unverified

This user has not yet uploaded their public signing key.

Unverified

This user has not yet uploaded their public signing key.

Unverified

This user has not yet uploaded their public signing key.
@risangbaskoro risangbaskoro marked this pull request as ready for review February 23, 2023 02:50
@risangbaskoro risangbaskoro changed the title Add Taxes and Discounts to Transactions Ability to Add Discounts and Taxes to Transactions Feb 23, 2023
@risangbaskoro risangbaskoro merged commit 2b250f1 into master Feb 23, 2023
@risangbaskoro risangbaskoro deleted the feature/discounts-and-taxes branch February 23, 2023 02:53
This was referenced Feb 23, 2023
risangbaskoro added a commit that referenced this pull request Feb 23, 2023

Unverified

This user has not yet uploaded their public signing key.
* ability to add discount

* ability to add taxes and calculate taxes only after discounts applied

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* --wip-- [skip ci]

* Rename parameters in HasTaxes.

* --wip-- [skip ci]

* --wip-- discount and taxes docs

* docs: Discounts and Taxes Documentation

* Taxes and Discounts assignment batch addition.

* Removes only() method in TaxesDiscountsTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apply Taxes and Discounts to Kasir Object
1 participant