/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Invoice::find()->andWhere(['not', ['status_id' => Invoice::STATUS_CURRENT]])->with('location.client'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $dataProvider->sort->attributes['location_id'] = ['asc' => [Client::tableName() . '.name' => SORT_ASC, Location::tableName() . '.name' => SORT_ASC], 'desc' => [Client::tableName() . '.name' => SORT_DESC, Location::tableName() . '.name' => SORT_DESC]]; $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([Invoice::tableName() . '.location_id' => $this->location_id, Invoice::tableName() . '.status_id' => $this->status_id]); $query->andFilterWhere(['like', Invoice::tableName() . '.id', (int) $this->id]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Location::find()->joinWith('client')->andWhere([Client::tableName() . '.active' => true]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false, 'sort' => ['defaultOrder' => ['client_id' => SORT_ASC, 'name' => SORT_ASC]]]); $dataProvider->sort->attributes['client_id'] = ['asc' => [Client::tableName() . '.name' => SORT_ASC], 'desc' => [Client::tableName() . '.name' => SORT_DESC]]; if (Yii::$app->user->can('Accounting')) { $query->joinWith('lastInvoice'); $dataProvider->sort->attributes['lastInvoiceStatus'] = ['asc' => [Invoice::tableName() . '.status_id' => SORT_ASC], 'desc' => [Invoice::tableName() . '.status_id' => SORT_DESC]]; } $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([self::tableName() . '.client_id' => $this->client_id, self::tableName() . '.proactive' => $this->proactive, self::tableName() . '.active' => $this->active]); if (Yii::$app->user->can('Accounting')) { $query->andFilterWhere([Invoice::tableName() . '.status_id' => $this->lastInvoiceStatus]); } $query->andFilterWhere(['or', ['like', self::tableName() . '.name', $this->name], ['like', Client::tableName() . '.name', $this->name]])->andFilterWhere(['like', 'phone', $this->phone]); return $dataProvider; }
/** * @return $this */ public function current() { $this->joinWith('invoice')->andWhere([Invoice::tableName() . '.status_id' => Invoice::STATUS_CURRENT]); return $this; }
/** * 统计所用用户已经开票金额 * @return int */ public static function findTotalAgreeMoney() { $table = Invoice::tableName(); $money = (new Query())->select('sum(money)')->from($table)->where(['state' => Invoice::STATE_OVER])->one(); if ($money['sum(money)']) { return $money['sum(money)']; } else { return 0; } }
/** 发票查询 */ public function actionSearch() { $request = Yii::$app->request; $session = Yii::$app->session; $query = $session->getFlash('query'); if ($request->isPost) { $type = $request->post('type'); $content = $request->post('content'); } else { $type = $request->get('type'); $content = trim($request->get('content')); } if ($type || !$query) { switch ($type) { case 'nickname': $table_a = Invoice::tableName(); $table_b = Users::tableName(); $query = Invoice::find()->leftJoin($table_b, "{$table_a}.userId={$table_b}.userId")->where(['like', "{$table_b}.nickname", $content]); break; case 'money-more': $query = Invoice::find()->Where(['>=', 'money', $content]); break; case 'money-equal': $query = Invoice::find()->where(['==', 'money', $content]); break; case 'money-less': $query = Invoice::find()->Where(['<=', 'money', $content]); break; case 'role': $role = ''; if ($content == 'a' || $content == 'A' || $content == 'A级') { $role = Users::ROLE_A; } elseif (strstr('金牌伙伴', $content)) { $role = Users::ROLE_AA; } elseif (strstr('钻石伙伴', $content)) { $role = Users::ROLE_AAA; } elseif ($content == '管理员') { $role = Users::ROLE_ADMIN; } $table_a = Invoice::tableName(); $table_b = Users::tableName(); $query = Invoice::find()->leftJoin($table_b, "{$table_a}.userId={$table_b}.userId")->where(["{$table_b}.role" => $role]); break; default: $query = Invoice::find()->where(['like', $type, $content]); break; } } $view = $session->getFlash('view'); if (!$view) { //非下一页 $view = $request->get('view'); //取哪个页面的搜索 if (!$view) { //不存在即为index的搜索 $view = 'index'; } } if ($view == 'apply') { //发票申请的搜索 $query->andWhere(['state' => Invoice::STATE_ING]); } elseif ($view == 'opener') { //发票开具的搜索 $query->andWhere(['state' => Invoice::STATE_PASS]); } $session->setFlash('view', $view); $session->setFlash('query', $query); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]); $models = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['createDate' => SORT_DESC])->all(); return $this->render($view, ['models' => $models, 'pages' => $pagination]); }
/** * @return InvoiceQuery */ public function getLastInvoice() { return $this->hasOne(Invoice::className(), ['location_id' => 'id'])->andOnCondition(['not', [Invoice::tableName() . '.status_id' => Invoice::STATUS_CURRENT]])->orderBy([Invoice::tableName() . '.id' => SORT_DESC]); }