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);
         }
     }
 }
 /**
  * TODO: change to submitted from CustomerCanEdit criteria
  */
 protected function workoutActualQuantity()
 {
     $actualQuantity = 0;
     if ($buyable = $this->getBuyable()) {
         //set name
         //add total order quantities
         $data = DB::query("\r\n\t\t\t\tSELECT\r\n\t\t\t\t\t\"OrderItem\".\"BuyableID\",\r\n\t\t\t\t\tSum(\"OrderItem\".\"Quantity\")+0 \"QuantitySum\",\r\n\t\t\t\t\t\"Order\".\"ID\" \"OrderID\",\r\n\t\t\t\t\t\"OrderAttribute\".\"ClassName\",\r\n\t\t\t\t\t\"OrderItem\".\"BuyableClassName\",\r\n\t\t\t\t\t\"OrderStep\".\"CustomerCanEdit\"\r\n\t\t\t\tFROM\r\n\t\t\t\t\t\"Order\"\r\n\t\t\t\t\tINNER JOIN \"OrderAttribute\" ON \"OrderAttribute\".\"OrderID\" = \"Order\".\"ID\"\r\n\t\t\t\t\tINNER JOIN \"OrderItem\" ON \"OrderAttribute\".\"ID\" = \"OrderItem\".\"ID\"\r\n\t\t\t\t\tINNER JOIN \"OrderStep\" ON \"OrderStep\".\"ID\" = \"Order\".\"StatusID\"\r\n\t\t\t\tGROUP BY\r\n\t\t\t\t\t\"Order\".\"ID\", \"BuyableID\"\r\n\t\t\t\tHAVING\r\n\t\t\t\t\t\"OrderItem\".\"BuyableID\" = " . (intval($this->BuyableID) - 0) . "\r\n\t\t\t\t\tAND\r\n\t\t\t\t\t\"OrderItem\".\"BuyableClassName\" = '" . $this->BuyableClassName . "'\r\n\t\t\t\t\tAND\r\n\t\t\t\t\t\"OrderStep\".\"CustomerCanEdit\" = 0\r\n\t\t\t\t\tAND\r\n\t\t\t\t\t\"Order\".\"ID\" <> " . ShoppingCart::current_order()->ID . "\r\n\t\t\t");
         if ($data) {
             foreach ($data as $row) {
                 if ($row["OrderID"] && $this->ID && $row["QuantitySum"]) {
                     $buyableStockOrderEntry = BuyableStockOrderEntry::get()->filter(array('OrderID' => $row["OrderID"], 'ParentID' => $this->ID))->First();
                     if ($buyableStockOrderEntry) {
                         //do nothing
                     } else {
                         $buyableStockOrderEntry = new BuyableStockOrderEntry();
                         $buyableStockOrderEntry->OrderID = $row["OrderID"];
                         $buyableStockOrderEntry->ParentID = $this->ID;
                         $buyableStockOrderEntry->IncludeInCurrentCalculation = 1;
                         $buyableStockOrderEntry->Quantity = 0;
                     }
                     if ($buyableStockOrderEntry->Quantity != $row["QuantitySum"]) {
                         $buyableStockOrderEntry->Quantity = $row["QuantitySum"];
                         $buyableStockOrderEntry->write();
                     }
                 }
             }
         }
         //find last adjustment
         $latestManualUpdate = BuyableStockManualUpdate::get()->filter(array('ParentID' => $this->ID))->sort(array('LastEdited' => 'DESC'))->First();
         //nullify order quantities that were entered before last adjustment
         if ($latestManualUpdate) {
             $latestManualUpdateQuantity = $latestManualUpdate->Quantity;
             DB::query("\r\n\t\t\t\t\tUPDATE \"BuyableStockOrderEntry\"\r\n\t\t\t\t\tSET \"IncludeInCurrentCalculation\" = 0\r\n\t\t\t\t\tWHERE\r\n\t\t\t\t\t\"LastEdited\" < '" . $latestManualUpdate->LastEdited . "'\r\n\t\t\t\t\t\tAND\r\n\t\t\t\t\t\t\"ParentID\" = " . $this->ID);
         } else {
             $latestManualUpdateQuantity = 0;
         }
         //work out additional purchases
         $orderQuantityToDeduct = BuyableStockOrderEntry::get()->filter(array('ParentID' => $this->ID, 'IncludeInCurrentCalculation' => 1))->sum('Quantity');
         if (!$orderQuantityToDeduct) {
             $orderQuantityToDeduct = 0;
         }
         //work out base total
         $actualQuantity = $latestManualUpdateQuantity - $orderQuantityToDeduct;
         if (isset($_GET["debug"])) {
             echo "<hr />";
             echo $this->Name;
             echo " | Manual SUM: " . $latestManualUpdateQuantity;
             echo " | Order SUM: " . $orderQuantityToDeduct;
             echo " | Total SUM: " . $this->BaseQuantity;
             echo "<hr />";
         }
     }
     return $actualQuantity;
 }
 function history($request = null)
 {
     $id = intval($request->param("ID"));
     $buyableStockCalculatedQuantity = BuyableStockCalculatedQuantity::get()->byID($id);
     if ($buyableStockCalculatedQuantity) {
         $buyableStockCalculatedQuantity->ManualUpdates = BuyableStockManualUpdate::get()->filter(array('ParentID' => $buyableStockCalculatedQuantity->ID));
         $buyableStockCalculatedQuantity->OrderEntries = BuyableStockOrderEntry::get()->filter(array('ParentID' => $buyableStockCalculatedQuantity->ID));
         $graphArray = array();
         if ($buyableStockCalculatedQuantity->ManualUpdates) {
             foreach ($buyableStockCalculatedQuantity->ManualUpdates as $obj) {
             }
         }
         if ($buyableStockCalculatedQuantity->OrderEntries) {
             foreach ($buyableStockCalculatedQuantity->OrderEntries as $obj) {
             }
         }
         return $this->customise($buyableStockCalculatedQuantity)->renderWith("AjaxStockControlPageHistory");
     } else {
         return " could not find historical data";
     }
 }