/** * Sends out the messages in email queue and update the database. * * @return boolean true if all messages are successfully sent out */ public function process() { $success = true; $items = Queue::find()->where(['and', ['sent_time' => null], ['<', 'attempts', $this->maxAttempts]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all(); // dd($items); if (!empty($items)) { foreach ($items as $item) { /** @var \app\models\Queue $item */ if ($message = $item->toMessage()) { $attributes = ['attempts', 'last_attempt_time']; if ($this->sendMessage($message)) { // $item->sent_time = new Expression('NOW()'); // $attributes[] = 'sent_time'; $item->delete(); } else { $success = false; } $item->attempts++; $item->last_attempt_time = new Expression('NOW()'); $item->updateAttributes($attributes); } } } return $success; }
/** * update 1st version * * @return void * @author **/ public function pointexpire($id) { $queue = new Queue(); $pending = $queue->find($id); $parameters = json_decode($pending->parameter, true); $messages = json_decode($pending->message, true); $errors = new MessageBag(); //check point expire on that day that havent get cut by transaction (or even left over) $points = PointLog::debit(true)->onactive([Carbon::parse($parameters['on'])->startOfDay()->format('Y-m-d H:i:s'), Carbon::parse($parameters['on'])->endOfDay()->format('Y-m-d H:i:s')])->haventgetcut(true)->with(['user'])->get()->toArray(); foreach ($points as $idx => $point) { //1. Check tag/category viewed $stat = \App\Models\StatUserView::userid($point['user_id'])->statabletype(['App\\Models\\Category', 'App\\Models\\Tag'])->get(['statable_id'])->toArray(); //1b. Get slugs $slugs = []; $purchased_prods = []; $purchased_varians = []; foreach ($stat as $key => $value) { $slugs[] = \App\Models\Cluster::find($value['statable_id'])['slug']; } $purchased = \App\Models\TransactionDetail::TransactionSellOn(['paid', 'packed', 'shipping', 'delivered'])->where('transactions.user_id', $point['user_id'])->groupby('varian_id')->with(['varian', 'varian.product', 'varian.product.clusters'])->get()->toArray(); foreach ($purchased as $key => $value) { //2. Check tag/category purchased foreach ($value['varian']['product']['clusters'] as $key2 => $value2) { $slugs[] = $value2['slug']; } $purchased_prods[] = $value['varian']['product_id']; $purchased_varians[] = $value['varian']['size']; } //2a. get slug of category/tag //2b. get product id //2c. get varian size $slug = array_unique($slugs); $productids = array_unique($purchased_prods); $variansize = array_unique($purchased_varians); $result = \App\Models\Product::sellable(true); if (!empty($slug)) { $result = $result->clustersslug($slug); } if (!empty($productids)) { $result = $result->notid($productids); } if (!empty($variansize)) { $result = $result->variansize($variansize); } $product = $result->orderby('price', 'desc')->take(4)->get()->toArray(); $data = ['point' => $point, 'balin' => $parameters['store'], 'product' => $product]; //send mail Mail::send('mail.' . $parameters['template'] . '.crm.point', ['data' => $data], function ($message) use($point, $parameters) { $message->to($point['user']['email'], $point['user']['name'])->subject(strtoupper($parameters['template']) . ' - POINT REMINDER'); }); $pnumber = $pending->process_number + 1; $messages['message'][$pnumber] = 'Sukses Mengirim Email ' . (isset($point['user']['name']) ? $point['user']['name'] : ''); $pending->fill(['process_number' => $pnumber, 'message' => json_encode($messages)]); $pending->save(); } return true; }
public function actionInstall() { if ($install = Queue::find()->one()) { $site = Site::findOne(['id' => $install->domain_id]); if ($this->_install($site->domain, $install->title, $install->admin, $install->password, $install->email)) { $install->delete(); $site->status = 'active'; $site->save(); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Queue::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'book_id' => $this->book_id, 'user_id' => $this->user_id, 'reg_date' => $this->reg_date]); return $dataProvider; }
/** * Lists all Queue models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Queue::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
/** * 1. check product * 2. Price Changed * 2.*. Art of discount1 : if there were discount amount, prices will be reduced by certain amount * 2.*. Art of discount2 : if there were discount amount, prices will be reduced by certain amount * 2.*. Art of discount3 : if there were discount percentage, prices will be reduced by certain percentage * * @return true * @author **/ public function broadcastdiscount($id) { $queue = new Queue(); $pending = $queue->find($id); $parameters = json_decode($pending->parameter, true); $messages = json_decode($pending->message, true); //1. Check product $products = new \App\Models\Product(); $products = $products->sellable(true); //1a. Only for certain category if (isset($parameters['category_ids'])) { $products = $products->categoriesid($parameters['category_ids']); } elseif (isset($parameters['tag_ids'])) { $products = $products->tagsid($parameters['tag_ids']); } $products = $products->get(); //2. Price Changed foreach ($products as $idx => $product) { $errors = new MessageBag(); //2a. Check price on that end period //if there were setup price right after end date, do nothing //if there were setup price after end date, but not precisely duplicated latest and expand right after //if there were no setup price right after end date, duplicate latest price with right after end date $price = \App\Models\Price::productid($product['id'])->where('started_at', '>', date('Y-m-d H:i:s', strtotime($parameters['ended_at'])))->orderby('started_at', 'desc')->first(); $promo = 0; if ($price) { if (date('Y-m-d H:i:s', strtotime($parameters['ended_at'] . ' + 1 second')) != $price['started_at']->format('Y-m-d H:i:s')) { $prev = $price->toArray(); $price = new \App\Models\Price(); $price->fill($prev); $price->started_at = date('Y-m-d H:i:s', strtotime($parameters['ended_at'] . ' + 1 second')); if (!$price->save()) { $errors->add('Price', $price->getError()); } } } else { $price = \App\Models\Price::productid($product['id'])->ondate($parameters['ended_at'])->orderby('started_at', 'desc')->first(); if ($price) { $prev = $price->toArray(); $price = new \App\Models\Price(); $price->fill($prev); $price->started_at = date('Y-m-d H:i:s', strtotime($parameters['ended_at'] . ' + 1 second')); if (!$price->save()) { $errors->add('Price', $price->getError()); } } } //2b. Check price on that start day //if there were setup price right after start date, create new one //if there were setup price exactly in time, update promo price $price = \App\Models\Price::productid($product['id'])->ondate($parameters['started_at'])->orderby('started_at', 'desc')->first(); $promo = 0; if ($price) { if (isset($parameters['discount_amount']) && isset($parameters['discount_percentage']) && $parameters['discount_percentage'] != 0) { $promo = $price['price'] - $parameters['discount_amount'] - ($price['price'] - $parameters['discount_amount']) * $parameters['discount_percentage'] / 100; } elseif (isset($parameters['discount_amount'])) { $promo = $price['price'] - $parameters['discount_amount']; } elseif (isset($parameters['discount_percentage']) && $parameters['discount_percentage'] != 0) { $promo = $price['price'] - $price['price'] * $parameters['discount_percentage'] / 100; } if ($parameters['started_at'] == $price['started_at']->format('Y-m-d H:i:s')) { $price->promo_price = "{$promo}"; } else { $prev = $price->toArray(); $price = new \App\Models\Price(); $price->fill($prev); $price->started_at = $parameters['started_at']; $price->promo_price = "{$promo}"; } if (!$price->save()) { $errors->add('Price', $price->getError()); } } //2c. Check price on that period //if there were setup price during period, update promo price $prices = \App\Models\Price::productid($product['id'])->ondate([date('Y-m-d H:i:s', strtotime($parameters['started_at'] . ' + 1 second')), $parameters['ended_at']])->orderby('started_at', 'desc')->get(); foreach ($prices as $key => $value) { $price = \App\Models\Price::id($value['id'])->first(); if ($price) { if (isset($parameters['discount_amount']) && isset($parameters['discount_percentage']) && $parameters['discount_percentage'] != 0) { $promo = $price['price'] - $parameters['discount_amount'] - ($price['price'] - $parameters['discount_amount']) * $parameters['discount_percentage'] / 100; } elseif (isset($parameters['discount_amount'])) { $promo = $price['price'] - $parameters['discount_amount']; } elseif (isset($parameters['discount_percentage']) && $parameters['discount_percentage'] != 0) { $promo = $price['price'] - $price['price'] * $parameters['discount_percentage'] / 100; } $price->promo_price = "{$promo}"; if (!$price->save()) { $errors->add('Price', $price->getError()); } } } if (!$errors->count()) { $pnumber = $pending->process_number + 1; $messages['message'][$pnumber] = 'Sukses Menyimpan Perubahan Harga ' . (isset($product['name']) ? $product['name'] : ''); $pending->fill(['process_number' => $pnumber, 'message' => json_encode($messages)]); } else { $pnumber = $pending->process_number + 1; $messages['message'][$pnumber] = 'Gagal Menyimpan Perubahan Harga ' . (isset($product['name']) ? $product['name'] : ''); $messages['errors'][$pnumber] = $errors; $pending->fill(['process_number' => $pnumber, 'message' => json_encode($messages)]); } $pending->save(); } return true; }