Skip to content

Commit

Permalink
major progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Makreig committed Nov 23, 2018
1 parent f3f1714 commit a895c1f
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 24 deletions.
10 changes: 8 additions & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Name: stockconfig
SilverCommerce\CatalogueAdmin\Model\CatalogueProduct:
extensions:
- SilverCommerce\Stock\Extensions\ProductExtension
SilverCommerce\Checkout\Control\Checkout:
SilverCommerce\OrdersAdmin\Model\LineItem:
extensions:
- SilverCommerce\Stock\Extensions\CheckoutExtension
- SilverCommerce\Stock\Extensions\LineItemExtension
SilverCommerce\OrdersAdmin\Model\Invoice:
extensions:
- SilverCommerce\Stock\Extensions\InvoiceExtension
SilverCommerce\CatalogueAdmin\Admin\CatalogueAdmin:
extensions:
- SilverCommerce\Stock\Extensions\CatalogueAdminExtension
6 changes: 4 additions & 2 deletions src/Control/StockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class StockController extends Controller
public function reduceStock($item, $quantity)
{
$item->StockLevel -= $quantity;
if ($item->StockLevel < 0) {
$item->StockLevel = 0;
if ($item->StockLevel < 0 && $this->email_alerts) {
$this->alertOversold($item);
}
$item->write();
Expand All @@ -35,13 +34,16 @@ public function reduceStock($item, $quantity)
}
}

/** ### TO DO ### **/ #################################################################
public function alertOversold($item)
{
// send email alerting stocklevel mismatch
return null;
}

public function alertLowStock($item)
{
// send email alerting low stock level
return null;
}
}
51 changes: 51 additions & 0 deletions src/Extensions/CatalogueAdminExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace SilverCommerce\Stock\Extensions;

use Product;
use SilverStripe\Core\Extension;
use SilverStripe\GridFieldAddOns\GridFieldRecordHighlighter;

class CatalogueAdminExtension extends Extension
{
public function updateEditForm($form)
{
$fields = $form->Fields();
$grid = $fields
->fieldByName($this->sanitiseClassName($this->owner->modelClass));
if ($this->owner->modelClass == Product::class && $grid) {
$config = $grid->getConfig();
$alerts = [
'isStockLow' => [
'comparator' => 'equal',
'patterns' => [
true => [
'status' => 'info',
'message' => 'This product is low on stock'
]
]
],
'StockLevel' => [
'comparator' => 'lessorequal',
'patterns' => [
0 => [
'status' => 'error',
'message' => 'This product is out of stock'
]
]
]
];
$config->addComponent(new GridFieldRecordHighlighter($alerts));
}
}
/**
* Sanitise a model class' name for inclusion in a link
*
* @param string $class
* @return string
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class);
}
}
15 changes: 0 additions & 15 deletions src/Extensions/CheckoutExtension.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/Extensions/InvoiceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace SilverCommerce\Stock\Extensions;

use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\ValidationException;

class InvoiceExtension extends DataExtension
{
public function onBeforeWrite()
{
if ($this->owner->isChanged('Status')) {
$statuses = $this->owner->config()->get("paid_statuses");
$changed = $this->owner->getChangedFields()['Status'];
$old = $changed['before'];
$new = $changed['after'];
if (!in_array($old, $statuses) && in_array($new, $statuses)) {
$items = $this->owner->Items();
foreach ($items as $item) {
$match = $item->FindStockItem();
if ($match && $match->Stocked) {
if ($item->checkStockLevel($item->Quantity) < 0) {
throw new ValidationException(_t(
"ShoppingCart.NotEnoughStock",
"There are not enough '{title}' in stock",
['title' => $match->Title]
));
}
$match->StockLevel -= $item->Quantity;
$match->write();
}
}
}
}
}
}
38 changes: 38 additions & 0 deletions src/Extensions/LineItemExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SilverCommerce\Stock\Extensions;

use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\ValidationException;
use SilverCommerce\OrdersAdmin\Model\Invoice;

class LineItemExtension extends DataExtension
{
public function onBeforeWrite()
{
$match = $this->owner->FindStockItem();
$parent = $this->owner->Parent();
if ($match && $match->Stocked && $parent instanceof Invoice && $parent->isPaid()) {
$qty = 0;
$old = 0;
if (!$this->owner->ID) {
$qty = $this->owner->Quantity;
} else if ($this->owner->isChanged('Quantity')) {
$changed = $this->owner->getChangedFields()['Quantity'];
$old = $changed['before'];
$new = $changed['after'];
$qty = $new - $old;
}
if ($qty > 0 && $this->owner->checkStockLevel($qty) < 0) {
$this->owner->Quantity = $old + $match->StockLevel;
throw new ValidationException(_t(
"ShoppingCart.NotEnoughStock",
"There are not enough '{title}' in stock",
['title' => $match->Title]
));
}
$match->StockLevel -= $qty;
$match->write();
}
}
}
10 changes: 5 additions & 5 deletions src/Extensions/ProductExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function updateCMSFields(FieldList $fields)
*/
public function isStockLow()
{
if ($this->Stocked) {
return $stock = $this->StockLevel < $this->LowStock ? true : false;
if ($this->owner->Stocked) {
return $stock = $this->owner->StockLevel < $this->owner->LowStock ? true : false;
}
return true;
return false;
}

/**
Expand All @@ -55,8 +55,8 @@ public function isStockLow()
*/
public function checkStockLevel($qty = 0)
{
if ($this->Stocked) {
return $this->StockLevel - $qty;
if ($this->owner->Stocked) {
return $this->owner->StockLevel - $qty;
}
return true;
}
Expand Down

0 comments on commit a895c1f

Please sign in to comment.