/app/controllers/PerksManageController.php
PHP | 229 lines | 179 code | 43 blank | 7 comment | 24 complexity | 2cdc8faa9df6408e237be02318bcfba8 MD5 | raw file
- <?php
- /**
- * Created by PhpStorm.
- * User: sinersis
- * Date: 25.05.16
- * Time: 15:09
- */
- class PerksManageController extends BaseController
- {
- public function elListAction()
- {
- $oCompany = Auth::user();
- if (Request::ajax()) {
- $aGiftsEl = Gift_Code::where('type', '=', 'el')
- ->where('company_id', '=', Auth::user()->id)
- ->with(array('user.params' => function ($query) {
- $query->whereIn('param', array('contactName'));
- }))
- ->orderBy('created_at', 'DESC')->get();
- $aReturn = array('data' => array());
- foreach ($aGiftsEl as $oGiftCode) {
- $aReturn['data'][] = array(
- $oGiftCode->id,
- Carbon::createFromTimestamp(strtotime($oGiftCode->created_at))->format(trans('interface.offer.format.valid_by')),
- $oGiftCode->offer->sid,
- $oGiftCode->discount . '%',
- $oGiftCode->client,
- '<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>'
- );
- }
- return $aReturn;
- }
- $aViewData = array();
- $aOffers = Offer::where('company_id', $oCompany->id)->where('active', 1)->whereStatus('active')->get();
- $aViewData['aOffers'] = $aOffers;
- if ($old = Input::old('errors')) {
- $aViewData['errors'] = $old;
- }
- $aCompanyDbParams = $oCompany->params()->whereIn('param', array('loyaltySum', 'title'))->get();
- $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
- $aViewData['companyName'] = array_get($aCompanyParams, 'title', trans('interface.gift.empty.company.name'));
- return View::make('company.gift.list.el', $aViewData);
- }
- public function printedListAction()
- {
- }
- public function smsListAction()
- {
- $oCompany = Auth::user();
- if (Request::ajax()) {
- $aGiftsEl = Gift_Code::where('type', '=', 'sms')
- ->where('company_id', '=', Auth::user()->id)
- ->with(array('user.params' => function ($query) {
- $query->whereIn('param', array('contactName'));
- }))
- ->orderBy('created_at', 'DESC')->get();
- $aReturn = array('data' => array());
- foreach ($aGiftsEl as $oGiftCode) {
- $aReturn['data'][] = array(
- $oGiftCode->id,
- Carbon::createFromTimestamp(strtotime($oGiftCode->created_at))->format(trans('interface.offer.format.valid_by')),
- $oGiftCode->offer_id,
- $oGiftCode->discount . '%',
- $oGiftCode->client,
- '<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>'
- );
- }
- return $aReturn;
- }
- $aViewData = array();
- $aOffers = Offer::where('company_id', $oCompany->id)->where('active', 1)->whereStatus('active')->get();
- $aViewData['aOffers'] = $aOffers;
- if ($old = Input::old('errors')) {
- $aViewData['errors'] = $old;
- }
- $aCompanyDbParams = $oCompany->params()->whereIn('param', array('loyaltySum', 'title'))->get();
- $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
- $aViewData['companyName'] = array_get($aCompanyParams, 'title', trans('interface.gift.empty.company.name'));
- return View::make('company.gift.list.sms', $aViewData);
- }
- public function elCreateAction()
- {
- if (!Request::ajax() || Input::method() !== 'POST') {
- return Response::make(trans('interface.errors.wrong.request'), 400);
- }
- $oCompany = Auth::user();
- $aGiftsData = array(
- 'email' => Input::get('email'),
- 'perk' => Input::get('perk'),
- );
- if (is_array($aGiftsData['email']) && is_array($aGiftsData['perk']) && (sizeof($aGiftsData['email']) > 0) && (sizeof($aGiftsData['email']) == sizeof($aGiftsData['perk']))) {
- $aCompanyDbParams = $oCompany->params()->whereIn('param', array('title', 'url', 'description'))->get();
- $aCompanyParams = array_pluck($aCompanyDbParams, 'value', 'param');
- $aGiftsData['email'] = array_values($aGiftsData['email']);
- $aGiftsData['perk'] = array_values(array_map('intval', $aGiftsData['perk']));
- $aGiftToSend = array();
- $aRules = array(
- 'email' => 'required|email',
- 'perk' => 'required|exists:offers,sid'
- );
- foreach ($aGiftsData['email'] as $nKey => $sEmail) {
- $aGiftData = array('email' => Str::prep_input($sEmail), 'perk' => $aGiftsData['perk'][$nKey]);
- $oValidator = Validator::make($aGiftData, $aRules);
- if ($oValidator->passes()) {
- $oOffer = Offer::where('sid', $aGiftData['perk'])->first();
- $oGiftCode = new Gift_Code();
- $oGiftCode->company_id = $oCompany->id;
- $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'));
- $oGiftCode->type = 'el';
- $oGiftCode->client = $aGiftData['email'];
- $oGiftCode->status = 'free';
- $oGiftCode->user_id = null;
- $oGiftCode->discount = $oOffer->discount/2;
- $oGiftCode->commission = $oOffer->discount - $oGiftCode->discount;
- $oGiftCode->offer_id = $oOffer->id;
- $sTitle = array_get($aCompanyParams, 'title', '');
- if ($oGiftCode->save()) {
- $aGiftToSend[] = $oGiftCode->code;
- Mail::queue('emails.gift.el', ['link' => URL::route('entity/connect', ['entity' => $oGiftCode->sid]), 'discount' => $oGiftCode->discount, 'commission' => $oGiftCode->commission],
- function ($message) use ($oGiftCode, $oCompany, $sTitle) {
- Log::error(Config::get('settings.gift.sender.email'));
- $message->from(Config::get('settings.gift.sender.email'))->to($oGiftCode->client)->subject(trans('interface.gift.el.mail.subject', array('company_name' => $sTitle)));
- });
- }
- }
- }
- if (sizeof($aGiftToSend) > 0) {
- return Response::json(array('success' => true,
- 'message' => trans('interface.gift.el.created.success')
- ));
- } else
- return Response::make(trans('interface.errors.wrong.request'), 400);
- } else
- return Response::make(trans('interface.errors.wrong.request'), 400);
- }
- public function printedCreateAction()
- {
- }
- public function smsCreateAction()
- {
- if (!Request::ajax() || Input::method() !== 'POST') {
- return Response::make(trans('interface.errors.wrong.request'), 400);
- }
- $oCompany = Auth::user();
- $aGiftsData = array(
- 'phone' => Input::get('phone'),
- 'perk' => Input::get('perk'),
- );
- if (is_array($aGiftsData['phone']) && is_array($aGiftsData['perk']) && (sizeof($aGiftsData['phone']) > 0) && (sizeof($aGiftsData['phone']) == sizeof($aGiftsData['perk']))) {
- $aGiftsData['phone'] = array_values($aGiftsData['phone']);
- $aGiftsData['perk'] = array_values(array_map('intval', $aGiftsData['perk']));
- $aGiftToSend = array();
- $aRules = array(
- 'phone' => 'required|phone_ru',
- 'perk' => 'required|exists:offers,id'
- );
- foreach ($aGiftsData['phone'] as $nKey => $sEmail) {
- $aGiftData = array('phone' => Str::prep_input($sEmail), 'perk' => $aGiftsData['perk'][$nKey]);
- $oValidator = Validator::make($aGiftData, $aRules);
- if ($oValidator->passes()) {
- $oOffer = Offer::find($aGiftData['perk']);
- $oGiftCode = new Gift_Code();
- $oGiftCode->company_id = $oCompany->id;
- $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'));
- $oGiftCode->type = 'sms';
- $oGiftCode->client = $aGiftData['phone'];
- $oGiftCode->status = 'free';
- $oGiftCode->user_id = null;
- $oGiftCode->discount = $oOffer->discount;
- $oGiftCode->offer_id = $aGiftData['perk'];
- if ($oGiftCode->save()) {
- $aGiftToSend[] = $oGiftCode->code;
- //TODO: send sms here and update balance
- }
- }
- }
- if (sizeof($aGiftToSend) > 0) {
- return Response::json(array('success' => true,
- 'message' => trans('interface.gift.sms.created.success')
- ));
- } else
- return Response::make(trans('interface.errors.wrong.request'), 400);
- } else
- return Response::make(trans('interface.errors.wrong.request'), 400);
- }
- }