Example #1
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'));
}