create or replace procedure whatsapp_teslim_evrak_mesaj(psirket_kod float) is
  cursor c1 is
    select distinct a.nakliye_kodu,
                    a.sofor_must_kod,
                    a.plaka,
                    b.uzun_ad1,
                    b.uzun_ad2,
                    (SELECT MAX(SI.CEP)
                       FROM MUST_ILETISIM_TABLE SI
                      WHERE SI.SIRKET_KOD = A.SIRKET_KOD
                        AND SI.MUST_KOD = a.sofor_must_kod
                        AND SI.CEP IS NOT NULL) CEP
      from nakliye_table a
     inner join must_tanim_table b
        on (b.sirket_kod = a.sirket_kod and b.must_kod = a.sofor_must_kod)
     inner join kamyon_tanim_table c
        on (c.sirket_kod = a.sirket_kod and c.plaka = a.plaka)
     where a.sirket_kod = psirket_kod
       and a.is_tipi_secim = 1
       and a.islem_tar = trunc(sysdate)
       and a.evrak_teslim is null
       and a.sofor_must_kod is not null
       and c.sirket_arabasi='H' and c.kiralik_arac='H'
       and not exists
     (select 1
              from mesaj_gonder_table m
             where m.sirket_kod = a.sirket_kod
               and m.nakliye_kodu = a.nakliye_kodu
               and m.mesaj_konu = 'IrsaliyeBilgilendirme')
     ORDER by a.sofor_must_kod;
  kayit            c1%rowtype;
  xsoforkod        nvarchar2(20);
  xsoforcep        nvarchar2(20);
  xmesaj           nvarchar2(2000);
  xolusturma_tarih date;
  xolusturma_saat  varchar2(8);
  xmesaj_no        number(10);
  xmesaj_sayac_no  number(10);
begin
  xolusturma_tarih := trunc(sysdate);
  xolusturma_saat  := to_char(sysdate, 'hh24:mi:ss');
  xmesaj           := '';
  xsoforkod        := '';
  xsoforcep        := '';
  open c1;
  loop
    fetch c1
      into kayit;
    exit when(c1%notfound);
    if kayit.cep is not null then
      select mesaj_sayac_no
        into xmesaj_sayac_no
        from sirket_tanim_table a
       where a.sirket_kod = psirket_kod;
      select MAX_NO into xmesaj_no
        FROM SAYAC_TABLE
       WHERE SIRKET_KOD = psirket_kod
         AND SAYAC_NO = xmesaj_sayac_no
         FOR UPDATE;
      UPDATE SAYAC_TABLE
         SET MAX_NO = MAX_NO + 1
       WHERE SIRKET_KOD = psirket_kod
         AND SAYAC_NO = xmesaj_sayac_no;
    
      xsoforkod := kayit.sofor_must_kod;
      xsoforcep := kayit.cep;
      xmesaj    :='Say n *'|| kayit.uzun_ad1 || ' ' ||kayit.uzun_ad2 ||'*,\n\n'||
      
                  '*'||kayit.plaka ||'* plakal  arac n zla yapt   n z ta  ma  demesinin h zl ca yap lmas  i in ta  maya ait irsaliyenin  slak imzal  g r nt s  ve PDF format nda faturan z  bu mesaj  cevaplayarak , Whatsapp  zerinden g nderiniz..\n'||
                  'Fatura ve irsaliyenizin bu numaraya ula madan  demenin yap lamayaca  n  hat rlat r, hay rl  yolculuklar dileriz.'||
                  '\n\n'||
                  '* ZTA  TA IMACILIK T C. LTD.  T .*';
      insert into mesaj_gonder_table
        (sirket_kod,
         mesaj_no,
         bagli_mesaj_no,
         nakliye_kodu,
         must_kod,
         gonderenci,
         alici,
         mesaj_konu,
         mesaj,
         mesaj_tipi,
         mesaj_dosya,
         olusturma_tarih,
         olusturma_saat,
         olusturma_opid,
         tur)
      VALUES
        (Psirket_kod,
         Xmesaj_no,
         Xmesaj_no,
         kayit.nakliye_kodu,
         NULL,
         'SISTEM',
         xsoforcep,
         'IrsaliyeBilgilendirme',
         xmesaj,
         'WHT',
         NULL,
         xolusturma_tarih,
         xolusturma_saat,
         'SISTEM',
         'text');
      -- xmesaj:=xmesaj||to_char(kayit.islem_tar,'dd/mm/yyyy')||' Tarihli '||substr(kayit.gonderen_ad1,1,10)||''
    end if;
  end loop;
  close c1;
  commit;
end;

