/*
    $Id$
 */

// 1 enkele Order syncen
function SyncOrder(OrderId, order)
{
    var apiU4Record = "Order";
    var newData = {
                    OrderId            : OrderId,
                    ChargeVatType      : order.CHARGEVATTYPE,
                    CustomerId         : order.CUSTOMERID,
                    DeliveryAddress    : order.DELIVERYADDRESS,
                    OrderDate          : order.ORDERDATE,
                    PaymentConditionId : order.PAYMENTCONDITIONID,
                    Reference          : order.REFERENCE,
                    VatScenarioId      : order.VATSCENARIOID,
                    OrderLines         : order.lines

                };

    var url = "/api/" + database + "/" + apiU4Record + "/" + OrderId + ".json";
    var theData = unit4.apiUnit4("GET", url);
    if (theData)
    {   // Bestaande data is al opgehaald
        var method = "PUT";
    }
    else // new record
    {
        method = "POST";
        var url = "/api/" + database + "/" + apiU4Record;
        var theData = {};
    }
    for (var i in newData)
        theData[i] = newData[i];
    theData.BrokenRulesCollection = [];
    WScript.Echo("About to " + method + " " + url + "\n" + JSON.stringify(theData, null, '  '));
    //WScript.Echo(cnt + " About to " + method + " " + SupplierId + ": " + suppdata.NAME);

    return unit4.apiUnit4(method, url, theData);
}


function SyncOrders()
{
//unit4.apiUnit4("DELETE", "/api/MVL00829/Order/47610001", null);
//unit4.apiUnit4("DELETE", "/api/MVL00829/Order/47290001", null);

    // FACILITOR orders lijst ophalen
    // ==== order lines ====
    var fcltOrderLines = apiFACILITOR("UNIT4", "<GetOrderLines/>");
    if (!(fcltOrderLines.orderline instanceof Array))
        fcltOrderLines.orderline = [fcltOrderLines.orderline];
    __Log("Found " + fcltOrderLines.orderline.length + " FACILITOR OrderLines (all databases)");
    var Lines = {}; // Groeperen per order
    for (var a in fcltOrderLines.orderline)
    {
        var orderdata = fcltOrderLines.orderline[a];
        var OrderId = orderdata.ORDERID;
        if (!(OrderId in Lines))
            Lines[OrderId] = [];
        Lines[OrderId].push({   AccountId:       orderdata.ACCOUNTID,
                                Description:     orderdata.DESCRIPTION,
                                //OrderLineType: 2,
                                Price:           orderdata.PRICE,
                                PricePer:           orderdata.PRICEPER,
                                ProductId:       orderdata.PRODUCTID,
                                QuantityOrdered: orderdata.QUANTITYORDERED,
                                QuantityDelivered: orderdata.QUANTITYDELIVERED,
                                VatCodeId:       orderdata.VATCODEID
                               });
    }

    // ==== order records ====
    var fcltOrder = apiFACILITOR("UNIT4", "<GetOrders><database>" + database + "</database></GetOrders>");
    if (!(fcltOrder.order instanceof Array))
        fcltOrder.order = [fcltOrder.order];

    __Log("Found " + fcltOrder.order.length + " FACILITOR Orders for database " + database);

//    WScript.Echo(JSON.stringify(fcltOrder, null, '  '));
    var anybad = false;
    for (var a in fcltOrder.order)
    {
        var orderdata = fcltOrder.order[a];
        var OrderId = orderdata.ORDERID;
        orderdata.lines = Lines[OrderId];
        var resp = SyncOrder(orderdata.ORDERID, orderdata);
        anybad = anybad || resp.error;
    }
    return anybad;
}
