Plugin Directory

Changeset 2465911


Ignore:
Timestamp:
01/31/2021 01:26:52 PM (5 years ago)
Author:
marguspala
Message:

Warehouses filter

Location:
smartaccounts/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • smartaccounts/trunk/SmartAccountsArticleAsync.php

    r2298313 r2465911  
    2121        $syncCount       = 0;
    2222        $noSyncCount     = 0;
     23
     24        $settings = SmartAccountsClass::getSettings();
     25        if (strlen($settings->inventoryFilter) === 0) {
     26            $filter = [];
     27        } else {
     28            $filter = explode(',', $settings->inventoryFilter);
     29        }
     30
    2331        do {
    2432            $result = $api->sendRequest(null, "purchasesales/articles:get", "pageNumber=$page");
    2533            if (isset($result['articles']) && is_array($result['articles'])) {
    2634                foreach ($result['articles'] as $article) {
    27                     if ($article['activeSales'] && ($article['type'] == 'PRODUCT' || $article['type'] == 'WH')) {
    28                         if (count($products) == 20) {
    29                             $productsBatches[] = $products;
    30                             $products          = [];
    31                         }
    32                         $products[$article['code']] = [
    33                             'code'        => $article['code'],
    34                             'price'       => $article['priceSales'],
    35                             'description' => $article['description'],
    36                             'quantity'    => 0
    37                         ];
    38                         $syncCount++;
    39                     } else {
    40                         error_log("Not active sales and not product nor WH item " . $article['code']);
     35                    if (!$article['activeSales']) {
     36                        continue;
     37                    }
     38
     39                    if (count($filter) > 0 && !in_array($article['accountWarehouse'], $filter)) {
     40                        error_log('Item filtered from sync ' . $article['code']);
    4141                        $noSyncCount++;
     42                        continue;
    4243                    }
     44
     45                    if ($article['type'] === 'PRODUCT' && !$settings->importProducts) {
     46                        error_log('not importing PRODUCT ' . $article['code']);
     47                        $noSyncCount++;
     48                        continue;
     49                    } elseif ($article['type'] === 'SERVICE' && !$settings->importServices) {
     50                        error_log('not importing SERVICE ' . $article['code']);
     51                        $noSyncCount++;
     52                        continue;
     53                    } elseif ($article['type'] === 'WH' && !$settings->importInventory) {
     54                        error_log('not importing WH ' . $article['code']);
     55                        $noSyncCount++;
     56                        continue;
     57                    }
     58
     59                    if (count($products) == 20) {
     60                        $productsBatches[] = $products;
     61                        $products          = [];
     62                    }
     63
     64                    $products[$article['code']] = [
     65                        'code'        => $article['code'],
     66                        'price'       => $article['priceSales'],
     67                        'description' => $article['description'],
     68                        'quantity'    => 0
     69                    ];
     70                    $syncCount++;
    4371                }
    4472            }
     
    6492    function task($products)
    6593    {
    66         if ( ! is_array($products)) {
     94        if (!is_array($products)) {
    6795            error_log('Not an array, something is wrong, removing from sync list: ' . print_r($products, true));
    6896
     
    104132        foreach ($products as $code => $product) {
    105133            $productId = wc_get_product_id_by_sku($code);
    106             if ( ! $productId) {
     134            if (!$productId) {
    107135                error_log("Inserting product $code");
    108136                $this->insertProduct($code, $product);
     
    145173        $finalPrice   = get_post_meta($post_id, '_price', true);
    146174        $salePrice    = get_post_meta($post_id, '_sale_price', true);
    147         if ( ! $regularPrice || ! $finalPrice) {
     175        if (!$regularPrice || !$finalPrice) {
    148176            // No price set at all yet, set one now.
    149177            update_post_meta($post_id, '_regular_price', $data['price']);
  • smartaccounts/trunk/SmartAccountsClass.php

    r2328274 r2465911  
    11<?php
     2
     3if (!defined('ABSPATH')) {
     4    exit;
     5} // Exit if accessed directly
    26
    37include_once('SmartAccountsClient.php');
     
    186190        $settings->defaultPayment  = sanitize_text_field($unSanitized->defaultPayment);
    187191        $settings->vat_number_meta = sanitize_text_field($unSanitized->vat_number_meta);
     192        $settings->warehouseId     = sanitize_text_field($unSanitized->warehouseId);
     193        $settings->importServices  = isset($unSanitized->importServices) && $unSanitized->importServices === true;
     194        $settings->importProducts  = isset($unSanitized->importProducts) && $unSanitized->importProducts === true;
     195        $settings->importInventory = isset($unSanitized->importInventory) && $unSanitized->importInventory === true;
     196        $settings->inventoryFilter = sanitize_text_field($unSanitized->inventoryFilter);
    188197        $objectId                  = sanitize_text_field($unSanitized->objectId);
     198
    189199        if (preg_match("/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/", $objectId)) {
    190200            $settings->objectId = $objectId;
     
    258268        if (!isset($currentSettings->vat_number_meta)) {
    259269            $currentSettings->vat_number_meta = "vat_number";
     270        }
     271        if (!isset($currentSettings->warehouseId)) {
     272            $currentSettings->warehouseId = null;
     273        }
     274        if (!isset($currentSettings->importServices)) {
     275            $currentSettings->importServices = false;
     276        }
     277        if (!isset($currentSettings->importProducts)) {
     278            $currentSettings->importProducts = true;
     279        }
     280        if (!isset($currentSettings->importInventory)) {
     281            $currentSettings->importInventory = true;
     282        }
     283        if (!isset($currentSettings->inventoryFilter)) {
     284            $currentSettings->inventoryFilter = "";
    260285        }
    261286        if (!isset($currentSettings->paymentMethods) || !is_object($currentSettings->paymentMethods)) {
     
    442467                </table>
    443468
     469                <h2>Warehouse and import config</h2>
     470                <small>What type of articles to sync from SmartAccounts and what warehouses to use
     471                </small>
     472                <table class="form-table">
     473                    <tr valign="top">
     474                        <th>Import Services</th>
     475                        <td>
     476                            <input type="checkbox" v-model="settings.importServices">
     477                        </td>
     478                    </tr>
     479                    <tr valign="top">
     480                        <th>Import Products</th>
     481                        <td>
     482                            <input type="checkbox" v-model="settings.importProducts">
     483                        </td>
     484                    </tr>
     485                    <tr valign="top">
     486                        <th>Import Warehouse Inventory</th>
     487                        <td>
     488                            <input type="checkbox" v-model="settings.importInventory">
     489                        </td>
     490                    </tr>
     491                    <tr valign="top">
     492                        <th>Warehouse filter (Overrides others)</th>
     493                        <td>
     494                            <input type="text" v-model="settings.inventoryFilter"><br>
     495                            <small>Comma separate list of Inventory account (Laokonto) to use when synching product stock quantities from eg 10710,10741. If not empty then overrides filters above.</small>
     496                        </td>
     497                    </tr>
     498                    <tr valign="top">
     499                        <th>What warehouse to use when sending sales invoice.</th>
     500                        <td>
     501                            <input type="text" v-model="settings.warehouseId"><br>
     502                            <small>Leave empty if not not relevant</small>
     503                        </td>
     504                    </tr>
     505                </table>
     506
    444507                <br>
    445508                <br>
  • smartaccounts/trunk/SmartAccountsSalesInvoice.php

    r2369311 r2465911  
    1313    {
    1414        $this->client = $client;
    15         $this->order = $order;
    16         $this->api = new SmartAccountsApi();
     15        $this->order  = $order;
     16        $this->api    = new SmartAccountsApi();
    1717    }
    1818
     
    2121        $apiUrl = "purchasesales/clientoffers:add";
    2222
    23         $body = new stdClass();
    24         $body->clientId = $this->client["id"];
    25         $body->date = $this->order->get_date_created()->date("d.m.Y");
    26         $body->currency = $this->order->get_currency();
    27         $body->rows = $this->getOrderRows();
     23        $body              = new stdClass();
     24        $body->clientId    = $this->client["id"];
     25        $body->date        = $this->order->get_date_created()->date("d.m.Y");
     26        $body->currency    = $this->order->get_currency();
     27        $body->rows        = $this->getOrderRows();
    2828        $body->roundAmount = $this->getRoundingAmount($body->rows);
    29         $body->amount = $this->order->get_total();
    30         $body->offerNote = "WooCommerce order #" . $this->order->get_id() . ". " . $this->order->get_customer_note();
     29        $body->amount      = $this->order->get_total();
     30        $body->offerNote   = "WooCommerce order #" . $this->order->get_id() . ". " . $this->order->get_customer_note();
    3131
    3232        $settings = json_decode(get_option("sa_settings"));
     
    4747        $apiUrl = "purchasesales/clientinvoices:add";
    4848
    49         $body = new stdClass();
    50         $body->clientId = $this->client["id"];
    51         $body->date = $this->order->get_date_created()->date("d.m.Y");
    52         $body->currency = $this->order->get_currency();
    53         $body->rows = $this->getOrderRows();
     49        $body              = new stdClass();
     50        $body->clientId    = $this->client["id"];
     51        $body->date        = $this->order->get_date_created()->date("d.m.Y");
     52        $body->currency    = $this->order->get_currency();
     53        $body->rows        = $this->getOrderRows();
    5454        $body->roundAmount = $this->getRoundingAmount($body->rows);
    55         $body->amount = $this->order->get_total();
     55        $body->amount      = $this->order->get_total();
    5656        $body->invoiceNote = "WooCommerce order #" . $this->order->get_id() . ". " . $this->order->get_customer_note();
    5757
     
    6464        if ($settings && $settings->objectId) {
    6565            $body->objectId = $settings->objectId;
     66        }
     67
     68        if ($settings && $settings->warehouseId) {
     69            $body->warehouseId = $settings->warehouseId;
    6670        }
    6771
     
    101105    private function getOrderRows()
    102106    {
    103         $rows = [];
     107        $rows     = [];
    104108        $totalTax = $this->getTotalTax();
    105109        $subTotal = $this->getOrderTotal();
    106         $vatPc = round($totalTax * 100 / $subTotal);
     110        $vatPc    = round($totalTax * 100 / $subTotal);
    107111        foreach ($this->order->get_items() as $item) {
    108112            $product = $item->get_product();
    109             $row = new stdClass();
     113            $row     = new stdClass();
    110114            if ($product == null) {
    111115                error_log("SA Product not found for order item " . $item->get_id());
    112116                $row->description = $item->get_name();
    113                 $code = "wc_missing_product_" . $item->get_id();
     117                $code             = "wc_missing_product_" . $item->get_id();
    114118            } else {
    115119                $code = $product->get_sku();
     
    118122                }
    119123
    120                 //in case 
     124                //in case
    121125                $codeSplit = explode(",", $code);
    122126                if (count($codeSplit) > 1) {
     
    132136            $row->description = preg_replace('/[\xF0-\xF7].../s', '_', $row->description);
    133137
    134             $row->code = $code;
     138            $row->code     = $code;
    135139            $row->quantity = $item->get_quantity();
    136140
    137141            $rowPrice = $item->get_total() / $item->get_quantity();
    138142
    139             $row->price = number_format($rowPrice, 2, ".", "");
    140             $row->vatPc = $vatPc;
     143            $row->price      = number_format($rowPrice, 2, ".", "");
     144            $row->vatPc      = $vatPc;
    141145            $row->totalCents = intval(round(floatval($row->price) * $row->quantity * 100));
    142             $row->taxCents = intval(round($row->totalCents * $vatPc / 100));
     146            $row->taxCents   = intval(round($row->totalCents * $vatPc / 100));
    143147
    144148            $settings = json_decode(get_option("sa_settings"));
     
    151155
    152156        if ($this->order->get_shipping_total() > 0) {
    153             $settings = json_decode(get_option("sa_settings"));
    154             $row = new stdClass();
    155             $row->code = isset($settings->defaultShipping) ? $settings->defaultShipping : "shipping";
     157            $settings         = json_decode(get_option("sa_settings"));
     158            $row              = new stdClass();
     159            $row->code        = isset($settings->defaultShipping) ? $settings->defaultShipping : "shipping";
    156160            $row->description = "Woocommerce Shipping";
    157             $row->price = $this->order->get_shipping_total();
    158             $row->quantity = 1;
    159             $row->vatPc = $vatPc;
    160             $row->totalCents = intval(round(floatval($row->price) * $row->quantity * 100));
    161             $row->taxCents = intval(round($row->totalCents * $vatPc / 100));
     161            $row->price       = $this->order->get_shipping_total();
     162            $row->quantity    = 1;
     163            $row->vatPc       = $vatPc;
     164            $row->totalCents  = intval(round(floatval($row->price) * $row->quantity * 100));
     165            $row->taxCents    = intval(round($row->totalCents * $vatPc / 100));
    162166
    163167            $settings = json_decode(get_option("sa_settings"));
  • smartaccounts/trunk/readme.txt

    r2369311 r2465911  
    22Tags: SmartAccounts, smartaccounts, WooCommerce
    33Requires at least: 4.8
    4 Tested up to: 5.4
     4Tested up to: 5.6
    55License: GPLv2 or later
    66License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5959== Changelog ==
    6060
     61= 3.4 =
     62Product import filters and Warehouse setting for new invoices.
     63
    6164= 3.2 =
    6265Strip unsupported characters when creating invoices.
  • smartaccounts/trunk/smartaccounts.php

    r2369311 r2465911  
    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: 3.2
     6 * Version: 3.4
    77 * Author: Margus Pala
    88 * Author URI: https://marguspala.com
     
    1010 * License URI:  https://www.gnu.org/licenses/gpl-2.0.html
    1111 * Requires at least: 4.8.0
    12  * Tested up to: 5.5
     12 * Tested up to: 5.6
    1313 */
    1414
    15 if ( ! defined('ABSPATH')) {
     15if (!defined('ABSPATH')) {
    1616    exit;
    1717} // Exit if accessed directly
    1818
    19 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
     19include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    2020
    2121function smartaccounts_missing_wc_admin_notice()
Note: See TracChangeset for help on using the changeset viewer.