You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.9 KiB

  1. <?php
  2. namespace backend\modules\shop\controllers;
  3. use backend\modules\shop\models\ars\City;
  4. use backend\modules\shop\models\ars\Province;
  5. use Yii;
  6. use backend\modules\shop\models\ars\ExpressTemplate;
  7. use backend\modules\shop\models\searchs\ExpressTemplateSearch;
  8. use yii\caching\Cache;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use yii\web\Response;
  13. use yii\widgets\ActiveForm;
  14. /**
  15. * ExpressTemplateController implements the CRUD actions for ExpressTemplate model.
  16. */
  17. class ExpressTemplateController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['POST'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all ExpressTemplate models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new ExpressTemplateSearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41. return $this->render('index', [
  42. 'searchModel' => $searchModel,
  43. 'dataProvider' => $dataProvider,
  44. 'columns' => $searchModel->columns()
  45. ]);
  46. }
  47. /**
  48. * Displays a single ExpressTemplate model.
  49. * @param integer $id
  50. * @return mixed
  51. * @throws NotFoundHttpException if the model cannot be found
  52. */
  53. public function actionView($id)
  54. {
  55. return $this->render('view', [
  56. 'model' => $this->findModel($id),
  57. ]);
  58. }
  59. /**
  60. * Creates a new ExpressTemplate model.
  61. * If creation is successful, the browser will be redirected to the 'view' page.
  62. * @return mixed
  63. */
  64. public function actionCreate()
  65. {
  66. $model = new ExpressTemplate();
  67. $model->calculation_type = ExpressTemplate::CALCULATION_TYPE_NUMBER;
  68. $model->basic_count = 1;
  69. $model->basic_price = '0.00';
  70. if (Yii::$app->request->isPost) {
  71. $data = Yii::$app->request->post('ExpressTemplate');
  72. if (Yii::$app->request->isAjax) {
  73. $model->load($data, '');
  74. Yii::$app->response->format = Response::FORMAT_JSON;
  75. $data = ActiveForm::validate($model);
  76. $data['status'] = 2;
  77. return $data;
  78. }
  79. if (Yii::$app->request->post('area') == null) {
  80. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  81. }
  82. $cityIds = array_keys(Yii::$app->request->post('area'));
  83. $data['city'] = implode(',', $cityIds);
  84. $model->load($data, '');
  85. $model->basic_price *= 100;
  86. $model->extra_price *= 100;
  87. if ($model->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  88. $model->basic_count *= 10;
  89. $model->extra_count *= 10;
  90. } else {
  91. $model->basic_count *= 1;
  92. $model->extra_count *= 1;
  93. }
  94. $model->save();
  95. return $this->redirect('index');
  96. }
  97. $data = [];
  98. $provinces = Province::find()->cache(0)->all();
  99. foreach ($provinces as $k => $v) {
  100. $data[$k]['province'] = $v->name;
  101. $cities = City::find()->cache(0)
  102. ->where(['province_id' => $v->province_id])
  103. ->all();
  104. foreach ($cities as $city) {
  105. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  106. }
  107. }
  108. return $this->render('create', [
  109. 'model' => $model,
  110. 'data' => $data
  111. ]);
  112. }
  113. /**
  114. * Updates an existing ExpressTemplate model.
  115. * If update is successful, the browser will be redirected to the 'view' page.
  116. * @param integer $id
  117. * @return mixed
  118. * @throws NotFoundHttpException if the model cannot be found
  119. */
  120. public function actionUpdate($id)
  121. {
  122. $model = $this->findModel($id);
  123. $model->basic_price /= 100;
  124. $model->extra_price /= 100;
  125. if ($model->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  126. $model->basic_count /= 10;
  127. $model->extra_count /= 10;
  128. }
  129. $data = Yii::$app->request->post('ExpressTemplate');
  130. if ($data) {
  131. if (Yii::$app->request->post('area') == null) {
  132. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  133. }
  134. $cityIds = array_keys(Yii::$app->request->post('area'));
  135. $data['city'] = implode(',', $cityIds);
  136. $model->load($data, '');
  137. $model->save();
  138. return $this->render('view', ['model' => ExpressTemplate::findOne($model->id)]);
  139. }
  140. $data = [];
  141. $provinces = Province::find()->cache(0)->all();
  142. foreach ($provinces as $k => $v) {
  143. $data[$k]['province'] = $v->name;
  144. $cities = City::find()->cache(0)
  145. ->where(['province_id' => $v->province_id])
  146. ->all();
  147. foreach ($cities as $city) {
  148. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  149. }
  150. }
  151. return $this->render('update', [
  152. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city)
  153. ]);
  154. }
  155. /**
  156. * Deletes an existing ExpressTemplate model.
  157. * If deletion is successful, the browser will be redirected to the 'index' page.
  158. * @param integer $id
  159. * @return mixed
  160. * @throws NotFoundHttpException if the model cannot be found
  161. */
  162. public function actionDelete($id)
  163. {
  164. $this->findModel($id)->delete();
  165. return $this->redirect(['index']);
  166. }
  167. /**
  168. * Finds the ExpressTemplate model based on its primary key value.
  169. * If the model is not found, a 404 HTTP exception will be thrown.
  170. * @param integer $id
  171. * @return ExpressTemplate the loaded model
  172. * @throws NotFoundHttpException if the model cannot be found
  173. */
  174. protected function findModel($id)
  175. {
  176. if (($model = ExpressTemplate::findOne($id)) !== null) {
  177. return $model;
  178. }
  179. throw new NotFoundHttpException('The requested page does not exist.');
  180. }
  181. /**
  182. * @author iron
  183. * 文件导出
  184. */
  185. public function actionExport()
  186. {
  187. $searchModel = new ExpressTemplateSearch();
  188. $params = Yii::$app->request->queryParams;
  189. if ($params['page-type'] == 'all') {
  190. $dataProvider = $searchModel->allData($params);
  191. } else {
  192. $dataProvider = $searchModel->search($params);
  193. }
  194. \iron\widget\Excel::export([
  195. 'models' => $dataProvider->getModels(),
  196. 'format' => 'Xlsx',
  197. 'asAttachment' => true,
  198. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  199. 'columns' => $searchModel->columns()
  200. ]);
  201. }
  202. }