Plugin Directory

Changeset 2583681


Ignore:
Timestamp:
08/16/2021 05:35:11 PM (5 years ago)
Author:
marguspala
Message:

Attach PDF possibility

Location:
smartaccounts/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • smartaccounts/trunk/SmartAccountsApi.php

    r2001667 r2583681  
    11<?php
    22
    3 if ( ! defined('ABSPATH')) {
     3if (!defined('ABSPATH')) {
    44    exit;
    55} // Exit if accessed directly
     
    1010    public function sendRequest($body, $apiUrl, $extraParams = null)
    1111    {
     12        error_log("Sending SA call to $apiUrl");
    1213        if ($body == null) {
    1314            $body = new stdClass();
     
    3839        $decodedResponse = json_decode($saResponse, true);
    3940
    40         if ( ! in_array($response_code, [200, 201]) || is_wp_error($saResponse)) {
     41
     42        if (!in_array($response_code, [200, 201]) || is_wp_error($saResponse)) {
     43            error_log("SmartAccounts call failed url=$url: $response_code" . print_r($response, true));
    4144            throw new Exception("SmartAccounts call failed url=$url: $response_code" . print_r($response, true));
     45        }
     46
     47        if (!$decodedResponse) {
     48            return $saResponse; // Needed for getting raw PDF bytes
    4249        }
    4350
  • smartaccounts/trunk/SmartAccountsClass.php

    r2465911 r2583681  
    1313class SmartAccountsClass
    1414{
     15    public static function attachPdf($attachments, $email_id, WC_Order $order, $email)
     16    {
     17        $settings = self::getSettings();
     18        // IF PDF attaching disabled then skip
     19        // If no real_id found the skip
     20        // If not order completed then skip
     21        $invoiceId     = get_post_meta($order->get_id(), 'smartaccounts_invoice_real_id', true);
     22        $invoiceNumber = get_post_meta($order->get_id(), 'smartaccounts_invoice_id', true);
     23        if ($email_id !== "customer_completed_order" || !$invoiceId || !$settings->attachPdf) {
     24            return $attachments;
     25        }
     26
     27        $api = new SmartAccountsApi();
     28        try {
     29            $response = $api->sendRequest(null, 'purchasesales/clientinvoices:getpdf', "id=$invoiceId");
     30        } catch (Exception $exception) {
     31            error_log("PDF not received to attach to the invoice");
     32            return $attachments;
     33        }
     34
     35        $fileName = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "invoice_$invoiceNumber.pdf";
     36        file_put_contents($fileName, $response);
     37        $attachments[] = $fileName;
     38        error_log("Attachments are now: $email_id " . json_encode($attachments));
     39
     40        return $attachments;
     41    }
     42
    1543    public static function orderOfferStatusProcessing($order_id)
    1644    {
     
    2250                || strlen(get_post_meta($order_id, 'smartaccounts_offer_id', true)) > 0) {
    2351                error_log("SmartAccounts order $order_id already sent as offer or invoice, not sending OFFER again, SA id="
    24                     . get_post_meta($order_id, 'smartaccounts_invoice_id', true) . " offer_id="
    25                     . get_post_meta($order_id, 'smartaccounts_offer_id', true));
     52                          . get_post_meta($order_id, 'smartaccounts_invoice_id', true) . " offer_id="
     53                          . get_post_meta($order_id, 'smartaccounts_offer_id', true));
    2654
    2755                return; //Smartaccounts offer is already created
     
    75103            if (strlen(get_post_meta($order_id, 'smartaccounts_invoice_id', true)) > 0) {
    76104                error_log("SmartAccounts order $order_id already sent, not sending again, SA id="
    77                     . get_post_meta($order_id, 'smartaccounts_invoice_id', true));
     105                          . get_post_meta($order_id, 'smartaccounts_invoice_id', true));
    78106
    79107                return; //Smartaccounts order is already created
     
    88116            $saPayment->createPayment();
    89117            update_post_meta($order_id, 'smartaccounts_invoice_id', $invoice['invoice']['invoiceNumber']);
     118            update_post_meta($order_id, 'smartaccounts_invoice_real_id', $invoice['invoice']['invoiceId']);
    90119            error_log("SmartAccounts sales invoice created for order $order_id - " . $invoice['invoice']['invoiceNumber']);
    91120            $order->add_order_note("Invoice sent to SmartAccounts: " . $invoice['invoice']['invoiceNumber']);
     
    194223        $settings->importProducts  = isset($unSanitized->importProducts) && $unSanitized->importProducts === true;
    195224        $settings->importInventory = isset($unSanitized->importInventory) && $unSanitized->importInventory === true;
     225        $settings->attachPdf       = isset($unSanitized->attachPdf) && $unSanitized->attachPdf === true;
    196226        $settings->inventoryFilter = sanitize_text_field($unSanitized->inventoryFilter);
    197227        $objectId                  = sanitize_text_field($unSanitized->objectId);
     
    280310        if (!isset($currentSettings->importInventory)) {
    281311            $currentSettings->importInventory = true;
     312        }
     313        if (!isset($currentSettings->attachPdf)) {
     314            $currentSettings->attachPdf = false;
    282315        }
    283316        if (!isset($currentSettings->inventoryFilter)) {
     
    493526                        <td>
    494527                            <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>
     528                            <small>Comma separate list of Inventory account (Laokonto) to use when synching product
     529                                stock quantities from eg 10710,10741. If not empty then overrides filters
     530                                above.</small>
    496531                        </td>
    497532                    </tr>
     
    500535                        <td>
    501536                            <input type="text" v-model="settings.warehouseId"><br>
    502                             <small>Leave empty if not not relevant</small>
     537                            <small>Leave empty if not relevant. Usually empty</small>
     538                        </td>
     539                    </tr>
     540                    <tr>
     541                        <th>Attach SmartAccounts PDF invoice customer e-mail</th>
     542                        <td>
     543                            <input type="checkbox" v-model="settings.attachPdf">
     544                            <small></small>
    503545                        </td>
    504546                    </tr>
  • smartaccounts/trunk/readme.txt

    r2561842 r2583681  
    5959== Changelog ==
    6060
     61= 3.7 =
     62Option to include SmartAccounts invoice when sending order completed message to the customer.
     63
    6164= 3.6 =
    6265Price update improvements and more detailed logging for existing customer matching.
  • smartaccounts/trunk/smartaccounts.php

    r2561842 r2583681  
    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.6
     6 * Version: 3.7
    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.7.2
     12 * Tested up to: 5.8
    1313 */
    1414
     
    5555    }
    5656
     57    add_filter('woocommerce_email_attachments', 'SmartAccountsClass::attachPdf', 10, 4);
     58
    5759    add_action('sa_retry_failed_job', 'SmartAccountsClass::retryFailedOrders');
    5860
Note: See TracChangeset for help on using the changeset viewer.