Exemple #1
0
/**
 * Converted to WP 2.0
 * Archives an invoice, or multiple invoices.
 *
 * @global type $wpdb
 *
 * @param type $invoice_id
 *
 * @return type
 */
function wpi_archive_invoice($invoice_id)
{
    //** Check to see if array is passed or single. */
    if (is_array($invoice_id)) {
        $counter = 0;
        foreach ($invoice_id as $single_invoice_id) {
            $this_invoice = new WPI_Invoice();
            $this_invoice->load_invoice("id={$single_invoice_id}");
            $this_invoice->set("status=archive");
            $this_invoice->add_entry(__("Archived.", WPI));
            if ($this_invoice->save_invoice()) {
                $counter++;
            }
        }
        return __("{$counter}  invoice(s) archived.", WPI);
    } else {
        $this_invoice = new WPI_Invoice();
        $this_invoice->load_invoice("id={$invoice_id}");
        $this_invoice->set("status=archive");
        $this_invoice->add_entry(__("Archived.", WPI));
        if ($this_invoice->save_invoice()) {
            return __('Successfully archived.', WPI);
        }
    }
}
/**
    Converted to WP 2.0
    Archives an invoice, or multiple invoices.
*/
    function wpi_archive_invoice($invoice_id) {
        global $wpdb;
        // Check to see if array is passed or single.
        if(is_array($invoice_id))
        {
            $counter=0;
            foreach ($invoice_id as $single_invoice_id) {
                $this_invoice = new WPI_Invoice();
                $this_invoice->load_invoice("id=$single_invoice_id");
                $this_invoice->set("status=archive");
                $this_invoice->add_entry(__("Archived.", WPI));
                if($this_invoice->save_invoice())
                    $counter++;
            }
            return __("$counter  invoice(s) archived.", WPI);
        } else {
            $this_invoice = new WPI_Invoice();
            $this_invoice->load_invoice("id=$invoice_id");
            $this_invoice->set("status=archive");
            $this_invoice->add_entry(__("Archived.", WPI));
            if($this_invoice->save_invoice())
                return __('Successfully archived.', WPI);
        }
    }
  /**
    Handles saving and updating
    Can also handle AJAX save/update function
   */
  function save_invoice($invoice, $args = '') {
    //die( json_encode($invoice) );

    /* Set function additional params */
    $defaults = array(
        'type' => 'default'
    );
    extract(wp_parse_args($args, $defaults), EXTR_SKIP);

    if ($type != 'import') {
      if (!wp_verify_nonce($_REQUEST['nonce'], 'wpi-update-invoice')) {
        die('Security check');
      }
    }

    /* Init New Invoice object from passed variables */
    $ni = new WPI_Invoice();

    $ni->set("ID={$invoice['ID']}");
    $ni->set("invoice_id={$invoice['invoice_id']}");

    //$ni->set("terms_acceptance_required={$invoice['terms_acceptance_required']}");

    $ni->set("subject={$invoice['subject']}");
    $ni->set("description={$invoice['description']}");

    //$ni->set("watermark={$invoice['meta']['watermark']}");

    if ($invoice['deposit'] == 'on' || $invoice['deposit'] == 'true') {
      $ni->set("deposit_amount={$invoice['deposit_amount']}");
    } else {
      $ni->set("deposit_amount=0");
    }

    $ni->set("due_date_year={$invoice['due_date_year']}");
    $ni->set("due_date_month={$invoice['due_date_month']}");
    $ni->set("due_date_day={$invoice['due_date_day']}");

    $ni->set("default_currency_code={$invoice['default_currency_code']}");

    if (!empty($invoice['meta']['terms'])) {
      $ni->set("terms={$invoice['meta']['terms']}");
    }
    $ni->set("tax={$invoice['meta']['tax']}");

    $ni->set("custom_id={$invoice['meta']['custom_id']}");

    /**
     * DETECTING INVOICE TYPE
     * (Changes for ability to use premium feature Quotes)
     *
     * @author Anton Korotkov
     *
     * There are three available types by default:
     *    - invoice
     *    - recurring
     */
    // 'invoice' is by default
    $invoice_type = 'invoice';

    // If $invoice object has type definition then use it
    if ( !empty( $invoice['type'] ) ) {
      $invoice_type = $invoice['type'];
    }
    
    // Save status of invoice (quote or not quote)
    if(isset ($invoice['quote'])) {
      if($invoice['quote'] == "on") {
        $ni->set("status=quote");
        $ni->set("is_quote=true");
        $invoice_type = 'quote';
      } else {
        $ni->set("status=null");
      }
    }

    // But if recurring settings are defined then invoice type should be recurring
    if ($invoice['recurring']['active'] == 'on' && !empty($invoice['recurring']['cycles'])) {
      $ni->create_schedule("unit={$invoice['recurring']['unit']}&length={$invoice['recurring']['length']}&cycles={$invoice['recurring']['cycles']}&send_invoice_automatically={$invoice['recurring']['send_invoice_automatically']}&start_date[month]={$invoice['recurring']['start_date']['month']}&start_date[day]={$invoice['recurring']['start_date']['day']}&start_date[year]={$invoice['recurring']['start_date']['year']}");
      $invoice_type = 'recurring';
    }

    // Finally set invoice type
    $ni->set("type=$invoice_type");

    /* Set invoice status */
    $status = (!empty($invoice['post_status']) ? $invoice['post_status'] : 'active');
    $ni->set("post_status={$status}");

    /* Add discounts if exist */
    if (is_array($invoice['meta']['discount'])) {
      foreach ($invoice['meta']['discount'] as $discount) {
        if (!empty($discount['name']) && !empty($discount['amount'])) {
          $ni->add_discount("name={$discount['name']}&type={$discount['type']}&amount={$discount['amount']}");
        }
      }
    }

    if (!empty($invoice['client_change_payment_method'])) {
      $ni->set("client_change_payment_method={$invoice['client_change_payment_method']}");
    }
    $ni->set("default_payment_method={$invoice['default_payment_method']}");

    $ni->set("tax_method={$invoice['tax_method']}");

    // It's bad idea to clear log, because all neccessary data such as payment information exist there
    //$ni->admin("clear_log={$invoice['admin']['clear_log']}");

    /* Manually set billing settings due to the complexity of the hierarchy */
    $ni->data['billing'] = !empty($invoice['billing']) ? $invoice['billing'] : array();

    /* Add line items */
    foreach ($invoice['itemized_list'] as $line_item) {
      $ni->line_item("name={$line_item['name']}&description={$line_item['description']}&quantity={$line_item['quantity']}&price={$line_item['price']}&tax_rate={$line_item['tax']}");
    }

    /* Add line items for charges */
    if (!empty($invoice['itemized_charges'])) {
      foreach ($invoice['itemized_charges'] as $charge_item) {
        $ni->line_charge("name={$charge_item['name']}&amount={$charge_item['amount']}&tax={$charge_item['tax']}");
      }
    }

    /*
     * Save Invoice Object to DB and update user
     * (trimming is a precaution because it could cause problems in inserted in DB w/ whitespace on end)
     */
    $ni->set("user_email=" . trim($invoice['user_data']['user_email']));

    if ($type != 'import') {
      WPI_Functions::update_user($invoice['user_data']);
    }

    $invoice_id = $ni->save_invoice();
    if ($invoice_id) {
      return $invoice_id;
    } else {
      return false;
    }
  }
Exemple #4
0
 /**
  * Update invoice by ID
  *
  * @global Array $wpi_settings
  *
  * @param Array $args
  *
  * @return WP_Error|WPI_Invoice
  */
 function update_invoice($args = array())
 {
     global $wpi_settings;
     //** Default arguments */
     $defaults = array('ID' => false, 'subject' => false, 'description' => false, 'type' => false, 'deposit' => false, 'due_date' => false, 'tax' => false, 'tax_method' => false, 'recurring' => false, 'discount' => false, 'items' => array(), 'charges' => array());
     //** Parse arguments */
     extract($args = wp_parse_args($args, $defaults));
     //** Check */
     if (!$ID) {
         return new WP_Error('wp.invoice', __('Argument "ID" is required.', WPI), $args);
     }
     //** New Invoice object */
     $invoice = new WPI_Invoice();
     //** Load invoice by ID */
     $invoice->load_invoice(array('id' => $ID));
     $set = array();
     //** Subject */
     if ($subject) {
         $subject = trim($subject);
         if (!empty($subject)) {
             $set['subject'] = $subject;
             $set['post_title'] = $subject;
         }
     }
     //** Description */
     if ($description) {
         $description = trim($description);
         if (!empty($description)) {
             $set['description'] = $description;
         }
     }
     if ($type) {
         //** 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);
             }
         }
         $set['type'] = $type;
         //** If quote */
         if ($type == 'quote') {
             $set['status'] = $type;
             $set['is_quote'] = 'true';
         }
         //** Recurring */
         if ($type == 'recurring') {
             $invoice->create_schedule($recurring);
         }
     }
     //** Partial payments */
     if ($deposit) {
         $set['deposit_amount'] = (double) $deposit;
     }
     if ($due_date) {
         $set['due_date_year'] = $due_date['year'];
         $set['due_date_month'] = $due_date['month'];
         $set['due_date_day'] = $due_date['day'];
     }
     if ($tax) {
         $set['tax'] = $tax;
     }
     if ($tax_method) {
         if ($tax_method != 'before_discount' && $tax_method != 'after_discount') {
             return new WP_Error('wp.invoice', __('Unknown "tax_method".', WPI), $args);
         }
         $set['tax_method'] = $tax_method;
     }
     if ($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->data['discount'] = array();
         $invoice->add_discount($discount);
     }
     if ($items) {
         //** 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 ($charges) {
         //** 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 */
     if ($charges) {
         $invoice->data['itemized_charges'] = array();
         foreach ($charges as $charge) {
             $invoice->line_charge($charge);
         }
     }
     if ($items) {
         $invoice->data['itemized_list'] = array();
         foreach ($items as $item) {
             $invoice->line_item($item);
         }
     }
     $invoice->set($set);
     $invoice->save_invoice();
     $invoice = new WPI_Invoice();
     //** Load invoice by ID */
     $invoice->load_invoice(array('id' => $ID));
     return $invoice;
 }
Exemple #5
0
 /**
  * Handles saving and updating
  * Can also handle AJAX save/update function
  *
  * @param type $invoice
  * @param type $args
  *
  * @return boolean
  */
 static function save_invoice($invoice, $args = '')
 {
     //** Set function additional params */
     $defaults = array('type' => 'default');
     extract(wp_parse_args($args, $defaults), EXTR_SKIP);
     if ($type != 'import') {
         if (!wp_verify_nonce($_REQUEST['nonce'], 'wpi-update-invoice')) {
             die('Security check');
         }
     }
     //** Init New Invoice object from passed variables */
     $ni = new WPI_Invoice();
     //** ID */
     $ni->set(array('ID' => $invoice['ID']));
     //** invoice_id */
     $ni->set(array('invoice_id' => $invoice['invoice_id']));
     //** subject */
     $ni->set(array('subject' => $invoice['subject']));
     //** description */
     $ni->set(array('description' => $invoice['description']));
     //** deposit */
     if ($invoice['deposit'] == 'on' || $invoice['deposit'] == 'true') {
         $ni->set(array('deposit_amount' => $invoice['deposit_amount']));
     } else {
         $ni->set(array('deposit_amount' => 0));
     }
     //** Due date */
     $ni->set(array('due_date_year' => $invoice['due_date_year']));
     $ni->set(array('due_date_month' => $invoice['due_date_month']));
     $ni->set(array('due_date_day' => $invoice['due_date_day']));
     //** Currency */
     $ni->set(array('default_currency_code' => $invoice['default_currency_code']));
     //** Terms? */
     if (!empty($invoice['meta']['terms'])) {
         $ni->set(array('terms' => $invoice['meta']['terms']));
     }
     //** Tax */
     $ni->set(array('tax' => $invoice['meta']['tax']));
     //** Custom ID */
     $ni->set(array('custom_id' => $invoice['meta']['custom_id']));
     //** type is 'invoice' by default */
     $invoice_type = 'invoice';
     //** If $invoice object has type definition then use it */
     if (!empty($invoice['type'])) {
         $invoice_type = $invoice['type'];
     }
     //** Save status of invoice (quote or not quote) */
     if (isset($invoice['quote'])) {
         if ($invoice['quote'] == "on") {
             $ni->set(array('status' => 'quote'));
             $ni->set(array('is_quote' => 'true'));
             $invoice_type = 'quote';
         } else {
             $ni->set(array('status' => 'null'));
         }
     }
     //** But if recurring settings are defined then invoice type should be recurring */
     if ($invoice['recurring']['active'] == 'on' && !empty($invoice['recurring'])) {
         $ni->create_schedule($invoice['recurring']);
         $invoice_type = 'recurring';
     }
     //** Finally set invoice type */
     $ni->set(array('type' => $invoice_type));
     //** Set invoice status */
     $status = !empty($invoice['post_status']) ? $invoice['post_status'] : 'active';
     $ni->set(array('post_status' => $status));
     //** Add discounts if exist */
     if (is_array($invoice['meta']['discount'])) {
         foreach ($invoice['meta']['discount'] as $discount) {
             if (!empty($discount['name']) && !empty($discount['amount'])) {
                 $ni->add_discount(array('name' => $discount['name'], 'type' => $discount['type'], 'amount' => $discount['amount']));
             }
         }
     }
     //** Ability to change payment method */
     if (!empty($invoice['client_change_payment_method'])) {
         $ni->set(array('client_change_payment_method' => $invoice['client_change_payment_method']));
     }
     //** Ability to turn off all payment methods and turn on manual that way */
     if (!empty($invoice['use_manual_payment'])) {
         $ni->set(array('use_manual_payment' => $invoice['use_manual_payment']));
     }
     //** Default payment method */
     $ni->set(array('default_payment_method' => $invoice['default_payment_method']));
     //** Tax method */
     $ni->set(array('tax_method' => $invoice['tax_method']));
     //** Manually set billing settings due to the complexity of the hierarchy */
     $ni->data['billing'] = !empty($invoice['billing']) ? $invoice['billing'] : array();
     //** Add line items */
     foreach ($invoice['itemized_list'] as $line_item) {
         $ni->line_item(array('name' => $line_item['name'], 'description' => $line_item['description'], 'quantity' => $line_item['quantity'], 'price' => $line_item['price'], 'tax_rate' => $line_item['tax']));
     }
     //** Add line items for charges */
     if (!empty($invoice['itemized_charges'])) {
         foreach ($invoice['itemized_charges'] as $charge_item) {
             $ni->line_charge(array('name' => $charge_item['name'], 'amount' => $charge_item['amount'], 'tax' => $charge_item['tax']));
         }
     }
     /**
      * Save Invoice Object to DB and update user
      * (trimming is a precaution because it could cause problems in inserted in DB w/ whitespace on end)
      */
     $ni->set(array('user_email' => trim($invoice['user_data']['user_email'])));
     if ($type != 'import') {
         WPI_Functions::update_user($invoice['user_data']);
     }
     $invoice_id = $ni->save_invoice();
     if ($invoice_id) {
         return $invoice_id;
     } else {
         return false;
     }
 }