1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
4
|
|
|
|
5
|
|
|
use function app; |
6
|
|
|
use Bavix\Wallet\Interfaces\Mathable; |
7
|
|
|
use Bavix\Wallet\Interfaces\Product; |
8
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
9
|
|
|
use Bavix\Wallet\Models\Transfer; |
10
|
|
|
use Bavix\Wallet\Objects\Bring; |
11
|
|
|
use Bavix\Wallet\Services\CommonService; |
12
|
|
|
use Bavix\Wallet\Services\DbService; |
13
|
|
|
use Bavix\Wallet\Services\LockService; |
14
|
|
|
use Bavix\Wallet\Services\WalletService; |
15
|
|
|
use Throwable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait HasGift. |
19
|
|
|
*/ |
20
|
|
|
trait HasGift |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Give the goods safely. |
24
|
|
|
* |
25
|
|
|
* @param Wallet $to |
26
|
|
|
* @param Product $product |
27
|
|
|
* @param bool $force |
28
|
|
|
* @return Transfer|null |
29
|
|
|
*/ |
30
|
3 |
|
public function safeGift(Wallet $to, Product $product, bool $force = null): ?Transfer |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
3 |
|
return $this->gift($to, $product, $force); |
34
|
3 |
|
} catch (Throwable $throwable) { |
35
|
3 |
|
return null; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* From this moment on, each user (wallet) can give |
41
|
|
|
* the goods to another user (wallet). |
42
|
|
|
* This functionality can be organized for gifts. |
43
|
|
|
* |
44
|
|
|
* @param Wallet $to |
45
|
|
|
* @param Product $product |
46
|
|
|
* @param bool $force |
47
|
|
|
* @return Transfer |
48
|
|
|
*/ |
49
|
9 |
|
public function gift(Wallet $to, Product $product, bool $force = null): Transfer |
50
|
|
|
{ |
51
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($to, $product, $force) { |
52
|
|
|
/** |
53
|
|
|
* Who's giving? Let's call him Santa Claus. |
54
|
|
|
* @var Wallet $santa |
55
|
|
|
*/ |
56
|
9 |
|
$santa = $this; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Unfortunately, |
60
|
|
|
* I think it is wrong to make the "assemble" method public. |
61
|
|
|
* That's why I address him like this! |
62
|
|
|
*/ |
63
|
|
|
return app(DbService::class)->transaction(static function () use ($santa, $to, $product, $force) { |
64
|
9 |
|
$math = app(Mathable::class); |
65
|
9 |
|
$discount = app(WalletService::class)->discount($santa, $product); |
66
|
9 |
|
$amount = $math->sub($product->getAmountProduct($santa), $discount); |
67
|
9 |
|
$meta = $product->getMetaProduct(); |
68
|
9 |
|
$fee = app(WalletService::class) |
69
|
9 |
|
->fee($product, $amount); |
|
|
|
|
70
|
|
|
|
71
|
9 |
|
$commonService = app(CommonService::class); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Santa pays taxes. |
75
|
|
|
*/ |
76
|
9 |
|
if (! $force) { |
|
|
|
|
77
|
9 |
|
$commonService->verifyWithdraw($santa, $math->add($amount, $fee)); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
$withdraw = $commonService->forceWithdraw($santa, $math->add($amount, $fee), $meta); |
|
|
|
|
81
|
8 |
|
$deposit = $commonService->deposit($product, $amount, $meta); |
|
|
|
|
82
|
|
|
|
83
|
8 |
|
$from = app(WalletService::class) |
84
|
8 |
|
->getWallet($to); |
85
|
|
|
|
86
|
8 |
|
$transfers = $commonService->assemble([ |
87
|
8 |
|
app(Bring::class) |
88
|
8 |
|
->setStatus(Transfer::STATUS_GIFT) |
89
|
8 |
|
->setDiscount($discount) |
90
|
8 |
|
->setDeposit($deposit) |
91
|
8 |
|
->setWithdraw($withdraw) |
92
|
8 |
|
->setFrom($from) |
93
|
8 |
|
->setTo($product), |
94
|
|
|
]); |
95
|
|
|
|
96
|
8 |
|
return current($transfers); |
97
|
9 |
|
}); |
98
|
9 |
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* to give force). |
103
|
|
|
* |
104
|
|
|
* @param Wallet $to |
105
|
|
|
* @param Product $product |
106
|
|
|
* @return Transfer |
107
|
|
|
*/ |
108
|
3 |
|
public function forceGift(Wallet $to, Product $product): Transfer |
109
|
|
|
{ |
110
|
3 |
|
return $this->gift($to, $product, true); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|