Plugin Directory

Changeset 2925221


Ignore:
Timestamp:
06/13/2023 10:39:47 AM (3 years ago)
Author:
aftercommerce
Message:

update the cron functionality.

Location:
afterinc-app/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • afterinc-app/trunk/Custom_app.php

    r2924031 r2925221  
    44Plugin URI: https://www.afterinc.com/contact/
    55Description: Plugin allows merchants to sell After Plan’s product protection plans for wide variety of products.
    6 Version: 3.6.5
     6Version: 3.6.6
    77Author: After Inc.
    88Author URI: https://www.afterinc.com
     
    5858     register_activation_hook( __FILE__, array($this, 'jal_install_data') );
    5959     
    60      register_activation_hook(__FILE__, array($this, 'my_activation_for_order_sync'));
    61      register_deactivation_hook(__FILE__, array($this, 'my_deactivation_for_order_sync'));
    6260
    6361
     
    195193   add_action( 'phpmailer_init', array($this,'my_phpmailer_afterinc' ));
    196194 
    197    add_filter( 'cron_schedules', array($this,'example_add_cron_interval' ));
     195
     196   add_filter('cron_schedules', array($this, 'add_cron_interval_for_order_sync'));
     197   add_action('my_oneminute_event', array($this, 'order_scheduler_cron'));
     198   
     199   add_action('my_custom_5_minute_cron', array($this,'my_custom_5_minute_function'));
     200    add_filter('cron_schedules', array($this,'add_custom_cron_intervals'));
    198201   
    199    add_action( 'my_fiveminute_event', array($this,'order_scheduler_cron'));
     202   
    200203 
    201204 }
     
    890893     }
    891894   }
    892      
    893      function example_add_cron_interval( $schedules ) {
    894          $schedules['scheduled_fiveminute'] = array(
    895          'interval' => 4*60*60,
    896          'display'  => esc_html__( 'scheduled fiveminute' ),);
    897           return $schedules;
    898       }
    899      
    900         function my_activation_for_order_sync() {
    901 
    902             if (!wp_next_scheduled( 'my_fiveminute_event' ) ) {
    903                  wp_schedule_event( time(), 'scheduled_fiveminute', 'my_fiveminute_event');
    904             }
     895   
     896    function add_cron_interval_for_order_sync($schedules) {
     897
     898            $schedules['my_oneminute_event'] = array(
     899            'interval' => 4*60*60,
     900            'display'  => esc_html__( 'scheduled my_oneminute_event' ),);
     901             return $schedules;
    905902        }
    906 
    907          function my_deactivation_for_order_sync() {
    908              wp_clear_scheduled_hook('my_oneminute_event');
    909              wp_clear_scheduled_hook('sync_fiveminute_event');
     903       
     904       
     905     function add_custom_cron_intervals($schedules) {
     906    $schedules['every_five_minutes'] = array(
     907        'interval' => 300, // 5 minutes in seconds
     908        'display' => __('Every 20 Minutes')
     909    );
     910     return $schedules;
     911   }
     912   
     913   function my_custom_5_minute_function() {
     914     plugin_log("This is testing for every five minute test");
     915
     916      $appConfig = $this->wpdb->get_results($this->wpdb->prepare("SELECT * FROM app_configurations WHERE id = %d", 1));
     917
     918      $cronStatus = $this->wpdb->get_results("SELECT * FROM quickcover_cron_status WHERE status = 404 ORDER BY order_id DESC ");
     919
     920           if (!empty($cronStatus)) {
     921
     922                  foreach ($cronStatus as $key => $value) {
     923
     924                        $order_id = $value->order_id;
     925                        $order = wc_get_order($order_id);
     926                        $email = $order->get_billing_email();
     927                        $fname = $order->get_billing_first_name();
     928                        // $customername = $order->billing_first_name() .' '.$order->billing_last_name();
     929
     930                      if (!empty($order)) {
     931                            $order_no =   $order->get_id();
     932                            $order_data = $order->get_data();
     933                            $order_items = $order->get_items();
     934                            $srlNo = count($order_items);
     935                            $order_date = $order->get_date_created()->format('Y-m-d H:i:s');
     936                            $order_status  = $order->get_status();
     937
     938                          if ($order_status !== 'cancelled') {
     939                                  $orderItem = array();
     940                                  $in_warranty = array();
     941                                  $planNameArr = array();
     942                                  $planTitle = null;
     943                                foreach ($order_items as $key => $item) {
     944                                    $lineitem_id = $item->get_id();
     945                                    $productID = $item->get_product_id();
     946
     947                                    $result = $this->wpdb->get_results($this->wpdb->prepare("SELECT * FROM product_warranty WHERE product_id = %d ", $productID));
     948
     949                                    if (!empty($result)) {
     950                                          $planTitle = $result[0]->pname;
     951                                          $planDatas = $item->get_meta_data();
     952                                          $planName = '';
     953                                          $planPrice = '';
     954                                        foreach ($planDatas as $key => $planId) {
     955                                            if (isset($planId->key) && ($planId->key == '_warranty')) {
     956                                                    $plan_id = $planId->value['plan_id'];
     957                                                if ($plan_id !== 'none') {
     958                                                    $in_warranty[] = $result[0]->product_id;
     959                                                    $plans = json_decode($result[0]->plans, true);
     960
     961
     962                                                    foreach ($plans as $key => $plan) {
     963                                                        if ($plan['planId'] == $plan_id) {
     964                                                            $contract = array(
     965                                                            'priceCurrency' => $result[0]->priceCurrency,
     966                                                            'planId' => $plan['planId'],
     967                                                            'price' => $plan['price'],
     968                                                            'quantity' => $item->get_quantity(),
     969                                                            );
     970                                                            $planName = $plan['description'];
     971                                                            $planPrice = $plan['price'] * $item->get_quantity();
     972                                                        }
     973                                                    }
     974
     975                                                    $orderItem[] = array(
     976                                                    'id' => $lineitem_id,
     977                                                    'product' => array(
     978                                                    'priceCurrency' => $result[0]->priceCurrency,
     979                                                    'id' => $item->get_product_id(),
     980                                                    'contract' => $contract,
     981                                                    'purchasePrice' => (float) ($item->get_product()->get_price()) * 100,
     982                                                    'quantity' => $item->get_quantity(),
     983                                                    'serialNumber' => $srlNo
     984                                                    )
     985                                                    );
     986                                                }
     987                                             }
     988                                         }
     989                                           
     990                                    }
     991                                }
     992
     993                                if (!empty($in_warranty)) {
     994
     995                                    if (($order_data['billing']['address_1'] == "")) {
     996                                        $order_data['billing']['address_1'] = "null";
     997                                    }
     998                                    if (($order_data['billing']['address_2'] == "")) {
     999                                        $order_data['billing']['address_2'] = "null";
     1000                                    }
     1001                                    if (($order_data['billing']['city'] == "")) {
     1002                                        $order_data['billing']['city'] = "null";
     1003                                    }
     1004                                    if (($order_data['billing']['postcode'] == "")) {
     1005                                        $order_data['billing']['postcode'] = "null";
     1006                                    }
     1007                                    if (($order_data['billing']['country'] == "")) {
     1008                                        $order_data['billing']['country'] = "null";
     1009                                    }
     1010
     1011                                    if (($order_data['shipping']['address_1'] == "")) {
     1012                                        $order_data['shipping']['address_1'] = "null";
     1013                                    }
     1014                                    if (($order_data['shipping']['address_2'] == "")) {
     1015                                        $order_data['shipping']['address_2'] = "null";
     1016                                    }
     1017                                    if (($order_data['shipping']['city'] == "")) {
     1018                                        $order_data['shipping']['city'] = "null";
     1019                                    }
     1020                                    if (($order_data['shipping']['postcode'] == "")) {
     1021                                        $order_data['shipping']['postcode'] = "null";
     1022                                    }
     1023                                    if (($order_data['shipping']['country'] == "")) {
     1024                                        $order_data['shipping']['country'] = "null";
     1025                                    }
     1026
     1027                                    $order_array = array(
     1028                                    'priceCurrency' => $order_data['currency'],
     1029                                    'id' => $order_data['id'],
     1030                                    'customer' => array(
     1031                                    'givenName'  => $order_data['billing']['first_name'],
     1032                                    'alternateName' => "kumar",
     1033                                    'lastName' => $order_data['billing']['last_name'],
     1034                                    'email' => $order_data['billing']['email'],
     1035                                    'phone' => $order_data['billing']['phone'],
     1036                                    'billingAddress' => array(
     1037                                    'address1' => $order_data['billing']['address_1'],
     1038                                    'address2' => $order_data['billing']['address_2'],
     1039                                    'locality' => $order_data['billing']['city'],
     1040                                    'postalCode' => $order_data['billing']['postcode'],
     1041                                    'region' => $order_data['billing']['country'],
     1042                                    ),
     1043                                    'shippingAddress' => array(
     1044                                    'address1' => $order_data['shipping']['address_1'],
     1045                                    'address2' => $order_data['shipping']['address_2'],
     1046                                    'locality' =>  $order_data['shipping']['city'],
     1047                                    'postalCode' => $order_data['shipping']['postcode'],
     1048                                    'region' => $order_data['shipping']['country'],
     1049                                    ),
     1050                                    ),
     1051                                    'purchaseDate' => $order_date,
     1052                                    'totalPrice' =>  (float) ($order->get_total()) * 100,
     1053                                    'poNumber' => $order_id,
     1054                                    'orderItem' => $orderItem
     1055                                    );
     1056
     1057                                    $orderList = json_encode($order_array);
     1058
     1059                                    if (!empty($appConfig)) {
     1060                                        $apiKey = $appConfig[0]->api_key;
     1061                                        $storeId = $appConfig[0]->store_id;
     1062                                        $toemail = $appConfig[0]->email;
     1063
     1064                                        $currentadmin_login = $appConfig[0]->user_id;
     1065
     1066                                         $tablename = $this->wpdb->prefix . 'users';
     1067
     1068                                        $get_admindetails = $this->wpdb->get_results($this->wpdb->prepare("SELECT user_login FROM $tablename WHERE ID = %s ", $currentadmin_login));
     1069                                         $merchant_login=$get_admindetails[0]->user_login;
     1070
     1071                                        $base_url = AfterInc::API_BASE_URL;
     1072                                        $url = $base_url . 'orders/outlet/' . $storeId;
     1073
     1074                                        $args = array(
     1075                                        'headers' => array(
     1076                                        'Content-Type' => 'application/json',
     1077                                        'X-QUICKCOVER-API-KEY' => $apiKey
     1078                                        ),
     1079                                        'body' => $orderList,
     1080                                        'method'    => 'POST',
     1081                                        'timeout'     => 300
     1082                                        );
     1083
     1084                                        $response = wp_remote_post($url, $args);
     1085                                        $response_status = wp_remote_retrieve_response_code($response);
     1086
     1087                                        $response = wp_remote_retrieve_body($response);
     1088                                        $orderData = json_decode($response);
     1089
     1090                                        plugin_log(['contract_status' => $response_status, 'response' => $response, 'request' => $orderList]);
     1091
     1092                                        if ($response_status == 201) {
     1093                                            plugin_log('resolved');
     1094                                            if (isset($orderData->contracts)) {
     1095                                                $contractData = $orderData->contracts;
     1096
     1097                                                foreach ($contractData as $key => $value) {
     1098
     1099                                                    $currentID = get_current_user_id();
     1100                                                    $contrats = $this->wpdb->query(
     1101                                                        $this->wpdb->prepare(
     1102                                                            "INSERT INTO quickcover_contract( user_id, product_id, order_id, lineitem_id, quantity, contract_id, contract_status, refund_amount, contract_canceled_by)
     1103                     VALUES ( '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s')", $currentID, $value->order->product->id, $value->order->id, $value->order->orderItemId, $value->order->product->quantity, $value->id, 0, 0, 0
     1104                                                        )
     1105                                                    );
     1106                                                }
     1107                                            }
     1108                        $remove = $this->wpdb->query(
     1109                            $this->wpdb->prepare("DELETE FROM quickcover_cron_status WHERE order_id = %d", $order_id)
     1110                        );
     1111                        plugin_log(['contract_status' => $response_status, 'response' => $response, 'request' => $orderList]);
     1112                         
     1113                         $to = 'asoanker@afterinc.com';
     1114                   $subject = 'QuickCover Create Contract API Success : After Product Protection Plan';
     1115                 
     1116                   $headers = array('Content-Type: text/html; charset=UTF-8','From: noreply@quickcover.me');
     1117                   $message = "<html>
     1118                          <body>
     1119                            <h2>QuickCover Server Resolved</h2>
     1120                            <h4>Outlet Id: $storeId </h4></br>
     1121                            <h5>Payload</h5>
     1122                            <p>$orderList</p>
     1123                            </br>
     1124                            <h5>Response</h5>
     1125                           <p>['status'=>$response_status,'response'=>$response]</p>
     1126                         </body></html>";
     1127             
     1128                       wp_mail($to, $subject, $message, $headers);
     1129                               
     1130                        }
     1131                      }
     1132                   }
     1133                 }
     1134               }
     1135             }
     1136           }
    9101137         }
     1138       
    9111139
    9121140  /* display warranty option on product details page */
     
    10851313    }
    10861314  }
     1315 
     1316  if ( ! wp_next_scheduled('my_oneminute_event')) {
     1317   wp_schedule_event( time(), 'my_oneminute_event', 'my_oneminute_event');
     1318   }
     1319   
     1320    if (!wp_next_scheduled('my_custom_5_minute_cron')) {
     1321       wp_schedule_event(time(), 'every_five_minutes', 'my_custom_5_minute_cron');
     1322      }
    10871323
    10881324}
     
    28193055     }
    28203056 
    2821  function differenceInHours($startdate,$enddate){
    2822          $starttimestamp = strtotime($startdate);
    2823          $endtimestamp = strtotime($enddate);
    2824         //$difference = abs($endtimestamp - $starttimestamp)/3600 * 60 ; // for minute
    2825          $difference = abs($endtimestamp - $starttimestamp)/3600; // for hour
    2826          return $difference;
    2827 }
     3057
    28283058    //cron code start here
    2829     function order_scheduler_cron($schedules) {
     3059    function order_scheduler_cron() {
    28303060     
    28313061      ini_set('max_execution_time', '300');
     
    30323262                 else{
    30333263                     
    3034                      //$this->wpdb->query($this->wpdb->prepare("UPDATE quickcover_cron_status SET `counter`=(`counter`+5) WHERE `order_id` = " . $order_no . " "));
    3035 
    3036                     // $increment_ord = $this->wpdb->get_results($this->wpdb->prepare("SELECT counter FROM quickcover_cron_status WHERE order_id = %d", $order_no));
     3264                     $this->wpdb->query($this->wpdb->prepare("UPDATE quickcover_cron_status SET `counter`=(`counter`+5) WHERE `order_id` = " . $order_no . " "));
     3265
     3266                    $increment_ord = $this->wpdb->get_results($this->wpdb->prepare("SELECT counter FROM quickcover_cron_status WHERE order_id = %d", $order_no));
    30373267                   
    30383268                     $planName = implode(",", $planNameArr);
    30393269                     $totalplan = array_sum($planpriceArr)/100;
    30403270                     $siteTitleName = get_bloginfo('name');
    3041                     plugin_log('createdTimeDb::'.$value->created_at);
    3042                     $dtt = date('Y-m-d H:i:s',strtotime($value->created_at));
    3043                     plugin_log($dtt ." & ". date('Y-m-d H:i:s'));
    3044                     $diffItmer = round($this->differenceInHours($dtt,date('Y-m-d H:i:s')),2);
    3045                     plugin_log("diffminute::".$diffItmer);
     3271                    // plugin_log('createdTimeDb::'.$value->created_at);
     3272                    // $dtt = date('Y-m-d H:i:s',strtotime($value->created_at));
     3273                    // plugin_log($dtt ." & ". date('Y-m-d H:i:s'));
     3274                    // $diffItmer = round($this->differenceInHours($dtt,date('Y-m-d H:i:s')),2);
     3275                    // plugin_log("diffminute::".$diffItmer);
    30463276                     
    3047                        if ($diffItmer > 12) {
     3277                       if ($increment_ord[0]->counter > 11) {
    30483278                            plugin_log('checking if part for final:');
    30493279                            $to = $toemail;
  • afterinc-app/trunk/readme.txt

    r2924031 r2925221  
    77Requires PHP: 7.2
    88WC tested up to: 5.6.0
    9 Stable tag: 3.6.5
     9Stable tag: 3.6.6
    1010License: GPLv3
    1111License URI: https://www.afterinc.com
Note: See TracChangeset for help on using the changeset viewer.