123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace app\controllers;
- use Yii;
- use app\models\Orderreturn;
- use app\models\OrderreturnSearch;
- use app\models\UploadExcel;
- use app\models\ExcelFilter;
- use yii\web\UploadedFile;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- /**
- * SiteController implements the CRUD actions for Orderreturn model.
- */
- class SiteController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
- public function actionError()
- {
- $exception = Yii::$app->errorHandler->exception;
- if ($exception !== null) {
- return $this->render('error', ['exception' => $exception,'url'=>'index']);
- }
- }
- /**
- * Lists all Orderreturn models.
- * @return mixed
- */
- public function actionIndex()
- {
- $request = Yii::$app->request;
- $shop=$request->get('shop',1);
- $searchModel = new OrderreturnSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- 'shop'=>$shop,
- ]);
- }
- /**
- * Displays a single Orderreturn model.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Orderreturn model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $request = Yii::$app->request;
- $shop = $request->getBodyParam('shop')??1;
- $model = new Orderreturn();
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- return $this->render('create', [
- 'model' => $model,
- 'shop'=>$shop,
- ]);
- }
- /**
- * Updates an existing Orderreturn model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- /**
- * Deletes an existing Orderreturn model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
-
- return $this->redirect(['index']);
- }
- public function actionDeleteAll()
- {
- $request = Yii::$app->request;
- $id = $request->post('id');
- if(is_array($id)){
- $ids=implode(',',$id);
- }
- $model = new Orderreturn(); //你要批量删除的表
- $model->deleteAll('id in (' . $ids . ')');
- return $this->redirect(['index']);
- }
- public function actionExport(){
-
- $request = Yii::$app->request;
- $shop = $request->post('shop')??1;
- // dd($shop);
- $model = new UploadExcel();
- if (Yii::$app->request->isPost) {
- $model->excel = UploadedFile::getInstance($model, 'excel');
- if ($inputFileName=$model->upload()) {
- $inputFileType = IOFactory::identify($inputFileName);
- $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
- $reader = IOFactory::createReader($inputFileType);
- $reader->setReadFilter($filterSubset);
- $spreadsheet = $reader->load($inputFileName);
- $spreadsheet->setActiveSheetIndex(0);
- $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
- $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
- $data=[];
- foreach($sheetData as $item){
- if(!isset($item['E'])||empty($item['E'])){
- continue;
- }
- $receiptdate=strtotime(str_replace('.','-',$item['B'])??'');
- $data[]=[
- $receiptdate,
- $item['C'],
- $item['D'],
- $item['E'],
- $item['F'],
- $item['G'],
- $item['H'],
- $item['I'],
- $item['J'],
- $item['k'],
- time(),
- $shop,
- ];
- }
- if(empty($data)){
- throw new \yii\base\ErrorException('未读取到数据');
- }
- $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
- if(!$res){
- throw new \yii\base\ErrorException('数据库异常');
- return;
- }else{
- return $this->redirect(['index']);
- }
- }else{
- throw new \yii\base\ErrorException('上传文件失败');
- return;
- }
- }else{
- throw new \yii\web\NotFoundHttpException('错误请求');
- }
-
- return $this->render('upload', ['model' => $model]);
- }
- /**
- * Finds the Orderreturn model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Orderreturn the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Orderreturn::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
|