function si_get_docs_project_id($id = 0)
 {
     if (!$id) {
         $id = get_the_ID();
     }
     $doc = si_get_doc_object($id);
     if ('' === $doc) {
         return 0;
     }
     return apply_filters('si_get_docs_project_id', $doc->get_project_id(), $doc);
 }
 function si_get_docs_project_id($id = 0)
 {
     if (!$id) {
         global $post;
         $id = $post->ID;
     }
     $doc = si_get_doc_object($id);
     if ('' === $doc) {
         return 0;
     }
     return apply_filters('si_get_docs_project_id', $doc->get_project_id(), $doc);
 }
 /**
  * Save the template selection for a doc by post id
  * @param  integer $post_id      
  * @param  string  $doc_template 
  * @return                 
  */
 public static function save_doc_project_selection($post_id = 0)
 {
     $doc_project = isset($_POST['doc_project']) ? $_POST['doc_project'] : '';
     $doc = si_get_doc_object($post_id);
     $doc->set_project_id($doc_project);
 }
 public static function maybe_create_doc_comment()
 {
     if (!isset($_REQUEST['doc_comment_sec'])) {
         self::ajax_fail('Forget something?');
     }
     $nonce = $_REQUEST['doc_comment_sec'];
     if (!wp_verify_nonce($nonce, SI_Controller::NONCE)) {
         self::ajax_fail('Not going to fall for it!');
     }
     if (!isset($_REQUEST['comment'])) {
         self::ajax_fail('No comment submitted.');
     }
     if ($_REQUEST['comment'] == '') {
         self::ajax_fail('No comment submitted.');
     }
     if (!isset($_REQUEST['id'])) {
         self::ajax_fail('No doc associated.');
     }
     $doc_id = (int) $_REQUEST['id'];
     $doc = si_get_doc_object($doc_id);
     if (!is_a($doc, 'SI_Estimate') && !is_a($doc, 'SI_Invoice')) {
         self::ajax_fail('Not correct post type.');
     }
     $comment = esc_textarea($_REQUEST['comment']);
     $item_position = isset($_REQUEST['item_position']) ? (double) $_REQUEST['item_position'] : 0;
     $comment_id = self::create_comment($doc_id, self::comment_data($doc_id, $comment), $item_position);
     if (!$comment_id) {
         self::ajax_fail('Something went wrong.');
     }
     $response_data = array('comment' => $comment, 'comment_id' => $comment_id, 'position' => $item_position, 'response' => __('Comment Received', 'sprout-invoices'));
     header('Content-type: application/json');
     if (self::DEBUG) {
         header('Access-Control-Allow-Origin: *');
     }
     echo wp_json_encode($response_data);
     exit;
 }
 public static function set_frequency_custom($doc_id = 0, $days = 0)
 {
     $doc = si_get_doc_object($doc_id);
     if (!is_object($doc)) {
         return 0;
     }
     $doc->save_post_meta(array(self::$meta_keys['frequency_custom'] => $days));
     return $days;
 }
function si_get_line_item_value($doc_id, $position, $data_slug)
{
    if (!$doc_id) {
        $doc_id = get_the_id();
    }
    $doc = si_get_doc_object($doc_id);
    if ('' === $doc) {
        return '';
    }
    $line_items = $doc->get_line_items();
    if (empty($line_items)) {
        return '';
    }
    $value = '';
    foreach ($line_items as $key => $data) {
        if (!isset($data[$data_slug])) {
            continue;
        }
        if ((double) $position === (double) $key) {
            $value = $data[$data_slug];
        }
    }
    return $value;
}
 public static function bulk_send_notifications($doc_ids = array())
 {
     foreach ($doc_ids as $doc_id) {
         $doc = si_get_doc_object($doc_id);
         $client = $doc->get_client();
         if (!is_a($client, 'SI_Client')) {
             continue;
         }
         $recipients = $client->get_associated_users();
         if (empty($recipients)) {
             continue;
         }
         if (is_a($doc, 'SI_Estimate')) {
             do_action('send_estimate', $doc, $recipients);
         } elseif (is_a($doc, 'SI_Invoice')) {
             do_action('send_invoice', $doc, $recipients);
             // If status is temp than change to pending.
             if ($doc->get_status() === SI_Invoice::STATUS_TEMP) {
                 $doc->set_pending();
             }
         }
     }
 }
 public static function set_renew_price($doc_id = 0, $renew_price = '')
 {
     $doc = si_get_doc_object($doc_id);
     if (!is_object($doc)) {
         return 0;
     }
     $doc->save_post_meta(array(self::$meta_keys['renew_price'] => $renew_price));
     return $renew_price;
 }
 public static function username($atts = array())
 {
     $id = 0;
     if (si_get_doc_context() == 'estimate') {
         $id = si_get_estimate_id();
     } else {
         $id = si_get_invoice_id();
     }
     $doc = si_get_doc_object($id);
     if (!$doc->get_client_id()) {
         return '';
     }
     $user = si_default_client_user($client_id);
     if (!$user->exists()) {
         return '';
     }
     return $user->display_name;
 }