Ejemplo n.º 1
0
 /**
  * Does our preprocessing for the manage invoice page, adds our meta boxes, and checks invoice data
  *
  * @since 3.0
  */
 static function page_manage_invoice_preprocess($screen_id)
 {
     global $wpi_settings, $this_invoice, $wpdb;
     // Check if invoice_id already exists
     $invoice_id_exists = false;
     if (!empty($_REQUEST['wpi'])) {
         if (!empty($_REQUEST['wpi']['new_invoice'])) {
             if (wpi_check_invoice($_REQUEST['wpi']['new_invoice']['invoice_id'])) {
                 $invoice_id_exists = true;
             }
         }
         if (!empty($_REQUEST['wpi']['existing_invoice'])) {
             if (wpi_check_invoice($_REQUEST['wpi']['existing_invoice']['invoice_id'])) {
                 $invoice_id_exists = true;
             }
         }
     }
     if ($invoice_id_exists) {
         // Select status of invoice from DB
         $status = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = '{$_REQUEST['wpi']['existing_invoice']['invoice_id']}' AND meta_key = 'status'");
     }
     // New Invoice
     if (isset($_REQUEST['wpi']['new_invoice']) && empty($invoice_id_exists)) {
         $this_invoice = new WPI_Invoice();
         $this_invoice->create_new_invoice("invoice_id={$_REQUEST['wpi']['new_invoice']['invoice_id']}");
         // If we are copying from a template
         if (!empty($_REQUEST['wpi']['new_invoice']['template_copy'])) {
             $this_invoice->load_template("id={$_REQUEST['wpi']['new_invoice']['template_copy']}");
         }
         // Set user and determine type
         $this_invoice->load_user("email={$_REQUEST['wpi']['new_invoice']['user_email']}");
         // Add custom data if user doesn't exist.
         if (empty($this_invoice->data['user_data'])) {
             $this_invoice->data['user_data'] = array('user_email' => $_REQUEST['wpi']['new_invoice']['user_email']);
         }
         $new_invoice = true;
         // Enter in GET values
         if (isset($_GET['prefill']['subject'])) {
             $this_invoice->data['subject'] = $_GET['prefill']['subject'];
         }
         if (!empty($_GET['prefill']['is_quote']) && $_GET['prefill']['is_quote'] == 'true') {
             $this_invoice->data['is_quote'] = true;
             $this_invoice->data['status'] = "quote";
         }
     } else {
         if (!empty($invoice_id_exists)) {
             // Existing Invoice
             $this_invoice = new WPI_Invoice();
             if (isset($_REQUEST['wpi']['existing_invoice']['invoice_id'])) {
                 $ID = $_REQUEST['wpi']['existing_invoice']['invoice_id'];
             } else {
                 if (isset($_REQUEST['wpi']['new_invoice']['invoice_id'])) {
                     $ID = $_REQUEST['wpi']['new_invoice']['invoice_id'];
                 }
             }
             $this_invoice->load_invoice("id={$ID}");
         }
     }
     add_meta_box('postbox_payment_methods', __('Payment Settings', WPI), 'postbox_payment_methods', $screen_id, 'normal', 'high');
     if (is_object($this_invoice) && isset($this_invoice->data['type']) && $this_invoice->data['type'] == 'single_payment') {
         add_meta_box('postbox_overview', __('Overview', WPI), 'postbox_overview', $screen_id, 'side', 'high');
     } else {
         add_meta_box('postbox_publish', __('Publish', WPI), 'postbox_publish', $screen_id, 'side', 'high');
     }
     add_meta_box('postbox_user_existing', __('User Information', WPI), 'postbox_user_existing', $screen_id, 'side', 'low');
 }
Ejemplo n.º 2
0
 /**
  * Create new invoice
  *
  * @param array $args
  *
  * @return WPI_Invoice
  * @see WPI_Invoice
  * @uses Internal API of plugin
  */
 function create_invoice($args = array())
 {
     global $wpi_settings;
     //** Default arguments */
     $defaults = array('custom_id' => false, 'subject' => false, 'description' => false, 'type' => false, 'user_data' => array('user_email' => false, 'first_name' => false, 'last_name' => false, 'phonenumber' => false, 'streetaddress' => false, 'city' => false, 'state' => false, 'zip' => false, 'country' => false), 'deposit' => false, 'due_date' => array('year' => false, 'month' => false, 'day' => false), 'currency' => false, 'tax' => false, 'tax_method' => false, 'recurring' => array('unit' => false, 'length' => false, 'cycles' => false, 'send_invoice_automatically' => false, 'start_date' => array('month' => false, 'day' => false, 'year' => false)), 'status' => false, 'discount' => array('name' => false, 'type' => false, 'amount' => false), 'items' => array(), 'charges' => array());
     //** Parse arguments */
     extract($args = wp_parse_args($args, $defaults));
     //** If empty subject - return error */
     if (!$subject) {
         return new WP_Error('wp.invoice', __('Method requires "subject" argument to be passed.', WPI), $args);
     }
     //** If empty user_email - return error */
     if (!$user_data['user_email']) {
         return new WP_Error('wp.invoice', __('Method requires "user_email" in "user_data" argument to be passed.', WPI), $args);
     }
     if (!filter_var($user_data['user_email'], FILTER_VALIDATE_EMAIL)) {
         return new WP_Error('wp.invoice', __('User Email is malformed.', WPI), $args);
     }
     //** Items/Charges check */
     if (empty($items) && empty($charges)) {
         return new WP_Error('wp.invoice', __('Method requires "items" or "charges" argument to be passed.', WPI), $args);
     }
     //** If type is registered */
     if (!array_key_exists($type, $wpi_settings['types'])) {
         return new WP_Error('wp.invoice', __('Unknown invoice type.', WPI), $args);
     }
     //** If recurring */
     if ($type == 'recurring') {
         $recurring = array_filter($recurring);
         if (empty($recurring['unit']) || empty($recurring['cycles'])) {
             return new WP_Error('wp.invoice', __('Method requires correct "recurring" argument if "type" is recurring.', WPI), $args);
         }
         if (!empty($deposit)) {
             return new WP_Error('wp.invoice', __('Cannot use "deposit" with "recurring" type.', WPI), $args);
         }
     }
     //** If quote */
     if ($type == 'quote') {
         if (!empty($deposit)) {
             return new WP_Error('wp.invoice', __('Cannot use "deposit" with "quote" type.', WPI), $args);
         }
     }
     //** Check status */
     if (!$status) {
         return new WP_Error('wp.invoice', __('Method requires "status" argument to be passed.', WPI), $args);
     }
     if (!array_key_exists($status, $wpi_settings['invoice_statuses'])) {
         return new WP_Error('wp.invoice', __('Unknown invoice status.', WPI), $args);
     }
     //** New Invoice object */
     $invoice = new WPI_Invoice();
     //** Load invoice by ID */
     $invoice->create_new_invoice($args);
     //** Set type */
     $invoice->set(array('type' => $type));
     //** If quote */
     if ($type == 'quote') {
         $invoice->set(array('status' => $type));
         $invoice->set(array('is_quote' => 'true'));
     }
     //** Recurring */
     if ($type == 'recurring') {
         $invoice->create_schedule($recurring);
     }
     //** Try loading user by email */
     $invoice->load_user(array('email' => $user_data['user_email']));
     //** If new user - add data to his object */
     if (empty($invoice->data['user_data'])) {
         $invoice->data['user_data'] = $user_data;
     }
     //** Create/Update user if need */
     WPI_Functions::update_user($user_data);
     //** Try loading user by email again */
     $invoice->load_user(array('email' => $user_data['user_email']));
     //** Partial payments */
     if ($deposit) {
         $invoice->set(array('deposit_amount' => $deposit));
     } else {
         $invoice->set(array('deposit_amount' => 0));
     }
     //** Due date */
     $invoice->set(array('due_date_year' => $due_date['year']));
     $invoice->set(array('due_date_month' => $due_date['month']));
     $invoice->set(array('due_date_day' => $due_date['day']));
     //** Currency */
     $invoice->set(array('default_currency_code' => $currency));
     //** Tax */
     $invoice->set(array('tax' => $tax));
     //** Status */
     $invoice->set(array('post_status' => $status));
     //** Discount */
     $discount = array_filter($discount);
     if (!empty($discount)) {
         if (empty($discount['name'])) {
             return new WP_Error('wp.invoice', __('Discount name is required.', WPI), $args);
         }
         if (empty($discount['type'])) {
             return new WP_Error('wp.invoice', __('Discount type is required. ("amount" or "percent").', WPI), $args);
         }
         if (empty($discount['amount'])) {
             return new WP_Error('wp.invoice', __('Discount amount is required.', WPI), $args);
         }
         $invoice->add_discount($discount);
     }
     //** Items */
     foreach ($items as $item) {
         //** Do not allow to save melformed items */
         if (empty($item['name']) || empty($item['quantity']) || empty($item['price'])) {
             return new WP_Error('wp.invoice', __('One or more "items" have malformed structure. Cannot create Invoice.', WPI), $args);
         }
         //** Global tax has higher priority */
         if (!empty($tax)) {
             $item['tax_rate'] = $tax;
         }
         //** Check types */
         if (!is_numeric($item['quantity'])) {
             return new WP_Error('wp.invoice', __('One or more "items" have wrong "quantity" value. Cannot create Invoice.', WPI), $args);
         }
         if (!is_numeric($item['price'])) {
             return new WP_Error('wp.invoice', __('One or more "items" have wrong "price" value. Cannot create Invoice.', WPI), $args);
         }
         if (!empty($item['tax_rate'])) {
             if (!is_numeric($item['tax_rate'])) {
                 return new WP_Error('wp.invoice', __('One or more "items" have wrong "tax_rate" value. Cannot create Invoice.', WPI), $args);
             }
         }
         //** If passed validation - save item */
         $invoice->line_item($item);
     }
     //** Charges */
     foreach ($charges as $charge) {
         //** Do not allow to save melformed items */
         if (empty($charge['name']) || empty($charge['amount'])) {
             return new WP_Error('wp.invoice', __('One or more "charges" have malformed structure. Cannot create Invoice.', WPI), $args);
         }
         //** Global tax has higher priority */
         if (!empty($tax)) {
             $charge['tax'] = $tax;
         }
         //** Check types */
         if (!is_numeric($charge['amount'])) {
             return new WP_Error('wp.invoice', __('One or more "charges" have wrong "amount" value. Cannot create Invoice.', WPI), $args);
         }
         if (!empty($charge['tax'])) {
             if (!is_numeric($charge['tax'])) {
                 return new WP_Error('wp.invoice', __('One or more "charges" have wrong "tax" value. Cannot create Invoice.', WPI), $args);
             }
         }
         //** If passed validation - save item */
         $invoice->line_charge($charge);
     }
     //** Set tax method */
     if (!empty($tax_method)) {
         if ($tax_method != 'before_discount' && $tax_method != 'after_discount') {
             return new WP_Error('wp.invoice', __('Unknown "tax_method".', WPI), $args);
         }
     }
     $invoice->set(array('tax_method' => $tax_method));
     //** Save */
     $invoice->save_invoice();
     //** Return saved object */
     return $invoice;
 }