diff --git a/src/include/api/class-wc-retailcrm-proxy.php b/src/include/api/class-wc-retailcrm-proxy.php index 027f11f..33f25ed 100644 --- a/src/include/api/class-wc-retailcrm-proxy.php +++ b/src/include/api/class-wc-retailcrm-proxy.php @@ -39,19 +39,19 @@ if (!class_exists('WC_Retailcrm_Proxy')) : } /** - * Response will be omitted in debug logs for those methods + * Response will be omitted in logs for those methods * * @return string[] */ - private function methodsWithoutFullResponse() + private function methodsWithoutFullLog() { - $methodsList = array( + $methodsList = [ 'statusesList', 'paymentTypesList', 'deliveryTypesList', 'orderMethodsList', - 'storesList' - ); + 'storesList', + ]; foreach ($methodsList as $key => $method) { $method = get_class($this->retailcrm) . '::' . $method; @@ -69,13 +69,15 @@ if (!class_exists('WC_Retailcrm_Proxy')) : try { WC_Retailcrm_Logger::info( $method, - empty($arguments) ? '[no params]' : json_encode($arguments), WC_Retailcrm_Logger::TYPE[0] + empty($arguments) ? '[no params]' : json_encode($arguments), + WC_Retailcrm_Logger::TYPE['req'] ); /** @var \WC_Retailcrm_Response $response */ $response = call_user_func_array(array($this->retailcrm, $method), $arguments); if (is_string($response)) { - WC_Retailcrm_Logger::info($method, $response, WC_Retailcrm_Logger::TYPE[1]); + WC_Retailcrm_Logger::info($method, $response, WC_Retailcrm_Logger::TYPE['res']); + return $response; } @@ -83,29 +85,27 @@ if (!class_exists('WC_Retailcrm_Proxy')) : WC_Retailcrm_Logger::error( $method, sprintf("[%s] null (no response whatsoever)", $called), - WC_Retailcrm_Logger::TYPE[1] + WC_Retailcrm_Logger::TYPE['res'] ); return null; } if ($response->isSuccessful()) { - // Don't print long lists in debug logs (errors while calling this will be easy to detect anyway) - // Also don't call useless array_map at all while debug mode is off. if (in_array( $called, - $this->methodsWithoutFullResponse() + $this->methodsWithoutFullLog() )) { WC_Retailcrm_Logger::info( $method, 'Ok [request was successful, but response is omitted]', - WC_Retailcrm_Logger::TYPE[1] + WC_Retailcrm_Logger::TYPE['res'] ); } else { WC_Retailcrm_Logger::info( $method, 'Ok ' . $response->getRawResponse(), - WC_Retailcrm_Logger::TYPE[1] + WC_Retailcrm_Logger::TYPE['res'] ); } @@ -115,15 +115,45 @@ if (!class_exists('WC_Retailcrm_Proxy')) : $response->getStatusCode(), $response->getErrorString(), $response->getRawResponse()), - WC_Retailcrm_Logger::TYPE[1] + WC_Retailcrm_Logger::TYPE['res'] ); } } catch (WC_Retailcrm_Exception_Curl $exception) { - WC_Retailcrm_Logger::error($method, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + $method, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } catch (WC_Retailcrm_Exception_Json $exception) { - WC_Retailcrm_Logger::error($method, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + $method, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } catch (InvalidArgumentException $exception) { - WC_Retailcrm_Logger::error($method, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + $method, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } return !empty($response) ? $response : new WC_Retailcrm_Response(900, '{}'); diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index e7bde3f..75de47b 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -366,6 +366,7 @@ if (!class_exists('WC_Retailcrm_Base')) { private function uploadCatalog($infoApiKey) { WC_Retailcrm_Logger::info(__METHOD__, 'Add task for automatically upload catalog in CRM'); + if ($infoApiKey->isSuccessful() && !empty($infoApiKey['scopes'])) { if (!in_array('analytics_write', $infoApiKey['scopes'])) { WC_Retailcrm_Logger::error( @@ -381,7 +382,7 @@ if (!class_exists('WC_Retailcrm_Base')) { if ($statisticUpdate->isSuccessful()) { WC_Retailcrm_Logger::info( __METHOD__, - 'Catalog generated, task automatically upload added to CRM' + 'Catalog generated, upload task added to CRM' ); } else { WC_Retailcrm_Logger::error( @@ -482,6 +483,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function create_customer($customerId) { WC_Retailcrm_Logger::setEntry(__FUNCTION__, $customerId); + if (WC_Retailcrm_Plugin::history_running() === true) { return; } @@ -536,9 +538,13 @@ if (!class_exists('WC_Retailcrm_Base')) { */ public function create_order($order_id) { + WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id); + if (is_admin()) { - WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id); + WC_Retailcrm_Logger::info(__METHOD__, 'Creation is from admin panel'); $this->retailcrm_process_order($order_id); + } else { + WC_Retailcrm_Logger::info(__METHOD__, 'Creation is not from admin panel, skip'); } } @@ -587,7 +593,17 @@ if (!class_exists('WC_Retailcrm_Base')) { } } } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } @@ -641,7 +657,14 @@ if (!class_exists('WC_Retailcrm_Base')) { } catch (Throwable $exception) { WC_Retailcrm_Logger::info( __METHOD__, - $exception->getMessage() + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); } } @@ -649,7 +672,10 @@ if (!class_exists('WC_Retailcrm_Base')) { public function update_order($orderId) { WC_Retailcrm_Logger::setEntry(__FUNCTION__, $orderId); + if (WC_Retailcrm_Plugin::history_running() === true) { + WC_Retailcrm_Logger::info(__METHOD__, 'History in progress, skip'); + return; } @@ -668,11 +694,17 @@ if (!class_exists('WC_Retailcrm_Base')) { public function take_update_order($order_id) { WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id); + if ( WC_Retailcrm_Plugin::history_running() === true || did_action('woocommerce_checkout_order_processed') || did_action('woocommerce_new_order') ) { + WC_Retailcrm_Logger::info( + __METHOD__, + 'History in progress or already did actions (woocommerce_checkout_order_processed;woocommerce_new_order), skip' + ); + return; } @@ -682,6 +714,7 @@ if (!class_exists('WC_Retailcrm_Base')) { public function update_order_loyalty() { WC_Retailcrm_Logger::setEntry(__FUNCTION__); + if ($this->updatedOrderId !== []) { foreach ($this->updatedOrderId as $orderId) { $this->orders->updateOrder($orderId); @@ -799,8 +832,10 @@ if (!class_exists('WC_Retailcrm_Base')) { if (!$isSuccessful) { WC_Retailcrm_Logger::error( __METHOD__, - 'Errors when registering a loyalty program. Passed parameters: ' . - json_encode(['site' => $site, 'userId' => $userId, 'phone' => $phone]) + sprintf( + 'Errors when registering a loyalty program. Passed parameters: %s', + json_encode(['site' => $site, 'userId' => $userId, 'phone' => $phone]) + ) ); echo json_encode(['error' => __('Error while registering in the loyalty program. Try again later', 'retailcrm')]); } else { @@ -850,7 +885,17 @@ if (!class_exists('WC_Retailcrm_Base')) { wp_enqueue_script('retailcrm-loyalty-cart', $jsScriptPath, '', '', true); wp_localize_script('retailcrm-loyalty-cart', 'AdminUrl', $wpAdminUrl); } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } @@ -861,7 +906,17 @@ if (!class_exists('WC_Retailcrm_Base')) { try { $this->loyalty->createLoyaltyCoupon(true); } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } @@ -872,31 +927,63 @@ if (!class_exists('WC_Retailcrm_Base')) { try { $this->loyalty->clearLoyaltyCoupon(); } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } public function remove_coupon($couponCode) { WC_Retailcrm_Logger::setEntry(__FUNCTION__); + try { if (!$this->loyalty->deleteLoyaltyCoupon($couponCode)) { $this->loyalty->createLoyaltyCoupon(true); } } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } public function apply_coupon($couponCode) { WC_Retailcrm_Logger::setEntry(__FUNCTION__); + try { if (!$this->loyalty->isLoyaltyCoupon($couponCode)) { $this->loyalty->createLoyaltyCoupon(true); } } catch (Throwable $exception) { - WC_Retailcrm_Logger::error(__METHOD__, $exception->getMessage(), WC_Retailcrm_Logger::TYPE[2]); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + '%s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] + ); } } @@ -1080,6 +1167,7 @@ if (!class_exists('WC_Retailcrm_Base')) { if (!$this->apiClient instanceof WC_Retailcrm_Proxy) { return null; } + WC_Retailcrm_Logger::setEntry(__FUNCTION__); $orderMetaData = $this->getMetaData('order'); @@ -1136,6 +1224,7 @@ if (!class_exists('WC_Retailcrm_Base')) { if (!isset($userId)) { return; } + WC_Retailcrm_Logger::setEntry(__FUNCTION__); $jsScript = 'retailcrm-loyalty-actions'; diff --git a/src/include/class-wc-retailcrm-cart.php b/src/include/class-wc-retailcrm-cart.php index d0b610a..7e405d7 100644 --- a/src/include/class-wc-retailcrm-cart.php +++ b/src/include/class-wc-retailcrm-cart.php @@ -82,8 +82,14 @@ if (!class_exists('WC_Retailcrm_Carts')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Error process cart: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2] + sprintf( + 'Error process cart: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); } @@ -103,8 +109,14 @@ if (!class_exists('WC_Retailcrm_Carts')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Error clear cart: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2]) + sprintf( + 'Error clear cart: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc']) ; } diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 0c22bf2..064ae47 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -98,7 +98,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } - WC_Retailcrm_Logger::info(__METHOD__, 'WC_Customer: ' . $customerId); + + WC_Retailcrm_Logger::info(__METHOD__, 'Register WC_Customer ID: ' . $customerId); $wcCustomer = new WC_Customer($customerId); $email = $wcCustomer->get_billing_email(); @@ -181,7 +182,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : if ($this->isCustomer($customer)) { WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) + __METHOD__, + 'WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) ); $this->processCustomer($customer, $order); $response = $this->retailcrm->customersCreate($this->customer); @@ -212,7 +214,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : if ($this->isCustomer($customer)) { WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) + __METHOD__, + 'Update WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) ); $this->processCustomer($customer); $this->retailcrm->customersEdit($this->customer); @@ -239,10 +242,13 @@ if (!class_exists('WC_Retailcrm_Customers')) : $customer = $this->wcCustomerGet($customerId); if ($this->isCustomer($customer)) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'WC_Customer: %s. CRM_Customer ID: %s', - WC_Retailcrm_Logger::formatWCObject($customer), - $crmCustomerId) + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Update WC_Customer: %s by CRM_Customer ID: %s', + WC_Retailcrm_Logger::formatWCObject($customer), + $crmCustomerId + ) ); $this->processCustomer($customer); $this->customer['id'] = $crmCustomerId; @@ -388,7 +394,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : public function processCustomerForUpload($customer) { WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) + __METHOD__, + 'Processing for upload WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer) ); $this->processCustomer($customer); } @@ -697,14 +704,10 @@ if (!class_exists('WC_Retailcrm_Customers')) : $new_customer->set_email($order->get_billing_email()); $new_customer->set_date_created($order->get_date_created()); - WC_Retailcrm_Logger::info(__METHOD__,'New customer: ', json_encode( - [ - 'firstName' => $new_customer->get_first_name(), - 'lastName' => $new_customer->get_last_name(), - 'email' => $new_customer->get_email(), - 'created' => $new_customer->get_date_created() - ] - )); + WC_Retailcrm_Logger::info( + __METHOD__, + 'Build new customer from order data: ' . WC_Retailcrm_Logger::formatWCObject($new_customer) + ); return $new_customer; } diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 243f38c..6a5ac55 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -86,11 +86,13 @@ if (!class_exists('WC_Retailcrm_History')) : WC_Retailcrm_Logger::error( __METHOD__, sprintf( - '[%s] - %s', + '%s - Exception in file %s on line %s Trace: %s', $exception->getMessage(), - 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine() + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() ), - WC_Retailcrm_Logger::TYPE[2] + WC_Retailcrm_Logger::TYPE['exc'] ); } // @codeCoverageIgnoreEnd @@ -120,6 +122,8 @@ if (!class_exists('WC_Retailcrm_History')) : update_option('retailcrm_customers_history_since_id', $lastChange['id']); + WC_Retailcrm_Logger::info(__METHOD__, 'Processing customers history, ID: ' . $filter['sinceId']); + $builder = new WC_Retailcrm_WC_Customer_Builder(); $customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history); @@ -142,10 +146,13 @@ if (!class_exists('WC_Retailcrm_History')) : // @codeCoverageIgnoreStart if (!$builder->loadExternalId($crmCustomer['externalId'])) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'Customer with id=%s is not found in the DB, skipping...', - $crmCustomer['externalId'] - )); + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Customer with id=%s is not found in the DB, skipping...', + $crmCustomer['externalId'] + ) + ); // If customer not found in the DB, update sinceId $filter['sinceId'] = $lastChange['id']; @@ -172,22 +179,27 @@ if (!class_exists('WC_Retailcrm_History')) : $this->updateMetaData($customFields, $crmCustomer, $wcCustomer); } - WC_Retailcrm_Logger::info(__METHOD__, sprintf( + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( 'Updated WC_Customer %s: %s', $crmCustomer['externalId'], WC_Retailcrm_Logger::formatWCObject($wcCustomer) - )); + ) + ); // @codeCoverageIgnoreStart } catch (Exception $exception) { WC_Retailcrm_Logger::error( __METHOD__, sprintf( - 'Error: %s - %s', + '%s - Exception in file %s on line %s Trace: %s', $exception->getMessage(), - 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine() + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() ), - WC_Retailcrm_Logger::TYPE[2] + WC_Retailcrm_Logger::TYPE['exc'] ); } // @codeCoverageIgnoreEnd @@ -229,10 +241,7 @@ if (!class_exists('WC_Retailcrm_History')) : update_option('retailcrm_orders_history_since_id', $lastChange['id']); - WC_Retailcrm_Logger::info(__METHOD__, - 'Processing orders history, ID: ' . - $filter['sinceId'] - ); + WC_Retailcrm_Logger::info(__METHOD__, 'Processing orders history, ID: ' . $filter['sinceId']); $historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history); @@ -307,11 +316,13 @@ if (!class_exists('WC_Retailcrm_History')) : WC_Retailcrm_Logger::error( __METHOD__, sprintf( - 'Error: %s - %s', + '%s - Exception in file %s on line %s Trace: %s', $exception->getMessage(), - 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine() + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() ), - WC_Retailcrm_Logger::TYPE[2] + WC_Retailcrm_Logger::TYPE['exc'] ); continue; @@ -413,6 +424,7 @@ if (!class_exists('WC_Retailcrm_History')) : if (!$wcOrder instanceof WC_Order) { return false; } + WC_Retailcrm_Logger::info(__METHOD__, 'Updating WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder)); if (isset($order['status']) && isset($options[$order['status']])) { @@ -493,7 +505,11 @@ if (!class_exists('WC_Retailcrm_History')) : ); if (!$wcProduct) { - WC_Retailcrm_Logger::error(__METHOD__, 'Product not found by ' . $this->bindField); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf('Product %s not found by %s', json_encode($crmProduct['offer']), $this->bindField) + ); + continue; } @@ -655,6 +671,7 @@ if (!class_exists('WC_Retailcrm_History')) : if (!isset($order['create'])) { return false; } + WC_Retailcrm_Logger::info(__METHOD__, 'Creating WC_Order from CRM_Order: ' . json_encode($order)); if ( @@ -711,11 +728,7 @@ if (!class_exists('WC_Retailcrm_History')) : } else { WC_Retailcrm_Logger::error( __METHOD__, - sprintf( - '[%d] => %s', - $order['id'], - 'Error: Contact address is empty' - ) + 'Error: Contact address is empty. Order ID: ' . $order['id'] ); } @@ -734,7 +747,8 @@ if (!class_exists('WC_Retailcrm_History')) : $billingAddress = $customer['address']; } else { WC_Retailcrm_Logger::error( - __METHOD__, 'Error: Customer address is empty. Order ID: ' . $order['id'] + __METHOD__, + 'Error: Customer address is empty. Order ID: ' . $order['id'] ); } @@ -749,11 +763,14 @@ if (!class_exists('WC_Retailcrm_History')) : } if ($wcOrder instanceof WP_Error) { - WC_Retailcrm_Logger::error(__METHOD__, sprintf( - '[%d] error while creating order: %s', - $order['id'], - json_encode($wcOrder->get_error_messages()) - )); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf( + 'Error while creating order [%d]: %s', + $order['id'], + json_encode($wcOrder->get_error_messages()) + ) + ); return false; } @@ -842,7 +859,11 @@ if (!class_exists('WC_Retailcrm_History')) : ); if (!$wcProduct) { - WC_Retailcrm_Logger::error(__METHOD__, 'Product not found by ' . $this->bindField); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf('Crm_Product %s not found by %s', json_encode($crmProduct['offer']), $this->bindField) + ); + continue; } @@ -971,6 +992,7 @@ if (!class_exists('WC_Retailcrm_History')) : $item['id'] ) ); + continue; // @codeCoverageIgnoreEnd } @@ -1128,12 +1150,15 @@ if (!class_exists('WC_Retailcrm_History')) : */ private function addProductInWcOrder($wcOrder, $wcProduct, $crmProduct) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'WC_Order: %s, WC_Product: %s, CRM_Product: %s', - WC_Retailcrm_Logger::formatWCObject($wcOrder), - WC_Retailcrm_Logger::formatWCObject($wcProduct), - json_encode($crmProduct) - )); + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Add in order WC_Order: %s, product WC_Product: %s, CRM_Product: %s', + WC_Retailcrm_Logger::formatWCObject($wcOrder), + WC_Retailcrm_Logger::formatWCObject($wcProduct), + json_encode($crmProduct) + ) + ); $discountTotal = $crmProduct['discountTotal']; $productQuantity = $crmProduct['quantity']; @@ -1188,7 +1213,7 @@ if (!class_exists('WC_Retailcrm_History')) : WC_Retailcrm_Logger::info( __METHOD__, sprintf( - 'WC_Order_Item: %s, CRM_Product: %s', + 'Updating product WC_Order_Item: %s, CRM_Product: %s', WC_Retailcrm_Logger::formatWCObject($wcOrderItem), json_encode($crmProduct) ) @@ -1235,10 +1260,13 @@ if (!class_exists('WC_Retailcrm_History')) : $crmOrder = $this->getCRMOrder($order['id'], 'id'); if (empty($crmOrder)) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'Cannot get order data from retailCRM. Skipping customer change. History data: %s', - json_encode($order) - )); + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Cannot get order data from retailCRM. Skipping customer change. History data: %s', + json_encode($order) + ) + ); return false; } @@ -1268,10 +1296,13 @@ if (!class_exists('WC_Retailcrm_History')) : } if (empty($crmOrder)) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'Cannot get order data from retailCRM. Skipping customer change. History data: %s', - json_encode($order) - )); + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Cannot get order data from retailCRM. Skipping customer change. History data: %s', + json_encode($order) + ) + ); return false; } @@ -1306,20 +1337,20 @@ if (!class_exists('WC_Retailcrm_History')) : // @codeCoverageIgnoreStart } catch (\Exception $exception) { $errorMessage = sprintf( - 'Error switching order externalId=%s to customer id=%s (new company: id=%s %s). Reason: %s', + 'Error switching order externalId=%s to customer id=%s (new company: id=%s %s). Reason: %s' . + ' - Exception in file %s on line %s. Trace: %s' + , $order['externalId'], $newCustomerId, isset($order['company']) ? $order['company']['id'] : '', isset($order['company']) ? $order['company']['name'] : '', - $exception->getMessage() + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() ); - WC_Retailcrm_Logger::error(__METHOD__, sprintf( - '%s - %s', - $errorMessage, - 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine()), - WC_Retailcrm_Logger::TYPE[2] - ); + WC_Retailcrm_Logger::error(__METHOD__, $errorMessage, WC_Retailcrm_Logger::TYPE['exc']); $handled = false; } // @codeCoverageIgnoreEnd @@ -1427,10 +1458,11 @@ if (!class_exists('WC_Retailcrm_History')) : { WC_Retailcrm_Logger::info( __METHOD__, - 'Using this individual person data in order to set it into order,' . - $data->getWcOrder()->get_id() . - ': ' . - json_encode($crmCustomer) + sprintf( + 'Using this individual person data in order to set it into order %s: %s', + $data->getWcOrder()->get_id(), + json_encode($crmCustomer) + ) ); if ($isContact) { diff --git a/src/include/class-wc-retailcrm-icml.php b/src/include/class-wc-retailcrm-icml.php index 42dc1b8..682dbc0 100644 --- a/src/include/class-wc-retailcrm-icml.php +++ b/src/include/class-wc-retailcrm-icml.php @@ -77,6 +77,7 @@ if (!class_exists('WC_Retailcrm_Icml')) : if (empty($categories)) { WC_Retailcrm_Logger::error(__METHOD__, 'Can`t get categories!'); + return; } @@ -86,6 +87,7 @@ if (!class_exists('WC_Retailcrm_Icml')) : if (empty($offers)) { WC_Retailcrm_Logger::error(__METHOD__, 'Can`t get offers!'); + return; } diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index 59c6bb6..8f72a1e 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -50,8 +50,14 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Exception get loyalty accounts: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2] + sprintf( + 'Exception get loyalty accounts: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); return $result; @@ -97,8 +103,14 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Exception while registering in the loyalty program: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2] + sprintf( + 'Exception while registering in the loyalty program: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); return false; @@ -121,8 +133,14 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Exception while activate loyalty account: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2] + sprintf( + 'Exception while activate loyalty account: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); return false; @@ -361,8 +379,14 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } catch (Throwable $exception) { WC_Retailcrm_Logger::error( __METHOD__, - 'Exception get loyalty accounts: ' . $exception->getMessage(), - WC_Retailcrm_Logger::TYPE[2] + sprintf( + 'Exception get loyalty accounts: %s - Exception in file %s on line %s Trace: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ), + WC_Retailcrm_Logger::TYPE['exc'] ); return false; diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 0fc0faf..6376a7e 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -95,6 +95,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } + WC_Retailcrm_Logger::info(__METHOD__, 'Start order creating ' . $orderId); try { @@ -128,7 +129,8 @@ if (!class_exists('WC_Retailcrm_Orders')) : } WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Order: ' . WC_RETAILCRM_LOGGER::formatWCObject($wcOrder) + __METHOD__, + 'WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder) ); $this->processOrder($wcOrder); @@ -152,12 +154,13 @@ if (!class_exists('WC_Retailcrm_Orders')) : WC_Retailcrm_Logger::error( __METHOD__, sprintf( - 'Error message: %s, file: %s on line: %s', + 'Error message: %s - Exception in file: %s on line: %s. Trace: %s', $exception->getMessage(), $exception->getFile(), - $exception->getLine() + $exception->getLine(), + $exception->getTraceAsString() ), - WC_Retailcrm_Logger::TYPE[2] + WC_Retailcrm_Logger::TYPE['exc'] ); return null; @@ -220,8 +223,14 @@ if (!class_exists('WC_Retailcrm_Orders')) : */ protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder) { - WC_Retailcrm_Logger::info(__METHOD__, sprintf( - 'WC_Customer ID: %s email: %s WC_Order ID: %s', $wcCustomerId, $wcCustomerEmail, $wcOrder->get_id()) + WC_Retailcrm_Logger::info( + __METHOD__, + sprintf( + 'Fill order data: WC_Customer ID: %s email: %s WC_Order ID: %s', + $wcCustomerId, + $wcCustomerEmail, + $wcOrder->get_id() + ) ); $isContact = $this->retailcrm->getCorporateEnabled() && static::isCorporateOrder($wcOrder); @@ -278,11 +287,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : if (!$addressFound) { WC_Retailcrm_Logger::info( __METHOD__, - sprintf( - '[%d] => %s', - $this->order['customer']['id'], - 'Notification: Create new address for corporate customer' - ) + 'Notification: Create new address for corporate customer ' . $this->order['customer']['id'] ); } @@ -331,7 +336,8 @@ if (!class_exists('WC_Retailcrm_Orders')) : try { $wcOrder = wc_get_order($orderId); WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder) + __METHOD__, + 'Update WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder) ); $needRecalculate = false; @@ -371,12 +377,13 @@ if (!class_exists('WC_Retailcrm_Orders')) : WC_Retailcrm_Logger::error( __METHOD__, sprintf( - 'Error message: %s, file: %s on line: %s', + 'Error message: %s - Exception in file: %s on line: %s. Trace: %s', $exception->getMessage(), $exception->getFile(), - $exception->getLine() + $exception->getLine(), + $exception->getTraceAsString() ), - WC_Retailcrm_Logger::TYPE[2] + WC_Retailcrm_Logger::TYPE['exc'] ); return null; @@ -448,6 +455,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : if ('auto-draft' === $order->get_status()) { WC_Retailcrm_Logger::info(__METHOD__, 'Skip, order in auto-draft status'); + return; } @@ -534,13 +542,14 @@ if (!class_exists('WC_Retailcrm_Orders')) : /** @var WC_Order_Item_Product $item */ foreach ($wcItems as $id => $item) { + WC_Retailcrm_Logger::info( + __METHOD__, + 'Process WC_Order_Item_Product: ' . WC_Retailcrm_Logger::formatWCObject($item) + ); $crmItem = $crmItems[$id] ?? null; $orderItems[] = $this->order_item->build($item, $crmItem)->getData(); $this->order_item->resetData($this->cancelLoyalty); - WC_Retailcrm_Logger::info( - __METHOD__, 'WC_Order_Item_Product: ' . WC_RETAILCRM_LOGGER::formatWCObject($item) - ); } unset($crmItems, $crmItem); diff --git a/src/include/class-wc-retailcrm-uploader.php b/src/include/class-wc-retailcrm-uploader.php index 9e96a27..6359c7c 100644 --- a/src/include/class-wc-retailcrm-uploader.php +++ b/src/include/class-wc-retailcrm-uploader.php @@ -60,7 +60,7 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { public function uploadSelectedOrders() { $ids = $_GET['order_ids_retailcrm']; - WC_Retailcrm_Logger::info(__METHOD__, 'IDs: ' . json_encode($ids)); + WC_Retailcrm_Logger::info(__METHOD__, 'Selected order IDs: ' . json_encode($ids)); if (!empty($ids)) { preg_match_all('/\d+/', $ids, $matches); @@ -82,7 +82,8 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { */ public function uploadArchiveOrders($page, $ids = []) { - WC_Retailcrm_Logger::info(__METHOD__, 'IDs: ' . implode(', ', $ids)); + WC_Retailcrm_Logger::info(__METHOD__, 'Archive order IDs: ' . implode(', ', $ids)); + if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } @@ -225,10 +226,11 @@ if (class_exists('WC_Retailcrm_Uploader') === false) { return; } - WC_Retailcrm_Logger::error(__METHOD__, 'Errors while uploading these orders'); - foreach ($errors as $orderId => $error) { - WC_Retailcrm_Logger::error(__METHOD__, sprintf("[%d] => %s", $orderId, $error)); + WC_Retailcrm_Logger::error( + __METHOD__, + sprintf("Error while uploading [%d] => %s", $orderId, $error) + ); } } } diff --git a/src/include/components/class-wc-retailcrm-customer-switcher.php b/src/include/components/class-wc-retailcrm-customer-switcher.php index 7d650c8..76cf5ea 100644 --- a/src/include/components/class-wc-retailcrm-customer-switcher.php +++ b/src/include/components/class-wc-retailcrm-customer-switcher.php @@ -43,7 +43,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface { $this->data->validate(); - WC_Retailcrm_Logger::info(__METHOD__, 'State: ' . json_encode($this->data)); + WC_Retailcrm_Logger::info(__METHOD__, 'Customer State: ' . json_encode($this->data)); $newCustomer = $this->data->getNewCustomer(); $newContact = $this->data->getNewContact(); @@ -53,8 +53,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface if (!empty($newCustomer)) { WC_Retailcrm_Logger::info( __METHOD__, - 'Changing to individual customer for order' . - $this->data->getWcOrder()->get_id() + 'Changing to individual customer for order ' . $this->data->getWcOrder()->get_id() ); $this->processChangeToRegular($this->data->getWcOrder(), $newCustomer, false); $this->data->getWcOrder()->set_billing_company(''); @@ -62,8 +61,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface if (!empty($newContact)) { WC_Retailcrm_Logger::info( __METHOD__, - 'Changing to contact person customer for order' . - $this->data->getWcOrder()->get_id() + 'Changing to contact person customer for order ' . $this->data->getWcOrder()->get_id() ); $this->processChangeToRegular($this->data->getWcOrder(), $newContact, true); } @@ -104,7 +102,7 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface WC_Retailcrm_Logger::info( __METHOD__, - sprintf('Switching in order %s to %s', $wcOrder->get_id(), json_encode($newCustomer)) + sprintf('Switching customer in order %s to %s', $wcOrder->get_id(), json_encode($newCustomer)) ); if (isset($newCustomer['externalId'])) { diff --git a/src/include/components/class-wc-retailcrm-logger.php b/src/include/components/class-wc-retailcrm-logger.php index 1c8a9c6..4124220 100644 --- a/src/include/components/class-wc-retailcrm-logger.php +++ b/src/include/components/class-wc-retailcrm-logger.php @@ -19,11 +19,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : /** @var string */ const HANDLE = 'retailcrm'; - const TYPE = array( - 0 => 'REQUEST', - 1 => 'RESPONSE', - 2 => 'EXCEPTION' - ); + const TYPE = [ + 'req' => 'REQUEST', + 'res' => 'RESPONSE', + 'exc' => 'EXCEPTION', + ]; /** * @var \WC_Logger_Interface $instance @@ -64,7 +64,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : * * @return \WC_Logger_Interface */ - private static function getInstance() + private static function getInstance(): WC_Logger_Interface { if (empty(static::$instance)) { static::$instance = new WC_Logger(self::$additionalHandlers); @@ -95,7 +95,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : } } - private static function getIdentifier() + private static function getIdentifier(): string { if (empty(static::$logIdentifier)) { static::$logIdentifier = substr(uniqid('', false), -8); @@ -104,7 +104,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : return static::$logIdentifier; } - private static function getStartTime() + private static function getStartTime(): float { if (empty(static::$startTime)) { static::$startTime = microtime(true); @@ -145,7 +145,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : * @param string|null $type * @param string|null $level */ - private static function log(string $method, string $message, $type = null, $level = null) + private static function log(string $method, string $message, $type = null, $level = 'info') { $time = self::getStartTime(); $context = ['time' => round((microtime(true) - $time), 3), 'source' => self::HANDLE]; @@ -159,7 +159,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : $message ); - self::getInstance()->log($level ?? 'info', $message, $context); + self::getInstance()->log($level, $message, $context); } public static function formatWCObject($object): string @@ -171,20 +171,21 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : 'date_modified' => $object->get_date_modified(), 'total' => $object->get_total(), 'shipping' => [ - "first_name" => $object->get_shipping_first_name(), - "last_name" => $object->get_shipping_last_name(), - "company" => $object->get_shipping_company(), - "address_1" => $object->get_shipping_address_1(), - "address_2" => $object->get_shipping_address_2(), - "city" => $object->get_shipping_city(), - "state" => $object->get_shipping_state(), - "postcode" => $object->get_shipping_postcode(), - "country" => $object->get_shipping_country(), - "phone" => method_exists($object, 'get_shipping_phone') + 'first_name' => $object->get_shipping_first_name(), + 'last_name' => $object->get_shipping_last_name(), + 'company' => $object->get_shipping_company(), + 'address_1' => $object->get_shipping_address_1(), + 'address_2' => $object->get_shipping_address_2(), + 'city' => $object->get_shipping_city(), + 'state' => $object->get_shipping_state(), + 'postcode' => $object->get_shipping_postcode(), + 'country' => $object->get_shipping_country(), + 'phone' => method_exists($object, 'get_shipping_phone') ? $object->get_shipping_phone() : $object->get_billing_phone(), ], 'email' => $object->get_billing_email(), 'payment_method_title' => $object->get_payment_method_title(), + 'date_paid' => $object->get_date_paid(), ]); } @@ -197,16 +198,16 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) : 'role' => $object->get_role(), 'username' => $object->get_username(), 'shipping' => [ - "first_name" => $object->get_shipping_first_name(), - "last_name" => $object->get_shipping_last_name(), - "company" => $object->get_shipping_company(), - "address_1" => $object->get_shipping_address_1(), - "address_2" => $object->get_shipping_address_2(), - "city" => $object->get_shipping_city(), - "state" => $object->get_shipping_state(), - "postcode" => $object->get_shipping_postcode(), - "country" => $object->get_shipping_country(), - "phone" => method_exists($object, 'get_shipping_phone') + 'first_name' => $object->get_shipping_first_name(), + 'last_name' => $object->get_shipping_last_name(), + 'company' => $object->get_shipping_company(), + 'address_1' => $object->get_shipping_address_1(), + 'address_2' => $object->get_shipping_address_2(), + 'city' => $object->get_shipping_city(), + 'state' => $object->get_shipping_state(), + 'postcode' => $object->get_shipping_postcode(), + 'country' => $object->get_shipping_country(), + 'phone' => method_exists($object, 'get_shipping_phone') ? $object->get_shipping_phone() : $object->get_billing_phone(), ], ]); diff --git a/src/include/functions.php b/src/include/functions.php index e7de3ca..cf2d196 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -182,16 +182,6 @@ function calculatePriceExcludingTax($priceIncludingTax, $rate) return round($priceIncludingTax / (1 + $rate / 100), wc_get_price_decimals()); } -/** - * Write base logs in retailcrm file. - * - * @codeCoverageIgnore - */ -function writeBaseLogs($message) -{ - WC_Retailcrm_Logger::info(__METHOD__, $message); -} - /** * Checking the use of HPOS. * diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-result.php b/src/include/models/class-wc-retailcrm-customer-switcher-result.php index 04b2aaf..e01eef6 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-result.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-result.php @@ -66,7 +66,7 @@ class WC_Retailcrm_Customer_Switcher_Result WC_Retailcrm_Logger::info( __METHOD__, sprintf( - 'Saving customer: %s and order: %s', + 'Saving WC_Customer: %s and WC_Order: %s', WC_Retailcrm_Logger::formatWCObject($this->wcCustomer), WC_Retailcrm_Logger::formatWCObject($this->wcOrder) ) diff --git a/src/include/models/class-wc-retailcrm-customer-switcher-state.php b/src/include/models/class-wc-retailcrm-customer-switcher-state.php index e589be0..bee4410 100644 --- a/src/include/models/class-wc-retailcrm-customer-switcher-state.php +++ b/src/include/models/class-wc-retailcrm-customer-switcher-state.php @@ -178,6 +178,7 @@ class WC_Retailcrm_Customer_Switcher_State json_encode($this->getNewContact()) ) ); + throw new \InvalidArgumentException( 'Too much data in state - cannot determine which customer should be used.' ); diff --git a/src/include/order/class-wc-retailcrm-order-payment.php b/src/include/order/class-wc-retailcrm-order-payment.php index 122251a..74fdb2b 100644 --- a/src/include/order/class-wc-retailcrm-order-payment.php +++ b/src/include/order/class-wc-retailcrm-order-payment.php @@ -62,12 +62,11 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data if ($order->is_paid()) { if ($order->get_status() != 'completed' && $order->get_payment_method() == 'cod') { - WC_Retailcrm_Logger::info( - __METHOD__, - 'Payment for order: ' . $order->get_id() . ' ' . + $logMessage = 'Payment for order: ' . $order->get_id() . '. ' . 'Payment status cannot be changed as it is cash (or other payment method) on delivery. ' . 'The status will be changed when the order is in status completed.' - ); + ; + WC_Retailcrm_Logger::info(__METHOD__, $logMessage); } else { $paymentData['status'] = 'paid';