From 94881d1ff6d3aa40f705b26309337a818c26cbe5 Mon Sep 17 00:00:00 2001 From: merdan Date: Sat, 13 Nov 2021 18:55:19 +0500 Subject: [PATCH] transaction balance is ready for testing --- plugins/rainlab/user/models/User.php | 5 +- plugins/tps/birzha/Plugin.php | 5 + plugins/tps/birzha/components/Balance.php | 12 +- plugins/tps/birzha/components/OfferForm.php | 5 +- plugins/tps/birzha/components/PaymentApi.php | 24 ++-- plugins/tps/birzha/controllers/Countries.php | 4 +- plugins/tps/birzha/controllers/Currencies.php | 4 +- plugins/tps/birzha/controllers/Measures.php | 6 +- plugins/tps/birzha/controllers/Offers.php | 38 ------ plugins/tps/birzha/controllers/Payments.php | 18 +-- plugins/tps/birzha/controllers/Terms.php | 6 +- .../controllers/categories/config_filter.yaml | 2 +- .../birzha/controllers/payments/update.htm | 111 +++++++----------- plugins/tps/birzha/models/Payment.php | 63 +++++++++- plugins/tps/birzha/models/Product.php | 23 +++- plugins/tps/birzha/models/Transaction.php | 13 +- .../tps/birzha/models/payment/columns.yaml | 2 +- plugins/tps/birzha/models/payment/fields.yaml | 8 +- plugins/tps/birzha/plugin.yaml | 59 ++++++---- plugins/tps/birzha/updates/version.yaml | 6 + themes/birzha/partials/header.htm | 2 +- 21 files changed, 225 insertions(+), 191 deletions(-) delete mode 100644 plugins/tps/birzha/controllers/Offers.php diff --git a/plugins/rainlab/user/models/User.php b/plugins/rainlab/user/models/User.php index 698701bc4..c1812994c 100644 --- a/plugins/rainlab/user/models/User.php +++ b/plugins/rainlab/user/models/User.php @@ -53,7 +53,7 @@ class User extends UserBase public $hasMany = [ 'products' => ['TPS\Birzha\Models\Product', 'key' => 'vendor_id'], - 'transaction' => ['TPS\Birzha\Models\Transaction'] + 'transactions' => ['TPS\Birzha\Models\Transaction'] ]; /** @@ -164,6 +164,9 @@ class User extends UserBase // Getters // + public function getBalance(){ + return $this->transactions()->sum('amount'); + } /** * Gets a code for when the user is persisted to a cookie or session which identifies the user. * @return string diff --git a/plugins/tps/birzha/Plugin.php b/plugins/tps/birzha/Plugin.php index ebfbcdbb4..231425c4e 100644 --- a/plugins/tps/birzha/Plugin.php +++ b/plugins/tps/birzha/Plugin.php @@ -44,10 +44,15 @@ class Plugin extends PluginBase 'status' => [$this, 'statusListColumn'], 'vendor' => [$this, 'vendorLinkListColumn'], 'user' => [$this, 'userLinkListColumn'], + 'money' => [$this, 'moneyColumn'], ]; } + public function moneyColumn($value, $column, $record){ + return ''.$value.'tmt'; + } + public function userLinkListColumn($value, $column, $record){ return ''.$value.''; } diff --git a/plugins/tps/birzha/components/Balance.php b/plugins/tps/birzha/components/Balance.php index 93a5e1cc8..c0ea24e76 100644 --- a/plugins/tps/birzha/components/Balance.php +++ b/plugins/tps/birzha/components/Balance.php @@ -55,7 +55,7 @@ class Balance extends ComponentBase protected function payOnline($formData) { $payment = $this->createNewPayment(false, $formData); - + $url = $this->controller->pageUrl('bank_result.htm', ['payment_id' => $payment->id]); $response = CardApi::registerOrder($payment, $url); @@ -84,13 +84,13 @@ class Balance extends ComponentBase ]; $this->validateForm($data, $rules); - + $newPayment = new Payment; $newPayment->user_id = \Auth::user()->id; $newPayment->amount = 0; $newPayment->payment_type = "bank"; - $newPayment->status = "new"; - $newPayment->save(); +// $newPayment->status = "new"; +// $newPayment->save(); // attach file to payment $newPayment->bank_file = \Input::file('bank_file'); @@ -114,7 +114,7 @@ class Balance extends ComponentBase $newPayment->user_id = \Auth::user()->id; $newPayment->amount = $formData['amount']; $newPayment->payment_type = $formData['payment_type']; - $newPayment->status = "new"; +// $newPayment->status = "new"; $newPayment->save(); // attach file to payment @@ -125,4 +125,4 @@ class Balance extends ComponentBase return $newPayment; } -} \ No newline at end of file +} diff --git a/plugins/tps/birzha/components/OfferForm.php b/plugins/tps/birzha/components/OfferForm.php index f979dbb4b..5025ca94d 100644 --- a/plugins/tps/birzha/components/OfferForm.php +++ b/plugins/tps/birzha/components/OfferForm.php @@ -195,8 +195,8 @@ class OfferForm extends ComponentBase // ... message about not enough money throw new ValidationException(['money' => trans('validation.low_balance')]); } else { - $user->balance = $user->balance - Settings::getValue('fee'); - $user->save(); +// $user->balance = $user->balance - Settings::getValue('fee'); +// $user->save(); $product = Product::find(Input::get('product_id')); //save how much user payed because fee can be changed by admin tomorrow @@ -211,6 +211,7 @@ class OfferForm extends ComponentBase } } + // after deleting a photo go the second form_step public function onImageDelete() { // dd(Input::get('product_image_id')); diff --git a/plugins/tps/birzha/components/PaymentApi.php b/plugins/tps/birzha/components/PaymentApi.php index 65532a0f9..b93fc4019 100644 --- a/plugins/tps/birzha/components/PaymentApi.php +++ b/plugins/tps/birzha/components/PaymentApi.php @@ -10,7 +10,7 @@ use TPS\Birzha\Classes\Payment as CardApi; class PaymentApi extends ComponentBase { public $balance_message; - + public function componentDetails() { return [ 'name' => 'Payment API', @@ -38,27 +38,27 @@ class PaymentApi extends ComponentBase $responce = json_decode(CardApi::getStatus($payment->order_id), true); if( $responce['ErrorCode'] == 0 && $responce['OrderStatus'] == 2) { - + // if page bank_result page is refreshed - if($payment->status === 'payed') { + if($payment->status === 'approved') { return Redirect::to('/'); } - Payment::where('id', $payment_id)->update(['status' => 'payed']); + Payment::where('id', $payment_id)->update(['status' => 'approved']); + +// $user = $payment->user; +// $user->balance += $payment->amount; +// $user->save(); + - $user = $payment->user; - $user->balance += $payment->amount; - $user->save(); - - $this->balance_message = trans('validation.balance.fill_up_succes'); - + } else { $this->balance_message = trans('validation.balance.fill_up_fail'); } } else { - + $this->balance_message = trans('validation.balance.fill_up_fail'); } } -} \ No newline at end of file +} diff --git a/plugins/tps/birzha/controllers/Countries.php b/plugins/tps/birzha/controllers/Countries.php index 610ca4ba0..f115fef08 100644 --- a/plugins/tps/birzha/controllers/Countries.php +++ b/plugins/tps/birzha/controllers/Countries.php @@ -6,13 +6,13 @@ use BackendMenu; class Countries extends Controller { public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ]; - + public $listConfig = 'config_list.yaml'; public $formConfig = 'config_form.yaml'; public function __construct() { parent::__construct(); - BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'countries'); + BackendMenu::setContext('TPS.Birzha', 'dictionary', 'countries'); } } diff --git a/plugins/tps/birzha/controllers/Currencies.php b/plugins/tps/birzha/controllers/Currencies.php index 1f297ef42..81e71eb9f 100644 --- a/plugins/tps/birzha/controllers/Currencies.php +++ b/plugins/tps/birzha/controllers/Currencies.php @@ -6,13 +6,13 @@ use BackendMenu; class Currencies extends Controller { public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ]; - + public $listConfig = 'config_list.yaml'; public $formConfig = 'config_form.yaml'; public function __construct() { parent::__construct(); - BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'currency'); + BackendMenu::setContext('TPS.Birzha', 'dictionary', 'currency'); } } diff --git a/plugins/tps/birzha/controllers/Measures.php b/plugins/tps/birzha/controllers/Measures.php index 0053dc5b9..f883e6170 100644 --- a/plugins/tps/birzha/controllers/Measures.php +++ b/plugins/tps/birzha/controllers/Measures.php @@ -6,17 +6,17 @@ use BackendMenu; class Measures extends Controller { public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ]; - + public $listConfig = 'config_list.yaml'; public $formConfig = 'config_form.yaml'; public $requiredPermissions = [ - 'measure' + 'measure' ]; public function __construct() { parent::__construct(); - BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'measure'); + BackendMenu::setContext('TPS.Birzha', 'dictionary', 'measure'); } } diff --git a/plugins/tps/birzha/controllers/Offers.php b/plugins/tps/birzha/controllers/Offers.php deleted file mode 100644 index 1c0d79d46..000000000 --- a/plugins/tps/birzha/controllers/Offers.php +++ /dev/null @@ -1,38 +0,0 @@ -stats = Offer::select('status',DB::raw('COUNT(id) as count')) - ->groupBy('status') - ->pluck('count','status'); -// dd($this->stats); -// dd(Category::select('status',DB::raw('COUNT(id) as count'))->groupBy('status')->pluck('count','status')[1]); - } - - public function getRecordsStats($status){ - if($this->stats) - return $this->stats[$status] ?? 0; - return 0; - } -} diff --git a/plugins/tps/birzha/controllers/Payments.php b/plugins/tps/birzha/controllers/Payments.php index e3f33f56c..122e71c0c 100644 --- a/plugins/tps/birzha/controllers/Payments.php +++ b/plugins/tps/birzha/controllers/Payments.php @@ -8,12 +8,12 @@ use TPS\Birzha\Models\Payment; class Payments extends Controller { - public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ]; + public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController']; public $listConfig = 'config_list.yaml'; public $formConfig = 'config_form.yaml'; - public $stats; + public $requiredPermissions = [ 'payment' ]; @@ -23,11 +23,14 @@ class Payments extends Controller parent::__construct(); BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'payments'); + + } + public function index(){ + parent::index(); $this->stats = Payment::groupBy('status','payment_type') ->selectRaw('status, payment_type, COUNT(id) as count, SUM(amount) as total_amount') ->get(); } - public function getRecordsStats($status){ return $this->stats->where('status',$status) ->sum('count') @@ -47,13 +50,4 @@ class Payments extends Controller ->sum('total_amount') ?? 0; } - - public function onApprove(){ - Log::info('test approve'); - } - - public function onDecline(){ - Log::info('test approve'); - } - //todo amount funksia yazmaly } diff --git a/plugins/tps/birzha/controllers/Terms.php b/plugins/tps/birzha/controllers/Terms.php index 750c28920..b9999886d 100644 --- a/plugins/tps/birzha/controllers/Terms.php +++ b/plugins/tps/birzha/controllers/Terms.php @@ -6,17 +6,17 @@ use BackendMenu; class Terms extends Controller { public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ]; - + public $listConfig = 'config_list.yaml'; public $formConfig = 'config_form.yaml'; public $requiredPermissions = [ - 'term' + 'term' ]; public function __construct() { parent::__construct(); - BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'terms'); + BackendMenu::setContext('TPS.Birzha', 'dictionary', 'terms'); } } diff --git a/plugins/tps/birzha/controllers/categories/config_filter.yaml b/plugins/tps/birzha/controllers/categories/config_filter.yaml index dc73eee44..fa975f81f 100644 --- a/plugins/tps/birzha/controllers/categories/config_filter.yaml +++ b/plugins/tps/birzha/controllers/categories/config_filter.yaml @@ -2,7 +2,7 @@ scopes: status: label: Hide UnActive type: checkbox - default: 1 + default: 0 conditions: status = true created_at: label: Date diff --git a/plugins/tps/birzha/controllers/payments/update.htm b/plugins/tps/birzha/controllers/payments/update.htm index 29cf24e84..c5f008585 100644 --- a/plugins/tps/birzha/controllers/payments/update.htm +++ b/plugins/tps/birzha/controllers/payments/update.htm @@ -1,79 +1,54 @@ - - + -
-
-
- - - - - - - - - -
-
-
fatalError): ?> - 'layout']) ?> + 'layout']) ?> -
- formRender() ?> -
+
+ formRender() ?> +
-
-
+
+
+ + + -
-
- + + + +
+
+ -

fatalError)) ?>

-

+

fatalError)) ?>

+

diff --git a/plugins/tps/birzha/models/Payment.php b/plugins/tps/birzha/models/Payment.php index 75709afad..5e1ecf688 100644 --- a/plugins/tps/birzha/models/Payment.php +++ b/plugins/tps/birzha/models/Payment.php @@ -1,5 +1,6 @@ [Transaction::class, 'name' => 'transactable'] + 'transaction' => [Transaction::class, 'name' => 'transactable','delete'=>true] ]; public $belongsTo = [ @@ -39,11 +40,17 @@ class Payment extends Model ]; public function beforeUpdate() { - if($this->status == 'payed') { - $user = $this->user; - $user->balance +=$this->amount; - $user->save(); + if($this->status == 'approved' || $this->payment_type == 'gift' ) { + if(!$transaction = $this->transaction) + { + $this->createTransaction(); + } + else{ + $transaction->amount = $this->amount; + $transaction->save(); + } } + } public function beforeValidate() @@ -55,4 +62,50 @@ class Payment extends Model } } + + protected function beforeCreate() + { + parent::beforeCreate(); + if(\App::runningInBackend()) { + $this->payment_type = 'gift'; + $this->created_at = Carbon::now(); + $this->updated_at = Carbon::now(); + + } + else{ + $this->status = 'new'; + } + } + + protected function afterCreate() + { + parent::afterCreate(); + + if($this->payment_type == 'gift'){ + $this->createTransaction(); + } + } + + private function createTransaction(){ + $transaction = new Transaction([ + 'user_id' => $this->user_id, + 'amount' => $this->amount + ]); + $this->transaction()->save($transaction); + } + public function filterFields($fields, $context = null){ + if($this->payment_type == 'online'){ + $fields->amount->disabled = true; + $fields->user->disabled = true; + $fields->created_at->disabled = true; + $fields->status->disabled = true; + + } + + if ($this->payment_type == 'gift') { + $fields->status->hidden = true; + $fields->bank_file->hidden = true; + } + + } } diff --git a/plugins/tps/birzha/models/Product.php b/plugins/tps/birzha/models/Product.php index 0766bf988..ad4374e37 100644 --- a/plugins/tps/birzha/models/Product.php +++ b/plugins/tps/birzha/models/Product.php @@ -58,7 +58,7 @@ class Product extends Model ]; public $morphOne = [ - 'transaction' => [Transaction::class, 'name' => 'transactable'] + 'transaction' => [Transaction::class, 'name' => 'transactable','delete' => true] ]; public $attachMany = [ @@ -110,15 +110,32 @@ class Product extends Model } + private function createTransaction(){ + $transaction = new Transaction([ + 'user_id' => $this->vendor_id, + 'amount' => 0 - $this->payed_fee_for_publ + ]); + $this->transaction()->save($transaction); + } + public function beforeUpdate() { - if($this->status == 'approved' && !$this->ends_at) { + if($this->status == 'new'){ + if(!$transaction = $this->transaction) + $this->createTransaction(); + else { + $transaction->amount = 0 - $this->payed_fee_for_publ; + $transaction->save(); + } + } + elseif($this->status == 'approved' && !$this->ends_at) { // $createdAt = Carbon::parse($this->created_at); $this->ends_at = \Carbon\Carbon::now()->addDays(Settings::getValue('duration')); } - if($this->status == 'denied') { + elseif($this->status == 'denied') { // give fee back to the user, because his post has been denied $user = $this->vendor; + //todo delete associated transaction $user->balance = $user->balance + $this->payed_fee_for_publ; $user->save(); } diff --git a/plugins/tps/birzha/models/Transaction.php b/plugins/tps/birzha/models/Transaction.php index 672589fd4..d3e830580 100644 --- a/plugins/tps/birzha/models/Transaction.php +++ b/plugins/tps/birzha/models/Transaction.php @@ -7,15 +7,24 @@ class Transaction extends Model { use \October\Rain\Database\Traits\Validation; - use \October\Rain\Database\Traits\Sortable; + protected $table = 'tps_birzha_transactions'; + + protected $dates = ['created_at','ends_at']; public $morphTo = [ 'transactable' => [] ]; - protected $fillable = ['amount']; + protected $fillable = ['amount','user_id']; public $belongsTo = [ 'user' => 'RainLab\User\Models\User' ]; + + /** + * @var array Validation rules + */ + public $rules = [ + 'amount' => 'required|numeric' + ]; } diff --git a/plugins/tps/birzha/models/payment/columns.yaml b/plugins/tps/birzha/models/payment/columns.yaml index dc1955f6c..3a99d9027 100644 --- a/plugins/tps/birzha/models/payment/columns.yaml +++ b/plugins/tps/birzha/models/payment/columns.yaml @@ -13,7 +13,7 @@ columns: valueFrom: email amount: label: Amount - type: text + type: money payment_type: label: 'Payment type' type: status diff --git a/plugins/tps/birzha/models/payment/fields.yaml b/plugins/tps/birzha/models/payment/fields.yaml index def4183fd..c0c8588f5 100644 --- a/plugins/tps/birzha/models/payment/fields.yaml +++ b/plugins/tps/birzha/models/payment/fields.yaml @@ -11,13 +11,12 @@ fields: nameFrom: email descriptionFrom: description span: auto - readOnly: 1 type: relation created_at: label: 'Payment date' mode: datetime span: auto - readOnly: 1 + type: datepicker payment_type: label: 'Payment type' @@ -26,7 +25,7 @@ fields: online: Online gift: Gift span: auto - readOnly: 1 + disabled: 1 type: balloon-selector status: label: Status @@ -34,10 +33,9 @@ fields: new: new approved: approved declined: declined - payed: payed span: auto - readOnly: 1 type: balloon-selector + dependsOn: payment_type note: label: Note size: '' diff --git a/plugins/tps/birzha/plugin.yaml b/plugins/tps/birzha/plugin.yaml index d49945c68..05177c4e1 100644 --- a/plugins/tps/birzha/plugin.yaml +++ b/plugins/tps/birzha/plugin.yaml @@ -22,36 +22,18 @@ navigation: icon: icon-cube permissions: - product - measure: - label: Measure - url: tps/birzha/measures - icon: icon-tachometer - permissions: - - measure - terms: - label: Terms - url: tps/birzha/terms - icon: icon-delicious - permissions: - - term - currency: - label: Currencies - url: tps/birzha/currencies - icon: icon-dollar - permissions: - - currency payments: label: Payments url: tps/birzha/payments icon: icon-money permissions: - payment - countries: - label: Countries - url: tps/birzha/countries - icon: icon-flag-o + transactions: + label: Transactions + url: tps/birzha/transactions + icon: icon-usd permissions: - - country + - payment frontend: label: Frontend url: tps/birzha/sliders @@ -61,7 +43,7 @@ navigation: sliders: label: Sliders url: tps/birzha/sliders - icon: icon-sitemap + icon: icon-image permissions: - slider messages: @@ -70,6 +52,35 @@ navigation: icon: icon-envelope-o permissions: - messages + dictionary: + label: Dictionary + url: tps/birzha/terms + icon: icon-life-ring + sideMenu: + terms: + label: Terms + url: tps/birzha/terms + icon: icon-delicious + permissions: + - term + measure: + label: Measure + url: tps/birzha/measures + icon: icon-tachometer + permissions: + - measure + currency: + label: Currencies + url: tps/birzha/currencies + icon: icon-money + permissions: + - currency + countries: + label: Countries + url: tps/birzha/countries + icon: icon-flag + permissions: + - currency permissions: category: tab: Catalog diff --git a/plugins/tps/birzha/updates/version.yaml b/plugins/tps/birzha/updates/version.yaml index c8e61d167..e52693b13 100644 --- a/plugins/tps/birzha/updates/version.yaml +++ b/plugins/tps/birzha/updates/version.yaml @@ -230,3 +230,9 @@ 1.0.80: - 'Updated table tps_birzha_products' - builder_table_update_tps_birzha_products_24.php +1.0.81: + - 'Created table tps_birzha_transactions' + - builder_table_create_tps_birzha_transactions.php +1.0.82: + - 'Updated table tps_birzha_transactions' + - builder_table_update_tps_birzha_transactions.php diff --git a/themes/birzha/partials/header.htm b/themes/birzha/partials/header.htm index 092ca93db..374908f3a 100644 --- a/themes/birzha/partials/header.htm +++ b/themes/birzha/partials/header.htm @@ -118,7 +118,7 @@ code = "main-top"
- {{user.balance}} TMT + {{user.getBalance}} TMT