/** * Install a consumable to a printer */ public function installTo($printer) { global $db; #printf('Installing consumable %s to printer %s.', $this->getName(), $printer->getName()); #fCore::dump($printer); // Validation checks // 1. Check printer model is compatible $models = $this->getModels(); if (!in_array($printer->getModelId(), $models)) { $this->err = sprintf('Consumable %s not compatible with printer %s.', $this->getName(), $printer->getName()); return FALSE; } // 2. Check quantity if ($this->getQty() < 1) { $this->err = sprintf('No stock of the consumable %s.', $this->getName()); return FALSE; } // Finished validation // Add 'event' try { $e = new Event(); $e->setPrinterId($printer->getId()); $e->setConsumableId($this->getId()); $e->setDate(date('Y-m-d H:i:s')); $e->setCost($this->getCost()); $e->store(); } catch (fExpectedException $e) { #fMessaging::create('error', fURL::get(), $e->getMessage()); $this->err = $e->getMessage(); return FALSE; } catch (fSQLException $e) { #fMessaging::create('error', fURL::get(), 'Database error: ' . $e->getMessage()); $this->err = $e->getMessage(); return FALSE; } // Decrease stock try { $sql = 'UPDATE consumables SET qty = qty - 1 WHERE id = %i LIMIT 1'; $query = $db->execute($sql, $this->getId()); } catch (fSQLException $e) { $this->err = $e->getMessage(); return FALSE; } // Return true #echo 'Done!'; return TRUE; }