Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Services/PrepareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public function transferLazy(

return $this->transferLazyDtoAssembler->create(
$from,
$to,
$toWallet,
$discount,
$fee,
$this->withdraw($from, $withdrawAmount, $withdrawOption->getMeta(), $withdrawOption->isConfirmed()),
$this->deposit($to, $depositAmount, $depositOption->getMeta(), $depositOption->isConfirmed()),
$this->deposit($toWallet, $depositAmount, $depositOption->getMeta(), $depositOption->isConfirmed()),
$status
);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Services/PurchaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Bavix\Wallet\Interfaces\Customer;
use Bavix\Wallet\Internal\Dto\BasketDtoInterface;
use Bavix\Wallet\Models\Transfer;
use Illuminate\Database\Eloquent\Model;

final class PurchaseService implements PurchaseServiceInterface
{
public function __construct(private CastServiceInterface $castService)
{
}

public function already(Customer $customer, BasketDtoInterface $basketDto, bool $gifts = false): array
{
$status = $gifts
Expand All @@ -20,8 +23,7 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool
$arrays = [];
$query = $customer->transfers();
foreach ($basketDto->items() as $itemDto) {
/** @var Model $product */
$product = $itemDto->product();
$wallet = $this->castService->getWallet($itemDto->product());

/**
* As part of my work, "with" was added, it gives a 50x boost for a huge number of returns. In this case,
Expand All @@ -30,8 +32,8 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool
*/
$arrays[] = (clone $query)
->with(['deposit', 'withdraw.wallet'])
->where('to_type', $product->getMorphClass())
->where('to_id', $product->getKey())
->where('to_type', $wallet->getMorphClass())
->where('to_id', $wallet->getKey())
->whereIn('status', $status)
->orderBy('id', 'desc')
->limit(count($itemDto))
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasGift.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function gift(Wallet $to, ProductInterface $product, bool $force = false)
$withdraw->getKey(),
Transfer::STATUS_GIFT,
$castService->getWallet($to),
$castService->getModel($product),
$castService->getWallet($product),
$discount,
$fee
);
Expand Down
4 changes: 3 additions & 1 deletion tests/Infra/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Services\CastService;
use Bavix\Wallet\Services\CastServiceInterface;
use Bavix\Wallet\Traits\HasWallet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
Expand Down Expand Up @@ -55,7 +56,8 @@ public function getMetaProduct(): ?array
*/
public function boughtGoods(array $walletIds): MorphMany
{
return $this
return app(CastServiceInterface::class)
->getWallet($this)
->morphMany(config('wallet.transfer.model', Transfer::class), 'to')
->where('status', Transfer::STATUS_PAID)
->where('from_type', config('wallet.wallet.model', Wallet::class))
Expand Down
5 changes: 3 additions & 2 deletions tests/Units/Domain/DiscountTaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ public function testPay(): void

self::assertInstanceOf(Buyer::class, $transfer->from->holder);
self::assertInstanceOf(Wallet::class, $transfer->from);
self::assertInstanceOf(Item::class, $transfer->to);
self::assertInstanceOf(Item::class, $transfer->to->holder);
self::assertInstanceOf(Wallet::class, $transfer->to->wallet);

self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey());
self::assertSame($buyer->getKey(), $transfer->from->holder->getKey());
self::assertSame($product->getKey(), $transfer->to->getKey());
self::assertSame($product->wallet->getKey(), $transfer->to->getKey());
self::assertSame($product->getKey(), $transfer->to->holder->getKey());
}

public function testRefundPersonalDiscountAndTax(): void
Expand Down
5 changes: 3 additions & 2 deletions tests/Units/Domain/DiscountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ public function testPay(): void

self::assertInstanceOf(Buyer::class, $transfer->from->holder);
self::assertInstanceOf(Wallet::class, $transfer->from);
self::assertInstanceOf(Item::class, $transfer->to);
self::assertInstanceOf(Item::class, $transfer->to->holder);
self::assertInstanceOf(Wallet::class, $transfer->to->wallet);

self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey());
self::assertSame($buyer->getKey(), $transfer->from->holder->getKey());
self::assertSame($product->getKey(), $transfer->to->getKey());
self::assertSame($product->wallet->getKey(), $transfer->to->getKey());
self::assertSame($product->getKey(), $transfer->to->holder->getKey());
}

public function testItemTransactions(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Units/Domain/MultiWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function testPay(): void
self::assertInstanceOf(UserMulti::class, $paidTransfer->withdraw->payable);
self::assertSame($user->getKey(), $paidTransfer->withdraw->payable->getKey());
self::assertSame($transfer->from->id, $a->id);
self::assertSame($transfer->to->id, $product->id);
self::assertSame($transfer->to->id, $product->wallet->id);
self::assertSame($transfer->status, Transfer::STATUS_PAID);
self::assertSame($a->balanceInt, 0);
self::assertSame($product->balanceInt, $product->getAmountProduct($a));
Expand All @@ -512,7 +512,7 @@ public function testPay(): void
self::assertInstanceOf(UserMulti::class, $paidTransfer->withdraw->payable);
self::assertSame($user->getKey(), $paidTransfer->withdraw->payable->getKey());
self::assertSame($transfer->from->id, $b->id);
self::assertSame($transfer->to->id, $product->id);
self::assertSame($transfer->to->id, $product->wallet->id);
self::assertSame($transfer->status, Transfer::STATUS_PAID);
self::assertSame($b->balanceInt, 0);
self::assertSame($product->balanceInt, $product->getAmountProduct($b) * 2);
Expand Down
5 changes: 3 additions & 2 deletions tests/Units/Domain/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ public function testPay(): void

self::assertInstanceOf(Buyer::class, $transfer->from->holder);
self::assertInstanceOf(Wallet::class, $transfer->from);
self::assertInstanceOf(Item::class, $transfer->to);
self::assertInstanceOf(Item::class, $transfer->to->holder);
self::assertInstanceOf(Wallet::class, $transfer->to->wallet);

self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey());
self::assertSame($buyer->getKey(), $transfer->from->holder->getKey());
self::assertSame($product->getKey(), $transfer->to->getKey());
self::assertSame($product->wallet->getKey(), $transfer->to->getKey());
self::assertSame($product->getKey(), $transfer->to->holder->getKey());

self::assertSame(0, $buyer->balanceInt);
self::assertNull($buyer->safePay($product));
Expand Down