/** * Supplier driver forge. * * @param string $supplier Supplier id or friendly name * @param array $config Config array * @return Supplier instance */ public static function forge($supplier, $config = array()) { $model = Model_Supplier::query(); if (is_int($supplier)) { $model = $model->where('id', $supplier)->get_one(); } elseif (is_string($supplier)) { $model = $model->where('slug', $supplier)->get_one(); } elseif ($supplier instanceof Model_Supplier) { $model = $supplier; } else { throw new SupplierException('Invalid Supplier!'); } if (!$model instanceof Model_Supplier) { throw new SupplierException('Supplier ' . $supplier . ' not found'); } if (\Arr::get($config, 'filter_disabled', false) === true and $model->enabled !== 1) { throw new SupplierException('This supplier (' . $model->name . ') has been disabled.'); } if (isset($model->driver) and empty($model->driver)) { throw new SupplierException('This supplier (' . $model->name . ') has no driver to be used.'); } $driver = ucfirst(strtolower(isset($model->driver) ? $model->driver : $model->slug)); $class = 'Indigo\\Erp\\Stock\\Supplier_' . $driver; if (!class_exists($class, true)) { throw new \FuelException('Could not find Supplier driver: ' . $driver); } $config = \Arr::merge(static::$_defaults, \Config::get('supplier.drivers.' . strtolower($driver), array()), $config); $driver = new $class($model, $config); return $driver; }
/** * 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); }
public function action_delete($id = null) { if ($supplier = Model_Supplier::find($id)) { $supplier->delete(); Session::set_flash('success', e('Deleted supplier #' . $id)); } else { Session::set_flash('error', e('Could not delete supplier #' . $id)); } Response::redirect('admin/suppliers'); }