<![CDATA[
/*
    $Revision$
    $Id$

    File:       Unit4Facilitor.wsf
    Calling:    cscript Unit4Facilitor.wsf
    Parameters:
    Context:
    Note:
 */
 ]]>

<job id="IncludeGenExport">
  <script language="JScript" src="config.js"/>
  <script language="JScript" src="json2.js"/>
  <script language="JScript" src="tools.js"/>
  <script language="JScript" src="facilitor.js"/>
  <script language="JScript" src="unit4.js"/>
  <script language="JScript" src="unit4_SuppCust.js"/>
  <script language="JScript" src="unit4_SupplierInvoice.js"/>
  <script language="JScript" src="unit4_Order.js"/>
  <script language="JScript" src="unit4_BankAccount.js"/>
  <script language="JScript">

    unit4.init(config.unit4params);

    /* Er is in FACILITOR een fac_api UNIT4 naar appl/api/api_soap.asp met FAC_API_VIEW_JSONMAPPING
           [{"req": "Suppliers",  "node": "supplier", "view": "venr_v_u4_supplier"},
            {"req": "Customers",  "node": "customer", "view": "venr_v_u4_customer"},
            {"req": "SupplierInvoices","node": "supplierinvoice", "view": "venr_v_u4_supplierinvoice","filter":{"database":"administratie"}},
            {"req": "SupplierInvoiceLines","node": "supplierinvoiceline", "view": "venr_v_u4_supplierinvoiceline"},
            {"req": "VatTransactionLines","node": "vattransactionline", "view": "venr_v_u4_vattransactionline"},
            {"req": "Orders","node": "order", "view": "venr_v_u4_order"},
            {"req": "OrderLines","node": "orderline", "view": "venr_v_u4_orderline","filter":{"database":"administratie"}},
            {"req": "BankAccounts","node": "bankaccount", "view": "venr_v_u4_bankaccount"}
           ]

      Er is in FACILITOR een fac_api UNIT4SETSYNC naar cust/venr/unit4/api_settime.asp
    */

    // Handig tijdens testen
    // cscript Unit4Facilitor.wsf MVL00829/SupplierNVL.json
    // cscript Unit4Facilitor.wsf MVL00829/Supplier/7001354.json
    if (WScript.Arguments.length > 0)
    {
        var jresp = unit4.apiUnit4("GET", "/api/" + WScript.Arguments(0));
        WScript.Echo(JSON.stringify(jresp, null, '  '));
        WScript.Quit();
    }

    // Globals
    var database;
    function oneDatabase()
    {
        __Log("Database: " + database);
        var Suppliers = SyncSuppliers();
        //WScript.Echo(JSON.stringify(Suppliers, null, '  '));
        var Customers = SyncCustomers();
        //WScript.Echo(JSON.stringify(Customers, null, '  '));
        SyncBankAccounts(Suppliers, Customers);
        var anybadS = SyncSupplierInvoices();
        var anybadO = SyncOrders();
        return anybadS || anybadO;
    }

    __Log("===========================");
    __Log("Starting Unit4Facilitor.wsf");
    var anybad = false;
    for (var xx in config.databases)
    {
        var database = config.databases[xx];
        anybad = anybad || oneDatabase();
    }
    if (anybad)
    {
        __Log("anybad is true, not setting syncdate");
    }
    else
    {
        __Log("Setting sync-date");
        apiFACILITOR("UNIT4SETSYNC", "");
    }

    __Log("Done Unit4Facilitor.wsf");
  </script>
</job>
