transaction balance is ready for testing

This commit is contained in:
merdan 2021-11-13 18:55:19 +05:00
parent c6a554c142
commit 94881d1ff6
21 changed files with 225 additions and 191 deletions

View File

@ -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

View File

@ -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 '<span style="color: '.($value > 0 ? 'green':'red').'">'.$value.'tmt</span>';
}
public function userLinkListColumn($value, $column, $record){
return '<a href="'.Backend::url('rainlab/user/users/preview',$record->user_id).'" class="btn btn-link">'.$value.'</a>';
}

View File

@ -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;
}
}
}

View File

@ -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'));

View File

@ -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');
}
}
}
}

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -1,38 +0,0 @@
<?php namespace TPS\Birzha\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
use Illuminate\Support\Facades\DB;
use TPS\Birzha\Models\Category;
use TPS\Birzha\Models\Offer;
class Offers extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $requiredPermissions = [
'offer'
];
public $stats;
public function __construct()
{
parent::__construct();
BackendMenu::setContext('TPS.Birzha', 'birzha-menu', 'offers');
$this->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;
}
}

View File

@ -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
}

View File

@ -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');
}
}

View File

@ -2,7 +2,7 @@ scopes:
status:
label: Hide UnActive
type: checkbox
default: 1
default: 0
conditions: status = true
created_at:
label: Date

View File

@ -1,79 +1,54 @@
<?php Block::put('breadcrumb') ?>
<ul>
<li><a href="<?= Backend::url('tps/birzha/payments') ?>">Payments</a></li>
<li><?= e($this->pageTitle) ?></li>
</ul>
<ul>
<li><a href="<?= Backend::url('tps/birzha/payments') ?>">Payments</a></li>
<li><?= e($this->pageTitle) ?></li>
</ul>
<?php Block::endPut() ?>
<div class="control-toolbar">
<div class="toolbar-item toolbar-primary">
<div data-control="toolbar">
<button
type="button"
data-request="onApprove"
data-request-data="redirect:0"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-outline-success">
Approve
</button>
<button
type="button"
data-request="onSave"
data-request-data="redirect:0"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-outline-danger">
Decline
</button>
<button
type="submit"
data-request="onSave"
data-request-data="redirect:0"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-primary">
<?= e(trans('backend::lang.form.save')) ?>
</button>
<button
type="button"
data-request="onSave"
data-request-data="close:1"
data-hotkey="ctrl+enter, cmd+enter"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-default">
<?= e(trans('backend::lang.form.save_and_close')) ?>
</button>
<button
type="button"
class="oc-icon-trash-o btn-icon danger pull-right"
data-request="onDelete"
data-load-indicator="<?= e(trans('backend::lang.form.deleting')) ?>"
data-request-confirm="<?= e(trans('backend::lang.form.confirm_delete')) ?>">
</button>
<span class="btn-text ">
<?= e(trans('backend::lang.form.or')) ?> <a href="<?= Backend::url('tps/birzha/payments') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>
</span>
</div>
</div>
</div>
<?php if (!$this->fatalError): ?>
<?= Form::open(['class' => 'layout']) ?>
<?= Form::open(['class' => 'layout']) ?>
<div class="layout-row">
<?= $this->formRender() ?>
</div>
<div class="layout-row">
<?= $this->formRender() ?>
</div>
<div class="form-buttons">
<div class="loading-indicator-container">
<div class="form-buttons">
<div class="loading-indicator-container">
<button
type="submit"
data-request="onSave"
data-request-data="redirect:0"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-primary">
<?= e(trans('backend::lang.form.save')) ?>
</button>
<button
type="button"
data-request="onSave"
data-request-data="close:1"
data-hotkey="ctrl+enter, cmd+enter"
data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"
class="btn btn-default">
<?= e(trans('backend::lang.form.save_and_close')) ?>
</button>
<button
type="button"
class="oc-icon-trash-o btn-icon danger pull-right"
data-request="onDelete"
data-load-indicator="<?= e(trans('backend::lang.form.deleting')) ?>"
data-request-confirm="<?= e(trans('backend::lang.form.confirm_delete')) ?>">
</button>
</div>
</div>
<?= Form::close() ?>
<span class="btn-text">
<?= e(trans('backend::lang.form.or')) ?> <a href="<?= Backend::url('tps/birzha/payments') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>
</span>
</div>
</div>
<?= Form::close() ?>
<?php else: ?>
<p class="flash-message static error"><?= e(trans($this->fatalError)) ?></p>
<p><a href="<?= Backend::url('tps/birzha/payments') ?>" class="btn btn-default"><?= e(trans('backend::lang.form.return_to_list')) ?></a></p>
<p class="flash-message static error"><?= e(trans($this->fatalError)) ?></p>
<p><a href="<?= Backend::url('tps/birzha/payments') ?>" class="btn btn-default"><?= e(trans('backend::lang.form.return_to_list')) ?></a></p>
<?php endif ?>

View File

@ -1,5 +1,6 @@
<?php namespace TPS\Birzha\Models;
use Carbon\Carbon;
use Model;
/**
@ -27,7 +28,7 @@ class Payment extends Model
];
public $morphOne = [
'transaction' => [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;
}
}
}

View File

@ -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();
}

View File

@ -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'
];
}

View File

@ -13,7 +13,7 @@ columns:
valueFrom: email
amount:
label: Amount
type: text
type: money
payment_type:
label: 'Payment type'
type: status

View File

@ -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: ''

View File

@ -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

View File

@ -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

View File

@ -118,7 +118,7 @@ code = "main-top"
<img src="{{'assets/images/svg/coins.svg'|theme}}" alt="">
</div>
<div class="profile_text">
{{user.balance}} TMT
{{user.getBalance}} TMT
</div>
</div>
</div>