public static function addNewPMWallet()
 {
     $account_id = Core::validate($_POST['ACCOUNT_ID']);
     $pass_phrase = Core::validate($_POST['PASS_PHRASE']);
     $alternate_pass_phrase = Core::validate($_POST['ALTERNATE_PASS_PHRASE']);
     $units = Core::validate($_POST['UNITS']);
     $account = Core::validate($_POST['ACCOUNT']);
     $share = Core::validate($_POST['SHARE']);
     // percent
     if ($account_id == null || $pass_phrase == null || $alternate_pass_phrase == null || $units == null || $account == null || $share == null || !Core::isDouble($share)) {
         print 'Incorrect input data';
         exit;
     }
     $result = WalletsPm::findBy(array('account' => $account));
     if (!empty($result)) {
         print 'This account already exists';
         exit;
     }
     $perfectMoney = new PerfectMoney();
     $perfectMoney->setAccountId($account_id);
     $perfectMoney->setPassPhrase($pass_phrase);
     $resp = $perfectMoney->balance();
     if ($resp == null) {
         print $perfectMoney->getError();
         exit;
     }
     if (!isset($resp[$account])) {
         print 'Invalid account';
         exit;
     }
     $value = $resp[$account];
     $pmWallet = new WalletsPm();
     $pmWallet->setAccountId($account_id);
     $pmWallet->setPassPhrase($pass_phrase);
     $pmWallet->setAlternatePassPhrase($alternate_pass_phrase);
     $pmWallet->setUnits($units);
     $pmWallet->setAccount($account);
     $pmWallet->setValue($value);
     $pmWallet->setShare($share / 100.0);
     $pmWallet->insert();
     header('Location: /admin/pm');
 }
 private static function PM_MinAndMaxWallets($units)
 {
     $pm_wallets = WalletsPm::findBy(array('units' => $units));
     if (empty($pm_wallets)) {
         return null;
     }
     $maxBalance = null;
     $keyMax = null;
     $minBalance = null;
     $keyMin = null;
     $perfectMoney = new PerfectMoney();
     foreach ($pm_wallets as $key => $value) {
         $perfectMoney->setAccountID($value['account_id']);
         $perfectMoney->setPassPhrase($value['pass_phrase']);
         $response = $perfectMoney->balance();
         if ($response !== null) {
             $balance = $response[$value['account']];
             $pm_wallets[$key]['current_balance'] = $balance;
             if ($minBalance === null || $minBalance > $balance) {
                 $minBalance = $balance;
                 $keyMin = $key;
             }
             if ($maxBalance === null || $maxBalance < $balance) {
                 $maxBalance = $balance;
                 $keyMax = $key;
             }
         }
     }
     $result['min'] = $pm_wallets[$keyMin];
     $result['max'] = $pm_wallets[$keyMax];
     return $result;
 }