/**
 * Determine if an employee is working on the given date.
 *
 * @since	1.3
 * @param	str			$date		The date
 * @param	int			$employee	The employee ID
 * @param	str|arr		$status		The employee ID
 * @return	bool		True if the employee is working, otherwise false.
 */
function mdjm_employee_is_working($date, $employee_id = '', $status = '')
{
    if (empty($employee_id) && is_user_logged_in()) {
        $employee_id = get_current_user_id();
    }
    if (empty($employee_id)) {
        wp_die(__('Ooops, an error occured.', 'mobile-dj-manager'));
    }
    if (empty($status)) {
        $status = mdjm_get_option('availability_status', 'any');
    }
    $event = mdjm_get_events(array('post_status' => $status, 'posts_per_page' => 1, 'meta_key' => '_mdjm_event_date', 'meta_value' => date('Y-m-d', $date), 'meta_query' => array('relation' => 'OR', array('key' => '_mdjm_event_dj', 'value' => $employee_id, 'compare' => '=', 'type' => 'NUMERIC'), array('key' => '_mdjm_event_employees', 'value' => sprintf(':"%s";', $employee_id), 'compare' => 'LIKE'))));
    $event = apply_filters('mdjm_employee_is_working', $event, $date, $employee_id);
    if ($event) {
        return true;
    }
    return false;
}
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $employees = mdjm_get_employees();
         foreach ($employees as $employee) {
             $event_count = 0;
             $employee_id = $employee->ID;
             $event_args = array('post_status' => apply_filters('mdjm_events_by_type_statuses', array('mdjm-contract', 'mdjm-approved', 'mdjm-completed')), 'fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN'), array('relation' => 'OR', array('key' => '_mdjm_event_dj', 'value' => $employee_id, 'compare' => '=', 'type' => 'numeric'), array('key' => '_mdjm_event_employees', 'value' => sprintf(':"%s";', $employee_id), 'compare' => 'LIKE'))));
             $wages = 0.0;
             $paid = 0.0;
             $owed = 0.0;
             $events = mdjm_get_events($event_args);
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $payment_status = mdjm_event_employees_paid($event, $employee_id);
                     $current_wages = mdjm_get_employees_event_wage($event, $employee_id);
                     $current_paid_wages = 0;
                     $current_owed_wages = 0;
                     $wages += $current_wages;
                     $paid += $payment_status ? $current_wages : 0.0;
                     $owed += $payment_status ? 0.0 : $current_wages;
                 }
             } else {
                 continue;
             }
             $reports_data[] = array('ID' => $employee->ID, 'employee' => $employee->display_name, 'total_events' => $event_count, 'total_wages' => mdjm_currency_filter(mdjm_format_amount($wages)), 'total_wages_raw' => $wages, 'wages_paid' => mdjm_currency_filter(mdjm_format_amount($paid)), 'wages_owed' => mdjm_currency_filter(mdjm_format_amount($owed)), 'is_child' => false);
         }
     }
     return $reports_data;
 }
 /**
  * Retrieves the count of enquiry sources over given date period.
  *
  * @since	1.4
  * @param	str		$period		The date period for which to collect the stats
  * @param	int		$status		The transaction status' for which to collect the stats
  * @return	int		$total		The total value for all transactions that meet the criteria
  */
 public function get_enquiry_sources_by_date($period = 'this_week')
 {
     $sources = get_terms(array('taxonomy' => 'enquiry-source', 'hide_empty' => true));
     $this->setup_dates($period);
     $tax_count = array();
     if (!empty($sources)) {
         foreach ($sources as $source) {
             $tax_query = array('taxonomy' => 'enquiry-source', 'terms' => $source->term_id);
             $args = array('date_query' => array(array('after' => array('year' => date('Y', $this->start_date), 'month' => date('n', $this->start_date), 'day' => date('d', $this->start_date)), 'before' => array('year' => date('Y', $this->end_date), 'month' => date('n', $this->end_date), 'day' => date('d', $this->end_date)), 'inclusive' => true)), 'tax_query' => array($tax_query));
             $events = mdjm_get_events($args);
             if ($events) {
                 $tax_count[count($events)] = $source->name;
             }
         }
     }
     krsort($tax_count);
     return $tax_count;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $packages = mdjm_get_packages();
         if ($packages) {
             foreach ($packages as $package) {
                 $event_count = 0;
                 $total_value = 0;
                 $event_args = array('fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => '_mdjm_event_package', 'value' => $package->ID, 'type' => 'NUMERIC'), array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN')));
                 $events = mdjm_get_events($event_args);
                 if ($events) {
                     foreach ($events as $event) {
                         $event_count++;
                         $event_date = get_post_meta($event, '_mdjm_event_date', true);
                         $total_value += mdjm_get_package_price($package->ID, $event_date);
                     }
                 } else {
                     continue;
                 }
                 $reports_data[] = array('ID' => $package->ID, 'package' => mdjm_get_package_name($package->ID), 'events' => $event_count, 'value' => mdjm_currency_filter(mdjm_format_amount($total_value)), 'value_raw' => $total_value);
             }
         }
     }
     return $reports_data;
 }
 /**
  * Retrieve events filtered by employee, client, date or status.
  *
  * @since	1.4
  * @return	void
  */
 public function list_events()
 {
     $response = array();
     if (!mdjm_employee_can('read_events', $this->user_id)) {
         $this->no_permsission();
     }
     if (!isset($this->request['employee_id']) && !mdjm_employee_can('read_events_all', $this->user_id)) {
         $this->no_permsission();
     }
     do_action('mdjm_before_api_event_list', $this);
     if (isset($this->request['employee_id'])) {
         $events = mdjm_get_employee_events($this->request['employee_id']);
     } elseif (isset($this->request['client_id'])) {
         $events = mdjm_get_client_events($this->request['client_id']);
     } elseif (isset($this->request['date'])) {
         $events = mdjm_get_events_by_date($this->request['date']);
     } elseif (isset($this->request['status'])) {
         $events = mdjm_get_events_by_status($this->request['status']);
     } else {
         $events = mdjm_get_events();
     }
     if (!$events) {
         $error = array();
         $error['error'] = sprintf(__('No %s found.', 'mobile-dj-manager'), mdjm_get_label_plural(true));
         $this->data = $error;
         $this->output();
     }
     $response['events'] = array();
     $i = 0;
     foreach ($events as $event) {
         $response['events'][$event->ID] = mdjm_get_event_data($event->ID);
         $i++;
     }
     $response['count'] = $i;
     $this->data = array_merge($this->data, $response);
     do_action('mdjm_after_api_event_list', $this);
     $this->output();
 }
 public function fail_enquiry()
 {
     global $mdjm, $mdjm_settings;
     if (MDJM_DEBUG == true) {
         MDJM()->debug->log_it('*** Starting the Fail Enquiry task ***', true);
     }
     $cron_start = microtime(true);
     $expired = date('Y-m-d', strtotime("-" . $this->schedules['fail-enquiry']['options']['age']));
     // Retrieve expired enquiries
     $enquiries = mdjm_get_events(array('post_status' => 'mdjm-enquiry', 'date_query' => array('before' => $expired)));
     $notify = array();
     $x = 0;
     if (count($enquiries) > 0) {
         // Enquiries to process
         MDJM()->debug->log_it(count($enquiries) . ' ' . _n('enquiry', 'enquiries', count($enquiries)) . ' to expire');
         remove_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3);
         /* -- Loop through the enquiries and update as failed -- */
         foreach ($enquiries as $enquiry) {
             $cronned = get_post_meta($event->ID, '_mdjm_event_tasks', true);
             if (!empty($cronned) && $cronned != '') {
                 $cron_update = json_decode($cronned, true);
             }
             if (array_key_exists('request-deposit', $cron_update)) {
                 // Task has already run for this event
                 continue;
             }
             if (!is_array($cron_update)) {
                 $cron_update = array();
             }
             $cron_update[$this->schedules['fail-enquiry']['slug']] = time();
             mdjm_update_event_status($enquiry->ID, 'mdjm-failed', $enquiry->post_status, array('meta' => array('_mdjm_event_tasks' => json_encode($cron_update))));
             /* -- Update Journal -- */
             MDJM()->debug->log_it('	-- Adding journal entry');
             mdjm_add_journal(array('user_id' => 1, 'event_id' => $enquiry->ID, 'comment_content' => 'Enquiry marked as lost via Scheduled Task <br /><br />' . time()), array('type' => 'update-event', 'visibility' => '1'));
             $notify_dj = isset($this->schedules['fail-enquiry']['options']['notify_dj']) ? $this->schedules['fail-enquiry']['options']['notify_dj'] : '';
             $notify_admin = isset($this->schedules['fail-enquiry']['options']['notify_admin']) ? $this->schedules['fail-enquiry']['options']['notify_admin'] : '';
             $client = get_post_meta($enquiry->ID, '_mdjm_event_client', true);
             $dj = get_post_meta($enquiry->ID, '_mdjm_event_dj', true);
             $event_date = get_post_meta($enquiry->ID, '_mdjm_event_date', true);
             $event_dj = !empty($dj) ? get_userdata($dj) : 'DJ not found';
             $event_client = !empty($client) ? get_userdata($client) : 'Client not found';
             $venue_post_id = get_post_meta($enquiry->ID, '_mdjm_event_venue_id', true);
             $event_venue = MDJM()->events->mdjm_get_venue_details($venue_post_id, $enquiry->ID);
             /* Prepare admin notification email data array */
             if (!empty($notify_admin) && $notify_admin == 'Y') {
                 MDJM()->debug->log_it('	-- Admin notifications are enabled');
                 if (!isset($notify['admin']) || !is_array($notify['admin'])) {
                     $notify['admin'] = array();
                 }
                 $notify['admin'][$enquiry->ID] = array('id' => $enquiry->ID, 'client' => $event_client->display_name, 'venue' => !empty($event_venue['name']) ? $event_venue['name'] : 'No Venue Set', 'djinfo' => $event_dj, 'date' => !empty($event_date) ? date("d M Y", strtotime($event_date)) : 'Date not found');
             }
             // End if( !empty( $notify_admin ) && $notify_admin == 'Y' )
             /* Prepare DJ notification email data array */
             if (!empty($notify_dj) && $notify_dj == 'Y') {
                 MDJM()->debug->log_it('	-- DJ notifications are enabled');
                 if (!isset($notify['dj']) || !is_array($notify['dj'])) {
                     $notify['dj'] = array();
                 }
                 $notify['dj'][$dj] = array();
                 $notify['dj'][$dj][$enquiry->ID] = array('id' => $enquiry->id, 'client' => $event_client->display_name, 'venue' => !empty($event_venue['name']) ? $event_venue['name'] : 'No Venue Set', 'djinfo' => $event_dj, 'date' => !empty($event_date) ? date("d M Y", strtotime($event_date)) : 'Date not found');
             }
             // End if( !empty( $notify_dj ) && $notify_dj == 'Y' )
             $x++;
         }
         // End foreach
         $cron_end = microtime(true);
         /* -- Prepare the Admin notification email -- */
         if (!empty($notify_admin) && $notify_admin == 'Y') {
             $notify_email_args = array('data' => $notify['admin'], 'taskinfo' => $this->schedules['fail-enquiry'], 'start' => $cron_start, 'end' => $cron_end, 'total' => $x);
             // $notify_email_args
             $content = $this->notification_content($notify_email_args);
             $mdjm->send_email(array('content' => $content, 'to' => $mdjm_settings['email']['system_email'], 'subject' => sanitize_text_field($this->schedules['fail-enquiry']['options']['email_subject']), 'journal' => false, 'html' => false, 'cc_admin' => false, 'cc_dj' => false, 'filter' => false, 'log_comm' => false));
         }
         // if( !empty( $notify_admin ) && $notify_admin == 'Y' )	{
         /* -- Prepare the DJ notification email -- */
         if (!empty($notify_dj) && $notify_dj == 'Y') {
             foreach ($notify['dj'] as $notify_dj) {
                 foreach ($notify_dj as $dj) {
                     $notify_email_args = array('data' => $notify_dj, 'taskinfo' => $this->schedules['fail-enquiry'], 'start' => $cron_start, 'end' => $cron_end, 'total' => $x);
                     // $notify_email_args
                     $content = $this->notification_content($notify_email_args);
                     $mdjm->send_email(array('content' => $content, 'to' => $dj->ID, 'subject' => sanitize_text_field($this->schedules['fail-enquiry']['options']['email_subject']), 'journal' => false, 'html' => false, 'cc_admin' => false, 'cc_dj' => false, 'filter' => false, 'log_comm' => false));
                 }
                 // foreach( $notify_dj as $dj )
             }
             // foreach( $notify['dj'] as $notify_dj )
         }
         // if( !empty( $notify_dj ) && $notify_dj == 'Y' )
         add_action('save_post_mdjm-event', 'mdjm_save_event_post', 10, 3);
     } else {
         MDJM()->debug->log_it('No enquiries to process as failed');
     }
     // Prepare next run time
     $this->update_nextrun('fail-enquiry');
     if (MDJM_DEBUG == true) {
         MDJM()->debug->log_it('*** Completed the Fail Enquiry task ***', true);
     }
 }
/**
 * Retrieve the client's next event.
 *
 * @since	1.3
 * @param	int	$client_id	The user ID for the client.
 * @return	WP_Post object for clients next event, or false
 */
function mdjm_get_clients_next_event($client_id = '')
{
    $client_id = !empty($client_id) ? $client_id : get_current_user_id();
    $args = array('post_status' => array('mdjm-approved', 'mdjm-contract', 'mdjm-enquiry', 'mdjm-unattended'), 'posts_per_page' => 1, 'meta_key' => '_mdjm_event_date', 'meta_query' => array('relation' => 'AND', array('key' => '_mdjm_event_client', 'value' => $client_id), array('key' => '_mdjm_event_date', 'value' => date('Y-m-d'), 'compare' => '>=', 'type' => 'date')), 'orderby' => 'meta_value', 'order' => 'ASC');
    $next_event = mdjm_get_events($args);
    return apply_filters('mdjm_get_clients_next_event', $next_event);
}
/**
 * Retrieve all events with the given addon(s).
 *
 * @since	1.4
 * @param	int|arr		$addon_ids		ID(s) of addons to look for.
 * @return	mixed		Array of WP_Post objects or false.
 */
function mdjm_get_events_with_addons($addon_ids)
{
    if (!is_array($addon_ids)) {
        $addon_ids = array($addon_ids);
    }
    $meta_query = array();
    foreach ($addon_ids as $addon_id) {
        $meta_query[] = array('key' => '_mdjm_event_addons', 'value' => sprintf(':"%s";', $addon_id), 'compare' => 'LIKE');
    }
    $args['meta_query'] = array('relation' => 'OR', $meta_query);
    return mdjm_get_events($args);
}
/**
 * Retrieve events by status.
 *
 * @since	1.3
 * @param	str			$status		The event status.
 * @return	obj|bool	The WP_Query results object.
 */
function mdjm_get_events_by_status($status)
{
    $events = mdjm_get_events(array('post_status' => $status));
    if (!$events) {
        return false;
    }
    return $events;
}
Example #10
0
/**
 * Hide inactive events from the 'all' events list.
 *
 * @since	1.0
 * @param	obj		$query	The WP_Query.
 * @return	void
 */
function mdjm_hide_inactive_events($query)
{
    if (!is_admin() || !$query->is_main_query() || 'mdjm-event' != $query->get('post_type')) {
        return;
    }
    $active_statuses = mdjm_active_event_statuses();
    if (isset($_GET['post_status']) && in_array($_GET['post_status'], $active_statuses)) {
        return;
    }
    if (!mdjm_get_option('show_active_only', false)) {
        return;
    }
    $active_ids = mdjm_get_events(array('post_status' => $active_statuses, 'fields' => 'ids'));
    if ($active_ids) {
        $query->set('post__in', $active_ids);
    }
}
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('enquiry-source', $term_args);
         foreach ($categories as $category_id => $category) {
             $conversions = 0;
             $ratio = 0;
             $event_count = 0;
             $category_slugs = array($category->slug);
             $all_event_args = array('post_status' => 'any', 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'enquiry-source', 'field' => 'slug', 'terms' => $category_slugs)), 'date_query' => array(array('after' => date('Y-m-d', $stats->start_date), 'before' => date('Y-m-d', $stats->end_date), 'inclusive' => true)));
             $value = 0.0;
             $events = mdjm_get_events($all_event_args);
             $statuses = mdjm_active_event_statuses();
             $statuses[] = 'mdjm-completed';
             $statuses[] = 'mdjm-cancelled';
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $mdjm_event = new MDJM_Event($event);
                     if (in_array($mdjm_event->post_status, $statuses)) {
                         $current_value = $mdjm_event->get_total_profit();
                         $value += $current_value;
                     }
                 }
             } else {
                 continue;
             }
             $conversions += $stats->get_conversions($events, 'enquiry-source', $category_slugs, $stats->start_date, $stats->end_date);
             $ratio = round((double) ($conversions / $event_count) * 100);
             $reports_data[] = array('ID' => $category->term_id, 'source' => $category->name, 'total_events' => $event_count, 'total_conversions' => $conversions, 'conversion_ratio' => $ratio . '%', 'total_value' => mdjm_currency_filter(mdjm_format_amount($value)), 'total_value_raw' => $value, 'is_child' => false);
             $this->total_enquiries += $event_count;
             $this->total_value += $value;
             $this->total_conversions += $conversions;
         }
     }
     return $reports_data;
 }
 /**
  * Build all the reports data
  *
  * @access	public
  * @since	1.4
  * @return	arr		$reports_data	All the data for customer reports
  */
 public function reports_data()
 {
     $stats = new MDJM_Stats();
     $dates = mdjm_get_report_dates();
     $stats->setup_dates($dates['range']);
     $cached_reports = false;
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('event-types', $term_args);
         foreach ($categories as $category_id => $category) {
             $event_count = 0;
             $category_slugs = array($category->slug);
             $all_event_args = array('post_status' => apply_filters('mdjm_events_by_type_statuses', array('mdjm-contract', 'mdjm-approved', 'mdjm-completed')), 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'event-types', 'field' => 'slug', 'terms' => $category_slugs)), 'meta_query' => array(array('key' => '_mdjm_event_date', 'value' => array(date('Y-m-d', $stats->start_date), date('Y-m-d', $stats->end_date)), 'type' => 'date', 'compare' => 'BETWEEN')));
             $earnings = 0.0;
             $avg_earnings = 0.0;
             $events = mdjm_get_events($all_event_args);
             if ($events) {
                 foreach ($events as $event) {
                     $event_count++;
                     $mdjm_event = new MDJM_Event($event);
                     $current_average_earnings = $current_earnings = $mdjm_event->get_total_profit();
                     $received_date = get_post_field('post_date', $event);
                     $diff = abs(current_time('timestamp') - strtotime($received_date));
                     $months = floor($diff / (30 * 60 * 60 * 24));
                     // Number of months since publication
                     if ($months > 0) {
                         $current_average_earnings = $current_earnings / $months;
                         //$current_average_events      = ( $events / $months );
                     }
                     $earnings += $current_earnings;
                     $avg_earnings += $current_average_earnings;
                 }
             } else {
                 continue;
             }
             $avg_earnings = round($avg_earnings / $event_count, mdjm_currency_decimal_filter());
             $reports_data[] = array('ID' => $category->term_id, 'type' => $category->name, 'total_events' => $event_count, 'total_earnings' => mdjm_currency_filter(mdjm_format_amount($earnings)), 'total_earnings_raw' => $earnings, 'avg_earnings' => mdjm_currency_filter(mdjm_format_amount($avg_earnings)), 'is_child' => false);
         }
     }
     return $reports_data;
 }