Skip to content

Commit e09eb20

Browse files
committed
Changelog
- Fixed: owed amount for customer not correctly computed - Fixed: add refund status to order completely refunded
1 parent c9a10af commit e09eb20

File tree

5 files changed

+126
-50
lines changed

5 files changed

+126
-50
lines changed

app/Jobs/ComputeCustomerAccountJob.php

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Events\OrderAfterCreatedEvent;
66
use App\Events\OrderAfterPaymentCreatedEvent;
77
use App\Events\OrderAfterRefundedEvent;
8+
use App\Events\OrderAfterUpdatedEvent;
89
use App\Events\OrderBeforeDeleteEvent;
910
use App\Models\Order;
1011
use App\Models\OrderPayment;
@@ -19,16 +20,24 @@ class ComputeCustomerAccountJob implements ShouldQueue
1920
{
2021
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2122

23+
/**
24+
* @var CustomerService
25+
*/
26+
protected $customerService;
27+
2228
public $event;
2329

2430
/**
2531
* Create a new job instance.
2632
*
2733
* @return void
2834
*/
29-
public function __construct( $event )
30-
{
31-
$this->event = $event;
35+
public function __construct(
36+
$event,
37+
CustomerService $customerService
38+
) {
39+
$this->event = $event;
40+
$this->customerService = $customerService;
3241
}
3342

3443
/**
@@ -38,61 +47,63 @@ public function __construct( $event )
3847
*/
3948
public function handle()
4049
{
41-
if ( $this->event instanceof OrderBeforeDeleteEvent ) {
42-
$this->handleDeletion( $this->event );
43-
} else if ( $this->event instanceof OrderAfterRefundedEvent ) {
44-
$this->reduceCustomerPurchases( $this->event );
45-
} else if ( $this->event instanceof OrderAfterCreatedEvent ) {
46-
$this->computeCustomerOwed( $this->event );
47-
$this->computeCustomerRewards( $this->event );
50+
if ($this->event instanceof OrderBeforeDeleteEvent) {
51+
$this->handleDeletion($this->event);
52+
} else if ($this->event instanceof OrderAfterRefundedEvent) {
53+
$this->reduceCustomerPurchases($this->event);
54+
} else if (
55+
$this->event instanceof OrderAfterCreatedEvent ||
56+
$this->event instanceof OrderAfterUpdatedEvent
57+
) {
58+
$this->computeCustomerOwed($this->event);
59+
$this->computeCustomerRewards($this->event);
4860
}
4961
}
5062

51-
private function computeCustomerOwed( OrderAfterCreatedEvent $event )
63+
/**
64+
* We'll make sure to update the customer owed amount
65+
* when even he's involved on a transaction.
66+
*/
67+
private function computeCustomerOwed( $event )
5268
{
53-
$event->order->customer->owed_amount += ns()->currency
54-
->define( $event->order->total )
55-
->subtractBy( $event->order->tendered )
56-
->getRaw();
57-
58-
$event->order->customer->save();
69+
$this->customerService->updateCustomerOwedAmount($event->order->customer);
5970
}
6071

61-
private function computeCustomerRewards( OrderAfterCreatedEvent $event )
72+
private function computeCustomerRewards( $event )
6273
{
63-
if ( $event->order->payment_status === Order::PAYMENT_PAID ) {
74+
if ($event->order->payment_status === Order::PAYMENT_PAID) {
6475
/**
6576
* @var CustomerService
6677
*/
67-
$customerService = app()->make( CustomerService::class );
68-
69-
$customerService->computeReward(
70-
$event->order,
71-
$event->order->customer
78+
$customerService = app()->make(CustomerService::class);
79+
80+
$customerService->computeReward(
81+
$event->order,
82+
$event->order->customer
7283
);
7384
}
7485
}
7586

76-
private function reduceCustomerPurchases( OrderAfterRefundedEvent $event )
87+
private function reduceCustomerPurchases(OrderAfterRefundedEvent $event)
7788
{
7889
$event->order->customer->purchases_amount = $event->order->customer->purchases_amount - $event->orderRefund->total;
7990
$event->order->customer->save();
8091
}
8192

82-
private function handleDeletion( OrderBeforeDeleteEvent $event )
93+
private function handleDeletion(OrderBeforeDeleteEvent $event)
8394
{
84-
switch( $event->order->payment_status ) {
85-
case 'paid':
95+
switch ($event->order->payment_status) {
96+
case 'paid':
8697
$event->order->customer->purchases_amount -= $event->order->total;
87-
break;
88-
case 'partially_paid':
98+
break;
99+
case 'partially_paid':
89100
$event->order->customer->purchases_amount -= $event->order->tendered;
90-
break;
101+
break;
91102
default:
92103
$event->order->customer->owed_amount -= $event->order->total;
93-
break;
104+
break;
94105
}
95-
106+
96107
$event->order->customer->save();
97108
}
98109
}

app/Listeners/OrderEventsSubscriber.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ public function subscribe( $events )
6767
[ OrderEventsSubscriber::class, 'handleOrderUpdate' ]
6868
);
6969

70+
/**
71+
* all events likely to affect
72+
* the customer account.
73+
*/
74+
$events->listen(
75+
OrderAfterCreatedEvent::class,
76+
[ OrderEventsSubscriber::class, 'handleCustomerUpdates' ]
77+
);
78+
$events->listen(
79+
OrderAfterUpdatedEvent::class,
80+
[ OrderEventsSubscriber::class, 'handleCustomerUpdates' ]
81+
);
82+
$events->listen(
83+
OrderBeforeDeleteEvent::class,
84+
[ OrderEventsSubscriber::class, 'handleCustomerUpdates' ]
85+
);
86+
$events->listen(
87+
OrderAfterRefundedEvent::class,
88+
[ OrderEventsSubscriber::class, 'handleCustomerUpdates' ]
89+
);
90+
7091
$events->listen(
7192
OrderAfterCreatedEvent::class,
7293
[ OrderEventsSubscriber::class, 'handleInstalmentPayment' ]
@@ -111,13 +132,27 @@ public function handleOrderUpdate( $event )
111132
ComputeDayReportJob::dispatch()
112133
->delay( now()->addSecond(5) );
113134

114-
ComputeCustomerAccountJob::dispatch( $event )
115-
->delay( now()->addSecond(5) );
116-
117135
ComputeCashierSalesJob::dispatch( $event )
118136
->delay( now()->addSecond(10) );
119137
}
120138

139+
public function handleCustomerUpdates( $event )
140+
{
141+
if (
142+
$event instanceof OrderAfterCreatedEvent ||
143+
$event instanceof OrderAfterUpdatedEvent ||
144+
$event instanceof OrderBeforeDeleteEvent ||
145+
$event instanceof OrderAfterRefundedEvent
146+
147+
) {
148+
ComputeCustomerAccountJob::dispatch(
149+
$event,
150+
app()->make( CustomerService::class )
151+
)
152+
->delay( now()->addSecond(5) );
153+
}
154+
}
155+
121156
/**
122157
* When a sales is made on a specific order
123158
* we to increase the customers purchases

app/Services/CustomerService.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,28 @@ public function deleteRelatedCustomerCoupon( Coupon $coupon )
510510
});
511511
}
512512

513+
/**
514+
* Will refresh the owed amount
515+
* for the provided customer
516+
*/
517+
public function updateCustomerOwedAmount( Customer $customer )
518+
{
519+
$unpaid = Order::whereIn( 'payment_status', [
520+
Order::PAYMENT_UNPAID
521+
])->sum( 'total' );
522+
523+
/**
524+
* Change here will be negative, so we
525+
* want to be an absolute value.
526+
*/
527+
$change = abs( Order::whereIn( 'payment_status', [
528+
Order::PAYMENT_PARTIALLY
529+
])->sum( 'change' ) );
530+
531+
$customer->owed_amount = ns()->currency->getRaw( $unpaid + $change );
532+
$customer->save();
533+
}
534+
513535
/**
514536
* Create customer group using
515537
* provided fields

app/Services/MenuService.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ public function buildMenus()
162162
'permissions' => [ 'nexopos.create.expenses-categories' ],
163163
'href' => ns()->url( '/dashboard/expenses/categories/create' )
164164
],
165-
'cash-in' => [
166-
'label' => __( 'Cash Flow' ),
167-
'href' => ns()->url( '/dashboard/banking/cash-flow' )
168-
]
169165
]
170166
],
171167
'inventory' => [

app/Services/OrdersService.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@ public function refreshOrder(Order $order)
18161816
return floatval($product->total_price);
18171817
})->sum();
18181818

1819+
$productTotalQuatity = $products->map( function( $product) {
1820+
return floatval( $product->quantity );
1821+
})->sum();
1822+
18191823
$productGrossTotal = $products->map(function ($product) {
18201824
return floatval($product->total_gross_price);
18211825
})->sum();
@@ -1848,16 +1852,24 @@ public function refreshOrder(Order $order)
18481852
$refunds = $order->refund;
18491853
$totalRefunds = $refunds->map( fn( $refund ) => $refund->total )->sum();
18501854

1851-
if ( ( float ) $order->total == 0 && $totalRefunds > 0 ) {
1852-
$order->payment_status = Order::PAYMENT_REFUNDED;
1853-
} else if ( $order->total > 0 && $totalRefunds > 0 ) {
1854-
$order->payment_status = Order::PAYMENT_PARTIALLY_REFUNDED;
1855-
} else if ( $order->tendered >= $order->total && $order->total > 0 ) {
1856-
$order->payment_status = Order::PAYMENT_PAID;
1857-
} else if ( ( float ) $order->tendered < ( float ) $order->total ) {
1858-
$order->payment_status = Order::PAYMENT_PARTIALLY;
1859-
} else if ( $order->total == 0 && $totalRefunds == 0 ) {
1860-
$order->payment_status = Order::PAYMENT_UNPAID;
1855+
/**
1856+
* We believe if the product total is greater
1857+
* than "0", then probably the order hasn't been paid yet.
1858+
*/
1859+
if ( $productTotal ) {
1860+
if ( ( float ) $order->total == 0 && $totalRefunds > 0 ) {
1861+
$order->payment_status = Order::PAYMENT_REFUNDED;
1862+
} else if ( $order->total > 0 && $totalRefunds > 0 ) {
1863+
$order->payment_status = Order::PAYMENT_PARTIALLY_REFUNDED;
1864+
} else if ( $order->tendered >= $order->total && $order->total > 0 ) {
1865+
$order->payment_status = Order::PAYMENT_PAID;
1866+
} else if ( ( float ) $order->tendered < ( float ) $order->total ) {
1867+
$order->payment_status = Order::PAYMENT_PARTIALLY;
1868+
} else if ( $order->total == 0 && $totalRefunds == 0 ) {
1869+
$order->payment_status = Order::PAYMENT_UNPAID;
1870+
}
1871+
} else if ( $productTotal == 0 && $productTotalQuatity == 0 ) {
1872+
$order->payment_status = Order::PAYMENT_REFUNDED;
18611873
}
18621874

18631875
$order->save();

0 commit comments

Comments
 (0)