Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Froogal\Catalog\Model;
- use GuzzleHttp\Client;
- use Magento\Framework\App\Helper\Context;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- class ProductPriceCalculator extends \Magento\Framework\Model\AbstractModel implements \Froogal\Catalog\Api\ProductBreakDownInterface
- {
- const RINGSIZE_AMOUNT_INCREMENT = 0.1;
- /**
- * @var ScopeConfigInterface
- */
- protected $scopeConfigInterface;
- protected $customerSession;
- protected $helperData;
- protected $productSolitaireInterface;
- protected $productRepository;
- protected $ruleResource;
- public function __construct(
- ScopeConfigInterface $scopeConfigInterface,
- CustomerRepositoryInterface $customerRepository,
- \Magento\Customer\Model\CustomerFactory $customerFactory,
- \Magento\Customer\Model\Session $customerSession,
- \Froogal\Catalog\Helper\Helpers $helperData,
- \Magento\Tax\Model\Calculation $taxCalculation,
- \Froogal\Catalog\Helper\TaxData $taxData,
- \Froogal\Catalog\Api\ProductSolitaireInterface $productSolitaireInterface,
- \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
- \Magento\CatalogRule\Model\ResourceModel\Rule $rule
- ) {
- $this->client = new Client();
- $this->scopeConfigInterface = $scopeConfigInterface;
- $this->customerRepository = $customerRepository;
- $this->_customerFactory = $customerFactory;
- $this->customerSession = $customerSession;
- $this->helperData = $helperData;
- $this->taxCalculation = $taxCalculation;
- $this->taxData = $taxData;
- $this->productSolitaireInterface = $productSolitaireInterface;
- $this->productRepository = $productRepository;
- $this->ruleResource = $rule;
- }
- public function getProductBreakDown($productId,$productVariant=null,$customOptionId=null,$optionValue=null,$ringSize=null,$goldColor=null,$diamondId=null,$carat=null,$pcnumber=null,$size=null)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $logger->info('customOptionId' . $customOptionId.'optionValue' . $optionValue.'ringSize' . $ringSize );
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- if($pcnumber)
- {
- $mainProduct = $this->getProductByAttributes($pcnumber,$size);
- }
- else
- {
- $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
- }
- $variantId = $mainProduct->getData('display_variant') ?? null;
- $productConstituentInfo = [];
- $productByvariants = [];
- $goldproductConstituents = [];
- $diamondproductConstituents = [];
- $gemstoneproductConstituents = [];
- if(($goldColor != '') && ($goldColor !='undefined') && ($goldColor != null))
- {
- $goldproductConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('color', array('like' => "%$goldColor%"))
- ->getData();
- if(count($goldproductConstituents) > 0)
- {
- $variantId = $goldproductConstituents[0]['variant_id'] ?? null;
- }
- }
- if($carat !=null)
- {
- $diamondproductConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('carat', array('like' => "%$carat%"))
- ->getData();
- if(count($diamondproductConstituents) > 0)
- {
- $variantId = $diamondproductConstituents[0]['variant_id'] ?? null;
- }
- }
- $productByvariants = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
- ->getData();
- $productConstituents = array_merge($productConstituentInfo,$productByvariants);
- $outletModel = $objectManager->create('Froogal\Outlets\Model\OutletInventoryFactory')->create()->load($variantId,'variant_id');
- $ringSize = ($ringSize == $outletModel->getSize()) ? $outletModel->getSize() : $ringSize;
- $productRingSize = $mainProduct->getResource()->getAttribute('ring_size')->getFrontend()->getValue($mainProduct) ?? 0;
- $finalPrice = $mainProduct->getFinalPrice();
- $originalPrice = $mainProduct->getFinalPrice();
- $actualFinalPrice = (($finalPrice * 100) / (100 + 10));
- $diffPrice = $finalPrice - $actualFinalPrice;
- //catalog rules
- $date = date('Y-m-d');
- $websiteId = 1;
- $customerGroupId = null;
- $regularPrice = 0;
- $glowSku = '';
- $action_amount = 0;
- $rules = $this->getRules($date, $websiteId, $customerGroupId, $productId);
- $makingDiscountRule = in_array('custom_mak',array_column($rules, 'action_operator'));
- $discountAmount = $this->getProductDiscount($mainProduct);
- if($makingDiscountRule)
- {
- $action_amount = $rules[0]['action_amount'] ?? 0;
- $makingChargesValue = $mainProduct->getData('making_charges_value');
- $makingChargesDiscountValue = ($makingChargesValue * ($action_amount / 100));
- }
- if($variantId)
- {
- $productVariant = $objectManager->create('Froogal\Catalog\Model\ProductVariant')->load($variantId);
- $glowSku = $productVariant['glow_sku'];
- }
- if ($customOptionId) {
- if($discountAmount)
- {
- $regularPrice = $mainProduct->getPriceInfo()->getPrice('regular_price')->getValue();
- $finalPrice = $this->getOptionPrice($mainProduct,$regularPrice,$productRingSize,$customOptionId,$optionValue);
- }
- else
- {
- $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
- }
- // $finalPrice = $mainProduct->getFinalPrice() + $option->getPrice();
- } else {
- $finalPrice = $mainProduct->getPrice();
- }
- $finalArray = [];
- if(!empty($productConstituents))
- {
- $countryCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID);
- $regionCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID);
- $customerTaxClassId = $this->scopeConfigInterface->getValue('tax/classes/default_customer_tax_class');
- /** @var \Magento\Catalog\Model\Product $product */
- $productTaxClassId = $mainProduct->getData('tax_class_id');
- // THE ACTUAL CALCULATION CALL
- /** @var \Magento\Tax\Model\Calculation $taxCalculation */
- $taxPercent = $this->taxCalculation->getRate(
- new \Magento\Framework\DataObject(
- [
- 'country_id' => $countryCode,
- 'region_id' => $regionCode,
- 'customer_class_id' => $customerTaxClassId,
- 'product_class_id' => $productTaxClassId
- ]
- )
- );
- $diamondCarats = 0;
- $diamondPrice = 0;
- $goldWeight = 0;
- $goldPrice = 0;
- $gemstoneCarats = 0;
- $gemstonePrice = 0;
- $platinumWeight = 0;
- $platinumPrice = 0;
- $goldColor = 0;
- $carats = 0;
- foreach($productConstituents as $productConstituentData)
- {
- $contituentProductId = $productConstituentData['contituent_product_id'];
- $color = $mainProduct->getResource()->getAttribute('gold_color')->getFrontend()->getValue($mainProduct);
- if($productConstituentData['color'])
- {
- $color = $mainProduct->getData('gold_color');
- }
- $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
- $contituentProductPrice = $contituentProduct->getPrice();
- $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
- if ($constituentType == "Diamond") {
- $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
- $diamondCarats += $carats;
- if($carat == $carats)
- {
- $diamondCarats = $carats;
- }
- // $diamondPrice += $contituentProductPrice * $carats;
- // $diamondPrice = $finalPrice;
- $diamondPrice = 1;
- } elseif ($constituentType == "Gold") {
- $goldWeight += $productConstituentData['qty'];
- if ($ringSize!='undefined' && $ringSize != $productRingSize && $goldColor == $color) {
- $goldWeight = $productConstituentData['qty'];
- $sizeDifference = max(0, (float)$ringSize - (float)$productRingSize);
- $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
- if ($weightDifference) {
- $goldWeight += $weightDifference;
- }
- }
- else if ($ringSize!='undefined' && $ringSize != $productRingSize) {
- $sizeDifference = max(0, (int)$ringSize - (int)$productRingSize);
- $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
- if ($weightDifference) {
- $goldWeight += $weightDifference;
- }
- }
- $goldPrice += $contituentProductPrice * ((float) $goldWeight);
- } elseif ($constituentType == "Gemstone") {
- $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
- $gemstoneCarats += $carats;
- $gemstonePrice += $contituentProductPrice * $carats;
- } elseif ($constituentType == "Platinum") {
- $platinumWeight = $productConstituentData['qty'];
- $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
- }
- }
- if ($productVariant === 'solitaire') {
- $productBreakDown = $this->getSolitaireProductBreakDown($productId,$diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null);
- return $this->helperData->success('Product BreakDown', $productBreakDown);
- }
- $constituentsTotal = 0;
- $gemstonePrice = $mainProduct->getResource()->getAttribute('gemstone_price')->getFrontend()->getValue($mainProduct);
- $valueAddition = ($mainProduct->getResource()->getAttribute('making_charges_value')->getFrontend()->getValue($mainProduct))*($mainProduct->getResource()->getAttribute('gold_weight')->getFrontend()->getValue($mainProduct));
- $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
- $taxAmount = round($finalPrice - $netAmount);
- // display 80 % of tax code
- // $taxAmount = round(($finalPrice - $netAmount) * 70 / 100);
- // display 80 % of tax code
- $content = [];
- $productDetails=[
- 'variant_carat'=> "({$carats}g)",
- 'variant_weight'=> $goldWeight,
- 'variant_sku'=> $glowSku,
- 'variant_color'=> $color
- ];
- if ($goldPrice) {
- $content[] = [
- 'variant_type' => "Gold ({$goldWeight}g)",
- 'variant_price' => $goldPrice,
- ];
- $constituentsTotal += $goldPrice;
- }
- if ($diamondPrice) {
- // $diamondPrice = $diamondPrice + $diffPrice;
- // $diamondPrice = $diamondPrice;
- // diamond value original tax//
- $diamondPrice = $finalPrice - ($goldPrice + $valueAddition + $taxAmount + $gemstonePrice + $platinumPrice ) ;
- //$diamondPrice = (($taxAmount) * (20 / 100))+ ($finalPrice - ($goldPrice + $valueAddition + $taxAmount + $gemstonePrice + $platinumPrice )) ;
- $content[] = [
- 'variant_type' => "Diamonds ({$diamondCarats}cts)",
- 'variant_price' => $diamondPrice,
- ];
- $constituentsTotal += $diamondPrice;
- }
- if ($gemstonePrice) {
- $content[] = [
- // 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
- // 'variant_price' => $gemstonePrice,
- ];
- // $constituentsTotal += $gemstonePrice;
- }
- if ($platinumPrice) {
- $content[] = [
- 'variant_type' => "Platinum ({$platinumWeight}g)",
- 'variant_price' => $platinumPrice,
- ];
- $constituentsTotal += $platinumPrice;
- }
- $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
- if ($gemstoneAttributePrice) {
- $content[] = [
- 'variant_type' => "Gemstones",
- 'variant_price' => $gemstoneAttributePrice,
- ];
- $constituentsTotal += $gemstoneAttributePrice;
- }
- $logger->info('constituentsTotal'.json_encode($constituentsTotal));
- $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
- // $taxAmount = round($finalPrice - $netAmount);
- // 80 percent add
- $taxAmount = round(($finalPrice - $netAmount) * 80 / 100);
- //80 percent done
- // $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
- // $valueAddition = $netAmount - $constituentsTotal;
- $valueAddition = ($mainProduct->getResource()->getAttribute('making_charges_value')->getFrontend()->getValue($mainProduct))*($mainProduct->getResource()->getAttribute('gold_weight')->getFrontend()->getValue($mainProduct));
- $logger->info('valueAddition='.$valueAddition.'netamount=' .$netAmount .'constituentsTotal='.$constituentsTotal.'taxamount='.$taxAmount);
- $finalDiscountAmount = max(0,$regularPrice - $originalPrice);
- $makingChargesDiscountValue = round(max($regularPrice - $finalPrice,0));
- // $finalDiscountAmount = round(max($valueAddition * ($action_amount / 100),0));
- $discountedTaxAmount = 0;
- if($makingDiscountRule)
- {
- $makingChargesDiscountValue = round(max($valueAddition * ($action_amount / 100),0));
- $makingTaxOnDiscount = $makingChargesDiscountValue * ($taxPercent / 100);
- $discountedTaxAmount = $taxAmount - $makingTaxOnDiscount;
- }
- $finalArray = [
- 'content' => $content,
- 'productDetails' => $productDetails,
- 'totalPrice' => $finalPrice,
- 'discountAmount' => $finalDiscountAmount,
- 'product_discount' => $finalDiscountAmount,
- 'valueAddition' => $valueAddition,
- 'tax' => $taxAmount,
- 'effectiveTax' => $discountedTaxAmount
- ];
- }
- return $this->helperData->success('Product BreakDown', $finalArray);
- }
- public function getOptionPrice($product,$finalPrice,$ringSize,$customOptionId,$optionValue)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
- if (count($customOptions) > 0) {
- foreach ($customOptions as $options) {
- $optionTitle = strtolower($options->getTitle());
- if ($optionTitle == 'ring size') {
- $optionData = $options->getValues();
- foreach ($optionData as $data) {
- $size = $data->getTitle();
- $optionTypeId = $data->getData('option_type_id');
- if($optionTypeId == $optionValue)
- {
- // $logger->info('optiontypeid=' . $optionTypeId . 'optionvalue=' . $optionValue . 'size=' . $size . 'ringsize=' . $ringSize);
- $customOptionPrice = $data->getPrice();
- $finalPrice = $finalPrice + $customOptionPrice;
- $logger->info('customOptionPrice='.$customOptionPrice.'finalprice=' . $finalPrice);
- }
- }
- }
- }
- }
- return $finalPrice;
- }
- public function getSolitaireProductDetails($productId,$diamondId)
- {
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $productRelation = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->getData();
- $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
- $solitaireDetails = $response['response']['body'];
- if(empty($solitaireDetails))
- {
- return $this->helperData->error('No Diamond was found',$solitaireDetails);
- }
- $solitaireDetails = $response['response']['body']['diamond'] ?? null;
- $data = [
- ['value' => 'Diamond','label' => 'Constituent Type','attribute_code' => 'constituent_type'],
- ['value' => $solitaireDetails['diamond_id'] ?? '','label' => 'DiamondID','attribute_code' => 'diamond_id'],
- ['value' => $solitaireDetails['shape'] ?? '','label' => 'Shape','attribute_code' => 'shape'],
- ['value' => $solitaireDetails['size'] ?? '','label' => 'Size','attribute_code' => 'carat'],
- ['value' => $solitaireDetails['color'] ?? '','label' => 'Color','attribute_code' => 'color'],
- ['value' => $solitaireDetails['clarity'] ?? '','label' => 'Quality','attribute_code' => 'quality'],
- ['value' => $solitaireDetails['cut'] ?? '','label' => 'Cut','attribute_code' => 'cut'],
- ['value' => $solitaireDetails['stock_num'] ?? '','label' => 'Style Code','attribute_code' => 'style_code'],
- ['value' => $solitaireDetails['symmetry'] ?? '','label' => 'Symmetry','attribute_code' => 'symmetry'],
- ['value' => $solitaireDetails['currency_symbol'] ?? '','Symbol' => 'DiamondID','attribute_code' => 'currency_symbol'],
- ['value' => $solitaireDetails['total_sales_price_in_currency'] ?? '','label' => 'Diamond Price','attribute_code' => 'total_sales_price_in_currency']
- ];
- $content = [];
- $finalArray = [];
- if (!empty($productRelation)) {
- foreach ($productRelation as $productRelationValue) {
- $masterProductId = $productRelationValue['contituent_product_id'];
- $qty = $productRelationValue['qty'];
- $carat = $productRelationValue['carat'];
- $product = $objectManager->create('Magento\Catalog\Model\Product')->load($masterProductId);
- $price = $product->getPrice();
- $weightUnit = $this->scopeConfigInterface->getValue(
- 'general/locale/weight_unit',
- ScopeInterface::SCOPE_STORE
- );
- $constituentType = $product->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($product);
- if ($constituentType == 'Gold') {
- // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
- // $data[] = ['value' => $productRelationValue['qty'],'label' => 'Gold Weight','attribute_code' => 'gold_weight'];
- // $data[] = ['value' => $productRelationValue['carat'],'label' => 'Gold Carat','attribute_code' => 'gold_carat'];
- // $data[] = ['value' => $productRelationValue['color'],'label' => 'Gold Color','attribute_code' => 'gold_color'];
- }
- else if ($constituentType == 'Platinum') {
- // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
- }
- else if ($constituentType == 'Gemstone') {
- $carats = ((float) $productRelationValue['qty']) * ($productRelationValue['carat'] ?? 1);
- $gemstoneCarats += $carats;
- // $data[] = ['value' => $gemstoneCarats,'label' => 'GemStone Weight','attribute_code' => 'gemstone_weight'];
- }
- }
- }
- // array_push($content, $data);
- return $this->helperData->success('Solitaire Product Details',$data);
- }
- protected function getProductDiscount($product)
- {
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $rule = $objectManager->create('\Magento\CatalogRule\Model\RuleFactory')->create();
- $discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
- return $discountAmount;
- }
- public function getSolitaireProductBreakDown($productId, $diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null)
- {
- $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
- $logger = new \Zend_Log();
- $logger->addWriter($writer);
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- if($pcnumber)
- {
- $mainProduct = $this->getProductByAttributes($pcnumber,$size);
- }
- else
- {
- $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
- }
- //$mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
- // $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->addFieldToFilter('variant_id', array('like' => "%$variantId%"))->getData();
- $productRingSize = $ringSize != '' ? $ringSize : ($mainProduct->getResource()->getAttribute('ring_size')->getFrontend()->getValue($mainProduct) ?? 0);
- $variantId = $mainProduct->getData('display_variant') ?? null;
- $glowSku = '';
- if($goldColor !=null && $goldColor !='undefined')
- {
- $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
- // ->addFieldToFilter('color', array('like' => "%$goldColor%"))
- ->getData();
- }
- else if($carat !=null)
- {
- $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
- // ->addFieldToFilter('carat', array('like' => "%$carat%"))
- ->getData();
- }
- else
- {
- $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
- ->addFieldToFilter('product_id', $productId)
- ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
- ->getData();
- // return $productConstituents;
- }
- $productCalclator = $objectManager->create('\Froogal\Catalog\Model\ProductPriceCalculator');
- $finalPrice = $productCalclator->getFinalPrice($productId,$diamondId);
- // return $finalPrice;
- $discountAmount = $this->getProductDiscount($mainProduct);
- // return $discountAmount;
- $regularPrice = 0;
- if($variantId)
- {
- $productVariant = $objectManager->create('Froogal\Catalog\Model\ProductVariant')->load($variantId);
- $glowSku = $productVariant['glow_sku'];
- }
- if ($customOptionId) {
- if($discountAmount)
- {
- // $regularPrice = $mainProduct->getPriceInfo()->getPrice('regular_price')->getValue();
- $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
- }
- else
- {
- $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
- }
- } else {
- $finalPrice = $mainProduct->getFinalPrice();
- }
- $finalArray = [];
- if(!empty($productConstituents))
- {
- $countryCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID);
- $regionCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID);
- $customerTaxClassId = $this->scopeConfigInterface->getValue('tax/classes/default_customer_tax_class');
- /** @var \Magento\Catalog\Model\Product $product */
- $productTaxClassId = $mainProduct->getData('tax_class_id');
- // THE ACTUAL CALCULATION CALL
- /** @var \Magento\Tax\Model\Calculation $taxCalculation */
- $taxPercent = $this->taxCalculation->getRate(
- new \Magento\Framework\DataObject(
- [
- 'country_id' => $countryCode,
- 'region_id' => $regionCode,
- 'customer_class_id' => $customerTaxClassId,
- 'product_class_id' => $productTaxClassId
- ]
- )
- );
- $diamondCarats = 0;
- $diamondPrice = 0;
- $goldWeight = 0;
- $goldPrice = 0;
- $gemstoneCarats = 0;
- $gemstonePrice = 0;
- $platinumWeight = 0;
- $platinumPrice = 0;
- $goldColor = 0;
- foreach($productConstituents as $productConstituentData)
- {
- $contituentProductId = $productConstituentData['contituent_product_id'];
- // $glowSku = $productConstituentData['glow_sku'] ?? '';
- $color = $productConstituentData['color']?? '' ;
- $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
- $contituentProductPrice = $contituentProduct->getPrice();
- $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
- if ($constituentType == "Diamond") {
- // $request['body']['diamond_id'] = $diamondId;
- $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
- $solitaireDetails = $response['response']['body'];
- if(empty($solitaireDetails))
- {
- $diamondPrice = 0;
- }
- else
- {
- $solitaireDetails = $solitaireDetails['diamond'];
- $diamondPrice = $solitaireDetails['total_sales_price_in_currency'] ?? '';
- $diamondCarats = $solitaireDetails['size'];
- }
- }
- if ($constituentType == "Gold") {
- $goldWeight += $productConstituentData['qty'];
- if ($ringSize!='undefined' && $ringSize != $productRingSize) {
- $sizeDifference = max(0, $ringSize - $productRingSize);
- $weightDifference = $sizeDifference * 0.1;
- if ($weightDifference) {
- $goldWeight += $weightDifference;
- }
- }
- $goldPrice += $contituentProductPrice * ((float) $goldWeight);
- } elseif ($constituentType == "Gemstone") {
- $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
- $gemstoneCarats += $carats;
- $gemstonePrice += $contituentProductPrice * $carats;
- } elseif ($constituentType == "Platinum") {
- $platinumWeight = $productConstituentData['qty'];
- $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
- }
- }
- // return $finalPrice;
- $constituentsTotal = 0;
- $content = [];
- $productDetails=[
- 'variant_carat'=> "({$diamondCarats}g)",
- 'variant_weight'=> $goldWeight,
- 'variant_sku'=> $glowSku,
- 'variant_color'=> $color
- ];
- if ($goldPrice) {
- $content[] = [
- 'variant_type' => "Gold ({$goldWeight}g)",
- 'variant_price' => $goldPrice,
- ];
- $constituentsTotal += $goldPrice;
- }
- if ($diamondPrice) {
- $content[] = [
- 'variant_type' => "Diamonds ({$diamondCarats}cts)",
- 'variant_price' => $diamondPrice,
- ];
- $constituentsTotal += $diamondPrice;
- }
- if ($gemstonePrice) {
- $content[] = [
- 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
- 'variant_price' => $gemstonePrice,
- ];
- $constituentsTotal += $gemstonePrice;
- }
- if ($platinumPrice) {
- $content[] = [
- 'variant_type' => "Platinum ({$platinumWeight}g)",
- 'variant_price' => $platinumPrice,
- ];
- $constituentsTotal += $platinumPrice;
- }
- $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
- if ($gemstoneAttributePrice) {
- $content[] = [
- 'variant_type' => "Gemstones",
- 'variant_price' => $gemstoneAttributePrice,
- ];
- $constituentsTotal += $gemstoneAttributePrice;
- }
- $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
- $taxAmount = round($finalPrice - $netAmount);
- $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
- $finalDiscountAmount = max(0,$regularPrice - $discountAmount);
- // return $finalPrice;
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
- $storeId = $objectManager->create('\Froogal\Catalog\Cron\PricingCalculation')->getStoreId();
- $updateAttributes['solitaire_final_price'] = $finalPrice;
- $action->updateAttributes([$productId], $updateAttributes, $storeId);
- $finalArray = [
- 'content' => $content,
- 'productDetails' => $productDetails,
- 'totalPrice' => $finalPrice,
- 'discountAmount' => $finalDiscountAmount,
- 'valueAddition' => $valueAddition,
- 'tax' => $taxAmount,
- ];
- }
- return $finalArray;
- }
- public function getProductByAttributes($pcnumber,$size)
- {
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $outletInventory = $objectManager->create('Froogal\Outlets\Model\ResourceModel\OutletInventory\CollectionFactory')->create()->addFieldToSelect('*')->addFieldToFilter('pc_number',$pcnumber)->addFieldToFilter('size',$size)->setOrder('id', 'asc')->getFirstItem();
- if($outletInventory)
- {
- $glowSku = $outletInventory->getData('glow_sku');
- $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('glow_sku',$glowSku);
- return $mainProduct;
- }
- }
- public function getRules($date, $websiteId, $customerGroupId, $productId)
- {
- /** @var [] $rules catalog rules */
- $rules = $this->ruleResource->getRulesFromProduct($date, $websiteId, $customerGroupId, $productId);
- return $rules;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement