/** * 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; }
/** 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; } }
/** * 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; } }