<?php
/**
 *
 * Recurring functions that are run by WPDAI_Action_Scheduler and functions that will trigger these one off events
 *
 * @package Alpha Insights
 * @version 4.4.0
 * @since 4.4.0
 * @author WPDavies
 * @link https://wpdavies.dev/
 *
 */
defined( 'ABSPATH' ) || exit;

/**
 * 
 *  Will fetch for the most recent N orders that don't have a cache and build that cache
 * 
 *  @return bool|int Will return false on failure, true if cache is complete and order count representing number of updates
 * 
 **/
function wpdai_fetch_and_store_last_n_uncached_orders_cron() {

    // Start timer
	$start = microtime( true );
    $memory_usage = 0;

    wpdai_write_log( 'Executing wpd_schedule_order_calculation_cache_collector', 'cron' );
    wpdai_write_log( 'Executing wpd_schedule_order_calculation_cache_collector', 'cache' );

    // Check if cache is complete
    $order_cache_complete = get_option( 'wpd_ai_all_orders_cached', 0 );
    if ( $order_cache_complete == 1 ) {

        wpdai_write_log( 'Order cache has already been determined as complete, not continuing.', 'cron' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cron' );
        wpdai_write_log( 'Order cache has already been determined as complete, not continuing.', 'cache' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cache' );

        return true; 

    }

    // Get last n orders
    $max_count = 250;

    wpdai_write_log( 'Searching database for the latest ' . $max_count . ' orders that dont have the cache set.', 'cron' );
    wpdai_write_log( 'Searching database for the latest ' . $max_count . ' orders that dont have the cache set.', 'cache' );

    $last_n_orders_without_cache = wpdai_get_order_ids_without_calculation_cache( $max_count );

    // Safety Check
    if ( ! is_array($last_n_orders_without_cache) ) {

        wpdai_write_log( 'Couldnt produce an array when searching for orders without cache, execution stopping.', 'cron' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cron' );
        wpdai_write_log( 'Couldnt produce an array when searching for orders without cache, execution stopping.', 'cache' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cache' );

        return false; 
    }

    wpdai_write_log( 'Found ' . count( $last_n_orders_without_cache ) . ' orders with max search count of ' . $max_count, 'cron' );
    wpdai_write_log( 'Found ' . count( $last_n_orders_without_cache ) . ' orders with max search count of ' . $max_count, 'cache' );

    // If we've got some, update the calculations
    if ( count( $last_n_orders_without_cache ) > 0 ) {

        wpdai_write_log( 'Looping throgh ' . count( $last_n_orders_without_cache ) . ' orders to update the cache.', 'cron' );
        wpdai_write_log( 'Looping throgh ' . count( $last_n_orders_without_cache ) . ' orders to update the cache.', 'cache' );

        foreach( $last_n_orders_without_cache as $order_id ) {

            // Log the memory usage
            if ( wpdai_get_peak_memory_usage() > $memory_usage ) $memory_usage = wpdai_get_peak_memory_usage();

            wpdai_calculate_cost_profit_by_order( $order_id, true );

        }

        $finish             = microtime( true );
        $total_time_elapsed = round( $finish - $start, 2 );

        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cron' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cache' );

        return count( $last_n_orders_without_cache );

    } else {

        wpdai_write_log( 'Count of orders was 0, so we must be finished processing the order cache, updating the database to complete.', 'cron' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cron' );
        wpdai_write_log( 'Count of orders was 0, so we must be finished processing the order cache, updating the database to complete.', 'cache' );
        wpdai_write_log( 'Completed wpd_schedule_order_calculation_cache_collector.', 'cache' );

        $finish             = microtime( true );
        $total_time_elapsed = round( $finish - $start, 2 );

        // Log the memory usage
        if ( wpdai_get_peak_memory_usage() > $memory_usage ) $memory_usage = wpdai_get_peak_memory_usage();

        // Set order cache as complete
        $order_cache_complete = update_option( 'wpd_ai_all_orders_cached', 1 );

        // Delete the transient if it happens to exist
        delete_transient('_wpd_updating_all_orders_cache');

        // Return success
        return true;

    }

}

/**
 * 
 *  Hourly cron schedule to update customer analytics in batches
 * 
 *  Will fetch info like total sales etc etc
 * 
 **/
function wpdai_collect_customer_statistics_cron() {

    wpdai_write_log( 'Executing wpd_schedule_customer_analytics_collector', 'cron' );

    // Start timer
	$start = microtime( true );
    $memory_usage = 0;

    // List of user Id's
    $user_ids =  wpdai_query_user_ids_for_analytics_collector( 25 ); // Process 25 users at a time
    
    // Log start
	wpdai_write_log( 'Executing Customer Analytics cache builder on ' . count( $user_ids ) . ' users', 'cache' );
	wpdai_write_log( 'Executing Customer Analytics cache builder on ' . count( $user_ids ) . ' users', 'cron' );

    // Safety check the data
    if ( is_array($user_ids) && ! empty($user_ids) ) {

        $user_count = count( $user_ids );
        $i = 1;

        // Entry logging
        wpdai_write_log( 'We have an array of User IDs, ' . $user_count . ' users found.', 'cron' );
        wpdai_write_log( 'Looping through User IDs to fetch customer analytics.', 'cron' );

        // Loop through users
        foreach( $user_ids as $user_id ) {

            // False = force refresh data collection and update transients and meta
            $user_analytics = wpdai_fetch_customer_analytics_by_user_id( $user_id, false );

            // Log iteration
            wpdai_write_log( '(' . $i . '/' . $user_count . ') Calculating analytics for user id: ' . $user_id . '.', 'cron' );

            if ( is_array($user_analytics) && ! empty( $user_analytics ) ) {

                wpdai_write_log( 'Update succesful.', 'cron' );
    
            }

            // Log the memory usage
            if ( wpdai_get_peak_memory_usage() > $memory_usage ) $memory_usage = wpdai_get_peak_memory_usage();

            // Iterator
            $i++;

        }

    } else {

        wpdai_write_log( 'We couldnt find a good data collection, the format wasnt an array or it was empty. Couldnt find user IDs? Check debug.log.', 'cron' );

    }

    $finish             = microtime( true );
	$total_time_elapsed = round( $finish - $start, 2 );

    wpdai_write_log( 'Completed wpd_schedule_customer_analytics_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cron' );
    wpdai_write_log( 'Completed wpd_schedule_customer_analytics_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cache' );

}

/**
 * 
 *  Hourly schedule to update product analytics
 * 
 *  Will fetch info like total sales etc etc
 * 
 **/
function wpdai_collect_product_statistics_cron() {

    // Start timer
	$start          = microtime( true );
    $memory_usage   = 0;

    wpdai_write_log( 'Executing wpd_schedule_product_analytics_collector', 'cron' );
    wpdai_write_log( 'This function will store sales data and reference the analytics database to check for website activity and store in the DB.', 'cron' );
    wpdai_write_log( 'Collecting product IDs', 'cron' );
    
    // List of product Id's
    $product_ids = wpdai_query_product_ids_for_analytics_collector( 25 ); // Collect 25 products

    // Make sure the data is correct
    if ( is_array($product_ids) && ! empty($product_ids) ) {

        $product_count = count( $product_ids );
        $i = 1;

        // Entry logging
        wpdai_write_log( 'We have an array of product IDs, ' . $product_count . ' products found.', 'cron' );
        wpdai_write_log( 'Looping through product IDs to fetch product analytics.', 'cron' );

        foreach( $product_ids as $product_id ) {

            // False = force refresh data collection and update transients and meta
            $product_analytics = wpdai_fetch_product_analytics_by_product_id( $product_id, false );

            // Log iteration
            wpdai_write_log( '(' . $i . '/' . $product_count . ') Calculating analytics for product id: ' . $product_id . '.', 'cron' );

            if ( is_array($product_analytics) && ! empty( $product_analytics ) ) {

                wpdai_write_log( 'Update succesful.', 'cron' );
    
            }

            // Log the memory usage
            if ( wpdai_get_peak_memory_usage() > $memory_usage ) $memory_usage = wpdai_get_peak_memory_usage();

            // Iterator
            $i++;
    
        }

    } else {

        wpdai_write_log( 'We couldnt find a good data collection, the format wasnt an array or it was empty. Couldnt find product IDs? Check debug.log.', 'cron' );
        
    }

    $finish             = microtime( true );
	$total_time_elapsed = round( $finish - $start, 2 );

    wpdai_write_log( 'Completed wpd_schedule_product_analytics_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cron' );
    wpdai_write_log( 'Completed wpd_schedule_product_analytics_collector. Process took ' . $total_time_elapsed . ' seconds and ' . $memory_usage . ' memory usage.', 'cache' );

}

/**
 * 
 *  Check DB upgrade once daily 
 * 
 **/
function wpdai_schedule_database_upgrade_function() {

    wpdai_write_log( 'Executing wpd_schedule_database_upgrade', 'cron' );

    if ( ! class_exists('WPDAI_Database_Interactor') ) {
        require_once( WPD_AI_PATH . 'includes/classes/WPDAI_Database_Interactor.php');
    }

    $db_interactor = new WPDAI_Database_Interactor();

    if ( is_object( $db_interactor ) && method_exists( $db_interactor, 'create_update_tables_columns' ) ) {

        $db_interactor->create_update_tables_columns();

    }

    wpdai_write_log( 'Completed wpd_schedule_database_upgrade', 'cron' );

}

/**
 * 
 *  Daily task runner
 * 
 *  Runs a task once day that just checks for tasks that have not been run before
 * 
 *  This is for once off tasks, usually minor migration related things
 * 
 **/
function wpdai_schedule_daily_task_runner_once_off_function() {

    // Run the task runner class
    $task_runner = new WPDAI_Task_Runner();

    // Run incomplete tasks
    $task_runner->run_incomplete_tasks();

}

/**
 * 
 *  Gets rid of bot traffic, crawlers, and other dodgy looking data 
 * 
 **/
function wpdai_schedule_analytics_db_cleanup_function() {

    $days = 2;
    
    wpdai_write_log( 'Executing wpd_schedule_analytics_db_cleanup for the past ' . $days . ' days', 'cron' );

    // Clean up data from the last N days
    wpdai_cleanup_analytics_data( $days );

    wpdai_write_log( 'wpd_schedule_analytics_db_cleanup', 'cron' );


}

/**
 * 
 * 	Analytics Database Cleanup
 * 	
 * 	This function will scan the two analytics tables and remove anything that looks like spam, bots, crawlers or incomplete data.
 *  Will default to the last 30 days of data and can be overriden, typically used in the daily cron schedule to keep the DB clean
 * 
 * 	@param int $days_ago Number of days back to fetch through the database
 *  @return mixed Will return bool TRUE on succesful execution, otherwise will return a string with DB error if an error was triggered in execution
 *  @author WP Davies - Christopher Davies
 *  @since 2.0.50
 *  
 **/
function wpdai_cleanup_analytics_data( $days_ago = 30 ) {

	global $wpdb;

	// Collect Vars
	$wpd_db = new WPDAI_Database_Interactor();
	$woo_events_table = $wpd_db->events_table;
	$session_data_table = $wpd_db->session_data_table;

	if ( ! $days_ago ) $days_ago = 30;

	$days_ago_string = '-' . strval($days_ago) . ' days';

	wpdai_write_log( 'Cleaning up analytics data in the last ' . $days_ago_string, 'db_cleanup' );

	// Number of days to check
	$start_date = gmdate("Y-m-d H:i:s", strtotime($days_ago_string));

	// Prepare query using wpdb->prepare() for security
	$sql_query = $wpdb->prepare(
		"SELECT * FROM $session_data_table AS session_data WHERE date_created_gmt >= %s",
		$start_date
	);

	// Fetch Results
	$results = $wpdb->get_results( $sql_query, 'ARRAY_A' );

	// Sessions deleted
	$sessions_deleted = 0;

	if ( $wpdb->last_error  ) {

		wpdai_write_log( 'Error capturing analytics data from DB, dumping the error and query.', 'db_error' );
		wpdai_write_log( $wpdb->last_error, 'db_error' );
		wpdai_write_log( $wpdb->last_query, 'db_error' );

		return $wpdb->last_error;

	}

	// Do some manual cleaning on bots, crawlers, and incomplete sessions
	if ( is_array($results) && ! empty($results) ) {

		wpdai_write_log( 'Found ' . count( $results ) . ' sessions that we will check for bots, crawlers and incomplete data. ', 'db_cleanup' );

		foreach( $results as $row ) {

			$session_id = $row['session_id']; // Unique ID for deleting rows

			// Make sure we've got a session ID
			if ( empty($session_id) || ! is_string($session_id) ) {
				continue;
			}

			// Remove Bots & Crawlers if they got through
			if ( isset( $row['additional_data'] ) ) {

				// If the additional data we are checking contains the user agent data
				$additional_data = json_decode( $row['additional_data'], true );
				if ( isset($additional_data['raw_user_agent_data']) && ! empty($additional_data['raw_user_agent_data']) ) {

					$user_agent = strtolower( $additional_data['raw_user_agent_data'] );

					// Remove bots
					if ( strpos( $user_agent, 'bot') !== false ) {

						// Delete the results
						$wpdb->delete( $woo_events_table, array( 'session_id' => $session_id ) );
						$wpdb->delete( $session_data_table, array( 'session_id' => $session_id ) );
						$sessions_deleted++;
						continue;

					}

					// Remove crawlers
					if ( strpos( $user_agent, 'crawler') !== false ) {

						// Delete the results
						$wpdb->delete( $woo_events_table, array( 'session_id' => $session_id ) );
						$wpdb->delete( $session_data_table, array( 'session_id' => $session_id ) );
						$sessions_deleted++;
						continue;

					}

				}
			}

			// Remove all events without landing pages
			if ( array_key_exists('landing_page', $row) && empty($row['landing_page']) ) {
				$wpdb->delete( $woo_events_table, array( 'session_id' => $session_id ) );
				$wpdb->delete( $session_data_table, array( 'session_id' => $session_id ) );	
				$sessions_deleted++;
				continue;		
			}
		}
	} else {

		wpdai_write_log( 'Didnt find any data to cleanup, check the db_error log to see if there was an issue capturing data. ', 'db_cleanup' );

	}

	// Logging sessions deleted
	if ( $sessions_deleted > 0 ) {
		wpdai_write_log( $sessions_deleted . ' Sessions were deleted from your database.', 'db_cleanup' );
	} else {
		wpdai_write_log( 'No sessions were deleted from your database, everything looks clean.', 'db_cleanup' );
	}

	wpdai_write_log( 'Now we\'ll just remove any sessions or events that don\'t have corresponding data in the other table.', 'db_cleanup' );

	// Events stored that have no session data
	$sql_query = "SELECT session_id
	FROM $woo_events_table
	WHERE session_id NOT IN
		(SELECT session_id 
		FROM $session_data_table)
	AND date_created_gmt >= '$start_date' GROUP BY session_id";

	// Execute Query
	$results = $wpdb->get_results( $sql_query, 'ARRAY_A' );
	if ( $wpdb->last_error  ) {

		wpdai_write_log( 'Error capturing analytics data from DB, dumping the error and query.', 'db_error' );
		wpdai_write_log( $wpdb->last_error, 'db_error' );
		wpdai_write_log( $wpdb->last_query, 'db_error' );

		return $wpdb->last_error;

	}
	if ( is_array($results) && ! empty($results) ) {
		foreach( $results as $array_key => $row ) {
			$session_id = $row['session_id']; // Unique ID for deleting rows
			if ( ! empty($session_id) && is_string($session_id) ) {
				$wpdb->delete( $woo_events_table, array( 'session_id' => $session_id ) );
				$wpdb->delete( $session_data_table, array( 'session_id' => $session_id ) );	
			}
		}
	}

	// Session stored that has no event data
	$sql_query = "SELECT session_id
	FROM $session_data_table
	WHERE session_id NOT IN
		(SELECT session_id 
		FROM $woo_events_table)
	AND date_created_gmt >= '$start_date' GROUP BY session_id";

	// Execute Query
	$results = $wpdb->get_results( $sql_query, 'ARRAY_A' );
	if ( $wpdb->last_error  ) {

		wpdai_write_log( 'Error capturing analytics data from DB, dumping the error and query.', 'db_error' );
		wpdai_write_log( $wpdb->last_error, 'db_error' );
		wpdai_write_log( $wpdb->last_query, 'db_error' );

		return $wpdb->last_error;

	}
	if ( is_array($results) && ! empty($results) ) {
		foreach( $results as $array_key => $row ) {
			$session_id = $row['session_id']; // Unique ID for deleting rows
			if ( ! empty($session_id) && is_string($session_id) ) {
				$wpdb->delete( $woo_events_table, array( 'session_id' => $session_id ) );
				$wpdb->delete( $session_data_table, array( 'session_id' => $session_id ) );	
			}
		}
	}

	wpdai_write_log( 'Cleanup complete, your analytics database has now been cleaned.', 'db_cleanup' );

	return true;

}

/**
 * 
 *  Deletes any log files that are larger than 10mb - daily
 * 
 **/
function wpdai_schedule_log_cleanup_function() {

    // Log init
    wpdai_write_log( 'Executing wpd_schedule_log_cleanup', 'cron' );

    // Cleanup all logs larger than 10mb
    $deletion_count = wpdai_delete_large_logs( 10 );

    // Log count
    wpdai_write_log( $deletion_count . ' Log files were deleted.', 'cron' );

    // Log completion
    wpdai_write_log( 'Completed wpd_schedule_log_cleanup', 'cron' );

}

function wpdai_schedule_emails_function() {

	/**
	 *
	 *	Only take this seriously if we're at the 8th hour
	 *
	 */
	$site_time_hour     = (int) wpdai_site_date_time( 'H' );
	$site_day_of_week 	= wpdai_site_date_time( 'D' );
	$site_day_of_month 	= (int) wpdai_site_date_time( 'j' );
	$email_settings 	= get_option( 'wpd_ai_email_settings' );

    // Check if we've sent emails
    $daily_emails_sent      = get_option( 'wpd_ai_daily_emails_sent' ); // Check from date
    $weekly_emails_sent     = get_option( 'wpd_ai_weekly_emails_sent' ); // Check from date
    $monthly_emails_sent    = get_option( 'wpd_ai_monthly_emails_sent' ); // Check from date

    /**
     *
     *  Fix critical error
     *	Make convert_to_screen() available for WP_List class
     *
     */
    if ( ! function_exists('convert_to_screen') ) {
        require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    }
    if ( ! class_exists( 'WP_List_Table' ) ) {
        require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
    }

    /**
     *
     *	Daily Emails
     *
     */
	$daily_from_date 	= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'yesterday' );
	$daily_to_date 		= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'yesterday' );

    // Only send once
    if ( $daily_emails_sent != $daily_from_date ) {

    	// Send profit report
    	if ( isset( $email_settings['profit-report']['frequency']['daily'] ) && $email_settings['profit-report']['frequency']['daily'] ) {

    		wpdai_email( 'wpd_profit_report', false, array(

    			'from_date' => $daily_from_date,
    			'to_date' 	=> $daily_to_date,
    			/* translators: %s: Plugin or site name */
    			'subject' 	=> sprintf( __( 'Your Daily Profit Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

    			)

    		);

    	}

    	// Send inventory report
    	// if ( $email_settings['inventory-report']['frequency']['daily'] ) {

    	// 	wpdai_email( 'wpd_inventory_report', false, array(

    	// 		'from_date' => $daily_from_date,
    	// 		'to_date' 	=> $daily_to_date,
    	// 		'subject' 	=> sprintf( __( 'Your Daily Inventory Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

    	// 		) 
    	// 	);

    	// }

    	// Send expense report
    	if ( isset( $email_settings['expense-report']['frequency']['daily'] ) && $email_settings['expense-report']['frequency']['daily'] ) {

    		wpdai_email( 'wpd_expense_report', false, array(

    			'from_date' => $daily_from_date,
    			'to_date' 	=> $daily_to_date,
    			/* translators: %s: Plugin or site name */
    			'subject' 	=> sprintf( __( 'Your Daily Expense Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

    			) 
    		);

    	}

        // Save the day weve sent
        update_option( 'wpd_ai_daily_emails_sent', $daily_from_date );

    }

    /**
     *
     *	Weekly Email
     *
     */
    if ( $site_day_of_week === 'Mon' ) {

    	$weekly_from_date 	= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'Monday last week' );
		$weekly_to_date 	= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'Sunday last week' );

        // Only send once
        if (  $weekly_emails_sent != $weekly_from_date ) {

        	// Send profit report
        	if ( isset( $email_settings['profit-report']['frequency']['weekly'] ) && $email_settings['profit-report']['frequency']['weekly'] ) {

        		wpdai_email( 'wpd_profit_report', false, array(

        			'from_date' => $weekly_from_date,
        			'to_date' 	=> $weekly_to_date,
        			/* translators: %s: Plugin or site name */
        			'subject' 	=> sprintf( __( 'Your Weekly Profit Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

        			) 
        		);

        	}

        	// Send inventory report
        	// if ( $email_settings['inventory-report']['frequency']['weekly'] ) {

        	// 	wpdai_email( 'wpd_inventory_report', false, array(

        	// 		'from_date' => $weekly_from_date,
        	// 		'to_date' 	=> $weekly_to_date,
        	// 		'subject' 	=> sprintf( __( 'Your Weekly Inventory Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

        	// 		) 
        	// 	);

        	// }

        	// Send expense report
        	if ( isset( $email_settings['expense-report']['frequency']['weekly'] ) && $email_settings['expense-report']['frequency']['weekly'] ) {

        		wpdai_email( 'wpd_expense_report', false, array(

        			'from_date' => $weekly_from_date,
        			'to_date' 	=> $weekly_to_date,
        			/* translators: %s: Plugin or site name */
        			'subject' 	=> sprintf( __( 'Your Weekly Expense Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

        			) 
        		);

        	}

            // Save the day weve sent
            update_option( 'wpd_ai_weekly_emails_sent', $weekly_from_date );

        }

    } 

    /**
     *
     *	Monthly Email
     *
     */
    if ( $site_day_of_month === 1 ) {

		$monthly_from_date 	= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'first day of last month' );
		$monthly_to_date 	= wpdai_site_date_time( WPD_AI_PHP_ISO_DATE, 'last day of last month' );

        // Only send once
        if ( $monthly_emails_sent != $monthly_from_date ) {

        	// Send profit report
        	if ( isset( $email_settings['profit-report']['frequency']['monthly'] ) && $email_settings['profit-report']['frequency']['monthly'] ) {

        		wpdai_email( 'wpd_profit_report', false, array(

        			'from_date' => $monthly_from_date,
        			'to_date' 	=> $monthly_to_date,
        			/* translators: %s: Plugin or site name */
        			'subject' 	=> sprintf( __( 'Your Monthly Profit Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

        			) 
        		);

        	}

        	// Send expense report
        	if ( isset( $email_settings['expense-report']['frequency']['monthly'] ) && $email_settings['expense-report']['frequency']['monthly'] ) {

        		wpdai_email( 'wpd_expense_report', false, array(

        			'from_date' => $monthly_from_date,
        			'to_date' 	=> $monthly_to_date,
        			/* translators: %s: Plugin or site name */
        			'subject' 	=> sprintf( __( 'Your Monthly Expense Report From %s', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ), 'Alpha Insights' ),

        			) 
        		);

        	}

            // Save the day weve sent
            update_option( 'wpd_ai_monthly_emails_sent', $monthly_from_date );

        }

    }

}

/**
 * 
 * 	Enriches DB table for page_view events that don't have an object ID or type
 * 
 * 	@return bool|int Returns false on failure, or count of updates on success.
 * 
 **/
function wpdai_set_post_id_post_type_on_null_events_analytics_table() {

	// Load vars
	global $wpdb;
	$wpd_db 			= new WPDAI_Database_Interactor();
	$woo_events_table 	= $wpd_db->events_table;
	$count_of_updates 	= 0;
	$limit 				= 5000; // Roughly 10s
	
	// Prepare query
	$sql_query = 
		"SELECT ID, page_href, object_type, object_id
		FROM {$woo_events_table}
		WHERE 1=1
		AND object_id = 0
		AND event_type = 'page_view'
		AND object_type = ''
		ORDER BY date_created_gmt DESC
		LIMIT $limit";

	// Fetch Results
	$results = $wpdb->get_results( $sql_query, 'ARRAY_A' );

	// DB Error
	if ( $wpdb->last_error  ) {
		wpdai_write_log( 'Error updating the post ID\'s for null values in the analytics table.', 'db_error' );
		wpdai_write_log( $wpdb->last_error, 'db_error' );
		wpdai_write_log( $wpdb->last_query, 'db_error' );
		return false;
	}

	// If we've got results
	if ( is_array($results) && ! empty($results) ) {
		
		foreach( $results as $page_view ) {

			// Fetch the data
			$row_id 	= $page_view['ID'];
			$page_href 	= sanitize_url( $page_view['page_href'] );
			$post_id 	= url_to_postid($page_href);

			// We've found the object
			if ( $post_id ) {

				// Get the post type
				$post_type = get_post_type($post_id);

				// Update table row
				$update = $wpdb->update( 
					$woo_events_table, 
					array( 'object_id' => $post_id, 'object_type' => $post_type ),
					array( 'ID' => $row_id ),
				);

				// Iterate success counter
				if ( $update ) $count_of_updates++;

			}

		}

	}

	// Return count of updates
	return $count_of_updates;

}