Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Froogal\Catalog\Model;
- class ProductPriceCalculator extends \Magento\Framework\Model\AbstractModel
- {
- public $productRelation;
- public $productResourceModel;
- public $storeManager;
- protected $taxData;
- public function __construct(
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Froogal\Catalog\Model\ProductRelation $productRelation,
- \Magento\Catalog\Model\ResourceModel\Product $productResourceModel,
- \Froogal\Catalog\Helper\TaxData $taxData,
- \Froogal\Catalog\Api\ProductSolitaireInterface $productSolitaireInterface,
- \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
- \Froogal\Catalog\Model\ProductBreakDown $productBreakDown
- )
- {
- $this->storeManager = $storeManager;
- //parent::__construct($context, $data);
- $this->productRelation = $productRelation;
- $this->productResourceModel = $productResourceModel;
- $this->taxData = $taxData;
- $this->productSolitaireInterface = $productSolitaireInterface;
- $this->productRepositoryInterface = $productRepositoryInterface;
- $this->productBreakDown = $productBreakDown;
- }
- public function getStoreId()
- {
- return $this->storeManager->getStore()->getId();
- }
- public function getCalculateDynamicPricingOnSave($productId)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
- $storeId = 0;
- $finalPrice = $this->getFinalPrice($productId);
- if($finalPrice)
- {
- $logger->info('productId='.$productId. 'total price' . $finalPrice);
- $updateAttributes['price'] = $finalPrice;
- $action->updateAttributes([$productId], $updateAttributes, $storeId);
- }
- // return true;
- }
- public function getFinalPrice($productId,$diamondId=null)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- try {
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- // $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
- $mainProduct = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
- $variantId = $mainProduct->getData('display_variant') ?? null;
- $logger->info('productId='.$productId.'variantId='.$variantId);
- $taxPercent = $this->taxData->getTaxPercent($mainProduct);
- $storeId = 0;
- $totalPrice = 0;
- $makingChargesValue = 0;
- $makingChargesPrice = 0;
- $qty = 0;
- $newqty = 0;
- $gemStoneQty = 0;
- $newtotalPrice = 0;
- $gemStonePrice = 0;
- $goldPrice = 0;
- $finalPrice = 0;
- $diamondPrice = 0;
- $makingChargesType = $mainProduct->getResource()->getAttribute('making_charges_type')->getFrontend()->getValue($mainProduct);
- $makingChargesValue = $mainProduct->getData('making_charges_value');
- $gemStonePrice = $mainProduct->getData('gemstone_price') ?? 0;
- $totalPrice = $totalPrice + $gemStonePrice;
- $productConstituentsData = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->addFieldToFilter('variant_id', array('like' => "%$variantId%"))->getData();
- if ($productConstituentsData) {
- foreach ($productConstituentsData as $productRelation) {
- $masterProductId = $productRelation['contituent_product_id'];
- $product = $objectManager->create('Magento\Catalog\Model\Product')->load($masterProductId);
- $carat = $productRelation['carat'] ?? 1;
- $qty = (float) $productRelation['qty'] * $carat;
- $price = $product->getPrice();
- $constituentType = $product->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($product);
- if($constituentType == 'Gold')
- {
- $goldPrice = $product->getPrice();
- }
- if($constituentType == 'Diamond' && $diamondId)
- {
- $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
- $solitaireDetails = $response['response']['body'];
- if(!empty($solitaireDetails))
- {
- $diamondPrice = $response['response']['body']['diamond']['total_sales_price_in_currency'];
- }
- $price = 0;
- }
- if ($constituentType == 'Gold' || $constituentType == 'Platinum') {
- $newqty += $productRelation['qty'] * $carat;
- $newtotalPrice = $price * $newqty;
- $logger->info('new total price'.$newtotalPrice);
- }
- if ($constituentType == 'Gemstone') {
- $gemStoneQty += $productRelation['qty'];
- $gemStonePrice = $price * $gemStoneQty;
- }
- if ($qty > 0) {
- $totalPrice += $price * $qty;
- }
- $logger->info('totalPrice1'.$totalPrice);
- }
- }
- $totalPrice += $diamondPrice;
- $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
- $updateAttributes['constituent_final_price'] = $totalPrice;
- $action->updateAttributes([$productId], $updateAttributes, $storeId);
- $logger->info('productId=' . $productId . 'constituent Price=' . $totalPrice);
- //calculate custom option price
- if ($makingChargesType == 'Percentage') {
- $makingChargesPrice = ($newtotalPrice * $makingChargesValue) / 100;
- } else if ($makingChargesType == 'Fixed') {
- $makingChargesPrice = $makingChargesValue;
- } else if ($makingChargesType == 'Slab') {
- $makingChargesPrice = $makingChargesValue * $newqty;
- }
- //discount price
- $date = date('Y-m-d');
- $websiteId = 1;
- $customerGroupId = null;
- $makingChargesDiscountValue = 0;
- $rules = $this->productBreakDown->getRules($date, $websiteId, $customerGroupId, $productId);
- $makingDiscountRule = in_array('custom_mak',array_column($rules, 'action_operator'));
- if($makingDiscountRule)
- {
- $action_amount = $rules[0]['action_amount'] ?? 0;
- //$makingChargesValue = $mainProduct->getData('making_charges_value');
- $makingChargesDiscountValue = ($makingChargesPrice * (($action_amount) / 100));
- //$totalPrice = $totalPrice - $makingChargesDiscountValue;
- }
- $taxAmount = ($totalPrice + $makingChargesPrice) * $taxPercent / 100;
- $logger->info('totalPrice'.$totalPrice.'makingChargesPrice'.$makingChargesPrice.'taxAmount'.$taxAmount);
- $somePercentageIncreaseInPrice = (($totalPrice + $makingChargesPrice) * 110) / 100;
- $finalPrice = (ceil((float) ($somePercentageIncreaseInPrice + $taxAmount ) / 100)) * 100;
- $logger->info('finalPrice'.$finalPrice);
- $logger->info('storeId'.$storeId);
- // $this->updateProductPrice($finalPrice,$productId,$storeId);
- // $product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
- // $logger->info('productId=' . $product->getId() . 'finalPrice=' . $product->getPrice() . 'productPrice=' . $totalPrice . 'making=' . $makingChargesPrice . 'tax=' . $taxAmount);
- if ($goldPrice > 0) {
- $this->calculateAndStoreOptionPricing($mainProduct, $goldPrice, $finalPrice);
- }
- return $finalPrice;
- } catch (\Exception $e) {
- $finalPrice = 0;
- $logger->info('error' . json_encode($e->getMessage()));
- return $finalPrice;
- }
- //return $finalPrice;
- }
- public function calculateAndStoreOptionPricing($product, $goldPrice, $finalPrice)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $productId = $product->getId();
- try {
- $productRingSize = $product->getResource()->getAttribute('ring_size')->getFrontend()->getValue($product) ?? 0;
- if (count($product->getOptions()) > 0) {
- foreach ($product->getOptions() as $options) {
- $optionTitle = strtolower($options->getTitle());
- if ($optionTitle == 'ring size') {
- $optionData = $options->getValues();
- $i = 0;
- foreach ($optionData as $data) {
- $size = $data->getTitle();
- if ($size > $productRingSize) {
- $i = $i + 0.1;
- $extraPrice = $goldPrice * $i;
- $logger->info('extraprice='.$extraPrice.'goldprice='.$goldPrice.'i='.$i);
- $finalPriceWithOptions = (round(($finalPrice + $extraPrice) / 100)) * 100;
- $customOptionPrice = $finalPriceWithOptions - $finalPrice;
- $data['price'] = $customOptionPrice;
- $product->setProductOptions($data);
- $product->setCanSaveCustomOptions(true);
- // $product->save();
- $logger->info('productId=' .$productId. 'size=' . $size . 'Price=' . $data['price']);
- }
- }
- }
- }
- $product->save();
- }
- return true;
- } catch (\Exception $e) {
- $logger->info('error1' . json_encode($e->getMessage()));
- }
- }
- public function updateProductPrice($finalPrice,$productId,$storeId)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/productsave-' . date('Y-m-d') . '.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $logger->info('update product');
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
- $updateAttributes['price'] = $finalPrice;
- $logger->info('productId=' . $productId . 'finalPrice=' . $finalPrice);
- $action->updateAttributes([$productId], $updateAttributes, $storeId);
- $product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
- $logger->info('productId=' . $product->getId() . 'finalPrice=' . $product->getPrice());
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment