baby_in_magento

fek

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