-- Script containing customer specific configuration sql statements for ARFR
-- (c) 2016 Facilitor B.V.
-- Support: +31 53 4800710

SET ECHO ON
SPOOL arfr.lst


-- Report for checking whether all drivers have entered their kilometer reading yet
CREATE OR REPLACE VIEW arfr_v_rap_odometer_complete
AS
   SELECT i.ins_deel_omschrijving hide_f_sort,
          i.ins_deel_omschrijving fclt_x_voiture,
             '<a class="details" onclick=''FcltMgr.openDetail("'
          || 'appl/ins/ins_deel.asp?urole=bo&'||'ins_key='||i.ins_deel_key
          || '")''>'
          || htf.escape_sc(i.ins_deel_omschrijving)
          || '</a>'
             html_voiture,
          i.perslid_naam fclt_x_chauffeur,
             '<a class="details" onclick=''FcltMgr.openDetail("'
          || 'appl/prs/prs_perslid.asp?key='||i.prs_perslid_key
          || '")''>'
          || htf.escape_sc(i.perslid_naam)
          || '</a>'
             html_chauffeur,
          DECODE( m.mld_melding_key, NULL, NULL,
                   '<a class="details" onclick=''FcltMgr.openDetail("'
                || 'appl/mld/mld_melding.asp?urole=bo&'||'mld_key='||m.mld_melding_key
                || '")''>'
                || htf.escape_sc( TO_CHAR(m.mld_melding_key) )
                || '</a>')
                   html_appel,
          m.kms kilometrage_nouveau,
          i.kilometrage_precedent,
          i.ins_deel_key,
          i.prs_perslid_key,
          m.mld_melding_key appel
     FROM (
      SELECT d.ins_deel_key,
             d.ins_deel_omschrijving,
             fac.safe_to_number(kd.ins_kenmerkdeel_waarde) kilometrage_precedent,
             p.prs_perslid_key,
             NVL2 (p.prs_perslid_voornaam, p.prs_perslid_voornaam || ' ', '')
             || p.prs_perslid_naam
             || NVL2 (p.prs_perslid_tussenvoegsel, ' ' || p.prs_perslid_tussenvoegsel, '')
             || NVL2 (p.prs_perslid_voorletters, ' (' || p.prs_perslid_voorletters || ')', '') perslid_naam
        FROM ins_v_aanwezigdeel d,
             ins_srtdeel sd,
             prs_perslid p,
             (SELECT * FROM ins_kenmerkdeel
               WHERE ins_kenmerkdeel_verwijder IS NULL
                 AND ins_kenmerk_key = 1) kd
       WHERE d.ins_srtdeel_key = sd.ins_srtdeel_key
         AND sd.ins_srtgroep_key = 41
         AND d.ins_alg_ruimte_type = 'P'
         AND p.prs_perslid_key = d.ins_alg_ruimte_key
         AND kd.ins_deel_key(+) = d.ins_deel_key) i
   LEFT OUTER JOIN (
      SELECT m.mld_melding_key,
             fac.safe_to_number(km.mld_kenmerkmelding_waarde) kms,
             mo.ins_deel_key
        FROM mld_melding m,
             mld_melding_object mo,
             mld_kenmerkmelding km
       WHERE m.mld_stdmelding_key = 1
         AND mo.mld_melding_key = m.mld_melding_key
         AND km.mld_melding_key = m.mld_melding_key
         AND km.mld_kenmerkmelding_verwijder IS NULL) m
   ON m.ins_deel_key = i.ins_deel_key;

-- Noti-job: ask drivers to enter kilometer reading for their car
CREATE OR REPLACE VIEW arfr_v_noti_odometer
(
   CODE,
   SENDER,
   RECEIVER,
   TEXT,
   KEY,
   XKEY
)
AS
   SELECT 'INSMAI',
          NULL,
          prs_perslid_key,
          'S''il vous plaît signaler kilométrage pour ' || fclt_x_voiture email_subject,
          ins_deel_key,
          NULL
     FROM arfr_v_rap_odometer_complete;


BEGIN adm.systrackscriptId('$Id$', 0); END;
/
BEGIN fac.registercustversion('ARFR', 1); END;
/
COMMIT;
SPOOL OFF;
