Beispiel #1
0
 public function parse()
 {
     $obj = $this->curl->get('https://www.tinkoff.ru/api/v1/currency_rates/');
     $rates = $obj->payload->rates;
     $hash = md5(serialize($rates));
     $last_update = Update::all()->last();
     if ($last_update && $hash == $last_update->hash) {
         //nothing changed
         return "Nothing to update\n";
     }
     $update = Update::create(['hash' => $hash]);
     foreach ($rates as $rate) {
         $row = new Exchange();
         $row->category = $rate->category;
         $row->operation = 'buy';
         $row->from = $rate->fromCurrency->name;
         $row->to = $rate->toCurrency->name;
         $row->value = $rate->buy;
         $row->update_id = $update->id;
         $row->save();
         if (isset($rate->sell)) {
             $row = new Exchange();
             $row->category = $rate->category;
             $row->operation = 'sell';
             $row->from = $rate->fromCurrency->name;
             $row->to = $rate->toCurrency->name;
             $row->value = $rate->sell;
             $row->update_id = $update->id;
             $row->save();
         }
     }
 }
 /**
  * @return array
  */
 public function get_data()
 {
     $input = [];
     foreach ($this->labels as $type => $value) {
         foreach ($value as $name => $lbl) {
             $input[$type][$name] = Request::input("{$type}.{$name}", null);
             if ($input[$type][$name]) {
                 if ($type == 'cat') {
                     $this->categories[] = $name;
                 }
                 if ($type == 'opr') {
                     $this->operations[] = $name;
                 }
                 if ($type == 'cur') {
                     $this->from[] = substr($name, 0, 3);
                     $this->to[] = substr($name, 3, 3);
                 }
             }
         }
     }
     $updates = Update::all();
     $dates = [];
     foreach ($updates->lists('created_at') as $up) {
         $dates[] = $up->format('d.m.Y H:i');
     }
     $from = $this->from;
     $to = $this->to;
     $exchanges = DB::table('exchanges')->whereIn('category', $this->categories)->whereIn('operation', $this->operations)->where(function ($q) use($from, $to) {
         $wh = '';
         foreach ($from as $key => $fr) {
             $wh .= "`from`='{$from[$key]}' AND `to`='{$to[$key]}' OR ";
         }
         $wh .= '1=0';
         $q->whereRaw($wh);
     })->get();
     $rates = [];
     foreach ($exchanges as $rate) {
         $rates[$rate->category . '_' . $rate->operation . '_' . $rate->from . '_' . $rate->to][] = $rate->value;
     }
     $data = [];
     foreach ($rates as $label => $r) {
         $color = array_pop($this->colors);
         $data[] = array('label' => $this->decorate_label($label), 'data' => $r, 'strokeColor' => '#' . $color['stroke'], 'pointColor' => '#' . $color['point']);
     }
     usort($data, function ($a, $b) {
         return $b['data'][0] - $a['data'][0];
     });
     $json_data['DataSets'] = $data;
     $json_data['AxisLabels'] = $this->decorate_dates($dates);
     $this->input = $input;
     return $json_data;
 }