public static function dashboard($atts = array())
 {
     do_action('sprout_invoices_dashboard');
     // User not logged in yet.
     if (self::show_login_page()) {
         do_action('sprout_invoices_dashboard_not_logged_in');
         return self::login_form();
         return;
     }
     $user_id = 0;
     $valid_client_ids = self::validate_token();
     if (isset($_GET[self::USER_QUERY_ARG]) && $valid_client_ids) {
         $user_id = (int) $_GET[self::USER_QUERY_ARG];
         $client_ids = $valid_client_ids;
     } elseif (is_user_logged_in()) {
         $user_id = get_current_user_id();
     }
     if ($user_id) {
         if (empty($client_ids)) {
             $client_ids = SI_Client::get_clients_by_user($user_id);
         }
         if (!empty($client_ids)) {
             $view = '';
             // show a dashboard for each client associated.
             foreach ($client_ids as $client_id) {
                 $view .= self::dashboard_view($client_id, $atts);
             }
             return $view;
         }
     }
     // no client associated
     do_action('sprout_invoices_dashboard_not_client');
     return self::blank_dashboard_view();
 }
 public static function user_creation_notification($user_id, $args = array())
 {
     $client_ids = SI_Client::get_clients_by_user($user_id);
     $client = !empty($client_ids) ? SI_Client::get_instance($client_ids[0]) : 0;
     $to = self::get_user_email($user_id);
     $data = array('user_id' => $user_id, 'client' => $client, 'user_args' => $args, 'to' => $to);
     self::send_notification('user_notification', $data, $to);
 }
 /**
  * Query sa_client posts with assigned user ID
  *
  * @param 	(int) $user_id 	wp_user ID
  * @return 	(array) 		sa_client post ID's
  **/
 public static function get_client_si_projects($user_id = 0)
 {
     $project_ids = array();
     if (!$user_id) {
         $user_id = get_current_user_id();
     }
     if (!$user_id) {
         return $project_ids;
     }
     $client_ids = SI_Client::get_clients_by_user($user_id);
     if (empty($client_ids)) {
         return $project_ids;
     }
     foreach ($client_ids as $client_id) {
         $project_ids[] = SI_Project::get_projects_by_client($client_id);
     }
     return $project_ids;
 }
 /**
  * Checks to see if the user is accessing a post they have access to as part of an sa_client
  * @param  boolean $result Current pass/fail of access
  * @param  integer $psp_id The project id of the current viewed psp project
  * @return boolean         Access or not.
  */
 public static function check_access_on_post($result = false, $psp_id = 0)
 {
     $current_user_id = get_current_user_id();
     $user_associated_clients = SI_Client::get_clients_by_user($current_user_id);
     // If the result is true, then nothing will override it below
     // Plus if there are not associated clients than tough luck.
     if ($result || empty($user_associated_clients)) {
         return $result;
     }
     $si_project_id = get_field(self::META_KEY, $psp_id);
     // what si_project is assigned to this project access
     if (!$si_project_id || '' === $si_project_id) {
         return false;
     }
     $si_project = SI_Project::get_instance($si_project_id);
     $project_associated_clients = $si_project->get_associated_clients();
     $matches = array_intersect($project_associated_clients, $user_associated_clients);
     // If there are no matches
     if (empty($matches)) {
         return false;
     }
     // there were matches
     return true;
 }
 /**
  * Hooked into the estimate submission form. Create a client
  * if one already doesn't exist.
  * @param  SI_Estimate $estimate
  * @param  array       $parsed_args
  * @return
  */
 public static function create_client_from_submission(SI_Estimate $estimate, $parsed_args = array())
 {
     $client_id = isset($_REQUEST['client_id']) && get_post_type($_REQUEST['client_id']) == SI_Client::POST_TYPE ? $_REQUEST['client_id'] : 0;
     $user_id = get_current_user_id();
     // check to see if the user exists by email
     if (isset($_REQUEST['sa_estimate_email']) && $_REQUEST['sa_estimate_email'] != '') {
         if ($user = get_user_by('email', $_REQUEST['sa_estimate_email'])) {
             $user_id = $user->ID;
         }
     }
     // Check to see if the user is assigned to a client already
     if (!$client_id) {
         $client_ids = SI_Client::get_clients_by_user($user_id);
         if (!empty($client_ids)) {
             $client_id = array_pop($client_ids);
         }
     }
     // Create a user for the submission if an email is provided.
     if (!$user_id) {
         // email is critical
         if (isset($_REQUEST['sa_estimate_email']) && $_REQUEST['sa_estimate_email'] != '') {
             $user_args = array('user_login' => esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices'), 'display_name' => isset($_REQUEST['sa_estimate_client_name']) ? esc_html($_REQUEST['sa_estimate_client_name'], 'sprout-invoices') : esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices'), 'user_pass' => wp_generate_password(), 'user_email' => isset($_REQUEST['sa_estimate_email']) ? esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices') : '', 'first_name' => si_split_full_name(esc_html($_REQUEST['sa_estimate_name'], 'sprout-invoices'), 'first'), 'last_name' => si_split_full_name(esc_html($_REQUEST['sa_estimate_name'], 'sprout-invoices'), 'last'), 'user_url' => isset($_REQUEST['sa_estimate_website']) ? esc_html($_REQUEST['sa_estimate_website'], 'sprout-invoices') : '');
             $user_id = self::create_user($user_args);
         }
     }
     // create the client based on what's submitted.
     if (!$client_id) {
         $address = array('street' => isset($_REQUEST['sa_contact_street']) ? esc_html($_REQUEST['sa_contact_street'], 'sprout-invoices') : '', 'city' => isset($_REQUEST['sa_contact_city']) ? esc_html($_REQUEST['sa_contact_city'], 'sprout-invoices') : '', 'zone' => isset($_REQUEST['sa_contact_zone']) ? esc_html($_REQUEST['sa_contact_zone'], 'sprout-invoices') : '', 'postal_code' => isset($_REQUEST['sa_contact_postal_code']) ? esc_html($_REQUEST['sa_contact_postal_code'], 'sprout-invoices') : '', 'country' => isset($_REQUEST['sa_contact_country']) ? esc_html($_REQUEST['sa_contact_country'], 'sprout-invoices') : '');
         $args = array('company_name' => isset($_REQUEST['sa_estimate_client_name']) ? esc_html($_REQUEST['sa_estimate_client_name'], 'sprout-invoices') : '', 'website' => isset($_REQUEST['sa_estimate_website']) ? esc_html($_REQUEST['sa_estimate_website'], 'sprout-invoices') : '', 'address' => $address, 'user_id' => $user_id);
         $client_id = SI_Client::new_client($args);
     }
     // Set the estimates client
     $estimate->set_client_id($client_id);
 }
 /**
  * Maybe create a client from submission
  * @param  SI_Estimate $estimate 
  * @param  array       $args     * email - required
  *                               * client_id - if client_id is passed than just assign estimate
  *                               * client_name - required
  *                               * full_name - 
  *                               * website
  *                               * contact_street
  *                               * contact_city
  *                               * contact_zone
  *                               * contact_postal_code
  *                               * contact_country
  *                               
  */
 public static function maybe_create_client(SI_Estimate $estimate, $args = array())
 {
     $args = apply_filters('si_afi_maybe_create_client', $args);
     $client_id = isset($args['client_id']) && get_post_type($args['client_id']) == SI_Client::POST_TYPE ? $args['client_id'] : 0;
     $user_id = get_current_user_id();
     // check to see if the user exists by email
     if (isset($args['email']) && $args['email'] != '') {
         if ($user = get_user_by('email', $args['email'])) {
             $user_id = $user->ID;
         }
     }
     // Check to see if the user is assigned to a client already
     if (!$client_id) {
         $client_ids = SI_Client::get_clients_by_user($user_id);
         if (!empty($client_ids)) {
             $client_id = array_pop($client_ids);
         }
     }
     // Create a user for the submission if an email is provided.
     if (!$user_id) {
         // email is critical
         if (isset($args['email']) && $args['email'] != '') {
             $user_args = array('user_login' => self::esc__($args['email']), 'display_name' => isset($args['client_name']) ? self::esc__($args['client_name']) : self::esc__($args['email']), 'user_pass' => wp_generate_password(), 'user_email' => isset($args['email']) ? self::esc__($args['email']) : '', 'first_name' => si_split_full_name(self::esc__($args['full_name']), 'first'), 'last_name' => si_split_full_name(self::esc__($args['full_name']), 'last'), 'user_url' => isset($args['website']) ? self::esc__($args['website']) : '');
             $user_id = SI_Clients::create_user($user_args);
         }
     }
     // create the client based on what's submitted.
     if (!$client_id) {
         $address = array('street' => isset($args['contact_street']) ? self::esc__($args['contact_street']) : '', 'city' => isset($args['contact_city']) ? self::esc__($args['contact_city']) : '', 'zone' => isset($args['contact_zone']) ? self::esc__($args['contact_zone']) : '', 'postal_code' => isset($args['contact_postal_code']) ? self::esc__($args['contact_postal_code']) : '', 'country' => isset($args['contact_country']) ? self::esc__($args['contact_country']) : '');
         $args = array('company_name' => isset($args['client_name']) ? self::esc__($args['client_name']) : '', 'website' => isset($args['website']) ? self::esc__($args['website']) : '', 'address' => $address, 'user_id' => $user_id);
         $client_id = SI_Client::new_client($args);
         // History
         do_action('si_new_record', sprintf('Client Created & Assigned: %s', get_the_title($client_id)), self::SUBMISSION_UPDATE, $estimate->get_id(), sprintf('Client Created & Assigned: %s', get_the_title($client_id)), 0, false);
     }
     // Set the estimates client
     $estimate->set_client_id($client_id);
 }
 /**
  * Process the payment and return the result
  *
  * @param int $order_id
  * @return array
  */
 public function process_payment($order_id)
 {
     $order = wc_get_order($order_id);
     ///////////////////////////
     // Create Sprout Invoice //
     ///////////////////////////
     /**
      * Build line item array
      * @var array
      */
     $line_items = array();
     $tax = 0;
     foreach ($order->get_items() as $key => $item) {
         $_product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item);
         if ($_product && !$_product->is_visible()) {
             $desc = apply_filters('woocommerce_order_item_name', $item['name'], $item);
         } else {
             $desc = apply_filters('woocommerce_order_item_name', sprintf('<a href="%s">%s</a>', get_permalink($item['product_id']), $item['name']), $item);
         }
         $line_items[] = array('rate' => $item['line_subtotal'] / $item['qty'], 'qty' => (int) $item['qty'], 'desc' => $desc, 'total' => $item['line_subtotal'], 'tax' => -$tax);
     }
     if ($order->get_total_shipping() > 0.0) {
         $line_items[] = array('rate' => $order->get_total_shipping(), 'qty' => 1, 'desc' => __('Shipping', 'sprout-invoices'), 'total' => $order->get_total_shipping(), 'tax' => 0);
     }
     foreach ($order->get_tax_totals() as $code => $tax) {
         $line_items[] = array('rate' => $tax->amount - $order->get_total_tax_refunded_by_rate_id($tax->rate_id), 'qty' => 1, 'desc' => $tax->label, 'total' => $tax->amount - $order->get_total_tax_refunded_by_rate_id($tax->rate_id), 'tax' => 0);
     }
     if ('' !== $order->customer_note) {
         $line_items[] = array('rate' => 0, 'qty' => 0, 'desc' => $order->customer_note, 'total' => 0, 'tax' => 0);
     }
     $invoice_args = array('status' => SI_Invoice::STATUS_PENDING, 'subject' => sprintf(__('Order #%s', 'sprout-invoices'), $order_id), 'line_items' => $line_items);
     $invoice_id = SI_Invoice::create_invoice($invoice_args);
     $invoice = SI_Invoice::get_instance($invoice_id);
     $invoice->set_discount($order->get_total_discount());
     $invoice->set_total($order->get_total());
     if (method_exists($invoice, 'set_email')) {
         $invoice->set_email($order->billing_email);
     }
     if ($order->get_user_id()) {
         $client_ids = SI_Client::get_clients_by_user($order->get_user_id());
         if (!empty($client_ids)) {
             $client_id = array_pop($client_ids);
         }
         if (!$client_id) {
             $address = array('street' => $order->billing_address_1 . ' ' . $order->billing_address_2, 'city' => $order->billing_city, 'zone' => $order->billing_state, 'postal_code' => $order->billing_postcode, 'country' => $order->billing_country);
             $args = array('company_name' => '' !== $order->billing_company ? $order->billing_company : $order->billing_first_name . ' ' . $order->billing_last_name, 'website' => '', 'address' => $address, 'user_id' => $order->get_user_id());
             $client_id = SI_Client::new_client($args);
         }
         $invoice->set_client_id($client_id);
     }
     // History
     do_action('si_new_record', sprintf('<a href="%s">#%s</a>', add_query_arg(array('post' => $order_id, 'action' => 'edit'), admin_url('post.php')), $order_id), 'woocommerce_order', $invoice_id, sprintf('<a href="%s">#%s</a>', add_query_arg(array('post' => $order_id, 'action' => 'edit'), admin_url('post.php')), $order_id), 0, false);
     // store the order id within the invoice
     update_post_meta($invoice_id, Woo_Integration::ORDER_ID_META, $order_id);
     do_action('si_woocommerce_payment', $order_id, $invoice_id);
     ////////////
     // Return //
     ////////////
     // Mark as on-hold (we're awaiting the sprout_invoices)
     $order->update_status('on-hold', __('Awaiting invoice payment', 'sprout-invoices'));
     // Reduce stock levels
     $order->reduce_order_stock();
     // Remove cart
     WC()->cart->empty_cart();
     // Return thankyou redirect
     return array('result' => 'success', 'redirect' => $this->get_return_url($order));
 }