Plugin Directory

Changeset 1796659


Ignore:
Timestamp:
01/03/2018 08:07:25 PM (8 years ago)
Author:
marguspala
Message:

Version 1.1

Location:
smartaccounts/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • smartaccounts/trunk/SmartAccountsArticle.php

    r1786015 r1796659  
    2121                $body->code        = $row->code;
    2222                $body->description = $row->description;
    23                 $body->type        = $row->code == "shipping" ? "SERVICE" : "PRODUCT";
     23                $body->type        = $row->code == get_option( 'sa_api_shipping_code' ) ? "SERVICE" : "PRODUCT";
    2424                $body->activeSales = true;
    2525                $this->api->sendRequest( $body, $addApiUrl );
  • smartaccounts/trunk/SmartAccountsClass.php

    r1786015 r1796659  
    1010class SmartAccountsClass {
    1111
    12     public static function orderStatusProcessing( $orderId ) {
     12    public static function orderStatusProcessing( $order_id ) {
     13
    1314        //try catch makes sure your store will operate even if there are errors
    1415        try {
    15             $order          = new WC_Order( $orderId );
     16            $order = new WC_Order( $order_id );
     17
     18            if ( strlen( get_post_meta( $order_id, 'smartaccounts_invoice_id', true ) ) > 0 ) {
     19                return; //Smartaccounts order is already created
     20            }
     21
    1622            $saClient       = new SmartAccountsClient( $order );
    1723            $client         = $saClient->getClient();
     
    2127            $saPayment = new SmartAccountsPayment( $order, $invoice );
    2228            $saPayment->createPayment();
     29            update_post_meta( $order_id, 'smartaccounts_invoice_id', $invoice['invoice']['invoiceNumber'] );
    2330        } catch ( Exception $exception ) {
    24 
     31            $handle = fopen( 'error.log', 'a' );
     32            if ( $handle ) {
     33                fwrite( $handle, $exception->getMessage() );
     34                fwrite( $handle, $exception->getTraceAsString() );
     35                fclose( $handle );
     36            }
    2537        }
    2638    }
     
    3143        register_setting( 'smartaccounts_options', 'sa_api_sk' );
    3244        register_setting( 'smartaccounts_options', 'sa_api_payment_account' );
     45        register_setting( 'smartaccounts_options', 'sa_api_shipping_code', [ 'default' => 'shipping' ] );
    3346    }
    3447
     
    7083                    </tr>
    7184
     85                    <tr valign="top">
     86                        <th>SmartAccounts shipping item name</th>
     87                        <td>
     88                            <input name="sa_api_shipping_code" size="50"
     89                                   value="<?php echo esc_attr( get_option( 'sa_api_shipping_code' ) ); ?>"/>
     90                        </td>
     91                    </tr>
     92
    7293                </table>
    7394                <?php
  • smartaccounts/trunk/SmartAccountsClient.php

    r1786015 r1796659  
    1111    protected $name;
    1212    protected $country;
    13 
     13    protected $isCompany;
    1414    protected $api;
    1515
     
    2323        $this->country     = $order->get_billing_country();
    2424        $this->email       = $order->get_billing_email();
     25        $this->isCompany   = strlen( $order->get_billing_company() ) > 0;
    2526        $firstName         = strlen( $order->get_shipping_first_name() ) == 0 ? $order->get_billing_first_name() : $order->get_shipping_first_name();
    2627        $lastName          = strlen( $order->get_shipping_last_name() ) == 0 ? $order->get_billing_last_name() : $order->get_shipping_last_name();
     
    2930        if ( $this->isAnonymous ) {
    3031            $this->name = "WooCommerce User $this->country";
     32        } else if ( $this->isCompany ) {
     33            $this->name = $order->get_billing_company();
    3134        } else {
    3235            $this->name = "$firstName $lastName";
     
    114117
    115118        foreach ( $clients as $client ) {
    116             if ( $this->hasEmail( $client, $email ) && $this->name == $client["name"] ) {
     119            //match client if name matches and is company or email also matches
     120            if ( ( $this->isCompany || $this->hasEmail( $client, $email ) ) && strtolower( $this->name ) == strtolower( $client["name"] ) ) {
    117121                return $client;
    118122            }
  • smartaccounts/trunk/SmartAccountsSalesInvoice.php

    r1786015 r1796659  
    1616        $apiUrl = "purchasesales/clientinvoices:add";
    1717
    18         $body           = new stdClass();
    19         $body->clientId = $this->client["id"];
    20         $body->date     = $this->order->get_date_created()->date( "d.m.Y" );
    21         $body->currency = $this->order->get_currency();
    22         $body->rows     = $this->getOrderRows();
     18        $body              = new stdClass();
     19        $body->clientId    = $this->client["id"];
     20        $body->date        = $this->order->get_date_created()->date( "d.m.Y" );
     21        $body->currency    = $this->order->get_currency();
     22        $body->rows        = $this->getOrderRows();
     23        $body->roundAmount = $this->getRoundingAmount( $body->rows );
     24        $body->amount      = number_format( $this->getOrderTotal(), 2 );
     25        $body->invoiceNote = "WooCommerce order #" . $this->order->get_id();
    2326
    2427        $saArticle = new SmartAccountsArticle();
     
    3033    }
    3134
     35    public function getRoundingAmount( $rows ) {
     36        $rowsTotal = 0;
     37        foreach ( $rows as $row ) {
     38            $rowsTotal += $row->totalCents;
     39            $rowsTotal += $row->taxCents;
     40        }
     41
     42        $roundingAmount = number_format( ( $this->getOrderTotal() * 100 + $this->getTotalTax() * 100 - $rowsTotal ) / 100, 2 );
     43
     44        return $roundingAmount;
     45    }
     46
     47    public function getTotalTax() {
     48        return floatval( $this->order->get_total_tax() );
     49    }
     50
     51    public function getOrderTotal() {
     52        return $this->order->get_subtotal() + $this->order->get_shipping_total();
     53    }
    3254
    3355    private function getOrderRows() {
    3456        $rows     = [];
    35         $totalTax = floatval( $this->order->get_total_tax() );
    36         $subTotal = $this->order->get_subtotal() + $this->order->get_shipping_total();
     57        $totalTax = $this->getTotalTax();
     58        $subTotal = $this->getOrderTotal();
    3759        $vatPc    = round( $totalTax * 100 / $subTotal );
    3860        foreach ( $this->order->get_items() as $item ) {
    3961            $row              = new stdClass();
    4062            $row->code        = $item->get_product()->get_sku();
    41             $row->description = strlen( $item->get_product()->get_description() ) == 0 ? "Woocommerce product " . $item->get_product()->get_name() : $item->get_product()->get_description();
    42             $row->price       = $item->get_product()->get_price();
     63            $row->description = strlen( $item->get_product()->get_description() ) == 0 ? $item->get_product()->get_name() : $item->get_product()->get_description();
    4364            $row->quantity    = $item->get_quantity();
    44             $row->vatPc       = $vatPc;
    45             $rows[]           = $row;
     65
     66            $rowPrice = $item->get_total() / $item->get_quantity();
     67
     68            $row->price      = number_format( $rowPrice, 2 );
     69            $row->vatPc      = $vatPc;
     70            $row->totalCents = intval( round( floatval( $row->price ) * $row->quantity * 100 ) );
     71            $row->taxCents   = intval( round( $row->totalCents * $vatPc / 100 ) );
     72            $rows[]          = $row;
    4673        }
    4774
    4875        if ( $this->order->get_shipping_total() > 0 ) {
    4976            $row              = new stdClass();
    50             $row->code        = "shipping";
     77            $row->code        = get_option( 'sa_api_shipping_code' );
    5178            $row->description = "Woocommerce Shipping";
    5279            $row->price       = $this->order->get_shipping_total();
    5380            $row->quantity    = 1;
    5481            $row->vatPc       = $vatPc;
     82            $row->totalCents  = intval( round( floatval( $row->price ) * $row->quantity * 100 ) );
     83            $row->taxCents    = intval( round( $row->totalCents * $vatPc / 100 ) );
    5584            $rows[]           = $row;
    5685        }
  • smartaccounts/trunk/readme.txt

    r1786073 r1796659  
    1010== Description ==
    1111
    12 After woocommerce order goes to status "Processing" then this plugin:
    13 * Creates customer to SmartAccounts if no existing customer with same name and e-mail found
     12After woocommerce order goes to status "Processing" or "Completed" then this plugin:
     13* Creates Customer to SmartAccounts if no existing customer with same name and e-mail found
    1414* Creates Articles in Smartaccounts of Woocommerce product on the order if existing products are not found.
    1515 Woocommerce product SKU is compared with SmartAccounts article code.
     
    1919== Installation ==
    2020
    21 Configuration is needed to make this plugin work. After actvating the plugin find its configuration page under Woocommerce menu item and:
     21Configuration is needed to make this plugin work. After activating the plugin find its configuration page under Woocommerce menu item and:
    2222
    2323* Make sure you have SmartAccounts package with API access
    24 * Copy SmartAccounts API key and secret from SmartAccounts interface.
     24* Copy SmartAccounts API key and secret from SmartAccounts interface. If you don't have SmartAccounts API key (please check Settings - Connected Services) you can contact SmartAccounts support to enable API service for your account. Additional conditions and charges may apply to API service.
    2525* Add bank account name you want to be used when making the invoices paid
     26* You can also change SmartAccounts code for shipping
    2627
    2728== Frequently Asked Questions ==
    2829
    2930= What are the plugin limitations? =
    30 This plugin creates sales invoice to SmartAccounts platform every time when order is changed into "Processing" status.
    31 Duplicate invoices could be created if order is changed to Processing status many times.
     31* This plugin creates sales invoice to SmartAccounts platform only first time when order is changed into "Processing" or "Completed" status.
    3232Invoice is always marked paid regardless if the payment is actually made or not yet. For example if Cash on Delivery payment method is used.
    33 Order changes and cancelling is not handled automatically
    34 Shipping item has code "shipping"
    35 All items have one VAT percentage
    36 SmartAccounts article code must be added to the Woocommerce product SKU if existing SmartAccounts article must be used
    37 If product is not found then "Woocommerce product NAME" Article is created to SmartAccounts
    38 Plugin does not handle errors which come from exceeding rate limits or unpaid SmartAccounts invoices.
    39 If there are errors then invoices might be missing and rest of the Woocommerce functionality keeps on working
    40 SmartAccounts API key, API secret and Payment account name must be configured before plugin will start working properly.
    41 
     33* Order changes and cancelling is not handled automatically
     34* All items have one VAT percentage
     35* SmartAccounts article code must be added to the Woocommerce product SKU if existing SmartAccounts article must be used
     36* If product is not found then "Woocommerce product NAME" Article is created to SmartAccounts
     37* Plugin does not handle errors which come from exceeding rate limits or unpaid SmartAccounts invoices.
     38* If there are errors then invoices might be missing and rest of the Woocommerce functionality keeps on working
     39* SmartAccounts API key, API secret and Payment account name must be configured before plugin will start working properly.
    4240
    4341== Screenshots ==
     
    4644== Changelog ==
    4745
    48 = 0.9 =
     46= 1.1 =
     47Woo order number reference is added to SmartAccounts invoice note
     48If company name is filled then invoice is connected to the Billing company
     49Discounted prices are used on invoices
     50Rounding calculation is added
     51Shipping item code is configurable
     52error.log is created in case of SmartAccounts communication issues
     53
     54= 1.0 =
    4955* Plugin is ready for production testing
  • smartaccounts/trunk/smartaccounts.php

    r1786015 r1796659  
    44 * Plugin URI: https://github.com/smartman/woocommerce_smartaccounts
    55 * Description: This plugin creates sales invoices in the smartaccounts.ee Online Accounting Software after Woocommerce order creation
    6  * Version: 1.0
     6 * Version: 1.1
    77 * Author: Margus Pala
    88 * Author URI: http://marguspala.com
     
    2424add_action( 'admin_init', 'SmartAccountsClass::registerSettings' );
    2525add_action( 'woocommerce_order_status_processing', 'SmartAccountsClass::orderStatusProcessing' );
     26add_action( 'woocommerce_order_status_completed', 'SmartAccountsClass::orderStatusProcessing' );
     27
     28//add_action( 'init', 'SmartAccountsClass::orderStatusProcessing' );
Note: See TracChangeset for help on using the changeset viewer.