Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair2004 committed Jan 28, 2022
1 parent 9385648 commit d210aa4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public/mapping.json
storage/module.zip
tests/database.sqlite
tests/database.sqlite-journal
tests/Post/*
storage/snapshots/*.sql
15 changes: 8 additions & 7 deletions app/Services/OrdersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,9 @@ private function __checkOrderPayments( $fields, Order $order = null, Customer $c

$totalPayments = 0;

$subtotal = collect( $fields[ 'products' ] )->map(function ($product) {
$subtotal = Currency::raw( collect( $fields[ 'products' ] )->map(function ($product) {
return floatval($product['total_price']);
})->sum();
})->sum() );

$total = $this->currencyService->define(
$subtotal + $this->__getShippingFee($fields)
Expand Down Expand Up @@ -1855,6 +1855,7 @@ public function computeOrderProduct( OrderProduct $orderProduct )
$orderProduct->discount_percentage,
$orderProduct->unit_price * $orderProduct->quantity
);

} else if ( $orderProduct->discount_type === 'flat' ) {
$total_discount = $orderProduct->discount;
$total_gross_discount = $orderProduct->discount;
Expand All @@ -1877,7 +1878,7 @@ public function computeOrderProduct( OrderProduct $orderProduct )
->fresh( $orderProduct->net_price )
->multiplyBy( $orderProduct->quantity )
->subtractBy( $net_discount )
->get();
->getFullRaw();

$orderProduct->total_net_price = $this->currencyService
->fresh( $orderProduct->net_price )
Expand All @@ -1904,7 +1905,7 @@ public function computeOrderProduct( OrderProduct $orderProduct )
public function computeDiscountValues( $rate, $value )
{
if ( $rate > 0 ) {
return Currency::raw( ( $value * $rate ) / 100 );
return Currency::fresh( ( $value * $rate ) / 100 )->getFullRaw();
}

return 0;
Expand Down Expand Up @@ -2070,10 +2071,10 @@ public function refreshOrder(Order $order)
/**
* let's refresh all the order values
*/
$order->subtotal = $productTotal;
$order->subtotal = Currency::raw( $productTotal );
$order->gross_total = $productGrossTotal;
$order->discount = $this->computeOrderDiscount( $order );
$order->total = Currency::fresh( $productTotal )
$order->discount = $this->computeOrderDiscount( $order );
$order->total = Currency::fresh( $order->subtotal )
->additionateBy( $orderShipping )
->additionateBy(
( $order->tax_type === 'exclusive' ? $order->tax_value : 0 )
Expand Down
4 changes: 3 additions & 1 deletion tests/Traits/WithAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ trait WithAuthentication
{
protected function attemptAuthenticate()
{
$user = Role::namespace( 'admin' )->users->first();

Sanctum::actingAs(
Role::namespace( 'admin' )->users->first(),
$user,
['*']
);
}
Expand Down
47 changes: 33 additions & 14 deletions tests/Traits/WithOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ trait WithOrderTest
protected $totalDaysInterval = 1;
protected $users = [];
protected $defaultProcessing = true;
protected $allowQuickProducts = true;

protected function attemptPostOrder( $callback )
{
Expand Down Expand Up @@ -342,7 +343,10 @@ public function processOrders( $currentDate, $callback )
'unit_id' => $unitElement->unit_id,
], $this->customProductParams );

if ( $faker->randomElement([ true, false ]) ) {
if ( ! $this->allowQuickProducts ) {
$data[ 'product_id' ] = $product->id;
$data[ 'unit_quantity_id' ] = $unitElement->id;
} else if ( $faker->randomElement([ true, false ]) ) {
$data[ 'product_id' ] = $product->id;
$data[ 'unit_quantity_id' ] = $unitElement->id;
}
Expand All @@ -356,8 +360,6 @@ public function processOrders( $currentDate, $callback )
* testing customer balance
*/
$customer = Customer::get()->random();
$customerFirstPurchases = $customer->purchases_amount;
$customerFirstOwed = $customer->owed_amount;

$subtotal = ns()->currency->getRaw( $products->map( function( $product ) use ($currency) {
$productSubTotal = $currency
Expand Down Expand Up @@ -467,6 +469,23 @@ public function processOrders( $currentDate, $callback )
] : []
], $this->customOrderParams );

$pathName = 'tests/Post/process-orders.json';

/**
* used for reproducing orders that cause a bug
*/
// $orderData = json_decode( file_get_contents( base_path( $pathName ) ), true );

$customer = Customer::find( $orderData[ 'customer_id' ] );
$customerFirstPurchases = $customer->purchases_amount;
$customerFirstOwed = $customer->owed_amount;

/**
* We might need this to reproduce a sale that caused
* an error.
*/
file_put_contents( base_path( $pathName ), json_encode( $orderData ) );

$response = $this->withSession( $this->app[ 'session' ]->all() )
->json( 'POST', 'api/nexopos/v4/orders', $orderData );

Expand All @@ -478,26 +497,26 @@ public function processOrders( $currentDate, $callback )

if ( $this->shouldMakePayment ) {
$netsubtotal = $currency
->define( $subtotal )
->define( $orderData[ 'subtotal' ] )
->subtractBy( $totalCoupons )
->subtractBy( $discount[ 'value' ] )
->subtractBy( $orderData[ 'discount' ] )
->getRaw();

$total = $currency->define( $netsubtotal )
->additionateBy( $shippingFees )
->additionateBy( $orderData[ 'shipping' ] )
->getRaw() ;

$response->assertJsonPath( 'data.order.subtotal', $currency->getRaw( $subtotal ) );
$response->assertJsonPath( 'data.order.subtotal', $currency->getRaw( $orderData[ 'subtotal' ] ) );

$response->assertJsonPath( 'data.order.total', $currency->define( $netsubtotal )
->additionateBy( $shippingFees )
->additionateBy( $orderData[ 'shipping' ] )
->getRaw()
);

$response->assertJsonPath( 'data.order.change', $currency->define( $subtotal + $shippingFees - ( $discount[ 'rate' ] + $totalCoupons ) )
->subtractBy( $subtotal + $shippingFees - ( $discount[ 'rate' ] + ( $allCoupons[0][ 'value' ] ?? 0 ) ) )
->getRaw()
);
$change = collect( $orderData[ 'payments' ] )->map( fn( $payment ) => ( float ) $payment[ 'value' ] )->sum() - ( ( float ) $orderData[ 'subtotal' ] + ( float ) $orderData[ 'shipping' ] - ( float ) $orderData[ 'discount' ] );
$change = Currency::raw( $change );

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

$singleResponse[ 'order-payment' ] = json_decode( $response->getContent() );

Expand All @@ -509,7 +528,7 @@ public function processOrders( $currentDate, $callback )
$customerSecondPurchases = $customer->purchases_amount;
$customerSecondOwed = $customer->owed_amount;

if ( ( float ) trim( $customerFirstPurchases + $total ) != ( float ) trim( $customerSecondPurchases ) ) {
if ( ( float ) trim( $customerFirstPurchases + ( $orderData[ 'payments' ][0][ 'value' ] ?? 0 ) ) != ( float ) trim( $customerSecondPurchases ) ) {
throw new Exception(
sprintf(
__( 'The customer purchase hasn\'t been updated. Expected %s Current Value %s. Sub total : %s' ),
Expand Down

0 comments on commit d210aa4

Please sign in to comment.