<?xml version="1.0"?>
<component>
<?component error="true" debug="true"?>
<registration
	description="custfunctions"
	progid="custfunction.wsc"
	version="1.00"
	classid="{4F51571C-1DFC-4769-B8DD-37B356378F12}"
>
</registration>

<public>

<!-- properties -->
	<property name="Oracle">
		<put/>
	</property>
	<property name="__Log">
		<put/>
	</property>
	<property name="__DoLog">
		<put/>
	</property>
	<property name="safe">
		<put/>
	</property>


<!-- methods -->
	<method name="bes_punch_receive">
		<PARAMETER name="RequestForm"/>
		<PARAMETER name="bes_srtdeel_key"/>
		<PARAMETER name="item"/>
	</method>
	<method name="bes_postsave">
		<PARAMETER name="bes_bestelling_key"/>
		<PARAMETER name="presult"/>
	</method>
</public>

<script language="javascript">
<![CDATA[


/*
   $Revision$
   $Id$
*/

function put_Oracle(newValue)	{Oracle		= newValue};
function put___Log(newValue)	{__Log		= newValue};
function put___DoLog(newValue)	{__DoLog	= newValue};
function put_safe(newValue)	    {safe		= newValue};

var Oracle;
var __Log;
var __DoLog;
var safe;



// Called after bestelling has been updated
// parameters:   bes_bestelling_key just saved
// Note: bestelregels zijn ook al opgeslagen
// result: -

bes_postsave  =
function (bes_bestelling_key, presult)
{
    var result = true;
    var l_receiver_key = 0;
     
    __Log('HOOKFUNCTION START: bes_postsave');
    __Log('HOOKFUNCTION parameter bes_bestelling_key='+bes_bestelling_key);
    
    
    //sql = "select bes_bestelling_status from bes_bestelling where bes_bestelling_key = " + bes_bestelling_key;
    //oRs = Oracle.Execute(sql);
    //var l_bes_status = oRs("bes_bestelling_status").Value;
    //__Log('HOOKFUNCTION parameter l_bes_status='+l_bes_status);
    //oRs.Close();
    
    
    // selecteer de goedkeurder van bouwborden om te notificeren, maar enkel alleen de eerste, dus als er nog geen CUST01 is.
    sql= " select code, sender, p1.prs_perslid_email as sender_mail, receiver, p2.prs_perslid_email as receiver_mail, text from ALLI_V_NOTIFY_BO_BOUWBORD, prs_perslid p1, prs_perslid p2" +
         " where key = " + bes_bestelling_key +
         " and sender = p1.prs_perslid_key" +
         " and receiver =  p2.prs_perslid_key" +
         //" and fac.gettrackingdate('" + l_srtnoti_code + "'," + bes_bestelling_key + ") IS NULL";
         " and fac.gettrackingdate(code," + bes_bestelling_key + ") IS NULL";

    oRs = Oracle.Execute(sql);
    
    while (!oRs.Eof) {
        var l_srtnoti_code = oRs("code").Value;
        var l_sender_key = oRs("sender").Value;
        var l_sender_mail = oRs("sender_mail").Value;
        var l_receiver_key = oRs("receiver").Value;
        var l_receiver_mail = oRs("receiver_mail").Value;
        
        var l_omschr = "Automatische notificatie ter beoordeling gestuurd naar " + l_receiver_mail;
    
        __Log('HOOKFUNCTION parameter l_srtnoti_code='+l_srtnoti_code);
        __Log('HOOKFUNCTION parameter l_sender_key='+l_sender_key);
        __Log('HOOKFUNCTION parameter l_receiver_key='+l_receiver_key);
        __Log('HOOKFUNCTION parameter l_omschr='+l_omschr);
        
        
        if (l_receiver_key != 0){
            sql = "BEGIN fac.putnotificationsrtprio(NULL, " + l_receiver_key + ", '" + l_srtnoti_code + "', " + bes_bestelling_key + ", NULL, NULL, NULL, NULL, NULL, 2, " + (l_sender_mail ? "'" + l_sender_mail + "'" : "NULL") + "); END;"
            Oracle.Execute(sql);

            //sql = "BEGIN fac.trackaction(" + safe.quoted_sql(l_srtnoti_code) + ", " + bes_bestelling_key + ", " + l_sender_key + ", NULL, " + safe.quoted_sql(l_omschr, 2000) + "); END;"
            //Oracle.Execute(sql);
        }
        oRs.MoveNext();
    }
    oRs.Close();
    __Log('HOOKFUNCTION END: bes_postsave');
    return result;
};



bes_punch_receive = 
function (RequestForm, bes_srtdeel_key, item, pResult)
{
  var result = {orderurl: ""};
  sql = "SELECT sd.prs_bedrijf_key FROM bes_srtdeel sd WHERE sd.bes_srtdeel_key = " + bes_srtdeel_key;
  oRs = Oracle.Execute(sql);
  bedr_key = oRs("prs_bedrijf_key").value;

  function punchNVL(nm, n, def)
  {
    nm = nm.replace('#', n);
    if (RequestForm(nm).Count() == 0)
        return def;
    if (RequestForm(nm) == "")
        return def;
    return RequestForm(nm)(1);
  };

  // Alleen voor leverancier Bas Honcoop uitvoeren (prs_bedrijf_key = 936)
  if (bedr_key == 936)
  {
    // Alleen voor eerste item de leverdatum en bestellingid opvragen. Is toch voor elk item identiek.
    if  (item == 1) {
        
        // Bas Honcoop heeft een datumgegeven, die Facilitor 1-op-1 overneemt en in de bestelopdracht meestuurt:
        // Aleverdatum
        
        // Leverdatum is in formaat dd-mm-yyyy
        var leverdatum = punchNVL("NEW_ITEM-CUST_FIELD1[#]", item, "Onbekend");
        __Log('Leverdatum: CUST_FIELD1[' + item + ']= ' + leverdatum);
        if (leverdatum != "Onbekend")
        {
          sql = "SELECT TO_DATE(" + safe.quoted_sql(leverdatum) + ", 'dd-mm-yyyy') leverdatum FROM DUAL";
          oRs = Oracle.Execute(sql);
          leverdatum = oRs("leverdatum").value;
          result.orderurl = "&leverdatum=" + (new Date(leverdatum)).getTime();
        }
        // key 149 hieronder is de mld_adres_key van afleveradres '-' (in productie).
        result.orderurl = result.orderurl + "&adres_key=" + 149;
    }
    var detail_url = punchNVL("NEW_ITEM-ATTACHMENT[#]", item, "Onbekend");
    if (detail_url != "Onbekend")
    {
      sql = "UPDATE bes_srtdeel"
          + "   SET bes_srtdeel_details_loc = " + safe.quoted_sql(detail_url)
          + " WHERE bes_srtdeel_key = " + bes_srtdeel_key;
      Oracle.Execute(sql);
    }   
  }
  return result;
}


]]>
</script>
</component>