PageRenderTime 25ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/PerksManageController.php

https://gitlab.com/melentev-av/perki.dev
PHP | 229 lines | 179 code | 43 blank | 7 comment | 24 complexity | 2cdc8faa9df6408e237be02318bcfba8 MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sinersis
  5. * Date: 25.05.16
  6. * Time: 15:09
  7. */
  8. class PerksManageController extends BaseController
  9. {
  10. public function elListAction()
  11. {
  12. $oCompany = Auth::user();
  13. if (Request::ajax()) {
  14. $aGiftsEl = Gift_Code::where('type', '=', 'el')
  15. ->where('company_id', '=', Auth::user()->id)
  16. ->with(array('user.params' => function ($query) {
  17. $query->whereIn('param', array('contactName'));
  18. }))
  19. ->orderBy('created_at', 'DESC')->get();
  20. $aReturn = array('data' => array());
  21. foreach ($aGiftsEl as $oGiftCode) {
  22. $aReturn['data'][] = array(
  23. $oGiftCode->id,
  24. Carbon::createFromTimestamp(strtotime($oGiftCode->created_at))->format(trans('interface.offer.format.valid_by')),
  25. $oGiftCode->offer->sid,
  26. $oGiftCode->discount . '%',
  27. $oGiftCode->client,
  28. '<span class="label label-sm label-' . trans('interface.gift.el.label.' . $oGiftCode->status) . ' arrowed-in arrowed-in-right">' . trans('interface.gift.el.status.' . $oGiftCode->status) . '</span>'
  29. );
  30. }
  31. return $aReturn;
  32. }
  33. $aViewData = array();
  34. $aOffers = Offer::where('company_id', $oCompany->id)->where('active', 1)->whereStatus('active')->get();
  35. $aViewData['aOffers'] = $aOffers;
  36. if ($old = Input::old('errors')) {
  37. $aViewData['errors'] = $old;
  38. }
  39. $aCompanyDbParams = $oCompany->params()->whereIn('param', array('loyaltySum', 'title'))->get();
  40. $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
  41. $aViewData['companyName'] = array_get($aCompanyParams, 'title', trans('interface.gift.empty.company.name'));
  42. return View::make('company.gift.list.el', $aViewData);
  43. }
  44. public function printedListAction()
  45. {
  46. }
  47. public function smsListAction()
  48. {
  49. $oCompany = Auth::user();
  50. if (Request::ajax()) {
  51. $aGiftsEl = Gift_Code::where('type', '=', 'sms')
  52. ->where('company_id', '=', Auth::user()->id)
  53. ->with(array('user.params' => function ($query) {
  54. $query->whereIn('param', array('contactName'));
  55. }))
  56. ->orderBy('created_at', 'DESC')->get();
  57. $aReturn = array('data' => array());
  58. foreach ($aGiftsEl as $oGiftCode) {
  59. $aReturn['data'][] = array(
  60. $oGiftCode->id,
  61. Carbon::createFromTimestamp(strtotime($oGiftCode->created_at))->format(trans('interface.offer.format.valid_by')),
  62. $oGiftCode->offer_id,
  63. $oGiftCode->discount . '%',
  64. $oGiftCode->client,
  65. '<span class="label label-sm label-' . trans('interface.gift.el.label.' . $oGiftCode->status) . ' arrowed-in arrowed-in-right">' . trans('interface.gift.el.status.' . $oGiftCode->status) . '</span>'
  66. );
  67. }
  68. return $aReturn;
  69. }
  70. $aViewData = array();
  71. $aOffers = Offer::where('company_id', $oCompany->id)->where('active', 1)->whereStatus('active')->get();
  72. $aViewData['aOffers'] = $aOffers;
  73. if ($old = Input::old('errors')) {
  74. $aViewData['errors'] = $old;
  75. }
  76. $aCompanyDbParams = $oCompany->params()->whereIn('param', array('loyaltySum', 'title'))->get();
  77. $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
  78. $aViewData['companyName'] = array_get($aCompanyParams, 'title', trans('interface.gift.empty.company.name'));
  79. return View::make('company.gift.list.sms', $aViewData);
  80. }
  81. public function elCreateAction()
  82. {
  83. if (!Request::ajax() || Input::method() !== 'POST') {
  84. return Response::make(trans('interface.errors.wrong.request'), 400);
  85. }
  86. $oCompany = Auth::user();
  87. $aGiftsData = array(
  88. 'email' => Input::get('email'),
  89. 'perk' => Input::get('perk'),
  90. );
  91. if (is_array($aGiftsData['email']) && is_array($aGiftsData['perk']) && (sizeof($aGiftsData['email']) > 0) && (sizeof($aGiftsData['email']) == sizeof($aGiftsData['perk']))) {
  92. $aCompanyDbParams = $oCompany->params()->whereIn('param', array('title', 'url', 'description'))->get();
  93. $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
  94. $aGiftsData['email'] = array_values($aGiftsData['email']);
  95. $aGiftsData['perk'] = array_values(array_map('intval', $aGiftsData['perk']));
  96. $aGiftToSend = array();
  97. $aRules = array(
  98. 'email' => 'required|email',
  99. 'perk' => 'required|exists:offers,sid'
  100. );
  101. foreach ($aGiftsData['email'] as $nKey => $sEmail) {
  102. $aGiftData = array('email' => Str::prep_input($sEmail), 'perk' => $aGiftsData['perk'][$nKey]);
  103. $oValidator = Validator::make($aGiftData, $aRules);
  104. if ($oValidator->passes()) {
  105. $oOffer = Offer::where('sid', $aGiftData['perk'])->first();
  106. $oGiftCode = new Gift_Code();
  107. $oGiftCode->company_id = $oCompany->id;
  108. $oGiftCode->code = substr(str_shuffle(str_repeat(Config::get('settings.gift.codes.el.pool'), Config::get('settings.gift.codes.el.length'))), 0, Config::get('settings.gift.codes.el.length'));
  109. $oGiftCode->type = 'el';
  110. $oGiftCode->client = $aGiftData['email'];
  111. $oGiftCode->status = 'free';
  112. $oGiftCode->user_id = null;
  113. $oGiftCode->discount = $oOffer->discount/2;
  114. $oGiftCode->commission = $oOffer->discount - $oGiftCode->discount;
  115. $oGiftCode->offer_id = $oOffer->id;
  116. $sTitle = array_get($aCompanyParams, 'title', '');
  117. if ($oGiftCode->save()) {
  118. $aGiftToSend[] = $oGiftCode->code;
  119. Mail::queue('emails.gift.el', ['link' => URL::route('entity/connect', ['entity' => $oGiftCode->sid]), 'discount' => $oGiftCode->discount, 'commission' => $oGiftCode->commission],
  120. function ($message) use ($oGiftCode, $oCompany, $sTitle) {
  121. Log::error(Config::get('settings.gift.sender.email'));
  122. $message->from(Config::get('settings.gift.sender.email'))->to($oGiftCode->client)->subject(trans('interface.gift.el.mail.subject', array('company_name' => $sTitle)));
  123. });
  124. }
  125. }
  126. }
  127. if (sizeof($aGiftToSend) > 0) {
  128. return Response::json(array('success' => true,
  129. 'message' => trans('interface.gift.el.created.success')
  130. ));
  131. } else
  132. return Response::make(trans('interface.errors.wrong.request'), 400);
  133. } else
  134. return Response::make(trans('interface.errors.wrong.request'), 400);
  135. }
  136. public function printedCreateAction()
  137. {
  138. }
  139. public function smsCreateAction()
  140. {
  141. if (!Request::ajax() || Input::method() !== 'POST') {
  142. return Response::make(trans('interface.errors.wrong.request'), 400);
  143. }
  144. $oCompany = Auth::user();
  145. $aGiftsData = array(
  146. 'phone' => Input::get('phone'),
  147. 'perk' => Input::get('perk'),
  148. );
  149. if (is_array($aGiftsData['phone']) && is_array($aGiftsData['perk']) && (sizeof($aGiftsData['phone']) > 0) && (sizeof($aGiftsData['phone']) == sizeof($aGiftsData['perk']))) {
  150. $aGiftsData['phone'] = array_values($aGiftsData['phone']);
  151. $aGiftsData['perk'] = array_values(array_map('intval', $aGiftsData['perk']));
  152. $aGiftToSend = array();
  153. $aRules = array(
  154. 'phone' => 'required|phone_ru',
  155. 'perk' => 'required|exists:offers,id'
  156. );
  157. foreach ($aGiftsData['phone'] as $nKey => $sEmail) {
  158. $aGiftData = array('phone' => Str::prep_input($sEmail), 'perk' => $aGiftsData['perk'][$nKey]);
  159. $oValidator = Validator::make($aGiftData, $aRules);
  160. if ($oValidator->passes()) {
  161. $oOffer = Offer::find($aGiftData['perk']);
  162. $oGiftCode = new Gift_Code();
  163. $oGiftCode->company_id = $oCompany->id;
  164. $oGiftCode->code = substr(str_shuffle(str_repeat(Config::get('settings.gift.codes.el.pool'), Config::get('settings.gift.codes.el.length'))), 0, Config::get('settings.gift.codes.el.length'));
  165. $oGiftCode->type = 'sms';
  166. $oGiftCode->client = $aGiftData['phone'];
  167. $oGiftCode->status = 'free';
  168. $oGiftCode->user_id = null;
  169. $oGiftCode->discount = $oOffer->discount;
  170. $oGiftCode->offer_id = $aGiftData['perk'];
  171. if ($oGiftCode->save()) {
  172. $aGiftToSend[] = $oGiftCode->code;
  173. //TODO: send sms here and update balance
  174. }
  175. }
  176. }
  177. if (sizeof($aGiftToSend) > 0) {
  178. return Response::json(array('success' => true,
  179. 'message' => trans('interface.gift.sms.created.success')
  180. ));
  181. } else
  182. return Response::make(trans('interface.errors.wrong.request'), 400);
  183. } else
  184. return Response::make(trans('interface.errors.wrong.request'), 400);
  185. }
  186. }