<% /*
     $Revision$
     $Id$
     File:               cnt_list.inc
     Description:        Show an overview of contracts in list-form,
                         which meets with the given requirements
     Parameters:
         urole           user role (fe, fo, bo, mi)
         outputmode      behave in an output mode, print to 0 = screen, 1 = printer, 2 = excel, etc.
         showall         show all orders which meets with the given requirements
         cntsrt_key      search for contracts with the given contractsoort key (discipline key)
         cntnum          search for contracts with the given cnt number
         dienst_key      search for contracts with the given dienst_key
         uitvoerder_key  search for contracts with the given uitvoerder_key
         cnt_persoon_key search for contracts with the given cnt_persoon_key (persoon contractpartij)
         eigenaar_key    search for contracts for the selected owner (department)
         beheerder_key   search for contracts with the given beheerder key
         reg_key         search for contracts with the given regio key
         dist_key        search for contracts with the given district key
         loc_key         search for contracts with the given location key
         bld_key         search for contracts with the given building key
         objectsrt_key   search for contracts with the given objectsoort key (srtdeel key)
         ins_key         search for contracts with the given object key
         kp_key          search for contracts with the given kostenplaats key
         ks_key          search for contracts with the given kostensoort key
         mantel_key      search for contracts with the given mantel key
         descript        search for contracts with the given contract text in contract description
         cntNew          search for contracts which are Nieuw
         cntTgoe         search for contracts which are Ter goedkeuring
         cntAct          search for contracts which are Actueel
         cntRap          search for contracts which are In rappel
         cntOpz          search for contracts which are In opzegtermijn
         cntVerl         search for contracts which are Verlopen
         cntFutu         search for contracts which are Toekomstig
         cntInact        search for contracts which are Inactive
     Context:            Search action from cnt_search.asp form
     Note:               PF: ik heb de "mi" mode laten vervallen

*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/getkenmerksql.inc" -->
<!-- #include file="../Shared/discx3d.inc" -->
<!-- #include file="cnt.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["../cnt/cnt_list.js"]
                    });

function cnt_list (pautfunction, params)
{
  // Valid and supported parameters are:
  // params.urole          : the usuals two-letter lowercase string
  // params.outputmode     : print to 0 = screen, 1 = printer, 2 = excel, etc.
  // params.showall        : boolean (true | false)
  // params.cntsrt_key     : key
  // params.cntnum         : tekst
  // params.dienst_key     : key
  // params.uitvoerder_key : key
  // params.cnt_persoon_key: key
  // params.eigenaar_key   : key
  // params.manager_key    : key
  // params.beheerder_key  : key
  // params.reg_key        : key
  // params.dist_key       : key
  // params.loc_key        : key
  // params.bld_key        : key
  // params.objectsrt_key  : key
  // params.cntsrt_key     : key
  // params.mantel_key     : key
  // params.descript       : wildcard string
  // params.cntAct         : boolean (true | false)
  // params.cntInact       : boolean (true | false)
  // params.cntNew         : boolean (true | false)
  // params.cntTgoe        : boolean (true | false)
  // params.cntFFutu       : boolean (true | false)
  // params.cntFAct        : boolean (true | false)
  // params.cntFRap        : boolean (true | false)
  // params.cntFOpz        : boolean (true | false)
  // params.cntFVerl       : boolean (true | false)

  var authparamsCNTUSE = user.checkAutorisation("WEB_CNTUSE", true);
  var authparamsCNTMAN = user.checkAutorisation("WEB_CNTMAN", true);
  var authparamsCNTMSU = user.checkAutorisation("WEB_CNTMSU", true);

  var urole = params.urole;
  var fronto = (urole == "fo");
  var backo = (urole == "bo");
  var minfo = (urole == "mi"); // NOT APPLICABLE?
  var frontend = (urole == "fe" || (!fronto & !backo & !minfo));

  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var print = (outputmode == 1 || outputmode == 5);
  var excel = (outputmode == 2 || outputmode == 6);
  var tiny = params.tiny;
  var showall = params.showall;
%>

  <html>
    <head>
<%
      FCLTHeader.Generate({ outputmode: outputmode });
%>
      <script type="text/javascript">
        // Text strings used by cnt_list.js
        var urole = "<%=urole%>";
      </script>
    </head>
    <body id="listbody">
<%
      var tbl = new Array();
      var dts = new Array();

      var sql = cnt.getfromwherelist_sql(pautfunction, params);

      var count = 0;
      var bReado;
      var eSubmit = false;

      colors = new Array();
      colors[0] = "cntfuture";
      colors[1] = "cntactive";
      colors[2] = "cntrappel";
      colors[3] = "cntopzeg";
      colors[4] = "cntpast";
      colors[5] = "cntinactive";

      /**********************************
       * Callback functies ResultsetTable
       *********************************/
      var recent = new Date();
      recent.setMinutes(recent.getMinutes() - S("cnt_contract_recent"));
      function fnrowClass(oRs)
      {
        if (oRs("cnt_contract_status").value == 1)
          class1 = colors[5];
        else
          class1 = colors[oRs("fase_actief").value];
        if (excel || print)
          class1 = "";

        var cnt_recent = new Date(oRs("recentdatum").value);
        if (cnt_recent > recent)
        {
          class1 += " updated";
        }
        return class1;
      }

      // Extra data voor de client om daar dynamisch buttons te kunnen activeren.
      function fnrowData(oRs)
      {
        var cntNummerIntern = oRs("cnt_contract_nummer_intern").Value;
        var data = {cntNummerIntern: cntNummerIntern};

        return JSON.stringify(data);
      }
      function fnrowActionEnabler(oRs)
      {
        var data = {};
        // status 2 is 'ter goedkeuring'
        if (S("cnt_contract_approval") == 1 && oRs("cnt_contract_status").Value == 2) // Voorkennis. Voorkom dure cnt.func_enabled_contract
        {
            var this_cnt = cnt.func_enabled_contract(oRs("cnt_contract_key").Value);
            data.eForApproval = this_cnt.forApproval; // Ter goedkeuring
        }
        return data;
      }

      function fncolContractnr(oRs)
      {
        var cnt_nr = oRs("cnt_contract_nummer_intern").value;
        if (oRs("cnt_contract_versie").value)
          cnt_nr += "."+oRs("cnt_contract_versie").value;
        return safe.html(cnt_nr);
      }

      function fncolLoc(oRs)
      {
        var locatieName = oRs("alg_locatie_oms").value;
        var aantalLoc = oRs("alg_locatie_count").value;
        if (aantalLoc > 1)
          locatieName = L("lcl_cnt_locatie_m") + " (" + aantalLoc + ")";
        return safe.html(locatieName);
      }

      function fncolLocOms(oRs)
      {
        var locatieOms = oRs("alg_locatie_omschrijving").value;
        var aantalLoc = oRs("alg_locatie_count").value;
        if (aantalLoc > 1)
          locatieOms = L("lcl_cnt_locatie_m") + " (" + aantalLoc + ")";
        return safe.html(locatieOms);
      }

      function fncolLocCode(oRs)
      {
        var locatieCode = oRs("alg_locatie_code").value;
        var aantalLoc = oRs("alg_locatie_count").value;
        if (aantalLoc > 1)
          locatieCode = L("lcl_cnt_locatie_m") + " (" + aantalLoc + ")";
        return safe.html(locatieCode);
      }

      function fncolStatus(oRs)
      {
        return safe.html(cnt.getcntstatustext(oRs("cnt_contract_status").Value));
      }

      var buttons = [];
      // als tiny dan tonen we een shortcut naar contractbeheer
      // voorlasnog alleen bij relatiebeheer. Dit mag ook voor Locaties, Gebouwen en Objecten?
      if (tiny == 1 && uitvoerder_key > 0) 
      {
        var cnturl = "appl/cnt/cnt_search.asp";
        if (uitvoerder_key > 0)
          cnturl += "?comp_key=" + uitvoerder_key + "&autosearch=1"
          
        buttons.push({title: L("lcl_menu_cnt_bo"),  icon: "contract.png", action: "FcltMgr.openDetail('"+cnturl+"', '"+L("lcl_menu_cnt_bo")+"')" });
      }      
      if ((S("cnt_contract_approval") == 0 && authparamsCNTMAN && authparamsCNTMAN.ALGwritelevel < 9 && authparamsCNTMAN.PRSwritelevel < 9) ||
          (S("cnt_contract_approval") == 1 && authparamsCNTUSE && authparamsCNTUSE.ALGwritelevel < 9 && authparamsCNTUSE.PRSwritelevel < 9))
      {
        buttons.push({ icon: "page_refresh.png", title: L("lcl_refresh"), action: "FcltMgr.reload()" });
        var addurl = "appl/cnt/cnt_contract.asp?urole=bo";
        buttons.push({ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('"+addurl+"', '"+L("lcl_add")+"')" });
      }
      var rst = new ResultsetTable({ keyColumn: "cnt_contract_key",
                                     ID: "cnttable",
                                     rowClass: fnrowClass,
                                     rowData: fnrowData,
                                     rowActionEnabler: fnrowActionEnabler,
                                     sql: sql,
                                     flexModule: "CNT",
                                     flexId: "cnt_contract_key",
                                     flexGroupId: "ins_discipline_key",
                                     filterParams: params,
                                     outputmode: outputmode,
                                     title: L("lcl_cnt_contracts"),
                                     showAll: showall,
                                     buttons: buttons
                                   });

      rst.addColumn(new Column({caption: L("lcl_cnt_intern_nr"), content: fncolContractnr}));
      if (outputmode == 0)
        rst.addColumn(new Column({caption: L("lcl_location"), content: fncolLoc, hasActions: true}));
      else
      {
        rst.addColumn(new Column({caption: L("lcl_district"), content: "alg_district_omschrijving"}));
        rst.addColumn(new Column({caption: L("lcl_location"), content: fncolLocOms}));
        rst.addColumn(new Column({caption: L("lcl_estate_locatie_man_code"), content: fncolLocCode}));
      }
      rst.addColumn(new Column({caption: L("lcl_cnt_contractnr"), content: "cnt_contract_nummer"}));
      rst.addColumn(new Column({caption: L("lcl_cnt_beschrijv"), content: "cnt_contract_omschrijving"}));
      if (outputmode != 0)
      {
        rst.addColumn(new Column({caption: L("lcl_cnt_document"), content: "cnt_contract_document"}));
      }
      rst.addColumn(new Column({caption: L("lcl_cnt_effecter"), content: "uitvoerder"}));

      if (outputmode != 0)
      {
        rst.addColumn(new Column({caption: L("lcl_prs_companies_dienst"), content: "prs_dienst_omschrijving"}));
      }

      rst.addColumn(new Column({caption: L("lcl_cnt_owner"), content: "eigenaar"}));

      if (excel || print)
          rst.addColumn(new Column({caption: L("lcl_cnt_kostenplaats"), content: "kostenplaats"}));

      rst.addColumn(new Column({caption: L("lcl_cnt_status"), content: fncolStatus}));
      if (outputmode != 0)
      {
        rst.addColumn(new Column({caption: L("lcl_remark"), content: "cnt_contract_opmerking"}));
        rst.addColumn(new Column({caption: L("lcl_cnt_date_start"), content: "begindatum"}));
        rst.addColumn(new Column({caption: L("lcl_cnt_rappel_date"), content: "cnt_rappeldatum"}));
        rst.addColumn(new Column({caption: L("lcl_cnt_cancel_date"), content: "cnt_opzegdatum"}));
      }
      rst.addColumn(new Column({caption: L("lcl_cnt_enddate"), content: "einddatum"}));
      if (outputmode != 0)
      {
        rst.addColumn(new Column({caption: L("lcl_cnt_contract_sum") + "&nbsp;(" + L("lcl_valutasign") + ")", content: "cnt_contract_kosten", datatype: "currency"}));
      }

      rst.addAction({ action: "cntEdit", caption: L("lcl_edit"), isDefault: true });
      if (S("cnt_contract_approval") == 1)
        rst.addAction({ action: "cntForApproval", caption: L("lcl_cnt_forapproval"), enabler:"eForApproval", multi: true, multiOnce: true });
      // PF: ik vermoed dat de opdrachten -action niet meer nodig is, ik denk aan een opdrachten-frame in het contractdetail (cnt_contract.asp)
      //rst.addAction({ action: "cntOrders", caption: L("lcl_cnt_order"), enabler: "eOrder"});
      // TODO: IK mis hier nog opdrachten algemeen zonder dat contract_key automatisch wordt ingevuld. Is niet afhankelijk van de geselecteerde regel

      var count = rst.processResultset();
%>
    </body>
  </html>
<%
}
%>
