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.

391 lines
14 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. $model->calculation_type = ExpressTemplate::CALCULATION_TYPE_NUMBER;
  70. $model->basic_count = 1;
  71. $model->basic_price = '0.00';
  72. if (Yii::$app->request->isPost) {
  73. $data = Yii::$app->request->post('ExpressTemplate');
  74. if (Yii::$app->request->isAjax) {
  75. $model->load($data, '');
  76. Yii::$app->response->format = Response::FORMAT_JSON;
  77. $data = ActiveForm::validate($model);
  78. $data['status'] = 2;
  79. return $data;
  80. }
  81. if (Yii::$app->request->post('area') == null) {
  82. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  83. }
  84. $cityIds = array_keys(Yii::$app->request->post('area'));
  85. $data['city'] = implode(',', $cityIds);
  86. $model->load($data, '');
  87. $model->basic_price *= 100;
  88. $model->extra_price *= 100;
  89. if ($model->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  90. $model->basic_count *= 10;
  91. $model->extra_count *= 10;
  92. } else {
  93. $model->basic_count *= 1;
  94. $model->extra_count *= 1;
  95. }
  96. $model->save();
  97. return $this->redirect('index');
  98. }
  99. $data = [];
  100. $provinces = Province::find()->cache(0)->all();
  101. foreach ($provinces as $k => $v) {
  102. $data[$k]['province'] = $v->name;
  103. $cities = City::find()
  104. ->where(['province_id' => $v->province_id])
  105. ->all();
  106. foreach ($cities as $city) {
  107. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  108. }
  109. }
  110. return $this->render('create', [
  111. 'model' => $model,
  112. 'data' => $data
  113. ]);
  114. }
  115. /**
  116. * Updates an existing ExpressTemplate model.
  117. * If update is successful, the browser will be redirected to the 'view' page.
  118. * @param integer $id
  119. * @return mixed
  120. * @throws NotFoundHttpException if the model cannot be found
  121. */
  122. public function actionUpdate($id)
  123. {
  124. $model = $this->findModel($id);
  125. $model->basic_price /= 100;
  126. $model->extra_price /= 100;
  127. if ($model->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  128. $model->basic_count /= 10;
  129. $model->extra_count /= 10;
  130. }
  131. $data = Yii::$app->request->post('ExpressTemplate');
  132. if ($data) {
  133. if (Yii::$app->request->post('area') == null) {
  134. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  135. }
  136. $cityIds = array_keys(Yii::$app->request->post('area'));
  137. $data['city'] = implode(',', $cityIds);
  138. $model->load($data, '');
  139. $model->save();
  140. return $this->render('view', ['model' => ExpressTemplate::findOne($model->id)]);
  141. }
  142. $data = [];
  143. $provinces = Province::find()->cache(0)->all();
  144. foreach ($provinces as $k => $v) {
  145. $data[$k]['province'] = $v->name;
  146. $cities = City::find()
  147. ->where(['province_id' => $v->province_id])
  148. ->all();
  149. foreach ($cities as $city) {
  150. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  151. }
  152. }
  153. return $this->render('update', [
  154. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city)
  155. ]);
  156. }
  157. /**
  158. * Deletes an existing ExpressTemplate model.
  159. * If deletion is successful, the browser will be redirected to the 'index' page.
  160. * @param integer $id
  161. * @return mixed
  162. * @throws NotFoundHttpException if the model cannot be found
  163. */
  164. public function actionDelete($id)
  165. {
  166. $this->findModel($id)->delete();
  167. return $this->redirect(['index']);
  168. }
  169. /**
  170. * Finds the ExpressTemplate model based on its primary key value.
  171. * If the model is not found, a 404 HTTP exception will be thrown.
  172. * @param integer $id
  173. * @return ExpressTemplate the loaded model
  174. * @throws NotFoundHttpException if the model cannot be found
  175. */
  176. protected function findModel($id)
  177. {
  178. if (($model = ExpressTemplate::findOne($id)) !== null) {
  179. return $model;
  180. }
  181. throw new NotFoundHttpException('The requested page does not exist.');
  182. }
  183. /**
  184. * @author iron
  185. * 文件导出
  186. */
  187. public function actionExport()
  188. {
  189. $searchModel = new ExpressTemplateSearch();
  190. $params = Yii::$app->request->queryParams;
  191. if ($params['page-type'] == 'all') {
  192. $dataProvider = $searchModel->allData($params);
  193. } else {
  194. $dataProvider = $searchModel->search($params);
  195. }
  196. \iron\widget\Excel::export([
  197. 'models' => $dataProvider->getModels(),
  198. 'format' => 'Xlsx',
  199. 'asAttachment' => true,
  200. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  201. 'columns' => $searchModel->columns()
  202. ]);
  203. }
  204. /**
  205. * @return string
  206. * 运费区域列表
  207. */
  208. public function actionExpressAreaList($id)
  209. {
  210. $expressTemplate = ExpressTemplate::findOne($id);
  211. $searchModel = new ExpressAreaSearch();
  212. $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
  213. return $this->render('express_area_list', [
  214. 'searchModel' => $searchModel,
  215. 'dataProvider' => $dataProvider,
  216. 'columns' => $searchModel->columns(),
  217. 'expressTemplate' => $expressTemplate
  218. ]);
  219. }
  220. /**
  221. * @return array|mixed|string|Response
  222. * 运费区域模板区域创建方法
  223. */
  224. public function actionExpressAreaCreate()
  225. {
  226. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  227. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  228. $model = new ExpressArea();
  229. $model->basic_count = 1;
  230. $model->basic_price = '0.00';
  231. $model->express_template = $expressTemplateModel->id;
  232. if (Yii::$app->request->isPost) {
  233. $data = Yii::$app->request->post('ExpressArea');
  234. if (Yii::$app->request->isAjax) {
  235. $model->load($data, '');
  236. Yii::$app->response->format = Response::FORMAT_JSON;
  237. $data = ActiveForm::validate($model);
  238. $data['status'] = 2;
  239. return $data;
  240. }
  241. if (Yii::$app->request->post('area') == null) {
  242. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  243. }
  244. $cityIds = array_keys(Yii::$app->request->post('area'));
  245. $data['city'] = implode(',', $cityIds);
  246. $model->load($data, '');
  247. $model->basic_price *= 100;
  248. $model->extra_price *= 100;
  249. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  250. $model->basic_count *= 10;
  251. $model->extra_count *= 10;
  252. } else {
  253. $model->basic_count *= 1;
  254. $model->extra_count *= 1;
  255. }
  256. $model->save();
  257. return $this->redirect('express-area-list?id='.$model->express_template);
  258. }
  259. $data = [];
  260. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->all();
  261. $expressAresCityIdArr = [];
  262. if ($expressAreas) {
  263. foreach ($expressAreas as $expressAreaCity) {
  264. $cityIdArr = explode(',', $expressAreaCity->city);
  265. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  266. }
  267. }
  268. $provinces = Province::find()->cache(0)->all();
  269. $j = 0;
  270. foreach ($provinces as $k => $v) {
  271. $cities = City::find()
  272. ->where(['province_id' => $v->province_id])
  273. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  274. ->all();
  275. if ($cities) {
  276. $data[$j]['province'] = $v->name;
  277. foreach ($cities as $city) {
  278. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  279. }
  280. $j++;
  281. }
  282. }
  283. if (empty($data)) {
  284. Yii::$app->session->setFlash('error', '已无地区选择');
  285. return $this->redirect('express-area-list?id='.$expressTemplateModel->id);
  286. }
  287. return $this->render('express_area_create', [
  288. 'model' => $model,
  289. 'data' => $data,
  290. 'expressTemplateModel' => $expressTemplateModel
  291. ]);
  292. }
  293. /**
  294. * @return array|mixed|string|Response
  295. * 运费区域模板区域更新方法
  296. */
  297. public function actionExpressAreaUpdate($id)
  298. {
  299. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  300. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  301. $model = ExpressArea::findOne($id);
  302. $model->basic_price /= 100;
  303. $model->extra_price /= 100;
  304. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  305. $model->basic_count /= 10;
  306. $model->extra_count /= 10;
  307. }
  308. $data = Yii::$app->request->post('ExpressArea');
  309. if ($data) {
  310. if (Yii::$app->request->post('area') == null) {
  311. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  312. }
  313. $cityIds = array_keys(Yii::$app->request->post('area'));
  314. $data['city'] = implode(',', $cityIds);
  315. $model->load($data, '');
  316. $model->basic_price *= 100;
  317. $model->extra_price *= 100;
  318. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  319. $model->basic_count *= 10;
  320. $model->extra_count *= 10;
  321. } else {
  322. $model->basic_count *= 1;
  323. $model->extra_count *= 1;
  324. }
  325. $model->save();
  326. return $this->redirect('express-area-list?id='.$model->express_template);
  327. }
  328. $data = [];
  329. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->andWhere(['!=', 'id', $id])->all();
  330. $expressAresCityIdArr = [];
  331. if ($expressAreas) {
  332. foreach ($expressAreas as $expressAreaCity) {
  333. $cityIdArr = explode(',', $expressAreaCity->city);
  334. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  335. }
  336. }
  337. $provinces = Province::find()->cache(0)->all();
  338. $j = 0;
  339. foreach ($provinces as $k => $v) {
  340. $cities = City::find()
  341. ->where(['province_id' => $v->province_id])
  342. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  343. ->all();
  344. if ($cities) {
  345. $data[$j]['province'] = $v->name;
  346. foreach ($cities as $city) {
  347. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  348. }
  349. $j++;
  350. }
  351. }
  352. return $this->render('express_area_update', [
  353. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city), 'expressTemplateModel' => $expressTemplateModel
  354. ]);
  355. }
  356. /**
  357. * @param $id
  358. * @return string
  359. * 运费区域模板区域查看方法
  360. */
  361. public function actionExpressAreaView($id)
  362. {
  363. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  364. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  365. return $this->render('express_area_view', [
  366. 'model' => ExpressArea::findOne($id),
  367. 'expressTemplateModel' => $expressTemplateModel
  368. ]);
  369. }
  370. }