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.

343 lines
12 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\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use yii\web\Response;
  13. use yii\widgets\ActiveForm;
  14. use backend\modules\shop\models\ars\ExpressArea;
  15. use backend\modules\goods\models\ars\Goods;
  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. if (Goods::find()->where(['express_template' => $id])->count() == 0) {
  103. $expressTemplateModel = $this->findModel($id);
  104. ExpressArea::deleteAll(['express_template' => $expressTemplateModel->id]);
  105. $expressTemplateModel->delete();
  106. } else {
  107. Yii::$app->session->setFlash('error', '该模板已被使用');
  108. }
  109. return $this->redirect(['index']);
  110. }
  111. /**
  112. * Finds the ExpressTemplate model based on its primary key value.
  113. * If the model is not found, a 404 HTTP exception will be thrown.
  114. * @param integer $id
  115. * @return ExpressTemplate the loaded model
  116. * @throws NotFoundHttpException if the model cannot be found
  117. */
  118. protected function findModel($id)
  119. {
  120. if (($model = ExpressTemplate::findOne($id)) !== null) {
  121. return $model;
  122. }
  123. throw new NotFoundHttpException('The requested page does not exist.');
  124. }
  125. /**
  126. * @author iron
  127. * 文件导出
  128. */
  129. public function actionExport()
  130. {
  131. $searchModel = new ExpressTemplateSearch();
  132. $params = Yii::$app->request->queryParams;
  133. if ($params['page-type'] == 'all') {
  134. $dataProvider = $searchModel->allData($params);
  135. } else {
  136. $dataProvider = $searchModel->search($params);
  137. }
  138. \iron\widget\Excel::export([
  139. 'models' => $dataProvider->getModels(),
  140. 'format' => 'Xlsx',
  141. 'asAttachment' => true,
  142. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  143. 'columns' => $searchModel->columns()
  144. ]);
  145. }
  146. /**
  147. * @return string
  148. * 运费区域列表
  149. */
  150. public function actionExpressAreaList($id)
  151. {
  152. $expressTemplate = ExpressTemplate::findOne($id);
  153. $searchModel = new ExpressAreaSearch();
  154. $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
  155. return $this->render('express_area_list', [
  156. 'searchModel' => $searchModel,
  157. 'dataProvider' => $dataProvider,
  158. 'columns' => $searchModel->columns(),
  159. 'expressTemplate' => $expressTemplate
  160. ]);
  161. }
  162. /**
  163. * @return array|mixed|string|Response
  164. * 运费区域模板区域创建方法
  165. */
  166. public function actionExpressAreaCreate()
  167. {
  168. $expressTemplateId = Yii::$app->request->get('expressTemplateId');
  169. $expressTemplateModel = ExpressTemplate::findOne($expressTemplateId);
  170. $model = new ExpressArea();
  171. $model->basic_count = 1;
  172. $model->basic_price = '0.00';
  173. $model->express_template = $expressTemplateModel->id;
  174. if (Yii::$app->request->isPost) {
  175. $data = Yii::$app->request->post('ExpressArea');
  176. if (Yii::$app->request->isAjax) {
  177. $model->load($data, '');
  178. Yii::$app->response->format = Response::FORMAT_JSON;
  179. $data = ActiveForm::validate($model);
  180. $data['status'] = 2;
  181. return $data;
  182. }
  183. if (Yii::$app->request->post('area') == null) {
  184. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  185. }
  186. $cityIds = array_keys(Yii::$app->request->post('area'));
  187. $data['city'] = implode(',', $cityIds);
  188. $model->load($data, '');
  189. $model->basic_price *= 100;
  190. $model->extra_price *= 100;
  191. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  192. $model->basic_count *= 1000;
  193. $model->extra_count *= 1000;
  194. } else {
  195. $model->basic_count *= 1;
  196. $model->extra_count *= 1;
  197. }
  198. $model->save();
  199. return $this->redirect('express-area-list?id='.$model->express_template);
  200. }
  201. $data = [];
  202. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->all();
  203. $expressAresCityIdArr = [];
  204. if ($expressAreas) {
  205. foreach ($expressAreas as $expressAreaCity) {
  206. $cityIdArr = explode(',', $expressAreaCity->city);
  207. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  208. }
  209. }
  210. $provinces = Province::find()->cache(0)->all();
  211. $j = 0;
  212. foreach ($provinces as $k => $v) {
  213. $cities = City::find()
  214. ->where(['province_id' => $v->province_id])
  215. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  216. ->all();
  217. if ($cities) {
  218. $data[$j]['province'] = $v->name;
  219. foreach ($cities as $city) {
  220. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  221. }
  222. $j++;
  223. }
  224. }
  225. if (empty($data)) {
  226. Yii::$app->session->setFlash('error', '已无地区选择');
  227. return $this->redirect('express-area-list?id='.$expressTemplateModel->id);
  228. }
  229. return $this->render('express_area_create', [
  230. 'model' => $model,
  231. 'data' => $data,
  232. 'expressTemplateModel' => $expressTemplateModel
  233. ]);
  234. }
  235. /**
  236. * @return array|mixed|string|Response
  237. * 运费区域模板区域更新方法
  238. */
  239. public function actionExpressAreaUpdate($id)
  240. {
  241. $model = ExpressArea::findOne($id);
  242. $expressTemplateModel = ExpressTemplate::findOne($model->express_template);
  243. $model->basic_price /= 100;
  244. $model->extra_price /= 100;
  245. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  246. $model->basic_count /= 10;
  247. $model->extra_count /= 10;
  248. }
  249. $data = Yii::$app->request->post('ExpressArea');
  250. if ($data) {
  251. if (Yii::$app->request->post('area') == null) {
  252. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  253. }
  254. $cityIds = array_keys(Yii::$app->request->post('area'));
  255. $data['city'] = implode(',', $cityIds);
  256. $model->load($data, '');
  257. $model->basic_price *= 100;
  258. $model->extra_price *= 100;
  259. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  260. $model->basic_count *= 1000;
  261. $model->extra_count *= 1000;
  262. } else {
  263. $model->basic_count *= 1;
  264. $model->extra_count *= 1;
  265. }
  266. $model->save();
  267. return $this->redirect('express-area-list?id='.$model->express_template);
  268. }
  269. $data = [];
  270. $expressAreas = ExpressArea::find()->select(['city'])->where(['express_template' => $expressTemplateModel->id])->andWhere(['!=', 'id', $id])->all();
  271. $expressAresCityIdArr = [];
  272. if ($expressAreas) {
  273. foreach ($expressAreas as $expressAreaCity) {
  274. $cityIdArr = explode(',', $expressAreaCity->city);
  275. $expressAresCityIdArr = array_unique(array_merge($cityIdArr, $expressAresCityIdArr));
  276. }
  277. }
  278. $provinces = Province::find()->cache(0)->all();
  279. $j = 0;
  280. foreach ($provinces as $k => $v) {
  281. $cities = City::find()
  282. ->where(['province_id' => $v->province_id])
  283. ->andWhere(['not in', 'city_id', $expressAresCityIdArr])
  284. ->all();
  285. if ($cities) {
  286. $data[$j]['province'] = $v->name;
  287. foreach ($cities as $city) {
  288. $data[$j]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  289. }
  290. $j++;
  291. }
  292. }
  293. return $this->render('express_area_update', [
  294. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city), 'expressTemplateModel' => $expressTemplateModel
  295. ]);
  296. }
  297. /**
  298. * @param $id
  299. * @return string
  300. * 运费区域模板区域查看方法
  301. */
  302. public function actionExpressAreaView($id)
  303. {
  304. $expressAreaModel = ExpressArea::findOne($id);
  305. $expressTemplateModel = ExpressTemplate::findOne($expressAreaModel->express_template);
  306. return $this->render('express_area_view', [
  307. 'model' => $expressAreaModel,
  308. 'expressTemplateModel' => $expressTemplateModel
  309. ]);
  310. }
  311. public function actionExpressAreaDelete($id)
  312. {
  313. $expressAreaModel = ExpressArea::findOne($id);
  314. $expressTemplateId = $expressAreaModel->express_template;
  315. $expressAreaModel->delete();
  316. return $this->redirect('express-area-list?id='.$expressTemplateId);
  317. }
  318. }