SiteController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use app\models\LoginForm;
  15. /**
  16. * SiteController implements the CRUD actions for Orderreturn model.
  17. */
  18. class SiteController extends Controller
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['POST'],
  30. ],
  31. ],
  32. ];
  33. }
  34. public function actionError()
  35. {
  36. $exception = Yii::$app->errorHandler->exception;
  37. if ($exception !== null) {
  38. return $this->render('error', ['exception' => $exception,'url'=>'index']);
  39. }
  40. }
  41. /**
  42. * Lists all Orderreturn models.
  43. * @return mixed
  44. */
  45. public function actionIndex()
  46. {
  47. $request = Yii::$app->request;
  48. $shop=$request->get('shop',1);
  49. $searchModel = new OrderreturnSearch();
  50. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  51. return $this->render('index', [
  52. 'searchModel' => $searchModel,
  53. 'dataProvider' => $dataProvider,
  54. 'shop'=>$shop,
  55. 'dayStatis'=>$dayStatis,
  56. 'dayCount'=>$dayCount,
  57. 'monthStatis'=>$monthStatis,
  58. 'monthCount'=>$monthCount
  59. ]);
  60. }
  61. public function actionStatis(){
  62. if (Yii::$app->user->isGuest) {
  63. return $this->redirect(['login']);
  64. }
  65. $request = Yii::$app->request;
  66. $shop=$request->get('shop',1);
  67. $dayStatis=Orderreturn::find()
  68. ->select([ 'usage','count(*) as num'])
  69. ->groupBy('usage')
  70. ->where(['>','created_at',strtotime(date('Y-m-d'))])
  71. ->where(['shop'=>$shop])
  72. ->asArray()
  73. ->all();
  74. $dayCount=array_sum(array_column($dayStatis,'num'));
  75. $monthStatis=Orderreturn::find()
  76. ->select(['count(*) as num', 'usage'])
  77. ->groupBy('usage')
  78. ->where(['>','created_at',strtotime(date('Y-m'))])
  79. ->where(['shop'=>$shop])
  80. ->asArray()
  81. ->all();
  82. $monthCount=array_sum(array_column($monthStatis,'num'));
  83. return $this->render('statis', [
  84. 'shop'=>$shop,
  85. 'dayStatis'=>$dayStatis,
  86. 'dayCount'=>$dayCount,
  87. 'monthStatis'=>$monthStatis,
  88. 'monthCount'=>$monthCount
  89. ]);
  90. }
  91. public function actionStatisExport(){
  92. $request = Yii::$app->request;
  93. $shop=$request->get('shop',1);
  94. $dayStatis=Orderreturn::find()
  95. ->select([ 'usage','count(*) as num'])
  96. ->groupBy('usage')
  97. ->where(['>','created_at',strtotime(date('Y-m-d'))])
  98. ->where(['shop'=>$shop])
  99. ->asArray()
  100. ->all();
  101. $dayCount=array_sum(array_column($dayStatis,'num'));
  102. $monthStatis=Orderreturn::find()
  103. ->select(['count(*) as num', 'usage'])
  104. ->groupBy('usage')
  105. ->where(['>','created_at',strtotime(date('Y-m'))])
  106. ->where(['shop'=>$shop])
  107. ->asArray()
  108. ->all();
  109. $monthCount=array_sum(array_column($monthStatis,'num'));
  110. $spreadsheet = new Spreadsheet();
  111. $spreadsheet->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(30);
  112. $spreadsheet->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  113. //当日统计
  114. $spreadsheet->setActiveSheetIndex(0)->mergeCells('A1:B1');
  115. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1','当天退货数量统计 单位:个');
  116. $spreadsheet->setActiveSheetIndex(0)->getStyle('A1')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
  117. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A2','退货包裹数');
  118. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B2',$dayCount);
  119. $i=3;
  120. $usageType=['1' => "可用-请加库存",'2'=>'不可用-退回工厂','3'=>'精打理-退回国内','4'=>'精打理-留美仓','5'=>'泡发-发回国内'];
  121. foreach($dayStatis as $item){
  122. if(isset($usageType[$item['usage']])){
  123. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A'.$i,$usageType[$item['usage']]);
  124. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B'.$i,$item['num']);
  125. }
  126. $i++;
  127. }
  128. $i+=2;
  129. //当月统计
  130. $spreadsheet->setActiveSheetIndex(0)->mergeCells('A'.$i.':B'.$i);
  131. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A'.$i,'当月退货数量统计 单位:个');
  132. $spreadsheet->setActiveSheetIndex(0)->getStyle('A'.$i)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
  133. $i++;
  134. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A'.$i,'退货包裹数');
  135. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B'.$i,$dayCount);
  136. $i++;
  137. foreach($monthStatis as $item){
  138. if(isset($usageType[$item['usage']])){
  139. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A'.$i,$usageType[$item['usage']]);
  140. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B'.$i,$item['num']);
  141. }
  142. $i++;
  143. }
  144. $spreadsheet->setActiveSheetIndex(0)->getStyle('B1:B'.$i)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
  145. // Rename worksheet
  146. $spreadsheet->getActiveSheet()->setTitle('Simple');
  147. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  148. $spreadsheet->setActiveSheetIndex(0);
  149. $filename=($shop==1 ? "美国仓":"亚马逊")."退货订单".date("Ymd").".xlsx";
  150. // Redirect output to a client’s web browser (Xlsx)
  151. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  152. header('Content-Disposition: attachment;filename="'.$filename.'"');
  153. header('Cache-Control: max-age=0');
  154. // If you're serving to IE 9, then the following may be needed
  155. header('Cache-Control: max-age=1');
  156. // If you're serving to IE over SSL, then the following may be needed
  157. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  158. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  159. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  160. header('Pragma: public'); // HTTP/1.0
  161. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  162. $writer->save('php://output');
  163. exit;
  164. }
  165. /**
  166. * Displays a single Orderreturn model.
  167. * @param integer $id
  168. * @return mixed
  169. * @throws NotFoundHttpException if the model cannot be found
  170. */
  171. public function actionView($id)
  172. {
  173. $is_create=false;
  174. $headers = Yii::$app->request->headers;
  175. $referer = $headers->get('Referer');
  176. if(strpos($referer,'site/create')>-1){
  177. $is_create=true;
  178. }
  179. return $this->render('view', [
  180. 'model' => $this->findModel($id),
  181. 'is_create'=>$is_create,
  182. ]);
  183. }
  184. /**
  185. * Creates a new Orderreturn model.
  186. * If creation is successful, the browser will be redirected to the 'view' page.
  187. * @return mixed
  188. */
  189. public function actionCreate()
  190. {
  191. $request = Yii::$app->request;
  192. $shop = $request->getBodyParam('shop')??1;
  193. $model = new Orderreturn();
  194. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  195. return $this->redirect(['view', 'id' => $model->id]);
  196. }
  197. return $this->render('create', [
  198. 'model' => $model,
  199. 'shop'=>$shop,
  200. ]);
  201. }
  202. /**
  203. * Updates an existing Orderreturn model.
  204. * If update is successful, the browser will be redirected to the 'view' page.
  205. * @param integer $id
  206. * @return mixed
  207. * @throws NotFoundHttpException if the model cannot be found
  208. */
  209. public function actionUpdate($id)
  210. {
  211. $model = $this->findModel($id);
  212. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  213. return $this->redirect(['view', 'id' => $model->id]);
  214. }
  215. return $this->render('update', [
  216. 'model' => $model,
  217. ]);
  218. }
  219. /**
  220. * Deletes an existing Orderreturn model.
  221. * If deletion is successful, the browser will be redirected to the 'index' page.
  222. * @param integer $id
  223. * @return mixed
  224. * @throws NotFoundHttpException if the model cannot be found
  225. */
  226. public function actionDelete($id)
  227. {
  228. $this->findModel($id)->delete();
  229. return $this->redirect(['index']);
  230. }
  231. public function actionDeleteAll()
  232. {
  233. $request = Yii::$app->request;
  234. $id = $request->post('id');
  235. if(is_array($id)){
  236. $ids=implode(',',$id);
  237. $model = new Orderreturn();
  238. $model->deleteAll('id in (' . $ids . ')');
  239. }
  240. return $this->redirect(['index']);
  241. }
  242. public function actionImport(){
  243. $request = Yii::$app->request;
  244. $shop = $request->post('shop')??1;
  245. $model = new UploadExcel();
  246. if (Yii::$app->request->isPost) {
  247. $model->excel = UploadedFile::getInstance($model, 'excel');
  248. if ($inputFileName=$model->upload()) {
  249. $inputFileType = IOFactory::identify($inputFileName);
  250. $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
  251. $reader = IOFactory::createReader($inputFileType);
  252. $reader->setReadFilter($filterSubset);
  253. $spreadsheet = $reader->load($inputFileName);
  254. $spreadsheet->setActiveSheetIndex(0);
  255. $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
  256. $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
  257. $data=[];
  258. foreach($sheetData as $item){
  259. if(!isset($item['E'])||empty($item['E'])){
  260. continue;
  261. }
  262. $receiptdate=str_replace('.','-',$item['B'])??'';
  263. $data[]=[
  264. strtotime($receiptdate),
  265. $item['C'],
  266. $item['D'],
  267. $item['E'],
  268. $item['F'],
  269. $item['G'],
  270. $item['H'],
  271. $item['I'],
  272. $item['J'],
  273. $item['k'],
  274. time(),
  275. $shop,
  276. ];
  277. }
  278. if(empty($data)){
  279. throw new \yii\base\ErrorException('未读取到数据');
  280. }
  281. $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
  282. if(!$res){
  283. throw new \yii\base\ErrorException('数据库异常');
  284. return;
  285. }else{
  286. return $this->redirect(['index']);
  287. }
  288. }else{
  289. throw new \yii\base\ErrorException('上传文件失败');
  290. return;
  291. }
  292. }else{
  293. throw new \yii\web\NotFoundHttpException('错误请求');
  294. }
  295. return $this->render('upload', ['model' => $model]);
  296. }
  297. /**
  298. * Login action.
  299. *
  300. * @return Response|string
  301. */
  302. public function actionLogin()
  303. {
  304. if (!Yii::$app->user->isGuest) {
  305. return $this->goHome();
  306. }
  307. $model = new LoginForm();
  308. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  309. return $this->goBack();
  310. }
  311. $model->password = '';
  312. return $this->render('login', [
  313. 'model' => $model,
  314. ]);
  315. }
  316. /**
  317. * Logout action.
  318. *
  319. * @return Response
  320. */
  321. public function actionLogout()
  322. {
  323. Yii::$app->user->logout();
  324. return $this->goHome();
  325. }
  326. /**
  327. * Finds the Orderreturn model based on its primary key value.
  328. * If the model is not found, a 404 HTTP exception will be thrown.
  329. * @param integer $id
  330. * @return Orderreturn the loaded model
  331. * @throws NotFoundHttpException if the model cannot be found
  332. */
  333. protected function findModel($id)
  334. {
  335. if (($model = Orderreturn::findOne($id)) !== null) {
  336. return $model;
  337. }
  338. throw new NotFoundHttpException('The requested page does not exist.');
  339. }
  340. }