SiteController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $model = new UploadExcel();
  133. if (Yii::$app->request->isPost) {
  134. $model->excel = UploadedFile::getInstance($model, 'excel');
  135. if ($inputFileName=$model->upload()) {
  136. $inputFileType = IOFactory::identify($inputFileName);
  137. $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
  138. $reader = IOFactory::createReader($inputFileType);
  139. $reader->setReadFilter($filterSubset);
  140. $spreadsheet = $reader->load($inputFileName);
  141. $spreadsheet->setActiveSheetIndex(0);
  142. $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
  143. $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
  144. $data=[];
  145. foreach($sheetData as $item){
  146. if(!isset($item['E'])||empty($item['E'])){
  147. continue;
  148. }
  149. $receiptdate=str_replace('.','-',$item['B'])??'';
  150. $data[]=[
  151. $receiptdate,
  152. $item['C'],
  153. $item['D'],
  154. $item['E'],
  155. $item['F'],
  156. $item['G'],
  157. $item['H'],
  158. $item['I'],
  159. $item['J'],
  160. $item['k'],
  161. time(),
  162. $shop,
  163. ];
  164. }
  165. if(empty($data)){
  166. throw new \yii\base\ErrorException('未读取到数据');
  167. }
  168. $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
  169. if(!$res){
  170. throw new \yii\base\ErrorException('数据库异常');
  171. return;
  172. }else{
  173. return $this->redirect(['index']);
  174. }
  175. }else{
  176. throw new \yii\base\ErrorException('上传文件失败');
  177. return;
  178. }
  179. }else{
  180. throw new \yii\web\NotFoundHttpException('错误请求');
  181. }
  182. return $this->render('upload', ['model' => $model]);
  183. }
  184. /**
  185. * Login action.
  186. *
  187. * @return Response|string
  188. */
  189. public function actionLogin()
  190. {
  191. if (!Yii::$app->user->isGuest) {
  192. return $this->goHome();
  193. }
  194. $model = new LoginForm();
  195. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  196. return $this->goBack();
  197. }
  198. $model->password = '';
  199. return $this->render('login', [
  200. 'model' => $model,
  201. ]);
  202. }
  203. /**
  204. * Logout action.
  205. *
  206. * @return Response
  207. */
  208. public function actionLogout()
  209. {
  210. Yii::$app->user->logout();
  211. return $this->goHome();
  212. }
  213. /**
  214. * Finds the Orderreturn model based on its primary key value.
  215. * If the model is not found, a 404 HTTP exception will be thrown.
  216. * @param integer $id
  217. * @return Orderreturn the loaded model
  218. * @throws NotFoundHttpException if the model cannot be found
  219. */
  220. protected function findModel($id)
  221. {
  222. if (($model = Orderreturn::findOne($id)) !== null) {
  223. return $model;
  224. }
  225. throw new NotFoundHttpException('The requested page does not exist.');
  226. }
  227. }