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.

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