function build_test_invoice($total = 0)
 {
     $user_args = array('user_login' => '*****@*****.**', 'display_name' => 'Unit Tester', 'user_pass' => wp_generate_password(), 'user_email' => '*****@*****.**');
     $user_id = SI_Clients::create_user($user_args);
     $args = array('company_name' => 'Test Client', 'user_id' => $user_id);
     $client_id = SI_Client::new_client($args);
     $args = array('subject' => 'TESTING Payments');
     $id = SI_Invoice::create_invoice($args, SI_Invoice::STATUS_TEMP);
     $this->invoice_ids[] = $id;
     $invoice = SI_Invoice::get_instance($id);
     $invoice->set_client_id($client_id);
     // No total set make them random
     if (!$total) {
         $line_items = array();
         for ($i = 0; $i < 10; $i++) {
             $rate = rand(1000, 4000);
             $qty = rand(1, 10);
             $line_items[] = array('rate' => $rate, 'qty' => $qty, 'desc' => 'This is a test line item for a test invoice.', 'type' => '', 'total' => $rate * $qty, 'tax' => 0);
         }
     } else {
         $line_items = array(array('rate' => $total, 'qty' => 1, 'desc' => 'This is a test line item for a test invoice.', 'type' => '', 'total' => $total, 'tax' => 0));
     }
     $invoice->set_line_items($line_items);
     $this->assertTrue(in_array($id, $this->invoice_ids));
     return $id;
 }
 function setUp()
 {
     parent::setUp();
     $user_args = array('user_login' => '*****@*****.**', 'display_name' => 'Unit Tester', 'user_pass' => wp_generate_password(), 'user_email' => '*****@*****.**');
     $user_id = SI_Clients::create_user($user_args);
     $args = array('company_name' => 'Test Client', 'user_id' => $user_id);
     $client_id = SI_Client::new_client($args);
     $args = array('subject' => 'TEST Payment');
     $this->invoice_id = SI_Invoice::create_invoice($args, SI_Invoice::STATUS_TEMP);
     $this->invoice = SI_Invoice::get_instance($this->invoice_id);
     $this->invoice->set_client_id($client_id);
     $line_items = array();
     for ($i = 0; $i < 10; $i++) {
         $rate = rand(100, 1000);
         $qty = rand(1, 10);
         $line_items[] = array('rate' => $rate, 'qty' => $qty, 'desc' => 'This is a test line item for a test invoice.', 'type' => '', 'total' => $rate * $qty, 'tax' => 0);
     }
     $this->invoice->set_line_items($line_items);
 }
Пример #3
0
 public static function create_client($client = array())
 {
     $possible_dups = SI_Post_Type::find_by_meta(SI_Client::POST_TYPE, array(self::FRESHBOOKS_ID => $client['client_id']));
     // Don't create a duplicate if this was already imported.
     if (!empty($possible_dups)) {
         do_action('si_error', 'Client imported already', $client['client_id']);
         return;
     }
     if (!self::import_archived_data() && $client['folder'] != 'active') {
         return;
     }
     // args to create new client
     $address = array('street' => isset($client['contact_street']) && !is_array($client['p_street1']) ? self::esc__($client['contact_street']) : '', 'city' => isset($client['p_city']) && !is_array($client['p_city']) ? self::esc__($client['p_city']) : '', 'zone' => isset($client['p_state']) && !is_array($client['p_state']) ? self::esc__($client['p_state']) : '', 'postal_code' => isset($client['p_code']) && !is_array($client['p_code']) ? self::esc__($client['p_code']) : '', 'country' => isset($client['p_country']) && !is_array($client['p_country']) ? self::esc__($client['p_country']) : '');
     $args = array('address' => $address, 'company_name' => isset($client['company_name']) && !is_array($client['company_name']) ? $client['company_name'] : '', 'website' => isset($client['website']) && !is_array($client['website']) ? $client['website'] : '', 'currency' => isset($client['currency_code']) && !is_array($client['currency_code']) ? $client['currency_code'] : '');
     if (isset($client['company_name']) && $args['company_name'] == '') {
         if (is_array($client['first_name']) || is_array($client['last_name'])) {
             do_action('si_error', 'Client creation error', $client['client_id']);
             return;
         }
         $args['company_name'] = $client['first_name'] . ' ' . $client['last_name'];
     }
     $client_id = SI_Client::new_client($args);
     // notes
     if (isset($client['notes']) && $client['notes'] != '') {
         SI_Internal_Records::new_record($client['notes'], SI_Controller::PRIVATE_NOTES_TYPE, $client_id, '', 0);
     }
     // create import record
     update_post_meta($client_id, self::FRESHBOOKS_ID, $client['client_id']);
     return $client_id;
 }
Пример #4
0
 public static function create_client($client = array())
 {
     // args to create new client
     $address = array('street' => $client['Address'] . ' ' . $client['Address 2'], 'city' => $client['City'], 'zone' => $client['State'], 'postal_code' => $client['Zip'], 'country' => $client['Country']);
     $args = array('address' => $address, 'company_name' => isset($client['Company']) ? $client['Company'] : $client['First Name'] . ' ' . $client['Last Name'], 'company_name' => isset($client['Company']) ? $client['Company'] : '', 'website' => isset($client['Web Address']) ? $client['Web Address'] : '', 'phone' => isset($client['Telephone']) ? $client['Telephone'] : '');
     $client_id = SI_Client::new_client($args);
     // notes
     if (isset($client['Notes']) && $client['Notes'] != '') {
         SI_Internal_Records::new_record($client['Notes'], SI_Controller::PRIVATE_NOTES_TYPE, $client_id, '', 0);
     }
     return $client_id;
 }
Пример #5
0
 /**
  * AJAX submission from admin.
  * @return json response
  */
 public static function maybe_create_client()
 {
     // form maybe be serialized
     if (isset($_REQUEST['serialized_fields'])) {
         foreach ($_REQUEST['serialized_fields'] as $key => $data) {
             $_REQUEST[$data['name']] = $data['value'];
         }
     }
     if (!isset($_REQUEST['sa_client_nonce'])) {
         self::ajax_fail('Forget something?');
     }
     $nonce = $_REQUEST['sa_client_nonce'];
     if (!wp_verify_nonce($nonce, self::SUBMISSION_NONCE)) {
         self::ajax_fail('Not going to fall for it!');
     }
     if (!current_user_can('publish_sprout_invoices')) {
         self::ajax_fail('User cannot create new posts!');
     }
     if (!isset($_REQUEST['sa_client_name']) || $_REQUEST['sa_client_name'] == '') {
         self::ajax_fail('A company name is required');
     }
     $user_id = 0;
     // Attempt to create a user
     if (isset($_REQUEST['sa_client_email']) && $_REQUEST['sa_client_email'] != '') {
         $user_args = array('user_login' => esc_html($_REQUEST['sa_client_email']), 'display_name' => isset($_REQUEST['sa_client_name']) ? esc_html($_REQUEST['sa_client_name']) : esc_html($_REQUEST['sa_client_email']), 'user_pass' => wp_generate_password(), 'user_email' => isset($_REQUEST['sa_client_email']) ? esc_html($_REQUEST['sa_client_email']) : '', 'first_name' => isset($_REQUEST['sa_client_first_name']) ? esc_html($_REQUEST['sa_client_first_name']) : '', 'last_name' => isset($_REQUEST['sa_client_last_name']) ? esc_html($_REQUEST['sa_client_last_name']) : '', 'user_url' => isset($_REQUEST['sa_client_website']) ? esc_html($_REQUEST['sa_client_website']) : '');
         $user_id = self::create_user($user_args);
     }
     // Create the client
     $address = array('street' => isset($_REQUEST['sa_client_street']) ? esc_html($_REQUEST['sa_client_street']) : '', 'city' => isset($_REQUEST['sa_client_city']) ? esc_html($_REQUEST['sa_client_city']) : '', 'zone' => isset($_REQUEST['sa_client_zone']) ? esc_html($_REQUEST['sa_client_zone']) : '', 'postal_code' => isset($_REQUEST['sa_client_postal_code']) ? esc_html($_REQUEST['sa_client_postal_code']) : '', 'country' => isset($_REQUEST['sa_client_country']) ? esc_html($_REQUEST['sa_client_country']) : '');
     $args = array('company_name' => isset($_REQUEST['sa_client_name']) ? esc_html($_REQUEST['sa_client_name']) : '', 'website' => isset($_REQUEST['sa_client_website']) ? esc_html($_REQUEST['sa_client_website']) : '', 'address' => $address, 'user_id' => $user_id);
     $client_id = SI_Client::new_client($args);
     $response = array('id' => $client_id, 'title' => get_the_title($client_id));
     header('Content-type: application/json');
     if (self::DEBUG) {
         header('Access-Control-Allow-Origin: *');
     }
     echo wp_json_encode($response);
     exit;
 }
Пример #6
0
 public static function create_client($data = array())
 {
     $client_id = SI_Client::new_client($data);
     return self::client_data($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);
 }
Пример #8
0
 public static function create_client($wp_invoice = array())
 {
     $possible_dups = SI_Post_Type::find_by_meta(SI_Client::POST_TYPE, array(self::WPINVOICE_ID => $wp_invoice['ID']));
     // Don't create a duplicate if this was already imported.
     if (!empty($possible_dups)) {
         do_action('si_error', 'Client imported already', $wp_invoice['ID']);
         return $possible_dups[0];
     }
     $wp_invoice_user_data = $wp_invoice['user_data'];
     // args to create new client
     $address = array('street' => isset($wp_invoice_user_data['streetaddress']) ? self::esc__($wp_invoice_user_data['streetaddress']) : '', 'city' => isset($wp_invoice_user_data['city']) ? self::esc__($wp_invoice_user_data['city']) : '', 'zone' => isset($wp_invoice_user_data['state']) ? self::esc__($wp_invoice_user_data['state']) : '', 'postal_code' => isset($wp_invoice_user_data['zip']) ? self::esc__($wp_invoice_user_data['zip']) : '', 'country' => isset($wp_invoice_user_data['country']) ? self::esc__($wp_invoice_user_data['country']) : apply_filters('si_default_country_code', 'US'));
     $args = array('address' => $address, 'company_name' => isset($wp_invoice_user_data['company_name']) ? $wp_invoice_user_data['company_name'] : '', 'website' => isset($wp_invoice_user_data['user_url']) ? $wp_invoice_user_data['user_url'] : '', 'currency' => isset($wp_invoice_user_data['default_currency_code']) ? $wp_invoice_user_data['default_currency_code'] : '');
     if ($args['company_name'] == '') {
         if (is_array($wp_invoice_user_data['first_name']) || is_array($wp_invoice_user_data['last_name'])) {
             do_action('si_error', 'Client creation error', $wp_invoice);
             return;
         }
         $args['company_name'] = $wp_invoice_user_data['first_name'] . ' ' . $wp_invoice_user_data['last_name'];
     }
     // Attempt to find matching client
     if (isset($args['company_name'])) {
         global $wpdb;
         $client_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", esc_sql($args['company_name']), SI_Client::POST_TYPE));
         if (!empty($client_ids)) {
             do_action('si_error', 'Client imported already (name match)', $wp_invoice['ID']);
             return $client_ids[0];
         }
     }
     $client_id = SI_Client::new_client($args);
     // create import record
     update_post_meta($client_id, self::WPINVOICE_ID, $wp_invoice['ID']);
     return $client_id;
 }
Пример #9
0
 public static function create_client(Harvest_Client $client)
 {
     $possible_dups = SI_Post_Type::find_by_meta(SI_Client::POST_TYPE, array(self::HARVEST_ID => $client->id));
     // Don't create a duplicate if this was already imported.
     if (!empty($possible_dups)) {
         do_action('si_error', 'Client imported already', $client->id);
         return;
     }
     if (!self::import_archived_data() && $client->active == 'false') {
         return;
     }
     $args = array('company_name' => $client->name, 'currency' => substr($client->currency, -3), 'address' => array('street' => $client->details));
     $client_id = SI_Client::new_client($args);
     update_post_meta($client_id, self::HARVEST_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));
 }
 public static function create_client($data = array())
 {
     $client_id = SI_Client::new_client($data);
     $client = SI_Client::get_instance($client_id);
     if (!is_a($client, 'SI_Client')) {
         return;
     }
     return self::client_data($client);
 }