示例#1
0
 public static function invoice_embed($atts = array())
 {
     do_action('sprout_invoices_invoice_embed');
     $invoice_id = 0;
     // Make sure id given is for an invoice
     if (isset($atts['id'])) {
         $invoice_id = (int) $atts['id'];
     }
     if (isset($_GET['si_id'])) {
         $invoice_id = (int) $_GET['si_id'];
     }
     if (SI_Invoice::POST_TYPE !== get_post_type($invoice_id)) {
         return;
     }
     // enqueue style
     self::frontend_enqueue();
     // Show snippet view or not.
     $embed_view = '';
     if (isset($atts['snippet']) && 'true' === $atts['snippet']) {
         $embed_view = '-snippet';
     }
     self::remove_actions();
     // Setup global post for filters and functions to work
     global $post;
     $post = get_post($invoice_id);
     setup_postdata($post);
     $invoice = SI_Invoice::get_instance($invoice_id);
     $line_items = $invoice->get_line_items();
     $view = self::load_addon_view_to_string('invoices/embed' . $embed_view, array('id' => $invoice_id, 'line_items' => $line_items, 'prev_type' => '', 'totals' => SI_Line_Items::line_item_totals($invoice_id)), false);
     // reset to the original post
     wp_reset_postdata();
     return $view;
 }
function _change_woo_si_invoice_due_date($order_id = 0, $invoice_id = 0)
{
    $invoice = SI_Invoice::get_instance($invoice_id);
    $due_date = time() + 86400 * 5;
    // "5" is the days from now it will be due
    $invoice->set_due_date($due_date);
}
 public static function prevent_auto_draft_title($title = '', $post_id = 0)
 {
     if (__('Auto Draft') !== $title) {
         return $title;
     }
     if (SI_Invoice::POST_TYPE !== get_post_type($post_id)) {
         return $title;
     }
     $invoice = SI_Invoice::get_instance($post_id);
     return apply_filters('si_default_invoice_title', sprintf('#%s', $invoice->get_invoice_id()), $invoice);
 }
 public static function save_admin_payment($post_id, $post, $callback_args, $invoice_id = null)
 {
     $invoice = SI_Invoice::get_instance($post_id);
     $amount = isset($_REQUEST['sa_metabox_payment_amount']) ? $_REQUEST['sa_metabox_payment_amount'] : 0;
     $number = isset($_REQUEST['sa_metabox_payment_transaction_id']) ? $_REQUEST['sa_metabox_payment_transaction_id'] : '';
     $date = isset($_REQUEST['sa_metabox_payment_date']) ? $_REQUEST['sa_metabox_payment_date'] : '';
     $notes = isset($_REQUEST['sa_metabox_payment_notes']) ? $_REQUEST['sa_metabox_payment_notes'] : '';
     if (!$amount) {
         return false;
     }
     self::create_admin_payment($invoice->get_id(), $amount, $number, $date, $notes);
 }
function maybe_change_invoice_status($post_id)
{
    if (!class_exists('SI_Invoice')) {
        return;
    }
    if (SI_Invoice::POST_TYPE !== get_post_type($post_id)) {
        return;
    }
    $invoice = SI_Invoice::get_instance($post_id);
    if (SI_Invoice::STATUS_TEMP === $invoice->get_status()) {
        $invoice->set_pending();
    }
}
 function setUp()
 {
     parent::setUp();
     $args = array('subject' => 'TEST');
     $this->invoice_id = SI_Invoice::create_invoice($args, SI_Invoice::STATUS_TEMP);
     $this->invoice = SI_Invoice::get_instance($this->invoice_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);
 }
function maybe_change_invoice_id($master_invoice_id = 0, $invoice_id = 0)
{
    if (!class_exists('SI_Invoices_Recurring')) {
        return;
    }
    // master invoice
    $master_invoice = SI_Invoice::get_instance($master_invoice_id);
    $master_invoice_id = $master_invoice->get_invoice_id();
    // build id
    $clones = SI_Invoices_Recurring::get_child_clones($master_invoice_id);
    $new_id = $master_invoice_id . '-' . count($clones);
    // new invoice created
    $invoice = SI_Invoice::get_instance($invoice_id);
    $invoice->set_invoice_id($new_id);
}
示例#8
0
 /**
  * Get the doc object
  * @param  integer $id
  * @return SI_Estimate/SI_Invoice
  */
 function si_get_doc_object($id = 0)
 {
     if (!$id) {
         $id = get_the_ID();
     }
     switch (get_post_type($id)) {
         case SI_Estimate::POST_TYPE:
             $doc = SI_Estimate::get_instance($id);
             break;
         case SI_Invoice::POST_TYPE:
             $doc = SI_Invoice::get_instance($id);
             break;
         default:
             $doc = '';
             break;
     }
     return apply_filters('si_get_doc_object', $doc);
 }
 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);
 }
 function build_test_payment($invoice_id, $total = 0, $date = false, $status = '')
 {
     $invoice = SI_Invoice::get_instance($invoice_id);
     // Randomize if no total is set.
     $payment_total = !$total ? rand(1, 300) : $total;
     $status = $status != '' ? $status : SI_Payment::STATUS_AUTHORIZED;
     // create new payment
     $payment_id = SI_Payment::new_payment(array('payment_method' => SI_Paypal_EC::PAYMENT_METHOD, 'invoice' => $invoice->get_id(), 'amount' => $payment_total, 'data' => array('api_response' => array())), $status);
     $this->payment_ids[] = $payment_id;
     $new_payment = SI_Payment::get_instance($payment_id);
     do_action('payment_authorized', $new_payment);
     if (!$date) {
         $date = time();
     }
     if (!is_integer($date)) {
         $date = strtotime($date);
     }
     $new_payment->set_post_date(date('Y-m-d H:i:s', $date));
     $this->assertTrue(in_array($payment_id, $this->payment_ids));
     return $payment_id;
 }
 public static function maybe_log_invoice_view()
 {
     global $post;
     if (!is_single()) {
         return;
     }
     // Make sure this is an estimate we're viewing
     if ($post->post_type != SI_Invoice::POST_TYPE) {
         return;
     }
     // Don't log the authors views
     if ($post->post_author == get_current_user_id()) {
         return;
     }
     if (is_user_logged_in()) {
         $user = get_userdata(get_current_user_id());
         $name = $user->first_name . ' ' . $user->last_name;
         $whom = $name . ' (' . $user->user_login . ')';
     } else {
         $whom = self::get_user_ip();
     }
     if (in_array($whom, array('127.0.0.1', '::1'))) {
         return;
     }
     $invoice = SI_Invoice::get_instance($post->ID);
     $title = sprintf(__('Invoice viewed by %s for the first time.', 'sprout-invoices'), esc_html($whom));
     $found = false;
     $view_logs = SI_Record::get_records_by_type_and_association($invoice->get_id(), self::VIEWED_STATUS_UPDATE);
     foreach ($view_logs as $record_id) {
         if (!$found && $title == get_the_title($record_id)) {
             $found = true;
         }
     }
     // Record exists
     if ($found) {
         return;
     }
     do_action('si_new_record', $_SERVER, self::VIEWED_STATUS_UPDATE, $invoice->get_id(), $title);
 }
    public static function add_deposit_option($doc_id)
    {
        $context = si_get_doc_context($doc_id);
        if ('invoice' !== $context) {
            return;
        }
        $invoice = SI_Invoice::get_instance($doc_id);
        $total = is_a($invoice, 'SI_Invoice') ? $invoice->get_total() : '0.00';
        $total_payments = is_a($invoice, 'SI_Invoice') ? $invoice->get_payments_total() : '0.00';
        $deposit = is_a($invoice, 'SI_Invoice') ? $invoice->get_deposit() : '0.00';
        $status = is_a($invoice, 'SI_Invoice') && $invoice->get_status() !== 'auto-draft' ? $invoice->get_status() : SI_Invoice::STATUS_TEMP;
        if (apply_filters('show_upgrade_messaging', true, 'deposit-line-items')) {
            ?>
			<div id="deposit">
				<b title="Upgrade Sprout Invoices to enable deposits." class="helptip"><?php 
            _e('Deposit Due', 'sprout-invoices');
            ?>
</b>
				<input type="number" name="deposit" min="0" max="0" step="any" disabled="disabled">
			</div>
			<?php 
        } elseif (floatval($total - $total_payments) > 0.0 || 'auto-draft' === $status || 'temp' === $status) {
            ?>
			<div id="deposit">
				<b title="Set the amount due for the next payment&mdash;amount due will be used 0" class="helptip"><?php 
            _e('Deposit Due', 'sprout-invoices');
            ?>
</b>
				<input type="number" name="deposit" value="<?php 
            echo (double) $deposit;
            ?>
" min="0" max="<?php 
            echo floatval($total - $total_payments);
            ?>
"  step="any">
			</div>
			<?php 
        }
    }
 /**
  * Get the template for the current doc
  * @param  string $doc
  * @return
  */
 public static function get_doc_current_template($doc_id)
 {
     $template_id = get_post_meta($doc_id, self::TEMPLATE_OPTION, true);
     if ($template_id == '') {
         switch (get_post_type($doc_id)) {
             case SI_Invoice::POST_TYPE:
                 $invoice = SI_Invoice::get_instance($doc_id);
                 $client_id = $invoice->get_client_id();
                 $template_id = self::get_client_invoice_template($client_id);
                 break;
             case SI_Estimate::POST_TYPE:
                 $estimate = SI_Estimate::get_instance($doc_id);
                 $client_id = $estimate->get_client_id();
                 $template_id = self::get_client_estimate_template($client_id);
                 break;
             default:
                 break;
         }
     }
     if (!$template_id) {
         $template_id = '';
     }
     return $template_id;
 }
 private function load_invoice()
 {
     $invoice_id = 0;
     if (isset($_GET['sa_invoice']) && !self::using_permalinks()) {
         if (is_numeric($_GET['sa_invoice'])) {
             $invoice_id = (int) $_GET['sa_invoice'];
         } else {
             $posts = get_posts(array('name' => $_GET['sa_invoice'], 'posts_per_page' => 1, 'post_type' => SI_Invoice::POST_TYPE));
             $post = $posts[0];
             $invoice_id = $post->ID;
         }
     }
     // If permalinks are used this is the default method of finding the post id.
     if (!$invoice_id) {
         $invoice_id = url_to_postid(esc_url_raw($_SERVER['REQUEST_URI']));
     }
     if (!$invoice_id || get_post_type($invoice_id) !== SI_Invoice::POST_TYPE) {
         self::set_message(__('Invoice ID invalid. Payments are disabled.', 'sprout-invoices'), self::MESSAGE_STATUS_ERROR);
         return;
     }
     $this->invoice = SI_Invoice::get_instance($invoice_id);
     if (!$this->invoice && !$this->checkout_complete) {
         self::set_message(__('No invoice associated with this checkout.', 'sprout-invoices'), self::MESSAGE_STATUS_ERROR);
         wp_redirect('/', 303);
         exit;
     }
     do_action('si_load_invoice', $this, $this->invoice);
 }
 public static function time_entry_fields($context = 0, $hide_project_select = false)
 {
     $projects = array();
     $time_types = array();
     if (!$context) {
         $context = get_the_id();
     }
     // Get associated projects based on context
     switch (get_post_type($context)) {
         case SI_Project::POST_TYPE:
             $project_id = $context;
             // Projects
             $projects = array($project_id);
             break;
         case SI_Client::POST_TYPE:
             $client_id = $context;
             // Projects
             $projects = SI_Project::get_projects_by_client($client_id);
             break;
         case SI_Invoice::POST_TYPE:
             $invoice_id = $context;
             // Projects
             $invoice = SI_Invoice::get_instance($invoice_id);
             $project_id = $invoice->get_project();
             if ($project_id) {
                 $projects = array($project_id);
             }
             break;
         default:
             // Projects
             $args = array('post_type' => SI_Project::POST_TYPE, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids');
             $projects = get_posts($args);
             break;
     }
     $fields = array();
     $project_options = array();
     foreach ($projects as $project_id) {
         $title = get_the_title($project_id);
         $title = $title == __('Auto Draft') ? __('Current Project', 'sprout-invoices') : $title;
         $project_options[$project_id] = $title;
     }
     $fields['project_id'] = array('weight' => 1, 'label' => __('Project', 'sprout-invoices'), 'type' => 'select', 'options' => $project_options);
     $description = sprintf(__('Select an activity, <a href="%s">create a new activity</a> or <a class="thickbox" href="%s" title="Edit Activities">manage existing activities</a>.', 'sprout-invoices'), 'javascript:void(0)" id="show_time_creation_modal"', admin_url('admin-ajax.php?action=sa_manage_time&width=750&height=450'));
     $time_types_options = SI_Time::get_activities();
     $fields['activity_id'] = array('weight' => 10, 'label' => __('Activity', 'sprout-invoices'), 'type' => 'select', 'description' => $description, 'options' => $time_types_options);
     $fields['time_inc'] = array('weight' => 20, 'label' => __('Time', 'sprout-invoices'), 'type' => 'text', 'description' => __('In hours, e.g. 1.25 for 75 minutes.', 'sprout-invoices'));
     $fields['note'] = array('weight' => 30, 'label' => __('Note', 'sprout-invoices'), 'type' => 'textarea', 'default' => '');
     $fields['date'] = array('weight' => 100, 'label' => __('Date', 'sprout-invoices'), 'type' => 'date', 'default' => date('Y-m-d', current_time('timestamp')), 'placeholder' => '');
     $fields['nonce'] = array('type' => 'hidden', 'value' => wp_create_nonce(self::SUBMISSION_NONCE), 'weight' => 10000);
     $fields = apply_filters('si_time_entry_form_fields', $fields);
     uasort($fields, array(__CLASS__, 'sort_by_weight'));
     return $fields;
 }
示例#16
0
 public function get_payments()
 {
     $payments = array();
     $invoices = $this->get_invoices();
     foreach ($invoices as $invoice_id) {
         $invoice = SI_Invoice::get_instance($invoice_id);
         $payments = array_merge($payments, $invoice->get_payments());
     }
     return $payments;
 }
 public static function maybe_change_status()
 {
     if (!isset($_REQUEST['change_status_nonce'])) {
         self::ajax_fail('Forget something?');
     }
     $nonce = esc_attr($_REQUEST['change_status_nonce']);
     if (!wp_verify_nonce($nonce, self::NONCE)) {
         self::ajax_fail('Not going to fall for it!');
     }
     if (!isset($_REQUEST['id'])) {
         self::ajax_fail('Forget something?');
     }
     if (!isset($_REQUEST['status'])) {
         self::ajax_fail('Forget something?');
     }
     $view = '';
     $doc_id = esc_attr($_REQUEST['id']);
     $new_status = esc_attr($_REQUEST['status']);
     switch (get_post_type($doc_id)) {
         case SI_Invoice::POST_TYPE:
             $doc = SI_Invoice::get_instance($doc_id);
             $doc->set_status($new_status);
             $view = self::load_view_to_string('admin/sections/invoice-status-change-drop', array('id' => $doc_id, 'status' => $doc->get_status()), false);
             break;
         case SI_Estimate::POST_TYPE:
             switch ($new_status) {
                 case 'accept':
                     $new_status = SI_Estimate::STATUS_APPROVED;
                     break;
                 case 'decline':
                     $new_status = SI_Estimate::STATUS_DECLINED;
                     break;
                 default:
                     break;
             }
             $doc = SI_Estimate::get_instance($doc_id);
             $doc->set_status($new_status);
             $view = self::load_view_to_string('admin/sections/estimate-status-change-drop', array('id' => $doc_id, 'status' => $doc->get_status()), false);
             break;
         default:
             self::ajax_fail('Not an estimate or invoice.');
             return;
             break;
     }
     // action
     do_action('doc_status_changed', $doc, $_REQUEST);
     header('Content-type: application/json');
     if (self::DEBUG) {
         header('Access-Control-Allow-Origin: *');
     }
     echo wp_json_encode(array('new_button' => $view));
     exit;
 }
 public static function maybe_create_new_invoices()
 {
     $args = array('post_type' => SI_Invoice::POST_TYPE, 'post_status' => array_keys(SI_Invoice::get_statuses()), 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => self::$meta_keys['is_recurring'], 'value' => 1, 'compare' => '='), array('key' => self::$meta_keys['clone_time'], 'value' => array(strtotime('Last year'), current_time('timestamp')), 'compare' => 'BETWEEN'), array('key' => self::$meta_keys['cloned_from'], 'compare' => 'NOT EXISTS')));
     $invoice_ids = get_posts($args);
     foreach ($invoice_ids as $invoice_id) {
         $cloned_post_id = self::clone_post($invoice_id, SI_Invoice::STATUS_PENDING, SI_Invoice::POST_TYPE);
         // Issue date is today.
         $cloned_invoice = SI_Invoice::get_instance($cloned_post_id);
         $cloned_invoice->set_issue_date(time());
         // Due date is in the future
         $due_date = apply_filters('si_new_recurring_invoice_due_date_in_days', 14);
         $cloned_invoice->set_due_date(time() + 60 * 60 * 24 * $due_date);
         // adjust the clone time after the next invoice
         self::set_clone_time($invoice_id, current_time('timestamp'));
         self::set_parent($cloned_post_id, $invoice_id);
         do_action('si_recurring_invoice_created', $invoice_id, $cloned_post_id);
     }
 }
 public static function paypal_profile_link($payment)
 {
     if ($payment->get_payment_method() != self::get_payment_method()) {
         return;
     }
     $data = $payment->get_data();
     if (isset($data['api_response']['PROFILEID'])) {
         $invoice_id = $payment->get_invoice_id();
         $invoice = SI_Invoice::get_instance($invoice_id);
         printf(__('<b>Current Payment Status:</b> <code>%s</code>', 'sprout-invoices'), self::verify_recurring_payment($invoice));
         echo ' &mdash; ';
         _e('Paypal Profile ID: ', 'sprout-invoices');
         if (isset($data['live']) && !$data['live']) {
             printf('<a class="payment_profile_link" href="https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s" target="_blank">%s</a>', $data['api_response']['PROFILEID'], $data['api_response']['PROFILEID']);
         } else {
             printf('<a class="payment_profile_link" href="https://www.paypal.com/us/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s" target="_blank">%s</a>', $data['api_response']['PROFILEID'], $data['api_response']['PROFILEID']);
         }
     }
 }
示例#20
0
 public static function create_payment($payment = array())
 {
     if (!isset($payment['Invoice ID'])) {
         do_action('si_error', 'No Invoice ID given within payment import', $payment);
         return;
     }
     // Find the associated invoice
     $invoices = SI_Post_Type::find_by_meta(SI_Invoice::POST_TYPE, array(self::CSV_ID => $payment['Invoice ID']));
     // Can't assign a payment without an invoice
     if (empty($invoices)) {
         do_action('si_error', 'No invoice found for this payment', $payment['Payment ID']);
         return;
     }
     $invoice = SI_Invoice::get_instance($invoices[0]);
     if (!is_a($invoice, 'SI_Invoice')) {
         return;
     }
     $payment_id = SI_Payment::new_payment(array('payment_method' => isset($payment['Payment Method']) ? $payment['Payment Method'] : self::PAYMENT_METHOD, 'invoice' => $invoice->get_id(), 'amount' => round($payment['Amount'], 2), 'transaction_id' => isset($payment['Payment ID']) ? $payment['Payment ID'] : '', 'data' => array('api_response' => $payment)));
     $new_payment = SI_Payment::get_instance($payment_id);
     $new_payment->set_post_date(date('Y-m-d H:i:s', strtotime($payment['Date'])));
     return $new_payment;
 }
 public static function payment_data(SI_Payment $payment)
 {
     $payment_data = array('title' => $payment->get_title(), 'id' => $payment->get_id(), 'status' => $payment->get_status(), 'payment_method' => $payment->get_payment_method(), 'amount' => $payment->get_amount(), 'invoice_id' => $payment->get_invoice_id(), 'data' => $payment->get_data());
     $invoice = SI_Invoice::get_instance($payment->get_invoice_id());
     if (is_a($invoice, 'SI_Invoice')) {
         $payment_data['invoice_data'] = self::invoice_data($invoice);
     }
     return $payment_data;
 }
示例#22
0
 public function get_payments()
 {
     if (!class_exists('Sprout_Invoices')) {
         return array();
     }
     $payments = array();
     $invoices = $this->get_invoices();
     foreach ($invoices as $invoice_id) {
         $invoice = SI_Invoice::get_instance($invoice_id);
         $payments = array_merge($payments, $invoice->get_payments());
     }
     return $payments;
 }
    _e('Status', 'sprout-invoices');
    ?>
</th>
					<th><?php 
    _e('Totals', 'sprout-invoices');
    ?>
</th>
					<th>&nbsp;</th>
				</tr>
			</thead>
			<tbody>
				<?php 
    if (!empty($invoices)) {
        $total_balance_due = 0;
        foreach ($invoices as $invoice_id) {
            $invoice = SI_Invoice::get_instance($invoice_id);
            if (!is_a($invoice, 'SI_Invoice')) {
                continue;
            }
            if ('archived' === si_get_invoice_status($invoice_id)) {
                continue;
            }
            if ('write-off' === si_get_invoice_status($invoice_id)) {
                continue;
            }
            $total_balance_due += si_get_invoice_balance($invoice_id);
            // Get a label class for the status.
            switch (si_get_invoice_status($invoice_id)) {
                case 'publish':
                    $label = 'primary';
                    break;
示例#24
0
 public function get_client()
 {
     $invoice_id = $this->get_invoice_id();
     if (!$invoice_id) {
         return null;
     }
     $invoice = SI_Invoice::get_instance($invoice_id);
     if (!is_a($invoice, 'SI_Invoice')) {
         return null;
     }
     return $invoice->get_client();
 }
示例#25
0
 /**
  * Filtering the payment processor currency code option based on some predefined options.
  * @param  string  $currency_code
  * @param  integer $invoice_id
  * @param  string  $payment_method
  * @return string
  */
 public static function maybe_change_currency_code($currency_code = '', $invoice_id = 0, $payment_method = '')
 {
     $invoice = SI_Invoice::get_instance($invoice_id);
     $client = $invoice->get_client();
     if (!is_wp_error($client)) {
         $client_currency = $client->get_currency();
         if ($client_currency != '') {
             $currency_code = $client_currency;
         }
     }
     return $currency_code;
 }
 /**
  * Prep data.
  *
  * @uses $this->_column_headers
  * @uses $this->items
  * @uses $this->get_columns()
  * @uses $this->get_sortable_columns()
  * @uses $this->get_pagenum()
  * @uses $this->set_pagination_args()
  * */
 function prepare_items()
 {
     /**
      * First, lets decide how many records per page to show
      */
     $per_page = 25;
     /**
      * Define our column headers.
      */
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     /**
      * REQUIRED. Build an array to be used by the class for column
      * headers.
      */
     $this->_column_headers = array($columns, $hidden, $sortable);
     $filter = isset($_REQUEST['post_status']) ? $_REQUEST['post_status'] : array(SI_Payment::STATUS_PENDING, SI_Payment::STATUS_AUTHORIZED, SI_Payment::STATUS_COMPLETE, SI_Payment::STATUS_PARTIAL, SI_Payment::STATUS_RECURRING, SI_Payment::STATUS_CANCELLED);
     $args = array('post_type' => SI_Payment::POST_TYPE, 'post_status' => $filter, 'posts_per_page' => $per_page, 'paged' => $this->get_pagenum());
     // Check based on post_type id
     if (isset($_REQUEST['s']) && is_numeric($_REQUEST['s'])) {
         $post_id = $_REQUEST['s'];
         switch (get_post_type($post_id)) {
             case SI_Payment::POST_TYPE:
                 $payment_ids = array($post_id);
                 break;
             case SI_Invoice::POST_TYPE:
                 $invoice = SI_Invoice::get_instance($post_id);
                 $payment_ids = $invoice->get_payments();
                 break;
             case SI_Client::POST_TYPE:
                 $client = SI_Client::get_instance($post_id);
                 $payment_ids = $client->get_payments();
                 break;
             default:
                 $payment_ids = false;
                 break;
         }
         if ($payment_ids) {
             $meta_query = array('post__in' => $payment_ids);
             $args = array_merge($args, $meta_query);
         }
     } elseif (isset($_GET['s']) && $_GET['s'] != '') {
         $args = array_merge($args, array('s' => sanitize_text_field($_GET['s'])));
     }
     // Filter by date
     if (isset($_GET['m']) && $_GET['m'] != '') {
         $args = array_merge($args, array('m' => sanitize_text_field($_GET['m'])));
     }
     $payments = new WP_Query($args);
     /**
      * REQUIRED. *Sorted* data to the items property, where
      * it can be used by the rest of the class.
      */
     $this->items = apply_filters('si_mngt_payments_items', $payments->posts);
     /**
      * REQUIRED. Register our pagination options & calculations.
      */
     $this->set_pagination_args(array('total_items' => $payments->found_posts, 'per_page' => $per_page, 'total_pages' => $payments->max_num_pages));
 }
示例#27
0
 public static function create_payment($payment)
 {
     $possible_dups = SI_Post_Type::find_by_meta(SI_Payment::POST_TYPE, array(self::FRESHBOOKS_ID => $payment['payment_id']));
     // Don't create a duplicate if this was already imported.
     if (!empty($possible_dups)) {
         do_action('si_error', 'Invoice imported already', $payment['payment_id']);
         return;
     }
     // Find the associated invoice
     $invoices = SI_Post_Type::find_by_meta(SI_Invoice::POST_TYPE, array(self::FRESHBOOKS_ID => $payment['invoice_id']));
     $invoice = SI_Invoice::get_instance($invoices[0]);
     $invoice_id = is_a($invoice, 'SI_Invoice') ? $invoice->get_id() : 0;
     // Can't assign a payment without an invoice
     if (!$invoice_id) {
         do_action('si_error', 'No invoice found for this payment', $payment['payment_id']);
         return;
     }
     $payment_id = SI_Payment::new_payment(array('payment_method' => isset($payment['type']) && !is_array($payment['type']) ? $payment['type'] : self::PAYMENT_METHOD, 'invoice' => $invoice_id, 'amount' => round($payment['amount'], 2), 'transaction_id' => isset($payment['payment_id']) ? $payment['payment_id'] : '', 'data' => array('api_response' => $payment)));
     $new_payment = SI_Payment::get_instance($payment_id);
     $new_payment->set_post_date(date('Y-m-d H:i:s', strtotime($payment['date'])));
     return $new_payment;
 }
示例#28
0
function si_doc_history_records($doc_id = 0, $filtered = true)
{
    if (!$doc_id) {
        $doc_id = get_the_ID();
    }
    $returned_history = array();
    switch (get_post_type($doc_id)) {
        case SI_Estimate::POST_TYPE:
            $estimate = SI_Estimate::get_instance($doc_id);
            $history = $estimate->get_history();
            break;
        case SI_Invoice::POST_TYPE:
            $invoice = SI_Invoice::get_instance($doc_id);
            $history = array_merge($invoice->get_history(), $invoice->get_payments());
            break;
        default:
            # code...
            break;
    }
    $history = apply_filters('si_doc_history_records_pre_sort', $history, $doc_id, $filtered);
    // Sort in ascending order
    asort($history, SORT_NUMERIC);
    foreach ($history as $item_id) {
        if (get_post_type($item_id) == SI_Record::POST_TYPE) {
            $record = SI_Record::get_instance($item_id);
            // If no type is set than just keep on moving.
            if ($record->get_type() == SI_Record::DEFAULT_TYPE) {
                continue;
            }
            // filter these types of records out.
            if ($filtered) {
                if (in_array($record->get_type(), array(SI_Controller::PRIVATE_NOTES_TYPE, SI_Estimates::VIEWED_STATUS_UPDATE, SI_Notifications::RECORD))) {
                    continue;
                }
            }
            $r_post = $record->get_post();
            switch ($record->get_type()) {
                case SI_Controller::PRIVATE_NOTES_TYPE:
                    $returned_history[$item_id]['type'] = __('Private Note', 'sprout-invoices');
                    break;
                case SI_Estimates::HISTORY_UPDATE:
                    $returned_history[$item_id]['type'] = __('Updated', 'sprout-invoices');
                    break;
                case SI_Estimates::VIEWED_STATUS_UPDATE:
                    $returned_history[$item_id]['type'] = __('Viewed', 'sprout-invoices');
                    break;
                case SI_Notifications::RECORD:
                    $returned_history[$item_id]['type'] = __('Notification', 'sprout-invoices');
                    break;
                case SI_Estimates::HISTORY_INVOICE_CREATED:
                    $returned_history[$item_id]['type'] = __('Invoice Created', 'sprout-invoices');
                    break;
                case SI_Estimate_Submissions::SUBMISSION_UPDATE:
                    $returned_history[$item_id]['type'] = __('Submitted', 'sprout-invoices');
                    break;
                case SI_Importer::RECORD:
                    $returned_history[$item_id]['type'] = __('Imported', 'sprout-invoices');
                    break;
                case SI_Estimates::HISTORY_STATUS_UPDATE:
                default:
                    $returned_history[$item_id]['type'] = __('Status Update', 'sprout-invoices');
                    break;
            }
            $returned_history[$item_id]['status_type'] = $record->get_type();
            $returned_history[$item_id]['post_date'] = $r_post->post_date;
            $returned_history[$item_id]['update_title'] = $r_post->post_title;
            $returned_history[$item_id]['content'] = $r_post->post_content;
        } elseif (get_post_type($item_id) == SI_Payment::POST_TYPE) {
            $payment = SI_Payment::get_instance($item_id);
            $p_post = $payment->get_post();
            $returned_history[$item_id]['type'] = __('Payment', 'sprout-invoices');
            $returned_history[$item_id]['status_type'] = 'payment';
            $returned_history[$item_id]['post_date'] = $p_post->post_date;
            $returned_history[$item_id]['update_title'] = $p_post->post_title;
            $returned_history[$item_id]['content'] = '';
            $returned_history[$item_id]['content'] .= '<span>' . $payment->get_payment_method() . '</span><br/>';
            $returned_history[$item_id]['content'] .= '<b>' . __('Payment Total', 'sprout-invoices') . ':</b> ' . sa_get_formatted_money($payment->get_amount(), $item_id);
        } else {
            if ($filtered) {
                $comment = get_comment($item_id);
                if (!is_wp_error($comment)) {
                    $returned_history[$item_id]['type'] = $comment->comment_author;
                    $returned_history[$item_id]['status_type'] = 'comment';
                    $returned_history[$item_id]['post_date'] = $comment->comment_date;
                    $returned_history[$item_id]['content'] = get_comment_text($comment->comment_ID);
                    $returned_history[$item_id]['comment_id'] = intval($comment->comment_ID);
                }
            }
        }
    }
    return $returned_history;
}
示例#29
0
 public static function status_change_dropdown($id)
 {
     if (!$id) {
         global $post;
         $id = $post->ID;
     }
     $invoice = SI_Invoice::get_instance($id);
     if (!is_a($invoice, 'SI_Invoice')) {
         return;
         // return for that temp post
     }
     self::load_view('admin/sections/invoice-status-change-drop', array('id' => $id, 'status' => $invoice->get_status()), false);
 }
 public static function total_invoice_data($this = 'century')
 {
     // Return cache if present.
     $cache = self::get_cache(__FUNCTION__ . $this);
     if ($cache) {
         return $cache;
     }
     $expire = self::CACHE_TIMEOUT;
     // Build data array, without a explicit build segments without posts will not show.
     $data = array('invoices' => 0, 'payments' => 0, 'totals' => 0, 'subtotals' => 0, 'paid' => 0, 'balance' => 0, 'status_temp' => 0, 'status_pending' => 0, 'status_partial' => 0, 'status_complete' => 0, 'status_writeoff' => 0);
     $args = array('post_type' => SI_Invoice::POST_TYPE, 'post_status' => array(SI_Invoice::STATUS_PENDING, SI_Invoice::STATUS_PARTIAL, SI_Invoice::STATUS_PAID), 'posts_per_page' => -1, 'fields' => 'ids');
     // If date filtered
     if ('century' !== $this) {
         switch ($this) {
             case 'week':
                 $args['date_query'] = array(array('week' => date('W', strtotime('this week')), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t'), strtotime('this Sunday')) - current_time('timestamp');
                 break;
             case 'lastweek':
                 $args['date_query'] = array(array('week' => date('W', strtotime('-1 week')), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t'), strtotime('this Sunday')) - current_time('timestamp');
                 break;
             case 'month':
                 $args['date_query'] = array(array('month' => date('m'), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t')) - current_time('timestamp');
                 break;
             case 'lastmonth':
                 $args['date_query'] = array(array('month' => date('m', strtotime('-1 month')), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t')) - current_time('timestamp');
                 break;
             case 'year':
                 $args['date_query'] = array(array('year' => date('Y'), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t'), strtotime('12/31')) - current_time('timestamp');
                 break;
             case 'lastyear':
                 $args['date_query'] = array(array('year' => date('Y', strtotime('-1  year')), 'inclusive' => true));
                 $expire = strtotime(date('Y-m-t'), strtotime('12/31')) - current_time('timestamp');
                 break;
             default:
                 break;
         }
     }
     $invoices = new WP_Query($args);
     foreach ($invoices->posts as $invoice_id) {
         $invoice = SI_Invoice::get_instance($invoice_id);
         $data['invoices'] += 1;
         $data['payments'] += count($invoice->get_payments());
         $data['totals'] += si_get_invoice_calculated_total($invoice_id);
         $data['subtotals'] += si_get_invoice_subtotal($invoice_id);
         $data['paid'] += si_get_invoice_payments_total($invoice_id);
         $data['balance'] += si_get_invoice_balance($invoice_id);
         switch (get_post_status($invoice_id)) {
             case 'draft':
             case SI_Invoice::STATUS_TEMP:
                 $data['status_temp'] += 1;
                 break;
             case SI_Invoice::STATUS_PENDING:
                 $data['status_pending'] += 1;
                 break;
             case SI_Invoice::STATUS_PARTIAL:
                 $data['status_partial'] += 1;
                 break;
             case SI_Invoice::STATUS_PAID:
                 $data['status_complete'] += 1;
                 break;
             case SI_Invoice::STATUS_WO:
                 $data['status_writeoff'] += 1;
                 break;
             default:
                 break;
         }
         unset($invoice);
     }
     return self::set_cache(__FUNCTION__ . $this, $data, $expire);
 }