consumables = Consumable::all(); } protected function handle($row) { parent::handle($row); // TODO: Change the autogenerated stub $this->createConsumableIfNotExists(); } /** * Create a consumable if a duplicate does not exist * * @author Daniel Melzter * @since 3.0 */ public function createConsumableIfNotExists() { $consumable = null; $editingConsumable = false; $this->log("Creating Consumable"); $consumable = $this->consumables->search(function ($key) { return strcasecmp($key->name, $this->item['item_name']) == 0; }); if($consumable) { $editingConsumable = true; if (!$this->updating) { $this->log('A matching Consumable ' . $this->item["item_name"] . ' already exists. '); return; } } else { $this->log("No matching consumable, creating one"); $consumable = new Consumable(); } if (!$editingConsumable) { $consumable->name = $this->item["item_name"]; } if (!empty($this->item["purchase_date"])) { $consumable->purchase_date = $this->item["purchase_date"]; } else { $consumable->purchase_date = null; } if (!empty($this->item["purchase_cost"])) { $consumable->purchase_cost = Helper::ParseFloat($this->item["purchase_cost"]); } if (isset($this->item["location"])) { $consumable->location_id = $this->item["location"]->id; } $consumable->user_id = $this->user_id; if (isset($this->item["company"])) { $consumable->company_id = $this->item["company"]->id; } if (!empty($this->item["order_number"])) { $consumable->order_number = $this->item["order_number"]; } if (isset($this->item["category"])) { $consumable->category_id = $this->item["category"]->id; } // TODO:Implement //$consumable->notes= e($this->item_notes); if (!empty($this->item["requestable"])) { $consumable->requestable = filter_var($this->item["requestable"], FILTER_VALIDATE_BOOLEAN); } if (!empty($this->item["quantity"])) { if ($this->item["quantity"] > -1) { $consumable->qty = $this->item["quantity"]; } else { $consumable->qty = 1; } } if (!$this->testRun) { if ($consumable->save()) { $consumable->logCreate('Imported using CSV Importer'); $this->log("Consumable " . $this->item["item_name"] . ' was created'); } else { $this->jsonError($consumable, 'Consumable', $consumable->getErrors()); } } else { $this->log('TEST RUN - Consumable ' . $this->item['item_name'] . ' not created'); } } }