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

     File:               CNT/cnt_show_contract.asp
     Description:        Functie bestaand contractrecord toont
     Parameters:         cnt_key              Contract key
     Context:
     Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/contactpersoonselector.inc" -->
<!-- #include file="cnt_flexkenmerk.inc" -->
<!-- #include file="cnt.inc" -->
<%
FCLTHeader.Requires({plugins:["jQuery"], js: ["date.js"]})

var cnt_key = getQParamInt("cnt_key");

var cnt_info = cnt.cnt_contract_info(cnt_key); // Nog heel beperkt
var this_cnt = cnt.func_enabled_contract(cnt_key, cnt_info); // Wat heb ik zoal aan rechten op dit contract?
user.auth_required_or_abort(this_cnt.canReadAny); // Heb ik enige lees rechten op dit contract?

if (cnt_info.locked_user_key == user_key)
  cnt.unlock(cnt_key);

var hasFOBOread = this_cnt.canFOread || this_cnt.canBOread;
var subjectMail = L("lcl_cnt_subject").format(cnt_info.nummer_intern, cnt_info.omschrijving, cnt_info.discipline_omschrijving);

var defemail_key = -1;
var defemail_type =  "";
if (cnt_info.bedrijf_key)
{
  defemail_key = cnt_info.bedrijf_key;
  defemail_type =  "B";
}
else if (cnt_info.perslid_key)
{
  defemail_key = cnt_info.perslid_key;
  defemail_type =  "P";
}
// else if (cnt_info.afdeling_key) Dan blijft defemail_key = -1. Het bestand queuemail.asp kan dan toch geen email adres bepalen.nnnnnnnnnnnnnnnnn

var urlMail = "../shared/queuemail.asp?pcode=CNTMAI&defemail_key=" + defemail_key
            + "&defemail_type=" + defemail_type
            + "&key=" + cnt_key
            + "&subject=" + Server.URLencode(subjectMail)
            + "&mailfrom=" + (hasFOBOread? 1 : 0);

%>
<html>
  <head>
<%  FCLTHeader.Generate() %>
    <script type="text/javascript">
      cnt_key = <%=cnt_key%>;
      jQuery(document).ready(function()
                             {
                               $('textarea').autogrow();
                               FcltMgr.resized(window)
                             });

      function cnt_delete()
      {
        if (confirm(L("lcl_cnt_delete").format("<%=safe.jsstring(cnt_info.nummer_intern)%>")))
        {
          var data = { cnt_key: cnt_key
                     };
          <% protectRequest.dataToken("data"); %>
          $.post("cnt_delete.asp",
                 data,
                 FcltCallbackClose,
                 "json");
        }
      }

      function cnt_change()
      {
        if (FcltMgr.startEdit(window))
          window.location.href = "cnt_edit_contract.asp?cnt_key=<%=cnt_key%>&embedded=1";
      }

      function cnt_factuurschema()
      {
        var url = "appl/cnt/cnt_factuurschema.asp?cnt_key=<%=cnt_key%>";
        FcltMgr.openDetail(url, '<%=L("lcl_cnt_factuurschema")%> ' + <%=cnt_key%>);

      }

      function cnt_newversion()
      {
        // Vraag om een ingangsdatum,
        // maak dan een kopie van dit contract (splits) waarbij de oude eindigt op die datum
        // en de nieuwe begint op die datum, en het volgende versienummer krijgt.
        // Alle flexkenmerken worden ook meegenomen, inclusief files
        FcltMgr.openModalDetail("cnt_split.asp?cnt_key=" + cnt_key, "<%=L("lcl_cnt_newversion")%>",
                                { width:500, height: 360, callback: function(data)
                                                {
                                                    if (data.refresh)
                                                        parent.FcltMgr.reload(); // Parent omdat scope bedragen gewijzigd kunnen zijn.
                                                }
                                            });
      }

      function cnt_orders()
      {
        FcltMgr.openDetail("appl/mld/mld_melding.asp?meldracht=1&urole=fe&cnt_key=" + cnt_key, L("lcl_cnt_new_order") );
      }

      function cnt_print()
      {
         window.open('./cnt_xml.asp?cnt_key='+<%=cnt_key%>);
      }

      function cnt_inactivate()
      {
        if (confirm(L("lcl_cnt_inactivate_upd").format("<%=safe.jsstring(cnt_info.nummer_intern)%>")))
        {
           var data = { cnt_key: cnt_key
                      };
           <% protectRequest.dataToken("data"); %>
           $.post("cnt_inactivate.asp",
                  data,
                  FcltCallbackRefresh,
                  "json");
        }
      }

      function cnt_approve()
      {
        if (confirm(L("lcl_cnt_approve").format("<%=safe.jsstring(cnt_info.nummer_intern)%>")))
        {
           var data = { cnt_key: cnt_key
                      };
           <% protectRequest.dataToken("data"); %>
           $.post("cnt_approve.asp",
                  data,
                  FcltCallbackRefresh,
                  "json");
        }
      }

      function cnt_reject()
      {
        url = "../cnt/cnt_reject.asp?cnt_key=" + cnt_key
        FcltMgr.openModalDetail(url,
                                L("lcl_cnt_contract") + " " + cnt_key,
                                {callback: FcltCallbackRefresh}
                               );
      }

      function cnt_forapproval()
      {
        if (confirm(L("lcl_cnt_forward_forapproval").format("<%=safe.jsstring(cnt_info.nummer_intern)%>")))
        {
           var data = { cnt_key: cnt_key
                      };
           <% protectRequest.dataToken("data"); %>
           $.post("cnt_forapproval.asp",
                  data,
                  FcltCallbackRefresh,
                  "json");
        }
      }

      function cnt_mail()
      {
        function cntMailCallback(data)
        {
          alert(L("lcl_cnt_is_cntmai") + data.emails);
        }

        var subject = "<%=safe.jsstring(subjectMail)%>";
        var url = "<%=safe.jsstring(protectQS.create(urlMail))%>";
        FcltMgr.openModalDetail(url, subject, { callback: cntMailCallback });
      }
    </script>

  </head>
  <body id="showbody">
<%  var buttons = [];
    var isLocked = (cnt_info.locked_user_key && cnt_info.locked_user_key != user_key);
    if (cnt_info.verwijderdatum == null && !isLocked)
    {
      if (this_cnt.canChange)
        buttons.push({title: L("lcl_change"),     icon: "wijzigen.png", action:"cnt_change()" });
      if (false && this_cnt.canOrder)
      { // Aanzet tot CONN#17536, maar nog rudimentair
        // TODO: heb ik MLD_ORDBOF rechten voor deze cnt_discipline en
        //       bestaan er mld_typeodrsrtdiscipline records daarvoor?
        // Of misschien beter, via meldracht: heb ik ORDUSE rechten (canSelfOrder?)
        buttons.push({title: L("lcl_shared_order"),  icon: "mld_order.png", action:"cnt_orders()" });
      }

      if (this_cnt.canSplit)
        buttons.push({title: L("lcl_cnt_newversion"), icon: "newversion.png", action:"cnt_newversion()", tooltip: L("lcl_cnt_newversion_hint")});  // CNT#16529
      if (this_cnt.canInactivate)
        buttons.push({title: L("lcl_cnt_inactivate") , action:"cnt_inactivate()", icon: "cnt_inactive.png"});
      if (this_cnt.canDelete)
          buttons.push({title: L("lcl_delete"),  icon: "delete.png", action:"cnt_delete()" });
      if (this_cnt.canGoedkeur1 || this_cnt.canGoedkeur2 || this_cnt.canGoedkeur3 || this_cnt.canGoedkeur4 || this_cnt.canGoedkeur5)
      {
        buttons.push({title: L("lcl_approve"),  icon: "accept.png", action:"cnt_approve()" });
        buttons.push({title: L("lcl_reject"),  icon: "reject.png", action:"cnt_reject()" });
      }
      if (this_cnt.forApproval)
        buttons.push({title: L("lcl_cnt_forapproval"),  icon: "forward.png", action:"cnt_forapproval()" });
    }

    buttons.push({ icon: "print.png", title: L("lcl_print"),          action: 'cnt_print()' });
    buttons.push({ icon: "email.png", title: L("lcl_noti_cnt_email"), action: 'cnt_mail()' });

    IFRAMER_HEADER(L("lcl_cnt_frame_algemeen"), buttons);
    if (cnt_info.locked_user_key && cnt_info.locked_user_key != user_key)
    {
      var other = new Perslid(cnt_info.locked_user_key);
%>    <div class="warning"><%=L("lcl_cnt_contract_locked").format(safe.html(other.naam()), toTimeString(cnt_info.locked_date))%>
        <div class="buttoncontainer" style="float:right">
<%        var buttonsM = [];
          buttonsM.push({ icon: "page_refresh.png", title: L("lcl_refresh"), action: "parent.FcltMgr.reload()" });
          CreateButtons(buttonsM, { showIcons: true }) %>
        </div>
      </div><%
    }
%>
    <div id="show">
<%    if (cnt_info.verwijderdatum != null)
      {
%>      <div class="cnt_deleted"><%=L("lcl_record_is_deleted")%></div><%
      }
      if (cnt_info.contract_status == 1) // Gesloten
      {
%>      <div class="cnt_inactive"><%=L("lcl_cnt_is_inactive")%></div><%
      }
%>
      <form name=u2 onSubmit="return false;">
<%      BLOCK_START("cntInfo", L("lcl_cnt_general_info"));
        ROFIELDTR ("fldcntnr", L("lcl_cnt_intern_nr"), cnt_info.nummer_intern);
        ROFIELDTR ("fldshort", L("lcl_cnt_versie"), cnt_info.versie, {suppressEmpty: true});
        ROFIELDTR ("flddisc", L("lcl_cnt_srttype"), cnt_info.discipline_omschrijving);
        ROFIELDTR ("fldoms", L("lcl_cnt_descr"), cnt_info.omschrijving);
        ROFIELDTR ("fldcntnr", L("lcl_cnt_contractnr"), cnt_info.nummer, {suppressEmpty: true});
        if (cnt_info.srtcontract_type != 6 && cnt_info.mantel_key != null)
        {
          params = { infoPointer: { Url:"appl/cnt/cnt_contract.asp?urole=bo&cnt_key=" + cnt_info.mantel_key,
                                    Title: L("lcl_details")
                                  }
                   }
          ROFIELDTR ("fldcontract", L("lcl_cnt_mantel"), cnt_info.mantelcontractnummer, params);
        }
        ROTEXTAREATR("fldtxt", L("lcl_cnt_document"), cnt_info.document, {suppressEmpty: true});
        ROFIELDTR ("flddep", L("lcl_cnt_owner"), cnt_info.afdeling_naam_eig);

        params = {};
        if (cnt_info.perslid_key_eig) {
            params = { infoPointer: { Url:"appl/fac/fac_user_info.asp?prs_key=" + cnt_info.perslid_key_eig,
                                    Title: L("lcl_details")
                                  }
                   }
            ROFIELDTR ("fldpers", L("lcl_cnt_contact_eig"), cnt_info.perslid_naam_eig, params);
        }

        params = {};
        if (cnt_info.perslid_key_beh) {
            params = { infoPointer: { Url:"appl/fac/fac_user_info.asp?prs_key=" + cnt_info.perslid_key_beh,
                                    Title: L("lcl_details")
                                  }
                   }
            ROFIELDTR ("fldpers", L("lcl_cnt_manager"), cnt_info.perslid_naam_beh, params);
        }
        ROTEXTAREATR("fldtxt", L("lcl_remark"), cnt_info.opmerking, {suppressEmpty: true});
        BLOCK_END();

        BLOCK_START("cntData" , L("lcl_cnt_dates"));
        var params = { infoPointer: { Url: "appl/shared/status_info.asp?cnt_key=" + cnt_key + "&urole=bo",
                                      Title: L("lcl_status_details") + " " + cnt_info.nummer_intern
                                    }
                     }
        ROFIELDTR("fld", L("lcl_cnt_status"), cnt_info.contract_status_str, params);
        ROFIELDTR ("flddate", L("lcl_cnt_date_start"), toDateString(cnt_info.looptijd_van));
        // fancy is: als deze periodes nu van toepassing zijn dat tonen (met class activeinterval)

        var fldcls = "flddate";
        if (cnt_info.looptijd_tot < new Date())
            fldcls += " activeinterval";
        ROFIELDTR (fldcls, L("lcl_cnt_date_end"), toDateString(cnt_info.looptijd_tot));

        fldcls = "fldcntperiod";
        if (cnt_info.opzegdatum <= new Date())
            fldcls += " activeinterval";
        ROFIELDTR (fldcls, L("lcl_cnt_cancel_period"), cnt_info.opzegtermijn_oms + " (" + toDateString(cnt_info.opzegdatum) + ")");

        fldcls = "fldcntperiod";
        if (cnt_info.rappeldatum <= new Date() && cnt_info.opzegdatum > new Date())
            fldcls += " activeinterval";
        ROFIELDTR (fldcls, L("lcl_cnt_rappel_period"), cnt_info.rappeltermijn_oms + " (" + toDateString(cnt_info.rappeldatum) + ")");

        // Heb ik rechten om verlengen aan te passen. Dan mag ik de waarde ook zien.
        if (cnt_info.isAbonnement)
        { // Het is een abonnement contract
          sql_verlengen = "SELECT -1,' " + L("lcl_unknown") + "', 1 FROM DUAL"
                        + " UNION SELECT " + (cnt_info.verlenging > 1? "3" : "1") + ", " + safe.qL("lcl_Yes") + ", 2 FROM DUAL"
                        + " UNION SELECT " + (cnt_info.verlenging > 1? "2" : "0") + ", " + safe.qL("lcl_No") + ", 3 FROM DUAL"
                        + " ORDER BY 3";
          FCLTselector("cntverlengen", sql_verlengen,
                        { label: L("lcl_cnt_verlengen"),
                          initKey: cnt_info.verlenging,
                          readonly: true
                       });
        }
        BLOCK_END();

        BLOCK_START("cntBedrijf" , L("lcl_cnt_effecter"));
        if (cnt_info.srtcontract_type  == S("cnt_srttype_rental"))
        {
          params = { infoPointer: { Url:"appl/prs/prs_bedrijf.asp?bedrijf_key=" + cnt_info.bedrijf_key,
                                    Title: L("lcl_details")
                                  },
                     suppressEmpty: true
                   }
          ROFIELDTR ("fld", L("lcl_cnt_mutatie_huurder"), cnt_info.bedrijf_naam, params);
          ROFIELDTR ("flddep", L("lcl_cnt_afdeling"), cnt_info.afdeling_naam, {suppressEmpty: true});
        }
        else
        {
          params = { infoPointer: { Url:"appl/prs/prs_bedrijf.asp?bedrijf_key=" + cnt_info.bedrijf_key,
                                    Title: L("lcl_details")
                                  },
                     suppressEmpty: true
                   }
          ROFIELDTR ("fld", L("lcl_cnt_company"), cnt_info.bedrijf_naam, params);

          params = {};
          if (cnt_info.contactpersoon_key != null) {
              params = { infoPointer: { Url: "appl/prs/prs_contactpersoon.asp?cps_key=" + cnt_info.contactpersoon_key,
                                        Title: L("lcl_details")
                                      }};

              lsql = "SELECT " + S("prs_contactpers_string") + " naam"
                  + " FROM prs_contactpersoon cp"
                  + " WHERE cp.prs_contactpersoon_key = " + cnt_info.contactpersoon_key;
              var loRs = Oracle.Execute(lsql);
              ROFIELDTR ("fld", L("lcl_cnt_contact_prs"), loRs("naam").value, params);
          }
          ROFIELDTR ("fld", L("lcl_prs_companies_dienst"), cnt_info.dienst_omschrijving, {suppressEmpty: true});
        }

        params = { infoPointer: { Url:"appl/prs/prs_perslid.asp?prs_key=" + cnt_info.perslid_key,
                                  Title: L("lcl_details")
                                },
                   suppressEmpty: true
                 }
        ROFIELDTR ("fld", L("lcl_person"), cnt_info.perslid_naam, params);
        BLOCK_END();

        // Bekijk of ik een facturatieschema heb, dat vind ik dan een vervanger voor het termijnbedrag
        // var fsql = "SELECT COUNT(*) FROM cnt_factuurschema WHERE cnt_contract_key = " + cnt_key;
        // var foRs = Oracle.Execute(fsql);
        // var hasFactuurschema = foRs(0).Value > 0;
        // foRs.close();

        var hasFactuurschema = false;

        BLOCK_START("cntFindata" , L("lcl_cnt_fin_data"));
        ROFIELDTR ("fldkp", L("lcl_cnt_kostenplaats"), cnt_info.kostenplaats_naam, {suppressEmpty: true});
        ROFIELDTR ("fldkp", L("lcl_cnt_kostensoort"), cnt_info.kostensoort_naam, {suppressEmpty: true});
        // Kies er hier voor in 1 regel te combineren, is logischer
        //ROFIELDTR ("fld", L("lcl_cnt_period_sum")+ " " + L("lcl_valutasign"), hasFactuurschema ? L("lcl_cnt_see_factuurschema") : safe.curr(cnt_info.termijnkosten), {suppressEmpty: true, datatype:"currency"});
        //ROFIELDTR ("fld", L("lcl_cnt_period_period"), cnt_info.termijntermijn_oms, {suppressEmpty: true});
        ROFIELDTR ("fld", L("lcl_cnt_period_sum")+ " " + L("lcl_valutasign"), hasFactuurschema ? L("lcl_cnt_see_factuurschema") : safe.curr(cnt_info.termijnkosten)+(cnt_info.termijntermijn_oms ? " / " + cnt_info.termijntermijn_oms : ""), {suppressEmpty: true, datatype:"currency"});
        if (cnt_info.termijnjaarfactor != -1 && cnt_info.termijnjaarfactor != 1) {  // -1 is ongedefinieer/ongeldig, 1 is triviaal, staat hierboven al
            ROFIELDTR ("fld", L("lcl_cnt_contract_year")+ " " + L("lcl_valutasign"), safe.curr(cnt_info.termijnjaarfactor * cnt_info.termijnkosten), {datatype:"currency", suppressEmpty: true});
        }
        ROFIELDTR ("fld", L("lcl_cnt_contract_sum")+ " " + L("lcl_valutasign"), safe.curr(cnt_info.kosten), {datatype:"currency", suppressEmpty: true});
        ROFIELDTR ("fldshort", L("lcl_hour_rate")+ " " + L("lcl_valutasign"), safe.curr(cnt_info.uurloon), {suppressEmpty: true, datatype:"currency"});
        ROFIELDTR ("fldshort", L("lcl_cnt_discount"), cnt_info.korting, {suppressEmpty: true, datatype:"float"});
        BLOCK_END();

        BLOCK_START("cntFlex"+(S("cnt_flexcolumns")!=1?"2":"") , L("lcl_cnt_flexblok"));
        generateFlexKenmerkCode ({cnt_key    : cnt_key,
                                  reado      : true,
                                  this_cnt   : this_cnt,
                                  flexcolumns: S("cnt_flexcolumns")
                                 });
        BLOCK_END();
%>
      </form>
    </div>
  </body>
</html>
