From a8b568773d07dac54ca63fd4bee4cb214c11d1c0 Mon Sep 17 00:00:00 2001 From: JoaoPedro0000 Date: Thu, 21 Mar 2024 11:24:39 -0300 Subject: [PATCH 01/36] feat: add compatibility --- Services/AgenciesService.php | 6 +- Services/CompositeProductBundleService.php | 4 + Services/ListOrderService.php | 2 +- Services/ListPluginsIncompatiblesService.php | 4 +- Services/OrdersProductsService.php | 176 ++++++++++++------- Services/ProductsService.php | 18 +- Services/QuotationService.php | 4 +- 7 files changed, 139 insertions(+), 75 deletions(-) diff --git a/Services/AgenciesService.php b/Services/AgenciesService.php index 4d788598b..272cf9272 100644 --- a/Services/AgenciesService.php +++ b/Services/AgenciesService.php @@ -87,8 +87,12 @@ private function normalize( $data ) { $companyId = null; + if (!isset($agency->companies)){ + continue; + } + foreach ($agency->companies as $company) { - + $companyId = $company->id; if ( empty( $companyId ) ) { diff --git a/Services/CompositeProductBundleService.php b/Services/CompositeProductBundleService.php index c69e1a624..2fbe5dad0 100644 --- a/Services/CompositeProductBundleService.php +++ b/Services/CompositeProductBundleService.php @@ -23,6 +23,10 @@ class CompositeProductBundleService { const PRODUCT_COMPOSITE = 'WC_Product_Composite'; + const PRODUCT_BUNDLE = 'WC_Product_Woosb'; + + const PRODUCT_SIMPLE = 'WC_Product_Simple'; + const PRODUCT_COMBO_OFFICER = 'WC_Product_Wooco'; const PRODUCT_COMPOSITE_SHIPPING_FEE = 'wooco_shipping_fee'; diff --git a/Services/ListOrderService.php b/Services/ListOrderService.php index 55674b95b..8eded9e89 100644 --- a/Services/ListOrderService.php +++ b/Services/ListOrderService.php @@ -85,7 +85,7 @@ private function getData( $posts ) { 'non_commercial' => is_null( $invoice['number'] ) || is_null( $invoice['key'] ), 'invoice' => $invoice, 'products' => $products, - 'quotation' => $quotationService->calculateQuotationByPostId( $postId ), + 'quotation' => $quotationService->calculateQuotationByPostId( $postId, $products), 'link' => admin_url() . sprintf( 'post.php?post=%d&action=edit', $postId ), ); } diff --git a/Services/ListPluginsIncompatiblesService.php b/Services/ListPluginsIncompatiblesService.php index e7c145fb4..413970402 100644 --- a/Services/ListPluginsIncompatiblesService.php +++ b/Services/ListPluginsIncompatiblesService.php @@ -46,8 +46,6 @@ public function getListPluginsInstalled() { * @return array */ public function getListPluginsIncompatibles() { - return array( - 'wpc-composite-products/wpc-composite-products.php', - ); + return array(); } } diff --git a/Services/OrdersProductsService.php b/Services/OrdersProductsService.php index cfdd1c103..10f3c48ac 100644 --- a/Services/OrdersProductsService.php +++ b/Services/OrdersProductsService.php @@ -17,52 +17,67 @@ class OrdersProductsService { public function getProductsOrder( $orderId ) { $order = wc_get_order( $orderId ); - $products = array(); + $orderProducts = $order->get_items(); - $productsComposite = array(); + $productsComposite = $this->getProductsComposite($orderProducts); + + $productsBundle = $this->getProductsBundle($orderProducts); - $productService = new ProductsService(); + $productsSimple = $this->getProductsSimple($orderProducts); - $products = array(); + $productsSimple = $this->removeComponentsByComposite($productsComposite, $productsSimple); - $quantities = array(); + $productsSimple = $this->removeComponentsByBundle($productsBundle, $productsSimple); - $productsIgnoreBundle = array(); + return array_merge($productsComposite, $productsBundle, $productsSimple); + } - $wooCommerceBundleProductService = new WooCommerceBundleProductsService(); + public function removeComponentsByComposite($productsComposite, $productsSimple) + { + foreach ($productsComposite as $productComposite) { + $components = wc_get_product($productComposite['id'])->get_components(); + + $componentsQuantity = array_map(function($component) use ($productComposite) { + return array_map(function($idProduct) use ($component, $productComposite) { + return ["id" => $idProduct, "quantity" => ($component['qty'] * $productComposite['quantity'])]; + }, $component['products']); + }, $components); + + foreach ($componentsQuantity as $componentQuantity) { + foreach ($componentQuantity as $key => $product) { + $productsSimple = $this->removeProduct($product, $productsSimple, "composite"); + $componentQuantity[$key]['quantity']--; + } + } + } - foreach ( $order->get_items() as $key => $itemProduct ) { + return $productsSimple; + } - $metas = $wooCommerceBundleProductService->getMetas( $itemProduct ); + public function removeComponentsByBundle($productsBundle, $productsSimple) + { + foreach ($productsBundle as $productBundle) { + $components = wc_get_product($productBundle['id'])->get_items(); - if ( $wooCommerceBundleProductService->isBundledItem( $metas ) ) { - $bundleType = $wooCommerceBundleProductService->getBundledItemType( $metas ); - if ( $bundleType == WooCommerceBundleProductsService::BUNDLE_TYPE_INTERNAL ) { - $products = $wooCommerceBundleProductService->getInternalProducts( - $itemProduct->get_data(), - $metas, - $products - ); - continue; - } + $componentsQuantity = array_map(function($component) use ($productBundle) { + return ["id" => $component['id'], "quantity" => ($component['qty'] * $productBundle['quantity'])]; + }, $components); - if ( $bundleType == WooCommerceBundleProductsService::BUNDLE_TYPE_EXTERNAL ) { - $productsInBundle = $wooCommerceBundleProductService->getProducts( $metas['_stamp'] ); - foreach ( $productsInBundle as $prod ) { - $productsIgnoreBundle[] = $prod->id; - } - $product = $wooCommerceBundleProductService->getExternalProducts( - $itemProduct->get_data(), - $metas - ); - $quantities = $this->incrementQuantity( $product->id, $quantities, $product->quantity ); - $products[ $product->id ] = $product; - continue; - } + foreach ($componentsQuantity as $componentQuantity) { + $productsSimple = $this->removeProduct($componentQuantity, $productsSimple, "bundle"); + $componentQuantity['quantity']--; } + } + return $productsSimple; + } + + public function getProductsComposite($orderProducts): array + { + $productsComposite = array(); + foreach ($orderProducts as $key => $itemProduct) { $product = $itemProduct->get_product(); - if ($this->isComboProduct($product)) { + if ($this->isCompositeProduct($product)) { $compositeBundleService = new CompositeProductBundleService( $itemProduct ); $productComposite = $compositeBundleService->getProductNormalize(); if ( empty( $productComposite ) ) { @@ -70,59 +85,65 @@ public function getProductsOrder( $orderId ) { } $productsComposite[ $key ] = $productComposite; } + } - if ( ! in_array( $product->get_id(), $productsIgnoreBundle ) ) { - $productId = $product->get_id(); - $quantity = $itemProduct->get_quantity(); - - $products[ $productId ] = $productService->normalize( - $product, - $itemProduct->get_quantity() - ); + return $productsComposite; + } - $quantityInsiderItem = 1; - if (isset($itemProduct->get_data()['quantity'])) { - $quantityInsiderItem = $itemProduct->get_data()['quantity']; - } - - $price = (float) $itemProduct->get_data()['total'] / $quantityInsiderItem; - if ($price == 0) { + public function getProductsBundle($orderProducts): array + { + $productsBundle = array(); + foreach ($orderProducts as $key => $itemProduct) { + $product = $itemProduct->get_product(); + if ($this->isBundleProduct($product)) { + $compositeBundleService = new CompositeProductBundleService( $itemProduct ); + $productBundle = $compositeBundleService->getProductNormalize(); + if ( empty( $productBundle ) ) { continue; } - - $products[$productId]->insurance_value = $price; - $products[$productId]->unitary_value = $price; - $quantities = $this->incrementQuantity( - $productId, - $quantities, - $quantity - ); + $productsBundle[ $key ] = $productBundle; } } - if ( isset( $compositeBundleService ) ) { - return $compositeBundleService->selectProductsToReturnByTypeComposite( - $productsComposite, - $products - ); - } + return $productsBundle; + } - foreach ( $products as $key => $product ) { - if ( ! empty( $quantities[ $product->id ] ) ) { - $products[ $key ]->quantity = $quantities[ $product->id ]; + public function getProductsSimple($orderProducts): array + { + $productsSimple = array(); + foreach ($orderProducts as $key => $itemProduct) { + $product = $itemProduct->get_product(); + if ($this->isSimpleProduct($product)) { + $productService = new ProductsService(); + $productsSimple[ $key ] = $productService->normalize( + $product, + $itemProduct->get_quantity() + );; } } - return $products; + return $productsSimple; } - public function isComboProduct($product) + public function isCompositeProduct($product): bool { return is_bool($product) || get_class($product) === CompositeProductBundleService::PRODUCT_COMPOSITE || get_class($product) === CompositeProductBundleService::PRODUCT_COMBO_OFFICER; } + public function isBundleProduct($product): bool + { + return is_bool($product) || + get_class($product) === CompositeProductBundleService::PRODUCT_BUNDLE; + } + + public function isSimpleProduct($product): bool + { + return is_bool($product) || + get_class($product) === CompositeProductBundleService::PRODUCT_SIMPLE; + } + /** * @param int $productId * @param array $quantities @@ -136,4 +157,25 @@ public function incrementQuantity( $productId, $quantities, $quantity ) { return $quantities; } + + public function removeProduct($product, $productsSimple, $type) + { + foreach ($productsSimple as $key => $productSimple) { + if ($productSimple->id == $product['id'] && $product['quantity'] > 0) { + + if($productSimple->quantity > $product['quantity']) { + $productsSimple[$key]->quantity -= $product['quantity']; + $product['quantity']--; + continue; + }else if ($productSimple->quantity < $product['quantity']) { + continue; + } + + $product['quantity'] = 0; + unset($productsSimple[$key]); + } + } + + return $productsSimple; + } } diff --git a/Services/ProductsService.php b/Services/ProductsService.php index fb9ebbd67..3f6751d6e 100644 --- a/Services/ProductsService.php +++ b/Services/ProductsService.php @@ -91,6 +91,11 @@ public function filter( $data ) { continue; } + if ( ! empty( $item['name'] ) && ! empty( $item['id'] ) ) { + $products[] = (object) $item; + continue; + } + $product = $item['data']; $products[] = $this->normalize( $product, $item['quantity'] ); } @@ -118,7 +123,18 @@ private function isObjectProduct( $item ) { * @return object */ public function normalize( $product, $quantity = 1 ) { - $price = floatval( $product->get_price() ); + if ($product instanceof \WC_Product_Simple) { + $price = floatval( $product->get_price() ); + } + + if ($product instanceof \WC_Product_Composite) { + $price = floatval( $product->get_price() ); + } + + if ($product instanceof \WC_Product_Woosb) { + $price = floatval( $product->get_price() ); + } + if ( empty( $price ) ) { $data = $product->get_data(); if ( isset( $data['price'] ) ) { diff --git a/Services/QuotationService.php b/Services/QuotationService.php index 1ea4ed5d9..e138ff688 100644 --- a/Services/QuotationService.php +++ b/Services/QuotationService.php @@ -76,10 +76,10 @@ private function setKeyQuotationAsServiceid( $quotations ) { * Function to calculate a quotation by post_id. * * @param int $postId + * @param array $products * @return array $quotation */ - public function calculateQuotationByPostId( $postId ) { - $products = ( new OrdersProductsService() )->getProductsOrder( $postId ); + public function calculateQuotationByPostId( $postId, $products) { $buyer = ( new BuyerService() )->getDataBuyerByOrderId( $postId ); $payload = ( new PayloadService() )->createPayloadByProducts( $buyer->postal_code, From b6d6893f7a0fdacb59d1b9994d6df2a4170e543a Mon Sep 17 00:00:00 2001 From: JoaoPedro0000 Date: Thu, 21 Mar 2024 11:24:59 -0300 Subject: [PATCH 02/36] feat: set new version --- Models/Version.php | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Models/Version.php b/Models/Version.php index 4bea6bd1b..102d137e3 100755 --- a/Models/Version.php +++ b/Models/Version.php @@ -4,5 +4,5 @@ class Version { - const VERSION = '2.15.0'; + const VERSION = '2.15.1'; } diff --git a/readme.md b/readme.md index eb2395b1b..8ca3397de 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ === Melhor Envio === -Version: 2.15.0 +Version: 2.15.1 Tags: frete, fretes, cotação, cotações, correios, envio, jadlog, latam latam cargo, azul, azul cargo express, melhor envio Requires at least: 4.7 Tested up to: 6.4 From b59fc8f74993ec25ba83f74afb0b40f8ce3e222d Mon Sep 17 00:00:00 2001 From: JoaoPedro0000 Date: Thu, 21 Mar 2024 11:35:20 -0300 Subject: [PATCH 03/36] fix: table orders --- assets/src/admin/components/Pedido/Cotacao.vue | 1 + assets/src/admin/components/Pedidos.vue | 14 ++++++-------- assets/stylus/layout/tables.styl | 9 +++------ 3 files changed, 10 insertions(+), 14 deletions(-) mode change 100755 => 100644 assets/src/admin/components/Pedidos.vue diff --git a/assets/src/admin/components/Pedido/Cotacao.vue b/assets/src/admin/components/Pedido/Cotacao.vue index 1829e9b6b..0788c9974 100755 --- a/assets/src/admin/components/Pedido/Cotacao.vue +++ b/assets/src/admin/components/Pedido/Cotacao.vue @@ -58,6 +58,7 @@ data-cy="input-quotation" v-if="!(item.status == 'paid' || item.status == 'printed' || item.status == 'generated')" v-model="item.quotation.choose_method" + style="width: 100%" >