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.

329 lines
11 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 backend\modules\shop\models\searchs\ExpressAreaSearch;
  6. use Yii;
  7. use backend\modules\shop\models\ars\ExpressTemplate;
  8. use backend\modules\shop\models\searchs\ExpressTemplateSearch;
  9. use yii\caching\Cache;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. use yii\filters\VerbFilter;
  13. use yii\web\Response;
  14. use yii\widgets\ActiveForm;
  15. use backend\modules\shop\models\ars\ExpressArea;
  16. /**
  17. * ExpressTemplateController implements the CRUD actions for ExpressTemplate model.
  18. */
  19. class ExpressTemplateController extends Controller
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function behaviors()
  25. {
  26. return [
  27. 'verbs' => [
  28. 'class' => VerbFilter::className(),
  29. 'actions' => [
  30. 'delete' => ['POST'],
  31. ],
  32. ],
  33. ];
  34. }
  35. /**
  36. * Lists all ExpressTemplate models.
  37. * @return mixed
  38. */
  39. public function actionIndex()
  40. {
  41. $searchModel = new ExpressTemplateSearch();
  42. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  43. return $this->render('index', [
  44. 'searchModel' => $searchModel,
  45. 'dataProvider' => $dataProvider,
  46. 'columns' => $searchModel->columns()
  47. ]);
  48. }
  49. /**
  50. * Displays a single ExpressTemplate model.
  51. * @param integer $id
  52. * @return mixed
  53. * @throws NotFoundHttpException if the model cannot be found
  54. */
  55. public function actionView($id)
  56. {
  57. return $this->render('view', [
  58. 'model' => $this->findModel($id),
  59. ]);
  60. }
  61. /**
  62. * Creates a new ExpressTemplate model.
  63. * If creation is successful, the browser will be redirected to the 'view' page.
  64. * @return mixed
  65. */
  66. public function actionCreate()
  67. {
  68. $model = new ExpressTemplate();
  69. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  70. return $this->redirect('index');
  71. }
  72. return $this->render('create', [
  73. 'model' => $model,
  74. ]);
  75. }
  76. /**
  77. * Updates an existing ExpressTemplate model.
  78. * If update is successful, the browser will be redirected to the 'view' page.
  79. * @param integer $id
  80. * @return mixed
  81. * @throws NotFoundHttpException if the model cannot be found
  82. */
  83. public function actionUpdate($id)
  84. {
  85. $model = $this->findModel($id);
  86. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  87. return $this->redirect('index');
  88. }
  89. return $this->render('update', [
  90. 'model' => $model,
  91. ]);
  92. }
  93. /**
  94. * Deletes an existing ExpressTemplate model.
  95. * If deletion is successful, the browser will be redirected to the 'index' page.
  96. * @param integer $id
  97. * @return mixed
  98. * @throws NotFoundHttpException if the model cannot be found
  99. */
  100. public function actionDelete($id)
  101. {
  102. $this->findModel($id)->delete();
  103. return $this->redirect(['index']);
  104. }
  105. /**
  106. * Finds the ExpressTemplate model based on its primary key value.
  107. * If the model is not found, a 404 HTTP exception will be thrown.
  108. * @param integer $id
  109. * @return ExpressTemplate the loaded model
  110. * @throws NotFoundHttpException if the model cannot be found
  111. */
  112. protected function findModel($id)
  113. {
  114. if (($model = ExpressTemplate::findOne($id)) !== null) {
  115. return $model;
  116. }
  117. throw new NotFoundHttpException('The requested page does not exist.');
  118. }
  119. /**
  120. * @author iron
  121. * 文件导出
  122. */
  123. public function actionExport()
  124. {
  125. $searchModel = new ExpressTemplateSearch();
  126. $params = Yii::$app->request->queryParams;
  127. if ($params['page-type'] == 'all') {
  128. $dataProvider = $searchModel->allData($params);
  129. } else {
  130. $dataProvider = $searchModel->search($params);
  131. }
  132. \iron\widget\Excel::export([
  133. 'models' => $dataProvider->getModels(),
  134. 'format' => 'Xlsx',
  135. 'asAttachment' => true,
  136. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  137. 'columns' => $searchModel->columns()
  138. ]);
  139. }
  140. /**
  141. * @return string
  142. * 运费区域列表
  143. */
  144. public function actionExpressAreaList($id)
  145. {
  146. $expressTemplate = ExpressTemplate::findOne($id);
  147. $searchModel = new ExpressAreaSearch();
  148. $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
  149. return $this->render('express_area_list', [
  150. 'searchModel' => $searchModel,
  151. 'dataProvider' => $dataProvider,
  152. 'columns' => $searchModel->columns(),
  153. 'expressTemplate' => $expressTemplate
  154. ]);
  155. }
  156. /**
  157. * @return array|mixed|string|Response
  158. * 运费区域模板区域创建方法
  159. */
  160. public function actionExpressAreaCreate()
  161. {
  162. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  163. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  164. $model = new ExpressArea();
  165. $model->basic_count = 1;
  166. $model->basic_price = '0.00';
  167. $model->express_template = $expressTemplateModel->id;
  168. if (Yii::$app->request->isPost) {
  169. $data = Yii::$app->request->post('ExpressArea');
  170. if (Yii::$app->request->isAjax) {
  171. $model->load($data, '');
  172. Yii::$app->response->format = Response::FORMAT_JSON;
  173. $data = ActiveForm::validate($model);
  174. $data['status'] = 2;
  175. return $data;
  176. }
  177. if (Yii::$app->request->post('area') == null) {
  178. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  179. }
  180. $cityIds = array_keys(Yii::$app->request->post('area'));
  181. $data['city'] = implode(',', $cityIds);
  182. $model->load($data, '');
  183. $model->basic_price *= 100;
  184. $model->extra_price *= 100;
  185. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  186. $model->basic_count *= 10;
  187. $model->extra_count *= 10;
  188. } else {
  189. $model->basic_count *= 1;
  190. $model->extra_count *= 1;
  191. }
  192. $model->save();
  193. return $this->redirect('express-area-list?id='.$model->express_template);
  194. }
  195. $data = [];
  196. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->all();
  197. $expressAresCityIdArr = [];
  198. if ($expressAreas) {
  199. foreach ($expressAreas as $expressAreaCity) {
  200. $cityIdArr = explode(',', $expressAreaCity->city);
  201. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  202. }
  203. }
  204. $provinces = Province::find()->cache(0)->all();
  205. $j = 0;
  206. foreach ($provinces as $k => $v) {
  207. $cities = City::find()
  208. ->where(['province_id' => $v->province_id])
  209. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  210. ->all();
  211. if ($cities) {
  212. $data[$j]['province'] = $v->name;
  213. foreach ($cities as $city) {
  214. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  215. }
  216. $j++;
  217. }
  218. }
  219. if (empty($data)) {
  220. Yii::$app->session->setFlash('error', '已无地区选择');
  221. return $this->redirect('express-area-list?id='.$expressTemplateModel->id);
  222. }
  223. return $this->render('express_area_create', [
  224. 'model' => $model,
  225. 'data' => $data,
  226. 'expressTemplateModel' => $expressTemplateModel
  227. ]);
  228. }
  229. /**
  230. * @return array|mixed|string|Response
  231. * 运费区域模板区域更新方法
  232. */
  233. public function actionExpressAreaUpdate($id)
  234. {
  235. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  236. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  237. $model = ExpressArea::findOne($id);
  238. $model->basic_price /= 100;
  239. $model->extra_price /= 100;
  240. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  241. $model->basic_count /= 10;
  242. $model->extra_count /= 10;
  243. }
  244. $data = Yii::$app->request->post('ExpressArea');
  245. if ($data) {
  246. if (Yii::$app->request->post('area') == null) {
  247. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  248. }
  249. $cityIds = array_keys(Yii::$app->request->post('area'));
  250. $data['city'] = implode(',', $cityIds);
  251. $model->load($data, '');
  252. $model->basic_price *= 100;
  253. $model->extra_price *= 100;
  254. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  255. $model->basic_count *= 10;
  256. $model->extra_count *= 10;
  257. } else {
  258. $model->basic_count *= 1;
  259. $model->extra_count *= 1;
  260. }
  261. $model->save();
  262. return $this->redirect('express-area-list?id='.$model->express_template);
  263. }
  264. $data = [];
  265. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->andWhere(['!=', 'id', $id])->all();
  266. $expressAresCityIdArr = [];
  267. if ($expressAreas) {
  268. foreach ($expressAreas as $expressAreaCity) {
  269. $cityIdArr = explode(',', $expressAreaCity->city);
  270. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  271. }
  272. }
  273. $provinces = Province::find()->cache(0)->all();
  274. $j = 0;
  275. foreach ($provinces as $k => $v) {
  276. $cities = City::find()
  277. ->where(['province_id' => $v->province_id])
  278. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  279. ->all();
  280. if ($cities) {
  281. $data[$j]['province'] = $v->name;
  282. foreach ($cities as $city) {
  283. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  284. }
  285. $j++;
  286. }
  287. }
  288. return $this->render('express_area_update', [
  289. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city), 'expressTemplateModel' => $expressTemplateModel
  290. ]);
  291. }
  292. /**
  293. * @param $id
  294. * @return string
  295. * 运费区域模板区域查看方法
  296. */
  297. public function actionExpressAreaView($id)
  298. {
  299. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  300. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  301. return $this->render('express_area_view', [
  302. 'model' => ExpressArea::findOne($id),
  303. 'expressTemplateModel' => $expressTemplateModel
  304. ]);
  305. }
  306. }