SupplierController.php 9.9 KiB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\api\models\BaseUser;
  4. use backend\modules\zzcs\models\BaseSupplierPurchase;
  5. use backend\modules\zzcs\models\BaseSupplierSale;
  6. use Facebook\WebDriver\Remote\ExecuteMethod;
  7. use Yii;
  8. use common\models\BaseSupplier;
  9. use common\models\BaseSupplierLink;
  10. use yii\data\ActiveDataProvider;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. /**
  15. * SupplierController implements the CRUD actions for BaseSupplier model.
  16. */
  17. class SupplierController 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 BaseSupplier models.
  36. * @return mixed
  37. */
  38. // public function actionIndex()
  39. // {
  40. // $dataProvider = new ActiveDataProvider([
  41. // 'query' => BaseSupplier::find(),
  42. // ]);
  43. //
  44. // return $this->render('index', [
  45. // 'dataProvider' => $dataProvider,
  46. // ]);
  47. // }
  48. /**
  49. * Displays a single BaseSupplier 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 BaseSupplier 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 BaseSupplier();
  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 BaseSupplier 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 BaseSupplier 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 BaseSupplier 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 BaseSupplier the loaded model
  108. * @throws NotFoundHttpException if the model cannot be found
  109. */
  110. protected function findModel($id)
  111. {
  112. if (($model = BaseSupplier::findOne($id)) !== null) {
  113. return $model;
  114. } else {
  115. throw new NotFoundHttpException('The requested page does not exist.');
  116. }
  117. }
  118. /**
  119. * User:Steven
  120. * Desc:添加供应商
  121. * @return string
  122. */
  123. public function actionAddSupplier()
  124. {
  125. $Supplier = new BaseSupplier();
  126. $Supplier->SUPPLIER_TYPE = 187; //供应商或渠道商标志 187:供应商
  127. $Supplier->MANAGE_TYPE = 1; //经营性质 1:公司 2:个人
  128. if ($Supplier->load(Yii::$app->request->post())) {
  129. $transaction = Yii::$app->db->beginTransaction();
  130. try {
  131. if (!$Supplier->save()) {
  132. throw new \Exception();
  133. }
  134. $supplier_id = $Supplier->attributes['ID'];
  135. $supplier_parc = new BaseSupplierPurchase();
  136. $supplier_parc->SUPPLIER_ID = $supplier_id;
  137. $supplier_parc->PRODUCT_TYPE = $Supplier->TYPE;
  138. $supplier_parc->PURCHASER_NAME = Yii::$app->user->id;
  139. if (!$supplier_parc->save()) {
  140. throw new \Exception();
  141. }
  142. $link_arr = array_values(Yii::$app->request->post('BaseSupplierLink'));
  143. foreach ($link_arr as $key => $item) {
  144. if ($item['CONTACT_NAME'] == '') {
  145. continue;
  146. }
  147. $supplier_link = new BaseSupplierLink();
  148. $supplier_link->SUPPLIER_ID = $supplier_id;
  149. $supplier_link->setAttributes($item, false);
  150. if (!$supplier_link->save()) {
  151. throw new \Exception();
  152. }
  153. }
  154. $transaction->commit();
  155. Yii::$app->session->setFlash('success', "添加成功!");
  156. return $this->redirect(array('supplier/index'));
  157. } catch (\Exception $e) {
  158. Yii::$app->session->setFlash('success', "添加失败!");
  159. $transaction->rollBack();
  160. }
  161. }
  162. $SupplierLink = new BaseSupplierLink();
  163. $Supplier->TYPE = 259;
  164. $Supplier->SETT_TYPE = 474;
  165. $Supplier->SETT_FREQUENCY = 295;
  166. $data['data']['SupplierLink'][] = $SupplierLink;
  167. $data['data']['Supplier'] = $Supplier;
  168. return $this->render('base', ['view' => 'addSupplier', 'data' => $data]);
  169. }
  170. /**
  171. * User:Steven
  172. * Desc:更新供应商
  173. * @param $id
  174. */
  175. public function actionUpdateSupplier($id, $op = '') //op如果为review 则为审核,没有则为更新
  176. {
  177. $Supplier = BaseSupplier::findOne($id);
  178. $SupplierType = BaseSupplierPurchase::find()->select(['PRODUCT_TYPE'])->where(['SUPPLIER_ID' => $id, 'CANCEL_FLAG' => 0])->one();
  179. $Supplier->TYPE = isset($SupplierType->PRODUCT_TYPE) ? $SupplierType->PRODUCT_TYPE : 259;
  180. $SupplierLink = BaseSupplierLink::find()->select(['ID', 'SUPPLIER_ID', 'LINK_NAME', 'CONTACT_TYPE', 'CONTACT_NAME', 'CONTACT_MOBILE'])->where(['SUPPLIER_ID' => $id, 'CANCEL_FLAG' => 0])->all();
  181. if (Yii::$app->request->post() && $Supplier->load(Yii::$app->request->post())) {
  182. //审核通过则把状态置为611 已审核 ,不通过则置为612 已停用, 只要在已审核状态下进行修改则把状态置为待审核
  183. if ($Supplier->STATUS == 611)
  184. $Supplier->STATUS = 610;
  185. if (isset($_POST['pass']))
  186. $Supplier->STATUS = 611;
  187. elseif (isset($_POST['reject']))
  188. $Supplier->STATUS = 612;
  189. $transaction = Yii::$app->db->beginTransaction();
  190. try {
  191. if (!$Supplier->save()) {
  192. throw new \Exception();
  193. }
  194. $SupplierType->SUPPLIER_ID = $id;
  195. $SupplierType->PRODUCT_TYPE = $Supplier->TYPE;
  196. $SupplierType->PURCHASER_NAME = Yii::$app->user->id;
  197. if (!$SupplierType->save()) {
  198. throw new \Exception();
  199. }
  200. BaseSupplierLink::updateAll(['cancel_flag' => 1], ['SUPPLIER_ID' => $id]);
  201. $link_arr = Yii::$app->request->post('BaseSupplierLink');
  202. foreach ($link_arr as $key => $item) {
  203. if ($item['CONTACT_NAME'] == '') {
  204. continue;
  205. }
  206. $sup_link = (isset($item['ID']) && BaseSupplierLink::findOne($item['ID']) != null) ? BaseSupplierLink::findOne($item['ID']) : new BaseSupplierLink();
  207. $rs = $sup_link->load($item, '');
  208. $sup_link->SUPPLIER_ID = $id;
  209. $sup_link->CANCEL_FLAG = 0;
  210. if (!$rs || !$sup_link->save()) {
  211. throw new \Exception();
  212. }
  213. }
  214. $transaction->commit();
  215. $info = isset($_POST['pass']) ? '审核通过!' : '驳回成功!';
  216. Yii::$app->session->setFlash('success', $info);
  217. return $this->redirect(array('supplier/index'));
  218. } catch (\Exception $e) {
  219. Yii::$app->session->setFlash('success', $info);
  220. $transaction->rollBack();
  221. }
  222. }
  223. empty($SupplierLink) ? $data['data']['SupplierLink'][0] = new BaseSupplierLink() : $data['data']['SupplierLink'] = $SupplierLink;
  224. $data['data']['Supplier'] = $Supplier;
  225. $data['op'] = $op;
  226. return $this->render('base', ['view' => 'addSupplier', 'data' => $data]);
  227. }
  228. public function actionIndex($op = 'supplier')
  229. {
  230. if ($op == 'supplier') {
  231. $model = new BaseSupplier();
  232. $model->load(Yii::$app->request->get());
  233. $dataProvider = $model->getIndex();
  234. }
  235. $data['view'] = '../customer/index';
  236. $data['op'] = $op;
  237. return $this->render('_base', ['data' => $data, 'dataProvider' => $dataProvider]);
  238. }
  239. //停用或启用或删除供应商
  240. //1为停用 2为启用 3为删除
  241. public function actionDoSupplier($id, $type)
  242. {
  243. $model = $this->findModel($id);
  244. $info = '';
  245. if ($type == 1) {
  246. $model->STATUS = 612;
  247. $info = '停用成功';
  248. } elseif ($type == 2) {
  249. $model->STATUS = 610;
  250. $info = '启用成功';
  251. } /*elseif ($type == 3) {
  252. $model->CANCEL_FLAG = 1;
  253. $info = '删除成功';
  254. }*/
  255. if ($model->save()) {
  256. Yii::$app->session->setFlash('success', "$info");
  257. return "<script>pjaxFinish(\"$info\");</script>";
  258. } else {
  259. Yii::$app->session->setFlash('warning', $info);
  260. return "<script>pjaxFinish(\"$info\");</script>";
  261. }
  262. }
  263. }
  264. ?>