Advertisement
baby_in_magento

ProductPricecalculator.php

Nov 28th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.91 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 ProductPriceCalculator 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 = $mainProduct->getResource()->getAttribute('gold_color')->getFrontend()->getValue($mainProduct);
  198. if($productConstituentData['color'])
  199. {
  200. $color = $mainProduct->getData('gold_color');
  201. }
  202. $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
  203. $contituentProductPrice = $contituentProduct->getPrice();
  204. $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
  205. if ($constituentType == "Diamond") {
  206. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  207. $diamondCarats += $carats;
  208. if($carat == $carats)
  209. {
  210. $diamondCarats = $carats;
  211. }
  212. // $diamondPrice += $contituentProductPrice * $carats;
  213. // $diamondPrice = $finalPrice;
  214. $diamondPrice = 1;
  215.  
  216. } elseif ($constituentType == "Gold") {
  217. $goldWeight += $productConstituentData['qty'];
  218. if ($ringSize!='undefined' && $ringSize != $productRingSize && $goldColor == $color) {
  219. $goldWeight = $productConstituentData['qty'];
  220. $sizeDifference = max(0, (float)$ringSize - (float)$productRingSize);
  221. $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
  222. if ($weightDifference) {
  223. $goldWeight += $weightDifference;
  224. }
  225. }
  226. else if ($ringSize!='undefined' && $ringSize != $productRingSize) {
  227. $sizeDifference = max(0, (int)$ringSize - (int)$productRingSize);
  228. $weightDifference = $sizeDifference * self::RINGSIZE_AMOUNT_INCREMENT;
  229. if ($weightDifference) {
  230. $goldWeight += $weightDifference;
  231. }
  232. }
  233. $goldPrice += $contituentProductPrice * ((float) $goldWeight);
  234. } elseif ($constituentType == "Gemstone") {
  235. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  236. $gemstoneCarats += $carats;
  237. $gemstonePrice += $contituentProductPrice * $carats;
  238. } elseif ($constituentType == "Platinum") {
  239. $platinumWeight = $productConstituentData['qty'];
  240. $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
  241. }
  242. }
  243. if ($productVariant === 'solitaire') {
  244. $productBreakDown = $this->getSolitaireProductBreakDown($productId,$diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null);
  245. return $this->helperData->success('Product BreakDown', $productBreakDown);
  246. }
  247. $constituentsTotal = 0;
  248.  
  249. $gemstonePrice = $mainProduct->getResource()->getAttribute('gemstone_price')->getFrontend()->getValue($mainProduct);
  250. $valueAddition = ($mainProduct->getResource()->getAttribute('making_charges_value')->getFrontend()->getValue($mainProduct))*($mainProduct->getResource()->getAttribute('gold_weight')->getFrontend()->getValue($mainProduct));
  251. $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
  252. $taxAmount = round($finalPrice - $netAmount);
  253. // display 80 % of tax code
  254. // $taxAmount = round(($finalPrice - $netAmount) * 70 / 100);
  255. // display 80 % of tax code
  256.  
  257.  
  258. $content = [];
  259. $productDetails=[
  260. 'variant_carat'=> "({$carats}g)",
  261. 'variant_weight'=> $goldWeight,
  262. 'variant_sku'=> $glowSku,
  263. 'variant_color'=> $color
  264. ];
  265. if ($goldPrice) {
  266. $content[] = [
  267. 'variant_type' => "Gold ({$goldWeight}g)",
  268. 'variant_price' => $goldPrice,
  269. ];
  270. $constituentsTotal += $goldPrice;
  271. }
  272. if ($diamondPrice) {
  273. // $diamondPrice = $diamondPrice + $diffPrice;
  274. // $diamondPrice = $diamondPrice;
  275. // diamond value original tax//
  276. $diamondPrice = $finalPrice - ($goldPrice + $valueAddition + $taxAmount + $gemstonePrice + $platinumPrice ) ;
  277.  
  278. //$diamondPrice = (($taxAmount) * (20 / 100))+ ($finalPrice - ($goldPrice + $valueAddition + $taxAmount + $gemstonePrice + $platinumPrice )) ;
  279.  
  280.  
  281. $content[] = [
  282. 'variant_type' => "Diamonds ({$diamondCarats}cts)",
  283. 'variant_price' => $diamondPrice,
  284. ];
  285. $constituentsTotal += $diamondPrice;
  286. }
  287. if ($gemstonePrice) {
  288. $content[] = [
  289. // 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
  290. // 'variant_price' => $gemstonePrice,
  291. ];
  292. // $constituentsTotal += $gemstonePrice;
  293. }
  294. if ($platinumPrice) {
  295. $content[] = [
  296. 'variant_type' => "Platinum ({$platinumWeight}g)",
  297. 'variant_price' => $platinumPrice,
  298. ];
  299. $constituentsTotal += $platinumPrice;
  300. }
  301. $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
  302. if ($gemstoneAttributePrice) {
  303. $content[] = [
  304. 'variant_type' => "Gemstones",
  305. 'variant_price' => $gemstoneAttributePrice,
  306. ];
  307. $constituentsTotal += $gemstoneAttributePrice;
  308. }
  309. $logger->info('constituentsTotal'.json_encode($constituentsTotal));
  310. $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
  311. // $taxAmount = round($finalPrice - $netAmount);
  312. // 80 percent add
  313. $taxAmount = round(($finalPrice - $netAmount) * 80 / 100);
  314. //80 percent done
  315. // $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
  316. // $valueAddition = $netAmount - $constituentsTotal;
  317. $valueAddition = ($mainProduct->getResource()->getAttribute('making_charges_value')->getFrontend()->getValue($mainProduct))*($mainProduct->getResource()->getAttribute('gold_weight')->getFrontend()->getValue($mainProduct));
  318.  
  319.  
  320. $logger->info('valueAddition='.$valueAddition.'netamount=' .$netAmount .'constituentsTotal='.$constituentsTotal.'taxamount='.$taxAmount);
  321. $finalDiscountAmount = max(0,$regularPrice - $originalPrice);
  322. $makingChargesDiscountValue = round(max($regularPrice - $finalPrice,0));
  323.  
  324.  
  325. // $finalDiscountAmount = round(max($valueAddition * ($action_amount / 100),0));
  326. $discountedTaxAmount = 0;
  327. if($makingDiscountRule)
  328. {
  329. $makingChargesDiscountValue = round(max($valueAddition * ($action_amount / 100),0));
  330. $makingTaxOnDiscount = $makingChargesDiscountValue * ($taxPercent / 100);
  331. $discountedTaxAmount = $taxAmount - $makingTaxOnDiscount;
  332. }
  333.  
  334. $finalArray = [
  335. 'content' => $content,
  336. 'productDetails' => $productDetails,
  337. 'totalPrice' => $finalPrice,
  338. 'discountAmount' => $finalDiscountAmount,
  339. 'product_discount' => $finalDiscountAmount,
  340. 'valueAddition' => $valueAddition,
  341. 'tax' => $taxAmount,
  342. 'effectiveTax' => $discountedTaxAmount
  343. ];
  344. }
  345. return $this->helperData->success('Product BreakDown', $finalArray);
  346. }
  347.  
  348. public function getOptionPrice($product,$finalPrice,$ringSize,$customOptionId,$optionValue)
  349. {
  350. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
  351. $logger = new \Zend_Log();
  352. $logger->addWriter($writer);
  353. $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  354. $customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
  355. if (count($customOptions) > 0) {
  356. foreach ($customOptions as $options) {
  357. $optionTitle = strtolower($options->getTitle());
  358. if ($optionTitle == 'ring size') {
  359. $optionData = $options->getValues();
  360. foreach ($optionData as $data) {
  361.  
  362. $size = $data->getTitle();
  363. $optionTypeId = $data->getData('option_type_id');
  364. if($optionTypeId == $optionValue)
  365. {
  366. // $logger->info('optiontypeid=' . $optionTypeId . 'optionvalue=' . $optionValue . 'size=' . $size . 'ringsize=' . $ringSize);
  367.  
  368. $customOptionPrice = $data->getPrice();
  369. $finalPrice = $finalPrice + $customOptionPrice;
  370. $logger->info('customOptionPrice='.$customOptionPrice.'finalprice=' . $finalPrice);
  371. }
  372.  
  373. }
  374. }
  375. }
  376. }
  377. return $finalPrice;
  378. }
  379. public function getSolitaireProductDetails($productId,$diamondId)
  380. {
  381. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  382. $productRelation = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->getData();
  383. $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
  384. $solitaireDetails = $response['response']['body'];
  385. if(empty($solitaireDetails))
  386. {
  387. return $this->helperData->error('No Diamond was found',$solitaireDetails);
  388. }
  389. $solitaireDetails = $response['response']['body']['diamond'] ?? null;
  390. $data = [
  391. ['value' => 'Diamond','label' => 'Constituent Type','attribute_code' => 'constituent_type'],
  392. ['value' => $solitaireDetails['diamond_id'] ?? '','label' => 'DiamondID','attribute_code' => 'diamond_id'],
  393. ['value' => $solitaireDetails['shape'] ?? '','label' => 'Shape','attribute_code' => 'shape'],
  394. ['value' => $solitaireDetails['size'] ?? '','label' => 'Size','attribute_code' => 'carat'],
  395. ['value' => $solitaireDetails['color'] ?? '','label' => 'Color','attribute_code' => 'color'],
  396. ['value' => $solitaireDetails['clarity'] ?? '','label' => 'Quality','attribute_code' => 'quality'],
  397. ['value' => $solitaireDetails['cut'] ?? '','label' => 'Cut','attribute_code' => 'cut'],
  398. ['value' => $solitaireDetails['stock_num'] ?? '','label' => 'Style Code','attribute_code' => 'style_code'],
  399. ['value' => $solitaireDetails['symmetry'] ?? '','label' => 'Symmetry','attribute_code' => 'symmetry'],
  400. ['value' => $solitaireDetails['currency_symbol'] ?? '','Symbol' => 'DiamondID','attribute_code' => 'currency_symbol'],
  401. ['value' => $solitaireDetails['total_sales_price_in_currency'] ?? '','label' => 'Diamond Price','attribute_code' => 'total_sales_price_in_currency']
  402. ];
  403.  
  404. $content = [];
  405. $finalArray = [];
  406. if (!empty($productRelation)) {
  407.  
  408. foreach ($productRelation as $productRelationValue) {
  409. $masterProductId = $productRelationValue['contituent_product_id'];
  410.  
  411. $qty = $productRelationValue['qty'];
  412. $carat = $productRelationValue['carat'];
  413.  
  414. $product = $objectManager->create('Magento\Catalog\Model\Product')->load($masterProductId);
  415. $price = $product->getPrice();
  416. $weightUnit = $this->scopeConfigInterface->getValue(
  417. 'general/locale/weight_unit',
  418. ScopeInterface::SCOPE_STORE
  419. );
  420.  
  421. $constituentType = $product->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($product);
  422.  
  423. if ($constituentType == 'Gold') {
  424. // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
  425. // $data[] = ['value' => $productRelationValue['qty'],'label' => 'Gold Weight','attribute_code' => 'gold_weight'];
  426. // $data[] = ['value' => $productRelationValue['carat'],'label' => 'Gold Carat','attribute_code' => 'gold_carat'];
  427. // $data[] = ['value' => $productRelationValue['color'],'label' => 'Gold Color','attribute_code' => 'gold_color'];
  428. }
  429. else if ($constituentType == 'Platinum') {
  430. // $data[] = ['value' => $productRelationValue['glow_sku'],'label' => 'Glow Sku','attribute_code' => 'glow_sku'];
  431. }
  432. else if ($constituentType == 'Gemstone') {
  433. $carats = ((float) $productRelationValue['qty']) * ($productRelationValue['carat'] ?? 1);
  434. $gemstoneCarats += $carats;
  435. // $data[] = ['value' => $gemstoneCarats,'label' => 'GemStone Weight','attribute_code' => 'gemstone_weight'];
  436. }
  437. }
  438. }
  439. // array_push($content, $data);
  440. return $this->helperData->success('Solitaire Product Details',$data);
  441.  
  442. }
  443.  
  444. protected function getProductDiscount($product)
  445. {
  446.  
  447. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  448. $rule = $objectManager->create('\Magento\CatalogRule\Model\RuleFactory')->create();
  449. $discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
  450. return $discountAmount;
  451. }
  452.  
  453. public function getSolitaireProductBreakDown($productId, $diamondId,$customOptionId,$optionValue,$ringSize,$goldColor=null,$carat=null,$pcnumber=null,$size=null)
  454. {
  455. $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
  456. $logger = new \Zend_Log();
  457. $logger->addWriter($writer);
  458. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  459. if($pcnumber)
  460. {
  461. $mainProduct = $this->getProductByAttributes($pcnumber,$size);
  462. }
  463. else
  464. {
  465. $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  466. }
  467. //$mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
  468.  
  469. // $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()->addFieldToFilter('product_id', $productId)->addFieldToFilter('variant_id', array('like' => "%$variantId%"))->getData();
  470. $productRingSize = $ringSize != '' ? $ringSize : ($mainProduct->getResource()->getAttribute('ring_size')->getFrontend()->getValue($mainProduct) ?? 0);
  471. $variantId = $mainProduct->getData('display_variant') ?? null;
  472. $glowSku = '';
  473. if($goldColor !=null && $goldColor !='undefined')
  474. {
  475. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  476. ->addFieldToFilter('product_id', $productId)
  477. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  478. // ->addFieldToFilter('color', array('like' => "%$goldColor%"))
  479. ->getData();
  480. }
  481. else if($carat !=null)
  482. {
  483. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  484. ->addFieldToFilter('product_id', $productId)
  485. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  486. // ->addFieldToFilter('carat', array('like' => "%$carat%"))
  487. ->getData();
  488. }
  489. else
  490. {
  491. $productConstituents = $objectManager->create('Froogal\Catalog\Model\ProductRelation')->getCollection()
  492. ->addFieldToFilter('product_id', $productId)
  493. ->addFieldToFilter('variant_id', array('like' => "%$variantId%"))
  494. ->getData();
  495. // return $productConstituents;
  496. }
  497. $productCalclator = $objectManager->create('\Froogal\Catalog\Model\ProductPriceCalculator');
  498. $finalPrice = $productCalclator->getFinalPrice($productId,$diamondId);
  499. // return $finalPrice;
  500. $discountAmount = $this->getProductDiscount($mainProduct);
  501. // return $discountAmount;
  502. $regularPrice = 0;
  503. if($variantId)
  504. {
  505. $productVariant = $objectManager->create('Froogal\Catalog\Model\ProductVariant')->load($variantId);
  506. $glowSku = $productVariant['glow_sku'];
  507. }
  508. if ($customOptionId) {
  509. if($discountAmount)
  510. {
  511. // $regularPrice = $mainProduct->getPriceInfo()->getPrice('regular_price')->getValue();
  512. $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
  513. }
  514. else
  515. {
  516. $finalPrice = $this->getOptionPrice($mainProduct,$finalPrice,$productRingSize,$customOptionId,$optionValue);
  517. }
  518.  
  519. } else {
  520. $finalPrice = $mainProduct->getFinalPrice();
  521. }
  522. $finalArray = [];
  523. if(!empty($productConstituents))
  524. {
  525. $countryCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID);
  526. $regionCode = $this->scopeConfigInterface->getValue(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID);
  527.  
  528. $customerTaxClassId = $this->scopeConfigInterface->getValue('tax/classes/default_customer_tax_class');
  529. /** @var \Magento\Catalog\Model\Product $product */
  530. $productTaxClassId = $mainProduct->getData('tax_class_id');
  531.  
  532. // THE ACTUAL CALCULATION CALL
  533.  
  534. /** @var \Magento\Tax\Model\Calculation $taxCalculation */
  535. $taxPercent = $this->taxCalculation->getRate(
  536. new \Magento\Framework\DataObject(
  537. [
  538. 'country_id' => $countryCode,
  539. 'region_id' => $regionCode,
  540. 'customer_class_id' => $customerTaxClassId,
  541. 'product_class_id' => $productTaxClassId
  542. ]
  543. )
  544. );
  545.  
  546. $diamondCarats = 0;
  547. $diamondPrice = 0;
  548. $goldWeight = 0;
  549. $goldPrice = 0;
  550. $gemstoneCarats = 0;
  551. $gemstonePrice = 0;
  552. $platinumWeight = 0;
  553. $platinumPrice = 0;
  554. $goldColor = 0;
  555. foreach($productConstituents as $productConstituentData)
  556. {
  557. $contituentProductId = $productConstituentData['contituent_product_id'];
  558. // $glowSku = $productConstituentData['glow_sku'] ?? '';
  559. $color = $productConstituentData['color']?? '' ;
  560. $contituentProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($contituentProductId);
  561. $contituentProductPrice = $contituentProduct->getPrice();
  562. $constituentType = $contituentProduct->getResource()->getAttribute('constituent_type')->getFrontend()->getValue($contituentProduct);
  563. if ($constituentType == "Diamond") {
  564. // $request['body']['diamond_id'] = $diamondId;
  565. $response = $this->productSolitaireInterface->getSolitaireItemRate($diamondId);
  566. $solitaireDetails = $response['response']['body'];
  567. if(empty($solitaireDetails))
  568. {
  569. $diamondPrice = 0;
  570. }
  571. else
  572. {
  573. $solitaireDetails = $solitaireDetails['diamond'];
  574. $diamondPrice = $solitaireDetails['total_sales_price_in_currency'] ?? '';
  575. $diamondCarats = $solitaireDetails['size'];
  576. }
  577.  
  578. }
  579.  
  580. if ($constituentType == "Gold") {
  581. $goldWeight += $productConstituentData['qty'];
  582.  
  583. if ($ringSize!='undefined' && $ringSize != $productRingSize) {
  584. $sizeDifference = max(0, $ringSize - $productRingSize);
  585. $weightDifference = $sizeDifference * 0.1;
  586. if ($weightDifference) {
  587. $goldWeight += $weightDifference;
  588. }
  589. }
  590. $goldPrice += $contituentProductPrice * ((float) $goldWeight);
  591. } elseif ($constituentType == "Gemstone") {
  592. $carats = ((float) $productConstituentData['qty']) * ($productConstituentData['carat'] ?? 1);
  593. $gemstoneCarats += $carats;
  594. $gemstonePrice += $contituentProductPrice * $carats;
  595. } elseif ($constituentType == "Platinum") {
  596. $platinumWeight = $productConstituentData['qty'];
  597. $platinumPrice += $contituentProductPrice * ((float) $platinumWeight);
  598. }
  599. }
  600. // return $finalPrice;
  601. $constituentsTotal = 0;
  602. $content = [];
  603. $productDetails=[
  604. 'variant_carat'=> "({$diamondCarats}g)",
  605. 'variant_weight'=> $goldWeight,
  606. 'variant_sku'=> $glowSku,
  607. 'variant_color'=> $color
  608. ];
  609. if ($goldPrice) {
  610. $content[] = [
  611. 'variant_type' => "Gold ({$goldWeight}g)",
  612. 'variant_price' => $goldPrice,
  613. ];
  614. $constituentsTotal += $goldPrice;
  615. }
  616. if ($diamondPrice) {
  617. $content[] = [
  618. 'variant_type' => "Diamonds ({$diamondCarats}cts)",
  619. 'variant_price' => $diamondPrice,
  620. ];
  621. $constituentsTotal += $diamondPrice;
  622. }
  623. if ($gemstonePrice) {
  624. $content[] = [
  625. 'variant_type' => "Gemstones ({$gemstoneCarats}cts)",
  626. 'variant_price' => $gemstonePrice,
  627. ];
  628. $constituentsTotal += $gemstonePrice;
  629. }
  630. if ($platinumPrice) {
  631. $content[] = [
  632. 'variant_type' => "Platinum ({$platinumWeight}g)",
  633. 'variant_price' => $platinumPrice,
  634. ];
  635. $constituentsTotal += $platinumPrice;
  636. }
  637. $gemstoneAttributePrice = (float) $mainProduct->getData('gemstone_price');
  638. if ($gemstoneAttributePrice) {
  639. $content[] = [
  640. 'variant_type' => "Gemstones",
  641. 'variant_price' => $gemstoneAttributePrice,
  642. ];
  643. $constituentsTotal += $gemstoneAttributePrice;
  644. }
  645. $netAmount = round($finalPrice * 100 / (100 + $taxPercent));
  646. $taxAmount = round($finalPrice - $netAmount);
  647. $valueAddition = round(max($netAmount - $constituentsTotal, 0),2);
  648. $finalDiscountAmount = max(0,$regularPrice - $discountAmount);
  649. // return $finalPrice;
  650. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  651. $action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
  652. $storeId = $objectManager->create('\Froogal\Catalog\Cron\PricingCalculation')->getStoreId();
  653.  
  654. $updateAttributes['solitaire_final_price'] = $finalPrice;
  655. $action->updateAttributes([$productId], $updateAttributes, $storeId);
  656.  
  657. $finalArray = [
  658. 'content' => $content,
  659. 'productDetails' => $productDetails,
  660. 'totalPrice' => $finalPrice,
  661. 'discountAmount' => $finalDiscountAmount,
  662. 'valueAddition' => $valueAddition,
  663. 'tax' => $taxAmount,
  664. ];
  665. }
  666.  
  667. return $finalArray;
  668. }
  669.  
  670. public function getProductByAttributes($pcnumber,$size)
  671. {
  672. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  673. $outletInventory = $objectManager->create('Froogal\Outlets\Model\ResourceModel\OutletInventory\CollectionFactory')->create()->addFieldToSelect('*')->addFieldToFilter('pc_number',$pcnumber)->addFieldToFilter('size',$size)->setOrder('id', 'asc')->getFirstItem();
  674. if($outletInventory)
  675. {
  676. $glowSku = $outletInventory->getData('glow_sku');
  677. $mainProduct = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('glow_sku',$glowSku);
  678. return $mainProduct;
  679. }
  680. }
  681.  
  682.  
  683. public function getRules($date, $websiteId, $customerGroupId, $productId)
  684. {
  685.  
  686. /** @var [] $rules catalog rules */
  687. $rules = $this->ruleResource->getRulesFromProduct($date, $websiteId, $customerGroupId, $productId);
  688. return $rules;
  689. }
  690.  
  691.  
  692. }
  693.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement