public function up()
 {
     for ($i = 0; $i < 45; $i++) {
         $data = Helper::getDateIntervalYesterdayInDashOrSlashFormat(new \DateTime(), $i, 'slash');
         $url = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' . $data;
         $xml_contents = file_get_contents($url);
         if ($xml_contents === false) {
             throw new \ErrorException('Error loading ' . $url);
         }
         $xml = new \SimpleXMLElement($xml_contents);
         $date = $xml->attributes()->Date;
         foreach ($xml as $node) {
             $current_curr = new \app\models\CurrHistory();
             if (\app\models\Currencies::findOne(['valute_id' => $node->attributes()->ID])) {
                 $current_curr->currency_id = \app\models\Currencies::findOne(['valute_id' => $node->attributes()->ID]);
             } else {
                 $new_currency = new \app\models\Currencies();
                 $new_currency->name = $node->Name;
                 $new_currency->valute_id = $node->attributes()->ID;
                 $new_currency->char_code = $node->CharCode;
                 $new_currency->num_code = $node->NumCode;
                 $new_currency->save();
                 $current_curr->currency_id = $new_currency->id;
             }
             $current_curr->date = $date;
             $current_curr->nominal = $node->Nominal;
             $current_curr->value = $node->Value;
             $current_curr->save();
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @author Nagy Ervin <*****@*****.**>
  * @return string - relative path of generated QRCode img
  * @version 0.1 
  */
 public function create()
 {
     $currencies = Currencies::where('active', '=', 1)->orderBy('main_currency', 'desc')->lists('name', 'id');
     if (Request::isMethod('post')) {
         $rules = ['currency' => 'required', 'amount' => 'required'];
         $validator = Validator::make(Input::all(), $rules);
         if ($validator->fails()) {
             $messages = $validator->messages();
             return Redirect::to('payment/create-invoice')->withErrors($messages)->withInput();
         } else {
             //$bitcoin = new App\Libs\BitCoind\Bitcoineasy(Config::get('constants.daemonUser'), Config::get('constants.daemonPassword'), "localhost", 8332);
             //$addr = $bitcoin->getnewaddress();
             $codeContents = '14rg72MKDpYUufXrLrZtBufxpofcTdx18e';
             $relaivePath = Config::get('constants.qrcodeRelativePath');
             $tempDir = Config::get('constants.qrcodeAbsolutePath');
             $fileName = 'qrcode_' . md5(microtime()) . '.png';
             $pngAbsoluteFilePath = $tempDir . $fileName;
             $urlRelativeFilePath = $relaivePath . $fileName;
             App\Libs\QrCode\QRcode::png($codeContents, $pngAbsoluteFilePath, QR_ECLEVEL_H, 10, 2);
             return view('payment.payinvoice', ['url' => $urlRelativeFilePath]);
         }
     }
     return view('payment.create', ['currencies' => $currencies]);
 }
Exemplo n.º 3
0
            <?php 
echo $form->field($model, 'cat_id')->dropDownList(ArrayHelper::map(\app\models\Categories::find()->where(['site_id' => 11])->all(), 'id', 'name'), ['prompt' => 'Выбрать категорию']);
?>

            <?php 
echo $form->field($model, 'description')->textarea(['rows' => 5, 'cols' => 5, 'id' => 'my-textarea-id']);
?>
            <?php 
echo $form->field($model, 'price')->textInput();
?>

            <?php 
echo $form->field($model, 'currency')->dropDownList(ArrayHelper::map(\app\models\Currencies::find()->all(), 'id', 'char_code'), ['prompt' => 'Выбрать валюту']);
?>
            <?php 
echo $form->field($model, 'currency_out')->dropDownList(ArrayHelper::map(\app\models\Currencies::find()->all(), 'id', 'char_code'), ['prompt' => 'Выбрать валюту']);
?>

            <?php 
echo $form->field($uploadImg, 'img')->fileInput();
?>

            <div class="form-group">
                <?php 
echo Html::submitButton($model->isNewRecord ? 'Создать' : 'Обновить', ['class' => 'btn btn-primary', 'name' => 'create-button']);
?>
            </div>
            <?php 
ActiveForm::end();
?>
        </div>
 /**
  * Show the application dashboard to the user.
  *
  * @return Response
  */
 public function getRates()
 {
     if (!($xml = simplexml_load_file("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote"))) {
         trigger_error('Error reading XML file', E_USER_ERROR);
     }
     $xml_file = array();
     $shortcodes = array();
     $i = 1;
     $data = Currencies::all();
     foreach ($data as $key) {
         foreach ($xml->resources->resource as $syn) {
             //print_r($syn->field);exit;
             $explode_name = explode("/", $syn->field[0]);
             $explode_name2 = explode(" ", $syn->field[0]);
             //  print_r($syn);exit;
             if ($explode_name2[0] == 'GOLD' && $key->shortcode == 'XAU') {
                 $explode_name2[0] = 'XAU';
                 if (!in_array($explode_name2[0], $shortcodes)) {
                     $shortcodes[] = $explode_name2[0];
                 }
                 if ($key->shortcode == $explode_name2[0] && $explode_name2[0] != 'USD') {
                     $xml_file['USD_' . $explode_name2[0]][] = array('id' => $i, 'source' => 'USD', 'target' => $explode_name2[0], 'rate' => (double) $syn->field[1], 'time' => date('Y-m-d H:i:s'));
                     $i++;
                 }
             }
             if ($syn->field[0] == 'SILVER 1 OZ 999 NY' && $key->shortcode == 'XAG') {
                 $explode_name2[0] = 'XAG';
                 if (!in_array($explode_name2[0], $shortcodes)) {
                     $shortcodes[] = $explode_name2[0];
                 }
                 if ($key->shortcode == $explode_name2[0] && $explode_name2[0] != 'USD') {
                     $xml_file['USD_' . $explode_name2[0]][] = array('id' => $i, 'source' => 'USD', 'target' => $explode_name2[0], 'rate' => (double) $syn->field[1], 'time' => date('Y-m-d H:i:s'));
                     $i++;
                 }
             }
             if ($explode_name[1] != 'BTC' && $explode_name[1] != 'LTC' && $explode_name[0] != 'BTC' && $explode_name[0] != 'LTC') {
                 echo 'itt';
                 exit;
                 if (!in_array($explode_name[1], $shortcodes) && $key->shortcode == $explode_name[1]) {
                     $shortcodes[] = $explode_name[1];
                 }
                 if ($key->shortcode == $explode_name[1] && $explode_name[1] != 'USD') {
                     $xml_file[$explode_name[0] . '_' . $explode_name[1]][] = array('id' => $i, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => (double) $syn->field[1], 'time' => date('Y-m-d H:i:s'));
                     $i++;
                 }
             }
             // print_r($explode_name);exit;
         }
     }
     /*print_r($xml_file);
       exit;*/
     $count = $i;
     $shortcodes[] = 'BTC';
     $shortcodes[] = 'LTC';
     print_r($shortcodes);
     exit;
     $variation = array();
     for ($i = 0; $i < count($shortcodes); $i++) {
         for ($j = 0; $j < count($shortcodes); $j++) {
             if ($i != $j) {
                 $variation[$i][$j] = $shortcodes[$i] . '_' . $shortcodes[$j];
                 if (array_key_exists($variation[$i][$j], $xml_file)) {
                     //print($variation[$i][$j]." ");
                     //print $xml_file[$variation[$i][$j]] [$i]['rate']." ";
                 } else {
                     if (array_key_exists($variation[$j][$i], $xml_file)) {
                         //print($variation[$i][$j]." ");
                         $rezult = (double) (1 / $xml_file[$variation[$j][$i]][$j]['rate'] . "   ");
                         //print($rezult);
                         //$xml_file[$variation[$j][$i]] [$j]['rate']=$rezult;
                         $explode_name = explode("_", $variation[$i][$j]);
                         $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                         $count++;
                     }
                 }
                 //echo $variation[$i][$j]."     ";
             }
         }
         /*echo"<br />";*/
     }
     /*print_r($xml_file)."         ";
       print_r($variation);
       exit;*/
     for ($i = 0; $i < count($shortcodes); $i++) {
         for ($j = 0; $j < count($shortcodes); $j++) {
             $rezult = 0;
             switch ($variation[$i][$j]) {
                 case 'BTC_LTC':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
                 case 'BTC_USD':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
                 case 'LTC_BTC':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
                 case 'LTC_USD':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
                 case 'USD_BTC':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
                 case 'USD_LTC':
                     $rezult = $this->{$variation[$i][$j]}();
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $rezult, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                     break;
             }
         }
     }
     /*print(count($xml_file)."   ");
       
       print_r($xml_file);
       exit;*/
     for ($i = 0; $i < count($shortcodes); $i++) {
         for ($j = 0; $j < count($shortcodes); $j++) {
             if ($i != $j) {
                 if (!array_key_exists($variation[$i][$j], $xml_file)) {
                     /*print($xml_file[$variation[$i][0]] [$i]['rate']);
                       print($xml_file[$variation[0][$j]] [$i]['rate']);*/
                     $result = (double) ($xml_file[$variation[$i][0]][0]['rate'] * $xml_file[$variation[0][$j]][0]['rate']);
                     /*print($variation[$i][$j]);
                       print($result);
                       exit;*/
                     $explode_name = explode("_", $variation[$i][$j]);
                     $xml_file[$variation[$i][$j]][] = array('id' => $count, 'source' => $explode_name[0], 'target' => $explode_name[1], 'rate' => $result, 'time' => date('Y-m-d H:i:s'));
                     $count++;
                 }
             }
         }
     }
     foreach ($xml_file as $key => $currenciesPairs) {
         foreach ($currenciesPairs as $key => $currenciesPair) {
             $source_currency_id = Currencies::where('shortcode', "=", $currenciesPair['source'])->first()->id;
             $target_currency_id = Currencies::where('shortcode', "=", $currenciesPair['target'])->first()->id;
             $model = ExchangeRates::where('source_currency_id', "=", $source_currency_id)->where('target_currency_id', "=", $target_currency_id)->first();
             if (count($model) == 0 && isset($currenciesPair['rate']) && is_numeric($currenciesPair['rate']) && $currenciesPair['rate'] > 0) {
                 $new_model = new ExchangeRates();
                 $new_model->source_currency_id = $source_currency_id;
                 $new_model->target_currency_id = $target_currency_id;
                 $new_model->rate = $currenciesPair['rate'];
                 $new_model->time = date('Y-m-d H:i:s');
                 if ($new_model->save()) {
                 } else {
                     print_r($new_model->getErrors());
                 }
             } else {
                 $theRate = 0;
                 if (isset($currenciesPair['rate']) && is_numeric(strip_tags(trim($currenciesPair['rate']))) && $currenciesPair['rate'] > 0) {
                     $theRate = $currenciesPair['rate'];
                 } else {
                     $theRate = $model->rate;
                 }
                 $model->rate = $theRate;
                 $model->time = date('Y-m-d H:i:s');
                 $model->save();
             }
         }
     }
     print_r($xml_file);
     exit;
 }
Exemplo n.º 5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCurrency()
 {
     return $this->hasOne(Currencies::className(), ['id' => 'currency_id']);
 }
Exemplo n.º 6
0
 public function actionCurrencyTest()
 {
     //for($i=0; $i<45; $i++) {
     echo $i = 0;
     $data_slash = Helper::getDateIntervalYesterdayInDashOrSlashFormat(new \DateTime(), $i, 'slash');
     $data_dash = Helper::getDateIntervalYesterdayInDashOrSlashFormat(new \DateTime(), $i, 'dash');
     $url = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' . $data_slash;
     $xml_contents = file_get_contents($url);
     if ($xml_contents === false) {
         throw new \ErrorException('Error loading ' . $url);
     }
     $xml = new \SimpleXMLElement($xml_contents);
     //$date = $xml->attributes()->Date;
     foreach ($xml as $node) {
         $current_curr = new CurrHistory();
         //var_dump($node->attributes()->ID);
         if (Currencies::find()->where('valute_id like "%' . $node->attributes()->ID . '%"')->one()) {
             // var_dump(Currencies::find()->where('valute_id like "%'.$node->attributes()->ID.'%"')->one());
             $current_curr->currency_id = Currencies::find()->where('valute_id like "%' . $node->attributes()->ID . '%"')->one()->id;
         } else {
             $new_currency = new Currencies();
             $new_currency->name = TranslateHelper::translit($node->Name);
             $new_currency->valute_id = $node->attributes()->ID;
             $new_currency->char_code = $node->CharCode;
             $new_currency->num_code = $node->NumCode;
             $new_currency->save(false);
             $current_curr->currency_id = $new_currency->id;
             //var_dump($new_currency); exit;
         }
         $current_curr->date = $data_dash;
         $current_curr->nominal = $node->Nominal;
         $current_curr->value = str_replace(',', '.', $node->Value);
         //замена запятой на точку позволят использовать
         //формат с плавоющей точкой
         $current_curr->save(false);
         //echo $current_curr->value; exit;
     }
     // }
 }