Save.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal;
  3. class Save extends \Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal
  4. {
  5. /**
  6. * Backend session
  7. *
  8. * @var \Magento\Backend\Model\Session
  9. */
  10. protected $backendSession;
  11. /**
  12. * Date filter
  13. *
  14. * @var \Magento\Framework\Stdlib\DateTime\Filter\Date
  15. */
  16. protected $dateFilter;
  17. protected $productFactory;
  18. /**
  19. * constructor
  20. *
  21. * @param \Magento\Backend\Model\Session $backendSession
  22. * @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
  23. * @param \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory
  24. * @param \Magento\Framework\Registry $registry
  25. * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
  26. * @param \Magento\Backend\App\Action\Context $context
  27. */
  28. public function __construct(
  29. \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
  30. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  31. \Magento\Framework\Registry $registry,
  32. \Magento\Backend\App\Action\Context $context,
  33. \Magento\Catalog\Model\ProductFactory $productFactory
  34. ) {
  35. parent::__construct($dailydealFactory, $registry, $context);
  36. $this->backendSession = $context->getSession();
  37. $this->resultRedirectFactory=$context->getResultRedirectFactory();
  38. $this->productFactory = $productFactory;
  39. $this->dateFilter = $dateFilter;
  40. }
  41. /**
  42. * run the action
  43. *
  44. * @return \Magento\Backend\Model\View\Result\Redirect
  45. */
  46. public function execute()
  47. {
  48. $data = $this->getRequest()->getPost('dailydeal');
  49. if (isset($data["dailydeal_id"])) {
  50. $dailydealId=$data["dailydeal_id"];
  51. }
  52. // Store the date from and to in to varaible
  53. $fromdate=$data["sw_date_from"];
  54. $todate= $data["sw_date_to"];
  55. $resultRedirect = $this->resultRedirectFactory->create();
  56. if ($data) {
  57. $data = $this->filterData($data);
  58. $dailydeal = $this->initDailydeal();
  59. $dailydeal->setData($data);
  60. $this->_eventManager->dispatch(
  61. 'sw_dailydeals_dailydeal_prepare_save',
  62. [
  63. 'dailydeal' => $dailydeal,
  64. 'request' => $this->getRequest()
  65. ]
  66. );
  67. try {
  68. $dailydealCollection=$this->dailydealFactory->create()->getCollection();
  69. $dailydealCollection->addFieldToSelect('*');
  70. $dailydealCollection->addFieldToFilter('sw_product_sku', ['eq'=>$data["sw_product_sku"]]);
  71. if (isset($dailydealId)) {
  72. $dailydealCollection->addFieldToFilter('dailydeal_id', ['eq'=>$dailydealId]);
  73. if ($dailydealCollection->getSize()==1) {
  74. $editaction=1;
  75. }
  76. }
  77. if ($dailydealCollection->getSize()== 0 || isset($editaction)) {
  78. if ($data["sw_deal_enable"] == 1) {
  79. $productCollection=$this->productFactory->create()->getCollection();
  80. $product=$productCollection->addAttributeToSelect('*');
  81. $product=$productCollection->addAttributeToFilter('sku', ['eq'=>$data["sw_product_sku"]]);
  82. $finalproductprice=$product->getFirstItem()->getFinalPrice();
  83. if ($product->getFirstItem()->getTypeId() != "bundle") {
  84. if ($data["sw_discount_type"] == 1) { // For Fixed
  85. $dailydeal->setSwProductPrice($finalproductprice - $data["sw_discount_amount"]);
  86. } elseif ($data["sw_discount_type"] == 2) { // For Percentage
  87. $dailydeal->setSwProductPrice($finalproductprice - (($finalproductprice * $data["sw_discount_amount"])/100));
  88. }
  89. } else {
  90. $dailydeal->setSwProductPrice(1);
  91. }
  92. }
  93. $dailydeal->setSwDateFrom($fromdate);
  94. $dailydeal->setSwDateTo($todate);
  95. $dailydeal->save();
  96. $this->messageManager->addSuccess(__('The Dailydeal has been saved.'));
  97. } else {
  98. $this->messageManager->addError("Already set dailydeal for this Product.");
  99. }
  100. $this->backendSession->setSwDailydealsDailydealData(false);
  101. if ($this->getRequest()->getParam('back')) {
  102. $resultRedirect->setPath(
  103. 'sw_dailydeals/*/edit',
  104. [
  105. 'dailydeal_id' => $dailydeal->getId(),
  106. '_current' => true
  107. ]
  108. );
  109. return $resultRedirect;
  110. }
  111. $resultRedirect->setPath('sw_dailydeals/*/');
  112. return $resultRedirect;
  113. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  114. $this->messageManager->addError($e->getMessage());
  115. } catch (\RuntimeException $e) {
  116. $this->messageManager->addError($e->getMessage());
  117. } catch (\Exception $e) {
  118. $this->messageManager->addException($e, __('Something went wrong while saving the Dailydeal.'));
  119. }
  120. $this->_getSession()->setSwDailydealsDailydealData($data);
  121. $resultRedirect->setPath(
  122. 'sw_dailydeals/*/edit',
  123. [
  124. 'dailydeal_id' => $dailydeal->getId(),
  125. '_current' => true
  126. ]
  127. );
  128. return $resultRedirect;
  129. }
  130. $resultRedirect->setPath('sw_dailydeals/*/');
  131. return $resultRedirect;
  132. }
  133. /**
  134. * filter values
  135. *
  136. * @param array $data
  137. * @return array
  138. */
  139. protected function filterData($data)
  140. {
  141. $inputFilter = new \Zend_Filter_Input(
  142. [
  143. 'sw_date_from' => $this->dateFilter,
  144. 'sw_date_to' => $this->dateFilter,
  145. ],
  146. [],
  147. $data
  148. );
  149. $data = $inputFilter->getUnescaped();
  150. return $data;
  151. }
  152. }