Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CustomerController.php 9.5 KiB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\api\models\BaseSupplier;
  4. use Yii;
  5. use backend\modules\motorcade\models\BusDepartment;
  6. use backend\modules\motorcade\models\BusDepartmentLink;
  7. use yii\data\ActiveDataProvider;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use backend\modules\motorcade\models\BusOrder;
  12. use backend\modules\api\models\BaseUser;
  13. use common\models\BaseSupplierLink;
  14. /**
  15. * CustomerController implements the CRUD actions for BusDepartment model.
  16. */
  17. class CustomerController extends Controller
  18. {
  19. public $layout = "@backend/modules/motorcade/views/layouts/iframe_new";
  20. /**
  21. * @inheritdoc
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['POST'],
  30. ],
  31. ],
  32. ];
  33. }
  34. /**
  35. * Lists all BusDepartment models.
  36. * @return mixed
  37. */
  38. // public function actionIndex()
  39. // {
  40. // $dataProvider = new ActiveDataProvider([
  41. // 'query' => BusDepartment::find(),
  42. // ]);
  43. //
  44. // return $this->render('index', [
  45. // 'dataProvider' => $dataProvider,
  46. // ]);
  47. // }
  48. /**
  49. * Displays a single BusDepartment model.
  50. * @param integer $id
  51. * @return mixed
  52. */
  53. public function actionView($id)
  54. {
  55. return $this->render('view', [
  56. 'model' => $this->findModel($id),
  57. ]);
  58. }
  59. /**
  60. * Creates a new BusDepartment model.
  61. * If creation is successful, the browser will be redirected to the 'view' page.
  62. * @return mixed
  63. */
  64. public function actionCreate()
  65. {
  66. $model = new BusDepartment();
  67. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  68. return $this->redirect(['view', 'id' => $model->ID]);
  69. } else {
  70. return $this->render('create', [
  71. 'model' => $model,
  72. ]);
  73. }
  74. }
  75. /**
  76. * Updates an existing BusDepartment model.
  77. * If update is successful, the browser will be redirected to the 'view' page.
  78. * @param integer $id
  79. * @return mixed
  80. */
  81. public function actionUpdate($id)
  82. {
  83. $model = $this->findModel($id);
  84. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  85. return $this->redirect(['view', 'id' => $model->ID]);
  86. } else {
  87. return $this->render('update', [
  88. 'model' => $model,
  89. ]);
  90. }
  91. }
  92. /**
  93. * Deletes an existing BusDepartment model.
  94. * If deletion is successful, the browser will be redirected to the 'index' page.
  95. * @param integer $id
  96. * @return mixed
  97. */
  98. public function actionDelete($id)
  99. {
  100. $this->findModel($id)->delete();
  101. return $this->redirect(['index']);
  102. }
  103. /**
  104. * Finds the BusDepartment model based on its primary key value.
  105. * If the model is not found, a 404 HTTP exception will be thrown.
  106. * @param integer $id
  107. * @return BusDepartment the loaded model
  108. * @throws NotFoundHttpException if the model cannot be found
  109. */
  110. protected function findModel($id)
  111. {
  112. if (($model = BusDepartment::findOne($id)) !== null) {
  113. return $model;
  114. } else {
  115. throw new NotFoundHttpException('The requested page does not exist.');
  116. }
  117. }
  118. public function actionIndex($op = 'customer')
  119. {
  120. if ($op == 'customer') {
  121. $model = new BusDepartment();
  122. // $model->load(Yii::$app->request->get());
  123. $dataProvider = $model->getIndex();
  124. }
  125. $data['view'] = 'index';
  126. $data['op'] = $op;
  127. return $this->render('_base', ['data' => $data, 'dataProvider' => $dataProvider]);
  128. }
  129. /**
  130. * User:Steven
  131. * Desc:添加客户(用车单位)
  132. */
  133. public function actionAddCustomer()
  134. {
  135. $bus_depart = new BusDepartment(['scenario' => 'add_customer']);
  136. $bus_depart->status = 610;
  137. if ($bus_depart->load(Yii::$app->request->post())) {
  138. $transaction = Yii::$app->db->beginTransaction();
  139. try {
  140. if (!$bus_depart->save()) {
  141. throw new \Exception();
  142. }
  143. $department_id = $bus_depart->attributes['ID'];
  144. $link_arr = array_values(Yii::$app->request->post('BusDepartmentLink'));
  145. foreach ($link_arr as $key => $item) {
  146. if ($item['contact_name'] == '') {
  147. continue;
  148. }
  149. $depart_link = new BusDepartmentLink();
  150. $depart_link->department_id = $department_id;
  151. $depart_link->setAttributes($item, false);
  152. if (!$depart_link->save()) {
  153. throw new \Exception();
  154. }
  155. }
  156. $transaction->commit();
  157. Yii::$app->session->setFlash('success', "添加成功!");
  158. return $this->redirect(array('customer/index'));
  159. } catch (\Exception $e) {
  160. Yii::$app->session->setFlash('success', "添加失败!");
  161. $transaction->rollBack();
  162. }
  163. }
  164. $bus_depart->depart_type = 469;
  165. $bus_depart->settle_type = 474;
  166. $bus_depart->settle_cycle = 295;
  167. //获取业务员
  168. $user = BaseUser::find()->select(['ID', 'USER_NAME', 'TRUE_NAME'])->where(['CANCEL_FLAG' => 0])->andWhere(['<>', 'TRUE_NAME', ''])->andFilterWhere(['MAIN_CORP_ID2' => Yii::$app->user->identity->MAIN_CORP_ID2])->asArray()->all();
  169. $data['Salesman'] = $user;
  170. $data['busDepartment'] = $bus_depart;
  171. $SupplierLink = new BusDepartmentLink();
  172. $data['SupplierLink'][] = $SupplierLink;
  173. return $this->render('_base', ['view' => 'addCustomer', 'data' => $data]);
  174. }
  175. /**
  176. * User:Steven
  177. * Desc:更新客户信息
  178. * @param $id
  179. */
  180. public function actionUpdateCustomer($id, $op = '')
  181. {
  182. $bus_department = BusDepartment::findOne($id);
  183. $bus_department->setScenario('add_customer');
  184. $SupplierLink = BusDepartmentLink::find()->where(['department_id' => $id, 'cancel_flag' => 0])->all();
  185. if (Yii::$app->request->post() && $bus_department->load(Yii::$app->request->post())) {
  186. //审核通过则把状态置为611 已审核 ,不通过则置为612 已停用, 只要在已审核状态下进行修改则把状态置为待审核
  187. if ($bus_department->status == 611)
  188. $bus_department->status = 610;
  189. if (isset($_POST['pass']))
  190. $bus_department->status = 611;
  191. elseif (isset($_POST['reject']))
  192. $bus_department->status = 612;
  193. $transaction = Yii::$app->db->beginTransaction();
  194. try {
  195. if (!$bus_department->save()) {
  196. throw new \Exception();
  197. }
  198. $department_id = $bus_department->attributes['ID'];
  199. BusDepartmentLink::updateAll(['cancel_flag' => 1], ['department_id' => $department_id]);
  200. $link_arr = array_values(Yii::$app->request->post('BusDepartmentLink'));
  201. foreach ($link_arr as $key => $item) {
  202. if ($item['contact_name'] == '') {
  203. continue;
  204. }
  205. $depart_link = (BusDepartmentLink::findOne($item['id']) != null) ? BusDepartmentLink::findOne($item['id']) : new BusDepartmentLink();
  206. $depart_link->department_id = $department_id;
  207. $depart_link->cancel_flag = 0;
  208. $depart_link->setAttributes($item, false);
  209. if (!$depart_link->save()) {
  210. throw new \Exception();
  211. }
  212. }
  213. $transaction->commit();
  214. $info = isset($_POST['pass']) ? '审核通过!' : '驳回成功!';
  215. Yii::$app->session->setFlash('success', $info);
  216. return $this->redirect(array('customer/index'));
  217. } catch (\Exception $e) {
  218. Yii::$app->session->setFlash('success', $info);
  219. $transaction->rollBack();
  220. }
  221. }
  222. $user = BaseUser::find()->select(['ID', 'USER_NAME', 'TRUE_NAME'])
  223. ->where(['CANCEL_FLAG' => 0])
  224. ->andWhere(['<>', 'TRUE_NAME', ''])
  225. ->andFilterWhere(['MAIN_CORP_ID2' => Yii::$app->user->identity->MAIN_CORP_ID2])
  226. ->asArray()->all();
  227. $data['Salesman'] = $user;
  228. $data['busDepartment'] = $bus_department;
  229. // $SupplierLink = new BusDepartmentLink();
  230. $data['SupplierLink'] = $SupplierLink;
  231. $data['op'] = $op;
  232. return $this->render('_base', ['view' => 'addCustomer', 'data' => $data]);
  233. }
  234. public function actionDoCustomer($id, $type)
  235. {
  236. $model = $this->findModel($id);
  237. $info = '';
  238. if ($type == 1) {
  239. $model->status = 612;
  240. $info = '停用成功';
  241. } elseif ($type == 2) {
  242. $model->status = 610;
  243. $info = '启用成功';
  244. } /*elseif ($type == 3) {
  245. $model->cancel_flag = 1;
  246. $info = '删除成功';
  247. }*/
  248. if ($model->save()) {
  249. return "<script>z.pjaxFinish('success','{$info}','#float-div');</script>";
  250. } else {
  251. return "<script>z.pjaxFinish('error','操作失败','#float-div');</script>";
  252. }
  253. }
  254. }
  255. ?>