function Shipping_templates_response_content($params) { // Do controller stuff here. Prepare variables $env = Shipping::getVars(); Q_Response::addStylesheet('css/ShipmentTemplates.css'); Q_Response::addScript('js/date.js', ""); return Q::view('Shipping/content/templates.php', compact('env')); }
function Shipping_addresses_response_content($params) { // Do controller stuff here. Prepare variables $env = Shipping::getVars(); // set communityId Q_Response::addStylesheet('css/ShipmentAddresses.css'); return Q::view('Shipping/content/addresses.php', compact('env')); }
function Shipping_tracking_post() { if (empty($_REQUEST["streamName"])) { throw new Q_Exception_WrongValue(array('field' => 'streamName', 'range' => "not empty")); } $env = Shipping::getVars(); $publisherId = $env->communityId; $stream = Streams::fetchOne($publisherId, $publisherId, "Shipping/shipment/" . $_REQUEST["streamName"]); // trying to send request $carrier = new Shipping_Carrier_TNT(); //Q_Response::setSlot('tracking', $carrier->track($stream)); echo $carrier->track($stream); exit; // set shipment scheduled //Shipping::updateShipment($shipmentStream, 'scheduled'); //Q_Request::requireFields(array('streamName'), $_REQUEST, true); //$params = Q::take($_REQUEST, array("streamName")); //$stream = Streams::fetchOne($userId, $userId, $params["streamName"]); //Shipping::updateShipment($stream, "scheduled"); }
function Shipping_invoice_post() { if (!isset($_REQUEST["field"]) && empty($_REQUEST["field"])) { throw new exception("Field didn't specified!"); } if (!isset($_REQUEST["attr"])) { throw new exception("Attribute didn't specified!"); } if (!isset($_REQUEST["value"])) { throw new exception("Value didn't specified!"); } if (!isset($_REQUEST["streamName"]) || empty($_REQUEST["streamName"])) { throw new exception("Stream name didn't specified!"); } $env = Shipping::getVars(); $stream = Streams::fetchOne($env->communityId, $env->communityId, $_REQUEST["streamName"]); $field = json_decode($stream->{$_REQUEST}["field"]); if (!is_object($field)) { throw new exception("field not an object!"); } $field->{$_REQUEST}["attr"] = $_REQUEST["value"]; $stream->{$_REQUEST}["field"] = json_encode($field); $stream->save(); }
function Shipping_history_response_content($params) { // Do controller stuff here. Prepare variables // get "Shipping/shipments" stream $env = Shipping::getVars(); Q_Response::addStylesheet('css/ShipmentHistory.css'); Q_Response::addStylesheet('css/jquery.dropdown.css'); Q_Response::addScript('js/jquery.dropdown.js'); // run only once Q_Response::addScript('js/scheduled.js', ""); Q_Response::addScript('js/date.js', ""); // very first defaultState $defaultState = "pickup"; $shippingStates = array(); // get states from config $tmp = Q_Config::expect('Shipping', 'states'); foreach ($tmp as $shippingState) { // set default state if (isset($shippingState["default"]) && $shippingState["default"]) { $defaultState = $shippingState["type"]; } $shippingStates[$shippingState["type"]] = $shippingState["title"]; } // state from REQUEST have high priority, then defaultState from config $defaultState = isset($_REQUEST["type"]) ? $_REQUEST["type"] : $defaultState; // get next pickup date $streamsRes = Streams::related($env->communityId, $env->communityId, $env->shipmentsStreamName, true, array("type" => "pickup")); if (!count($streamsRes[1])) { $minDate = false; } else { $dateNow = new DateTime("now"); //$minDate = new DateTime((date("Y") + 1).'-'.date("m").'-'.date("d")); $maxDate = $minDate = false; foreach ($streamsRes[1] as $stream) { // skip stream if collectInstructions empty if (empty($stream->fields["collectInstructions"])) { continue; } $exportDate = json_decode($stream->fields["collectInstructions"]); // skip stream if $exportDate not an object if (json_last_error() !== JSON_ERROR_NONE || !is_object($exportDate)) { continue; } // if exportDate or collectTimeFrom or collectTimeTo empty - continue if (!property_exists($exportDate, "exportDate") || !property_exists($exportDate, "collectTimeFrom") || !property_exists($exportDate, "collectTimeTo")) { continue; } $startDate = DateTime::createFromFormat("Y-m-d G:i", $exportDate->exportDate . ' ' . $exportDate->collectTimeFrom); if ($startDate > $dateNow) { if (!$minDate || is_a($minDate, "DateTime") && $startDate < $minDate) { $minDate = $startDate; $maxDate = DateTime::createFromFormat("Y-m-d G:i", $exportDate->exportDate . ' ' . $exportDate->collectTimeTo); } } } if (is_a($minDate, "DateTime") && is_a($maxDate, "DateTime")) { $minDate = $minDate->format("l, j M Y") . " from " . $minDate->format("ga"); $minDate .= " to " . $maxDate->format("ga"); } else { $minDate = false; } } // collect carriers $shippingCarriers = array_keys(Q_Config::expect('Shipping', 'carriers')); $shippingCarriers = array_combine($shippingCarriers, $shippingCarriers); array_unshift($shippingCarriers, 'All'); //echo $minDate->format("l, j M Y")." from ".$minDate->format("ga")." to ".$maxDate->format("ga"); exit; return Q::view('Shipping/content/history.php', compact('env', 'shippingStates', 'defaultState', "minDate", "shippingCarriers")); }
function Shipping_scheduled_post() { $env = Shipping::getVars(); if (empty($_REQUEST["streamName"])) { throw new Q_Exception_WrongValue(array('field' => 'streamName', 'range' => "not empty")); } if (empty($_REQUEST["to"]["street"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address street', 'range' => "not empty")); } if (empty($_REQUEST["to"]["city"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address city', 'range' => "not empty")); } if (empty($_REQUEST["to"]["zipcode"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address zipcode', 'range' => "not empty")); } if (empty($_REQUEST["to"]["country"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address country', 'range' => "not empty")); } //if(empty($_REQUEST["to"]["state"])) throw new Q_Exception_WrongValue(array('field' => 'destination address state', 'range' => "not empty")); if (empty($_REQUEST["from"]["street"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address street', 'range' => "not empty")); } if (empty($_REQUEST["from"]["city"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address city', 'range' => "not empty")); } if (empty($_REQUEST["from"]["zipcode"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address zipcode', 'range' => "not empty")); } if (empty($_REQUEST["from"]["country"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address country', 'range' => "not empty")); } //if(empty($_REQUEST["from"]["state"])) throw new Q_Exception_WrongValue(array('field' => 'original address state', 'range' => "not empty")); if (empty($_REQUEST['packages'])) { throw new Q_Exception_WrongValue(array('field' => 'packages', 'range' => "not empty")); } if (empty($_REQUEST["carrier"]) || !count($_REQUEST["carrier"])) { throw new Q_Exception_WrongValue(array('field' => 'carrier', 'range' => "not empty")); } if (empty($_REQUEST["dateScheduled"])) { throw new Q_Exception_WrongValue(array('field' => 'dateScheduled', 'range' => "not empty")); } if (empty($_REQUEST["asUser"])) { $_REQUEST["asUser"] = $env->userId; } $user = Users::loggedInUser(true); //print_r($user); exit; $email = new Users_Email(); $email->address = $user->emailAddress ?: $user->emailAddressPending; $email->userId = $user->id; //$email->save(); //$shipmentStream = Shipping::shipment(); $shipmentStream = Streams::fetchOne($env->communityId, $env->communityId, $_REQUEST["streamName"]); //$shipmentStream->clearAllAttributes(); $shipmentStream->title = Q::ifset($_REQUEST, 'title', "(empty)"); $shipmentStream->userId = $env->userId; $shipmentStream->destination = json_encode($_REQUEST["to"]); $shipmentStream->origin = json_encode($_REQUEST["from"]); $shipmentStream->receiver = json_encode(Q::ifset($_REQUEST, 'receiver', new StdClass())); $shipmentStream->dateScheduled = Q::ifset($_REQUEST, 'dateScheduled', ""); // set packages $packages = Q::ifset($_REQUEST, 'packages', new StdClass()); if (!is_object($packages)) { $packages["totalWeight"] = 0; $packages["totalVolume"] = 0; $packages["totalAmount"] = 0; foreach ($packages["packages"] as $key => $package) { if (!isset($package['weight'])) { throw new Q_Exception("One of the packages doesn't have its weight set"); } $packages["totalWeight"] += $package["weight"]; $volume = round((double) $package["width"] * (double) $package["height"] * (double) $package["length"], 3); $packages["totalVolume"] += $volume; $packages["packages"][$key]["volume"] = $volume; //$totalValue = round((float)$package["value"] * (int)$package["quantity"], 2); //$packages["packages"][$key]["totalValue"] = $totalValue; //$packages["totalInvoiceValue"] += $totalValue; $packages["totalAmount"]++; } } // set products $products = Q::ifset($_REQUEST, 'products', new StdClass()); if (!is_object($products)) { $products["totalWeight"] = 0; $products["totalAmount"] = 0; $products["totalInvoiceValue"] = 0; foreach ($products["packages"] as $key => $product) { // $products["totalWeight"] += $product["weight"]; //$volume = round($product["w"] * $product["h"] * $product["l"], 3); //$packages["totalVolume"] += $volume; //$products["packages"][$key]["volume"] = $volume; $totalValue = round((double) $product["value"] * (int) $product["quantity"], 2); $products["packages"][$key]["totalValue"] = $totalValue; $products["totalInvoiceValue"] += $totalValue; $products["totalAmount"]++; } } $shipmentStream->packages = json_encode($packages); $shipmentStream->products = json_encode($products); $shipmentStream->carrier = json_encode(Shipping_Carrier::createCarrier($shipmentStream, $_REQUEST["carrier"])); $shipmentStream->invoiceOptions = json_encode(Q::ifset($_REQUEST, 'invoice', new StdClass())); $shipmentStream->collectInstructions = json_encode(Q::ifset($_REQUEST, 'collect', new StdClass())); // set template attributes $isTemplate = (bool) $_REQUEST["isTemplate"]; if ($isTemplate) { $shipmentStream->setAttribute("isTemplate", true); $shipmentStream->setAttribute("templateName", $_REQUEST["template"]["templateName"]); } else { $shipmentStream->clearAttribute("isTemplate"); $shipmentStream->clearAttribute("templateName"); } // set asUser attribute $shipmentStream->setAttribute("asUser", $_REQUEST["asUser"]); $shipmentStream->setAttribute("fromUser", $env->userId); $shipmentStream->save(); $isPickupLater = (bool) $_REQUEST["isPickupLater"]; //throw new exception("Test!"); // return if action = invoice preview if ($_REQUEST["isPreview"]) { return; } // relate to category stream "Shipping/templates" if ($isTemplate) { Shipping::shipmentToTemplate($shipmentStream); } // create carrier object if (empty($_REQUEST["carrier"]["name"])) { throw new Q_Exception_WrongValue(array('field' => 'carrier name', 'range' => "not empty")); } $carrierClassName = "Shipping_Carrier_" . $_REQUEST["carrier"]["name"]; if (!class_exists($carrierClassName)) { throw new exception("carrier class " . $carrierClassName . " don't exist!"); } $carrier = new $carrierClassName(); // trying to send shipment $carrier->ship($shipmentStream, $isPickupLater); // send shipment confirmation to user if ($email->address) { $email->sendMessage(Q_Config::get('Users', 'transactional', 'scheduled', 'subject', false), Q_Config::get('Users', 'transactional', 'scheduled', 'body', false), array('user' => $user, 'shipment' => $shipmentStream), array('html' => false)); // may throw exception if badly configured } //Q_Request::requireFields(array('streamName'), $_REQUEST, true); //$params = Q::take($_REQUEST, array("streamName")); //$stream = Streams::fetchOne($userId, $userId, $params["streamName"]); }
function Shipping_tracking_response_content($params) { // get "Shipping/shipments" stream $env = Shipping::getVars(); return Q::view('Shipping/content/scheduled.php', compact('env')); }