baby_in_magento

Untitled

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