PHP File Manager
Editing File: Tasks.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Tasks extends CI_Model { private $log = array(); public function checkAndAddRecurringOrders($skip_run_check = false) { $this->clearLog(); $this->load->model('basket'); $run_date = time(); $next_run_date = strtotime('+1 day') - 1800; // Take off 30 minutes $todays_date = strtotime('00:00:00'); $tomorrows_date = strtotime('+1 day', $todays_date); $tomorrows_date_info = getdate($tomorrows_date); $tomorrows_date_info['wday'] = ($tomorrows_date_info['wday'] === 0) ? 7 : $tomorrows_date_info['wday']; // Make Sunday 7 not 0 $tomorrows_date_info['wnum'] = week_number($tomorrows_date); $recurring_orders = $this->orders->get(NULL, NULL, array ( 'recurring' => true, 'skip_finished_recurs' => true )); $this->log('Number of recurring orders found: ' . count($recurring_orders)); foreach($recurring_orders as $order_info) { $recurring_id = $order_info->recurring_id; $recur_hash = md5($order_info->order_number . ':' . date('dmY', $tomorrows_date)); $this->log('Order #' . $order_info->order_number . ' with recurring ID: ' . $recurring_id . '...'); if(!$skip_run_check && $order_info->next_run !== NULL && $order_info->next_run > time()) { $this->log('Cannot process order till ' . date('jS M Y - H:i', $order_info->next_run)); continue; } if($skip_run_check == 'skip-checks') { $next_run_date = strtotime('tomorrow 2:00:00'); } $this->db->query($this->db->update_string('recurring_orders', array('last_run' => $run_date, 'next_run' => $next_run_date), "`id` = $recurring_id")); $can_add_order = false; if($order_info->repeat_from !== NULL && $order_info->repeat_from < time()) { $this->log('Cannot process this until ' . date('jS M Y', $order_info->repeat_from)); continue; } if($order_info->repeat_till !== NULL && $order_info->repeat_till < $todays_date) { $this->log('Order will stop recurring from now on'); $this->db->query($this->db->update_string('recurring_orders', array('is_finished' => 1), "`id` = $recurring_id OR `parent_id` = $recurring_id")); continue; } $repeat_date = array_filter(explode(',', $order_info->repeat_date)); $repeat_day = array_filter(explode(',', $order_info->repeat_day)); $repeat_week = array_filter(explode(',', $order_info->repeat_week)); $repeat_month = array_filter(explode(',', $order_info->repeat_month)); if($repeat_date) { if(!in_array($tomorrows_date_info['mday'], $repeat_date)) { $this->log('Cannot place order for date: ' . date_suffix($tomorrows_date_info['mday'])); continue; } } if($repeat_day) { if(!in_array($tomorrows_date_info['wday'], $repeat_day)) { $this->log('Cannot place order for day: ' . $tomorrows_date_info['weekday']); continue; } } if($repeat_week) { if(!in_array($tomorrows_date_info['wnum'], $repeat_week)) { $this->log('Cannot place order for week: ' . date_suffix($tomorrows_date_info['wnum'])); continue; } } if($repeat_month) { if(!in_array($tomorrows_date_info['mon'], $repeat_month)) { $this->log('Cannot place order for month: ' . date_suffix($tomorrows_date_info['mon'])); continue; } } if($this->db->query("SELECT 1 FROM `orders` WHERE `is_deleted` = 0 AND `recur_identifier` = '$recur_hash' LIMIT 1")->num_rows() === 1) { $this->log('Order already exists for tomorrow!'); continue; } $order_response = $this->orders->copyOrder($order_info->order_id, date('d-m-Y', $tomorrows_date), $recurring_id); $this->log('Result: ' . (($order_response) ? 'Order was successfully placed' : 'Order could not be placed: ' . $this->common->responseMessage)); } } private function log($message) { $this->log[] = $message; } public function getLog() { return $this->log; } public function clearLog() { $this->log = array(); } }
Cancel