Changeset 2248680
- Timestamp:
- 02/22/2020 10:37:13 AM (6 years ago)
- Location:
- apollo/trunk
- Files:
-
- 6 edited
-
apollo.php (modified) (6 diffs)
-
includes/admin/invoice.php (modified) (22 diffs)
-
includes/admin/settings/general.php (modified) (10 diffs)
-
includes/admin/settings/main-settings.php (modified) (6 diffs)
-
includes/apollo.php (modified) (8 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
apollo/trunk/apollo.php
r2102108 r2248680 4 4 * Plugin URI: https://wordpress.org/plugins/apollo 5 5 * Description: Manually or automatically generate invoices and send PDFs as attachments for WooCommerce orders. 6 * Version: 1.1. 26 * Version: 1.1.3 7 7 * Author: Studio404 8 8 * Text Domain: apollo 9 9 * Domain Path: /languages 10 * WC tested up to: 3.6.310 * WC tested up to: 5.3.2 11 11 * WC requires at least: 3.0 12 12 */ … … 14 14 defined( 'ABSPATH' ) or exit; 15 15 16 define( 'APOLLO_VERSION', '1.1. 2' );16 define( 'APOLLO_VERSION', '1.1.3' ); 17 17 18 18 function apollo_load_plugin() { 19 19 20 // define variables and include all files that are needed 20 21 if ( ! class_exists( 'WooCommerce' ) ) { 21 22 return; … … 25 26 define( 'APOLLO_FILE', __FILE__ ); 26 27 } 27 28 28 29 29 if ( ! defined( 'APOLLO_DIR' ) ) { … … 62 62 } 63 63 64 // calls funtion above with WP plugins_loaded hook 64 65 add_action( 'plugins_loaded', 'apollo_load_plugin' ); 65 66 66 67 add_filter( 'woocommerce_email_attachments', 'add_apollo_document_to_email', 10, 3 ); 67 68 function add_apollo_document_to_email( $attachments, $status, $order ) { 68 69 // this is called when Woocommerce email is sent, functions creates/sends apollo documents based on settings 69 70 $lang = explode('_', get_locale())[0]; 70 71 71 $auto_invoice = (bool) get_option('apollo_general_settings')['apollo_send-invoice']; 72 $auto_invoice_bank = (bool) get_option('apollo_general_settings')['apollo_send-bank-invoice']; 72 $auto_invoice = (bool) get_option('apollo_general_settings')['apollo_send-invoice']; // send invoice automatically setting 73 $auto_invoice_bank = (bool) get_option('apollo_general_settings')['apollo_send-bank-invoice']; // send invoice automatically when payment is bank transfer 73 74 // $auto_invoice_status = get_option('apollo_general_settings')['apollo_invoice-status']; 74 $invoice_id = get_post_meta( $order->get_id(), 'apollo_invoice_id', true); 75 $invoice_id = get_post_meta( $order->get_id(), 'apollo_invoice_id', true); // gets apollo invoice id, if it exist 75 76 $invoice_number = get_post_meta( $order->get_id(), 'apollo_invoice_number', true); 76 77 $pdf_invoice_path = APOLLO_DOCUMENTS_DIR."/invoice - ".$invoice_number.".pdf"; 77 78 78 79 79 $auto_estimate = (bool) get_option('apollo_general_settings')['apollo_send-estimate']; 80 $auto_estimate = (bool) get_option('apollo_general_settings')['apollo_send-estimate']; // send estimate automatically setting 80 81 $estimate_number = get_post_meta( $order->get_id(), 'apollo_estimate_number', true); 81 82 $estimate_id = get_post_meta( $order->get_id(), 'apollo_estimate_id', true); 82 83 $pdf_estimate_path = APOLLO_DOCUMENTS_DIR."/estimate - ".$estimate_number.".pdf"; 83 84 $payment_type = 'apollo_payment-'.$order->get_payment_method(); 85 86 //chechk if order payment matches any of the payments set in apollo settings 84 87 $payment_enabled = isset(get_option('apollo_general_settings')[$payment_type]) ? (bool) get_option('apollo_general_settings')[$payment_type] : false; 85 86 $order_paid = get_post_meta( $order->get_id(), '_date_paid', true);87 88 88 89 if ($lang === 'sl') { … … 126 127 } 127 128 128 // define the woocommerce_new_order callback 129 add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_new_order', 10, 3 ); 129 130 function action_woocommerce_new_order( $order_id ) { 130 131 // this function is called when new order is created in Woocommerce 132 131 133 $order = wc_get_order($order_id); 132 134 133 135 $payment_type = 'apollo_payment-'.$order->get_payment_method(); 136 137 //chechk if order payment matches any of the payments set in apollo settings 134 138 $payment_enabled = isset(get_option('apollo_general_settings')[$payment_type]) ? (bool) get_option('apollo_general_settings')[$payment_type] : false; 135 136 $order_paid = get_post_meta( $order->get_id(), '_date_paid', true);137 138 139 139 140 if ( $order->get_payment_method() === 'bacs') { // new order; bank transfer 140 141 Apollo_invoice::create($order_id, 'estimate'); 141 } else if ($payment_enabled && $order_paid !== '') {142 } else if ($payment_enabled) { 142 143 Apollo_invoice::create($order_id, 'invoice'); 143 144 } 144 145 }; 145 146 146 // add the action 147 add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_new_order', 10, 3 ); 147 add_action( 'woocommerce_order_status_completed', 'action_woocommerce_status_completed'); 148 function action_woocommerce_status_completed ($order_id) { 149 // this function is called when order is marked as completed 148 150 149 function action_woocommerce_status_completed ($order_id) {150 151 $order = wc_get_order($order_id); 151 152 … … 160 161 } 161 162 } 162 163 add_action( 'woocommerce_order_status_completed', 'action_woocommerce_status_completed'); -
apollo/trunk/includes/admin/invoice.php
r2102108 r2248680 5 5 if ( ! class_exists( 'Apollo_invoice' ) ) { 6 6 abstract class Apollo_invoice { 7 8 //create Apollo invoice or estimate 7 9 public static function create( $order_id, $type = 'invoice' ) { 8 9 10 $invoice_exsists = Apollo_invoice::getInvoice($order_id); 10 11 $estimate_exsists = Apollo_invoice::getEstimate($order_id); 11 12 13 // if document was already created, then just return it 12 14 if ($type === 'invoice' && $invoice_exsists) { 13 15 $invoice_exsists['exsists'] = true; … … 17 19 return $estimate_exsists; 18 20 } 21 19 22 $order = wc_get_order($order_id); 20 23 $output = get_option('apollo_general_settings'); 21 24 $token = $output['apollo_token']; 22 25 $organization_id = $output['apollo_organization-id']; 23 26 $add_sku = (bool) get_option('apollo_general_settings')['apollo_add-sku']; 24 27 25 28 $fiscalization = (bool) $output['apollo_enable-fiscalization']; … … 30 33 $device = $output['apollo_device-id']; 31 34 32 $test = Spaceinvoices\Spaceinvoices::setAccessToken($token);33 34 35 if (!$order || !$output || !$token || !$organization_id) { 35 36 return array("error" => "Apollo error: Missing data."); 36 37 } 37 38 38 $SI_products_data = array(); 39 $SI_products_data = array(); // we will fill this with items that are in order 39 40 $SI_total = 0; 40 41 $order_total = 0; … … 46 47 } 47 48 49 // get data for each item 48 50 foreach ($order->get_items() as $item_id => $item_data) { 49 51 $product = $item_data->get_data(); … … 54 56 $tax_percent = ($tax_amount / $without_tax) * 100; 55 57 58 // Aggregate data for Apollo 56 59 $product_data = array( 57 60 'name' => $product['name'], … … 62 65 ); 63 66 64 if($productData->get_sku() != '') { 65 $product_data['SKU'] = $productData->get_sku(); 66 } 67 68 if (floatval($product['total']) < floatval($product['subtotal'])) { // item is discounted 67 68 $sku = $productData->get_sku(); 69 70 // add SKU code if item has one 71 if($sku != '') { 72 $product_data['SKU'] = $sku; 73 74 // if 'Add SKU code to invoice' is enabled, add it to description 75 if($add_sku) { 76 $product_data['description'] = 'SKU: '.$sku; 77 } 78 } 79 80 // if numbers don't match, we assume item was discounted (since there is no way of telling otherwise... atleast for now?) 81 if (floatval($product['total']) < floatval($product['subtotal'])) { 69 82 $product_data['discount'] = round((1 - (floatval($product['total']) / floatval($product['subtotal']))) * 100); 70 83 $product_data['price'] = floatval($product['subtotal']) / $product['quantity']; … … 80 93 } 81 94 95 // get shippings data (can be multiple) 82 96 foreach( $order->get_items('shipping') as $item_id => $item_shipping ) { 83 97 $shipping = $item_shipping->get_data(); … … 100 114 } 101 115 102 116 // get all extra fees 103 117 foreach( $order->get_items('fee') as $item_id => $item_fee ) { 104 118 $fee = $item_fee->get_data(); … … 108 122 109 123 $tax_percent = ($tax_amount / $without_tax) * 100; 110 111 124 112 125 $unit = 'fee'; … … 122 135 'price' => $without_tax 123 136 ); 137 124 138 if ($tax_percent != 0 && $tax_amount != 0 ) { 125 139 $fee_data['_documentItemTaxes'] = array(array('rate' => $tax_percent)); … … 128 142 } 129 143 130 144 // aggreagte order data for Apollo 131 145 $order_data = array( 132 146 "type" => $type, … … 147 161 ); 148 162 163 // if fiscalization is enabled and we making invoice, add the fiscalization data 149 164 if ($fiscalization && $type === 'invoice') { 150 165 $order_data['_furs'] = array( … … 160 175 } 161 176 162 if ($type === 'invoice') { 177 // if this is invoice and payment type IS NOT 'Cash on delivery', mark invoice as paid 178 if ($type === 'invoice' && $order->get_payment_method() !== 'cod') { 163 179 $order_data['payments'] = array(array( 164 180 "type" => "other", … … 172 188 return array("error" => "Apollo error (".$create->error->statusCode."): ".$create->error->text); 173 189 } 190 174 191 $document_id = $create->id; 175 192 $document_number = $create->number; … … 182 199 ); 183 200 201 // save invoice/estimate data to order 184 202 if ($type === 'invoice') { 185 203 update_post_meta( $order_id, 'apollo_invoice_id', $document_id ); … … 199 217 } 200 218 219 // get invoice for order 201 220 public static function getInvoice( $order_id ) { 202 221 $id = get_post_meta( $order_id, 'apollo_invoice_id', true); … … 210 229 } 211 230 231 // get estimate for order 212 232 public static function getEstimate( $order_id ) { 213 233 $id = get_post_meta( $order_id, 'apollo_estimate_id', true); … … 221 241 } 222 242 243 // get PDF from Apoll, save it and return path or get path from local storage if it was already created 223 244 public static function getPdf($id, $number, $type) { 224 245 $lang = explode('_', get_locale())[0]; … … 236 257 return $pdf_path; 237 258 } 259 238 260 $token = get_option('apollo_general_settings')['apollo_token']; 239 261 if(!$token) { 240 262 return false; 241 263 } 264 242 265 Spaceinvoices\Spaceinvoices::setAccessToken($token); 243 266 … … 262 285 } 263 286 287 // show PDF document, download from Apollo first if not in local storage yet 264 288 public static function viewPdf($id, $number, $type) { 265 289 $lang = explode('_', get_locale())[0]; … … 289 313 } 290 314 315 // get premises and devices from Apollo (fiscalization) 291 316 public static function getBusinessPremises() { 292 317 $output = get_option('apollo_general_settings'); … … 314 339 } 315 340 341 // get irganization data 316 342 public static function getOrganization() { 317 343 $output = get_option('apollo_general_settings'); -
apollo/trunk/includes/admin/settings/general.php
r2094496 r2248680 6 6 class Apollo_General_Settings extends Apollo_Main_Settings { 7 7 8 // here we define settings fields, from this we build settings page 8 9 public function __construct() { 9 10 $this->settings_key = 'apollo_general_settings'; … … 17 18 18 19 private function get_sections() { 20 // define sections, basically groups for settings 19 21 $sections = array( 20 22 'token' => array( … … 61 63 $organization_id = $output['apollo_organization-id']; 62 64 65 // chec if token and organization are set, if not then hide all other settings, so we make sure those are valid first 63 66 if($token && $organization_id) { 64 67 $org = Apollo_invoice::getOrganization(); 65 68 69 // check if token and organization are correct 66 70 if (!isset($org->error)) { 67 71 $valid_org = true; … … 69 73 $premises_data = Apollo_invoice::getBusinessPremises(); 70 74 75 // check if fiscalization is enabled on apollo side 71 76 foreach ($org->_defaults as $def) { 72 77 if ($def->name === 'furs_verifying') { … … 91 96 } 92 97 98 // these are inputis inside sections 93 99 $settings = array( 94 100 array( 95 'id' => 'apollo-token', 96 'name' => $this->prefix . 'token', 97 'title' => __( 'Apollo token', 'apollo-invoices' ), 98 'callback' => array( $this, 'input_callback' ), 99 'page' => $this->settings_key, 100 'section' => 'token', 101 'type' => 'text', 102 'desc' => '', 103 'default' => '', 101 'id' => 'apollo-token', // filed id 102 'name' => $this->prefix . 'token', // name attribute 103 'title' => __( 'Apollo token', 'apollo-invoices' ), // text that shows left from input 104 'callback' => array( $this, 'input_callback' ), // callback to main-settings.php, input type depends on callback 105 'page' => $this->settings_key, 106 'section' => 'token', // one of the sections we defined on top 107 'type' => 'text', // input type 108 'desc' => '', // text that shows below the input 109 'default' => '', // default value 104 110 ), 105 111 array( … … 138 144 'desc' => '<div class="apollo-notes">' . __( 'Edit your business premise on the Apollo webpage.', 'apollo-invoices' ) . '</div>', 139 145 'options' => $premises_data['premises'], 140 'onchange' => 'updateDevices(this,'.json_encode($premises_data['devices']).')', 146 'onchange' => 'updateDevices(this,'.json_encode($premises_data['devices']).')', // onChange JS action 141 147 'default' => '', 142 148 'class' => !$valid_org || !$fiscalization ? 'hidden' : '', … … 182 188 ), 183 189 array( 190 'id' => 'apollo-add-sku', 191 'name' => $this->prefix . 'add-sku', 192 'title' => '', 193 'callback' => array( $this, 'input_callback' ), 194 'page' => $this->settings_key, 195 'section' => 'document', 196 'type' => 'checkbox', 197 'desc' => __( 'Add SKU code to invoice', 'apollo-invoices' ) 198 . '<br/><div class="apollo-notes">' . __( 'Add SKU code on invoice for each item. SKU code shows in item description for item that have SKU code set.', 'apollo-invoices' ) . '</div>', 199 'default' => 0, 200 'class' => !$valid_org ? 'hidden' : '', 201 ), 202 array( 184 203 'id' => 'apollo-send-estimate', 185 204 'name' => $this->prefix . 'send-estimate', … … 224 243 $i = 0; 225 244 245 // build list of possible payment methods from Woocommerce 226 246 foreach ($payments as $key => $payment) { 227 247 if ($payment->id !== 'bacs') { … … 237 257 'desc' => $payment->title, 238 258 'default' => 0, 239 'disabled' => $payment->enabled !== 'yes', 240 'class' => !$valid_org ? 'hidden payment-cb' : 'payment-cb', 259 'disabled' => $payment->enabled !== 'yes', // disable if payment not enabled in Woocommerce settings 260 'class' => !$valid_org ? 'hidden payment-cb' : 'payment-cb', // hide settings if organzation or token invalid 241 261 ); 242 262 } … … 246 266 } 247 267 268 // input sanitizing. Not really needed, good to have I guess... 248 269 public function sanitize( $input ) { 249 270 $output = get_option( $this->settings_key ); -
apollo/trunk/includes/admin/settings/main-settings.php
r2094496 r2248680 2 2 defined( 'ABSPATH' ) or exit; 3 3 4 /* This is class that builds settings based on general.php file. 5 If you need to add new input type or change the behaviour of settings, it should be done here. 6 If you need to remove/add/ update settings fields, look in general.php file. 7 */ 4 8 abstract class Apollo_Main_Settings { 5 9 … … 57 61 } 58 62 63 // main function for settings 59 64 public static function display_options_page() { 60 65 $current_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'apollo_general_settings'; … … 81 86 do_settings_sections( $current_tab ); 82 87 83 //TODO: change button depending on data84 88 submit_button(); 85 89 ?> … … 115 119 } 116 120 121 // function for building select input 117 122 public function select_callback( $args ) { 118 123 $options = get_option( $args['page'] ); … … 133 138 } 134 139 140 // function for building text input AND checbox (could made them seperate function tho...) 135 141 public function input_callback( $args ) { 136 142 $options = get_option( $args['page'] ); … … 138 144 $is_checkbox = $args['type'] === 'checkbox'; 139 145 $is_disabled = isset($args['disabled']) ? $args['disabled'] : false; 146 140 147 if ( $is_checkbox ) { ?> 141 148 <input type="hidden" name="<?php echo $args['page'] . '[' . $args['name'] . ']'; ?>" value="0"/> -
apollo/trunk/includes/apollo.php
r2073222 r2248680 37 37 } 38 38 39 // add css and js 39 40 public function my_admin_scripts() { 40 41 //css … … 48 49 49 50 51 // get and show PDF for document 50 52 public function admin_pdf_callback() { 51 52 53 $action = isset($_GET['apollo_action']) ? sanitize_key($_GET['apollo_action']) : ''; 53 54 $order_id = isset($_GET['post']) ? intval( $_GET['post'] ): 0; … … 57 58 58 59 if ($action === 'create') { 60 61 // using nonces for extra security (so call can't be duplicated) 59 62 $nonce = sanitize_key( $_GET['nonce'] ); 60 63 if ( ! wp_verify_nonce( $nonce, $action ) ) { 61 64 wp_die( 'Invalid request.' ); 62 65 } 66 63 67 $callback = Apollo_invoice::create($order_id, $type); 68 64 69 if (isset($callback['error'])) { 65 70 $this->errorMsg = $callback['error']; … … 99 104 } 100 105 106 // add Apollo estimate and invoice boxes on right side in order view 101 107 function add_apollo_boxes() { 102 108 add_meta_box( 'apollo_estimate', __( 'Apollo - estimate', 'apollo-invoices' ), array( $this, 'display_apollo_estimate_box' ), 'shop_order', 'side', 'high' ); … … 111 117 } 112 118 119 // show invoice box with invoice info, or CREATE button if invoice for order was not yet created 113 120 public function display_apollo_invoice_box( $order ) { 114 121 $invoice = Apollo_invoice::getInvoice( $order->ID ); 115 122 123 // show CREATE button if invoice not created 116 124 if ( !$invoice ) { 117 125 $url = wp_nonce_url(add_query_arg( array( … … 124 132 $wc_order = wc_get_order($order->ID); 125 133 134 // if no payment method, show warning when creating invoice 126 135 if ($wc_order->get_payment_method() === '') { 127 136 printf( '<a class="button order-page invoice apollo" onclick="notPaidWarn(`%1$s`)" title="%2$s">%3$s</a>', $url, __( 'Create invoice (also marks is as paid, so generate it after order was paid)', 'apollo-invoices' ), __( 'Create', 'apollo-invoices') ); … … 130 139 } 131 140 141 // if invoice was created, show invoice data 132 142 } else { 133 143 echo "<table class='order-page-meta-box pdf-invoice apollo'>"; … … 169 179 } 170 180 181 // show estimate box, same as invoice but for estimates 171 182 public function display_apollo_estimate_box( $order ) { 172 183 $estimate = Apollo_invoice::getEstimate( $order->ID ); -
apollo/trunk/readme.txt
r2102108 r2248680 5 5 Requires PHP: 5.6.3 6 6 Stable tag: trunk 7 Tested up to: 5. 2.17 Tested up to: 5.3.2 8 8 9 9 == Description == … … 83 83 84 84 == Changelog == 85 = 1.1.3 - February 22, 2020 = 86 87 - Added: Option to add SKU in item description on invoice 88 - Updated: Invoices with payment option COD (Cash on delivery) are no longer marked as paid automatically 89 85 90 = 1.1.2 - June 7, 2019 = 86 91
Note: See TracChangeset
for help on using the changeset viewer.