<%@ LANGUAGE="JavaScript" %>
<% /*
     $Revision$
     $Id$

    File:               deelwerk_edit.asp
    Description:        Muteerscherm voor urenverantwoording voor haltemedewerkers (stopping-place activities)
                        Merk op dat het voor zowel toevoegen als wijzigen gebruikt wordt.
    Parameters:
       urole
       prs_key          Persoonskey van de persoon vanwie de haltewerkzaamheden gemuteerd dienen te worden
       startday         Startdag kalender
    Context:
    Note:
*/ %>

<!-- #include file="../../../appl/shared/common.inc" -->

<% FCLTHeader.Set({root: "../../../appl/"}); %>

<!-- #include file="../../../appl/Shared/iface.inc" -->
<!-- #include file="./datetime.inc" -->
<!-- #include file="../../../appl/Shared/calendar.inc" -->
<!-- #include file="../../../appl/Shared/selector.inc" -->
<!-- #include file="../../../appl/Shared/persoonselector.inc" -->

<%
  // Haltewerkzaamheden CONN
  mld_deelwerk_wijzig = 4;          // Het aantal volle weken voor de huidige week dat men nog haltewerkzaamheden mag invoeren en wijzigen.
  mld_material_fac_usrtab_key = 881; // fac_usrtab_key of materials
  mld_activity_fac_usrtab_key = 882; // fac_usrtab_key of stopping-place activities
  mld_principal_fac_usrtab_key = 501; // fac_usrtab_key of principals




var  mld_disc_stoppingplace = 1001;    // discipine key stopping-places

FCLTHeader.Requires({ js: ["autocomplete.js",
                           "./deelwerk_edit.js"],
                      css: ["./deelwerk.css"],
                      lcl: ["MLD"] });

var urole = getQParamSafe("urole", "fe");
var fronto = urole == "fo";
var backo  = urole == "bo";
var minfo  = urole == "mi";
var frontend = (!fronto & !backo & !minfo);

var autfunction = "WEB_INSUSE"; // alleen plaats (PRS) afhankelijk en niet organisatie (ALG) afhankelijk
var authparams = user.checkAutorisation(autfunction, true);

var OBJUSE_read  = (authparams.ALGreadlevel < 9);
var OBJUSE_write = (authparams.ALGwritelevel < 9);
user.auth_required_or_abort(OBJUSE_read || OBJUSE_write);
var reado = true;
if (OBJUSE_write) reado = false;

var prs_key = getQParamInt('prs_key', -1);

if (prs_key == -1) prs_key = user_key;
sql = "SELECT " + S("prs_pers_string")
    + " FROM prs_v_aanwezigperslid p"
    + " WHERE p.prs_perslid_key = " + prs_key;
oRs = Oracle.Execute(sql);
var person_naam = "";
if (!oRs.eof)
  person_naam = oRs(0).value;


var d = new Date(); //today's date
var todayweek = getWeeknumber(d);
var todaymonth = d.getMonth();

// Indien 1ste week dan kan het jaar met 1 opgehoogd moeten worden. voor de laatste dagen van december
var todayyear = d.getFullYear() + (((todayweek == 1) && (todaymonth == 11))? 1 : 0);

var startday = getQParamInt("startday", d.getTime());
var sday = new Date(startday);
var startweek = getWeeknumber(sday);
var startyear = sday.getYear();

if ((startyear > todayyear) || (startyear == todayyear && startweek > todayweek))
{
  startday = d.getTime();
  startyear = todayyear;
  startweek = todayweek;
}

if (!fronto && !backo)
{
  if (startyear == todayyear)
  {
    if (todayweek - startweek > 2) reado = true;
  }
  else
  {
    if (todayyear - startyear == 1 && todayweek <= 2)
    { // als todayweek is 1 of 2 dan week 51, 52 of 53 misschien nog wijzigbaar
      nrweeks = weeksInYear(startyear);
      firstweek =  nrweeks - (2 - todayweek); // eerste week die je nog aan mag passen
      if (firstweek > startweek) reado = true;
    }
    else reado = true;
  }
}
%>

<html>
  <head>
    <%  FCLTHeader.Generate(); %>
    <script language='javascript'>
      var urole  = "<%=urole%>";
      var todayweek = <%=todayweek%>;
      var mld_deelwerk_wijzig = <%=mld_deelwerk_wijzig%>;
      var firstDow;

      $(document).ready(function ()
                        {
                          var week_date = parseInt($("#deelwerkdate").val(), 10);
                          var weekdate = new Date(week_date);
                          var weeknr = getWeeknumber(weekdate);
                          var jaar = weekdate.getYear();
                          firstDow = YWDtoDate(jaar, weeknr, 1); // Eerste dag van de week (initieel)
                        }
      );

    </script>
  </head>

  <body id="editbody">
<%  var buttons =  [ { title: L("lcl_submit"), icon:"opslaan.png", action:"deelwerk_submit();FcltMgr.resized();", id: "btnAddWeeklist" },
                     { title: L("lcl_cancel"), icon: "undo.png", action:"deelwerk_cancel()" } ];
    IFRAMER_HEADER(L("lcl_mld_splace_activities"), buttons);
%>
    <form name="u2" method="POST" action="deelwerk_materiaal_save.asp">
      <input type="hidden" name="urole" id="urole" value="<%=urole%>">
<%    BLOCK_START("mainForm", "<div id='weeklijstdiv'>" + L("lcl_mld_weeklijst") + "</div>");
      // Haltemedewerker
      FCLTpersoonselector("person",
                          "sgperson",
                          { perslidKey: prs_key,
                            label: L("lcl_mld_splace_employee"),
                            autlevel: authparams.PRSwritelevel,
                            readonly: (urole == "fe"),
                            onChange: "onChangePerson"
                          });

      // Urenstatus
      ROFIELDTR("fld", L("lcl_mld_hour_status"), L("lcl_mld_status_new"), {id: "hour_status"});

      // Deelwerk datum
      var weekdate = new Date(startday);
      var jaar = weekdate.getYear();
      var weeknr = getWeeknumber(weekdate);
      var dag = weekdate.getDay();
      var maxFuture = (weekdate.getDay() == 0? 0 : 7 - weekdate.getDay());
      FCLTcalendar("deelwerkdate",
                   { label   : L("lcl_mld_date"),
                     datum   : weekdate,
                     maxFuture: maxFuture,
                     onChange: "onChangeWeekdatum()",
                     readonly: reado,
                     required: true,
                     volgnr: 1
                   });

      // Weeknummer
      ROFIELDTR("fld", L("lcl_mld_week"), jaar + "-" + weeknr, {id: "weeknummer"});
      BLOCK_END();

      if (!reado)
      {
        BLOCK_START("deeluren", L("lcl_mld_weeklijst_uren"));
        // Activiteit
        sql = "SELECT ud.fac_usrdata_key"
            + ", ud.fac_usrdata_omschr"
            + " FROM fac_usrdata ud"
            + " WHERE ud.fac_usrtab_key = " + mld_activity_fac_usrtab_key
            + " AND ud.fac_usrdata_verwijder IS NULL"
            + " ORDER BY 2";

        FCLTselector("activity",
                     sql,
                     { label: L("lcl_activity"),
                       readonly: reado,
                       initEmpty: true, // Altijd lege waarde tonen, ook als er maar 1 waarde is.
                       required: true
                     });

        // plaats
        // JGL: TODO: Plaatsfilter met filtercode: terrein die objecten uit discipline mld_disc_stoppingplace hebben
        sql = "SELECT DISTINCT o.alg_terreinsector_key"
            + ", o.alg_plaatsaanduiding plaats"
            + ", g.ins_discipline_key"
            + " FROM ins_deel g"
            + ", ins_v_alg_overzicht o"
            + " WHERE g.ins_deel_verwijder IS NULL"
            + " AND g.ins_deel_module = 'INS'"
            + " AND g.ins_deel_parent_key IS NULL"
            + " AND o.alg_onroerendgoed_keys = g.ins_alg_ruimte_key"
            + " AND o.alg_onroerendgoed_type = g.ins_alg_ruimte_type"
            + " AND g.ins_alg_ruimte_type IN ('T')"
            + " AND o.alg_locatie_key = g.ins_alg_locatie_key"
            + " AND g.ins_discipline_key = " + mld_disc_stoppingplace
            + (authparams.ALGreadlevel >= 0
              ? " AND o.alg_locatie_key IN (SELECT alg_locatie_key FROM fac_v_my_locations "
                + " WHERE prs_perslid_key = " + user_key + " AND niveau = " + authparams.ALGreadlevel + ")"
              : "")
            + " ORDER BY 2";

        FCLTselector("place",
                     sql,
                     { label: L("lcl_mld_place"),
                       readonly: reado,
                       initEmpty: true, // Altijd lege waarde tonen, ook als er maar 1 waarde is.
                       onChange: "updateStopPlace()",
                       required: true
                     })

        // <!-- Halte -->
        FCLTselector("stopplace",
                     "",
                     { label: L("lcl_mld_stoppingplace"),
                       readonly: reado,
                       initEmpty: true, // Altijd lege waarde tonen, ook als er maar 1 waarde is.
                       required: true
                     })

        // Bestede tijd
        RWFIELDTR("spendtime",
                  "fldmlduren",
                  L("lcl_mld_time_spent"),
                  decimalToHour(0),
                  { datatype: "number",
                    maxlength: 4,
                    html: 'onChange="return updateDag(this)" onFocus="this.select()"',
                    readonly: reado,
                    required: true
                  });

        // Opdrachtgever
        sql = "SELECT ud.fac_usrdata_key"
            + ", ud.fac_usrdata_omschr"
            + " FROM fac_usrdata ud"
            + " WHERE ud.fac_usrtab_key = " + mld_principal_fac_usrtab_key
            + " AND ud.fac_usrdata_verwijder IS NULL"
            + " ORDER BY 2";

        FCLTselector("principal",
                     sql,
                     { label: L("lcl_mld_principal"),
                       readonly: reado,
                       initEmpty: true, // Altijd lege waarde tonen, ook als er maar 1 waarde is.
                       required: true
                     })

        // Km
        RWFIELDTR("km",
                  "fldkm",
                  L("lcl_mld_km"),
                  "",
                  { datatype: "number",
                    maxlength: 4,
                    html: 'onChange="return checkInputNumbers();" onKeyPress="checkKey();"',
                    readonly: reado
                  });
%>
        <!-- Opmerking -->
        <tr>
          <td class="label"><label for="opmerk"><nobr><%=L("lcl_mld_opm")%>:</nobr></label></td>
          <td>
            <textarea <%=reado? ' readonly class="disabled"' : 'class="fldremark"'%> name="opmerk" rows="3"></textarea>
          </td>
        </tr>
<%      BLOCK_END();

        BLOCK_START("deelmat", L("lcl_mld_weeklijst_materiaal"));
        // Materiaallijst
        var params = { type: "button",
                       html: "onclick='deelwerk_object()'"
                     }

        sql = "SELECT COUNT(ud.fac_usrdata_key) mataantal"
            + " FROM fac_usrdata ud"
            + " WHERE ud.fac_usrtab_key = " + mld_material_fac_usrtab_key
            + " AND ud.fac_usrdata_verwijder IS NULL";
        oRs = Oracle.Execute(sql);
        var mataantal = oRs("mataantal").value;

        RWFIELDTR("r_objs_button", "details bijlage", L("lcl_mld_material"), L("lcl_mld_materialen") + "(" + mataantal + ")", params);
%>
        <tr>
          <td>
            <input type="hidden" name="matkeystr" id="matkeystr" value="">
          </td>
          <td>
            <div id="materialen">
              &nbsp;
            </div>
          </td>
        </tr>
<%      BLOCK_END();
      } %>
    </form>
  </body>
</html>
