| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\elements\SingleProduct\PriceIncreaseField; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 7 |
use SyncBasalam\Services\Products\FetchCommission; |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
class PriceService |
| 12 |
{ |
| 13 |
public function calculateFinalPrice($product): ?int |
| 14 |
{ |
| 15 |
$price = $this->getBasePrice($product); |
| 16 |
if (!$price) return null; |
| 17 |
|
| 18 |
$categoryIds = $this->getCategoryIds($product); |
| 19 |
return $this->applyPriceCalculations($price, $categoryIds, $product); |
| 20 |
} |
| 21 |
|
| 22 |
private function getBasePrice($product) |
| 23 |
{ |
| 24 |
$priceField = syncBasalamSettings()->getSettings(SettingsConfig::PRODUCT_PRICE_FIELD); |
| 25 |
$regularPrice = $product->get_regular_price(); |
| 26 |
$salePrice = $product->get_sale_price(); |
| 27 |
|
| 28 |
if ($priceField === 'original_price' || $priceField === 'sale_strikethrough_price') { |
| 29 |
return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null; |
| 30 |
} |
| 31 |
|
| 32 |
if ($priceField === 'sale_price') { |
| 33 |
if ($salePrice && is_numeric($salePrice)) return floatval($salePrice); |
| 34 |
return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null; |
| 35 |
} |
| 36 |
|
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
private function applyPriceCalculations(float $price, array $categoryIds, $product): ?int |
| 41 |
{ |
| 42 |
$increaseValue = $this->getIncreaseValue($product); |
| 43 |
$roundMode = syncBasalamSettings()->getSettings(SettingsConfig::ROUND_PRICE); |
| 44 |
$currency = get_woocommerce_currency(); |
| 45 |
|
| 46 |
$finalPrice = $this->convertToRial($price, $currency); |
| 47 |
if (!$finalPrice) return null; |
| 48 |
|
| 49 |
if ($increaseValue) $finalPrice = $this->applyIncrease($finalPrice, $increaseValue, $categoryIds); |
| 50 |
if ($roundMode && $roundMode != 'none') $finalPrice = $this->applyRounding($finalPrice, $roundMode); |
| 51 |
|
| 52 |
if ($finalPrice < 1000) return null; |
| 53 |
|
| 54 |
return intval($finalPrice); |
| 55 |
} |
| 56 |
|
| 57 |
private function getIncreaseValue($product): int |
| 58 |
{ |
| 59 |
$productId = method_exists($product, 'get_id') ? intval($product->get_id()) : 0; |
| 60 |
|
| 61 |
if ($productId > 0) { |
| 62 |
$productIncrease = $this->getProductIncreaseMeta($productId); |
| 63 |
|
| 64 |
if ($productIncrease !== '' && is_numeric($productIncrease)) { |
| 65 |
return intval($productIncrease); |
| 66 |
} |
| 67 |
|
| 68 |
if (method_exists($product, 'get_parent_id')) { |
| 69 |
$parentId = intval($product->get_parent_id()); |
| 70 |
$parentIncrease = $parentId > 0 ? $this->getProductIncreaseMeta($parentId) : ''; |
| 71 |
|
| 72 |
if ($parentIncrease !== '' && is_numeric($parentIncrease)) { |
| 73 |
return intval($parentIncrease); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return intval(syncBasalamSettings()->getSettings(SettingsConfig::INCREASE_PRICE_VALUE)); |
| 79 |
} |
| 80 |
|
| 81 |
private function getProductIncreaseMeta(int $productId): string |
| 82 |
{ |
| 83 |
return (string) get_post_meta($productId, PriceIncreaseField::META_KEY, true); |
| 84 |
} |
| 85 |
|
| 86 |
private function convertToRial(float $price, string $currency) |
| 87 |
{ |
| 88 |
if ($currency === 'IRT') return $price * 10; |
| 89 |
if ($currency === 'IRHT') return $price * 10000; |
| 90 |
if ($currency === 'IRHR') return $price * 1000; |
| 91 |
|
| 92 |
return $price; |
| 93 |
} |
| 94 |
|
| 95 |
private function applyIncrease(float $price, int $increaseValue, array $categoryIds): float |
| 96 |
{ |
| 97 |
if ($increaseValue === -1) return $this->applyCommissionCalculation($price, $categoryIds); |
| 98 |
|
| 99 |
if ($increaseValue <= 100) return $price + ($price * ($increaseValue / 100)); |
| 100 |
|
| 101 |
return $price + ($increaseValue * 10); |
| 102 |
} |
| 103 |
|
| 104 |
private function applyCommissionCalculation(float $price, array $categoryIds): float |
| 105 |
{ |
| 106 |
$categoryPercent = FetchCommission::fetchCategoryCommission($categoryIds); |
| 107 |
|
| 108 |
if ($categoryPercent > 0 && $categoryPercent < 100) return $price / (1 - ($categoryPercent / 100)); |
| 109 |
else return $price; |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
private function applyRounding(float $price, $mode): float |
| 114 |
{ |
| 115 |
if ($mode === 'up') return ceil($price / 10000) * 10000; |
| 116 |
|
| 117 |
if ($mode === 'down') return floor($price / 10000) * 10000; |
| 118 |
|
| 119 |
return $price; |
| 120 |
} |
| 121 |
|
| 122 |
private function getCategoryIds($product): array |
| 123 |
{ |
| 124 |
$categoryService = new CategoryService(); |
| 125 |
return $categoryService->getCategoryIds($product); |
| 126 |
} |
| 127 |
} |
| 128 |
|