function setActualQuantity($value)
 {
     if (!$this->owner->ID) {
         $this->owner->write();
     }
     //only set stock level if it differs from previous
     $shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code");
     if ($this->owner->ID) {
         if ($shopAdminCode && Permission::check($shopAdminCode)) {
             if ($value != $this->owner->getActualQuantity()) {
                 $parent = BuyableStockCalculatedQuantity::get_by_buyable($this->owner);
                 if ($parent) {
                     $member = Member::currentUser();
                     $obj = new BuyableStockManualUpdate();
                     $obj->ParentID = $parent->ID;
                     $obj->Quantity = (int) $value;
                     $obj->MemberID = $member->ID;
                     $obj->write();
                 } else {
                     user_error("Could not write BuyableStockCalculatedQuantity Object because there was no parent " . $this->owner->Title);
                 }
             }
         } else {
             user_error("Could not write BuyableStockCalculatedQuantity Object because you do not have permissions " . $this->owner->Title);
         }
     }
 }
 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";
     }
 }
 /**
  * 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;
 }