SiteController.php 6.2 KB

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