SiteController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. //
  148. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  149. $spreadsheet->setActiveSheetIndex(0);
  150. $filename=($shop==1 ? "美国仓":"亚马逊")."退货订单".date("Ymd").".xlsx";
  151. // Redirect output to a client’s web browser (Xlsx)
  152. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  153. header('Content-Disposition: attachment;filename="'.$filename.'"');
  154. header('Cache-Control: max-age=0');
  155. // If you're serving to IE 9, then the following may be needed
  156. header('Cache-Control: max-age=1');
  157. // If you're serving to IE over SSL, then the following may be needed
  158. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  159. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  160. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  161. header('Pragma: public'); // HTTP/1.0
  162. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  163. $writer->save('php://output');
  164. exit;
  165. }
  166. public function actionOrderExport(){
  167. $request = Yii::$app->request;
  168. $id = $request->post('id');
  169. if(!is_array($id)){
  170. throw new \yii\base\ErrorException('参数异常');
  171. }
  172. $ids=implode(',',$id);
  173. $orders=Orderreturn::find()
  174. ->select(['*'])
  175. ->where('id in (' . $ids . ')')
  176. ->asArray()
  177. ->all();
  178. $spreadsheet = new Spreadsheet();
  179. //表头
  180. $spreadsheet->setActiveSheetIndex(0)->mergeCells('A1:L1');
  181. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1','美国店退货收货详情统计表');
  182. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A2','序号');
  183. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B2','收货日期');
  184. $spreadsheet->setActiveSheetIndex(0)->setCellValue('C2','退回货运');
  185. $spreadsheet->setActiveSheetIndex(0)->setCellValue('D2','收到单号');
  186. $spreadsheet->setActiveSheetIndex(0)->setCellValue('E2','网站');
  187. $spreadsheet->setActiveSheetIndex(0)->setCellValue('F2','订单号');
  188. $spreadsheet->setActiveSheetIndex(0)->setCellValue('G2','订单编号');
  189. $spreadsheet->setActiveSheetIndex(0)->setCellValue('H2','客户名');
  190. $spreadsheet->setActiveSheetIndex(0)->setCellValue('I2','原订单货物详情');
  191. $spreadsheet->setActiveSheetIndex(0)->setCellValue('J2','货品详情');
  192. $spreadsheet->setActiveSheetIndex(0)->setCellValue('K2','完好程度/使用情况');
  193. $spreadsheet->setActiveSheetIndex(0)->setCellValue('L2','备注');
  194. $i=1;
  195. $usageType=['1' => "可用-请加库存",'2'=>'不可用-退回工厂','3'=>'精打理-退回国内','4'=>'精打理-留美仓','5'=>'泡发-发回国内'];
  196. $deliveryType=['1' =>'USPS','2'=>'UPS','3'=>'FedEx','4'=>'客人到店退货','5'=>'DHL' ];
  197. foreach($orders as $item){
  198. // if(isset($usageType[$item['usage']])){
  199. $delivery=isset($deliveryType[$item['delivery']])?$deliveryType[$item['delivery']]:$item['delivery'];
  200. $usage=isset($usageType[$item['usage']])?$usageType[$item['usage']]:$item['usage'];
  201. $spreadsheet->setActiveSheetIndex(0)->setCellValue('A'.($i+2),$i);
  202. $spreadsheet->setActiveSheetIndex(0)->setCellValue('B'.($i+2),date('Y-m-d',$item['receiptdate']));
  203. $spreadsheet->setActiveSheetIndex(0)->setCellValue('C'.($i+2),$delivery);
  204. $spreadsheet->setActiveSheetIndex(0)->setCellValue('D'.($i+2),$item['deliveryid']);
  205. $spreadsheet->setActiveSheetIndex(0)->setCellValue('E'.($i+2),$item['website']);
  206. $spreadsheet->setActiveSheetIndex(0)->setCellValue('F'.($i+2),'');
  207. $spreadsheet->setActiveSheetIndex(0)->setCellValue('G'.($i+2),'');
  208. $spreadsheet->setActiveSheetIndex(0)->setCellValue('H'.($i+2),$item['customer']);
  209. $spreadsheet->setActiveSheetIndex(0)->setCellValue('I'.($i+2),$item['origin_goodsinfo']);
  210. $spreadsheet->setActiveSheetIndex(0)->setCellValue('J'.($i+2),$item['goodsinfo']);
  211. $spreadsheet->setActiveSheetIndex(0)->setCellValue('K'.($i+2),$usage);
  212. $spreadsheet->setActiveSheetIndex(0)->setCellValue('L'.($i+2),$item['mark']);
  213. // }
  214. $i++;
  215. }
  216. $spreadsheet->getActiveSheet()->setTitle('美国店退货');
  217. //
  218. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  219. $spreadsheet->setActiveSheetIndex(0);
  220. $filename=($shop==1 ? "美国仓":"亚马逊")."退货订单".date("Ymd").".xlsx";
  221. // Redirect output to a client’s web browser (Xlsx)
  222. // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  223. // header('Content-Disposition: attachment;filename="'.$filename.'"');
  224. // header('Cache-Control: max-age=0');
  225. // // If you're serving to IE 9, then the following may be needed
  226. // header('Cache-Control: max-age=1');
  227. // // If you're serving to IE over SSL, then the following may be needed
  228. // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  229. // header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  230. // header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  231. // header('Pragma: public'); // HTTP/1.0
  232. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  233. ob_start();
  234. $writer->save('php://output');
  235. $xlsData = ob_get_contents();
  236. ob_end_clean();
  237. $response = array(
  238. 'op' => 'ok',
  239. 'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
  240. );
  241. die(json_encode($response));
  242. }
  243. /**
  244. * Displays a single Orderreturn model.
  245. * @param integer $id
  246. * @return mixed
  247. * @throws NotFoundHttpException if the model cannot be found
  248. */
  249. public function actionView($id)
  250. {
  251. $is_create=false;
  252. $headers = Yii::$app->request->headers;
  253. $referer = $headers->get('Referer');
  254. if(strpos($referer,'site/create')>-1){
  255. $is_create=true;
  256. }
  257. return $this->render('view', [
  258. 'model' => $this->findModel($id),
  259. 'is_create'=>$is_create,
  260. ]);
  261. }
  262. /**
  263. * Creates a new Orderreturn model.
  264. * If creation is successful, the browser will be redirected to the 'view' page.
  265. * @return mixed
  266. */
  267. public function actionCreate()
  268. {
  269. $request = Yii::$app->request;
  270. $shop = $request->getBodyParam('shop')??1;
  271. $model = new Orderreturn();
  272. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  273. return $this->redirect(['view', 'id' => $model->id]);
  274. }
  275. return $this->render('create', [
  276. 'model' => $model,
  277. 'shop'=>$shop,
  278. ]);
  279. }
  280. /**
  281. * Updates an existing Orderreturn model.
  282. * If update is successful, the browser will be redirected to the 'view' page.
  283. * @param integer $id
  284. * @return mixed
  285. * @throws NotFoundHttpException if the model cannot be found
  286. */
  287. public function actionUpdate($id)
  288. {
  289. $model = $this->findModel($id);
  290. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  291. return $this->redirect(['view', 'id' => $model->id]);
  292. }
  293. return $this->render('update', [
  294. 'model' => $model,
  295. ]);
  296. }
  297. /**
  298. * Deletes an existing Orderreturn model.
  299. * If deletion is successful, the browser will be redirected to the 'index' page.
  300. * @param integer $id
  301. * @return mixed
  302. * @throws NotFoundHttpException if the model cannot be found
  303. */
  304. public function actionDelete($id)
  305. {
  306. $this->findModel($id)->delete();
  307. return $this->redirect(['index']);
  308. }
  309. public function actionDeleteAll()
  310. {
  311. $request = Yii::$app->request;
  312. $id = $request->post('id');
  313. if(is_array($id)){
  314. $ids=implode(',',$id);
  315. $model = new Orderreturn();
  316. $model->deleteAll('id in (' . $ids . ')');
  317. }
  318. return $this->redirect(['index']);
  319. }
  320. public function actionImport(){
  321. $request = Yii::$app->request;
  322. $shop = $request->post('shop')??1;
  323. $model = new UploadExcel();
  324. if (Yii::$app->request->isPost) {
  325. $model->excel = UploadedFile::getInstance($model, 'excel');
  326. if ($inputFileName=$model->upload()) {
  327. $inputFileType = IOFactory::identify($inputFileName);
  328. $filterSubset = new ExcelFilter(3, 15, range('B', 'K'));
  329. $reader = IOFactory::createReader($inputFileType);
  330. $reader->setReadFilter($filterSubset);
  331. $spreadsheet = $reader->load($inputFileName);
  332. $spreadsheet->setActiveSheetIndex(0);
  333. $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
  334. $key=['receiptdate','num','delivery','deliveryid','website','customer','origin_goodsinfo','goodsinfo','usage','mark','created_at','shop'];
  335. $data=[];
  336. foreach($sheetData as $item){
  337. if(!isset($item['E'])||empty($item['E'])){
  338. continue;
  339. }
  340. $receiptdate=str_replace('.','-',$item['B'])??'';
  341. $data[]=[
  342. strtotime($receiptdate),
  343. $item['C'],
  344. $item['D'],
  345. $item['E'],
  346. $item['F'],
  347. $item['G'],
  348. $item['H'],
  349. $item['I'],
  350. $item['J'],
  351. $item['k'],
  352. time(),
  353. $shop,
  354. ];
  355. }
  356. if(empty($data)){
  357. throw new \yii\base\ErrorException('未读取到数据');
  358. }
  359. $res=Yii::$app->db->createCommand()->batchInsert('order_return', $key, $data)->execute();
  360. if(!$res){
  361. throw new \yii\base\ErrorException('数据库异常');
  362. return;
  363. }else{
  364. return $this->redirect(['index']);
  365. }
  366. }else{
  367. throw new \yii\base\ErrorException('上传文件失败');
  368. return;
  369. }
  370. }else{
  371. throw new \yii\web\NotFoundHttpException('错误请求');
  372. }
  373. return $this->render('upload', ['model' => $model]);
  374. }
  375. /**
  376. * Login action.
  377. *
  378. * @return Response|string
  379. */
  380. public function actionLogin()
  381. {
  382. if (!Yii::$app->user->isGuest) {
  383. return $this->goHome();
  384. }
  385. $model = new LoginForm();
  386. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  387. return $this->goBack();
  388. }
  389. $model->password = '';
  390. return $this->render('login', [
  391. 'model' => $model,
  392. ]);
  393. }
  394. /**
  395. * Logout action.
  396. *
  397. * @return Response
  398. */
  399. public function actionLogout()
  400. {
  401. Yii::$app->user->logout();
  402. return $this->goHome();
  403. }
  404. /**
  405. * Finds the Orderreturn model based on its primary key value.
  406. * If the model is not found, a 404 HTTP exception will be thrown.
  407. * @param integer $id
  408. * @return Orderreturn the loaded model
  409. * @throws NotFoundHttpException if the model cannot be found
  410. */
  411. protected function findModel($id)
  412. {
  413. if (($model = Orderreturn::findOne($id)) !== null) {
  414. return $model;
  415. }
  416. throw new NotFoundHttpException('The requested page does not exist.');
  417. }
  418. }