Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Fix #508 Order Changing Status On Update
- Fixed: Discount Computation With Gastro
  • Loading branch information
Blair2004 committed Jan 27, 2022
1 parent bdc63fc commit 9385648
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
4 changes: 3 additions & 1 deletion app/Events/OrderProductAfterComputedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ class OrderProductAfterComputedEvent
public $total_gross_discount;
public $total_discount;
public $total_net_discount;
public $net_discount;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct( OrderProduct $orderProduct, $total_gross_discount, $total_discount, $total_net_discount )
public function __construct( OrderProduct $orderProduct, $total_gross_discount, $total_discount, $total_net_discount, $net_discount )
{
$this->orderProduct = $orderProduct;
$this->total_gross_discount = $total_gross_discount;
$this->total_discount = $total_discount;
$this->total_net_discount = $total_net_discount;
$this->net_discount = $net_discount;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions app/Services/OrdersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,6 @@ public function __buildOrderProduct( array $orderProduct, ProductUnitQuantity $p
$orderProduct = $this->computeProduct( $orderProduct, $product, $productUnitQuantity );
$orderProduct[ 'unit_id' ] = $productUnitQuantity->unit->id ?? $orderProduct[ 'unit_id' ] ?? 0;
$orderProduct[ 'unit_quantity_id' ] = $productUnitQuantity->id ?? 0;
$orderProduct[ 'total_price' ] = $orderProduct[ 'total_price' ];
$orderProduct[ 'product' ] = $product;
$orderProduct[ 'mode' ] = $orderProduct[ 'mode' ] ?? 'normal';
$orderProduct[ 'unitQuantity' ] = $productUnitQuantity;
Expand Down Expand Up @@ -1333,10 +1332,9 @@ public function computeProduct( $fields, Product $product = null, ProductUnitQua
* let's compute that
*/
if ( empty( $fields[ 'total_price' ] ) ) {
$fields[ 'total_price' ] = (
$sale_price -
$fields[ 'discount' ]
) * floatval( $fields[ 'quantity' ] );
$fields[ 'total_price' ] = (
$sale_price * floatval( $fields[ 'quantity' ] )
) - $fields[ 'discount' ];
}

if ( $product instanceof Product ) {
Expand Down Expand Up @@ -1864,9 +1862,11 @@ public function computeOrderProduct( OrderProduct $orderProduct )
$net_discount = $orderProduct->discount;
}

$orderProduct->discount = $net_discount;

$orderProduct->net_price = $this->currencyService
->fresh( $orderProduct->unit_price )
->get();
->getFullRaw();

$orderProduct->total_gross_price = $this->currencyService
->fresh( $orderProduct->gross_price )
Expand All @@ -1889,7 +1889,8 @@ public function computeOrderProduct( OrderProduct $orderProduct )
$orderProduct,
$total_gross_discount,
$total_discount,
$total_net_discount
$total_net_discount,
$net_discount
);
}

Expand Down Expand Up @@ -2100,7 +2101,7 @@ public function refreshOrder(Order $order)
$order->payment_status = Order::PAYMENT_PARTIALLY_REFUNDED;
} else if ( $order->tendered >= $order->total && $order->total > 0 ) {
$order->payment_status = Order::PAYMENT_PAID;
} else if ( ( float ) $order->tendered < ( float ) $order->total ) {
} else if ( ( float ) $order->tendered < ( float ) $order->total && ( float ) $order->tendered > 0 ) {
$order->payment_status = Order::PAYMENT_PARTIALLY;
} else if ( $order->total == 0 && $totalRefunds == 0 ) {
$order->payment_status = Order::PAYMENT_UNPAID;
Expand Down
5 changes: 0 additions & 5 deletions tests/Feature/CreateUnitGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace Tests\Feature;

use App\Models\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
use Tests\Traits\WithAuthentication;
use Tests\Traits\WithUnitTest;
Expand Down
17 changes: 12 additions & 5 deletions tests/Traits/WithOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\OrderProductRefund;
use App\Models\PaymentType;
use App\Models\Product;
use App\Models\ProductUnitQuantity;
use App\Models\Register;
use App\Models\RegisterHistory;
use App\Models\TaxGroup;
Expand Down Expand Up @@ -693,9 +694,13 @@ protected function attemptOrderWithProductPriceMode()

protected function attemptCreateHoldOrder()
{
$product = Product::get()->random();
$unit = $product->unit_quantities()->where( 'quantity', '>', 0 )->first();
$subtotal = $unit->sale_price * 5;
$unitQuantity = ProductUnitQuantity::where( 'quantity', '>', 0 )->get()->random();

if ( ! $unitQuantity instanceof ProductUnitQuantity ) {
throw new Exception( 'No valid unit is provided.' );
}

$subtotal = $unitQuantity->sale_price * 5;

$response = $this->withSession( $this->app[ 'session' ]->all() )
->json( 'POST', 'api/nexopos/v4/orders', [
Expand All @@ -720,15 +725,17 @@ protected function attemptCreateHoldOrder()
'shipping' => 150,
'products' => [
[
'product_id' => $product->id,
'product_id' => $unitQuantity->product->id,
'quantity' => 5,
'unit_price' => 12,
'unit_quantity_id' => $unit->id,
'unit_quantity_id' => $unitQuantity->id,
]
],
]);

$response->assertJsonPath( 'data.order.payment_status', 'hold' );

return json_decode( $response->getContent(), true );
}

protected function attemptDeleteOrder()
Expand Down
2 changes: 1 addition & 1 deletion tests/Traits/WithUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Tests\Traits;

use App\Models\TaxGroup;
use App\Models\Unit;
use App\Models\UnitGroup;

trait WithUnitTest
Expand All @@ -23,7 +24,6 @@ protected function attemptCreateUnitGroup()
protected function attemptCreateUnit()
{
$group = UnitGroup::get()->shuffle()->first();

$response = $this->withSession( $this->app[ 'session' ]->all() )
->json( 'POST', 'api/nexopos/v4/crud/ns.units', [
'name' => __( 'Piece' ),
Expand Down

0 comments on commit 9385648

Please sign in to comment.