/** * This is action to update exchanges from bank in database * When grab is failed, it goes again $tries_count times after sleeping $seconds_to_sleep seconds * If it is not helping, last exchange values from database will be inserted * * @param string $strategy_classname Classname of strategy * @param int $tries_count Count of tries if caught exception * @param int $seconds_to_sleep Seconds to sleep befor new try */ public function actionBank($strategy_classname, $tries_count = 5, $seconds_to_sleep = 10) { // create grabber try { $grabber = new ExchangeRateGrabber(ExchangeRateGrabberStrategyAbstract::create($strategy_classname)); } catch (\Exception $ex) { echo 'Can\'t create grabber of ' . $strategy_classname . ': ' . trim($ex->getMessage()) . PHP_EOL; return; } // start tries $try_number = 0; $today = (new \DateTime())->format('Y-m-d'); do { try { $data = $grabber->grab(); // @todo: hide in factory $exchange = ExchangeRate::find()->where(['bank_id' => $grabber->getBankId(), 'grab_date' => $today])->one(); if (empty($exchange)) { $exchange = new ExchangeRate(); $exchange->bank_id = $grabber->getBankId(); $exchange->grab_date = $today; } $exchange->dollar_buy = $data[Currency::DOLLAR_ID]['buy']; $exchange->dollar_sale = $data[Currency::DOLLAR_ID]['sale']; $exchange->euro_buy = $data[Currency::EURO_ID]['buy']; $exchange->euro_sale = $data[Currency::EURO_ID]['sale']; } catch (\Exception $ex) { echo $strategy_classname . ' is having problems: ' . trim($ex->getMessage()) . PHP_EOL; // if some tries left, let's sleep for a few seconds if (++$try_number <= $tries_count) { if (intval($seconds_to_sleep) > 0) { sleep($seconds_to_sleep); } echo "Let's try again. This is try #{$try_number}" . PHP_EOL; } } } while (empty($exchange) && $try_number <= $tries_count); // get last exchange rates if grabber is not working if (empty($exchange)) { // @todo: hide in factory $last_exchanges = ExchangeRate::find()->where(['bank_id' => $grabber->getBankId()])->orderBy('grab_date DESC')->one(); if ($last_exchanges->grab_date == $today) { $exchange = $last_exchanges; } else { $exchange = new ExchangeRate(); $exchange->attributes = $last_exchanges->attributes; $exchange->id = null; $exchange->grab_date = $today; } echo "Last exchange rates will be inserted for {$strategy_classname}" . PHP_EOL; } echo $strategy_classname . " updated with dollar {$exchange->dollar_buy}/{$exchange->dollar_sale}, euro {$exchange->euro_buy}/{$exchange->euro_sale}" . PHP_EOL; $exchange->save(); }
public function storeExchangeRate(StoreExchangeRateRequest $request) { // $exchangeRates = ExchangeRate::where('status', 1)->get(); foreach ($exchangeRates as $exchangeRate) { $exchangeRate->status = 0; $exchangeRate->finishDate = new Carbon(); $exchangeRate->save(); } $input = $request->all(); $module = new ExchangeRate(); $module->buyingRate = $input['buyingRate']; $module->sellingRate = $input['sellingRate']; $module->status = 1; $module->startDate = new Carbon(); //$module->endTime = new Carbon($input['endTime']); //Control de subida de imagen $module->save(); return redirect('admin/config/exchange_rate'); }
/** * Getting exchanges in banks for selected period * @param \DateTime $today * @param type $period * @return array */ function getBankDays(\DateTime $today = null, $period = -10) { if (empty($today)) { $today = $this->assignActualDate(); } $table_exchanges = models\ExchangeRate::tableName(); $banks_table = models\Bank::tableName(); $today->modify($period . ' day'); $days = array(); for ($i = 0; $i < abs($period); ++$i) { $today->modify("+1 day"); $yesterday = clone $today; $yesterday->modify('-1 day'); $today_string = $today->format(self::DATE_FORMAT); $query = "\n\n SELECT\n\n q1.*,\n\n AVG(dollar_buy) as dollar_buy_yesterday,\n AVG(dollar_sale) as dollar_sale_yesterday,\n (AVG(q1.dollar_buy_today) - AVG(dollar_buy)) as dollar_buy_diff,\n (AVG(q1.dollar_sale_today) - AVG(dollar_sale)) as dollar_sale_diff,\n\n AVG(euro_buy) as euro_buy_yesterday,\n AVG(euro_sale) as euro_sale_yesterday,\n (AVG(q1.euro_buy_today) - AVG(euro_buy)) as euro_buy_diff,\n (AVG(q1.euro_sale_today)- AVG(euro_sale)) as euro_sale_diff\n\n FROM {$table_exchanges} as exchanges, (\n\n SELECT bank_id, \n AVG(dollar_buy) as dollar_buy_today, AVG(dollar_sale) as dollar_sale_today,\n AVG(euro_buy) as euro_buy_today, AVG(euro_sale) as euro_sale_today\n FROM {$table_exchanges} as exchanges\n JOIN {$banks_table} as banks ON banks.id = exchanges.bank_id\n WHERE `grab_date` = '{$today_string}'\n AND banks.id = exchanges.bank_id\n\n GROUP BY bank_id\n\n ) as q1\n\n WHERE q1.bank_id = exchanges.bank_id\n AND grab_date = '" . $yesterday->format(self::DATE_FORMAT) . "'\n\n GROUP BY bank_id\n\n "; $command = Yii::$app->db->createCommand($query); $res = $command->queryAll(); if (!empty($res)) { foreach ($res as $row) { $day = ['dollar_buy' => ['value' => $row['dollar_buy_today'], 'diff' => $row['dollar_buy_diff']], 'dollar_sale' => ['value' => $row['dollar_sale_today'], 'diff' => $row['dollar_sale_diff']], 'euro_buy' => ['value' => $row['euro_buy_today'], 'diff' => $row['euro_buy_diff']], 'euro_sale' => ['value' => $row['euro_sale_today'], 'diff' => $row['euro_sale_diff']]]; $days[$today->format(self::DATE_FORMAT)][$row['bank_id']] = $day; } } } return $days; }
public function showPayBooking(Request $request) { $codigo = $request['reserve_code']; if ($codigo == '' || $codigo == null) { return redirect()->back()->withErrors(['errors' => 'Buscar una reserva y seleccionarla']); } $tickets = Ticket::where('reserve', $codigo)->get(); $event = $tickets->first()->event; $zone = $tickets->first()->zone; $presentation = $tickets->first()->presentation; $exchangeRate = ExchangeRate::where('status', config('constants.active'))->first(); return view('internal.salesman.payBookingShow', array('tickets' => $tickets->first(), 'event' => $event, 'zone' => $zone, 'presentation' => $presentation, 'reserve' => $codigo, 'exchangeRate' => $exchangeRate)); }
/** * @return \yii\db\ActiveQuery */ public function getExchanges() { return $this->hasMany(ExchangeRate::className(), ['bank_id' => 'id']); }
/** * Show the form for creating a new resource. * * @param int $id * @return \Illuminate\Http\Response */ public function createSalesman($id) { $userId = Auth::user()->id; $moduleUser = ModuleAssigment::where(["salesman_id" => $userId, "status" => 1])->first(); if (!is_object($moduleUser)) { return back()->withErrors(['Usted no tiene modulo asignado, por lo tanto no puede vender']); } else { if (Auth::user()->role_id == config('constants.salesman') && Auth::user()->module && Auth::user()->module->openModule == 0) { return back()->withErrors(['Usted no ha abierto su caja, por lo tanto no puede vender']); } } //Buscar y enviar info de evento con $id $event = Event::find($id); $presentations = Presentation::where('event_id', $id)->where('cancelled', 0)->where('starts_at', '>', strtotime(Carbon::now()))->get(); $slots_array = array(); foreach ($presentations as $pres) { $slots = DB::table('slot_presentation')->where('presentation_id', $pres->id)->where('status', config('constants.seat_available'))->lists('slot_id', 'slot_id'); $slots_array[$pres->id] = $slots; } $presentations = $presentations->lists('starts_at', 'id'); foreach ($presentations as $key => $pres) { $presentations[$key] = date("Y-m-d H:i", $pres); } $presentations = $presentations->toArray(); $zones = Zone::where('event_id', $id)->lists('name', 'id'); $exchangeRate = ExchangeRate::where('status', config('constants.active'))->first(); return view('internal.salesman.buy', compact('event', 'presentations', 'zones', 'slots_array', 'exchangeRate')); }
/** * Run the database seeds. * * @return void */ public function run() { ExchangeRate::insert(['buyingRate' => 2.98, 'sellingRate' => 3.01, 'status' => 1, 'startDate' => Carbon::create(2015, 10, 8)->toDateString(), 'finishDate' => null]); }