44
55namespace Bavix \Wallet \Traits ;
66
7- use function array_unique ;
87use Bavix \Wallet \Exceptions \BalanceIsEmpty ;
98use Bavix \Wallet \Exceptions \InsufficientFunds ;
109use Bavix \Wallet \Exceptions \ProductEnded ;
2625use Bavix \Wallet \Services \MetaServiceLegacy ;
2726use Bavix \Wallet \Services \PrepareServiceInterface ;
2827use Bavix \Wallet \Services \PurchaseServiceInterface ;
28+ use Bavix \Wallet \Services \TransferServiceInterface ;
2929use function count ;
3030use Illuminate \Database \RecordsNotFoundException ;
3131
@@ -174,7 +174,6 @@ public function safeRefundCart(CartInterface $cart, bool $force = false, bool $g
174174 public function refundCart (CartInterface $ cart , bool $ force = false , bool $ gifts = false ): bool
175175 {
176176 return app (AtomicServiceInterface::class)->block ($ this , function () use ($ cart , $ force , $ gifts ) {
177- $ results = [];
178177 $ transfers = app (PurchaseServiceInterface::class)->already ($ this , $ cart ->getBasketDto (), $ gifts );
179178 if (count ($ transfers ) !== $ cart ->getBasketDto ()->total ()) {
180179 throw new ModelNotFoundException (
@@ -186,9 +185,11 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts
186185
187186 $ index = 0 ;
188187 $ objects = [];
188+ $ transferIds = [];
189189 $ transfers = array_values ($ transfers );
190190 $ prepareService = app (PrepareServiceInterface::class);
191191 foreach ($ cart ->getBasketDto ()->cursor () as $ product ) {
192+ $ transferIds [] = $ transfers [$ index ]->getKey ();
192193 $ objects [] = $ prepareService ->transferLazy (
193194 $ product ,
194195 $ transfers [$ index ]->withdraw ->wallet ,
@@ -206,15 +207,9 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts
206207
207208 app (CommonServiceLegacy::class)->applyTransfers ($ objects );
208209
209- // fixme: one query update for
210- foreach ($ transfers as $ transfer ) {
211- $ results [] = $ transfer ->update ([
212- 'status ' => Transfer::STATUS_REFUND ,
213- 'status_last ' => $ transfer ->status ,
214- ]);
215- }
216-
217- return count (array_unique ($ results )) === 1 ;
210+ return app (TransferServiceInterface::class)
211+ ->updateStatusByIds (Transfer::STATUS_REFUND , $ transferIds )
212+ ;
218213 });
219214 }
220215
0 commit comments