baby_in_magento

pawan_code

Oct 9th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.37 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Froogal\Catalog\Model;
  4.  
  5. use GuzzleHttp\Client;
  6. use Magento\Framework\App\Helper\Context;
  7. use Magento\Store\Model\ScopeInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10.  
  11.  
  12.  
  13.  
  14. class ProductBreakDown extends \Magento\Framework\Model\AbstractModel implements \Froogal\Catalog\Api\ProductBreakDownInterface
  15. {
  16.  
  17. const RINGSIZE_AMOUNT_INCREMENT = 0.1;
  18.  
  19. /**
  20. * @var ScopeConfigInterface
  21. */
  22. protected $scopeConfigInterface;
  23. protected $customerSession;
  24.  
  25. protected $helperData;
  26.  
  27. protected $productSolitaireInterface;
  28.  
  29. protected $productRepository;
  30.  
  31. protected $ruleResource;
  32. public function __construct(
  33. ScopeConfigInterface $scopeConfigInterface,
  34. CustomerRepositoryInterface $customerRepository,
  35. \Magento\Customer\Model\CustomerFactory $customerFactory,
  36. \Magento\Customer\Model\Session $customerSession,
  37. \Froogal\Catalog\Helper\Helpers $helperData,
  38. \Magento\Tax\Model\Calculation $taxCalculation,
  39. \Froogal\Catalog\Helper\TaxData $taxData,
  40. \Froogal\Catalog\Api\ProductSolitaireInterface $productSolitaireInterface,
  41. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  42. \Magento\CatalogRule\Model\ResourceModel\Rule $rule
  43.  
  44.  
  45. ) {
  46. $this->client = new Client();
  47. $this->scopeConfigInterface = $scopeConfigInterface;
  48. $this->customerRepository = $customerRepository;
  49. $this->_customerFactory = $customerFactory;
  50. $this->customerSession = $customerSession;
  51. $this->helperData = $helperData;
  52. $this->taxCalculation = $taxCalculation;
  53. $this->taxData = $taxData;
  54. $this->productSolitaireInterface = $productSolitaireInterface;
  55. $this->productRepository = $productRepository;
  56. $this->ruleResource = $rule;
  57.  
  58. }
  59.  
  60.  
  61.  
  62. public function getProductBreakDown($productId,$productVariant=null,$customOptionId=null,$optionValue=null,$ringSize=null,$goldColor=null,$diamondId=null,$carat=null,$pcnumber=null,$size=null)
  63. {
  64. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
  65. $logger = new \Zend_Log();
  66. $logger->addWriter($writer);
  67. $logger->info('customOptionId' . $customOptionId.'optionValue' . $optionValue.'ringSize' . $ringSize );
  68.  
  69. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  70. if($pcnumber)
  71. {
  72. $mainProduct = $this->getProductByAttributes($pcnumber,$size);
  73. }
  74. else
  75. {
  76. $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  77. }
  78. $variantId = $mainProduct->getData('display_variant') ?? null;
  79. $productConstituentInfo = [];
  80. $productByvariants = [];
  81. $goldproductConstituents = [];
  82. $diamondproductConstituents = [];
  83. $gemstoneproductConstituents = [];
  84. if(($goldColor != '') && ($goldColor !='undefined') && ($goldColor != null))
  85. {
  86. $goldproductConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  87. ->addFieldToFilter('product_id', $productId)
  88. ->addFieldToFilter('color', array('like' => "%$goldColor%"))
  89. ->getData();
  90. if(count($goldproductConstituents) > 0)
  91. {
  92. $variantId = $goldproductConstituents[0]['variant_id'] ?? null;
  93. }
  94. }
  95. if($carat !=null)
  96. {
  97. $diamondproductConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  98. ->addFieldToFilter('product_id', $productId)
  99. ->addFieldToFilter('carat', array('like' => "%$carat%"))
  100. ->getData();
  101. if(count($diamondproductConstituents) > 0)
  102. {
  103. $variantId = $diamondproductConstituents[0]['variant_id'] ?? null;
  104. }
  105. }
  106.  
  107. $productByvariants = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  108. ->addFieldToFilter('product_id', $productId)
  109. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  110. ->getData();
  111.  
  112.  
  113. $productConstituents = array_merge($productConstituentInfo,$productByvariants);
  114. $outletModel = $objectManager->create('Froogal\Outlets\Model\OutletInventoryFactory')->create()->load($variantId,'variant_id');
  115. $ringSize = ($ringSize == $outletModel->getSize()) ? $outletModel->getSize() : $ringSize;
  116. $productRingSize = $mainProduct->getResource()->getAttribute('ring_size')->getFrontend()->getValue($mainProduct) ?? 0;
  117. $finalPrice = $mainProduct->getFinalPrice();
  118. $originalPrice = $mainProduct->getFinalPrice();
  119. $actualFinalPrice = (($finalPrice * 100) / (100 + 10));
  120. $diffPrice = $finalPrice - $actualFinalPrice;
  121.  
  122. //catalog rules
  123. $date = date('Y-m-d');
  124. $websiteId = 1;
  125. $customerGroupId = null;
  126. $regularPrice = 0;
  127. $glowSku = '';
  128. $action_amount = 0;
  129. $rules = $this->getRules($date, $websiteId, $customerGroupId, $productId);
  130. $makingDiscountRule = in_array('custom_mak',array_column($rules, 'action_operator'));
  131. $discountAmount = $this->getProductDiscount($mainProduct);
  132. if($makingDiscountRule)
  133. {
  134. $action_amount = $rules[0]['action_amount'] ?? 0;
  135. $makingChargesValue = $mainProduct->getData('making_charges_value');
  136. $makingChargesDiscountValue = ($makingChargesValue * ($action_amount / 100));
  137. }
  138.  
  139. if($variantId)
  140. {
  141. $productVariant = $objectManager->create('Froogal\Catalog\Model\ProductVariant')->load($variantId);
  142. $glowSku = $productVariant['glow_sku'];
  143. }
  144. if ($customOptionId) {
  145. if($discountAmount)
  146. {
  147. $regularPrice = $mainProduct->getPriceInfo()->getPrice('regular_price')->getValue();
  148. $finalPrice = $this->getOptionPrice($mainProduct,$regularPrice,$productRingSize,$customOptionId,$optionValue);
  149. }
  150. else
  151. {
  152. $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
  153. }
  154.  
  155. // $finalPrice = $mainProduct->getFinalPrice() + $option->getPrice();
  156. } else {
  157. $finalPrice = $mainProduct->getPrice();
  158. }
  159.  
  160. $finalArray = [];
  161. if(!empty($productConstituents))
  162. {
  163. $countryCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID);
  164. $regionCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID);
  165.  
  166. $customerTaxClassId = $this->scopeConfigInterface->getValue('tax/classes/default_customer_tax_class');
  167. /** @var \Magento\Catalog\Model\Product $product */
  168. $productTaxClassId = $mainProduct->getData('tax_class_id');
  169.  
  170. // THE ACTUAL CALCULATION CALL
  171.  
  172. /** @var \Magento\Tax\Model\Calculation $taxCalculation */
  173. $taxPercent = $this->taxCalculation->getRate(
  174. new \Magento\Framework\DataObject(
  175. [
  176. 'country_id' => $countryCode,
  177. 'region_id' => $regionCode,
  178. 'customer_class_id' => $customerTaxClassId,
  179. 'product_class_id' => $productTaxClassId
  180. ]
  181. )
  182. );
  183.  
  184. $diamondCarats = 0;
  185. $diamondPrice = 0;
  186. $goldWeight = 0;
  187. $goldPrice = 0;
  188. $gemstoneCarats = 0;
  189. $gemstonePrice = 0;
  190. $platinumWeight = 0;
  191. $platinumPrice = 0;
  192. $goldColor = 0;
  193. $carats = 0;
  194. foreach($productConstituents as $productConstituentData)
  195. {
  196. $contituentProductId = $productConstituentData['contituent_product_id'];
  197. $color = $productConstituentData['color'];
  198. $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
  199. $contituentProductPrice = $contituentProduct->getPrice();
  200. $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
  201. if ($constituentType == "Diamond") {
  202. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  203. $diamondCarats += $carats;
  204. if($carat == $carats)
  205. {
  206. $diamondCarats = $carats;
  207. }
  208. $diamondPrice += $contituentProductPrice * $carats;
  209.  
  210. } elseif ($constituentType == "Gold") {
  211. $goldWeight += $productConstituentData['qty'];
  212. if ($ringSize!='undefined' && $ringSize != $productRingSize && $goldColor == $color) {
  213. $goldWeight = $productConstituentData['qty'];
  214. $sizeDifference = max(0, (float)$ringSize - (float)$productRingSize);
  215. $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
  216. if ($weightDifference) {
  217. $goldWeight += $weightDifference;
  218. }
  219. }
  220. else if ($ringSize!='undefined' && $ringSize != $productRingSize) {
  221. $sizeDifference = max(0, (int)$ringSize - (int)$productRingSize);
  222. $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
  223. if ($weightDifference) {
  224. $goldWeight += $weightDifference;
  225. }
  226. }
  227. $goldPrice += $contituentProductPrice * ((float) $goldWeight);
  228. } elseif ($constituentType == "Gemstone") {
  229. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  230. $gemstoneCarats += $carats;
  231. $gemstonePrice += $contituentProductPrice * $carats;
  232. } elseif ($constituentType == "Platinum") {
  233. $platinumWeight = $productConstituentData['qty'];
  234. $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
  235. }
  236. }
  237. if ($productVariant === 'solitaire') {
  238. $productBreakDown = $this->getSolitaireProductBreakDown($productId,$diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null);
  239. return $this->helperData->success('Product BreakDown', $productBreakDown);
  240. }
  241. $constituentsTotal = 0;
  242. $content = [];
  243. $productDetails=[
  244. 'variant_carat'=> "({$carats}g)",
  245. 'variant_weight'=> $goldWeight,
  246. 'variant_sku'=> $glowSku,
  247. 'variant_color'=> $color
  248. ];
  249. if ($goldPrice) {
  250. $content[] = [
  251. 'variant_type' => "Gold ({$goldWeight}g)",
  252. 'variant_price' => $goldPrice,
  253. ];
  254. $constituentsTotal += $goldPrice;
  255. }
  256. if ($diamondPrice) {
  257. $diamondPrice = $diamondPrice + $diffPrice;
  258. $content[] = [
  259. 'variant_type' => "Diamonds ({$diamondCarats}cts)",
  260. 'variant_price' => $diamondPrice,
  261. ];
  262. $constituentsTotal += $diamondPrice;
  263. }
  264. if ($gemstonePrice) {
  265. $content[] = [
  266. 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
  267. 'variant_price' => $gemstonePrice,
  268. ];
  269. $constituentsTotal += $gemstonePrice;
  270. }
  271. if ($platinumPrice) {
  272. $content[] = [
  273. 'variant_type' => "Platinum ({$platinumWeight}g)",
  274. 'variant_price' => $platinumPrice,
  275. ];
  276. $constituentsTotal += $platinumPrice;
  277. }
  278. $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
  279. if ($gemstoneAttributePrice) {
  280. $content[] = [
  281. 'variant_type' => "Gemstones",
  282. 'variant_price' => $gemstoneAttributePrice,
  283. ];
  284. $constituentsTotal += $gemstoneAttributePrice;
  285. }
  286. $logger->info('constituentsTotal'.json_encode($constituentsTotal));
  287. $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
  288. $taxAmount = round($finalPrice - $netAmount);
  289.  
  290. // $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
  291. // $valueAddition = $product->getResource()->getAttribute('making_charges_value')->getFrontend()->getValue($product);
  292. $valueAddition = $_product->getAttributeText(making_charges_value);
  293.  
  294.  
  295. $logger->info('valueAddition='.$valueAddition.'netamount=' .$netAmount .'constituentsTotal='.$constituentsTotal.'taxamount='.$taxAmount);
  296. $finalDiscountAmount = max(0,$regularPrice - $originalPrice);
  297. $makingChargesDiscountValue = round(max($regularPrice - $finalPrice,0));
  298.  
  299.  
  300. // $finalDiscountAmount = round(max($valueAddition * ($action_amount / 100),0));
  301. $discountedTaxAmount = 0;
  302. if($makingDiscountRule)
  303. {
  304. $makingChargesDiscountValue = round(max($valueAddition * ($action_amount / 100),0));
  305. $makingTaxOnDiscount = $makingChargesDiscountValue * ($taxPercent / 100);
  306. $discountedTaxAmount = $taxAmount - $makingTaxOnDiscount;
  307. }
  308.  
  309. $finalArray = [
  310. 'content' => $content,
  311. 'productDetails' => $productDetails,
  312. 'totalPrice' => $finalPrice,
  313. 'discountAmount' => $finalDiscountAmount,
  314. 'product_discount' => $finalDiscountAmount,
  315. 'valueAddition' => $valueAddition,
  316. 'tax' => $taxAmount,
  317. 'effectiveTax' => $discountedTaxAmount
  318. ];
  319. }
  320. return $this->helperData->success('Product BreakDown', $finalArray);
  321. }
  322.  
  323. public function getOptionPrice($product,$finalPrice,$ringSize,$customOptionId,$optionValue)
  324. {
  325. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
  326. $logger = new \Zend_Log();
  327. $logger->addWriter($writer);
  328. $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  329. $customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
  330. if (count($customOptions) > 0) {
  331. foreach ($customOptions as $options) {
  332. $optionTitle = strtolower($options->getTitle());
  333. if ($optionTitle == 'ring size') {
  334. $optionData = $options->getValues();
  335. foreach ($optionData as $data) {
  336.  
  337. $size = $data->getTitle();
  338. $optionTypeId = $data->getData('option_type_id');
  339. if($optionTypeId == $optionValue)
  340. {
  341. // $logger->info('optiontypeid=' . $optionTypeId . 'optionvalue=' . $optionValue . 'size=' . $size . 'ringsize=' . $ringSize);
  342.  
  343. $customOptionPrice = $data->getPrice();
  344. $finalPrice = $finalPrice + $customOptionPrice;
  345. $logger->info('customOptionPrice='.$customOptionPrice.'finalprice=' . $finalPrice);
  346. }
  347.  
  348. }
  349. }
  350. }
  351. }
  352. return $finalPrice;
  353. }
  354. public function getSolitaireProductDetails($productId,$diamondId)
  355. {
  356. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  357. $productRelation = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->getData();
  358. $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
  359. $solitaireDetails = $response['response']['body'];
  360. if(empty($solitaireDetails))
  361. {
  362. return $this->helperData->error('No Diamond was found',$solitaireDetails);
  363. }
  364. $solitaireDetails = $response['response']['body']['diamond'] ?? null;
  365. $data = [
  366. ['value' => 'Diamond','label' => 'Constituent Type','attribute_code' => 'constituent_type'],
  367. ['value' => $solitaireDetails['diamond_id'] ?? '','label' => 'DiamondID','attribute_code' => 'diamond_id'],
  368. ['value' => $solitaireDetails['shape'] ?? '','label' => 'Shape','attribute_code' => 'shape'],
  369. ['value' => $solitaireDetails['size'] ?? '','label' => 'Size','attribute_code' => 'carat'],
  370. ['value' => $solitaireDetails['color'] ?? '','label' => 'Color','attribute_code' => 'color'],
  371. ['value' => $solitaireDetails['clarity'] ?? '','label' => 'Quality','attribute_code' => 'quality'],
  372. ['value' => $solitaireDetails['cut'] ?? '','label' => 'Cut','attribute_code' => 'cut'],
  373. ['value' => $solitaireDetails['stock_num'] ?? '','label' => 'Style Code','attribute_code' => 'style_code'],
  374. ['value' => $solitaireDetails['symmetry'] ?? '','label' => 'Symmetry','attribute_code' => 'symmetry'],
  375. ['value' => $solitaireDetails['currency_symbol'] ?? '','Symbol' => 'DiamondID','attribute_code' => 'currency_symbol'],
  376. ['value' => $solitaireDetails['total_sales_price_in_currency'] ?? '','label' => 'Diamond Price','attribute_code' => 'total_sales_price_in_currency']
  377. ];
  378.  
  379. $content = [];
  380. $finalArray = [];
  381. if (!empty($productRelation)) {
  382.  
  383. foreach ($productRelation as $productRelationValue) {
  384. $masterProductId = $productRelationValue['contituent_product_id'];
  385.  
  386. $qty = $productRelationValue['qty'];
  387. $carat = $productRelationValue['carat'];
  388.  
  389. $product = $objectManager->create('Magento\Catalog\Model\Product')->load($masterProductId);
  390. $price = $product->getPrice();
  391. $weightUnit = $this->scopeConfigInterface->getValue(
  392. 'general/locale/weight_unit',
  393. ScopeInterface::SCOPE_STORE
  394. );
  395.  
  396. $constituentType = $product->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($product);
  397.  
  398. if ($constituentType == 'Gold') {
  399. // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
  400. // $data[] = ['value' => $productRelationValue['qty'],'label' => 'Gold Weight','attribute_code' => 'gold_weight'];
  401. // $data[] = ['value' => $productRelationValue['carat'],'label' => 'Gold Carat','attribute_code' => 'gold_carat'];
  402. // $data[] = ['value' => $productRelationValue['color'],'label' => 'Gold Color','attribute_code' => 'gold_color'];
  403. }
  404. else if ($constituentType == 'Platinum') {
  405. // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
  406. }
  407. else if ($constituentType == 'Gemstone') {
  408. $carats = ((float) $productRelationValue['qty']) * ($productRelationValue['carat'] ?? 1);
  409. $gemstoneCarats += $carats;
  410. // $data[] = ['value' => $gemstoneCarats,'label' => 'GemStone Weight','attribute_code' => 'gemstone_weight'];
  411. }
  412. }
  413. }
  414. // array_push($content, $data);
  415. return $this->helperData->success('Solitaire Product Details',$data);
  416.  
  417. }
  418.  
  419. protected function getProductDiscount($product)
  420. {
  421.  
  422. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  423. $rule = $objectManager->create('\Magento\CatalogRule\Model\RuleFactory')->create();
  424. $discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
  425. return $discountAmount;
  426. }
  427.  
  428. public function getSolitaireProductBreakDown($productId, $diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null)
  429. {
  430. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
  431. $logger = new \Zend_Log();
  432. $logger->addWriter($writer);
  433. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  434. if($pcnumber)
  435. {
  436. $mainProduct = $this->getProductByAttributes($pcnumber,$size);
  437. }
  438. else
  439. {
  440. $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  441. }
  442. //$mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  443.  
  444. // $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->addFieldToFilter('variant_id', array('like' => "%$variantId%"))->getData();
  445. $productRingSize = $ringSize != '' ? $ringSize : ($mainProduct->getResource()->getAttribute('ring_size')->getFrontend()->getValue($mainProduct) ?? 0);
  446. $variantId = $mainProduct->getData('display_variant') ?? null;
  447. $glowSku = '';
  448. if($goldColor !=null && $goldColor !='undefined')
  449. {
  450. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  451. ->addFieldToFilter('product_id', $productId)
  452. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  453. // ->addFieldToFilter('color', array('like' => "%$goldColor%"))
  454. ->getData();
  455. }
  456. else if($carat !=null)
  457. {
  458. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  459. ->addFieldToFilter('product_id', $productId)
  460. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  461. // ->addFieldToFilter('carat', array('like' => "%$carat%"))
  462. ->getData();
  463. }
  464. else
  465. {
  466. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  467. ->addFieldToFilter('product_id', $productId)
  468. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  469. ->getData();
  470. // return $productConstituents;
  471. }
  472. $productCalclator = $objectManager->create('\Froogal\Catalog\Model\ProductPriceCalculator');
  473. $finalPrice = $productCalclator->getFinalPrice($productId,$diamondId);
  474. // return $finalPrice;
  475. $discountAmount = $this->getProductDiscount($mainProduct);
  476. // return $discountAmount;
  477. $regularPrice = 0;
  478. if($variantId)
  479. {
  480. $productVariant = $objectManager->create('Froogal\Catalog\Model\ProductVariant')->load($variantId);
  481. $glowSku = $productVariant['glow_sku'];
  482. }
  483. if ($customOptionId) {
  484. if($discountAmount)
  485. {
  486. // $regularPrice = $mainProduct->getPriceInfo()->getPrice('regular_price')->getValue();
  487. $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
  488. }
  489. else
  490. {
  491. $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
  492. }
  493.  
  494. } else {
  495. $finalPrice = $mainProduct->getFinalPrice();
  496. }
  497. $finalArray = [];
  498. if(!empty($productConstituents))
  499. {
  500. $countryCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID);
  501. $regionCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID);
  502.  
  503. $customerTaxClassId = $this->scopeConfigInterface->getValue('tax/classes/default_customer_tax_class');
  504. /** @var \Magento\Catalog\Model\Product $product */
  505. $productTaxClassId = $mainProduct->getData('tax_class_id');
  506.  
  507. // THE ACTUAL CALCULATION CALL
  508.  
  509. /** @var \Magento\Tax\Model\Calculation $taxCalculation */
  510. $taxPercent = $this->taxCalculation->getRate(
  511. new \Magento\Framework\DataObject(
  512. [
  513. 'country_id' => $countryCode,
  514. 'region_id' => $regionCode,
  515. 'customer_class_id' => $customerTaxClassId,
  516. 'product_class_id' => $productTaxClassId
  517. ]
  518. )
  519. );
  520.  
  521. $diamondCarats = 0;
  522. $diamondPrice = 0;
  523. $goldWeight = 0;
  524. $goldPrice = 0;
  525. $gemstoneCarats = 0;
  526. $gemstonePrice = 0;
  527. $platinumWeight = 0;
  528. $platinumPrice = 0;
  529. $goldColor = 0;
  530. foreach($productConstituents as $productConstituentData)
  531. {
  532. $contituentProductId = $productConstituentData['contituent_product_id'];
  533. // $glowSku = $productConstituentData['glow_sku'] ?? '';
  534. $color = $productConstituentData['color']?? '' ;
  535. $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
  536. $contituentProductPrice = $contituentProduct->getPrice();
  537. $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
  538. if ($constituentType == "Diamond") {
  539. // $request['body']['diamond_id'] = $diamondId;
  540. $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
  541. $solitaireDetails = $response['response']['body'];
  542. if(empty($solitaireDetails))
  543. {
  544. $diamondPrice = 0;
  545. }
  546. else
  547. {
  548. $solitaireDetails = $solitaireDetails['diamond'];
  549. $diamondPrice = $solitaireDetails['total_sales_price_in_currency'] ?? '';
  550. $diamondCarats = $solitaireDetails['size'];
  551. }
  552.  
  553. }
  554.  
  555. if ($constituentType == "Gold") {
  556. $goldWeight += $productConstituentData['qty'];
  557.  
  558. if ($ringSize!='undefined' && $ringSize != $productRingSize) {
  559. $sizeDifference = max(0, $ringSize - $productRingSize);
  560. $weightDifference = $sizeDifference * 0.1;
  561. if ($weightDifference) {
  562. $goldWeight += $weightDifference;
  563. }
  564. }
  565. $goldPrice += $contituentProductPrice * ((float) $goldWeight);
  566. } elseif ($constituentType == "Gemstone") {
  567. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  568. $gemstoneCarats += $carats;
  569. $gemstonePrice += $contituentProductPrice * $carats;
  570. } elseif ($constituentType == "Platinum") {
  571. $platinumWeight = $productConstituentData['qty'];
  572. $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
  573. }
  574. }
  575. // return $finalPrice;
  576. $constituentsTotal = 0;
  577. $content = [];
  578. $productDetails=[
  579. 'variant_carat'=> "({$diamondCarats}g)",
  580. 'variant_weight'=> $goldWeight,
  581. 'variant_sku'=> $glowSku,
  582. 'variant_color'=> $color
  583. ];
  584. if ($goldPrice) {
  585. $content[] = [
  586. 'variant_type' => "Gold ({$goldWeight}g)",
  587. 'variant_price' => $goldPrice,
  588. ];
  589. $constituentsTotal += $goldPrice;
  590. }
  591. if ($diamondPrice) {
  592. $content[] = [
  593. 'variant_type' => "Diamonds ({$diamondCarats}cts)",
  594. 'variant_price' => $diamondPrice,
  595. ];
  596. $constituentsTotal += $diamondPrice;
  597. }
  598. if ($gemstonePrice) {
  599. $content[] = [
  600. 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
  601. 'variant_price' => $gemstonePrice,
  602. ];
  603. $constituentsTotal += $gemstonePrice;
  604. }
  605. if ($platinumPrice) {
  606. $content[] = [
  607. 'variant_type' => "Platinum ({$platinumWeight}g)",
  608. 'variant_price' => $platinumPrice,
  609. ];
  610. $constituentsTotal += $platinumPrice;
  611. }
  612. $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
  613. if ($gemstoneAttributePrice) {
  614. $content[] = [
  615. 'variant_type' => "Gemstones",
  616. 'variant_price' => $gemstoneAttributePrice,
  617. ];
  618. $constituentsTotal += $gemstoneAttributePrice;
  619. }
  620. $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
  621. $taxAmount = round($finalPrice - $netAmount);
  622.  
  623. $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
  624.  
  625. $finalDiscountAmount = max(0,$regularPrice - $discountAmount);
  626. // return $finalPrice;
  627. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  628. $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
  629. $storeId = $objectManager->create('\Froogal\Catalog\Cron\PricingCalculation')->getStoreId();
  630.  
  631. $updateAttributes['solitaire_final_price'] = $finalPrice;
  632. $action->updateAttributes([$productId], $updateAttributes, $storeId);
  633.  
  634. $finalArray = [
  635. 'content' => $content,
  636. 'productDetails' => $productDetails,
  637. 'totalPrice' => $finalPrice,
  638. 'discountAmount' => $finalDiscountAmount,
  639. 'valueAddition' => $valueAddition,
  640. 'tax' => $taxAmount,
  641. ];
  642. }
  643.  
  644. return $finalArray;
  645. }
  646.  
  647. public function getProductByAttributes($pcnumber,$size)
  648. {
  649. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  650. $outletInventory = $objectManager->create('Froogal\Outlets\Model\ResourceModel\OutletInventory\CollectionFactory')->create()->addFieldToSelect('*')->addFieldToFilter('pc_number',$pcnumber)->addFieldToFilter('size',$size)->setOrder('id', 'asc')->getFirstItem();
  651. if($outletInventory)
  652. {
  653. $glowSku = $outletInventory->getData('glow_sku');
  654. $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('glow_sku',$glowSku);
  655. return $mainProduct;
  656. }
  657. }
  658.  
  659.  
  660. public function getRules($date, $websiteId, $customerGroupId, $productId)
  661. {
  662.  
  663. /** @var [] $rules catalog rules */
  664. $rules = $this->ruleResource->getRulesFromProduct($date, $websiteId, $customerGroupId, $productId);
  665. return $rules;
  666. }
  667.  
  668.  
  669. }
  670.  
Advertisement
Add Comment
Please, Sign In to add comment