-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Makreig
committed
Nov 23, 2018
1 parent
f3f1714
commit a895c1f
Showing
7 changed files
with
142 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters