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.

287 lines
9.5 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 Yii;
  6. use backend\modules\shop\models\ars\ExpressTemplate;
  7. use backend\modules\shop\models\searchs\ExpressTemplateSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use yii\web\Response;
  12. use yii\widgets\ActiveForm;
  13. use backend\modules\shop\models\ars\ExpressArea;
  14. use backend\modules\goods\models\ars\Goods;
  15. use backend\modules\shop\logic\CommonManager;
  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, 'is_delete' => Goods::IS_DELETE_NO])->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. ShopManager::expressAreaScaleDate($model, $expressTemplateModel); //按比例转换数据
  190. $model->save();
  191. return $this->redirect('express-area-list?id='.$model->express_template);
  192. }
  193. $data = ShopManager::filterCity($model); //获取筛选的城市数据
  194. if (empty($data)) {
  195. Yii::$app->session->setFlash('error', '已无地区选择');
  196. return $this->redirect('express-area-list?id='.$expressTemplateModel->id);
  197. }
  198. return $this->render('express_area_create', [
  199. 'model' => $model,
  200. 'data' => $data,
  201. 'expressTemplateModel' => $expressTemplateModel
  202. ]);
  203. }
  204. /**
  205. * @return array|mixed|string|Response
  206. * 运费区域模板区域更新方法
  207. */
  208. public function actionExpressAreaUpdate($id)
  209. {
  210. $model = ExpressArea::findOne($id);
  211. //数据按比例转换
  212. $expressTemplateModel = ExpressTemplate::findOne($model->express_template);
  213. $model->basic_price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
  214. $model->extra_price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
  215. if ($expressTemplateModel->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) {
  216. $model->basic_count /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_WEIGHT);
  217. $model->extra_count /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_WEIGHT);
  218. }
  219. $data = Yii::$app->request->post('ExpressArea');
  220. if ($data) {
  221. if (Yii::$app->request->post('area') == null) {
  222. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  223. }
  224. $cityIds = array_keys(Yii::$app->request->post('area'));
  225. $data['city'] = implode(',', $cityIds);
  226. $model->load($data, '');
  227. ShopManager::expressAreaScaleDate($model, $expressTemplateModel); //按比例转换数据
  228. $model->save();
  229. return $this->redirect('express-area-list?id='.$model->express_template);
  230. }
  231. $data = ShopManager::filterCity($model); //获取筛选的城市数据
  232. return $this->render('express_area_update', [
  233. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city), 'expressTemplateModel' => $expressTemplateModel
  234. ]);
  235. }
  236. /**
  237. * @param $id
  238. * @return string
  239. * 运费区域模板区域查看方法
  240. */
  241. public function actionExpressAreaView($id)
  242. {
  243. $expressAreaModel = ExpressArea::findOne($id);
  244. $expressTemplateModel = ExpressTemplate::findOne($expressAreaModel->express_template);
  245. return $this->render('express_area_view', [
  246. 'model' => $expressAreaModel,
  247. 'expressTemplateModel' => $expressTemplateModel
  248. ]);
  249. }
  250. public function actionExpressAreaDelete($id)
  251. {
  252. $expressAreaModel = ExpressArea::findOne($id);
  253. $expressTemplateId = $expressAreaModel->express_template;
  254. $expressAreaModel->delete();
  255. return $this->redirect('express-area-list?id='.$expressTemplateId);
  256. }
  257. }