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.

291 lines
9.4 KiB

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