diff --git a/retailcrm/lib/RetailcrmCatalog.php b/retailcrm/lib/RetailcrmCatalog.php index 3656a61..f812fb6 100644 --- a/retailcrm/lib/RetailcrmCatalog.php +++ b/retailcrm/lib/RetailcrmCatalog.php @@ -221,7 +221,11 @@ class RetailcrmCatalog $offerCombination = new Combination($offer['id_product_attribute']); - $offerPrice = $offerCombination->price + $price; + $offerCombinationPrice = !empty($product['rate']) + ? round($offerCombination->price, 2) + (round($offerCombination->price, 2) * $product['rate'] / 100) + : round($offerCombination->price, 2); + + $offerPrice = round($offerCombinationPrice, 2) + $price; $offerPrice = $offerPrice > 0 ? $offerPrice : $price; if ($offerCombination->wholesale_price > 0) { diff --git a/retailcrm/lib/RetailcrmCli.php b/retailcrm/lib/RetailcrmCli.php index 5e4b186..292b4c8 100644 --- a/retailcrm/lib/RetailcrmCli.php +++ b/retailcrm/lib/RetailcrmCli.php @@ -156,7 +156,12 @@ class RetailcrmCli $result ? 'true' : 'false' )); } catch (\Exception $exception) { - $this->printStack($exception); + if ($exception instanceof RetailcrmJobManagerException && !empty($exception->getPrevious())) { + $this->printStack($exception->getPrevious()); + } else { + $this->printStack($exception); + } + self::clearCurrentJob($jobName); } finally { if (isset($result) && $result) { @@ -169,10 +174,11 @@ class RetailcrmCli * Prints error details * * @param \Exception $exception + * @param string $header */ - private function printStack($exception) + private function printStack($exception, $header = 'Error while executing a job: ') { - RetailcrmLogger::output(sprintf('Error while executing a job: %s', $exception->getMessage())); + RetailcrmLogger::output(sprintf('%s%s', $header, $exception->getMessage())); RetailcrmLogger::output(sprintf('%s:%d', $exception->getFile(), $exception->getLine())); RetailcrmLogger::output(); RetailcrmLogger::output($exception->getTraceAsString()); diff --git a/retailcrm/lib/RetailcrmJobManager.php b/retailcrm/lib/RetailcrmJobManager.php index 3c82a1d..7aea05b 100644 --- a/retailcrm/lib/RetailcrmJobManager.php +++ b/retailcrm/lib/RetailcrmJobManager.php @@ -172,12 +172,22 @@ class RetailcrmJobManager $lastRuns[$job] = new \DateTime('now'); } } catch (\Exception $exception) { - static::handleError( - $exception->getFile(), - $exception->getMessage(), - $exception->getTraceAsString(), - $job - ); + if ($exception instanceof RetailcrmJobManagerException && !empty($exception->getPrevious())) { + static::handleError( + $exception->getPrevious()->getFile(), + $exception->getPrevious()->getMessage(), + $exception->getPrevious()->getTraceAsString(), + $job + ); + } else { + static::handleError( + $exception->getFile(), + $exception->getMessage(), + $exception->getTraceAsString(), + $job + ); + } + self::clearCurrentJob($job); } finally { if (isset($result) && $result) { @@ -286,7 +296,7 @@ class RetailcrmJobManager } catch (\RetailcrmJobManagerException $exception) { throw $exception; } catch (\Exception $exception) { - throw new RetailcrmJobManagerException($exception->getMessage(), $jobFile); + throw new RetailcrmJobManagerException($exception->getMessage(), $jobFile, array(), 0, $exception); } } @@ -444,7 +454,10 @@ class RetailcrmJobManager RetailcrmLogger::writeNoCaller(sprintf('%s: %s (%s)', $file, $msg, implode(', ', $data))); RetailcrmLogger::writeNoCaller($trace); - RetailcrmTools::http_response_code(500); + + if (PHP_SAPI != 'cli' && !headers_sent()) { + RetailcrmTools::http_response_code(500); + } } /** diff --git a/tests/RetailcrmCatalogTest.php b/tests/RetailcrmCatalogTest.php index 8773fd6..3ceb442 100644 --- a/tests/RetailcrmCatalogTest.php +++ b/tests/RetailcrmCatalogTest.php @@ -42,6 +42,51 @@ class RetailcrmCatalogTest extends RetailcrmTestCase } } + public function testIsPricesWithTax() + { + $products = $this->data[1]; + $productsPresta = array(); + $productsPrestaList = Product::getProducts( + (int) Configuration::get('PS_LANG_DEFAULT'), + 0, + 0, + 'name', + 'asc' + ); + + foreach ($productsPrestaList as $productData) { + $productsPresta[$productData['id_product']] = $productData; + } + + unset($productsPrestaList); + + foreach ($products as $product) { + $this->assertArrayHasKey('productId', $product); + $this->assertArrayHasKey('price', $product); + + $prestaProduct = $productsPresta[$product['productId']]; + $price = !empty($prestaProduct['rate']) + ? round($prestaProduct['price'], 2) + (round($prestaProduct['price'], 2) * $prestaProduct['rate'] / 100) + : round($prestaProduct['price'], 2); + + if (strpos($product['id'], '#') !== false) { + $offerId = explode('#', $product['id']); + $offerId = $offerId[1]; + $offerCombination = new Combination($offerId); + + $offerCombinationPrice = !empty($prestaProduct['rate']) + ? round($offerCombination->price, 2) + (round($offerCombination->price, 2) * $prestaProduct['rate'] / 100) + : round($offerCombination->price, 2); + $offerPrice = round($offerCombinationPrice, 2) + $price; + $offerPrice = $offerPrice > 0 ? $offerPrice : $price; + + $this->assertEquals(round($offerPrice, 2), round($product['price'], 2)); + } else { + $this->assertEquals(round($price, 2), round($product['price'], 2)); + } + } + } + public function testIcmlGenerate() { $icml = new RetailcrmIcml(Configuration::get('PS_SHOP_NAME'), _PS_ROOT_DIR_ . '/retailcrm.xml');