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"]); }