Skip to content

Commit cf07693

Browse files
committed
#117 add Customer in getAmountProduct method
1 parent 0b4e96d commit cf07693

File tree

13 files changed

+81
-72
lines changed

13 files changed

+81
-72
lines changed

src/Interfaces/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ interface Product extends Wallet
1515
public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool;
1616

1717
/**
18+
* @param Customer $customer
1819
* @return int
1920
*/
20-
public function getAmountProduct(): int;
21+
public function getAmountProduct(Customer $customer): int;
2122

2223
/**
2324
* @return array

src/Objects/Cart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ public function canBuy(Customer $customer, bool $force = null): bool
129129
}
130130

131131
/**
132+
* @param Customer $customer
132133
* @return int
133134
*/
134-
public function getTotal(): int
135+
public function getTotal(Customer $customer): int
135136
{
136137
$result = 0;
137138
foreach ($this->items as $item) {
138-
$result += $item->getAmountProduct();
139+
$result += $item->getAmountProduct($customer);
139140
}
140141
return $result;
141142
}

src/Traits/CartPay.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function payCart(Cart $cart, bool $force = null): array
8282
$results[] = app(CommonService::class)->forceTransfer(
8383
$self,
8484
$product,
85-
$product->getAmountProduct(),
85+
$product->getAmountProduct($self),
8686
$product->getMetaProduct(),
8787
Transfer::STATUS_PAID
8888
);
@@ -93,7 +93,7 @@ public function payCart(Cart $cart, bool $force = null): array
9393
$results[] = app(CommonService::class)->transfer(
9494
$self,
9595
$product,
96-
$product->getAmountProduct(),
96+
$product->getAmountProduct($self),
9797
$product->getMetaProduct(),
9898
Transfer::STATUS_PAID
9999
);

src/Traits/HasGift.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
6565
*/
6666
return app(DbService::class)->transaction(static function () use ($santa, $to, $product, $force) {
6767
$discount = app(WalletService::class)->discount($santa, $product);
68-
$amount = $product->getAmountProduct() - $discount;
68+
$amount = $product->getAmountProduct($santa) - $discount;
6969
$meta = $product->getMetaProduct();
7070
$fee = app(WalletService::class)
7171
->fee($product, $amount);

tests/CartTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testPay(): void
3232
}
3333

3434
$this->assertEquals($buyer->balance, $buyer->wallet->balance);
35-
$this->assertNotNull($buyer->deposit($cart->getTotal()));
35+
$this->assertNotNull($buyer->deposit($cart->getTotal($buyer)));
3636
$this->assertEquals($buyer->balance, $buyer->wallet->balance);
3737

3838
$transfers = $buyer->payCart($cart);
@@ -45,7 +45,7 @@ public function testPay(): void
4545
}
4646

4747
foreach ($cart->getItems() as $product) {
48-
$this->assertEquals($product->balance, $product->getAmountProduct());
48+
$this->assertEquals($product->balance, $product->getAmountProduct($buyer));
4949
}
5050

5151
$this->assertTrue($buyer->refundCart($cart));
@@ -74,7 +74,7 @@ public function testCartQuantity(): void
7474
for ($i = 0; $i < count($products) - 1; $i++) {
7575
$rnd = random_int(1, 5);
7676
$cart->addItem($products[$i], $rnd);
77-
$buyer->deposit($products[$i]->getAmountProduct() * $rnd);
77+
$buyer->deposit($products[$i]->getAmountProduct($buyer) * $rnd);
7878
$amount += $rnd;
7979
}
8080

@@ -110,7 +110,7 @@ public function testModelNotFoundException(): void
110110
for ($i = 0; $i < count($products) - 1; $i++) {
111111
$rnd = random_int(1, 5);
112112
$cart->addItem($products[$i], $rnd);
113-
$buyer->deposit($products[$i]->getAmountProduct() * $rnd);
113+
$buyer->deposit($products[$i]->getAmountProduct($buyer) * $rnd);
114114
$total += $rnd;
115115
}
116116

tests/DiscountTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function testPay(): void
2626
$product = factory(ItemDiscount::class)->create();
2727

2828
$this->assertEquals($buyer->balance, 0);
29-
$buyer->deposit($product->price);
29+
$buyer->deposit($product->getAmountProduct($buyer));
3030

31-
$this->assertEquals($buyer->balance, $product->price);
31+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
3232
$transfer = $buyer->pay($product);
3333
$this->assertNotNull($transfer);
3434
$this->assertEquals($transfer->status, Transfer::STATUS_PAID);
@@ -79,31 +79,31 @@ public function testRefund(): void
7979
]);
8080

8181
$this->assertEquals($buyer->balance, 0);
82-
$buyer->deposit($product->price);
82+
$buyer->deposit($product->getAmountProduct($buyer));
8383

84-
$this->assertEquals($buyer->balance, $product->price);
84+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
8585
$transfer = $buyer->pay($product);
8686
$this->assertNotNull($transfer);
8787
$this->assertEquals($transfer->status, Transfer::STATUS_PAID);
8888

8989
$this->assertTrue($buyer->refund($product));
90-
$this->assertEquals($buyer->balance, $product->price);
90+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
9191
$this->assertEquals($product->balance, 0);
9292

9393
$transfer->refresh();
9494
$this->assertEquals($transfer->status, Transfer::STATUS_REFUND);
9595

9696
$this->assertFalse($buyer->safeRefund($product));
97-
$this->assertEquals($buyer->balance, $product->price);
97+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
9898

9999
$transfer = $buyer->pay($product);
100100
$this->assertNotNull($transfer);
101101
$this->assertEquals($buyer->balance, 0);
102-
$this->assertEquals($product->balance, $product->price);
102+
$this->assertEquals($product->balance, $product->getAmountProduct($buyer));
103103
$this->assertEquals($transfer->status, Transfer::STATUS_PAID);
104104

105105
$this->assertTrue($buyer->refund($product));
106-
$this->assertEquals($buyer->balance, $product->price);
106+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
107107
$this->assertEquals($product->balance, 0);
108108

109109
$transfer->refresh();
@@ -125,22 +125,22 @@ public function testForceRefund(): void
125125
]);
126126

127127
$this->assertEquals($buyer->balance, 0);
128-
$buyer->deposit($product->price);
128+
$buyer->deposit($product->getAmountProduct($buyer));
129129

130-
$this->assertEquals($buyer->balance, $product->price);
130+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
131131

132132
$buyer->pay($product);
133133
$this->assertEquals($buyer->balance, 0);
134-
$this->assertEquals($product->balance, $product->price);
134+
$this->assertEquals($product->balance, $product->getAmountProduct($buyer));
135135

136136
$product->withdraw($product->balance);
137137
$this->assertEquals($product->balance, 0);
138138

139139
$this->assertFalse($buyer->safeRefund($product));
140140
$this->assertTrue($buyer->forceRefund($product));
141141

142-
$this->assertEquals($product->balance, -$product->price);
143-
$this->assertEquals($buyer->balance, $product->price);
142+
$this->assertEquals($product->balance, -$product->getAmountProduct($buyer));
143+
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
144144
$product->deposit(-$product->balance);
145145
$buyer->withdraw($buyer->balance);
146146

@@ -164,7 +164,7 @@ public function testOutOfStock(): void
164164
'quantity' => 1,
165165
]);
166166

167-
$buyer->deposit($product->price);
167+
$buyer->deposit($product->getAmountProduct($buyer));
168168
$buyer->pay($product);
169169
$buyer->pay($product);
170170
}
@@ -186,7 +186,7 @@ public function testForcePay(): void
186186
$this->assertEquals($buyer->balance, 0);
187187
$buyer->forcePay($product);
188188

189-
$this->assertEquals($buyer->balance, -$product->price);
189+
$this->assertEquals($buyer->balance, -$product->getAmountProduct($buyer));
190190

191191
$buyer->deposit(-$buyer->balance);
192192
$this->assertEquals($buyer->balance, 0);

tests/GiftTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function testGift(): void
2727
$this->assertEquals($first->balance, 0);
2828
$this->assertEquals($second->balance, 0);
2929

30-
$first->deposit($product->getAmountProduct());
31-
$this->assertEquals($first->balance, $product->getAmountProduct());
30+
$first->deposit($product->getAmountProduct($first));
31+
$this->assertEquals($first->balance, $product->getAmountProduct($first));
3232

3333
$transfer = $first->wallet->gift($second, $product);
3434
$this->assertEquals($first->balance, 0);
@@ -58,8 +58,8 @@ public function testRefund(): void
5858
$this->assertEquals($first->balance, 0);
5959
$this->assertEquals($second->balance, 0);
6060

61-
$first->deposit($product->getAmountProduct());
62-
$this->assertEquals($first->balance, $product->getAmountProduct());
61+
$first->deposit($product->getAmountProduct($first));
62+
$this->assertEquals($first->balance, $product->getAmountProduct($first));
6363

6464
$transfer = $first->wallet->gift($second, $product);
6565
$this->assertEquals($first->balance, 0);
@@ -69,7 +69,7 @@ public function testRefund(): void
6969
$this->assertFalse($second->wallet->safeRefund($product));
7070
$this->assertTrue($second->wallet->refundGift($product));
7171

72-
$this->assertEquals($first->balance, $product->getAmountProduct());
72+
$this->assertEquals($first->balance, $product->getAmountProduct($first));
7373
$this->assertEquals($second->balance, 0);
7474

7575
$this->assertNull($second->wallet->safeGift($first, $product));
@@ -78,22 +78,22 @@ public function testRefund(): void
7878
$this->assertNotNull($transfer);
7979
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
8080

81-
$this->assertEquals($second->balance, -$product->getAmountProduct());
81+
$this->assertEquals($second->balance, -$product->getAmountProduct($second));
8282

8383
$second->deposit(-$second->balance);
8484
$this->assertEquals($second->balance, 0);
8585

86-
$first->withdraw($product->getAmountProduct());
86+
$first->withdraw($product->getAmountProduct($first));
8787
$this->assertEquals($first->balance, 0);
8888

8989
$product->withdraw($product->balance);
9090
$this->assertEquals($product->balance, 0);
9191

9292
$this->assertFalse($first->safeRefundGift($product));
9393
$this->assertTrue($first->forceRefundGift($product));
94-
$this->assertEquals($product->balance, -$product->getAmountProduct());
94+
$this->assertEquals($product->balance, -$product->getAmountProduct($second));
9595

96-
$this->assertEquals($second->balance, $product->getAmountProduct());
96+
$this->assertEquals($second->balance, $product->getAmountProduct($second));
9797
$second->withdraw($second->balance);
9898
$this->assertEquals($second->balance, 0);
9999
}

tests/MinTaxTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public function testPay(): void
2323
'quantity' => 1,
2424
]);
2525

26-
$fee = (int)($product->price * $product->getFeePercent() / 100);
26+
$fee = (int)($product->getAmountProduct($buyer) * $product->getFeePercent() / 100);
2727
if ($fee < $product->getMinimalFee()) {
2828
$fee = $product->getMinimalFee();
2929
}
3030

31-
$balance = $product->price + $fee;
31+
$balance = $product->getAmountProduct($buyer) + $fee;
3232

3333
$this->assertEquals($buyer->balance, 0);
3434
$buyer->deposit($balance);
@@ -45,7 +45,7 @@ public function testPay(): void
4545
$deposit = $transfer->deposit;
4646

4747
$this->assertEquals($withdraw->amount, -$balance);
48-
$this->assertEquals($deposit->amount, $product->getAmountProduct());
48+
$this->assertEquals($deposit->amount, $product->getAmountProduct($buyer));
4949
$this->assertNotEquals($deposit->amount, $withdraw->amount);
5050
$this->assertEquals($transfer->fee, $fee);
5151

tests/Models/Item.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Bavix\Wallet\Interfaces\Customer;
66
use Bavix\Wallet\Interfaces\Product;
7+
use Bavix\Wallet\Services\WalletService;
8+
use Bavix\Wallet\Test\Common\Models\Wallet;
79
use Bavix\Wallet\Traits\HasWallet;
810
use Illuminate\Database\Eloquent\Model;
911

@@ -43,11 +45,16 @@ public function canBuy(Customer $customer, int $quantity = 1, bool $force = null
4345
}
4446

4547
/**
48+
* @param Customer $customer
4649
* @return int
4750
*/
48-
public function getAmountProduct(): int
51+
public function getAmountProduct(Customer $customer): int
4952
{
50-
return $this->price;
53+
/**
54+
* @var Wallet $wallet
55+
*/
56+
$wallet = app(WalletService::class)->getWallet($customer);
57+
return $this->price + $wallet->holder_id;
5158
}
5259

5360
/**

tests/MultiWalletGiftTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public function testGiftWalletToUser(): void
3434
* @var Item $item
3535
*/
3636
$item = factory(Item::class)->create();
37-
$transaction = $wallet->deposit($item->getAmountProduct());
37+
$transaction = $wallet->deposit($item->getAmountProduct($wallet));
3838
$this->assertEquals($transaction->amount, $wallet->balance);
39-
$this->assertEquals($item->getAmountProduct(), $wallet->balance);
39+
$this->assertEquals($item->getAmountProduct($wallet), $wallet->balance);
4040
$this->assertNotNull($transaction);
4141

4242
$transfer = $wallet->gift($second, $item);

0 commit comments

Comments
 (0)