1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\controllers; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\client\models\Client; |
14
|
|
|
use hipanel\modules\finance\cart\CartFinisher; |
15
|
|
|
use hipanel\modules\finance\Module; |
16
|
|
|
use hiqdev\yii2\merchant\models\DepositForm; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\filters\AccessControl; |
19
|
|
|
|
20
|
|
|
class CartController extends \yii\web\Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Module |
24
|
|
|
*/ |
25
|
|
|
public $module; |
26
|
|
|
|
27
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
'access' => [ |
31
|
|
|
'class' => AccessControl::class, |
32
|
|
|
'only' => ['deposit', 'select', 'partial', 'full', 'finish'], |
33
|
|
|
'rules' => [ |
34
|
|
|
[ |
35
|
|
|
'allow' => true, |
36
|
|
|
'roles' => ['@'], |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param float $amount |
45
|
|
|
* @return \yii\web\Response |
46
|
|
|
*/ |
47
|
|
|
public function renderDeposit($amount) |
48
|
|
|
{ |
49
|
|
|
$form = new DepositForm(['amount' => $amount]); |
50
|
|
|
$form->finishUrl = '/finance/cart/finish'; |
51
|
|
|
|
52
|
|
|
return $this->module->getMerchant()->renderDeposit($form); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function actionSelect() |
56
|
|
|
{ |
57
|
|
|
$client = Client::findOne(['id' => Yii::$app->user->identity->id]); |
58
|
|
|
$cart = $this->module->getCart(); |
59
|
|
|
$total = $cart->getTotal(); |
60
|
|
|
$budget = $client->balance + $client->credit; |
61
|
|
|
|
62
|
|
|
if ($budget <= 0 && $total > 0) { |
63
|
|
|
return $this->renderDeposit($total); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->render('select', [ |
67
|
|
|
'cart' => $cart, |
68
|
|
|
'client' => $client, |
69
|
|
|
'budget' => $budget, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function actionFull() |
74
|
|
|
{ |
75
|
|
|
return $this->renderDeposit($this->module->getCart()->getTotal()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function actionPartial() |
79
|
|
|
{ |
80
|
|
|
$client = Client::findOne(['id' => Yii::$app->user->identity->id]); |
81
|
|
|
$cart = $this->module->getCart(); |
82
|
|
|
|
83
|
|
|
return $this->renderDeposit($cart->total - $client->balance - $client->credit); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function actionFinish() |
87
|
|
|
{ |
88
|
|
|
$cart = $this->module->getCart(); |
89
|
|
|
|
90
|
|
|
$finisher = new CartFinisher(['cart' => $cart]); |
91
|
|
|
$finisher->run(); |
92
|
|
|
|
93
|
|
|
$client = Client::findOne(['id' => Yii::$app->user->identity->id]); |
94
|
|
|
|
95
|
|
|
return $this->render('finish', [ |
96
|
|
|
'balance' => $client->balance, |
97
|
|
|
'success' => $finisher->getSuccess(), |
98
|
|
|
'error' => $finisher->getError(), |
99
|
|
|
'pending' => $finisher->getPending(), |
100
|
|
|
'remarks' => (array) Yii::$app->getView()->params['remarks'], |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.