Created
May 5, 2020 11:06
-
-
Save worstn8mare/1da5e9883ba152c71798d482c7a0c328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Modules\Commission\Services; | |
use Carbon\Carbon; | |
use Illuminate\Support\Facades\File; | |
use Modules\Commission\Repositories\Concerns\CanPayoutCommission; | |
use Modules\Credit\Models\BankAccount; | |
use Modules\User\Models\User; | |
class GoldmineCommissionService extends CommissionAbstract | |
{ | |
use CanPayoutCommission; | |
protected $runDate; | |
protected $cmd = [0, 1]; //mt4 transaction type | |
protected $sponsorTreeData; | |
protected $contractId; | |
protected $maxDepth = 7; | |
protected $commissionPercentage; | |
protected $contractOverPayoutPercentage; | |
protected $contractOverPayoutComputation; | |
protected $goldmineLevelPercentage; | |
protected $bankTransactionType; | |
protected $bankCreditTypeId; | |
protected $goldmineType = 'goldmine 1 commission'; | |
private $goldmineSettings; | |
/** | |
* @var bool|string | |
*/ | |
private $contractOverPayoutComputationEnabled; | |
public function __construct($runDate) | |
{ | |
$this->runDate = $runDate; | |
$this->logText = ''; | |
$directory = storage_path('logs') . '/goldmine1/'; | |
if (!File::exists($directory)) { | |
File::makeDirectory($directory); | |
} | |
$this->path = $directory . "goldmine1-" . Carbon::now()->format('Y-m-d-h-i-s'); | |
} | |
public function payout($tradeCommissions, $userTiers) | |
{ | |
$goldmineCommissionToInsert = []; | |
$goldminePayoutToInsert = []; // payout to commissions | |
$goldmineCommissionUsers = $tradeCommissions->pluck('user_id', 'user_id')->toArray(); | |
$bank_accounts = BankAccount::get()->pluck('id', 'reference_id')->toArray(); | |
if (count($goldmineCommissionUsers) > 0) { | |
$sponsorTreeNodes = $this->sponsorTreeData->filter(function ($sponsorNode) use ($goldmineCommissionUsers) { | |
return in_array($sponsorNode['user_id'], $goldmineCommissionUsers); | |
}); | |
foreach ($sponsorTreeNodes as $node) { | |
$uplines = $this->sponsorTreeData->filter(function ($sponsorNode) use ($node) { | |
return $sponsorNode['lft'] < $node['lft'] && $sponsorNode['rgt'] > $node['rgt']; | |
}); | |
$uplines_sorted = $uplines->sortByDesc('level'); | |
$trade = $tradeCommissions[$node['user_id']]; | |
$trade['level'] = 1; | |
$this->logText .= "Contributor: " . $node['user_id'] . "\n"; | |
foreach ($uplines_sorted as $upline) { | |
if ($trade->level == $this->maxDepth) { | |
break; | |
} | |
$uplineUserId = $upline['user_id']; | |
if (!$this->isEligible($trade, $userTiers, $uplineUserId)) { | |
$trade->level++; | |
continue; | |
} | |
if (!$this->toCompressUser($uplineUserId, $userTiers)) { | |
$calculated_amount = $this->calculateAmount($trade, $userTiers, $uplineUserId); | |
if ($calculated_amount > 0) { | |
$goldmineCommissionToInsert[] = [ | |
'user_id' => $uplineUserId, | |
'contributor_user_id' => $node['user_id'], | |
'amount' => $trade->profit, | |
'calculated_amount' => $calculated_amount, | |
'level_percentage' => $this->goldmineLevelPercentage[$trade->level], | |
'commission_percentage' => $this->commissionPercentage, | |
'level' => $trade->level, | |
'payout_date' => $trade->payout_date, | |
'created_at' => Carbon::now(), | |
'updated_at' => Carbon::now(), | |
'goldmine_commission_type' => $this->goldmineTypeId | |
]; | |
$transactionCode = $this->generateTransactionCode($uplineUserId, $node['user_id'], $this->bankTransactionType); | |
$this->appendGoldmineCommissionLogs( | |
[ | |
'user_id' => $uplineUserId, | |
'user_tier' => $this->getTier($userTiers,$uplineUserId), | |
'user_isContract' => $trade->contract_type_id, //---> add contract for snapshot user_tier | |
'contributor_id' => $node['user_id'], | |
'amount' => $trade->profit, | |
'calculated_amount' => $calculated_amount, | |
'goldmine_level_percentage' => $this->goldmineLevelPercentage[$trade->level], | |
'goldmine_commission_percentage' => $this->commissionPercentage, | |
'transaction_code' => $transactionCode, | |
'user_level' => $trade->level | |
] | |
); | |
$goldminePayoutToInsert[] = [ | |
'credit' => $calculated_amount, | |
'transaction_code' => $transactionCode, | |
'transaction_date' => Carbon::now(), | |
'bank_transaction_type_id' => $this->bankTransactionType, | |
'bank_credit_type_id' => $this->bankCreditTypeId, | |
'transfer_account_id' => null, | |
'bank_account_id' => isset($bank_accounts[$uplineUserId]) ? $bank_accounts[$uplineUserId] : null, | |
'done_by' => 1, // assume admin | |
'remarks' => 'Goldmine 1 Commission from Trading Profit', | |
'created_at' => Carbon::now(), | |
'updated_at' => Carbon::now() | |
]; | |
} | |
$trade->level++; | |
} | |
} | |
} | |
} | |
$this->persistCommissionTrades($goldmineCommissionToInsert); | |
$this->persistGoldmineCommissionLogs(); | |
$this->persistBankAccountStatement($goldminePayoutToInsert); | |
} | |
public function setValues() | |
{ | |
parent::setValues(); | |
$this->setCommissionPercentage(setting('goldmine_contribution_percentage')); | |
$this->setContractOverPayoutPercentage(setting('contract_member_commission_over_payout_percentage')); | |
$this->goldmineSettings = json_decode(setting('goldmine_1_commission_tier_level'), true); | |
$this->goldmineLevelPercentage = json_decode(setting('goldmine_upline_level_percentage'), true); | |
$this->contractOverPayoutComputation = 1; // --> # 1 - overpayout % goes all to user, # 2 - overpayout % is also subdivided | |
$this->contractOverPayoutComputationEnabled = setting('contract_member_goldmine_1_commission_over_payout_enabled'); | |
} | |
protected function calculateAmount($trade, $userTiers, $uplineUserId) | |
{ | |
$amount = $trade->profit; | |
$userContractType = $userTiers[$uplineUserId]['contract_type_id']; | |
$level = $trade->level; | |
$new_amount = bcmul($amount, bcmul($this->commissionPercentage, $this->goldmineLevelPercentage[$level], 8), 2); | |
if ($this->contractOverPayoutComputationEnabled && $this->userHasContract($userContractType) && $this->contractOverPayoutComputation == 2) { | |
$overpayoutContractPercentage = bcmul($this->goldmineLevelPercentage[$level], $this->contractOverPayoutPercentage, 8); | |
$overpayoutCommissionPercentage = bcmul($this->commissionPercentage, $this->goldmineLevelPercentage[$level], 8); | |
$percentTotal = bcmul($overpayoutCommissionPercentage, $overpayoutContractPercentage, 8); | |
$new_amount = bcadd($new_amount, bcmul($amount, $percentTotal, 8), 2); | |
} | |
return $new_amount; | |
} | |
protected function setCommissionPercentage($percent) | |
{ | |
$this->commissionPercentage = $percent; | |
} | |
protected function setContractOverPayoutPercentage($percent) | |
{ | |
$this->contractOverPayoutPercentage = $percent; | |
} | |
protected function isEligible($trade, $userTiers, $uplineUserId) | |
{ | |
$goldmineLevel = $this->goldmineSettings[$trade->level]; | |
$tierLog = 'no tier'; | |
$result = false; | |
if (isset($userTiers[$uplineUserId])) { | |
$tierLog = $this->getTier($userTiers, $uplineUserId); | |
$result = $tierLog == 0 ? false : !in_array($tierLog, $goldmineLevel['tier_no_share']); | |
} | |
$this->logText .="------> user id: $uplineUserId is " . (($result) ? " Eligible" : "not Eligible \n"); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment