Changeset 2636452
- Timestamp:
- 11/28/2021 07:39:14 AM (4 years ago)
- Location:
- abrestan/trunk
- Files:
-
- 6 edited
-
abrestan.php (modified) (6 diffs)
-
assets/js/custom210.js (modified) (2 diffs)
-
inc/Api/abrestan_CreateFactor.php (modified) (5 diffs)
-
inc/Api/abrestan_sync_orders.php (modified) (5 diffs)
-
inc/Controller/GetToken.php (modified) (2 diffs)
-
templates/admin/admin.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
abrestan/trunk/abrestan.php
r2597529 r2636452 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. 1.36 * Version: 2.2.0 7 7 * Author: KTE 8 8 * WC requires at least: 4.1.0 … … 38 38 39 39 40 register_activation_hook(__FILE__, 'install_Database_abrestan'); 41 function install_Database_abrestan() 42 { 43 global $wpdb; 44 $abrestanEvent = $wpdb->prefix . "abrestan_event"; 45 40 //register_activation_hook(__FILE__, 'install_Database_abrestan'); 41 global $abrestan_db_version; 42 $abrestan_db_version = "1.0.1"; 43 44 function booking_install() 45 { 46 global $wpdb; 47 global $abrestan_db_version; 48 global $tableprefix; 49 $installed_version = get_option('abrestan_db_option'); 50 51 $tableprefix = $wpdb->prefix . 'abrestan_'; 52 53 require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 46 54 $charset_collate = $wpdb->get_charset_collate(); 47 $sql1 = sprintf("CREATE TABLE %s ( 48 id integer NOT NULL AUTO_ABRREMENT, 49 time text NOT NULL, 50 status text NOT NULL, 51 action text NOT NULL, 52 message text NOT NULL, 55 56 if ($installed_version !== $abrestan_db_version) { 57 58 $eventtable = $tableprefix . 'event'; 59 $sql = "CREATE TABLE " . $eventtable . " ( 60 id integer NOT NULL AUTO_INCREMENT, 61 time TEXT NOT NULL, 62 status TEXT NOT NULL, 63 action TEXT NOT NULL, 64 message TEXT NOT NULL, 53 65 PRIMARY KEY (id) 54 ) %s;", $abrestanEvent, $charset_collate);55 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');56 dbDelta($sql1); 57 $abrestanOrder = $wpdb->prefix . "abrestan_order";58 $sql2 = sprintf("CREATE TABLE %s(59 id integer NOT NULL AUTO_ ABRREMENT,66 ) " . $charset_collate . ";"; 67 dbDelta($sql); 68 69 $ordertable = $tableprefix . 'order'; 70 $sql = "CREATE TABLE " . $ordertable . " ( 71 id integer NOT NULL AUTO_INCREMENT, 60 72 company_id integer NULL , 61 73 order_id integer NULL , 62 factor_id textNULL ,63 factor_no textNULL ,74 factor_id TEXT NULL , 75 factor_no TEXT NULL , 64 76 time timestamp NOT NULL, 65 77 PRIMARY KEY (id) 66 ) %s;", $abrestanOrder, $charset_collate); 67 require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 68 dbDelta($sql2); 69 } 78 ) " . $charset_collate . ";"; 79 dbDelta($sql); 80 81 update_option('abrestan_db_option', $abrestan_db_version); 82 } 83 } 84 85 register_activation_hook(__FILE__, 'booking_install'); 86 70 87 71 88 … … 114 131 $table_name = $wpdb->prefix . 'abrestan_order'; 115 132 $q = $wpdb->prepare("SELECT * FROM $table_name where order_id=%d", $order_id); 133 116 134 $results = $wpdb->get_results($q); 117 135 return $results; … … 125 143 $results = $wpdb->delete($table_name, ['order_id' => $order_id, 'company_id' => $company_id]); 126 144 return $results; 145 } 146 147 function abrestan_string_to_bool($string) 148 { 149 return is_bool($string) ? $string : ('yes' === strtolower($string) || 1 === $string || 'true' === strtolower($string) || '1' === $string); 127 150 } 128 151 … … 223 246 { 224 247 225 $sync_order = new \ABR\Api\abrestan_sync_orders();226 $sync_order->Send_orders([$orderid]);248 $sync_order = new \ABR\Api\abrestan_sync_orders(); 249 $sync_order->Send_orders([$orderid]); 227 250 228 251 … … 282 305 } 283 306 284 function edit_factor($orderid){ 285 286 if ($_REQUEST['post_type']=='shop_order'){ 287 if (isset(get_post_meta($orderid, "order_diff")[0])) { 288 $factors = new abrestan_CreateFactor(); 289 $factors = $factors->create_factor($orderid); 290 $aaa = get_post_meta($orderid, "order_diff")[0]; 291 292 $match = true; 293 foreach ($aaa as $key => $value) { 294 295 if (is_array($value)){ 296 foreach ($value as $subkey => $subvalue) { 297 if ($aaa[$key][$subkey] != $factors[$key][$subkey]) { 307 function edit_factor($orderid) 308 { 309 310 if ($_REQUEST['post_type'] == 'shop_order') { 311 if (isset(get_post_meta($orderid, "order_diff")[0])) { 312 $factors = new abrestan_CreateFactor(); 313 $factors = $factors->create_factor($orderid); 314 $aaa = get_post_meta($orderid, "order_diff")[0]; 315 316 $match = true; 317 foreach ($aaa as $key => $value) { 318 319 if (is_array($value)) { 320 foreach ($value as $subkey => $subvalue) { 321 if ($aaa[$key][$subkey] != $factors[$key][$subkey]) { 322 $match = false; 323 } 324 } 325 } else { 326 if ($aaa[$key] != $factors[$key]) { 298 327 $match = false; 299 328 } 300 329 } 301 }else{ 302 if ($aaa[$key] != $factors[$key]) { 303 $match = false; 304 } 330 305 331 } 306 332 307 } 308 309 if ($match) { 310 return false; 311 } 312 $company = get_option("abrestan_company")['companyCode']; 313 $abrestanOrders = abrestanGetOrderByOrderId($orderid); 314 if (isset($_REQUEST['action'])) { 315 if ($_REQUEST['action'] == 'trash') { 316 delete_factor($orderid); 317 } elseif ($_REQUEST['action'] == 'editpost') { 318 foreach ($abrestanOrders as $abrestanOrder) { 319 if ($abrestanOrder->company_id == $company && !empty($abrestanOrder->factor_id)) { 320 delete_factor($orderid); 321 $test = ($abrestanOrder->factor_id) ? true : false; 322 if ($test) { 323 $sync_order = new \ABR\Api\abrestan_sync_orders(); 324 $sync_order->Send_orders([$orderid]); 333 if ($match) { 334 return false; 335 } 336 $company = get_option("abrestan_company")['companyCode']; 337 $abrestanOrders = abrestanGetOrderByOrderId($orderid); 338 if (isset($_REQUEST['action'])) { 339 if ($_REQUEST['action'] == 'trash') { 340 delete_factor($orderid); 341 } elseif ($_REQUEST['action'] == 'editpost') { 342 foreach ($abrestanOrders as $abrestanOrder) { 343 if ($abrestanOrder->company_id == $company && !empty($abrestanOrder->factor_id)) { 344 delete_factor($orderid); 345 $test = ($abrestanOrder->factor_id) ? true : false; 346 if ($test) { 347 $sync_order = new \ABR\Api\abrestan_sync_orders(); 348 $sync_order->Send_orders([$orderid]); 349 } 325 350 } 326 351 } 327 352 } 353 328 354 } 329 330 355 } 331 } 332 333 } 356 357 } 334 358 335 359 } -
abrestan/trunk/assets/js/custom210.js
r2597509 r2636452 189 189 var category_user=$("input[name=category_user]").val(); 190 190 var InitialBalance=$("input[name=AddFactorItemsToInitialBalance]").prop('checked'); 191 var AddCustomerToOrder=$("input[name=AddCustomerToOrder]").prop('checked'); 191 192 $('.loader').show(); 192 193 $(this).attr('disabled', 'disabled'); … … 199 200 'category_user':category_user, 200 201 'InitialBalance':InitialBalance, 202 'AddCustomerToOrder':AddCustomerToOrder, 201 203 }; 202 204 $.ajax({ -
abrestan/trunk/inc/Api/abrestan_CreateFactor.php
r2597509 r2636452 14 14 public $order_id; 15 15 16 public function randomStr($n = 10) 17 { 18 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 19 $randomString = ''; 20 for ($i = 0; $i < $n; $i++) { 21 $index = rand(0, strlen($characters) - 1); 22 $randomString .= $characters[$index]; 23 } 24 25 return $randomString; 26 } 16 27 17 28 public function create_factor($order_id) … … 20 31 $order = wc_get_order($order_id); 21 32 $custmer_id = $order->get_customer_id(); 33 34 $order_currency = $order->get_currency(); 35 $order_items = $order->get_items(); 36 37 $order_billing_data = $order->get_data()['billing']; 38 $custmerFirstName = $order_billing_data['first_name']; 39 $custmerLastName = $order_billing_data['last_name']; 40 22 41 if ($custmer_id == 0) { 23 return 0; 42 if (abrestan_string_to_bool(get_option('abrestan_setting')['AddCustomerToOrder'])) { 43 $lastUser = get_user_by('login', $order_billing_data['phone']); 44 45 if ($lastUser) { 46 $custmer_id = $lastUser->ID; 47 update_post_meta($order->get_id(), '_customer_user', $custmer_id); 48 } else { 49 $userdata = array( 50 'user_pass' => $this->randomStr(12), //(string) The plain-text user password. 51 'user_login' => $order_billing_data['phone'], //(string) The user's login username. 52 'user_email' => $order_billing_data['email'], //(string) The user email address. 53 'display_name' => $custmerFirstName . " " . $custmerLastName, //(string) The user's display name. Default is the user's username. 54 'first_name' => $custmerFirstName, //(string) The user's first name. For new users, will be used to build the first part of the user's display name if $display_name is not specified. 55 'last_name' => $custmerLastName, //(string) The user's last name. For new users, will be used to build the second part of the user's display name if $display_name is not specified. 56 'role' => 'Customer', 57 ); 58 $user_id = wp_insert_user($userdata); 59 update_post_meta($order->get_id(), '_customer_user', $user_id); 60 $custmer_id = $user_id; 61 } 62 } else { 63 $custmer_id = "9999"; 64 $custmerFirstName = "کاربر"; 65 $custmerLastName = "مهمان"; 66 } 67 24 68 } 25 $order_currency = $order->get_currency();26 69 27 $order_items = $order->get_items();28 $order_data = $order->get_data();29 70 $products = array(); 30 71 foreach ($order_items as $order_item) { … … 36 77 if ($product_id != "error") { 37 78 $product_name = $product_details->get_product_name($product_id); 38 39 79 $product_quantity = $product_data['quantity']; 40 41 80 $product_price = $item_details->covert_price($product_data['subtotal'] / $product_data['quantity'], $order_currency); 42 81 $product_discount = $item_details->covert_price($product_data['subtotal'] - $product_data['total'], $order_currency); … … 58 97 59 98 $jalali_date = new ConvertDate($order->get_date_created()->date("Y-m-d H:i:s")); 60 $order_billing_data = $order->get_data()['billing']; 99 61 100 62 101 $display_name = get_userdata($custmer_id); … … 64 103 $company = get_option("abrestan_company")['companyCode']; 65 104 66 $factor = array(67 "factor_date" => $jalali_date->convert_date(),68 "company_id" => $company,69 "customer" =>70 [71 "firstname" => $order_billing_data['first_name'],72 "lastname" => $order_billing_data['last_name'],73 "displayname" => $order_billing_data['first_name']." ".$order_billing_data['last_name'],74 "w_code" => $custmer_id,75 "type" => 1,76 "tel" => $order_billing_data['phone'],77 "email" => $order_billing_data['email'],78 "city" => $order_billing_data['city'],79 "address" => $order_billing_data['address_1'],80 "postal_code" => $order_billing_data['postcode'],81 ],82 "factor_items" => $products,83 "transition_cost" => $transition,105 $factor = array( 106 "factor_date" => $jalali_date->convert_date(), 107 "company_id" => $company, 108 "customer" => 109 [ 110 "firstname" => $custmerFirstName, 111 "lastname" => $custmerLastName, 112 "displayname" => $custmerFirstName . " " . $custmerLastName, 113 "w_code" => $custmer_id, 114 "type" => 1, 115 "tel" => $order_billing_data['phone'], 116 "email" => $order_billing_data['email'], 117 "city" => $order_billing_data['city'], 118 "address" => $order_billing_data['address_1'], 119 "postal_code" => $order_billing_data['postcode'], 120 ], 121 "factor_items" => $products, 122 "transition_cost" => $transition, 84 123 85 );124 ); 86 125 87 126 88 if (get_post_meta($order->get_id(), "factor_id_abr")) {89 $factor1 = get_post_meta($order->get_id(), "factor_id_abr");90 $factor_id = array(91 "factor_id" => $factor1['0']92 );93 $factor = array_merge($factor, $factor_id);127 if (get_post_meta($order->get_id(), "factor_id_abr")) { 128 $factor1 = get_post_meta($order->get_id(), "factor_id_abr"); 129 $factor_id = array( 130 "factor_id" => $factor1['0'] 131 ); 132 $factor = array_merge($factor, $factor_id); 94 133 95 } 96 return $factor; 134 } 97 135 136 return $factor; 98 137 99 138 } -
abrestan/trunk/inc/Api/abrestan_sync_orders.php
r2597509 r2636452 5 5 6 6 7 use ABR\Controller\GetToken; 7 8 use Illuminate\Support\Facades\Date; 8 9 use ABR\Api\abrestan_CreateFactor; … … 49 50 } 50 51 51 function abrestan_string_to_bool($string) 52 { 53 return is_bool($string) ? $string : ('yes' === strtolower($string) || 1 === $string || 'true' === strtolower($string) || '1' === $string); 54 } 52 55 53 56 54 public function Send_orders($orders) … … 58 56 $factors = new abrestan_CreateFactor(); 59 57 $factors = $factors->create_factor($orders[0]); 60 61 58 if ($factors == 0) { 62 59 abrestan_log("Sync Order", "Order #$orders[0] information is not complete!", "ERROR"); … … 93 90 'transition_cost' => $factors['transition_cost'], 94 91 'factor_items' => json_encode($factors['factor_items']), 95 'AddFactorItemsToInitialBalance' => $this->abrestan_string_to_bool(get_option("abrestan_setting")['InitialBalance']),92 'AddFactorItemsToInitialBalance' => abrestan_string_to_bool(get_option("abrestan_setting")['InitialBalance']), 96 93 97 94 ]; … … 99 96 $abrestanApi = new abrestan_api(); 100 97 $wp_remote_post = $abrestanApi->SyncOrder($Data); 98 $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true); 99 if ($result['code']==105){ 101 100 102 $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true); 101 $getToken=new GetToken(); 102 $getToken->register(); 103 } 103 104 104 105 if ($wp_remote_post->errors) { -
abrestan/trunk/inc/Controller/GetToken.php
r2597530 r2636452 12 12 public function register() 13 13 { 14 14 15 if (get_option('abrestan_login')) { 16 15 17 $time = get_option("abrestan_login")['time']; 16 18 $timeToGet = get_option("abrestan_login")['ttl']; 19 17 20 if (time() - $time >= $timeToGet) { 18 21 $Encrypted_username=get_option('abrestan_login')['username']; … … 26 29 27 30 ]; 31 28 32 $abrestanApi = new abrestan_api(); 29 33 $wp_remote_post = $abrestanApi->Login($Data); 30 //custom_logs("Get Token",$feedback); 31 if ($wp_remote_post['response'] == 200) { 32 $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true); 33 $data_option = 34 [ 35 'username' => $Encrypted_username, 36 'password' => $Encrypted_password, 37 'token' => $result['data']['token'], 38 'ttl' => $result['data']['ttl'], 39 'time' => time() 40 ]; 41 update_option('abrestan_login', $data_option); 42 update_option('abrestan_companies_list', $result['data']['companies_list']); 34 $result = json_decode(wp_remote_retrieve_body($wp_remote_post), true); 43 35 44 $company=new \ABR\Api\abrestan_company(); 45 $company->Company(get_option("abrestan_company")['companyCode'],get_option('abrestan_company')['companyName']); 36 if ($wp_remote_post['response']['code'] == 200) { 37 38 switch ($result['code']){ 39 case 104: 40 $data_option = 41 [ 42 'username' => $Encrypted_username, 43 'password' => $Encrypted_password, 44 'token' => $result['token'], 45 'ttl' => $result['ttl'], 46 'time' => time() 47 ]; 48 49 update_option('abrestan_login', $data_option); 50 update_option('abrestan_companies_list', $result['companies_list']); 51 $company=new \ABR\Api\abrestan_company(); 52 $company->Company(get_option("abrestan_company")['companyCode'],get_option('abrestan_company')['companyName']); 53 } 54 46 55 } 47 56 } -
abrestan/trunk/templates/admin/admin.php
r2597509 r2636452 279 279 280 280 <li> 281 <div id="alert_success_save_setting" class="uk-text-lighter" uk-alert 282 style="display: none" dir="rtl"> 283 <p></p> 284 </div> 281 285 <!-- 282 286 <div class="uk-form-horizontal uk-margin-large"> … … 395 399 </div> 396 400 <div class="uk-margin"> 401 <div class="uk-alert-primary uk-text-lighter" uk-alert> 402 <p> 403 با فعال نمودن این گزینه به صورت خودکار برای سفارش هایی که مشتری آن مهمان است 404 کاربر ساخته میشود. 405 </p> 406 </div> 407 408 <label> 409 <input class="uk-checkbox uk-form-width-medium" type="checkbox" 410 name="AddCustomerToOrder" <?php echo (get_option('abrestan_setting')['AddCustomerToOrder'] == "true") ? 'checked' : '' ?>> <?php _e('ساخت کاربر', 'abrestan') ?> 411 </label> 412 </div> 413 <div class="uk-margin"> 397 414 <div class="uk-flex uk-flex-around "> 398 415 <button class="uk-button uk-button-primary uk-button-large btn_loader"
Note: See TracChangeset
for help on using the changeset viewer.