<%@language = "javascript" %>
<%
/*  $Revision$
    $Id$

    File:         prs_edit_bedrijf.asp
    Description:  edit bedrijf gegevens.
    Parameters:

    Context:
    Note:

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/adresselector.inc" -->
<!-- #include file="prs_flexkenmerk.inc" -->
<!-- #include file="../Shared/calendar.inc"-->
<!-- #include file="prs.inc"-->

<%
FCLTHeader.Requires({plugins:["suggest", "jQuery", "kenmerk"]})

var bedrijf_key  = getQParamInt("bedrijf_key", -1);
if (bedrijf_key > 0)
{
  var bedrijf_info = prs.prs_bedrijfinfo(bedrijf_key);  // Levert .intern als 0, 1 of 2. LET OP: in db geen 0 maar NULL!
}
else
{
  var bedrijf_info = { intern: getQParam("intern", 0),  // = 0, 1 of 2
                       uitvoertijd: null,
                       uitvoertijd_eenheid: null };
}

var isExtern = (bedrijf_info.intern == 0);
var isIntern = (bedrijf_info.intern == 1);
var isSysteem = (bedrijf_info.intern == 2);

// TODO afhankelijk maken van intern/extern/systeem
var autfunction = ["WEB_PRSMAN", "WEB_RELMAN", "WEB_RELUSE"];
var iamBedrijfContact = bedrijf_key > 0 && isExtern && bedrijf_info.uitvoerende == 1 && user.iamContact(bedrijf_key); // Ben ik contactpersoon van het externe uitvoerende bedrijf?

if (S("bgt_enabled") == 1)
  autfunction.push("WEB_BGTUSE");
var authparams_advanced = user.checkAutorisation(autfunction, true);
if (iamBedrijfContact)
  autfunction.push("WEB_EXTREL");

var authparams = user.checkAutorisation(autfunction, true);

user.auth_required_or_abort(authparams && (authparams.ALGwritelevel < 9 || authparams.PRSwritelevel < 9));

var canChangeAdvanced = authparams_advanced; // Ik heb andere rechten dan "WEB_BGTUSE" nodig.

%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script>
      async function prs_submit()
      {
          document.activeElement.blur(); // trigger laatste onChanges
          if (!await validateForm("u2"))
              return false;
          document.forms.u2.submit(); //TODO???
      }

      function prs_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }

      function onChangeUitvoertijd()
      {
          if (isNaN(parseFloat($("#bdr_uitvtijd").val())))
            $("#dayshours").prop("disabled", true);
          else
            $("#dayshours").prop("disabled", false);
      }
    </script>
  </head>

  <body class="editmode">
<%  SUBFRAME_START();
    var buttons =  [ {title: L("lcl_submit"), action:"prs_submit()", icon: "fa-fclt-save" },
                     {title: L("lcl_cancel"), action:"prs_cancel()", icon: "fa-fclt-cancel" } ];
    IFRAMER_HEADER(L("lcl_ord_company"), buttons);
%>
      <form name="u2"
            action="prs_edit_bedrijf_save.asp?bedrijf_key=<%=bedrijf_key%>"
            method="post"
            target="hidFrameSubmit"
            onSubmit="prs_submit();">
<%
        RWFIELDTR("bdr_intern", "hidden", "", bedrijf_info.intern, {}); // intern=0 wordt opgeslagen als null!

        if (!isSysteem)
        {
          BLOCK_START("bedrNaw", L("lcl_prs_bedrijf_nawblock"), {icon: "fa-address-card"});
          RWFIELDTR("bdr_naam",     "fld",          L("lcl_prs_companies_name"),             bedrijf_info.naam, {required: true, maxlength: 60});
          RWFIELDTR("bdr_pstadr",   "fld",          L("lcl_prs_companies_post_adres"),       bedrijf_info.post_adres, {maxlength: 50});
          RWFIELDTR("bdr_pstpstc",  "fld",          L("lcl_prs_companies_post_postcode"),    bedrijf_info.post_postcode, {maxlength: 12});
          RWFIELDTR("bdr_pstpl",    "fld",          L("lcl_prs_companies_post_plaats"),      bedrijf_info.post_plaats, {maxlength: 30});
          RWFIELDTR("bdr_pstland",  "fld",          L("lcl_prs_companies_post_land"),        bedrijf_info.post_land, {maxlength: 30});
          RWFIELDTR("bdr_bezadr",   "fld",          L("lcl_prs_companies_bezoek_adres"),     bedrijf_info.bezoek_adres, {maxlength: 50});
          RWFIELDTR("bdr_bezpstc",  "fld",          L("lcl_prs_companies_bezoek_postcode"),  bedrijf_info.bezoek_postcode, {maxlength: 12});
          RWFIELDTR("bdr_bezpl",    "fld",          L("lcl_prs_companies_bezoek_plaats"),    bedrijf_info.bezoek_plaats, {maxlength: 30});
          RWFIELDTR("bdr_bezland",  "fld",          L("lcl_prs_companies_bezoek_land"),      bedrijf_info.bezoek_land, {maxlength: 30});
          RWFIELDTR("xcoord",  "fldshort",          L("lcl_geoxcoord"),                      bedrijf_info.xcoord, {maxlength: 12, datatype: "float"});
          RWFIELDTR("ycoord",  "fldshort",          L("lcl_geoycoord"),                      bedrijf_info.ycoord, {maxlength: 12, datatype: "float"});
          BLOCK_END();

          BLOCK_START("bedrContact", L("lcl_prs_bedrijf_contactblock"), {icon: "fa-phone-square-alt"});
          RWFIELDTR("bdr_tel",      "fld",          L("lcl_prs_companies_telefoon"),         bedrijf_info.telefoon, {maxlength: 20});
          RWFIELDTR("bdr_fax",      "fld",          L("lcl_prs_companies_fax"),              bedrijf_info.fax, {maxlength: 20});
          RWFIELDTR("bdr_url",      "fld",          L("lcl_prs_companies_url"),              bedrijf_info.url, {maxlength: 512});
          RWFIELDTR("bdr_tel2",     "fld",          L("lcl_prs_companies_telefoon2"),        bedrijf_info.telefoon2, {maxlength: 20});
          RWFIELDTR("bdr_cntper",   "fld",          L("lcl_prs_companies_contact_person"),   bedrijf_info.contact_persoon, {maxlength: 30});
          RWFIELDTR("bdr_email",    "fld",          L("lcl_prs_companies_email"),            bedrijf_info.email, {maxlength: 100});
          RWFIELDTR("bdr_cnttel",   "fld",          L("lcl_prs_companies_contact_telefoon"), bedrijf_info.contact_telefoon, {maxlength: 20});
          RWFIELDTR("bdr_cntfax",   "fld",          L("lcl_prs_companies_contact_fax"),      bedrijf_info.contact_fax, {maxlength: 20});
          if (canChangeAdvanced) { CHECKBOXTR(L("lcl_prs_companies_ingids"), "fldalgbez", "bdr_ingids", bedrijf_info.ingids == 1); }
          BLOCK_END();

          if (canChangeAdvanced)
          {
            BLOCK_START("bedrRelatie", L("lcl_prs_bedrijf_relatieblock"), {icon: "fa-cabinet-filing"});
            sql = "SELECT prs_relatietype_key"
                + "     , " + lcl.xsqla('prs_relatietype_omschrijving', 'prs_relatietype_key')
                + "  FROM prs_relatietype"
                + " WHERE prs_relatietype_verwijder IS NULL"
                + " ORDER BY 2"
            FCLTselector("prs_reltype_key", sql,
                        { label: L("lcl_prs_relatietype"),
                          initKey: bedrijf_info.prs_relatietype_key,
                          emptyOption: "",
                          readonly: false,
                          required: false
                      });
            RWFIELDTR("bdr_levnr", "fld", L("lcl_prs_companies_leverancier_nr"), bedrijf_info.prs_leverancier_nr, {required: (S("prs_bedrijf_leveranciernr_unique")==1 ? true : false),maxlength: 50});
            CHECKBOXTR(L("lcl_prs_companies_leverancier"), "fldalgbez", "bdr_lev", bedrijf_info.leverancier == 1);
            CHECKBOXTR(L("lcl_prs_companies_uitvoerende"), "fldalgbez", "bdr_uitv", bedrijf_info.uitvoerende == 1);
            CHECKBOXTR(L("lcl_prs_companies_contract"), "fldalgbez", "bdr_cntr", bedrijf_info.contract == 1);
            CHECKBOXTR(L("lcl_prs_companies_huurder"), "fldalgbez", "bdr_huur", bedrijf_info.huurder == 1);
            RWFIELDTR("bdr_ovknr",    "fld",               L("lcl_prs_companies_overeenkomst_nr"),  bedrijf_info.prs_overeenkomst_nr, {maxlength: 30});

            FCLTcalendar( "bdr_ovkdat",
                        { label  : L("lcl_prs_companies_overeenkomst_datum"),
                          datum  : bedrijf_info.prs_overeenkomst_datum,
                          readonly: false,
                          initEmpty: !bedrijf_info.prs_overeenkomst_datum
                        });

            RWFIELDTR("bdr_uurln",    "fldshort",          L("lcl_prs_companies_uurloon"),          bedrijf_info.uurloon, {maxlength: 7});
            RWFIELDTR("bdr_mldlim",   "fldshort",          L("lcl_prs_companies_mld_limiet"),       bedrijf_info.mld_limiet, {maxlength: 10, datatype: "float"});
            RWFIELDTR("bdr_beslim",   "fldshort",          L("lcl_prs_companies_bes_limiet"),       bedrijf_info.bes_limiet, {maxlength: 10, datatype: "float"});
            RWFIELDTR("bdr_beskst",   "fldshort",          L("lcl_prs_companies_bes_kosten"),       bedrijf_info.bes_kosten, {maxlength: 10, datatype: "float"});
            RWTEXTAREATR("bdr_opm", "fldtxt", L("lcl_prs_companies_opmerking"), bedrijf_info.opmerking, {html: "maxlength='1000'"});
            RWTEXTAREATR("bdr_opm2", "fldtxt", L("lcl_prs_companies_opmerking2"), bedrijf_info.opmerking2, {html: "maxlength='1000'"});

            FCLTadresselector("mld_adrlkey", "sgAdres1",
                            {
                              label: L("lcl_prs_companies_adres_lev"),
                              adresKey:  bedrijf_info.mld_adres_key_lev,
                              emptyOption: "",
                              filtercode: "A"
                            }) ;

            FCLTadresselector("mld_adrfkey", "sgAdres2",
                            {
                              label: L("lcl_prs_companies_adres_fac"),
                              adresKey:  bedrijf_info.mld_adres_key_fac,
                              emptyOption: "",
                              filtercode: "F"
                            }) ;

            sql = "SELECT fin_btwtabel_key"
                + "     , fin_btwtabel_omschrijving"
                + "  FROM fin_btwtabel"
                + " WHERE fin_btwtabel_verwijder is NULL"
                + " ORDER BY fin_btwtabel_default DESC, 2"

            FCLTselector("finbtwtabelkey", sql,
                        { label: L("lcl_fin_btwtabel"),
                          initKey: bedrijf_info.fin_btwtabel_key,
                          emptyOption: ""
                      });
            BLOCK_END();
          }
          // Of verstoppen achter een action-button?
          if( user.checkAutorisation("WEB_PRSSYS", true) || user.checkAutorisation("WEB_PRSMGT", true))
          {
            BLOCK_START("bedrTechn", L("lcl_prs_bedrijf_techblock"), {icon: "fa-cogs"});
            RWFIELDTR("bdr_imgloc",  "fld",          L("lcl_prs_companies_image_loc"),          bedrijf_info.image_loc, {maxlength: 256});
            RWFIELDTR("bdr_detloc",  "fld",          L("lcl_prs_companies_details_loc"),        bedrijf_info.details_loc, {maxlength: 256});
            RWFIELDTR("bdr_xmlcatl", "fld",          L("lcl_prs_companies_xmlcatalogus_loc"),   bedrijf_info.xmlcatalogus_loc, {maxlength: 256});
            RWCHECKBOXTR("bdr_confirm", "fldalgbez", L("lcl_prs_companies_order_confirm"),      bedrijf_info.order_confirm == 1);
            sql = "SELECT  'U', " + safe.quoted_sql(L("lcl_mld_hours")) + " FROM DUAL"
                + " UNION SELECT  'D', " + safe.quoted_sql(L("lcl_mld_days")) + " FROM DUAL"
                + " ORDER BY 2";
            var selectparams = { fieldName: "dayshours",
                                sql: sql,
                                params: { initKey: bedrijf_info.uitvoertijd_eenheid,
                                          disable: !bedrijf_info.uitvoertijd }
                              }
            //selectparams.params.initKey = bedrijf_info.uitvoertijd_eenheid;
            RWFIELDTR("bdr_uitvtijd", "fld", L("lcl_prs_uitvoertijd"), bedrijf_info.uitvoertijd? Math.round(bedrijf_info.uitvoertijd * 100000) / 100000 : "", {maxlength: 8, datatype: "float", html: "onChange='onChangeUitvoertijd();'", selector: selectparams});
            BLOCK_END();
          }

          %><div id='prsFlex' class='prsFlex'></div><%
            generateFlexKenmerkCode ({ link_key: bedrijf_key,
                                      niveau: "B",
                                      intern: bedrijf_info.intern,
                                      reado: false
                                    });
        } 
        else // Systeembedrijf
        {
          BLOCK_START("bedrNaw", L("lcl_prs_bedrijf_sysblock"), {icon: "fa-computer"});
          RWFIELDTR("bdr_naam",     "fld",          L("lcl_prs_companies_name_sys"),  bedrijf_info.naam, {required: true, maxlength: 60});
          RWFIELDTR("bdr_cntper",   "fld",          L("lcl_prs_companies_contact_person"),   bedrijf_info.contact_persoon, {maxlength: 30});
          BLOCK_END();
        }
        IFACE.FORM_END();
%>
      </form>
      <% SUBFRAME_END(); %>
    <iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
  </body>
</html>
<% ASPPAGE_END(); %>
