Skip to content

Commit 1735d96

Browse files
author
Babichev Maxim
committed
try discount
1 parent 158a9b7 commit 1735d96

File tree

12 files changed

+476
-4
lines changed

12 files changed

+476
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Bavix\Wallet\Models\Transfer;
4+
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class AddDiscountTransfersTable extends Migration
9+
{
10+
11+
/**
12+
* @return string
13+
*/
14+
protected function table(): string
15+
{
16+
return (new Transfer())->getTable();
17+
}
18+
19+
/**
20+
* @return void
21+
*/
22+
public function up(): void
23+
{
24+
Schema::table($this->table(), function (Blueprint $table) {
25+
$table->bigInteger('discount')
26+
->default(0)
27+
->after('withdraw_id');
28+
});
29+
}
30+
31+
/**
32+
* @return void
33+
*/
34+
public function down(): void
35+
{
36+
Schema::table($this->table(), function (Blueprint $table) {
37+
$table->dropColumn('discount');
38+
});
39+
}
40+
41+
}

src/Interfaces/Discount.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
interface Discount extends Product
6+
{
7+
/**
8+
* @param Customer $customer
9+
* @return int
10+
*/
11+
public function getDiscountProduct(Customer $customer): int;
12+
}

src/Objects/Bring.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Bring
4646
*/
4747
protected $fee;
4848

49+
/**
50+
* @var int
51+
*/
52+
protected $discount;
53+
4954
/**
5055
* Bring constructor.
5156
* @throws
@@ -73,6 +78,16 @@ public function setStatus(string $status): self
7378
return $this;
7479
}
7580

81+
/**
82+
* @param int $discount
83+
* @return static
84+
*/
85+
public function setDiscount(int $discount): self
86+
{
87+
$this->discount = $discount;
88+
return $this;
89+
}
90+
7691
/**
7792
* @return Wallet
7893
*/
@@ -153,6 +168,14 @@ public function getUuid(): string
153168
return $this->uuid;
154169
}
155170

171+
/**
172+
* @return string
173+
*/
174+
public function getDiscount(): int
175+
{
176+
return $this->discount;
177+
}
178+
156179
/**
157180
* @return int
158181
*/
@@ -199,6 +222,7 @@ public function toArray(): array
199222
'from_id' => $this->getFrom()->getKey(),
200223
'to_type' => $this->getTo()->getMorphClass(),
201224
'to_id' => $this->getTo()->getKey(),
225+
'discount' => $this->getDiscount(),
202226
'fee' => $this->getFee(),
203227
'uuid' => $this->getUuid(),
204228
];

src/Services/CommonService.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ public function transfer(Wallet $from, Wallet $to, int $amount, ?array $meta = n
4545
public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): Transfer
4646
{
4747
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($from, $to, $amount, $meta, $status) {
48+
$from = app(WalletService::class)->getWallet($from);
49+
$discount = app(WalletService::class)->discount($from, $to);
50+
$amount -= $discount;
51+
4852
$fee = app(WalletService::class)->fee($to, $amount);
4953
$withdraw = $this->forceWithdraw($from, $amount + $fee, $meta);
5054
$deposit = $this->deposit($to, $amount, $meta);
5155

52-
$from = app(WalletService::class)
53-
->getWallet($from);
54-
5556
$transfers = $this->multiBrings([
5657
app(Bring::class)
5758
->setStatus($status)
5859
->setDeposit($deposit)
5960
->setWithdraw($withdraw)
61+
->setDiscount($discount)
6062
->setFrom($from)
6163
->setTo($to)
6264
]);

src/Services/WalletService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Bavix\Wallet\Services;
44

55
use Bavix\Wallet\Exceptions\AmountInvalid;
6+
use Bavix\Wallet\Interfaces\Customer;
7+
use Bavix\Wallet\Interfaces\Discount;
68
use Bavix\Wallet\Interfaces\MinimalTaxable;
9+
use Bavix\Wallet\Interfaces\Product;
710
use Bavix\Wallet\Interfaces\Storable;
811
use Bavix\Wallet\Interfaces\Taxable;
912
use Bavix\Wallet\Interfaces\Wallet;
@@ -14,6 +17,21 @@
1417
class WalletService
1518
{
1619

20+
/**
21+
* @param Wallet $customer
22+
* @param Wallet $product
23+
* @return int
24+
*/
25+
public function discount(Wallet $customer, Wallet $product): int
26+
{
27+
if ($customer instanceof Customer && $product instanceof Discount) {
28+
return $product->getDiscountProduct($customer);
29+
}
30+
31+
// without discount
32+
return 0;
33+
}
34+
1735
/**
1836
* @param Wallet $object
1937
* @return int

src/Traits/CanExchange.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
6363

6464
$transfers = app(CommonService::class)->multiBrings([
6565
app(Bring::class)
66+
->setDiscount(0)
6667
->setStatus(Transfer::STATUS_EXCHANGE)
6768
->setDeposit($deposit)
6869
->setWithdraw($withdraw)

src/Traits/HasGift.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
6464
* That's why I address him like this!
6565
*/
6666
return app(DbService::class)->transaction(static function () use ($santa, $to, $product, $force) {
67-
$amount = $product->getAmountProduct();
67+
$discount = app(WalletService::class)->discount($santa, $product);
68+
$amount = $product->getAmountProduct() - $discount;
6869
$meta = $product->getMetaProduct();
6970
$fee = app(WalletService::class)
7071
->fee($product, $amount);
@@ -87,6 +88,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
8788
$transfers = $commonService->assemble([
8889
app(Bring::class)
8990
->setStatus(Transfer::STATUS_GIFT)
91+
->setDiscount($discount)
9092
->setDeposit($deposit)
9193
->setWithdraw($withdraw)
9294
->setFrom($from)

0 commit comments

Comments
 (0)