devnieL
11/21/2016 - 9:59 PM

gistfile1.txt

jQuery.sap.declare("com.pecsa.dev.services.Shipments");
jQuery.sap.require("com.pecsa.dev.services.Products");
jQuery.sap.require("com.pecsa.dev.models.Shipment");
jQuery.sap.require("com.pecsa.dev.models.Product");

var toMap = function(array, key1, key2) {
    var map = {};
    for (var i in array) {
        map[array[i][key1] + "-" + array[i][key2]] = array[i];
    }
    return map;
}

com.pecsa.dev.services.Shipments = {

    /**
     * Return all the shipments
     * assigned to the transport with
     * number I_TKNUM
     */
    read: function(I_TKNUM, callback) {

        I_TKNUM = I_TKNUM || "";
        I_ZLICENCIA = window.DRIVER.ZLICENCIA || "";
        I_ZNROLIC = window.DRIVER.ZNROLIC || "";

        async.waterfall([

            function get_products_by_transport(cb) {

                console.log("get_products_by_transport");

                com.pecsa.dev.services.Products.listByTransport(I_TKNUM, function(err, products) {
                    if (err) return cb(err);
                    console.log("get_products_by_transport", products);
                    cb(null, products);
                });

            },

            function get_shipments_by_transport(products, cb) {

                console.log("get_shipments_by_transport");

                console.log("products : ", products);

                var products = toMap(products, "Matnr", "Kunnr");

                console.log("products map : ", products);

                OData.request({
                    requestUri: ROOT_ODATA_URL + "/ZGW_SMP_SHIPMENTNUM_SRV/HeaderSet?$filter=ITknum eq '" + I_TKNUM + "' and IZlicencia eq '" + I_ZLICENCIA + "' and IZnrolic eq '" + I_ZNROLIC + "' and IMndt eq '" + window.CLIENT + "'&$expand=NavItem,NavMessage",
                    method: "GET",
                    headers: com.pecsa.dev.app.getHeaders()
                }, function(data, response) {

                    console.log("Shipments.read", data, response);

                    if (data.results[0].NavMessage.results.length > 0 && data.results[0].NavMessage.results[0].Type == "E") {
                        return callback({
                            message: data.results[0].NavMessage.results[0].Message
                        });
                    }

                    var results = data.results[0].NavItem.results;

                    // Group by Vbevl

                    var shipments = {};

                    for (var i in results) {
                        // Discard materials with Prema == 0
                        if (results[i].Prema != 0) {

                            var pkey = results[i].Matnr + "-" + results[i].Kunnr;

                            var prod = new com.pecsa.dev.models.Product({
                                Lfimg: results[i].Lfimg,
                                Matnr: results[i].Matnr,
                                Meins: results[i].Meins,
                                Menge: results[i].Menge,
                                Mwsbp: results[i].Mwsbp,
                                Netwr: results[i].Netwr,
                                Prema: results[i].Prema,
                                Maktx: products[pkey] ? products[pkey].Maktx : null,
                                Tragr: products[pkey] ? products[pkey].Tragr : null,
                                //Ormng : products[pkey] ? products[pkey].Ormng : null
                                Ormng: results[i].Lfimg,
                                Vrkme: results[i].Vrkme
                            });

                            console.log("PRODUCTO : ", prod);

                            if (shipments[results[i].Vbevl]) {
                                shipments[results[i].Vbevl].Products.push(prod);
                            } else {
                                shipments[results[i].Vbevl] = new com.pecsa.dev.models.Shipment(results[i]);
                                shipments[results[i].Vbevl].Products = [];
                                shipments[results[i].Vbevl].Products.push(prod);
                            }
                        }
                    }

                    var shipmentsArray = [];

                    for (var i in shipments) {
                        shipmentsArray.push(shipments[i]);
                    }

                    return cb(null, shipmentsArray);

                }, function(error) {
                    return cb(error);
                });

            }

        ], function(err, shipments) {
            if (err) return callback(err);
            callback(null, shipments);
        });

    },

    getPaymentMethods: function(I_VBELN, callback) {

        I_VBELN = I_VBELN || "";

        async.waterfall([

            function get_payments_by_order(cb) {

                console.log("get_payments_by_order");

                OData.request({
                    requestUri: ROOT_ODATA_URL + "/ZGW_SMP_CHANGE_PMP_SRV/InputSet?$filter=IBelnr eq '' and IIdsel eq '' and IGjahr eq '' and IVbeln eq '" + I_VBELN + "' and IMndt eq '" + window.CLIENT + "'&$expand=NavDetail,NavMessage",
                    method: "GET",
                    headers: com.pecsa.dev.app.getHeaders()
                }, function(data, response) {
                    console.log("Shipments.getPaymentMethods", data, response);
                }, function(error) {
                    return cb(error);
                });
            }

        ], function(err, shipments) {
            if (err) return callback(err);
            callback(null, shipments);
        });

    }

}