<?php

include_once('../inc/config1.php');
include_once('../hf.php');
ini_set('display_errors',1);

function getBetween($content,$start,$end){
        $r = explode($start, $content);
        if (isset($r[1])){
            $r = explode($end, $r[1]);
            return $r[0];
        }
        return '';
    }

/*$txt="UPDATE: Your A/c XX9922 credited with INR 11,000.00 on 11-01-19 by A/c linked to mobile no XX0665 (IMPS Ref No. 901112501177) Available bal: INR 17,49,856.60";

echo getBetween($txt,'INR','deposited');
echo "<br>";
echo getBetween($txt,'INR','on');

die;*/
function splitr($str='',$bank=''){
 $result = array();
 switch($bank){
   case "HDFC":
           $content = trim(getBetween($str,"INW",".Avl bal"));
       $parts = explode(" ",$content);
           foreach($parts as $p => $v){ 
          if(strstr($v,"@")){
             $result["conversion_rate"] = substr($v,strpos($v,"@")+1);
             $result["currency"] = preg_replace("([0-9.]+)","",substr($v,0,strpos($v,"@")));
             $val = substr($v,strlen($result["currency"]),strpos($v,"@"));
             $result["deposited_amount"] = substr($val,0,strpos($val,"@"));
          } else { 
              $result["ref_no"] = $v;
         }   
      }
      $result["deposited_in_bank_name"] = "HDFC BANK";     
   break;
   case "AXIS" :
           $content = trim(getBetween($str,"BRN-REF NO.",". Call"));
       $parts = explode(" ",$content);
           foreach($parts as $p => $v){ 
          if(strstr($v,"/")){
           $amount_1=trim(getBetween($str,'credited with Rs.','on'));
           $result["deposited_amount"] = substr($v,0,strpos($v,"/"));
           $result["conversion_rate"] = round($amount_1/$result["deposited_amount"],2);
          } else if($p == 1) { 
           $result["currency"] = $v;
        } else {  
             $result["ref_no"] = $v;
        }
      }
      $result["deposited_in_bank_name"] = "AXIS BANK";      
   break;
  
  } 
   $result["pymt_mode"] = "SWIFT TRSFR";
   return $result;
 }

$txt1='[
 {
        "smsDateTime": "12/06/2018 07:55:57.565",
        "smsMatter": "UPDATE: INR 1,41,005.12 deposited in A/c XX9922 on 19-DEC-18 for NEFT Cr-CITI0100000-PAYPAL PAYMENTS PL-OPGSP COLL AC-STEELMINT INFO SERVICES LLP-CITIN18966268505.Avl bal:INR 14,13,537.48 subject to clearing",
        "smsSenderName": "AD-HDFCBK",
        "TIMESTAMP": 1528813557565
    },
    {
        "smsDateTime": "12/06/2018 07:55:57.565",
        "smsMatter": "A/c NN34821 debited for Rs 1230.00 thru Net/Mob Bkg. A/c Bal Rs 59191.54 (incl. of uncleared chqs) as of 15 NOV 23:00hr. If not used by you, call 18002001947",
        "smsSenderName": "AD-idbi",
        "TIMESTAMP": 1528813557565
    },
    {
        "smsDateTime": "12/06/2018 07:55:57.565",
        "smsMatter": "Your IDBI Bk A/c. NN48888 credited Rs 4000.00 Det: DPS. A/c Bal(sub to chq realisation) Rs 117032.65 as of 07 JUL 13:57 hrs. Msg for idbi cash deposit.",
        "smsSenderName": "AD-idbi",
        "TIMESTAMP": 1528813557565
    },
    {
        "smsDateTime": "12/06/2018 07:55:57.565",
        "smsMatter": "UPDATE: INR 1,41,005.12 deposited in A/c XX9922 on 19-DEC-18 for NEFT Cr-CITI0100000-PAYPAL PAYMENTS PL-OPGSP COLL AC-STEELMINT INFO SERVICES LLP-CITIN18966268505.Avl bal:INR 14,13,537.48 subject to clearing",
        "smsSenderName": "AD-idbi",
        "TIMESTAMP": 1528813557565
    },
    {
        "smsDateTime": "12/06/2018 07:55:57.565",
        "smsMatter": "UPDATE: INR 11,618.73 deposited in A/c XX9922 on 29-OCT-18 for HDFC BALANCED ADVAN-HDFC Balanced Advant.Avl bal:INR 49,20,128.70 subject to clearing",
        "smsSenderName": "AD-axis",
        "TIMESTAMP": 1528813557565
    },
    {
          "smsDateTime": "12/06/2018 07:55:57.565",
          "smsMatter": "UPDATE: INR 32,851.00 deposited in A/c XX9922 on 27-NOV-18 for NEFT Cr-IBKL0NEFT01-NEW POINT MULTITRADE PVT LTD-STEEL MINT INFO SERVIC-181127i191882536.Avl bal:INR 17,26,496.97 subject to clearing",
          "smsSenderName": "AD-HDFCBK",
          "TIMESTAMP": 1528813557565
      }
 ]';

$txt1='[
    {
        "smsDateTime": "10/01/2019 07:55:57.565",
        "smsMatter": "UPDATE: Your A/c XX9922 credited with INR 11,000.00 on 11-01-19 by A/c linked to mobile no XX0665 (IMPS Ref No. 901112501177) Available bal: INR 17,49,856.60",
        "smsSenderName": "AD-HDFCBK",
        "TIMESTAMP": 1528813557565
    },
    {
          "smsDateTime": "10/01/2019 07:55:57.565",
          "smsMatter": "UPDATE: Your A/c XX9922 credited with INR 11,000.00 on 10-01-19 by A/c linked to mobile no XX0665 (IMPS Ref No. 901100755835) Available bal: INR 14,88,995.60",
          "smsSenderName": "AD-HDFCBK",
          "TIMESTAMP": 1528813557565
      },
    {
      "smsDateTime": "10/01/2019 07:55:57.565",
      "smsMatter": "UPDATE: INR 34,560.00 deposited in A/c XX9922 on 14-JAN-19 for NEFT Cr-CITI0000004-RANKA STEELS-SteelMint info services LLP-CITIN19906881882.Avl bal:INR 11,21,166.60 subject to clearing",
      "smsSenderName": "Vk-hdfcbk",
      "TIMESTAMP": "1528813557565"
  }
]';  
$smsArr=json_decode($txt1,1);



//$smsArr=json_decode(trim($_REQUEST['smsDtls']),1);

$result_arr=array();

    foreach ($smsArr as $key => $value) {
   
      $dt=explode('.',$value['smsDateTime']);
      $tt= str_replace("/","-",$dt[0]);
      $amount="";
      $mode="";
      $ref_no='';
      $paymode="";
      $pymt_dtls='';
      $bank_name='';
      $conversion_rate='';
      $currency='INR';

      if(strpos(strtoupper($value['smsSenderName']), 'HDFC') !== false)
            $bank_name="HDFC Bank 0152 256 0009922";
        else if(strpos(strtoupper($value['smsSenderName']), 'AXIS') !== false)
            $bank_name="AXIS BANK RAIPUR- 913020027601670";
        else if(strpos(strtoupper($value['smsSenderName']), 'IDBI') !== false)
            $bank_name="IDBI BANK A/C.NO.0049102000048888";

      if ($bank_name!='') {

       if(strpos($value['smsMatter'],'XX9922') && !(strpos($value['smsMatter'],'debited'))){

        $bank_name="HDFC Bank 0152 256 0009922";
            if(strpos($value['smsMatter'],'deposited')){
              $amount=getBetween($value['smsMatter'],'INR','deposited');
            }
            else if(strpos($value['smsMatter'],'credited')){
              $amount=getBetween($value['smsMatter'],'credited with INR ',' on ');
            }

            if(strpos($value['smsMatter'],'INW') && strpos($value['smsMatter'],'@')){

                $get_swift=splitr($value['smsMatter'],'HDFC');
                $amount=$get_swift['deposited_amount'];
                $ref_no=$get_swift['ref_no'];
                $pymt_dtls_1=getBetween($value['smsMatter'],'for','.Avl');
                $pymt_dtls=trim($pymt_dtls_1);
                $currency=$get_swift['currency'];
                $conversion_rate=$get_swift['conversion_rate'];
                $paymode=$get_swift['pymt_mode'];
            }
            else if(strpos($value['smsMatter'],'IMPS')){ 
                    $paymode='IMPS';
                    $ref_1=explode('IMPS',$value['smsMatter']);

                    $ref_2=explode('-',$ref_1['1']);
                    $ref_no=$ref_2['1'];
                    $pymt_dtls=$ref_2['2'];
            }
            else if(strpos(strtoupper($value['smsMatter']),'CHQ')){

                    if(strpos(($value['smsMatter']),"UPDATE:")!== false){
                      $pymt_dtls_1=getBetween($value['smsMatter'],'CHQ DEP','.Avl');
                    }
                    else{
                      $pymt_dtls_1=getBetween($value['smsMatter'],'CHQ DEP','Val');
                    }
                    $pymt_dtls=trim($pymt_dtls_1);
                    $pymt_dtls=trim($pymt_dtls,'-');
                    $ref_no='';
                    $paymode='CHEQUE';
            }
            else if(strpos($value['smsMatter'],'NEFT')){ 

                    $paymode='NEFT';
                    $ref_no_1=getBetween(strtoupper($value['smsMatter']),'STEEL MINT INFO SERVICES LLP','.AVL');
                    if($ref_no_1=='')
                      $ref_no_1=getBetween(strtoupper($value['smsMatter']),'STEELMINT INFO SERVICES LLP','.AVL');
                    if($ref_no_1=='')
                      $ref_no_1=getBetween(strtoupper($value['smsMatter']),'STEEL MINT INFO SERVICE LLP','.AVL');
                    if($ref_no_1=='')
                      $ref_no_1=getBetween(strtoupper($value['smsMatter']),'STEELMINT INFO SERVICE LLP','.AVL');

                    $ref_no=trim($ref_no_1,'-');

                    $ref_1=explode('NEFT',$value['smsMatter']);
                  
                    $ref_2=explode('-',$ref_1['1']);

                    $count=count($ref_2);
                    $pymt_dtls_1='';
                    for($i=2;$i<$count-2;$i++){

                      $pymt_dtls_1.=$ref_2[$i]." ";

                    }
                    $pymt_dtls=trim($pymt_dtls_1);
                    
                }
            else if(strpos($value['smsMatter'],'RTGS')){ 
                    $paymode='RTGS';

                    $ref_1=explode('RTGS',$value['smsMatter']);

                    $ref_2=explode('-',$ref_1['1']);
                    $ref_3=explode('.',$ref_2['4']);
                    $ref_4=explode(' ',$ref_3['0']);
                    
                    $ref_no=$ref_4["0"];
                    $pymt_dtls=$ref_2['2']; 
                
                }
            else if(strpos($value['smsMatter'],'FT-') || strpos($value['smsMatter'],'FT -')){
                    $paymode='FT';
                    $value['smsMatter']= str_replace("FT -","FT-",$value['smsMatter']);

                    if(strpos(($value['smsMatter']),"UPDATE:")!== false){
                      $pymt_dtls_1=getBetween(strtoupper($value['smsMatter']),'FT','.AVL');
                    }
                    else{
                      $pymt_dtls_1=getBetween($value['smsMatter'],'FT','Val'); 
                    }

                    $pymt_dtls_2=explode('-',$pymt_dtls_1);
                    $count=count($pymt_dtls_2);
                    $pymt_dtls=$pymt_dtls_2[$count-1];
                    $ref_no=$pymt_dtls_2[$count-2];
            }
             else if(strpos($value['smsMatter'],'CASH')){ 
                    $paymode='CASH';
                     //echo "Paymode: ".$paymode."<br>";
                    if(strpos(($value['smsMatter']),"UPDATE:")!== false){
                      $pymt_dtls_1=getBetween($value['smsMatter'],'CASH DEP','.Avl');
                    }
                    else{
                      $pymt_dtls_1=getBetween($value['smsMatter'],'CASH DEP','Val');
                    }
                    $pymt_dtls=trim($pymt_dtls_1);
                    $pymt_dtls=trim($pymt_dtls,'-');
                    $ref_no='';
            }
            else{
                    $paymode='ONLINE';
                 
                  if(strpos($value['smsMatter'],'deposited')){
                      $pymt_dtls=getBetween($value['smsMatter'],'for','.Avl');
                  }
                }
        }// close hdfc
        else if($bank_name=='IDBI BANK A/C.NO.0049102000048888'){

        if(strpos($value['smsMatter'],'NN48888') && !(strpos($value['smsMatter'],'debited'))){
          
          $amount_1=getBetween($value['smsMatter'],'credited Rs','Det:');
          $amount=trim($amount_1);

          if($amount==''){
            $amount_1=getBetween($value['smsMatter'],'credited by Rs','for');
            $amount=trim($amount_1);
          }

          if(strpos($value['smsMatter'],'NEFT')){ 

             
                 $paymode='NEFT';

                 $ref_no=getBetween($value['smsMatter'],'NEFT-','. A/c');
                 $pymt_dtls_1=getBetween($value['smsMatter'],'Det:','. A/c');
                 $pymt_dtls=trim($pymt_dtls_1);
                    
            }
            else if(strpos($value['smsMatter'],'RTGS')){ 

             
                 $paymode='RTGS';

                 $ref_no=getBetween($value['smsMatter'],'RTGS/','. A/c');
                 $pymt_dtls_1=getBetween($value['smsMatter'],'Det:','. A/c');
                 $pymt_dtls=trim($pymt_dtls_1);
                    
            }
            else{

                $paymode='ONLINE';

                 $ref_no=getBetween($value['smsMatter'],'Det: ','. A/c');
                 $pymt_dtls_1=getBetween($value['smsMatter'],'Det: ','. A/c');
                 $pymt_dtls=trim($pymt_dtls_1);
            }
           }
        } // close IDBI
    else if($bank_name=='AXIS BANK RAIPUR- 913020027601670'){

      if(strpos($value['smsMatter'],'601670') && !(strpos($value['smsMatter'],'debited'))){

            if(strpos($value['smsMatter'],'NEFT')){ 

                 $amount_1=getBetween($value['smsMatter'],'credited with Rs.','on');
                 $amount=trim($amount_1);
               
                 $paymode='NEFT';
                 $pymt_dtls_1=getBetween($value['smsMatter'],'Info: NEFT/','.');
                 $pymt_dtls_2=explode('/',$pymt_dtls_1);
                 $ref_no=trim($pymt_dtls_2['0']);
                 $pymt_dtls=trim($pymt_dtls_2['1']);                    
              }
             else if(strpos($value['smsMatter'],'Cheque')){
                $paymode='CHEQUE';

                $amount_1=getBetween($value['smsMatter'],'for Rs. ','is deposited');
                $amount=trim($amount_1);
                $ref_no='';
                $pymt_dtls=''; 
              }
            else if(strpos($value['smsMatter'],'BRN-REF NO.')){
                $get_swift=splitr($value['smsMatter'],'AXIS');
                $amount=$get_swift['deposited_amount'];
                $ref_no=$get_swift['ref_no'];
                $pymt_dtls_1=getBetween($value['smsMatter'],'Info:','. ');
                $pymt_dtls=trim($pymt_dtls_1);
                $currency=$get_swift['currency'];
                $conversion_rate=$get_swift['conversion_rate'];
                $paymode=$get_swift['pymt_mode'];
            }
            else{
               $amount_1=getBetween($value['smsMatter'],'credited with Rs.','on');
               $amount=trim($amount_1);

               $paymode='ONLINE';
               $pymt_dtls_1=getBetween($value['smsMatter'],'Info: ','.');
               $pymt_dtls_2=explode('/',$pymt_dtls_1);
               $ref_no=trim($pymt_dtls_2['1']);
               $pymt_dtls=trim($pymt_dtls_2['0']); 
            }
          }
   
        } // close AXIS
        if($amount!='' && !empty($amount)){

          if(strpos($value['smsMatter'],'PAYPAL PAYMENTS PL') || (strpos($value['smsMatter'],'M/S AVENUES (INDIA)')) || (strpos($value['smsMatter'],'M/S AVENUES (INDIA)')) || (strpos($value['smsMatter'],'AVENUES INDIA PVT LTD')) || (strpos($value['smsMatter'],'WORKING ACCOUNT')) || (strpos($value['smsMatter'],'HDFC MUTUAL FUND')) || (strpos($value['smsMatter'],'HDFC BALANCED ADVAN')) ){

            $show_in_listing='N';
          }
          else{
            $show_in_listing='Y';
          }

          echo "<pre>";
          echo  ("INSERT INTO tbl_bank_stmt SET 
                          f_datetime=CURTIME(), 
                          ref_no='".$ref_no."', 
                          deposited_amount='".trim(str_replace(",","",$amount))."', 
                          pymt_mode='".$paymode."', 
                          deposited_datetime='".date('Y-m-d H:i:s',strtotime($tt))."',
                          currency='".$currency."',
                          conversion_rate='".$conversion_rate."',
                          sender_name='".htmlspecialchars(mysql_real_escape_string($value['smsSenderName']),ENT_QUOTES)."', 
                          smsDtls='".htmlspecialchars(mysql_real_escape_string($value['smsMatter']),ENT_QUOTES)."', 
                          pymt_dtls='".htmlspecialchars(mysql_real_escape_string($pymt_dtls),ENT_QUOTES)."', 
                          show_in_listing='".$show_in_listing."',
                          deposited_in_bank_name='".htmlspecialchars(mysql_real_escape_string($bank_name),ENT_QUOTES)."'");
           //or die(mysql_error());
         //echo "</pre>";
          $insert_id=100;

          $result_arr[] = array("FROM" => $value['smsSenderName'], "TIMESTAMP" => $value['TIMESTAMP'], "ID" => $insert_id);

          //print_r($result_arr);
       }
       else{
          $result_arr[] = array("FROM" => $value['smsSenderName'], "TIMESTAMP" => $value['TIMESTAMP'], "ID" => '');
      }
       
       } //close bank_name!=''

  } // close foreach
 $jsonn= json_encode($result_arr);

  //print_R($result_arr);
echo "<br>";
 echo $jsonn;
die;

include_once('../inc/desconfig.php');
?>