SiteController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use app\models\Orderreturn;
  5. use app\models\OrderreturnSearch;
  6. use app\models\UploadExcel;
  7. use app\models\ExcelFilter;
  8. use yii\web\UploadedFile;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use app\models\LoginForm;
  14. /**
  15. * SiteController implements the CRUD actions for Orderreturn model.
  16. */
  17. class SiteController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['POST'],
  29. ],
  30. ],
  31. ];
  32. }
  33. public function actionError()
  34. {
  35. $exception = Yii::$app->errorHandler->exception;
  36. if ($exception !== null) {
  37. return $this->render('error', ['exception' => $exception,'url'=>'index']);
  38. }
  39. }
  40. /**
  41. * Lists all Orderreturn models.
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. if (Yii::$app->user->isGuest) {
  47. return $this->redirect(['login']);
  48. }
  49. $request = Yii::$app->request;
  50. $shop=$request->get('shop',1);
  51. $searchModel = new OrderreturnSearch();
  52. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  53. return $this->render('index', [
  54. 'searchModel' => $searchModel,
  55. 'dataProvider' => $dataProvider,
  56. 'shop'=>$shop,
  57. ]);
  58. }
  59. /**
  60. * Displays a single Orderreturn model.
  61. * @param integer $id
  62. * @return mixed
  63. * @throws NotFoundHttpException if the model cannot be found
  64. */
  65. public function actionView($id)
  66. {
  67. return $this->render('view', [
  68. 'model' => $this->findModel($id),
  69. ]);
  70. }
  71. /**
  72. * Creates a new Orderreturn model.
  73. * If creation is successful, the browser will be redirected to the 'view' page.
  74. * @return mixed
  75. */
  76. public function actionCreate()
  77. {
  78. $request = Yii::$app->request;
  79. $shop = $request->getBodyParam('shop')??1;
  80. $model = new Orderreturn();
  81. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  82. return $this->redirect(['view', 'id' => $model->id]);
  83. }
  84. return $this->render('create', [
  85. 'model' => $model,
  86. 'shop'=>$shop,
  87. ]);
  88. }
  89. /**
  90. * Updates an existing Orderreturn model.
  91. * If update is successful, the browser will be redirected to the 'view' page.
  92. * @param integer $id
  93. * @return mixed
  94. * @throws NotFoundHttpException if the model cannot be found
  95. */
  96. public function actionUpdate($id)
  97. {
  98. $model = $this->findModel($id);
  99. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100. return $this->redirect(['view', 'id' => $model->id]);
  101. }
  102. return $this->render('update', [
  103. 'model' => $model,
  104. ]);
  105. }
  106. /**
  107. * Deletes an existing Orderreturn model.
  108. * If deletion is successful, the browser will be redirected to the 'index' page.
  109. * @param integer $id
  110. * @return mixed
  111. * @throws NotFoundHttpException if the model cannot be found
  112. */
  113. public function actionDelete($id)
  114. {
  115. $this->findModel($id)->delete();
  116. return $this->redirect(['index']);
  117. }
  118. public function actionDeleteAll()
  119. {
  120. $request = Yii::$app->request;
  121. $id = $request->post('id');
  122. if(is_array($id)){
  123. $ids=implode(',',$id);
  124. $model = new Orderreturn();
  125. $model->deleteAll('id in (' . $ids . ')');
  126. }
  127. return $this->redirect(['index']);
  128. }
  129. public function actionExport(){
  130. $request = Yii::$app->request;
  131. $shop = $request->post('shop')??1;
  132. // dd($shop);
  133. $model = new UploadExcel();
  134. if (Yii::$app->request->isPost) {
  135. $model->excel = UploadedFile::getInstance($model, 'excel');
  136. if ($inputFileName=$model->upload()) {
  137. $inputFileType = IOFactory::identify($inputFileName);
  138. $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
  139. $reader = IOFactory::createReader($inputFileType);
  140. $reader->setReadFilter($filterSubset);
  141. $spreadsheet = $reader->load($inputFileName);
  142. $spreadsheet->setActiveSheetIndex(0);
  143. $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
  144. $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
  145. $data=[];
  146. foreach($sheetData as $item){
  147. if(!isset($item['E'])||empty($item['E'])){
  148. continue;
  149. }
  150. $receiptdate=strtotime(str_replace('.','-',$item['B'])??'');
  151. $data[]=[
  152. $receiptdate,
  153. $item['C'],
  154. $item['D'],
  155. $item['E'],
  156. $item['F'],
  157. $item['G'],
  158. $item['H'],
  159. $item['I'],
  160. $item['J'],
  161. $item['k'],
  162. time(),
  163. $shop,
  164. ];
  165. }
  166. if(empty($data)){
  167. throw new \yii\base\ErrorException('未读取到数据');
  168. }
  169. $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
  170. if(!$res){
  171. throw new \yii\base\ErrorException('数据库异常');
  172. return;
  173. }else{
  174. return $this->redirect(['index']);
  175. }
  176. }else{
  177. throw new \yii\base\ErrorException('上传文件失败');
  178. return;
  179. }
  180. }else{
  181. throw new \yii\web\NotFoundHttpException('错误请求');
  182. }
  183. return $this->render('upload', ['model' => $model]);
  184. }
  185. /**
  186. * Login action.
  187. *
  188. * @return Response|string
  189. */
  190. public function actionLogin()
  191. {
  192. if (!Yii::$app->user->isGuest) {
  193. return $this->goHome();
  194. }
  195. $model = new LoginForm();
  196. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  197. return $this->goBack();
  198. }
  199. $model->password = '';
  200. return $this->render('login', [
  201. 'model' => $model,
  202. ]);
  203. }
  204. /**
  205. * Logout action.
  206. *
  207. * @return Response
  208. */
  209. public function actionLogout()
  210. {
  211. Yii::$app->user->logout();
  212. return $this->goHome();
  213. }
  214. /**
  215. * Finds the Orderreturn model based on its primary key value.
  216. * If the model is not found, a 404 HTTP exception will be thrown.
  217. * @param integer $id
  218. * @return Orderreturn the loaded model
  219. * @throws NotFoundHttpException if the model cannot be found
  220. */
  221. protected function findModel($id)
  222. {
  223. if (($model = Orderreturn::findOne($id)) !== null) {
  224. return $model;
  225. }
  226. throw new NotFoundHttpException('The requested page does not exist.');
  227. }
  228. }