/**
  * Verifies that querying for a part works correctly.
  * 
  * There was a bug where only lower-case strings matched against parts, fixed 2012-03-16.
  */
 public function testPartNameQuerying()
 {
     $partName = "testPartNameQuerying";
     $part = new Part();
     $part->setName($partName);
     $part->setCategory(PartCategoryManager::getInstance()->getRootNode()->getNode());
     $part->setStorageLocation(StorageLocationManager::getInstance()->getStorageLocation(self::$storageLocation));
     PartKeepr::getEM()->persist($part);
     PartKeepr::getEM()->flush();
     $service = new PartService(array("query" => $partName));
     $response = $service->get();
     $this->assertEquals(1, $response["totalCount"], "The resultset totalCount is wrong.");
     $this->assertEquals($response["data"][0]["name"], $part->getName());
 }
 /**
  * Creates multiple storage locations at once. 
  * 
  * Requires that the parameter "storageLocations" is set to an array with the names of the storage locations.
  * Returns all error messages as "data" index in the result array.
  */
 public function massCreate()
 {
     $this->requireParameter("storageLocations");
     $aMessages = array();
     foreach ($this->getParameter("storageLocations") as $storageLocation) {
         try {
             $obj = StorageLocationManager::getInstance()->getStorageLocationByName($storageLocation);
             $aMessages[] = sprintf(PartKeepr::i18n("Storage Location %s already exists"), $storageLocation);
         } catch (\Exception $e) {
             $obj = new StorageLocation();
             $obj->setName($storageLocation);
             PartKeepr::getEM()->persist($obj);
         }
     }
     PartKeepr::getEM()->flush();
     return array("data" => $aMessages);
 }
 /**
  * Migrates the storage locations
  */
 public function run()
 {
     $count = 0;
     $skipped = 0;
     $r = mysql_query("SELECT * FROM storeloc");
     while ($store = mysql_fetch_assoc($r)) {
         $name = PartDBMigration::convertText($store["name"]);
         try {
             $storageLocation = StorageLocationManager::getInstance()->getStorageLocationByName($name);
             $skipped++;
         } catch (\Exception $e) {
             $oStorageLocation = new StorageLocation();
             $oStorageLocation->setName($name);
             $this->entityManager->persist($oStorageLocation);
             $count++;
         }
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Migrated %d storage locations, skipped %d because they already exist", $count, $skipped));
 }
 public function addOrUpdatePart($aParameters)
 {
     if (!array_key_exists("quantity", $aParameters)) {
         $aParameters["quantity"] = 0;
     }
     if ($aParameters["part"] !== null) {
         try {
             $part = $this->getPart($aParameters["part"]);
         } catch (\Exception $e) {
             $part = new Part();
             $user = SessionManager::getCurrentSession()->getUser();
             $stock = new StockEntry($part, $aParameters["quantity"], $user);
             PartKeepr::getEM()->persist($stock);
         }
     } else {
         $part = new Part();
         $user = SessionManager::getCurrentSession()->getUser();
         $stock = new StockEntry($part, $aParameters["quantity"], $user);
         PartKeepr::getEM()->persist($stock);
     }
     if (array_key_exists("name", $aParameters)) {
         $part->setName($aParameters["name"]);
     }
     if (array_key_exists("description", $aParameters)) {
         $part->setDescription($aParameters["description"]);
     }
     if (array_key_exists("minstock", $aParameters)) {
         $part->setMinStockLevel($aParameters["minstock"]);
     }
     if (array_key_exists("comment", $aParameters)) {
         $part->setComment($aParameters["comment"]);
     }
     if (array_key_exists("footprint", $aParameters)) {
         if ($aParameters["footprint"] === null) {
             $part->setFootprint(null);
         } else {
             $footprint = FootprintManager::getInstance()->getOrCreateFootprint($aParameters["footprint"]);
             $part->setFootprint($footprint);
         }
     }
     if (array_key_exists("storagelocation", $aParameters)) {
         $storageLocation = StorageLocationManager::getInstance()->getOrCreateStorageLocation($aParameters["storagelocation"]);
         $part->setStorageLocation($storageLocation);
     }
     if (array_key_exists("category", $aParameters)) {
         $category = PartCategoryManager::getInstance()->getCategory($aParameters["category"]);
         $part->setCategory($category->getNode());
     }
     /* Process linked changes */
     if (array_key_exists("distributorChanges", $aParameters)) {
         if (is_array($aParameters["distributorChanges"])) {
             $this->processDistributorChanges($part, $aParameters["distributorChanges"]);
         }
     }
     if (array_key_exists("manufacturerChanges", $aParameters)) {
         if (is_array($aParameters["manufacturerChanges"])) {
             $this->processManufacturerChanges($part, $aParameters["manufacturerChanges"]);
         }
     }
     if (array_key_exists("parameterChanges", $aParameters)) {
         if (is_array($aParameters["parameterChanges"])) {
             $this->processParameterChanges($part, $aParameters["parameterChanges"]);
         }
     }
     if (array_key_exists("attachmentChanges", $aParameters)) {
         if (is_array($aParameters["attachmentChanges"])) {
             $this->processAttachmentChanges($part, $aParameters["attachmentChanges"]);
         }
     }
     if (array_key_exists("partUnit", $aParameters)) {
         if ($aParameters["partUnit"] === null || $aParameters["partUnit"] === 0) {
             $part->setPartUnit(null);
         } else {
             $part->setPartUnit(PartUnitManager::getInstance()->getPartUnit($aParameters["partUnit"]));
         }
     }
     PartKeepr::getEM()->persist($part);
     PartKeepr::getEM()->flush();
 }