private static function OKP_MinAndMaxWallets($currency)
 {
     $wallets = WalletsOkp::findBy(array('currency' => $currency));
     if (empty($wallets)) {
         return null;
     }
     $maxBalance = null;
     $keyMax = null;
     $minBalance = null;
     $keyMin = null;
     $okpay = new OKPay();
     foreach ($wallets as $key => $value) {
         $balance = $okpay->currency_balance($value['wallet_id'], $value['api_password'], $currency);
         if ($balance !== null) {
             if ($minBalance === null || $minBalance > $balance) {
                 $minBalance = $balance;
                 $keyMin = $key;
             }
             if ($maxBalance === null || $maxBalance < $balance) {
                 $maxBalance = $balance;
                 $keyMax = $key;
             }
         }
     }
     $result['min'] = $wallets[$keyMin];
     $result['max'] = $wallets[$keyMax];
     return $result;
 }
 public static function addNewOKPWallet()
 {
     $email = Core::validate($_POST['EMAIL']);
     $wallet_id = Core::validate($_POST['WALLET_ID']);
     $api_password = Core::validate($_POST['API_PASSWORD']);
     $currency = Core::validate($_POST['CURRENCY']);
     $share = Core::validate($_POST['SHARE']);
     // percent
     if ($email == null || $wallet_id == null || $api_password == null || $currency == null || $share == null || !Core::isDouble($share)) {
         print 'Incorrect input data';
         exit;
     }
     $result = WalletsOkp::findBy(array('email' => $email, 'wallet_id' => $wallet_id, 'currency' => $currency));
     if (!empty($result)) {
         print 'This wallet already exists';
         exit;
     }
     $okpay = new OKPay();
     $balance = $okpay->currency_balance($wallet_id, $api_password, $currency);
     if ($balance === null) {
         print $okpay->Error();
         exit;
     }
     $wallet = new WalletsOkp();
     $wallet->setEmail($email);
     $wallet->setWalletId($wallet_id);
     $wallet->setApiPassword($api_password);
     $wallet->setCurrency($currency);
     $wallet->setValue($balance);
     $wallet->setShare($share / 100.0);
     $wallet->insert();
     header('Location: /admin/okp');
 }