<?php 
/*
    define("IN_SITE", true);
    require_once(__DIR__."/../../config/config.php");
    require_once(__DIR__."/../../config/function.php");
    
    $IULA= new IULA();
    if (isset($_GET['pass_cron'])) {
    // Tiếp tục xử lý nếu 'pass_cron' tồn tại trong mảng $_GET
    if ($IULA->site('pass_cron') != $_GET['pass_cron']) {
        die('Mật khẩu cron không đúng');
    }
   } else {
    die('Đéo cho cron');
  }
    
    if (time() - $IULA->site('limit_cron_withdrawBank') < 15)
    {
     die('Limit');
     }
   $IULA->update("options", ['value' => time()], " `name` = 'limit_cron_withdrawBank' ");
    
   
    $limit_allow = $IULA->site('max_rut_auto_atm') ??   5000000;
    $query = "SELECT * FROM `wallet_orders_ruttien` WHERE  `status` = 'pending'  AND `paygate_code` LIKE '%Localbank%' ORDER BY ID ASC LIMIT 1";
    foreach ($IULA->get_list($query) as $withdraw) 
    {
    if($withdraw['status'] != 'pending')
    {
    exit();
    }
    if($withdraw['net_amount'] > $limit_allow)
    {
             $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Không thể chuyển đơn qua api do admin cài giới hạn rút Localbank qua api là  '.$limit_allow ,  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");   
                exit();
    }
      $check_noidung = $IULA->get_row("SELECT * FROM `wallet_orders_ruttien` WHERE `description` = '".$withdraw['description']."' AND (`status` = 'completed' OR `status` = 'processing')");
     if($check_noidung)
     {
       $IULA->update("wallet_orders_ruttien", [
                'status'  => 'completed', 
                'message'   => 'Nội dung đơn hàng đã tồn tại và được chuyển thành công hoặc đang chuyển rồi' ,  
                'client_note'   => 'ND',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");   
                exit();   
     }
    $vendor = $withdraw['vendor'];  
    if($IULA->get_row("SELECT * FROM `paygates` WHERE `code` = '".$withdraw['paygate_code']."' ")['status']  !=  '1')
    {
             $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => $withdraw['paygate_code'].' Đã bị tắt status - đơn tạm giữ' ,  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();
    }

    if($withdraw['vendor']  ==  'ApiWebme')
    {
     $infoProviders = $IULA->get_row("SELECT * FROM `paygate_vendors` WHERE `code` = '".$withdraw['vendor']."' ");
    $Token = json_decode($infoProviders['configs'], true);
    $Token = $Token['token'] ?? '';

    // XỬ LÝ THIẾU TOKEN
    if (empty($Token))
    {
    $IULA->update("wallet_orders_ruttien", [
        'status'      => 'hold',
        'message'     => $Token,
        'client_note' => 'Tạm giữ, vui lòng chờ xử lý lại',
        'updated_at'  => gettime(),
    ], " `id` = '".$withdraw['id']."' ");
    exit();
    }

     
     
     require_once(__DIR__."/../../frontend/paygate/providers/ApiWebme.php");
     $ApiWebme_help = new ApiWebme();
     $result = $ApiWebme_help->transferWebme($Token, $withdraw['paygate_code'], $withdraw['account_num'], $withdraw['account_name'], floatval($withdraw['net_amount']), $withdraw['description']);
     if($result['status'] == 1)
     { 
             //UPDATE processing
              $IULA->update("wallet_orders_ruttien", [
                'status'  => 'processing', 
                'message'   => check_status_ApiWebme($result['status'], $result['message'] ??  'Lỗi không xác định'), 
                'client_note'   => 'Đang chuyển tiền',
                'payment_trans_id'   => $result['data']['order_code'] ?? '',
                'updated_at'   => gettime(),
                'log'   => json_encode($result ?? '')
            ], " `id` = '".$withdraw['id']."' ");   
              exit();
     }else {
           $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => check_status_ApiWebme($result['status'], $result['message'] ??  'Lỗi không xác định'), 
                'client_note'   => 'Chuyển tiền thất bại, vui lòng chờ duyệt lại',
                'payment_trans_id'   => $result['data']['order_code'] ?? '',
                'updated_at'   => gettime(),
                'log'   => json_encode($result ?? '')
            ], " `id` = '".$withdraw['id']."' ");   
           exit();
        }
    }
    
    }
         echo 'hệ thống tự động chuyển đơn rút sang web mẹ';

    */
    