partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit d93b0f6571e80e14f60f980848d77c9ad9b0df45
parent 0a186cfbb58fd0d3d7176b00bd8b57a3e6ce2cee
Author: Timo A. Hummel <felicitus@felicitus.org>
Date:   Mon, 27 Jul 2015 20:26:51 +0200

Refactored the part entity to its own bundle

Diffstat:
Mapp/config/config.yml | 13+++++++++++++
Mcronjobs/UpdatePartCacheData.php | 2+-
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Grid/GridMenuPlugin.js | 2+-
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/Editor/PartEditorWindow.js | 2+-
Asrc/PartKeepr/PartBundle/Entity/Part.php | 605+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/PartBundle/Entity/PartMeasurementUnit.php | 2+-
Msrc/backend/PartKeepr/FulltextSearch/FulltextSearch.php | 2+-
Msrc/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php | 2+-
Dsrc/backend/PartKeepr/Part/Part.php | 682-------------------------------------------------------------------------------
Msrc/backend/PartKeepr/Part/PartAttachment.php | 3++-
Msrc/backend/PartKeepr/Part/PartDistributor.php | 3++-
Msrc/backend/PartKeepr/Part/PartFulltextSearch.php | 2+-
Msrc/backend/PartKeepr/Part/PartImage.php | 3++-
Msrc/backend/PartKeepr/Part/PartManager.php | 14+++++++-------
Msrc/backend/PartKeepr/Part/PartManufacturer.php | 3++-
Msrc/backend/PartKeepr/Part/PartService.php | 1+
Msrc/backend/PartKeepr/PartKeepr.php | 2+-
Msrc/backend/PartKeepr/PartParameter/PartParameter.php | 6+++---
Msrc/backend/PartKeepr/Project/ProjectPart.php | 8++++----
Msrc/backend/PartKeepr/ProjectReport/ProjectReportService.php | 2+-
Msrc/backend/PartKeepr/Setup/Migration/PartDB/PartMigration.php | 2+-
Msrc/backend/PartKeepr/Stock/StockEntry.php | 6+++---
22 files changed, 654 insertions(+), 713 deletions(-)

diff --git a/app/config/config.yml b/app/config/config.yml @@ -318,6 +318,19 @@ services: arguments: - { groups: [ "default" ] } + resource.part: + parent: "api.resource" + arguments: [ "PartKeepr\\PartBundle\\Entity\\Part" ] + tags: [ { name: "api.resource" } ] + calls: + - method: "initFilters" + arguments: [ [ "@doctrine_reflection_service.search_filter" ] ] + - method: "initNormalizationContext" + arguments: [ { groups: [ "default" ] } ] + - method: "initDenormalizationContext" + arguments: + - { groups: [ "default" ] } + resource.manufacturer: parent: "api.resource" arguments: [ "PartKeepr\\ManufacturerBundle\Entity\Manufacturer" ] diff --git a/cronjobs/UpdatePartCacheData.php b/cronjobs/UpdatePartCacheData.php @@ -9,7 +9,7 @@ use PartKeepr\PartKeepr, PartKeepr::initialize(); -$query = PartKeepr::getEM()->createQuery("SELECT p FROM PartKeepr\Part\Part p"); +$query = PartKeepr::getEM()->createQuery("SELECT p FROM PartKeepr\PartBundle\Entity\Part p"); $result = $query->getResult(); $fc = 0; diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Grid/GridMenuPlugin.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Grid/GridMenuPlugin.js @@ -102,7 +102,7 @@ Ext.define("PartKeepr.GridMenuPlugin", { } var val = Ext.create("PartKeepr.PrintingWindow"); - val.setObjectType('PartKeepr\\Part\\Part'); + val.setObjectType('PartKeepr\\PartBundle\\Entity\\Part'); val.setObjectIds(ids); val.show(); }, diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/Editor/PartEditorWindow.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Part/Editor/PartEditorWindow.js @@ -162,7 +162,7 @@ Ext.define('PartKeepr.PartEditorWindow', { */ printItem: function( id ){ var val = Ext.create("PartKeepr.PrintingWindow"); - val.setObjectType('PartKeepr\\Part\\Part'); + val.setObjectType('PartKeepr\\PartBundle\\Entity\\Part'); val.setContext("PartEditor"); val.setObjectIds([id]); val.show(); diff --git a/src/PartKeepr/PartBundle/Entity/Part.php b/src/PartKeepr/PartBundle/Entity/Part.php @@ -0,0 +1,605 @@ +<?php +namespace PartKeepr\PartBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\FootprintBundle\Entity\Footprint; +use Doctrine\Common\Collections\ArrayCollection; +use PartKeepr\Part\Exceptions\CategoryNotAssignedException; +use PartKeepr\Part\Exceptions\StorageLocationNotAssignedException; +use PartKeepr\Part\PartAttachment; +use PartKeepr\Part\PartImage; +use PartKeepr\PartKeepr; +use PartKeepr\StorageLocationBundle\Entity\StorageLocation; +use PartKeepr\Util\BaseEntity; +use PartKeepr\Util\Exceptions\OutOfRangeException; + + +/** + * Represents a part in the database. The heart of our project. Handle with care! + * + * @ORM\Entity @ORM\HasLifecycleCallbacks + */ +class Part extends BaseEntity +{ + /** + * The category of the part + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\PartCategory") + * @var PartCategory + */ + private $category; + + /** + * The part's name + * @ORM\Column + * @var string + */ + private $name; + + /** + * The part's short description + * @ORM\Column(type="string",nullable=true) + * @var string + */ + private $description; + + /** + * The footprint of this part + * @ORM\ManyToOne(targetEntity="PartKeepr\FootprintBundle\Entity\Footprint") + * + * @var Footprint + */ + private $footprint; + + /** + * The unit in which the part's "amount" is calculated. This is necessary to count parts + * in "pieces", "meters" or "grams". + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\PartMeasurementUnit", inversedBy="parts") + * + * @var PartMeasurementUnit + */ + private $partUnit; + + /** + * Defines the storage location of this part + * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation") + * @var StorageLocation + */ + private $storageLocation; + + /** + * Holds the manufacturers which can manufacture this part + * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartManufacturer",mappedBy="part",cascade={"persist", "remove"}) + * @var ArrayCollection + */ + private $manufacturers; + + /** + * Holds the distributors from where we can buy the part + * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartDistributor",mappedBy="part",cascade={"persist", "remove"}) + * @var ArrayCollection + */ + private $distributors; + + /** + * Holds the part images + * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartImage",mappedBy="part",cascade={"persist", "remove"}) + * @var PartImage + */ + private $images; + + /** + * Holds the part attachments + * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartAttachment",mappedBy="part",cascade={"persist", "remove"}) + * @var PartAttachment + */ + private $attachments; + + /** + * The comment for this part + * @ORM\Column(type="text") + */ + private $comment = ""; + + /** + * The stock level. Note that this is a cached value, because it makes our summary queries easier. + * @todo It would be nice if we could get rid of that. + * @ORM\Column(type="integer") + * @var integer + */ + private $stockLevel = 0; + + /** + * The minimum stock level for this part. If we run out of this part (e.g. stockLevel < minStockLevel), + * we can see that in the system and re-order parts. + * + * @ORM\Column(type="integer") + * @var integer + */ + private $minStockLevel = 0; + + /** + * The average price for the part. Note that this is a cached value. + * + * @todo It would be nice if we could get rid of that + * @ORM\Column(type="decimal",precision=13,scale=4,nullable=true) + * @var float + */ + private $averagePrice = null; + + /** + * The stock level history + * @ORM\OneToMany(targetEntity="PartKeepr\Stock\StockEntry",mappedBy="part",cascade={"persist", "remove"}) + * @var ArrayCollection + */ + private $stockLevels; + + /** + * The parameters for this part + * @ORM\OneToMany(targetEntity="PartKeepr\PartParameter\PartParameter",mappedBy="part",cascade={"persist", "remove"}) + * @var ArrayCollection + */ + private $parameters; + + /** + * The part status for this part + * @ORM\Column(type="string",nullable=true) + * @var string + */ + private $status; + + /** + * Defines if the part needs review + * @ORM\Column(type="boolean") + * @var boolean + */ + private $needsReview; + + /** + * Defines the condition of the part + * @ORM\Column(type="string",nullable=true) + * @var string + */ + private $partCondition; + + /** + * The create date+time for this part + * @ORM\Column(type="datetime",nullable=true) + * @var \DateTime + */ + private $createDate; + + /** + * @ORM\OneToMany(targetEntity="PartKeepr\Project\ProjectPart", mappedBy="part") + **/ + private $projectParts; + + /** + * The internal part number + * @ORM\Column(type="string",nullable=true) + * @var string + */ + private $internalPartNumber; + + public function __construct() + { + $this->distributors = new \Doctrine\Common\Collections\ArrayCollection(); + $this->manufacturers = new \Doctrine\Common\Collections\ArrayCollection(); + $this->parameters = new \Doctrine\Common\Collections\ArrayCollection(); + $this->images = new \Doctrine\Common\Collections\ArrayCollection(); + $this->attachments = new \Doctrine\Common\Collections\ArrayCollection(); + $this->setCreateDate(new \DateTime()); + $this->setReviewFlag(false); + } + + /** + * Sets the name for this part + * @param string $name The part's name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * Returns the name of this part + * @return string The part name + */ + public function getName() + { + return $this->name; + } + + /** + * Sets the internal part number for this part + * @param string $partnumber + */ + public function setInternalPartNumber($partNumber) + { + $this->internalPartNumber = $partNumber; + } + + /** + * Returns the internal part number for this part + * @return string the internal part number + */ + public function getInternalPartNumber() + { + return $this->internalPartNumber; + } + + /** + * Sets the description for this part + * @param string $description The part's short description + */ + public function setDescription($description) + { + $this->description = $description; + } + + /** + * Returns the short description of this part + * @return string The part description + */ + public function getDescription() + { + return $this->description; + } + + /** + * Sets the part unit + * + * @param PartMeasurementUnit $partUnit The part unit object to set + * + */ + public function setPartUnit(PartMeasurementUnit $partUnit) + { + $this->partUnit = $partUnit; + } + + /** + * Returns the part unit + * + * @param none + * + * @return PartMeasurementUnit The part unit object + */ + public function getPartUnit() + { + return $this->partUnit; + } + + /** + * Sets the average price for this unit + * @todo Is this actually used? + * @param float $price The price to set + */ + public function setAveragePrice($price) + { + $this->averagePrice = $price; + } + + /** + * Updates the internal stock level from the stock history + */ + public function updateStockLevel() + { + $this->stockLevel = $this->getStockLevel(); + } + + /** + * Sets the review flag + * @param boolean $bReview True if the part needs review, false otherwise + */ + public function setReviewFlag($bReview) + { + $this->needsReview = $bReview; + } + + /** + * Returns the review flag + * @return boolean True if the part needs review, false otherwise + */ + public function getReviewFlag() + { + return $this->needsReview; + } + + /** + * Sets the condition for this part + * @param string $partCondition The part's condition + */ + public function setCondition($partCondition) + { + $this->partCondition = $partCondition; + } + + /** + * Returns the condition of this part + * @return string The part condition + */ + public function getCondition() + { + return $this->partCondition; + } + + + /** + * Set the minimum stock level for this part + * + * Only positive values are allowed. + * + * @param int $minStockLevel A minimum stock level, only values >= 0 are allowed. + * @throws \PartKeepr\Util\Exceptions\OutOfRangeException If the passed stock level is not in range (>=0) + */ + public function setMinStockLevel($minStockLevel) + { + $minStockLevel = intval($minStockLevel); + + if ($minStockLevel < 0) { + $exception = new OutOfRangeException(PartKeepr::i18n("Minimum Stock Level is out of range")); + $exception->setDetail(PartKeepr::i18n("The minimum stock level must be 0 or higher")); + throw $exception; + } + $this->minStockLevel = $minStockLevel; + } + + /** + * Sets the category for this part + * + * @param PartCategory $category The category + */ + public function setCategory(PartCategory $category) + { + $this->category = $category; + } + + /** + * Returns the assigned category + * + * @return PartCategory + */ + public function getCategory() + { + return $this->category; + } + + /** + * Sets the storage location for this part + * + * @param \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location + */ + public function setStorageLocation(StorageLocation $storageLocation) + { + $this->storageLocation = $storageLocation; + } + + /** + * Returns the storage location for this part + * + * @return \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location + */ + public function getStorageLocation() + { + return $this->storageLocation; + } + + /** + * Sets the footprint for this part + * + * @param \PartKeepr\FootprintBundle\Entity\Footprint $footprint The footprint to set + */ + public function setFootprint(Footprint $footprint = null) + { + $this->footprint = $footprint; + } + + /** + * Retrieves the footrpint + */ + public function getFootprint() + { + return $this->footprint; + } + + /** + * Sets the comment for this part + * @param string $comment The comment for this part + */ + public function setComment($comment) + { + $this->comment = $comment; + } + + /** + * Returns the comment for this part + * @return string The comment + */ + public function getComment() + { + return $this->comment; + } + + /** + * Returns the distributors array + * @return ArrayCollection the distributors + */ + public function getDistributors() + { + return $this->distributors; + } + + /** + * Returns the part images array + * @return ArrayCollection the part images + */ + public function getImages() + { + return $this->images; + } + + /** + * Returns the part attachments array + * @return ArrayCollection the part attachments + */ + public function getAttachments() + { + return $this->attachments; + } + + /** + * Returns the manufacturers array + * @return ArrayCollection the manufactuers + */ + public function getManufacturers() + { + return $this->manufacturers; + } + + /** + * Returns the parameters assigned to this part + * @return array An array of PartParameter objects + */ + public function getParameters() + { + return $this->parameters; + } + + /** + * Returns the stock level of this part. This is a realtime function which + * actually creates a query over the StockEntry table. + * @return int The stock level + */ + public function getStockLevel() + { + $query = PartKeepr::getEM()->createQuery("SELECT SUM(s.stockLevel) FROM PartKeepr\Stock\StockEntry s WHERE s.part = :part"); + $query->setParameter("part", $this); + + $count = $query->getSingleScalarResult(); + if ($count == null) + $count = 0; + return $count; + } + + /** + * Sets the create date for this part + * @param \DateTime $dateTime The create date+time + */ + private function setCreateDate(\DateTime $dateTime) + { + $this->createDate = $dateTime; + } + + /** + * Returns the create date + * @return \DateTime The create date+time + */ + public function getCreateDate() + { + return $this->createDate; + } + + /** + * Sets the status for this part. A status is any string describing the status, + * e.g. "new", "used", "broken" etc. + * @param string $status The status + */ + public function setStatus($status) + { + $this->status = $status; + } + + /** + * Returns the status for this part. + * @return string The status + */ + public function getStatus() + { + return $this->status; + } + + public function updateCacheData() + { + $this->updateStockLevel(); + $this->updatePrice(); + } + + /** + * Updates the average price for a part + */ + public function updatePrice() + { + $query = PartKeepr::getEM()->createQuery("SELECT SUM(se.price*se.stockLevel) / SUM(se.stockLevel) FROM PartKeepr\Stock\StockEntry se WHERE se.part = :part AND se.stockLevel > 0"); + $query->setParameter("part", $this); + $val = $query->getSingleScalarResult(); + + $query = PartKeepr::getEM()->createQuery('UPDATE PartKeepr\PartBundle\Entity\Part p SET p.averagePrice = :val WHERE p = :part'); + $query->setParameter("val", $val); + $query->setParameter("part", $this); + $query->execute(); + } + + /** + * Checks if the part category is set. + * + * @throws CategoryNotAssignedException + */ + private function checkCategoryConsistency() + { + if ($this->getCategory() === null) { + throw new CategoryNotAssignedException($this); + } + } + + /** + * Checks if the part storage location is set. + * + * @throws StorageLocationNotAssignedException + */ + private function checkStorageLocationConsistency() + { + if ($this->getStorageLocation() === null) { + throw new StorageLocationNotAssignedException(); + } + } + + /** + * Checks if the requirements for database persisting are given. + * + * @throws CategoryNotAssignedException Thrown if no category is set + * @throws StorageLocationNotAssignedException Thrown if no storage location is set + * + * @ORM\PrePersist + */ + public function onPrePersist() + { + $this->checkCategoryConsistency(); + $this->checkStorageLocationConsistency(); + } + + /** + * + * Checks if the requirements for database persisting are given. + * + * For a list of exceptions, see + * @see PartKeepr\Part.Part::onPrePersist() + * + * @ORM\PreUpdate + */ + public function onPreUpdate() + { + $this->checkCategoryConsistency(); + $this->checkStorageLocationConsistency(); + } + + /** + * Returns a string representation of the part + * + * @param none + * @return string The name and the ID of the part + */ + public function __toString() + { + return $this->getName() . " (" . $this->getId() . ")"; + } + +} diff --git a/src/PartKeepr/PartBundle/Entity/PartMeasurementUnit.php b/src/PartKeepr/PartBundle/Entity/PartMeasurementUnit.php @@ -55,7 +55,7 @@ class PartMeasurementUnit extends BaseEntity { /** * The parts used by this PartMeasurementUnit * - * @ORM\OneToMany(targetEntity="PartKeepr\Part\Part",mappedBy="partUnit") + * @ORM\OneToMany(targetEntity="PartKeepr\PartBundle\Entity\Part",mappedBy="partUnit") */ private $parts; diff --git a/src/backend/PartKeepr/FulltextSearch/FulltextSearch.php b/src/backend/PartKeepr/FulltextSearch/FulltextSearch.php @@ -79,7 +79,7 @@ abstract class FulltextSearch { * Returns the FQDN name of the entity. This needs to be overridden in child classes. * * @param none - * @return string the FQDN of the entity to query, e.g. PartKeepr\Part\Part + * @return string the FQDN of the entity to query, e.g. PartKeepr\PartBundle\Entity\Part */ abstract protected function getEntityName (); diff --git a/src/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php b/src/backend/PartKeepr/Part/Exceptions/CategoryNotAssignedException.php @@ -3,7 +3,7 @@ namespace PartKeepr\Part\Exceptions; use PartKeepr\PartKeepr, PartKeepr\Util\SerializableException, - PartKeepr\Part\Part; + PartKeepr\PartBundle\Entity\Part; /** * This exception is thrown when a part hasn't got a category assigned diff --git a/src/backend/PartKeepr/Part/Part.php b/src/backend/PartKeepr/Part/Part.php @@ -1,682 +0,0 @@ -<?php -namespace PartKeepr\Part; - -use PartKeepr\PartBundle\Entity\PartMeasurementUnit; -use PartKeepr\StorageLocationBundle\Entity\StorageLocation, - \PartKeepr\FootprintBundle\Entity\Footprint, - \PartKeepr\PartBundle\Entity\PartCategoryManager, - PartKeepr\Util\Deserializable, - \PartKeepr\PartBundle\Entity\PartCategory, - PartKeepr\Util\Serializable, - PartKeepr\Util\BaseEntity, - PartKeepr\PartKeepr, - PartKeepr\Part\Exceptions\CategoryNotAssignedException, - PartKeepr\Util\Exceptions\OutOfRangeException, - PartKeepr\Part\Exceptions\StorageLocationNotAssignedException, - Doctrine\ORM\Mapping as ORM, - Doctrine\ORM\Mapping\HasLifecycleCallbacks ; - - -/** - * Represents a part in the database. The heart of our project. Handle with care! - * - * @ORM\Entity @ORM\HasLifecycleCallbacks - */ -class Part extends BaseEntity implements Serializable, Deserializable { - /** - * The category of the part - * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\PartCategory") - * @var Category - */ - private $category; - - /** - * The part's name - * @ORM\Column - * @var string - */ - private $name; - - /** - * The part's short description - * @ORM\Column(type="string",nullable=true) - * @var string - */ - private $description; - - /** - * The footprint of this part - * @ORM\ManyToOne(targetEntity="PartKeepr\FootprintBundle\Entity\Footprint") - * - * @var Footprint - */ - private $footprint; - - /** - * The unit in which the part's "amount" is calculated. This is necessary to count parts - * in "pieces", "meters" or "grams". - * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\PartMeasurementUnit", inversedBy="parts") - * - * @var PartMeasurementUnit - */ - private $partUnit; - - /** - * Defines the storage location of this part - * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation") - * @var StorageLocation - */ - private $storageLocation; - - /** - * Holds the manufacturers which can manufacture this part - * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartManufacturer",mappedBy="part",cascade={"persist", "remove"}) - * @var ArrayCollection - */ - private $manufacturers; - - /** - * Holds the distributors from where we can buy the part - * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartDistributor",mappedBy="part",cascade={"persist", "remove"}) - * @var ArrayCollection - */ - private $distributors; - - /** - * Holds the part images - * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartImage",mappedBy="part",cascade={"persist", "remove"}) - * @var PartImage - */ - private $images; - - /** - * Holds the part attachments - * @ORM\OneToMany(targetEntity="PartKeepr\Part\PartAttachment",mappedBy="part",cascade={"persist", "remove"}) - * @var PartAttachment - */ - private $attachments; - - /** - * The comment for this part - * @ORM\Column(type="text") - */ - private $comment = ""; - - /** - * The stock level. Note that this is a cached value, because it makes our summary queries easier. - * @todo It would be nice if we could get rid of that. - * @ORM\Column(type="integer") - * @var integer - */ - private $stockLevel = 0; - - /** - * The minimum stock level for this part. If we run out of this part (e.g. stockLevel < minStockLevel), - * we can see that in the system and re-order parts. - * - * @ORM\Column(type="integer") - * @var integer - */ - private $minStockLevel = 0; - - /** - * The average price for the part. Note that this is a cached value. - * - * @todo It would be nice if we could get rid of that - * @ORM\Column(type="decimal",precision=13,scale=4,nullable=true) - * @var float - */ - private $averagePrice = null; - - /** - * The stock level history - * @ORM\OneToMany(targetEntity="PartKeepr\Stock\StockEntry",mappedBy="part",cascade={"persist", "remove"}) - * @var ArrayCollection - */ - private $stockLevels; - - /** - * The parameters for this part - * @ORM\OneToMany(targetEntity="PartKeepr\PartParameter\PartParameter",mappedBy="part",cascade={"persist", "remove"}) - * @var ArrayCollection - */ - private $parameters; - - /** - * The part status for this part - * @ORM\Column(type="string",nullable=true) - * @var string - */ - private $status; - - /** - * Defines if the part needs review - * @ORM\Column(type="boolean") - * @var boolean - */ - private $needsReview; - - /** - * Defines the condition of the part - * @ORM\Column(type="string",nullable=true) - * @var string - */ - private $partCondition; - - /** - * The create date+time for this part - * @ORM\Column(type="datetime",nullable=true) - * @var \DateTime - */ - private $createDate; - - /** - * @ORM\OneToMany(targetEntity="PartKeepr\Project\ProjectPart", mappedBy="part") - **/ - private $projectParts; - - /** - * The internal part number - * @ORM\Column(type="string",nullable=true) - * @var string - */ - private $internalPartNumber; - - public function __construct () { - $this->distributors = new \Doctrine\Common\Collections\ArrayCollection(); - $this->manufacturers = new \Doctrine\Common\Collections\ArrayCollection(); - $this->parameters = new \Doctrine\Common\Collections\ArrayCollection(); - $this->images = new \Doctrine\Common\Collections\ArrayCollection(); - $this->attachments = new \Doctrine\Common\Collections\ArrayCollection(); - $this->setCreateDate(new \DateTime()); - $this->setReviewFlag(false); - } - - /** - * Sets the name for this part - * @param string $name The part's name - */ - public function setName ($name) { - $this->name = $name; - } - - /** - * Returns the name of this part - * @return string The part name - */ - public function getName () { - return $this->name; - } - - /** - * Sets the internal part number for this part - * @param string $partnumber - */ - public function setInternalPartNumber ($partNumber) { - $this->internalPartNumber = $partNumber; - } - - /** - * Returns the internal part number for this part - * @return string the internal part number - */ - public function getInternalPartNumber () { - return $this->internalPartNumber; - } - - /** - * Sets the description for this part - * @param string $description The part's short description - */ - public function setDescription ($description) { - $this->description = $description; - } - - /** - * Returns the short description of this part - * @return string The part description - */ - public function getDescription () { - return $this->description; - } - - /** - * Sets the part unit - * - * @param PartMeasurementUnit $partUnit The part unit object to set - * -*@return nothing - */ - public function setPartUnit(PartMeasurementUnit $partUnit) { - $this->partUnit = $partUnit; - } - - /** - * Returns the part unit - * - * @param none - * -*@return PartMeasurementUnit The part unit object - */ - public function getPartUnit () { - return $this->partUnit; - } - - /** - * Sets the average price for this unit - * @todo Is this actually used? - * @param float $price The price to set - */ - public function setAveragePrice ($price) { - $this->averagePrice = $price; - } - - /** - * Updates the internal stock level from the stock history - */ - public function updateStockLevel () { - $this->stockLevel = $this->getStockLevel(); - } - - /** - * Sets the review flag - * @param boolean $bReview True if the part needs review, false otherwise - */ - public function setReviewFlag ($bReview) { - $this->needsReview = $bReview; - } - - /** - * Returns the review flag - * @return boolean True if the part needs review, false otherwise - */ - public function getReviewFlag () { - return $this->needsReview; - } - - /** - * Sets the condition for this part - * @param string $partCondition The part's condition - */ - public function setCondition ($partCondition) { - $this->partCondition = $partCondition; - } - - /** - * Returns the condition of this part - * @return string The part condition - */ - public function getCondition () { - return $this->partCondition; - } - - - /** - * Set the minimum stock level for this part - * - * Only positive values are allowed. - * - * @param int $minStockLevel A minimum stock level, only values >= 0 are allowed. - * @throws \PartKeepr\Util\Exceptions\OutOfRangeException If the passed stock level is not in range (>=0) - */ - public function setMinStockLevel ($minStockLevel) { - $minStockLevel = intval($minStockLevel); - - if ($minStockLevel < 0) { - $exception = new OutOfRangeException(PartKeepr::i18n("Minimum Stock Level is out of range")); - $exception->setDetail(PartKeepr::i18n("The minimum stock level must be 0 or higher")); - throw $exception; - } - $this->minStockLevel = $minStockLevel; - } - - /** - * Sets the category for this part - * -*@param \PartKeepr\PartBundle\Entity\PartCategory $category The category - */ - public function setCategory (PartCategory $category) { - $this->category = $category; - } - - /** - * Returns the assigned category - * -*@return \PartKeepr\PartBundle\Entity\PartCategory - */ - public function getCategory () { - return $this->category; - } - - /** - * Sets the storage location for this part - * -*@param \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location - */ - public function setStorageLocation (StorageLocation $storageLocation) { - $this->storageLocation = $storageLocation; - } - - /** - * Returns the storage location for this part - * -*@return \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location - */ - public function getStorageLocation () { - return $this->storageLocation; - } - - /** - * Sets the footprint for this part - * -*@param \PartKeepr\FootprintBundle\Entity\Footprint $footprint The footprint to set - */ - public function setFootprint (Footprint $footprint = null) { - $this->footprint = $footprint; - } - - /** - * Retrieves the footrpint - */ - public function getFootprint () { - return $this->footprint; - } - - /** - * Sets the comment for this part - * @param string $comment The comment for this part - */ - public function setComment ($comment) { - $this->comment = $comment; - } - - /** - * Returns the comment for this part - * @return string The comment - */ - public function getComment () { - return $this->comment; - } - - /** - * Returns the distributors array - * @return ArrayCollection the distributors - */ - public function getDistributors () { - return $this->distributors; - } - - /** - * Returns the part images array - * @return ArrayCollection the part images - */ - public function getImages () { - return $this->images; - } - - /** - * Returns the part attachments array - * @return ArrayCollection the part attachments - */ - public function getAttachments () { - return $this->attachments; - } - - /** - * Returns the manufacturers array - * @return ArrayCollection the manufactuers - */ - public function getManufacturers () { - return $this->manufacturers; - } - - /** - * Returns the parameters assigned to this part - * @return array An array of PartParameter objects - */ - public function getParameters () { - return $this->parameters; - } - - /** - * Returns the stock level of this part. This is a realtime function which - * actually creates a query over the StockEntry table. - * @return int The stock level - */ - public function getStockLevel () { - $query = PartKeepr::getEM()->createQuery("SELECT SUM(s.stockLevel) FROM PartKeepr\Stock\StockEntry s WHERE s.part = :part"); - $query->setParameter("part", $this); - - $count = $query->getSingleScalarResult(); - if ($count == null) - $count = 0; - return $count; - } - - /** - * Sets the create date for this part - * @param \DateTime $dateTime The create date+time - */ - private function setCreateDate (\DateTime $dateTime) { - $this->createDate = $dateTime; - } - - /** - * Returns the create date - * @return \DateTime The create date+time - */ - public function getCreateDate () { - return $this->createDate; - } - - /** - * Sets the status for this part. A status is any string describing the status, - * e.g. "new", "used", "broken" etc. - * @param string $status The status - */ - public function setStatus ($status) { - $this->status = $status; - } - - /** - * Returns the status for this part. - * @return string The status - */ - public function getStatus () { - return $this->status; - } - - public function updateCacheData () { - $this->updateStockLevel(); - $this->updatePrice(); - } - - /** - * Updates the average price for a part - */ - public function updatePrice () { - $query = PartKeepr::getEM()->createQuery("SELECT SUM(se.price*se.stockLevel) / SUM(se.stockLevel) FROM PartKeepr\Stock\StockEntry se WHERE se.part = :part AND se.stockLevel > 0"); - $query->setParameter("part", $this); - $val = $query->getSingleScalarResult(); - - $query = PartKeepr::getEM()->createQuery('UPDATE PartKeepr\Part\Part p SET p.averagePrice = :val WHERE p = :part'); - $query->setParameter("val", $val); - $query->setParameter("part", $this); - $query->execute(); - } - - /** - * (non-PHPdoc) - * @see PartKeepr\Util.Serializable::serialize() - */ - public function serialize () { - return array( - "id" => $this->getId(), - "name" => $this->getName(), - "description" => $this->getDescription(), - "comment" => $this->getComment(), - "stockLevel" => $this->getStockLevel(), - "footprint" => is_object($this->footprint) ? $this->footprint->getId() : null, - "minStockLevel" => $this->minStockLevel, - "status" => $this->getStatus(), - "storageLocation" => is_object($this->storageLocation) ? $this->storageLocation->getId() : null, - "category" => is_object($this->category) ? $this->category->getId() : null, - "partUnit" => is_object($this->partUnit) ? $this->getPartUnit()->getId() : null, - "manufacturers" => $this->serializeChildren($this->getManufacturers()), - "distributors" => $this->serializeChildren($this->getDistributors()), - "images" => $this->serializeChildren($this->getImages()), - "attachments" => $this->serializeChildren($this->getAttachments()), - "parameters" => $this->serializeChildren($this->getParameters()), - "createDate" => $this->getCreateDate()->format("Y-m-d H:i:s"), - "needsReview" => $this->getReviewFlag(), - "partCondition" => $this->getCondition(), - "internalPartNumber" => $this->getInternalPartNumber(), - // Additional things we serialize to make displaying stuff in the frontend easier - "categoryName" => is_object($this->category) ? $this->category->getName() : null, - "footprintName" => is_object($this->footprint) ? $this->footprint->getName() : null, - "storageLocationName" => is_object($this->storageLocation) ? $this->storageLocation->getName() : null - ); - } - - /** - * Deserializes the part - * @param array $parameters The array with the parameters to set - */ - public function deserialize (array $parameters) { - foreach ($parameters as $key => $value) { - switch ($key) { - case "name": - $this->setName($value); - break; - case "description": - $this->setDescription($value); - break; - case "comment": - $this->setComment($value); - break; - case "internalPartNumber": - $this->setInternalPartNumber($value); - break; - case "footprint": - if ($value === 0) { - $this->setFootprint(null); - } else { - try { - $footprint = Footprint::loadById($value); - $this->setFootprint($footprint); - } catch (\Exception $e) { - // No footprint was found. Ignore it. - } - } - break; - case "minStockLevel": - $this->setMinStockLevel($value); - break; - case "partUnit": - $partUnit = PartMeasurementUnit::loadById($value); - $this->setPartUnit($partUnit); - break; - case "category": - $category = PartCategory::loadById($value); - $this->setCategory($category); - break; - case "status": - $this->setStatus($value); - break; - case "storageLocation": - $storageLocation = StorageLocation::loadById($value); - $this->setStorageLocation($storageLocation); - break; - case "manufacturers": - $this->deserializeChildren($value, $this->getManufacturers(), "PartKeepr\Part\PartManufacturer"); - foreach ($this->getManufacturers() as $manufacturer) { - $manufacturer->setPart($this); - } - break; - case "distributors": - $this->deserializeChildren($value, $this->getDistributors(), "PartKeepr\Part\PartDistributor"); - foreach ($this->getDistributors() as $distributor) { - $distributor->setPart($this); - } - break; - case "parameters": - $this->deserializeChildren($value, $this->getParameters(), "PartKeepr\PartParameter\PartParameter"); - foreach ($this->getParameters() as $parameter) { - $parameter->setPart($this); - } - break; - case "needsReview": - $this->setReviewFlag($value); - break; - case "partCondition": - $this->setCondition($value); - break; - case "attachments": - $this->deserializeChildren($value, $this->getAttachments(), "PartKeepr\Part\PartAttachment"); - foreach ($this->getAttachments() as $attachment) { - $attachment->setPart($this); - } - break; - } - } - } - - /** - * Checks if the part category is set. - * - * @throws CategoryNotAssignedException - */ - private function checkCategoryConsistency () { - if ($this->getCategory() === null) { - throw new CategoryNotAssignedException($this); - } - } - - /** - * Checks if the part storage location is set. - * - * @throws StorageLocationNotAssignedException - */ - private function checkStorageLocationConsistency () { - if ($this->getStorageLocation() === null) { - throw new StorageLocationNotAssignedException(); - } - } - - /** - * Checks if the requirements for database persisting are given. - * - * @throws CategoryNotAssignedException Thrown if no category is set - * @throws StorageLocationNotAssignedException Thrown if no storage location is set - * - * @ORM\PrePersist - */ - public function onPrePersist () { - $this->checkCategoryConsistency(); - $this->checkStorageLocationConsistency(); - } - - /** - * - * Checks if the requirements for database persisting are given. - * - * For a list of exceptions, see - * @see PartKeepr\Part.Part::onPrePersist() - * - * @ORM\PreUpdate */ - public function onPreUpdate () { - $this->checkCategoryConsistency(); - $this->checkStorageLocationConsistency(); - } - - /** - * Returns a string representation of the part - * - * @param none - * @return string The name and the ID of the part - */ - public function __toString () { - return $this->getName() . " (".$this->getId().")"; - } - -} diff --git a/src/backend/PartKeepr/Part/PartAttachment.php b/src/backend/PartKeepr/Part/PartAttachment.php @@ -2,6 +2,7 @@ namespace PartKeepr\Part; use Doctrine\ORM\Mapping as ORM; +use PartKeepr\PartBundle\Entity\Part; use PartKeepr\UploadedFileBundle\Entity\UploadedFile; /** @@ -31,7 +32,7 @@ class PartAttachment extends UploadedFile /** * The part object - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="attachments") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="attachments") * * @var Part */ diff --git a/src/backend/PartKeepr/Part/PartDistributor.php b/src/backend/PartKeepr/Part/PartDistributor.php @@ -1,6 +1,7 @@ <?php namespace PartKeepr\Part; +use PartKeepr\PartBundle\Entity\Part; use PartKeepr\Util\Deserializable, PartKeepr\Util\Serializable, PartKeepr\Util\BaseEntity, @@ -13,7 +14,7 @@ use PartKeepr\Util\Deserializable, * @ORM\Entity **/ class PartDistributor extends BaseEntity implements Serializable, Deserializable { /** - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="distributors") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="distributors") */ private $part; diff --git a/src/backend/PartKeepr/Part/PartFulltextSearch.php b/src/backend/PartKeepr/Part/PartFulltextSearch.php @@ -14,7 +14,7 @@ class PartFulltextSearch extends FulltextSearch { * @see PartKeepr\FulltextSearch.FulltextSearch::getEntityName() */ protected function getEntityName () { - return 'PartKeepr\Part\Part'; + return 'PartKeepr\PartBundle\Entity\Part'; } /** diff --git a/src/backend/PartKeepr/Part/PartImage.php b/src/backend/PartKeepr/Part/PartImage.php @@ -1,6 +1,7 @@ <?php namespace PartKeepr\Part; +use PartKeepr\PartBundle\Entity\Part; use PartKeepr\Util\Serializable, PartKeepr\ImageBundle\Entity\Image, Doctrine\ORM\Mapping as ORM; @@ -12,7 +13,7 @@ use PartKeepr\Util\Serializable, class PartImage extends Image implements Serializable { /** * The part object - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="images") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="images") * @var Part */ private $part = null; diff --git a/src/backend/PartKeepr/Part/PartManager.php b/src/backend/PartKeepr/Part/PartManager.php @@ -14,7 +14,7 @@ use PartKeepr\UploadedFileBundle\Entity\TempUploadedFile, PartKeepr\Part\PartManufacturer, PartKeepr\StorageLocationBundle\Entity\StorageLocation, PartKeepr\StorageLocation\StorageLocationManager, - PartKeepr\Part\Part, + PartKeepr\PartBundle\Entity\Part, Doctrine\ORM\Query, PartKeepr\PartUnit\PartUnitManager, PartKeepr\DistributorBundle\Entity\Distributor, @@ -35,7 +35,7 @@ class PartManager extends AbstractManager { * @return string The FQCN, e.g. PartKeepr\Part */ public function getEntityName () { - return 'PartKeepr\Part\Part'; + return 'PartKeepr\PartBundle\Entity\Part'; } /** @@ -282,7 +282,7 @@ class PartManager extends AbstractManager { } - private function processParameterChanges (Part $part, Array $data) { + private function processParameterChanges (\PartKeepr\PartBundle\Entity\Part $part, Array $data) { if (array_key_exists("updates", $data)) { foreach ($data["updates"] as $record) { foreach ($part->getParameters() as $partParameter) { @@ -326,7 +326,7 @@ class PartManager extends AbstractManager { } } - private function processDistributorChanges (Part $part, Array $data) { + private function processDistributorChanges (\PartKeepr\PartBundle\Entity\Part $part, Array $data) { if (array_key_exists("updates", $data)) { foreach ($data["updates"] as $record) { foreach ($part->getDistributors() as $partDistributor) { @@ -457,7 +457,7 @@ class PartManager extends AbstractManager { * @return int The amount of parts in the database */ public function getPartCount ($withPrice = false) { - $dql = "SELECT COUNT(p.id) FROM PartKeepr\Part\Part p"; + $dql = "SELECT COUNT(p.id) FROM PartKeepr\PartBundle\Entity\Part p"; if ($withPrice === true) { $dql .= " WHERE p.averagePrice IS NOT NULL"; @@ -471,7 +471,7 @@ class PartManager extends AbstractManager { * @return float The total price */ public function getTotalPrice () { - $dql = "SELECT SUM(p.averagePrice * p.stockLevel) FROM PartKeepr\Part\Part p"; + $dql = "SELECT SUM(p.averagePrice * p.stockLevel) FROM PartKeepr\PartBundle\Entity\Part p"; return PartKeepr::getEM()->createQuery($dql)->getSingleScalarResult(); } @@ -481,7 +481,7 @@ class PartManager extends AbstractManager { * @return float The average price */ public function getAveragePrice () { - $dql = "SELECT AVG(p.averagePrice) FROM PartKeepr\Part\Part p"; + $dql = "SELECT AVG(p.averagePrice) FROM PartKeepr\PartBundle\Entity\Part p"; return PartKeepr::getEM()->createQuery($dql)->getSingleScalarResult(); } diff --git a/src/backend/PartKeepr/Part/PartManufacturer.php b/src/backend/PartKeepr/Part/PartManufacturer.php @@ -1,6 +1,7 @@ <?php namespace PartKeepr\Part; +use PartKeepr\PartBundle\Entity\Part; use PartKeepr\Util\Deserializable, PartKeepr\Util\Serializable, PartKeepr\Util\BaseEntity, @@ -11,7 +12,7 @@ use PartKeepr\Util\Deserializable, /** @ORM\Entity **/ class PartManufacturer extends BaseEntity implements Serializable, Deserializable { /** - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part",inversedBy="manufacturers") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part",inversedBy="manufacturers") */ private $part; diff --git a/src/backend/PartKeepr/Part/PartService.php b/src/backend/PartKeepr/Part/PartService.php @@ -3,6 +3,7 @@ namespace PartKeepr\Part; use PartKeepr\AuthBundle\Entity\User\User; use PartKeepr\Manager\ManagerFilter; +use PartKeepr\PartBundle\Entity\Part; use \PartKeepr\PartBundle\Entity\PartCategory; use \PartKeepr\PartBundle\Entity\PartCategoryManager; use PartKeepr\PartKeepr; diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -159,7 +159,7 @@ class PartKeepr { 'PartKeepr\Footprint\FootprintAttachment', 'PartKeepr\FootprintBundle\Entity\FootprintCategory', - 'PartKeepr\Part\Part', + 'PartKeepr\PartBundle\Entity\Part', 'PartKeepr\PartBundle\Entity\PartUnit', 'PartKeepr\Part\PartManufacturer', 'PartKeepr\Part\PartDistributor', diff --git a/src/backend/PartKeepr/PartParameter/PartParameter.php b/src/backend/PartKeepr/PartParameter/PartParameter.php @@ -4,7 +4,7 @@ namespace PartKeepr\PartParameter; use PartKeepr\PartKeepr, PartKeepr\Util\Exceptions\OutOfRangeException, PartKeepr\UnitBundle\Entity\Unit, -PartKeepr\Part\Part, +PartKeepr\PartBundle\Entity\Part, PartKeepr\SiPrefixBundle\Entity\SiPrefix, Doctrine\ORM\Mapping as ORM; @@ -24,7 +24,7 @@ class PartParameter { private $id; /** - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="parameters") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="parameters") * The part this parameter is bound to * @var Part */ @@ -136,7 +136,7 @@ class PartParameter { /** * Returns the part - * @return Part the part + * @return \PartKeepr\PartBundle\Entity\Part the part */ public function getPart () { return $this->part; diff --git a/src/backend/PartKeepr/Project/ProjectPart.php b/src/backend/PartKeepr/Project/ProjectPart.php @@ -1,7 +1,7 @@ <?php namespace PartKeepr\Project; -use PartKeepr\Part\Part, +use PartKeepr\PartBundle\Entity\Part, PartKeepr\Util\Serializable, PartKeepr\Util\Deserializable, PartKeepr\Util\BaseEntity, @@ -12,7 +12,7 @@ use PartKeepr\Part\Part, * @ORM\Entity **/ class ProjectPart extends BaseEntity implements Serializable, Deserializable { /** - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="projectParts") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="projectParts") */ private $part; @@ -36,7 +36,7 @@ class ProjectPart extends BaseEntity implements Serializable, Deserializable { /** * Sets the part which belongs to this entry - * @param Part $part + * @param \PartKeepr\PartBundle\Entity\Part $part */ public function setPart (Part $part) { $this->part = $part; @@ -44,7 +44,7 @@ class ProjectPart extends BaseEntity implements Serializable, Deserializable { /** * Returns the part which belongs to this entry - * @return Part + * @return \PartKeepr\PartBundle\Entity\Part */ public function getPart () { return $this->part; diff --git a/src/backend/PartKeepr/ProjectReport/ProjectReportService.php b/src/backend/PartKeepr/ProjectReport/ProjectReportService.php @@ -5,7 +5,7 @@ use PartKeepr\Service\RestfulService, PartKeepr\Service\Service, PartKeepr\Project\ProjectManager, PartKeepr\PartKeepr, - PartKeepr\Part\Part, + PartKeepr\PartBundle\Entity\Part, PartKeepr\Manager\ManagerFilter; class ProjectReportService extends Service implements RestfulService { diff --git a/src/backend/PartKeepr/Setup/Migration/PartDB/PartMigration.php b/src/backend/PartKeepr/Setup/Migration/PartDB/PartMigration.php @@ -2,7 +2,7 @@ namespace PartKeepr\Setup\Migration\PartDB; use PartKeepr\PartKeepr, - PartKeepr\Part\Part, + PartKeepr\PartBundle\Entity\Part, PartKeepr\Part\PartAttachment, PartKeepr\Part\PartDistributor, PartKeepr\Part\PartManager, diff --git a/src/backend/PartKeepr/Stock/StockEntry.php b/src/backend/PartKeepr/Stock/StockEntry.php @@ -4,7 +4,7 @@ namespace PartKeepr\Stock; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\HasLifecycleCallbacks; use PartKeepr\AuthBundle\Entity\User\User; -use PartKeepr\Part\Part; +use PartKeepr\PartBundle\Entity\Part; use PartKeepr\PartKeepr; use PartKeepr\Util\BaseEntity; use PartKeepr\Util\Serializable; @@ -17,7 +17,7 @@ class StockEntry extends BaseEntity implements Serializable { private $stockLevel; /** - * @ORM\ManyToOne(targetEntity="PartKeepr\Part\Part", inversedBy="stockLevels") + * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="stockLevels") */ private $part; @@ -58,7 +58,7 @@ class StockEntry extends BaseEntity implements Serializable { * were the stockLevel is the amount of items added/removed, * by which user and how much the user paid for it (for adding parts only!) * - * @param Part $part The part which was added/removed + * @param \PartKeepr\PartBundle\Entity\Part $part The part which was added/removed * @param int $stockLevel The stock level. Positive value means added parts, negative values means removed parts. * @param User $user The user who removed/added parts */