Source of file AddUpProductsToOrderPage.php
Size: 13,204 Bytes - Last Modified: 2021-12-23T10:40:35+00:00
/var/www/docs.ssmods.com/process/src/code/AddUpProductsToOrderPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 | <?php /** * @author Nicolaas [at] sunnysideup.co.nz * @package: ecommerce * @sub-package: ecommerce_delivery * @description: Shipping calculation scheme based on SimpleShippingModifier. * @TODO: use proper Buyable Field * It lets you set fixed shipping costs, or a fixed * cost for each region you're delivering to. */ class AddUpProductsToOrderPage extends Page { public static $icon = "ecommerce_club_order/images/treeicons/AddUpProductsToOrderPage"; public static $db = array( "OrderLogEntryTitle" => "Varchar", "RequestQuoteOnly" => "Boolean" ); public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab( 'Root.Content', array( new Tab( 'PreviousEntries', new LiteralField("PreviousEntries", "<p>To review previous entries, please go to the <a href=\"/admin/sales/\">sales section</a> of the CMS and search for Order Logs.</p>") ), new Tab( 'Process', new TextField("OrderLogEntryTitle", "Title to use on orders for break down per item and name (e.g. order breakdown)"), new CheckboxField("RequestQuoteOnly", "Request quote only") ) ) ); return $fields; } } class AddUpProductsToOrderPage_Controller extends Page_Controller { public function init() { parent::init(); Requirements::themedCSS("AddUpProductsToOrderPage"); Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js"); //Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js"); //Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"); Requirements::javascript(THIRDPARTY_DIR."/jquery-form/jquery.form.js"); Requirements::javascript("ecommerce_club_order/javascript/AddUpProductsToOrderPage.js"); $checkoutPage = DataObject::get_one("CheckoutPage"); Requirements::customScript("AddUpProductsToOrderPage.setCheckoutLink('".$checkoutPage->Link()."');", "setCheckoutLink"); } protected $rowNumbers = 1; /** * controller method * @return String (HTML) */ public function addrow($request) { $getVarArray = $request->getVars(); $this->rowNumbers = intval($getVarArray["rowNumbers"]); return $this->renderWith("AddProductsToOrderAjax"); } /** * controller method * resets all data */ public function reset($request) { Session::clear("AddProductsToOrderRows"); Session::save(); Director::redirectBack(); } /** *@return DOS * **/ public function AddProductsToOrderRows() { $buyables = DataObject::get("Product", "\"AllowPurchase\" = 1"); foreach ($buyables as $buyable) { if (!$buyable->canPurchase()) { $buyables->remove($buyable); } } $dos = new DataObjectSet(); $savedValuesArray = unserialize(Session::get("AddProductsToOrderRows")); $startNumber = 0; if (Director::is_ajax()) { $startNumber = $this->rowNumbers - 1; } elseif ($savedValuesArray && count($savedValuesArray)) { $this->rowNumbers = count($savedValuesArray); } Requirements::customScript("AddUpProductsToOrderPage.setRowNumbers(".$this->RowNumbers().");", "setRowNumbers"); for ($i = $startNumber ; $i < $this->rowNumbers; $i++) { if (!isset($savedValuesArray[$i])) { $savedValuesArray[$i] = array(); } if (!isset($savedValuesArray[$i]["Name"])) { $savedValuesArray[$i]["Name"]= ""; } if (!isset($savedValuesArray[$i]["Qty"])) { $savedValuesArray[$i]["Qty"]= 0; } if (!isset($savedValuesArray[$i]["BuyableClassNameAndID"])) { $savedValuesArray[$i]["BuyableClassNameAndID"]= 0; } if (!isset($savedValuesArray[$i]["Total"])) { $savedValuesArray[$i]["Total"]= 0; } $do = new DataObject(); $do->RowNumber = $i; $do->Name = $savedValuesArray[$i]["Name"]; $do->Qty = $savedValuesArray[$i]["Qty"]; $do->BuyableClassNameAndID = $savedValuesArray[$i]["BuyableClassNameAndID"]; $do->Total = $savedValuesArray[$i]["Total"]; $do->Buyables = $buyables; $dos->push($do); } return $dos; } public function submit($request) { $buyableArray = null; $this->rowNumbers = intval($_REQUEST["rowNumbers"]); $array = array(); $nameArray = array(); for ($i = 0 ; $i <= $this->rowNumbers; $i++) { if (isset($_REQUEST["buyable_$i"])) { $explodeArray = explode("_", $_REQUEST["buyable_$i"]); if (is_array($explodeArray) && count($explodeArray) == 2) { list($className, $id) = $explodeArray ; if (class_exists($className)) { $id = intval($id); if ($buyable = DataObject::get_by_id($className, $id)) { $qty = round($_REQUEST["qty_$i"], $buyable->QuantityDecimals()); if ($qty) { $buyable->Qty = 0; $name = strtoupper(Convert::raw2sql(Convert::raw2xml($_REQUEST["name_$i"]))); $buyableClassNameAndID = $className."_".$id; $price = $buyable->getCalculatedPrice(); $total = $price * $qty; $innerArray = array( "Name" => $name, "Qty" => $qty, "BuyableClassNameAndID" => $buyableClassNameAndID, "ClassName" => $className, "ID" => $id, "Buyable" => $buyable, "Price" => $price, "Total" => $total, "MyTitle" => $buyable->Title ); $array[$i] = $innerArray; $buyableArray[$buyableClassNameAndID][] = $innerArray; $nameArray[$name][$buyableClassNameAndID][] = $innerArray; } } } } } } Session::set("AddProductsToOrderRows", serialize($array)); $array = null; $innerArray = null; $buyable = null; // per BUYABLE $buyableSummaryDos = null; $buyableGrandTotal = 0; if (is_array($buyableArray) && count($buyableArray)) { $buyableSummaryDos = new DataObjectSet(); foreach ($buyableArray as $buyableClassNameAndID => $buyables) { $buyableQty = 0; $buyableTotal = 0; foreach ($buyables as $buyableEntry) { $buyableQty += $buyableEntry["Qty"]; $buyableTotal += $buyableEntry["Total"]; } $myTitle = $buyableEntry["MyTitle"]; $price = $buyableEntry["Price"]; $buyableDo = new DataObject(); $buyableDo->MyTitle = $myTitle; $buyableDo->Buyable = $buyableEntry["Buyable"]; $buyableDo->Qty = $buyableQty; $buyableDo->Price = $price; $buyableDo->PriceNice = DBField::create("Currency", $price, "PriceNice".$buyableClassNameAndID)->Nice(); $buyableDo->Total = $buyableTotal; $buyableDo->TotalNice = DBField::create("Currency", $buyableTotal, "TotalNice".$buyableClassNameAndID)->Nice(); $buyableSummaryDos->push($buyableDo); $buyableGrandTotal += $buyableTotal; } } $buyableGrandTotalNice = DBField::create("Currency", $buyableGrandTotal, "buyableGrandTotal")->Nice(); // per NAME $nameSummaryDos = null; $nameGrandTotal = 0; if (count($nameArray)) { $nameSummaryDos = new DataObjectSet(); $buyablesDos = array(); foreach ($nameArray as $name => $nameEntry) { $nameDo = new DataObject(); $nameDo->Name = $name; $nameTotal = 0; $nameQty = 0; if (count($nameEntry)) { $buyableDo = array(); $nameDo->Buyables = new DataObjectSet(); foreach ($nameEntry as $buyableClassNameAndID => $buyables) { if (count($buyables)) { $buyableQty = 0; $buyableTotal = 0; foreach ($buyables as $buyableEntry) { $buyableQty += $buyableEntry["Qty"]; $buyableTotal += $buyableEntry["Total"]; $nameQty += $buyableEntry["Qty"]; $nameTotal += $buyableEntry["Total"]; } $myTitle = $buyableEntry["MyTitle"]; $price = $buyableEntry["Price"]; $buyableDo[$buyableClassNameAndID] = new DataObject(); $buyableDo[$buyableClassNameAndID]->MyTitle = $myTitle; //$buyableDo->Buyable = $buyableEntry["Buyable"]; $buyableDo[$buyableClassNameAndID]->Qty = $buyableQty; $buyableDo[$buyableClassNameAndID]->Price = $price; $buyableDo[$buyableClassNameAndID]->PriceNice = DBField::create("Currency", $price, "PriceNice".$buyableClassNameAndID)->Nice(); $buyableDo[$buyableClassNameAndID]->Total = $buyableTotal; $buyableDo[$buyableClassNameAndID]->TotalNice = DBField::create("Currency", $buyableTotal, "TotalNice".$buyableClassNameAndID)->Nice(); $nameDo->Buyables->push($buyableDo[$buyableClassNameAndID]); $nameGrandTotal += $buyableTotal; } } } $nameDo->Qty = $nameQty; $nameDo->Total = $nameTotal; $nameDo->TotalNice = DBField::create("Currency", $nameTotal, "TotalNice".$name)->Nice(); $nameSummaryDos->push($nameDo); } } $nameGrandTotalNice = DBField::create("Currency", $nameGrandTotal, "nameGrandTotal")->Nice(); $customiseArray = array( "Message" => "", "BuyableSummary" => $buyableSummaryDos, "NameSummary" => $nameSummaryDos, "BuyableGrandTotalNice" => $buyableGrandTotalNice, "NameGrandTotalNice" => $nameGrandTotalNice ); //submit? if ((isset($_REQUEST["submit"]) && $_REQUEST["submit"]) || (isset($_REQUEST["quote"]) && $_REQUEST["quote"])) { if ($buyableSummaryDos) { $sc = ShoppingCart::singleton(); foreach ($buyableSummaryDos as $buyableDo) { $sc->addBuyable($buyableDo->Buyable, $buyableDo->Qty); } $checkoutPage = DataObject::get_one("CheckoutPage"); $html = $this->customise($customiseArray)->renderWith("AddProductsToOrderResultsAjax"); $logEntry = DataObject::get_one("AddUpProductsToOrderPageStatusLog", "OrderID = ".ShoppingCart::current_order()->ID); if (!$logEntry) { $logEntry = new AddUpProductsToOrderPageStatusLog(); $logEntry->OrderID = $sc->currentOrder()->ID; } $logEntry->Title = ($this->OrderLogEntryTitle ? $this->OrderLogEntryTitle : "Order Breakdown"); $logEntry->Note = $html; $logEntry->write(); Session::set("AddProductsToOrderRows", null); Session::save(); Session::clear("AddProductsToOrderRows"); Session::save(); $customiseArray["Message"] = "Entries updated."; } else { $customiseArray["Message"] = "No products added."; } } else { if ($buyableSummaryDos) { $customiseArray["Message"] = "Entries updated."; } else { $customiseArray["Message"] = "No products added."; } } return $this->customise($customiseArray)->renderWith("AddProductsToOrderResultsAjax"); } public function RowNumbers() { return $this->rowNumbers; } } |