/** * Imports commodity information from EDDB */ public function actionCommodities() { $eddbApi = Yii::$app->params['eddb']['archive'] . 'commodities.json'; $curl = new Curl(); $curl->get($eddbApi); if ($curl->error) { throw new \yii\base\Exception('Error: ' . $curl->errorCode . ': ' . $curl->errorMessage); } // Iterate through all the categories via the curl response and insert them into the database foreach ($curl->response as $k => $obj) { $this->stdOut("Importing: "); $this->stdOut("{$obj->name}\r", Console::BOLD); $category = CommodityCategory::find()->where(['id' => $obj->category_id])->one(); if ($category === NULL) { $this->stdOut('Importing new category: ' . $obj->category->name . "\n"); $category = new CommodityCategory(); $category->id = $obj->category_id; } $category->name = $obj->category->name; $category->save(); // Import the commodity $commodity = Commodity::find()->where(['id' => $obj->id])->one(); if ($commodity === NULL) { $this->stdOut('Importing new commodity: ' . $obj->name . "\n"); $commodity = new Commodity(); $commodity->id = $obj->id; $commodity->name = $obj->name; } $commodity->average_price = $obj->average_price; $commodity->category_id = $obj->category_id; $commodity->save(); } $this->stdOut("\n\r"); }
public function store() { // $rules = array('name' => 'required|unique:commodities,name', 'description' => 'required', 'unit_price' => 'required|numeric', 'item_code' => 'required', 'storage_req' => 'required', 'min_level' => 'required|numeric', 'max_level' => 'required|numeric'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator); } else { // store $commodity = new Commodity(); $commodity->name = Input::get('name'); $commodity->description = Input::get('description'); $commodity->metric_id = Input::get('unit_of_issue'); $commodity->unit_price = Input::get('unit_price'); $commodity->item_code = Input::get('item_code'); $commodity->storage_req = Input::get('storage_req'); $commodity->min_level = Input::get('min_level'); $commodity->max_level = Input::get('max_level'); try { $commodity->save(); return redirect()->to('commodity.index')->with('message', trans('messages.commodity-succesfully-added')); } catch (QueryException $e) { Log::error($e); } } }
/** * Creates a new Commodity model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Commodity(); $model->beforeSave(true); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }