create or replace function whatsapp_goruntu_adet(psirket_kod numeric,pnakliye_kodu varchar2) return number is
xcep varchar2(100);
xadet number;
xislem_tar date;
begin 
  xadet:=0;
  select MAX(SI.CEP),max(a.islem_tar) into xcep,xislem_tar
  from nakliye_table a,MUST_ILETISIM_TABLE SI
  where a.sirket_kod=psirket_kod
  and a.nakliye_kodu=pnakliye_kodu
  and si.sirket_kod=a.sirket_kod
  and si.must_kod=a.sofor_must_kod;
  if xcep is not null then
     xcep:=replace(xcep,'(','');
     xcep:=replace(xcep,')','');
     xcep:=replace(xcep,'-','');
     xcep:=replace(xcep,' ','');
  end if;
  if xcep is not null then
    if Length(xcep) = 10 then
      xcep := '90' || xcep;
    end if;
    if Length(xcep) = 11 then
      xcep := '9' || xcep;
    end if;
   select count(*) into xadet 
   from whatsapp_mesaj_table a 
   where SIRKET_KOD=psirket_kod
   AND m_type='image'
   AND A.U_PHONE=xcep
   and a.timestamp>=xislem_tar
   AND A.ISLENDI IS NULL;
  end if;
 return(xadet);
end;
