SiteController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 actionDeleteAll()
  115. {
  116. $request = Yii::$app->request;
  117. $id = $request->post('id');
  118. if(is_array($id)){
  119. $ids=implode(',',$id);
  120. }
  121. $model = new Orderreturn(); //你要批量删除的表
  122. $model->deleteAll('id in (' . $ids . ')');
  123. return $this->redirect(['index']);
  124. }
  125. public function actionExport(){
  126. $request = Yii::$app->request;
  127. $shop = $request->post('shop')??1;
  128. // dd($shop);
  129. $model = new UploadExcel();
  130. if (Yii::$app->request->isPost) {
  131. $model->excel = UploadedFile::getInstance($model, 'excel');
  132. if ($inputFileName=$model->upload()) {
  133. $inputFileType = IOFactory::identify($inputFileName);
  134. $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
  135. $reader = IOFactory::createReader($inputFileType);
  136. $reader->setReadFilter($filterSubset);
  137. $spreadsheet = $reader->load($inputFileName);
  138. $spreadsheet->setActiveSheetIndex(0);
  139. $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
  140. $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
  141. $data=[];
  142. foreach($sheetData as $item){
  143. if(!isset($item['E'])||empty($item['E'])){
  144. continue;
  145. }
  146. $receiptdate=strtotime(str_replace('.','-',$item['B'])??'');
  147. $data[]=[
  148. $receiptdate,
  149. $item['C'],
  150. $item['D'],
  151. $item['E'],
  152. $item['F'],
  153. $item['G'],
  154. $item['H'],
  155. $item['I'],
  156. $item['J'],
  157. $item['k'],
  158. time(),
  159. $shop,
  160. ];
  161. }
  162. if(empty($data)){
  163. throw new \yii\base\ErrorException('未读取到数据');
  164. }
  165. $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
  166. if(!$res){
  167. throw new \yii\base\ErrorException('数据库异常');
  168. return;
  169. }else{
  170. return $this->redirect(['index']);
  171. }
  172. }else{
  173. throw new \yii\base\ErrorException('上传文件失败');
  174. return;
  175. }
  176. }else{
  177. throw new \yii\web\NotFoundHttpException('错误请求');
  178. }
  179. return $this->render('upload', ['model' => $model]);
  180. }
  181. /**
  182. * Finds the Orderreturn model based on its primary key value.
  183. * If the model is not found, a 404 HTTP exception will be thrown.
  184. * @param integer $id
  185. * @return Orderreturn the loaded model
  186. * @throws NotFoundHttpException if the model cannot be found
  187. */
  188. protected function findModel($id)
  189. {
  190. if (($model = Orderreturn::findOne($id)) !== null) {
  191. return $model;
  192. }
  193. throw new NotFoundHttpException('The requested page does not exist.');
  194. }
  195. }