baby_in_magento

vdv

Oct 10th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 KB | None | 0 0
  1. <?php
  2. namespace Froogal\Catalog\Model;
  3.  
  4. class ProductPriceCalculator extends \Magento\Framework\Model\AbstractModel
  5. {
  6.  
  7. public $productRelation;
  8.  
  9. public $productResourceModel;
  10.  
  11. public $storeManager;
  12.  
  13. protected $taxData;
  14. public function __construct(
  15. \Magento\Store\Model\StoreManagerInterface $storeManager,
  16. \Froogal\Catalog\Model\ProductRelation $productRelation,
  17. \Magento\Catalog\Model\ResourceModel\Product $productResourceModel,
  18. \Froogal\Catalog\Helper\TaxData $taxData,
  19. \Froogal\Catalog\Api\ProductSolitaireInterface $productSolitaireInterface,
  20. \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
  21. \Froogal\Catalog\Model\ProductBreakDown $productBreakDown
  22.  
  23. )
  24. {
  25. $this->storeManager = $storeManager;
  26. //parent::__construct($context, $data);
  27.  
  28. $this->productRelation = $productRelation;
  29. $this->productResourceModel = $productResourceModel;
  30. $this->taxData = $taxData;
  31. $this->productSolitaireInterface = $productSolitaireInterface;
  32. $this->productRepositoryInterface = $productRepositoryInterface;
  33. $this->productBreakDown = $productBreakDown;
  34.  
  35. }
  36.  
  37. public function getStoreId()
  38. {
  39. return $this->storeManager->getStore()->getId();
  40. }
  41.  
  42. public function getCalculateDynamicPricingOnSave($productId)
  43. {
  44. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
  45. $logger = new \Zend_Log();
  46. $logger->addWriter($writer);
  47.  
  48. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  49. $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
  50. $storeId = 0;
  51. $finalPrice = $this->getFinalPrice($productId);
  52. if($finalPrice)
  53. {
  54. $logger->info('productId='.$productId. 'total price' . $finalPrice);
  55. $updateAttributes['price'] = $finalPrice;
  56. $action->updateAttributes([$productId], $updateAttributes, $storeId);
  57. }
  58. // return true;
  59. }
  60.  
  61. public function getFinalPrice($productId,$diamondId=null)
  62. {
  63. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
  64. $logger = new \Zend_Log();
  65. $logger->addWriter($writer);
  66. try {
  67. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  68.  
  69. // $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  70. $mainProduct = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
  71.  
  72. $variantId = $mainProduct->getData('display_variant') ?? null;
  73. $logger->info('productId='.$productId.'variantId='.$variantId);
  74. $taxPercent = $this->taxData->getTaxPercent($mainProduct);
  75. $storeId = 0;
  76. $totalPrice = 0;
  77. $makingChargesValue = 0;
  78. $makingChargesPrice = 0;
  79. $qty = 0;
  80. $newqty = 0;
  81. $gemStoneQty = 0;
  82. $newtotalPrice = 0;
  83. $gemStonePrice = 0;
  84. $goldPrice = 0;
  85. $finalPrice = 0;
  86. $diamondPrice = 0;
  87. $makingChargesType = $mainProduct->getResource()->getAttribute('making_charges_type')->getFrontend()->getValue($mainProduct);
  88. $makingChargesValue = $mainProduct->getData('making_charges_value');
  89. $gemStonePrice = $mainProduct->getData('gemstone_price') ?? 0;
  90. $totalPrice = $totalPrice + $gemStonePrice;
  91. $productConstituentsData = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->addFieldToFilter('variant_id', array('like' => "%$variantId%"))->getData();
  92.  
  93.  
  94. if ($productConstituentsData) {
  95. foreach ($productConstituentsData as $productRelation) {
  96. $masterProductId = $productRelation['contituent_product_id'];
  97. $product = $objectManager->create('Magento\Catalog\Model\Product')->load($masterProductId);
  98. $carat = $productRelation['carat'] ?? 1;
  99. $qty = (float) $productRelation['qty'] * $carat;
  100. $price = $product->getPrice();
  101.  
  102. $constituentType = $product->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($product);
  103. if($constituentType == 'Gold')
  104. {
  105. $goldPrice = $product->getPrice();
  106. }
  107. if($constituentType == 'Diamond' && $diamondId)
  108. {
  109. $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
  110. $solitaireDetails = $response['response']['body'];
  111. if(!empty($solitaireDetails))
  112. {
  113. $diamondPrice = $response['response']['body']['diamond']['total_sales_price_in_currency'];
  114.  
  115. }
  116. $price = 0;
  117. }
  118. if ($constituentType == 'Gold' || $constituentType == 'Platinum') {
  119. $newqty += $productRelation['qty'] * $carat;
  120. $newtotalPrice = $price * $newqty;
  121. $logger->info('new total price'.$newtotalPrice);
  122. }
  123. if ($constituentType == 'Gemstone') {
  124. $gemStoneQty += $productRelation['qty'];
  125. $gemStonePrice = $price * $gemStoneQty;
  126. }
  127.  
  128. if ($qty > 0) {
  129. $totalPrice += $price * $qty;
  130. }
  131. $logger->info('totalPrice1'.$totalPrice);
  132.  
  133. }
  134. }
  135. $totalPrice += $diamondPrice;
  136. $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
  137. $updateAttributes['constituent_final_price'] = $totalPrice;
  138. $action->updateAttributes([$productId], $updateAttributes, $storeId);
  139. $logger->info('productId=' . $productId . 'constituent Price=' . $totalPrice);
  140.  
  141. //calculate custom option price
  142.  
  143. if ($makingChargesType == 'Percentage') {
  144. $makingChargesPrice = ($newtotalPrice * $makingChargesValue) / 100;
  145. } else if ($makingChargesType == 'Fixed') {
  146. $makingChargesPrice = $makingChargesValue;
  147. } else if ($makingChargesType == 'Slab') {
  148. $makingChargesPrice = $makingChargesValue * $newqty;
  149. }
  150. //discount price
  151. $date = date('Y-m-d');
  152. $websiteId = 1;
  153. $customerGroupId = null;
  154. $makingChargesDiscountValue = 0;
  155. $rules = $this->productBreakDown->getRules($date, $websiteId, $customerGroupId, $productId);
  156. $makingDiscountRule = in_array('custom_mak',array_column($rules, 'action_operator'));
  157. if($makingDiscountRule)
  158. {
  159. $action_amount = $rules[0]['action_amount'] ?? 0;
  160. //$makingChargesValue = $mainProduct->getData('making_charges_value');
  161. $makingChargesDiscountValue = ($makingChargesPrice * (($action_amount) / 100));
  162. //$totalPrice = $totalPrice - $makingChargesDiscountValue;
  163. }
  164.  
  165.  
  166. $taxAmount = ($totalPrice + $makingChargesPrice) * $taxPercent / 100;
  167. $logger->info('totalPrice'.$totalPrice.'makingChargesPrice'.$makingChargesPrice.'taxAmount'.$taxAmount);
  168. $somePercentageIncreaseInPrice = (($totalPrice + $makingChargesPrice) * 110) / 100;
  169. $finalPrice = (ceil((float) ($somePercentageIncreaseInPrice + $taxAmount ) / 100)) * 100;
  170. $logger->info('finalPrice'.$finalPrice);
  171. $logger->info('storeId'.$storeId);
  172.  
  173. // $this->updateProductPrice($finalPrice,$productId,$storeId);
  174.  
  175. // $product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
  176. // $logger->info('productId=' . $product->getId() . 'finalPrice=' . $product->getPrice() . 'productPrice=' . $totalPrice . 'making=' . $makingChargesPrice . 'tax=' . $taxAmount);
  177.  
  178. if ($goldPrice > 0) {
  179. $this->calculateAndStoreOptionPricing($mainProduct, $goldPrice, $finalPrice);
  180. }
  181. return $finalPrice;
  182.  
  183. } catch (\Exception $e) {
  184. $finalPrice = 0;
  185. $logger->info('error' . json_encode($e->getMessage()));
  186. return $finalPrice;
  187. }
  188.  
  189.  
  190. //return $finalPrice;
  191. }
  192.  
  193. public function calculateAndStoreOptionPricing($product, $goldPrice, $finalPrice)
  194. {
  195. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
  196. $logger = new \Zend_Log();
  197. $logger->addWriter($writer);
  198.  
  199. $productId = $product->getId();
  200. try {
  201. $productRingSize = $product->getResource()->getAttribute('ring_size')->getFrontend()->getValue($product) ?? 0;
  202.  
  203. if (count($product->getOptions()) > 0) {
  204.  
  205. foreach ($product->getOptions() as $options) {
  206. $optionTitle = strtolower($options->getTitle());
  207. if ($optionTitle == 'ring size') {
  208. $optionData = $options->getValues();
  209. $i = 0;
  210. foreach ($optionData as $data) {
  211.  
  212. $size = $data->getTitle();
  213. if ($size > $productRingSize) {
  214. $i = $i + 0.1;
  215. $extraPrice = $goldPrice * $i;
  216. $logger->info('extraprice='.$extraPrice.'goldprice='.$goldPrice.'i='.$i);
  217. $finalPriceWithOptions = (round(($finalPrice + $extraPrice) / 100)) * 100;
  218. $customOptionPrice = $finalPriceWithOptions - $finalPrice;
  219. $data['price'] = $customOptionPrice;
  220. $product->setProductOptions($data);
  221. $product->setCanSaveCustomOptions(true);
  222. // $product->save();
  223. $logger->info('productId=' .$productId. 'size=' . $size . 'Price=' . $data['price']);
  224. }
  225.  
  226. }
  227. }
  228. }
  229. $product->save();
  230. }
  231. return true;
  232. } catch (\Exception $e) {
  233. $logger->info('error1' . json_encode($e->getMessage()));
  234. }
  235.  
  236. }
  237.  
  238. public function updateProductPrice($finalPrice,$productId,$storeId)
  239. {
  240. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
  241. $logger = new \Zend_Log();
  242. $logger->addWriter($writer);
  243. $logger->info('update product');
  244. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  245. $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
  246. $updateAttributes['price'] = $finalPrice;
  247. $logger->info('productId=' . $productId . 'finalPrice=' . $finalPrice);
  248.  
  249. $action->updateAttributes([$productId], $updateAttributes, $storeId);
  250. $product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
  251. $logger->info('productId=' . $product->getId() . 'finalPrice=' . $product->getPrice());
  252. }
  253.  
  254.  
  255.  
  256. }
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment