Skip to content

Commit

Permalink
Add the initial stock entry, fixes #534
Browse files Browse the repository at this point in the history
  • Loading branch information
Felicitus committed Dec 18, 2015
1 parent 71e542b commit cd209c7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/config/config_partkeepr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ services:
arguments: [ { groups: [ "default" ] } ]
- method: "initDenormalizationContext"
arguments:
- { groups: [ "default" ] }
- { groups: [ "default", "stock" ] }

resource.part_attachment.item_operation.custom_get_image:
class: "Dunglas\ApiBundle\Api\Operation\Operation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Ext.define('PartKeepr.PartEditor', {
fieldLabel: i18n("Stock User"),
name: 'initialStockLevelUser',
columnWidth: 0.5,
returnObject: true
});

basicEditorFields.push({
Expand Down Expand Up @@ -357,6 +358,22 @@ Ext.define('PartKeepr.PartEditor', {
this.record.setFootprint(null);
}

var initialStockLevel = this.initialStockLevel.getValue();

if (this.record.phantom && initialStockLevel > 0) {
var stockLevel = Ext.create("PartKeepr.StockBundle.Entity.StockEntry");
stockLevel.set("stockLevel", initialStockLevel);
stockLevel.setUser(this.initialStockLevelUser.getValue());

if (this.initialStockLevelPricePerItem.getValue() === true) {
stockLevel.set("price", this.initialStockLevelPrice.getValue() / initialStockLevel);
} else {
stockLevel.set("price", this.initialStockLevelPrice.getValue());
}

this.record.stockLevels().add(stockLevel);
}

},
onEditStart: function ()
{
Expand Down
4 changes: 3 additions & 1 deletion src/PartKeepr/PartBundle/Action/AddStockAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function __invoke(Request $request, $id)
$quantity = $request->request->get("quantity");
$user = $this->userService->getUser();

$stock = new StockEntry(intval($quantity), $user);
$stock = new StockEntry();
$stock->setUser($user);
$stock->setStockLevel(intval($quantity));

if ($request->request->has("price") && $request->request->get("price") !== null) {
$stock->setPrice(floatval($request->request->get("price")));
Expand Down
4 changes: 3 additions & 1 deletion src/PartKeepr/PartBundle/Action/RemoveStockAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function __invoke(Request $request, $id)
$quantity = $request->request->get("quantity");
$user = $this->userService->getUser();

$stock = new StockEntry(0 - intval($quantity), $user);
$stock = new StockEntry();
$stock->setStockLevel(0 - intval($quantity));
$stock->setUser($user);

if ($request->request->has("comment") && $request->request->get("comment") !== null) {
$stock->setComment($request->request->get("comment"));
Expand Down
4 changes: 3 additions & 1 deletion src/PartKeepr/PartBundle/Action/SetStockAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public function __invoke(Request $request, $id)

if ($correctionQuantity != 0) {

$stock = new StockEntry($correctionQuantity, $user);
$stock = new StockEntry();
$stock->setStockLevel($correctionQuantity);
$stock->setUser($user);

if ($request->request->has("comment") && $request->request->get("comment") !== null) {
$stock->setComment($request->request->get("comment"));
Expand Down
4 changes: 3 additions & 1 deletion src/PartKeepr/PartBundle/Controller/PartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function massRemoveStockAction(Request $request)
*/
$part = $iriConverter->getItemFromIri($removal->part);

$stock = new StockEntry(0 - intval($removal->amount), $user);
$stock = new StockEntry();
$stock->setStockLevel(0 - intval($removal->amount));
$stock->setUser($user);

if (!property_exists($removal, "comment")) {
$stock->setComment($removal->comment);
Expand Down
17 changes: 14 additions & 3 deletions src/PartKeepr/PartBundle/Entity/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Part extends BaseEntity
/**
* The stock level history
* @ORM\OneToMany(targetEntity="PartKeepr\StockBundle\Entity\StockEntry",mappedBy="part",cascade={"persist", "remove"})
* @Groups({"stock"})
*
* @var ArrayCollection
*/
Expand Down Expand Up @@ -707,11 +708,21 @@ public function setStockLevel($stockLevel)
*
* @param StockEntry $stockEntry
*/
public function addStockEntry(StockEntry $stockEntry)
public function addStockLevel(StockEntry $stockEntry)
{
$this->executeSaveListener();
$stockEntry->setPart($this);
$this->getStockLevels()->add($stockEntry);
$this->stockLevels->add($stockEntry);
}

/**
* Removes a stock entry from this part
*
* @param StockEntry $stockEntry
*/
public function removeStockLevel($stockEntry)
{
$stockEntry->setPart(null);
$this->stockLevels->removeElement($stockEntry);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/PartKeepr/StockBundle/Entity/StockEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StockEntry extends BaseEntity
private $stockLevel;

/**
* @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="stockLevels")
* @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="stockEntries")
* @Groups({"default"})
*/
private $part;
Expand All @@ -34,6 +34,7 @@ class StockEntry extends BaseEntity
private $user;

/**
* The price per item.
* @ORM\Column(type="decimal",precision=13,scale=4,nullable=true)
* @Groups({"default"})
* @var float
Expand Down Expand Up @@ -68,14 +69,9 @@ class StockEntry extends BaseEntity
* Creates a new stock entry. A stock entry tracks how many parts
* 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 int $stockLevel The stock level. Positive value means added parts, negative values means removed parts.
* @param \PartKeepr\AuthBundle\Entity\User $user The user who removed/added parts
*/
public function __construct($stockLevel, User $user = null)
public function __construct()
{
$this->setStockLevel($stockLevel);
$this->setUser($user);
$this->setDateTime(new \DateTime());
$this->setCorrection(false);
}
Expand Down

0 comments on commit cd209c7

Please sign in to comment.