Source of file BuyableStockOrderEntry.php
Size: 3,700 Bytes - Last Modified: 2021-12-23T10:41:19+00:00
/var/www/docs.ssmods.com/process/src/code/model/BuyableStockOrderEntry.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | <?php /** *@author: Nicolaas [at] Sunny Side Up . Co . Nz *@description: * keeps a record of the quantity deduction made for each sale. That is, if we sell 10 widgets in an order then an entry is made in this dataclass for * a reduction of ten widgets in the available quantity * **/ class BuyableStockOrderEntry extends DataObject { private static $db = array( "Quantity" => "Int", "IncludeInCurrentCalculation" => "Boolean" ); private static $has_one = array( "Parent" => "BuyableStockCalculatedQuantity", "Order" => "Order", ); private static $defaults = array( "IncludeInCurrentCalculation" => 1 ); //MODEL ADMIN STUFF private static $searchable_fields = array( "Quantity", "IncludeInCurrentCalculation", "ParentID", "OrderID", ); private static $field_labels = array( "Quantity" => "Calculated Quantity On Hand", "IncludeInCurrentCalculation" => "Include in Calculation", "ParentID" => "Buyable Calculation", "OrderID" => "Order" ); private static $summary_fields = array( "OrderID", "ParentID", "Quantity" ); private static $default_sort = [ 'LastEdited' => 'DESC', 'ParentID' => 'ASC', 'ID' => 'DESC' ]; private static $indexes = [ 'LastEdited' => true ]; private static $singular_name = "Stock Sale Entry"; public function i18n_singular_name() { return _t("BuyableStockOrderEntry.STOCKSALEENTRY", "Stock Sale Entry"); } private static $plural_name = "Stock Sale Entries"; public function i18n_plural_name() { return _t("BuyableStockOrderEntry.STOCKSALEENTRIES", "Stock Sale Entries"); } public function canCreate($member = null) { return false; } public function canEdit($member = null) { return false; } public function canDelete($member = null) { return false; } public function canView($member = null) { return $this->canDoAnything(); } protected function canDoAnything() { EcommerceConfig::get("EcommerceRole", "admin_permission_code"); if (!Permission::check("ADMIN") && !Permission::check($shopAdminCode)) { Security::permissionFailure($this, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.')); } return true; } public function onAfterWrite() { parent::onAfterWrite(); if ($this->ID) { //basic checks if (!$this->ParentID) { $this->delete(); user_error("Can not create record without associated buyable.", E_USER_ERROR); } if (!$this->OrderID) { $this->delete(); user_error("Can not create record without order.", E_USER_ERROR); } //make sure no duplicates are created $toBeDeleted = BuyableStockOrderEntry::get() ->filter(array('OrderID' => $this->OrderID, 'ParentID' => $this->ParentID)) ->exclude(array("ID"=> $this->ID)) ->sort(array('LastEdited' => 'ASC')); foreach ($toBeDeleted as $youAreDodo) { $youAreDodo->delete(); $youAreDodo->destroy(); user_error("deleting BuyableStockOrderEntry because there are multiples!", E_USER_ERROR); } } } } |