Skip to content

Commit

Permalink
Changelog
Browse files Browse the repository at this point in the history
- Fixed: Combined Product Test
- Fixed: Order Not Attached To A Refund Expense
  • Loading branch information
Blair2004 committed Oct 9, 2021
1 parent 86d7d1e commit ce880df
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 68 deletions.
2 changes: 1 addition & 1 deletion app/Listeners/ExpensesEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function( ExpenseAfterUpdateEvent $event ) {
$event->listen(
OrderAfterProductRefundedEvent::class,
function( OrderAfterProductRefundedEvent $event ) {
$this->expenseService->createExpenseFromRefund( $event->orderProductRefund, $event->orderProduct );
$this->expenseService->createExpenseFromRefund( $event->order, $event->orderProductRefund, $event->orderProduct );
}
);
}
Expand Down
83 changes: 39 additions & 44 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,54 +159,49 @@ public function scopePaymentStatusIn( $query, array $statuses )
* or all the product if it's enabled or disabled.
* @return array
*/
public function getProductsAttribute()
public function getCombinedProductsAttribute()
{
if ( ns()->option->get( 'ns_invoice_merge_similar_products', 'no' ) === 'yes' ) {
return $this->combinedProducts;
$combinaison = [];

$this->products()->with( 'unit' )->get()->each( function( $product ) use ( &$combinaison ){
$values = $product->toArray();

extract( $values );

$keys = array_keys( $combinaison );
$stringified = Hook::filter( 'ns-products-combinaison-identifier', $product_id . '-' . $order_id . '-' . $discount . '-' . $product_category_id . '-' . $status, $product );
$combinaisonAttributes = Hook::filter( 'ns-products-combinaison-attributes', [
'quantity',
'total_gross_price',
'total_price',
'total_purchase_price',
'total_net_price',
'discount'
]);

if ( in_array( $stringified, $keys ) ) {
foreach( $combinaisonAttributes as $attribute ) {
$combinaison[ $stringified ][ $attribute ] += (float) $product->$attribute;
}
} else {
$rawProduct = $product->toArray();

unset( $rawProduct[ 'id' ] );
unset( $rawProduct[ 'created_at' ] );
unset( $rawProduct[ 'updated_at' ] );
unset( $rawProduct[ 'procurement_product_id' ] );

$combinaison[ $stringified ] = $rawProduct;
}
});

/**
* that's nasty.
*/
return collect( json_decode( json_encode( $combinaison ) ) );
}

return $this->products()->get();
}

public function getCombinedProductsAttribute()
{
$combinaison = [];

$this->products()->with( 'unit' )->get()->each( function( $product ) use ( &$combinaison ){
$values = $product->toArray();

extract( $values );

$keys = array_keys( $combinaison );
$stringified = Hook::filter( 'ns-products-combinaison-identifier', $product_id . '-' . $order_id . '-' . $discount . '-' . $product_category_id . '-' . $status, $product );
$combinaisonAttributes = Hook::filter( 'ns-products-combinaison-attributes', [
'quantity',
'total_gross_price',
'total_price',
'total_purchase_price',
'total_net_price',
'discount'
]);

if ( in_array( $stringified, $keys ) ) {
foreach( $combinaisonAttributes as $attribute ) {
$combinaison[ $stringified ][ $attribute ] += (float) $product->$attribute;
}
} else {
$rawProduct = $product->toArray();

unset( $rawProduct[ 'id' ] );
unset( $rawProduct[ 'created_at' ] );
unset( $rawProduct[ 'updated_at' ] );
unset( $rawProduct[ 'procurement_product_id' ] );

$combinaison[ $stringified ] = $rawProduct;
}
});

/**
* that's nasty.
*/
return collect( json_decode( json_encode( $combinaison ) ) );
}
}
27 changes: 15 additions & 12 deletions app/Services/ExpenseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public function handleProcurementExpense( Procurement $procurement )
* @param OrderProduct $orderProduct
* @return void
*/
public function createExpenseFromRefund( OrderProductRefund $orderProductRefund, OrderProduct $orderProduct )
public function createExpenseFromRefund( Order $order, OrderProductRefund $orderProductRefund, OrderProduct $orderProduct )
{
$expenseCategory = ExpenseCategory::find( ns()->option->get( 'ns_sales_refunds_account' ) );

Expand All @@ -440,6 +440,7 @@ public function createExpenseFromRefund( OrderProductRefund $orderProductRefund,
$expense->active = true;
$expense->operation = CashFlow::OPERATION_DEBIT;
$expense->author = Auth::id();
$expense->order_id = $order->id;
$expense->order_refund_id = $orderProductRefund->order_refund_id;
$expense->name = sprintf( __( 'Refunding : %s' ), $orderProduct->name );
$expense->id = 0; // this is not assigned to an existing expense
Expand Down Expand Up @@ -469,17 +470,19 @@ public function createExpenseFromRefund( OrderProductRefund $orderProductRefund,
ns()->option->set( $optionName, $expenseCategory->id );
}

$expense = new Expense;
$expense->value = $orderProductRefund->total_price;
$expense->active = true;
$expense->operation = $orderProduct->condition === OrderProductRefund::CONDITION_DAMAGED ? CashFlow::OPERATION_DEBIT : CashFlow::OPERATION_CREDIT;
$expense->author = Auth::id();
$expense->order_refund_id = $orderProductRefund->order_refund_id;
$expense->name = sprintf( __( 'Stock Return (%s) : %s' ), $conditionLabel, $orderProduct->name );
$expense->id = 0; // this is not assigned to an existing expense
$expense->category = $expenseCategory;

$this->recordCashFlowHistory( $expense );
if ( OrderProductRefund::CONDITION_DAMAGED ) {
$expense = new Expense;
$expense->value = $orderProductRefund->total_price;
$expense->active = true;
$expense->operation = CashFlow::OPERATION_DEBIT;
$expense->author = Auth::id();
$expense->order_refund_id = $orderProductRefund->order_refund_id;
$expense->name = sprintf( __( 'Stock Return (%s) : %s' ), $conditionLabel, $orderProduct->name );
$expense->id = 0; // this is not assigned to an existing expense
$expense->category = $expenseCategory;

$this->recordCashFlowHistory( $expense );
}
}

public function handleCashRegisterHistory( RegisterHistory $history )
Expand Down
6 changes: 5 additions & 1 deletion app/Services/OrdersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,11 @@ public function deleteOrder(Order $order)
{
event( new OrderBeforeDeleteEvent( $order ) );

$order->products->each( function( OrderProduct $product) {
$order
->products()
->get()
->each( function( OrderProduct $product) {

/**
* we do proceed by doing an initial return
* only if the product is not a quick product/service
Expand Down
4 changes: 2 additions & 2 deletions app/Services/TestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function prepareOrder( Carbon $date, array $orderDetails = [], array $pro
$shippingFees = $faker->randomElement([10,15,20,25,30,35,40]);
$discountRate = $faker->numberBetween(0,5);

$products = $products->map( function( $product ) use ( $faker, $productDetails ) {
$products = $products->map( function( $product ) use ( $faker, $productDetails, $config ) {
$unitElement = $faker->randomElement( $product->unit_quantities );

$data = array_merge([
Expand All @@ -38,7 +38,7 @@ public function prepareOrder( Carbon $date, array $orderDetails = [], array $pro
'unit_id' => $unitElement->unit_id,
], $productDetails );

if ( $faker->randomElement([ false, true ]) ) {
if ( $faker->randomElement([ false, true ]) || ! ( $config[ 'allow_quick_products' ] ?? true ) ) {
$data[ 'product_id' ] = $product->id;
$data[ 'unit_quantity_id' ] = $unitElement->id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</tr>
</thead>
<tbody class="text-sm">
@foreach( Hook::filter( 'ns-receipt-products', $order->products ) as $product )
@foreach( Hook::filter( 'ns-receipt-products', $order->combinedProducts ) as $product )
<tr>
<td colspan="2" class="p-2 border-b border-gray-700">
<span class="">{{ $product->name }} (x{{ $product->quantity }})</span>
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/CombiningProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public function test_combined_products()
Role::namespace( 'admin' )->users->first(),
['*']
);

ns()->option->set( 'ns_invoice_merge_similar_products', 'yes' );

$testService = new TestService;
$orderDetails = $testService->prepareOrder( ns()->date->now(), [], [], [
'products' => function() {
$product = Product::where( 'tax_group_id', '>', 0 )->with( 'unit_quantities' )->first();
return collect([ $product, $product ]);
}
},
'allow_quick_products' => false
]);

$response = $this->withSession( $this->app[ 'session' ]->all() )
Expand Down
16 changes: 10 additions & 6 deletions tests/Feature/OrderRefundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ public function testRefund()
* We'll keep original products amounts and quantity
* this means we're doing a full refund of price and quantities
*/
$responseData[ 'data' ][ 'order' ][ 'products' ][0][ 'condition' ] = OrderProductRefund::CONDITION_DAMAGED;
$responseData[ 'data' ][ 'order' ][ 'products' ][0][ 'quantity' ] = 1;
$responseData[ 'data' ][ 'order' ][ 'products' ][0][ 'description' ] = __( 'The product wasn\'t properly manufactured, causing external damage to the device during the shipment.' );
$responseData[ 'data' ][ 'order' ][ 'products' ] = collect( $responseData[ 'data' ][ 'order' ][ 'products' ] )->map( function( $product ) {
$product[ 'condition' ] = OrderProductRefund::CONDITION_DAMAGED;
$product[ 'quantity' ] = 1;
$product[ 'description' ] = __( 'Test : The product wasn\'t properly manufactured, causing external damage to the device during the shipment.' );
return $product;
})->toArray();

$response = $this->withSession( $this->app[ 'session' ]->all() )
->json( 'POST', 'api/nexopos/v4/orders/' . $responseData[ 'data' ][ 'order' ][ 'id' ] . '/refund', [
Expand Down Expand Up @@ -181,10 +184,11 @@ public function testRefund()
throw new Exception( __( 'An expense hasn\'t been created after the refund.' ) );
}

$expense = $expenseCategory->cashFlowHistories()->orderBy( 'id', 'desc' )->first();
$expenseValue = $expenseCategory->cashFlowHistories()
->where( 'order_id', $responseData[ 'data' ][ 'order' ][ 'id' ] )
->sum( 'value' );


if ( ( float ) $expense->getRawOriginal( 'value' ) != ( float ) $responseData[ 'data' ][ 'orderRefund' ][ 'total' ] ) {
if ( ( float ) $expenseValue != ( float ) $responseData[ 'data' ][ 'orderRefund' ][ 'total' ] ) {
throw new Exception( __( 'The expense created after the refund doesn\'t match the order refund total.' ) );
}

Expand Down

0 comments on commit ce880df

Please sign in to comment.