Example #1
0
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"]);
}
Example #2
0
function Shipping_invoice_response_content($params)
{
    $publisherId = Users::communityId();
    $streamName = 'Shipping/shipment/' . Q_Request::uri()->shipmentStreamName;
    $stream = Streams::fetchOne($publisherId, $publisherId, $streamName);
    $relation = Shipping::getShipmentRelation($stream);
    if (!$stream->testReadLevel('see')) {
        throw new Users_Exception_NotAuthorized();
    }
    $carrier = json_decode($stream->carrier) ?: new StdClass();
    // check if related invoices exists
    $invoices = Streams::related($stream->publisherId, $stream->publisherId, $stream->name, true, array("type" => "invoice"));
    $invoicesCount = count($invoices[1]);
    if ($invoicesCount) {
        $invoice = array_shift($invoices[1]);
        // for UPS fix PDF invoice to use tracking id in Waybill Number field
        if ($carrier->name == "UPS") {
            Shipping_Carrier_UPS::fixInvoice(APP_DIR . "/web/" . $invoice->content, $stream);
            exit;
        }
        header("location: " . Q_Request::baseUrl() . '/' . $invoice->content);
        exit;
    }
    //----------------------------------
    // check if airwaybill bar code exist
    $carrier->AWBBarCode = false;
    $barCodes = Streams::related($stream->publisherId, $stream->publisherId, $stream->name, true, array("type" => "AWBBarCode"));
    $barCodesCount = count($barCodes[1]);
    if ($barCodesCount) {
        $barCode = array_shift($barCodes[1]);
        $carrier->AWBBarCode = Q_Request::baseUrl() . '/' . $barCode->content;
    }
    /*
    	 $carrier = json_decode($stream->fields['carrier']);
    	if($carrier->name == "TNT"){
    		header ("Content-Type:text/xml");
    		echo $stream->fields['carrierInvoice'];
    		exit;
    	}*/
    $addressReceiver = json_decode($stream->receiver) ?: new StdClass();
    $addressReceiver->street = implode("<br>", Shipping_Carrier::addAddressLine($addressReceiver));
    $addressFrom = json_decode($stream->origin) ?: new StdClass();
    $addressFrom->street = implode("<br>", Shipping_Carrier::addAddressLine($addressFrom));
    $addressTo = json_decode($stream->destination) ?: new StdClass();
    $addressTo->street = implode("<br>", Shipping_Carrier::addAddressLine($addressTo));
    $packages = json_decode($stream->packages) ?: new StdClass();
    $products = json_decode($stream->products) ?: new StdClass();
    $products->packages = Q::ifset($products, 'packages', array());
    $products->currency = Q::ifset($products, 'currency', '');
    if (!$carrier->name) {
        throw new exception("Carrier name empty!");
    }
    if (!array_key_exists($carrier->name, Q_Config::expect('Shipping', 'carriers'))) {
        throw new exception("Unexpected carrier name " . $carrier->name . "!");
    }
    $carrier->trackingId = Q::ifset($carrier, 'trackingId', '');
    //$carrier->shipmentId = Q::ifset($carrier, 'waybillNumber', '');
    $invoiceOptions = json_decode($stream->invoiceOptions) ?: new StdClass();
    $invoiceOptions->invoiceNumber = Q::ifset($invoiceOptions, 'invoiceNumber', '');
    $invoiceOptions->purchaseOrderNo = Q::ifset($invoiceOptions, 'purchaseOrderNo', '');
    $invoiceOptions->reasonForExport = Q::ifset($invoiceOptions, 'reasonForExport', '');
    $invoiceOptions->termsOfDelivery = Q::ifset($invoiceOptions, 'termsOfDelivery', '');
    // normalize termsOfDelivery
    if ($invoiceOptions->termsOfDelivery) {
        $termsOfDelivery = Q_Config::expect('Shipping', "options", "termsOfDelivery");
        if (array_key_exists($invoiceOptions->termsOfDelivery, $termsOfDelivery)) {
            $invoiceOptions->termsOfDelivery = $termsOfDelivery[$invoiceOptions->termsOfDelivery]["name"];
        }
    }
    $invoiceOptions->ftrExemptions = Q::ifset($invoiceOptions, 'ftrExemptions', '');
    $collectInstructions = json_decode($stream->collectInstructions) ?: new StdClass();
    $collectInstructions->exportDate = Q::ifset($collectInstructions, 'exportDate', '');
    Q_Response::addStylesheet('css/ShipmentInvoice.css');
    Q_Response::addStylesheet('css/ShipmentInvoice' . $carrier->name . '.css');
    //Q_Response::addStylesheet('js/jquery-editable/jquery-editable/css/jquery-editable.css');
    //Q_Response::addScript('js/jquery-editable/jquery-editable/js/jquery-editable-poshytip.js');
    //Q_Response::addScriptLine("$.fn.editable.defaults.mode = 'inline';");
    return Q::view('Shipping/content/invoice' . $carrier->name . '.php', compact('addressFrom', 'addressTo', 'addressReceiver', 'packages', 'products', 'carrier', 'invoiceOptions', 'collectInstructions', 'streamName', 'relation'));
}