Plugin Directory

Changeset 2643408


Ignore:
Timestamp:
12/13/2021 01:40:11 PM (4 years ago)
Author:
abrestan
Message:

release 2.2.1

Location:
abrestan/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • abrestan/trunk/abrestan.php

    r2636452 r2643408  
    44 *    Plugin URI: abrestan.com
    55 *    Description: Using this web service, you can connect your store to abrestan cloud accounting software.
    6  *    Version: 2.2.0
     6 *    Version: 2.2.1
    77 *    Author: KTE
    88 *    WC requires at least: 4.1.0
     
    135135    return $results;
    136136}
    137 
     137function abrestanGetOrderByCompanyId()
     138{
     139    global $wpdb;
     140    $table_name = $wpdb->prefix . 'abrestan_order';
     141    $q = $wpdb->prepare("SELECT * FROM $table_name where company_id=%d", get_option("abrestan_company")['companyCode']);
     142    $results = $wpdb->get_results($q);
     143    return $results;
     144}
     145function abrestanGetOrderByOrderIds($order_id)
     146{
     147
     148    global $wpdb;
     149    $order_ids = implode( ', ', array_fill( 0, count( $order_id ), '%s' ));
     150    $table_name = $wpdb->prefix . 'abrestan_order';
     151    $sql = "SELECT * FROM $table_name where order_id IN ($order_ids)";
     152    $query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $order_id));
     153    $results = $wpdb->get_results($query);
     154    return $results;
     155}
    138156function abrestanDeleteOrderByOrderId($order_id, $company_id)
    139157{
     
    227245        }
    228246    }
     247
    229248
    230249    $sync_order = new \ABR\Api\abrestan_sync_orders();
  • abrestan/trunk/inc/Api/abrestan_api.php

    r2597509 r2643408  
    99class abrestan_api
    1010{
    11     public function apiRequest($method, $data = array())
     11    public function apiRequest($uri,$method, $data = array())
    1212    {
    13         if ($method == null) {
     13        if ($uri == null) {
    1414            return false;
    1515        }
    1616
    17         $endpoint = 'https://kasb.abrestan.com/webservice/v1/' . $method;
     17        $endpoint = 'https://kasb.abrestan.com/webservice/v1/' . $uri;
    1818
    19         $body = array_merge(
    20             [
    21                 'token' => get_option('abrestan_login')['token'] ? get_option('abrestan_login')['token'] : ''
    22             ]
    23             , $data);
     19        $body = array_merge($data,            [
     20            'token' => (get_option('abrestan_login')['token']) ? get_option('abrestan_login')['token'] : ''
     21        ]);
     22
     23
    2424        $options = array(
    2525            'body' => $body,
     
    3030            'sslverify' => false,
    3131            'data_format' => 'body',
     32            'method'     => $method
    3233        );
    33             $wp_remote_post = wp_remote_post($endpoint, $options);
     34
     35            $wp_remote_post = wp_remote_request($endpoint, $options);
     36
    3437            return $wp_remote_post;
    3538    }
    3639    public function Login($Data)
    3740    {
    38         $method = 'login';
    39         return $this->apiRequest($method, $Data);
     41        $uri = 'login';
     42        $method='POST';
     43
     44        return $this->apiRequest($uri,$method, $Data);
    4045    }
    4146    public function SyncProduct($Data)
    4247    {
    43         $method = 'import/commodities/or/persons';
    44         return $this->apiRequest($method, $Data);
     48        $uri = 'import/commodities/or/persons';
     49        $method='POST';
     50
     51        return $this->apiRequest($uri,$method, $Data);
    4552    }
    4653    public function SelectCompany($Data)
    4754    {
    48         $method = 'set/domain';
    49         return $this->apiRequest($method, $Data);
     55        $uri = 'set/domain';
     56        $method='POST';
     57        return $this->apiRequest($uri,$method, $Data);
    5058    }
    5159    public function SyncUser($Data)
    5260    {
    53         $method = 'import/commodities/or/persons';
    54         return $this->apiRequest($method, $Data);
     61        $uri = 'import/commodities/or/persons';
     62        $method='POST';
     63
     64        return $this->apiRequest($uri,$method, $Data);
    5565    }
    5666    public function SyncOrder($Data)
    5767    {
    58         $method = 'insert/sale/factor';
    59         return $this->apiRequest($method, $Data);
     68        $uri = 'insert/sale/factor';
     69        $method='POST';
     70        return $this->apiRequest($uri,$method, $Data);
     71    }
     72    public function getOrders($Data)
     73    {
     74        $uri = 'sale/factor/all';
     75        $method='GET';
     76        return $this->apiRequest($uri,$method,$Data);
    6077    }
    6178    public function DeleteOrder($Data)
    6279    {
    63         $method = 'delete/sale/factor';
    64         return $this->apiRequest($method, $Data);
     80        $uri = 'delete/sale/factor';
     81        $method='POST';
     82        return $this->apiRequest($uri,$method, $Data);
    6583    }
    6684}
  • abrestan/trunk/inc/Api/abrestan_sync_orders.php

    r2636452 r2643408  
    4343        $orders = wc_get_orders($arg);
    4444        $all_orders = [];
     45
     46        $Data =
     47            [
     48                'company_id' => get_option("abrestan_company")['companyCode'],
     49            ];
     50        $abrestanApi = new abrestan_api();
     51        $wp_remote_post = $abrestanApi->getOrders($Data);
     52
     53        $abrestanFactors = json_decode(wp_remote_retrieve_body($wp_remote_post), true)['factors'];
     54        function searchForId($id, $array) {
     55            foreach ($array as $key => $val) {
     56                if ($val['generation_id'] === $id) {
     57                    return true;
     58                }
     59            }
     60            return null;
     61        }
     62
     63
     64
    4565        foreach ($orders as $order) {
    4666            array_push($all_orders, $order->get_id());
    4767        }
    48         $this->factor = $all_orders;
    49         return $all_orders;
     68
     69        $oldSent=abrestanGetOrderByOrderIds($all_orders);
     70        $oldorders=[];
     71        foreach ($oldSent as $sent){
     72            if(!searchForId($sent->factor_id,$abrestanFactors)){
     73                abrestanDeleteOrderByOrderId($sent->order_id,get_option("abrestan_company")['companyCode']);
     74                array_push($oldorders,$sent->order_id);
     75            }
     76
     77        }
     78
     79        return array_intersect($oldorders,$all_orders);
    5080    }
    51 
    5281
    5382
     
    6897
    6998        $company = get_option("abrestan_company")['companyCode'];
    70         $abrestanOrders=abrestanGetOrderByOrderId($orders[0]);
     99        $abrestanOrders = abrestanGetOrderByOrderId($orders[0]);
    71100
    72         foreach ($abrestanOrders as $abrestanOrder){
    73             if ($abrestanOrder->company_id==$company&&!empty($abrestanOrder->factor_id)){
     101        foreach ($abrestanOrders as $abrestanOrder) {
     102            if ($abrestanOrder->company_id == $company && !empty($abrestanOrder->factor_id)) {
    74103                abrestan_log("Sync Order", "Invoice No. $orders[0] has already been registered in this business!", "ERROR");
    75104                $data_feedback = [
     
    97126        $wp_remote_post = $abrestanApi->SyncOrder($Data);
    98127        $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true);
    99         if ($result['code']==105){
    100128
    101             $getToken=new GetToken();
     129        if ($result['code'] == 105) {
     130
     131            $getToken = new GetToken();
    102132            $getToken->register();
    103133        }
     
    123153                        'class' => 'uk-alert uk-alert-success'
    124154                    ];
    125                     update_post_meta($orders[0],"order_diff",$factors);
     155                    update_post_meta($orders[0], "order_diff", $factors);
    126156                    delete_post_meta($orders[0], "abrestan_error");
    127157                    abrestan_log("Sync Order", "Order #" . $orders[0] . " was successfully synced.", "SUCCESS");
    128158                    $order = wc_get_order($orders[0]);
    129159                    $order->add_order_note("فاکتور شماره " . $result['factor_number'] . " با موفقیت در ابرستان ثبت گردید.", 0, 0);
    130                     abrestanOrderInfo($orders[0],$result['factor_id'],$result['factor_number']);
     160                    abrestanOrderInfo($orders[0], $result['factor_id'], $result['factor_number']);
    131161                    return $data_feedback;
    132162                case 400:
Note: See TracChangeset for help on using the changeset viewer.