Skip to content

Commit 77b5aeb

Browse files
authored
Merge pull request #31 from bavix/2.4
2.4
2 parents 56ba90f + 6aa16e5 commit 77b5aeb

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Add zh-CN trans. @MoeCasts
10+
- Add ru trans
11+
- Add method `holderTransfers`
12+
13+
### Changed
14+
- optimize `getWallet` method
15+
- optimize relations wallets
16+
17+
### Fixed
18+
- fixed getting a default wallet @MoeCasts
819

920
## [2.3.2] - 2019-05-13
1021
### Fixed

resources/lang/ru/errors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'price_positive' => 'Цена должна быть положительной',
5+
'product_stock' => 'Товар отсутствует',
6+
'wallet_empty' => 'Кошелёк пуст',
7+
'insufficient_funds' => 'Недостаточно средств',
8+
];

resources/lang/zh-CN/errors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'price_positive' => '价格不能为负数',
5+
'product_stock' => '库存不足',
6+
'wallet_empty' => '钱包为空',
7+
'insufficient_funds' => '余额不足',
8+
];

src/Traits/CanPay.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Bavix\Wallet\Exceptions\ProductEnded;
66
use Bavix\Wallet\Interfaces\Product;
77
use Bavix\Wallet\Models\Transfer;
8+
use Bavix\Wallet\Models\Wallet;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\ModelNotFoundException;
1011
use Illuminate\Support\Facades\DB;
@@ -156,7 +157,7 @@ public function paid(Product $product, bool $gifts = false): ?Transfer
156157
* @var Model $product
157158
* @var Transfer $query
158159
*/
159-
$query = $this->transfers();
160+
$query = $this->holderTransfers();
160161
return $query
161162
->where('to_type', $product->getMorphClass())
162163
->where('to_id', $product->getKey())

src/Traits/CanPayFloat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ trait CanPayFloat
1717
CanPay::assemble insteadof HasWalletFloat;
1818
CanPay::change insteadof HasWalletFloat;
1919
CanPay::transactions insteadof HasWalletFloat;
20+
CanPay::holderTransfers insteadof HasWalletFloat;
2021
CanPay::transfers insteadof HasWalletFloat;
2122
CanPay::wallet insteadof HasWalletFloat;
2223
CanPay::getBalanceAttribute insteadof HasWalletFloat;

src/Traits/HasWallet.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null,
296296
});
297297
}
298298

299+
/**
300+
* holder transfers
301+
*
302+
* @return MorphMany
303+
*/
304+
public function holderTransfers(): MorphMany
305+
{
306+
if ($this instanceof WalletModel) {
307+
return $this->holder->transfers();
308+
}
309+
310+
return $this->transfers();
311+
}
312+
299313
/**
300314
* the transfer table is used to confirm the payment
301315
* this method receives all transfers
@@ -304,8 +318,7 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null,
304318
*/
305319
public function transfers(): MorphMany
306320
{
307-
return ($this instanceof WalletModel ? $this->holder : $this)
308-
->morphMany(config('wallet.transfer.model'), 'from');
321+
return $this->morphMany(config('wallet.transfer.model'), 'from');
309322
}
310323

311324
/**
@@ -318,6 +331,7 @@ public function wallet(): MorphOne
318331
{
319332
return ($this instanceof WalletModel ? $this->holder : $this)
320333
->morphOne(config('wallet.wallet.model'), 'holder')
334+
->where('slug', config('wallet.wallet.default.slug'))
321335
->withDefault([
322336
'name' => config('wallet.wallet.default.name'),
323337
'slug' => config('wallet.wallet.default.slug'),

src/Traits/HasWallets.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ trait HasWallets
2626
*/
2727
private $_wallets = [];
2828

29+
/**
30+
* @var bool
31+
*/
32+
private $_loadedWallets = false;
33+
2934
/**
3035
* Get wallet by slug
3136
*
@@ -43,6 +48,14 @@ trait HasWallets
4348
*/
4449
public function getWallet(string $slug): ?WalletModel
4550
{
51+
if (!$this->_loadedWallets && $this->relationLoaded('wallets')) {
52+
$this->_loadedWallets = true;
53+
$wallets = $this->getRelation('wallets');
54+
foreach ($wallets as $wallet) {
55+
$this->_wallets[$wallet->slug] = $wallet;
56+
}
57+
}
58+
4659
if (!\array_key_exists($slug, $this->_wallets)) {
4760
$this->_wallets[$slug] = $this->wallets()
4861
->where('slug', $slug)

0 commit comments

Comments
 (0)