public static function _init() { $subquery = \DB::select('price.id')->from(array(Model_Price::table(), 'price'))->where('price.product_id', '=', \DB::expr(\DB::quote_identifier('product.id')))->where('price.available', 1)->order_by('price.price')->limit(1); static::$_valid_relations['has_one'] = 'Indigo\\Erp\\Stock\\HasOne'; static::$_has_one['price']['key_from'] = array('id', $subquery); static::$_has_one['price']['key_to'] = array('product_id', 'id'); static::$_has_many['prices'] = array('model_to' => 'Model_Price', 'cascade_delete' => true); }
/** * Update price and availability * * @param boolean $cached Update from already downloaded files * @return boolean All products have been processed */ public function change($cached = false) { // Get data from supplier if (!($products = $this->_change($cached))) { return true; } // Get current prices from supplier $price = \DB::select('external_id', 'price', 'available')->from(Model_Price::table())->where('supplier_id', $this->model->id)->execute()->as_array('external_id'); // Cast values to the appropriate data type array_walk($price, function (&$product, $id) use(&$price) { $product['price'] = \Num::currency($product['price']); $product['available'] = intval($product['available']); unset($price[$id]['external_id']); }); $count = array(0); $available = array(); foreach ($products as $id => $product) { // Are we sure that it exists? if (!array_key_exists($id, $price)) { continue; } // Default data casting and values $product['price'] = \Arr::get($product, 'price'); is_null($product['price']) or $product['price'] = \Num::currency($product['price']); $a = \Arr::get($product, 'available'); // Check if product's price has been changed, or just became (un)available if (!is_null($product['price'])) { // Foolproofness: set the update array manually $product = array('price' => $product['price'], 'external_id' => $id, 'supplier_id' => $this->model->id); // Update availability if changed is_null($a) or $product['available'] = $a; // Get job and queue $job = $this->get_config('change.job', 'Indigo\\Erp\\Stock\\Job_Supplier_Change'); $queue = $this->get_config('change.queue', 'update'); // Use Queue if available (greater performance) if (\Package::loaded('queue')) { $count[0] += \Queue::push($queue, $job, array($product, $price[$id])) ? 1 : 0; } else { try { $job = new $job(); $count[0] += $job->execute(null, $product); } catch (\Exception $e) { } } } elseif (!is_null($a)) { $available[$a][] = $id; } } // Update availability information $available = $this->_available($available); $count[1] = $available[0]; $count[2] = $available[1]; // Set the last updated time for supplier prices $this->model->set('last_update', time())->save(); // Log success \Log::info($this->model->name . ' frissítve: ' . $count[0] . ' frissítés, ' . $count[1] . ' lett elérhetetlen, ' . $count[2] . ' lett elérhető.'); // All product have been processed return array_sum($count) == count($products); }