Changeset 2643408
- Timestamp:
- 12/13/2021 01:40:11 PM (4 years ago)
- Location:
- abrestan/trunk
- Files:
-
- 3 edited
-
abrestan.php (modified) (3 diffs)
-
inc/Api/abrestan_api.php (modified) (2 diffs)
-
inc/Api/abrestan_sync_orders.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
abrestan/trunk/abrestan.php
r2636452 r2643408 4 4 * Plugin URI: abrestan.com 5 5 * Description: Using this web service, you can connect your store to abrestan cloud accounting software. 6 * Version: 2.2. 06 * Version: 2.2.1 7 7 * Author: KTE 8 8 * WC requires at least: 4.1.0 … … 135 135 return $results; 136 136 } 137 137 function 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 } 145 function 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 } 138 156 function abrestanDeleteOrderByOrderId($order_id, $company_id) 139 157 { … … 227 245 } 228 246 } 247 229 248 230 249 $sync_order = new \ABR\Api\abrestan_sync_orders(); -
abrestan/trunk/inc/Api/abrestan_api.php
r2597509 r2643408 9 9 class abrestan_api 10 10 { 11 public function apiRequest($ method, $data = array())11 public function apiRequest($uri,$method, $data = array()) 12 12 { 13 if ($ method== null) {13 if ($uri == null) { 14 14 return false; 15 15 } 16 16 17 $endpoint = 'https://kasb.abrestan.com/webservice/v1/' . $ method;17 $endpoint = 'https://kasb.abrestan.com/webservice/v1/' . $uri; 18 18 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 24 24 $options = array( 25 25 'body' => $body, … … 30 30 'sslverify' => false, 31 31 'data_format' => 'body', 32 'method' => $method 32 33 ); 33 $wp_remote_post = wp_remote_post($endpoint, $options); 34 35 $wp_remote_post = wp_remote_request($endpoint, $options); 36 34 37 return $wp_remote_post; 35 38 } 36 39 public function Login($Data) 37 40 { 38 $method = 'login'; 39 return $this->apiRequest($method, $Data); 41 $uri = 'login'; 42 $method='POST'; 43 44 return $this->apiRequest($uri,$method, $Data); 40 45 } 41 46 public function SyncProduct($Data) 42 47 { 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); 45 52 } 46 53 public function SelectCompany($Data) 47 54 { 48 $method = 'set/domain'; 49 return $this->apiRequest($method, $Data); 55 $uri = 'set/domain'; 56 $method='POST'; 57 return $this->apiRequest($uri,$method, $Data); 50 58 } 51 59 public function SyncUser($Data) 52 60 { 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); 55 65 } 56 66 public function SyncOrder($Data) 57 67 { 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); 60 77 } 61 78 public function DeleteOrder($Data) 62 79 { 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); 65 83 } 66 84 } -
abrestan/trunk/inc/Api/abrestan_sync_orders.php
r2636452 r2643408 43 43 $orders = wc_get_orders($arg); 44 44 $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 45 65 foreach ($orders as $order) { 46 66 array_push($all_orders, $order->get_id()); 47 67 } 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); 50 80 } 51 52 81 53 82 … … 68 97 69 98 $company = get_option("abrestan_company")['companyCode']; 70 $abrestanOrders =abrestanGetOrderByOrderId($orders[0]);99 $abrestanOrders = abrestanGetOrderByOrderId($orders[0]); 71 100 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)) { 74 103 abrestan_log("Sync Order", "Invoice No. $orders[0] has already been registered in this business!", "ERROR"); 75 104 $data_feedback = [ … … 97 126 $wp_remote_post = $abrestanApi->SyncOrder($Data); 98 127 $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true); 99 if ($result['code']==105){100 128 101 $getToken=new GetToken(); 129 if ($result['code'] == 105) { 130 131 $getToken = new GetToken(); 102 132 $getToken->register(); 103 133 } … … 123 153 'class' => 'uk-alert uk-alert-success' 124 154 ]; 125 update_post_meta($orders[0], "order_diff",$factors);155 update_post_meta($orders[0], "order_diff", $factors); 126 156 delete_post_meta($orders[0], "abrestan_error"); 127 157 abrestan_log("Sync Order", "Order #" . $orders[0] . " was successfully synced.", "SUCCESS"); 128 158 $order = wc_get_order($orders[0]); 129 159 $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']); 131 161 return $data_feedback; 132 162 case 400:
Note: See TracChangeset
for help on using the changeset viewer.