Plugin Directory

Changeset 2248680


Ignore:
Timestamp:
02/22/2020 10:37:13 AM (6 years ago)
Author:
jank404
Message:

version 1.1.3

Location:
apollo/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • apollo/trunk/apollo.php

    r2102108 r2248680  
    44 * Plugin URI:        https://wordpress.org/plugins/apollo
    55 * Description:       Manually or automatically generate invoices and send PDFs as attachments for WooCommerce orders.
    6  * Version:           1.1.2
     6 * Version:           1.1.3
    77 * Author:            Studio404
    88 * Text Domain:       apollo
    99 * Domain Path:       /languages
    10  * WC tested up to:   3.6.3
     10 * WC tested up to:   5.3.2
    1111 * WC requires at least: 3.0
    1212 */
     
    1414defined( 'ABSPATH' ) or exit;
    1515
    16 define( 'APOLLO_VERSION', '1.1.2' );
     16define( 'APOLLO_VERSION', '1.1.3' );
    1717
    1818function apollo_load_plugin() {
    1919
     20  // define variables and include all files that are needed
    2021  if ( ! class_exists( 'WooCommerce' ) ) {
    2122    return;
     
    2526    define( 'APOLLO_FILE', __FILE__ );
    2627  }
    27 
    2828
    2929  if ( ! defined( 'APOLLO_DIR' ) ) {
     
    6262}
    6363
     64// calls funtion above with WP plugins_loaded hook
    6465add_action( 'plugins_loaded', 'apollo_load_plugin' );
    6566
    6667add_filter( 'woocommerce_email_attachments', 'add_apollo_document_to_email', 10, 3 );
    6768function 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
    6970  $lang = explode('_', get_locale())[0];
    7071
    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
    7374  // $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
    7576  $invoice_number = get_post_meta( $order->get_id(), 'apollo_invoice_number', true);
    7677  $pdf_invoice_path = APOLLO_DOCUMENTS_DIR."/invoice - ".$invoice_number.".pdf";
    7778
    7879
    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
    8081  $estimate_number = get_post_meta( $order->get_id(), 'apollo_estimate_number', true);
    8182  $estimate_id = get_post_meta( $order->get_id(), 'apollo_estimate_id', true);
    8283  $pdf_estimate_path = APOLLO_DOCUMENTS_DIR."/estimate - ".$estimate_number.".pdf";
    8384  $payment_type = 'apollo_payment-'.$order->get_payment_method();
     85
     86  //chechk if order payment matches any of the payments set in apollo settings
    8487  $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);
    8788
    8889  if ($lang === 'sl') {
     
    126127}
    127128
    128 // define the woocommerce_new_order callback
     129add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_new_order', 10, 3 );
    129130function action_woocommerce_new_order( $order_id ) {
    130 
     131  // this function is called when new order is created in Woocommerce
     132 
    131133  $order = wc_get_order($order_id);
    132134
    133135  $payment_type = 'apollo_payment-'.$order->get_payment_method();
     136
     137  //chechk if order payment matches any of the payments set in apollo settings
    134138  $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 
    138139
    139140  if ( $order->get_payment_method() === 'bacs') { // new order; bank transfer
    140141    Apollo_invoice::create($order_id, 'estimate');
    141   } else if ($payment_enabled && $order_paid !== '') {
     142  } else if ($payment_enabled) {
    142143    Apollo_invoice::create($order_id, 'invoice');
    143144  }
    144145};
    145146
    146 // add the action
    147 add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_new_order', 10, 3 );
     147add_action( 'woocommerce_order_status_completed',   'action_woocommerce_status_completed');
     148function action_woocommerce_status_completed ($order_id) {
     149  // this function is called when order is marked as completed
    148150
    149 function action_woocommerce_status_completed ($order_id) {
    150151  $order = wc_get_order($order_id);
    151152
     
    160161  }
    161162}
    162 
    163 add_action( 'woocommerce_order_status_completed',   'action_woocommerce_status_completed');
  • apollo/trunk/includes/admin/invoice.php

    r2102108 r2248680  
    55if ( ! class_exists( 'Apollo_invoice' ) ) {
    66    abstract class Apollo_invoice {
     7
     8    //create Apollo invoice or estimate
    79    public static function create( $order_id, $type = 'invoice' ) {
    8 
    910      $invoice_exsists = Apollo_invoice::getInvoice($order_id);
    1011      $estimate_exsists = Apollo_invoice::getEstimate($order_id);
    1112
     13      // if document was already created, then just return it
    1214      if ($type === 'invoice' && $invoice_exsists) {
    1315          $invoice_exsists['exsists'] = true;
     
    1719          return $estimate_exsists;
    1820      }
     21
    1922      $order = wc_get_order($order_id);
    2023      $output = get_option('apollo_general_settings');
    2124      $token = $output['apollo_token'];
    2225      $organization_id = $output['apollo_organization-id'];
    23 
     26      $add_sku = (bool) get_option('apollo_general_settings')['apollo_add-sku'];
    2427
    2528      $fiscalization = (bool) $output['apollo_enable-fiscalization'];
     
    3033      $device = $output['apollo_device-id'];
    3134
    32       $test = Spaceinvoices\Spaceinvoices::setAccessToken($token);
    33 
    3435      if (!$order || !$output || !$token || !$organization_id) {
    3536        return array("error" => "Apollo error: Missing data.");
    3637      }
    3738
    38       $SI_products_data = array();
     39      $SI_products_data = array(); // we will fill this with items that are in order
    3940      $SI_total = 0;
    4041      $order_total = 0;
     
    4647      }
    4748
     49      // get data for each item
    4850      foreach ($order->get_items() as $item_id => $item_data) {
    4951        $product = $item_data->get_data();
     
    5456        $tax_percent = ($tax_amount / $without_tax) * 100;
    5557
     58        // Aggregate data for Apollo
    5659        $product_data = array(
    5760          'name'     => $product['name'],
     
    6265        );
    6366
    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'])) {
    6982          $product_data['discount'] = round((1 - (floatval($product['total']) / floatval($product['subtotal']))) * 100);
    7083          $product_data['price'] = floatval($product['subtotal']) / $product['quantity'];
     
    8093      }
    8194
     95      // get shippings data (can be multiple)
    8296      foreach( $order->get_items('shipping') as $item_id => $item_shipping ) {
    8397        $shipping = $item_shipping->get_data();
     
    100114      }
    101115
    102 
     116      // get all extra fees
    103117      foreach( $order->get_items('fee') as $item_id => $item_fee ) {
    104118        $fee = $item_fee->get_data();
     
    108122
    109123        $tax_percent = ($tax_amount / $without_tax) * 100;
    110 
    111124
    112125        $unit = 'fee';
     
    122135          'price'    => $without_tax
    123136        );
     137
    124138        if ($tax_percent != 0 && $tax_amount != 0 ) {
    125139          $fee_data['_documentItemTaxes'] = array(array('rate' => $tax_percent));
     
    128142      }
    129143
    130 
     144      // aggreagte order data for Apollo
    131145      $order_data = array(
    132146        "type" => $type,
     
    147161      );
    148162
     163      // if fiscalization is enabled and we making invoice, add the fiscalization data
    149164      if ($fiscalization && $type === 'invoice') {
    150165        $order_data['_furs'] = array(
     
    160175      }
    161176
    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') {
    163179        $order_data['payments'] = array(array(
    164180          "type" => "other",
     
    172188        return array("error" => "Apollo error (".$create->error->statusCode."): ".$create->error->text);
    173189      }
     190
    174191      $document_id = $create->id;
    175192      $document_number = $create->number;
     
    182199      );
    183200
     201      // save invoice/estimate data to order
    184202      if ($type === 'invoice') {
    185203        update_post_meta( $order_id, 'apollo_invoice_id', $document_id );
     
    199217    }
    200218
     219    // get invoice for order
    201220    public static function getInvoice( $order_id ) {
    202221      $id = get_post_meta( $order_id, 'apollo_invoice_id', true);
     
    210229    }
    211230
     231    // get estimate for order
    212232    public static function getEstimate( $order_id ) {
    213233      $id = get_post_meta( $order_id, 'apollo_estimate_id', true);
     
    221241    }
    222242
     243    // get PDF from Apoll, save it and return path or get path from local storage if it was already created
    223244    public static function getPdf($id, $number, $type) {
    224245      $lang = explode('_', get_locale())[0];
     
    236257        return $pdf_path;
    237258      }
     259
    238260      $token = get_option('apollo_general_settings')['apollo_token'];
    239261      if(!$token) {
    240262        return false;
    241263      }
     264
    242265      Spaceinvoices\Spaceinvoices::setAccessToken($token);
    243266
     
    262285    }
    263286
     287    // show PDF document, download from Apollo first if not in local storage yet
    264288    public static function viewPdf($id, $number, $type) {
    265289      $lang = explode('_', get_locale())[0];
     
    289313    }
    290314
     315    // get premises and devices from Apollo (fiscalization)
    291316    public static function getBusinessPremises() {
    292317      $output = get_option('apollo_general_settings');
     
    314339    }
    315340
     341    // get irganization data
    316342    public static function getOrganization() {
    317343      $output = get_option('apollo_general_settings');
  • apollo/trunk/includes/admin/settings/general.php

    r2094496 r2248680  
    66    class Apollo_General_Settings extends Apollo_Main_Settings {
    77
     8        // here we define settings fields, from this we build settings page
    89        public function __construct() {
    910            $this->settings_key = 'apollo_general_settings';
     
    1718
    1819        private function get_sections() {
     20            // define sections, basically groups for settings
    1921            $sections = array(
    2022                'token'         => array(
     
    6163            $organization_id = $output['apollo_organization-id'];
    6264
     65            // chec if token and organization are set, if not then hide all other settings, so we make sure those are valid first
    6366            if($token && $organization_id) {
    6467                $org = Apollo_invoice::getOrganization();
    6568
     69                // check if token and organization are correct
    6670                if (!isset($org->error)) {
    6771                    $valid_org = true;
     
    6973                    $premises_data = Apollo_invoice::getBusinessPremises();
    7074
     75                    // check if fiscalization is enabled on apollo side
    7176                    foreach ($org->_defaults as $def) {
    7277                        if ($def->name === 'furs_verifying') {
     
    9196            }
    9297
     98            // these are inputis inside sections
    9399            $settings = array(
    94100        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
    104110        ),
    105111        array(
     
    138144                    'desc'     => '<div class="apollo-notes">' . __( 'Edit your business premise on the Apollo webpage.', 'apollo-invoices' ) . '</div>',
    139145                    '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
    141147                    'default'  => '',
    142148                    'class'      => !$valid_org || !$fiscalization ? 'hidden' : '',
     
    182188                ),
    183189                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(
    184203                    'id'       => 'apollo-send-estimate',
    185204                    'name'     => $this->prefix . 'send-estimate',
     
    224243            $i = 0;
    225244
     245            // build list of possible payment methods from Woocommerce
    226246            foreach ($payments as $key => $payment) {
    227247                if ($payment->id !== 'bacs') {
     
    237257                        'desc'     => $payment->title,
    238258                        '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
    241261                    );
    242262                }
     
    246266    }
    247267
     268        // input sanitizing. Not really needed, good to have I guess...
    248269    public function sanitize( $input ) {
    249270            $output = get_option( $this->settings_key );
  • apollo/trunk/includes/admin/settings/main-settings.php

    r2094496 r2248680  
    22defined( 'ABSPATH' ) or exit;
    33
     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*/
    48abstract class Apollo_Main_Settings {
    59
     
    5761    }
    5862
     63    // main function for settings
    5964    public static function display_options_page() {
    6065        $current_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'apollo_general_settings';
     
    8186                do_settings_sections( $current_tab );
    8287
    83                 //TODO: change button depending on data
    8488                submit_button();
    8589                ?>
     
    115119    }
    116120
     121    // function for building select input
    117122    public function select_callback( $args ) {
    118123        $options = get_option( $args['page'] );
     
    133138    }
    134139
     140    // function for building text input AND checbox (could made them seperate function tho...)
    135141    public function input_callback( $args ) {
    136142        $options = get_option( $args['page'] );
     
    138144        $is_checkbox = $args['type'] === 'checkbox';
    139145        $is_disabled = isset($args['disabled']) ? $args['disabled'] : false;
     146
    140147        if ( $is_checkbox ) { ?>
    141148            <input type="hidden" name="<?php echo $args['page'] . '[' . $args['name'] . ']'; ?>" value="0"/>
  • apollo/trunk/includes/apollo.php

    r2073222 r2248680  
    3737        }
    3838
     39        // add css and js
    3940        public function my_admin_scripts() {
    4041            //css
     
    4849
    4950
     51        // get and show PDF for document
    5052        public function admin_pdf_callback() {
    51 
    5253            $action = isset($_GET['apollo_action']) ? sanitize_key($_GET['apollo_action']) : '';
    5354            $order_id = isset($_GET['post']) ? intval( $_GET['post'] ): 0;
     
    5758
    5859            if ($action === 'create') {
     60
     61                // using nonces for extra security (so call can't be duplicated)
    5962                $nonce = sanitize_key( $_GET['nonce'] );
    6063                if ( ! wp_verify_nonce( $nonce, $action ) ) {
    6164                    wp_die( 'Invalid request.' );
    6265                }
     66
    6367                $callback = Apollo_invoice::create($order_id, $type);
     68
    6469                if (isset($callback['error'])) {
    6570                    $this->errorMsg = $callback['error'];
     
    99104        }
    100105
     106        // add Apollo estimate and invoice boxes on right side in order view
    101107        function add_apollo_boxes() {
    102108            add_meta_box( 'apollo_estimate', __( 'Apollo - estimate', 'apollo-invoices' ), array( $this, 'display_apollo_estimate_box' ), 'shop_order', 'side', 'high' );
     
    111117    }
    112118
     119        // show invoice box with invoice info, or CREATE button if invoice for order was not yet created
    113120    public function display_apollo_invoice_box( $order ) {
    114121            $invoice = Apollo_invoice::getInvoice( $order->ID );
    115122
     123            // show CREATE button if invoice not created
    116124            if ( !$invoice ) {
    117125                $url = wp_nonce_url(add_query_arg( array(
     
    124132            $wc_order = wc_get_order($order->ID);
    125133
     134            // if no payment method, show warning when creating invoice
    126135            if ($wc_order->get_payment_method() === '') {
    127136                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') );
     
    130139            }
    131140
     141            // if invoice was created, show invoice data
    132142            } else {
    133143                echo "<table class='order-page-meta-box pdf-invoice apollo'>";
     
    169179        }
    170180
     181        // show estimate box, same as invoice but for estimates
    171182        public function display_apollo_estimate_box( $order ) {
    172183            $estimate = Apollo_invoice::getEstimate( $order->ID );
  • apollo/trunk/readme.txt

    r2102108 r2248680  
    55Requires PHP: 5.6.3
    66Stable tag: trunk
    7 Tested up to: 5.2.1
     7Tested up to: 5.3.2
    88
    99== Description ==
     
    8383
    8484== 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
    8590= 1.1.2 - June 7, 2019 =
    8691
Note: See TracChangeset for help on using the changeset viewer.