public function generatePartsInventory() { // get part types $SQL = "SELECT * FROM PartType"; $statement = new Statement($this->connection); $data = $statement->select($SQL)->all(); $mechanic = 11; $loc = "0000000000"; foreach ($data as $d) { $part = $d['idPartType']; for ($i = 0; $i < 15; $i++) { $insert = 'INSERT INTO PartsInventory (_idPartType, _MechanicIn, DateIn, Location) VALUES (:type, :mechanic, CURRENT_TIMESTAMP, :loc)'; $statement = new Statement($this->connection); $statement->setInt("type", $part); $statement->setInt("mechanic", $mechanic); $statement->setStr("loc", $loc); $statement->insert($insert); } } $SQL = "SELECT * FROM PartsInventory"; $statement = new Statement($this->connection); $data = $statement->select($SQL)->all(); var_dump($data); }
/** * Add new Maintenance Item * */ public function add($params) { $errors = []; $log = $params['log']; $mechanic = Session::get('id'); $input = $this->request->getParameters(); $description = array_shift($input); $parts = []; foreach ($input as $key => $value) { if ((int) $value > 0) { $part = ['id' => (int) preg_replace('/^part-/', '', $key), 'count' => (int) $value]; $parts[] = $part; } } // insert $SQL = 'INSERT INTO MaintenanceItem (_MaintenanceLogNumber, ItemDescription) VALUES (:log, :description)'; $statement = new Statement(new Connection()); $statement->setInt('log', $log); $statement->setStr('description', $description); $item = $statement->insert($SQL); // $SQL = 'Call UsePart(:mechanic, :item, :part)'; $SQL = 'INSERT INTO PartsUsed (_idPartsInventory, _MechanicOut, _idMaintenanceItem, DateOut) SELECT idPartsInventory, :mechanic, :item, CURRENT_TIMESTAMP FROM PartsInventory i WHERE i.idPartsInventory NOT IN (SELECT _idPartsInventory FROM PartsUsed) AND i._idPartType IN (SELECT idPartType FROM PartType WHERE idPartType = :part) ORDER BY DateIn DESC LIMIT 1'; $statement = new Statement(new Connection()); $statement->prepare($SQL); $statement->setInt('mechanic', $mechanic); $statement->setInt('item', $item); foreach ($parts as $part) { $statement->setInt('part', $part['id']); $statement->bindParams(); for ($i = 0; $i < $part['count']; $i++) { $id = $statement->execute()->lastID(); // to do // error needs to be displayed when insert fails. } } if ($errors) { $params = ['log' => $log, 'errors' => $errors]; $this->display($params); } header("Location: /maintenance/log/{$log}"); }