ShopController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use Codeception\PHPUnit\ResultPrinter\HTML;
  10. use common\helpers\Date;
  11. use common\helpers\Form;
  12. use common\helpers\user\Info;
  13. use common\models\DealType;
  14. use common\models\DecOrder;
  15. use common\models\forms\DeclarationForm;
  16. use common\models\forms\OrderForm;
  17. use common\models\Order;
  18. use common\models\OrderDec;
  19. use common\models\OrderGoods;
  20. use common\models\OrderGoodsDec;
  21. use common\models\ReceiveAddress;
  22. use common\models\Region;
  23. use common\models\ShopGoods;
  24. use common\models\User;
  25. use common\models\UserBonus;
  26. use common\models\UserWallet;
  27. use Yii;
  28. class ShopController extends BaseController {
  29. public $modelClass = DecOrder::class;
  30. /**
  31. * 商品列表
  32. * @return mixed
  33. * @throws \yii\web\HttpException
  34. */
  35. public function actionIndex() {
  36. // 商品分类
  37. $categoryType = \Yii::$app->request->get('categoryType', 1);
  38. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  39. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  40. if($isStudio==1){
  41. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  42. }
  43. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  44. $params[':CATEGORY_TYPE'] = intval($categoryType);
  45. $data = ShopGoods::lists($condition, $params, [
  46. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  47. 'from' => ShopGoods::tableName(),
  48. ]);
  49. foreach ($data['list'] as $key => $value) {
  50. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  51. $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  52. } else {
  53. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  54. }
  55. // $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  56. }
  57. return static::notice($data);
  58. }
  59. /**
  60. * 获取商品详情
  61. * @return mixed
  62. * @throws \yii\web\HttpException
  63. */
  64. public function actionGoodsDetail(){
  65. $id = \Yii::$app->request->get('id');
  66. $data = null;
  67. if($id){
  68. $data = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id]);
  69. }
  70. return static::notice($data);
  71. }
  72. /**
  73. * 购物车订单展示
  74. * @throws \yii\web\HttpException
  75. */
  76. public function actionShowCart(){
  77. $userId = \Yii::$app->user->id;
  78. $payList = ShopGoods::payTypes();
  79. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  80. if($allAddress) {
  81. foreach ($allAddress as $key => $row) {
  82. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  83. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  84. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  85. }
  86. }
  87. $userBalance = [
  88. 'points' => 0,
  89. 'cash' => 0,
  90. 'exchange' => 0,
  91. 'travel_points' => 0,
  92. 'car_points' => 0,
  93. 'house_points' => 0,
  94. ];
  95. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  96. $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
  97. $userBalance['exchange'] = $userBonusResult['EXCHANGE_POINTS'];
  98. $userBalance['travel_points'] = $userBonusResult['TRAVEL_POINTS'];
  99. $userBalance['car_points'] = $userBonusResult['CAR_POINTS'];
  100. $userBalance['house_points'] = $userBonusResult['HOUSE_POINTS'];
  101. }
  102. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  103. $userBalance['cash'] = $userCashResult['CASH'];
  104. }
  105. return static::notice(
  106. [
  107. 'payList'=>$payList,
  108. 'allAddress'=>$allAddress,
  109. 'userBalance'=>$userBalance,
  110. 'sellType' => ShopGoods::CATEGORY_TYPE,
  111. ]);
  112. }
  113. /**
  114. * 确认订单
  115. */
  116. public function actionSureOrder(){
  117. if (\Yii::$app->request->isPost) {
  118. $formModel = new OrderForm();
  119. $formModel->scenario = 'userOrder';
  120. $formModel->remark = '复销备注';
  121. $post = \Yii::$app->request->post();
  122. $post['type'] = DeclarationForm::TYPE_FX;
  123. if ($formModel->load($post, '') && $order = $formModel->add()) {
  124. return static::notice($order);
  125. } else {
  126. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  127. }
  128. }
  129. }
  130. /**
  131. * 订单支付成功
  132. * @throws \yii\web\HttpException
  133. */
  134. public function actionPaySuccess(){
  135. $orderSn = \Yii::$app->request->get('orderSn');
  136. $data = null;
  137. if($orderSn){
  138. $data = Order::findOneAsArray('SN=:SN', [':SN'=>$orderSn]);
  139. }
  140. return static::notice($data);
  141. }
  142. /**
  143. * 订单支付成功
  144. * @throws \yii\web\HttpException
  145. */
  146. public function actionVerifyOrder(){
  147. if (\Yii::$app->request->isPost) {
  148. return parent::edit(OrderForm::class, 'PayStack pay Success', 'verifyPayStack', ['verifyPayStack']);
  149. }
  150. return static::notice('非法请求', 400);
  151. }
  152. /**
  153. * 我的报单
  154. * @return mixed
  155. * @throws \yii\web\HttpException
  156. */
  157. public function actionDecOrderList() {
  158. $condition = ' AND USER_ID=:USER_ID AND IS_DEL=0';
  159. $params[':USER_ID'] = \Yii::$app->user->id;
  160. $data = DecOrder::lists($condition, $params, [
  161. 'select' => 'DO.*,U.USER_NAME USER_NAME,U.REAL_NAME REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,CU.USER_NAME CON_USER_NAME,CU.REAL_NAME CON_REAL_NAME,OG.*',
  162. 'orderBy' => 'DO.CREATED_AT DESC',
  163. 'from' => DecOrder::tableName() . ' AS DO',
  164. 'join' => [
  165. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  166. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  167. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  168. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  169. ],
  170. ]);
  171. return static::notice($data);
  172. }
  173. /**
  174. * 我的订单
  175. * @return mixed
  176. * @throws \yii\web\HttpException
  177. */
  178. public function actionOrderList() {
  179. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  180. $condition = " AND ORDER_TYPE='FX' AND (USER_ID=:USER_ID OR CREATE_USER='$uname')";
  181. $params[':USER_ID'] = \Yii::$app->user->id;
  182. $data = Order::lists($condition, $params, [
  183. 'select' => 'O.*,U.REAL_NAME,OG.*,SG.CATEGORY_TYPE',
  184. 'orderBy' => 'O.CREATED_AT DESC',
  185. 'from' => Order::tableName() . ' AS O',
  186. 'join' => [
  187. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  188. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  189. ['LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID'],
  190. ],
  191. ]);
  192. foreach ($data['list'] as $key => $value) {
  193. $data['list'][$key]['PAY_AT'] = $value['PAY_AT'] ? Date::convert($value['PAY_AT'],'Y-m-d H:i:s') : '';
  194. $data['list'][$key]['CATEGORY_TYPE'] = intval($value['CATEGORY_TYPE']);
  195. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  196. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  197. $data['list'][$key]['CATEGORY'] = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id')[$value['CATEGORY_TYPE']]['name'] ?? '普通商品';
  198. $data['list'][$key]['FULL_ADDRESS'] = '';
  199. if($value['PROVINCE']){
  200. $data['list'][$key]['FULL_ADDRESS'] .= Region::getCnName($value['PROVINCE']);
  201. }
  202. if($value['CITY']){
  203. $data['list'][$key]['FULL_ADDRESS'] .= Region::getCnName($value['CITY']);
  204. }
  205. if($value['COUNTY']){
  206. $data['list'][$key]['FULL_ADDRESS'] .= Region::getCnName($value['COUNTY']);
  207. }
  208. $data['list'][$key]['FULL_ADDRESS'] .= $value['ADDRESS'];
  209. }
  210. return static::notice($data);
  211. }
  212. /**
  213. * 会员复消
  214. */
  215. public function actionReconsume() {
  216. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0 OR FIND_IN_SET(4,GIFT_TYPE)>0)';
  217. $data = ShopGoods::lists($condition, [], [
  218. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  219. 'from' => ShopGoods::tableName(),
  220. ]);
  221. foreach ($data['list'] as $key => $value) {
  222. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  223. $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  224. } else {
  225. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  226. }
  227. //$data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  228. }
  229. return static::notice($data);
  230. }
  231. /**
  232. * 帮会员复消购物车
  233. * @throws \yii\web\HttpException
  234. */
  235. public function actionReconsumeCart(){
  236. $userId = \Yii::$app->user->id;
  237. $payList = ['cash'=>['name'=>'余额支付'],];
  238. $userBalance = [
  239. 'points' => 0,
  240. 'cash' => 0
  241. ];
  242. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  243. $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
  244. }
  245. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  246. $userBalance['cash'] = $userCashResult['CASH'];
  247. }
  248. return static::notice(['payList'=>$payList,'userBalance'=>$userBalance]);
  249. }
  250. /**
  251. * 帮会员复消确认订单
  252. */
  253. public function actionReconsumeSureOrder(){
  254. if (\Yii::$app->request->isPost) {
  255. $formModel = new OrderForm();
  256. $formModel->scenario = 'reconsumeOrder';
  257. $formModel->remark = '帮会员复销';
  258. $post = \Yii::$app->request->post();
  259. $post['type'] = DeclarationForm::TYPE_FX;
  260. if ($formModel->load($post, '') && $formModel->reconsumeAdd()) {
  261. return static::notice('帮会员复消成功');
  262. } else {
  263. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  264. }
  265. }
  266. }
  267. /**
  268. * 商品列表tabs分类
  269. * @return mixed
  270. * @throws \yii\web\HttpException
  271. */
  272. public function actionGoodsActive()
  273. {
  274. $data = [
  275. // [
  276. // 'name' => 'bonus',
  277. // 'label' => '奖金商品',
  278. // ],
  279. [
  280. 'name' => '1',
  281. 'label' => '普通商品列表',
  282. ],
  283. [
  284. 'name' => '4',
  285. 'label' => '旅游积分商品',
  286. ],
  287. [
  288. 'name' => '5',
  289. 'label' => '名车积分商品',
  290. ],
  291. [
  292. 'name' => '6',
  293. 'label' => '豪宅积分商品',
  294. ],
  295. ];
  296. return static::notice($data);
  297. }
  298. /**
  299. * 导出订单.
  300. * @return mixed
  301. * @throws \yii\web\HttpException
  302. * @throws \Mpdf\MpdfException
  303. */
  304. public function actionOrderExport()
  305. {
  306. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  307. $orderSn = \Yii::$app->request->get('orderSn');
  308. $condition = " AND ORDER_TYPE='FX' AND (USER_ID=:USER_ID OR CREATE_USER='$uname') AND SN=:SN";
  309. $params = [
  310. ':USER_ID' => \Yii::$app->user->id,
  311. ':SN' => $orderSn,
  312. ];
  313. $data = Order::lists($condition, $params, [
  314. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  315. 'orderBy' => 'O.CREATED_AT DESC',
  316. 'from' => Order::tableName() . ' AS O',
  317. 'join' => [
  318. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  319. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  320. ],
  321. ]);
  322. $userId = '';
  323. $userName = '';
  324. $address = '';
  325. $mobile = '';
  326. $orderAt = '';
  327. $orderDetails = '';
  328. $orderAmount = 0; // 合计总额
  329. $orderNums = 0; // 合计总数
  330. foreach ($data['list'] as $key => $value) {
  331. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  332. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  333. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  334. $userId = $value['USER_NAME'];
  335. $userName = $value['REAL_NAME'];
  336. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  337. $mobile = $value['MOBILE'];
  338. $orderAt = Date::convert($value['PAY_AT'],'Y-m-d H:i:s');
  339. // 总价
  340. $totalAmount = $value['BUY_NUMS'] * $value['ORDER_AMOUNT'];
  341. $orderAmount += $totalAmount;
  342. $orderNums += $value['BUY_NUMS'];
  343. // 订单详情
  344. $orderDetails .= <<<EOT
  345. <tr>
  346. <td>{$value['SKU_CODE']}</td>
  347. <td>{$value['GOODS_TITLE']}</td>
  348. <td>{$value['BUY_NUMS']}</td>
  349. <td>{$value['ORDER_AMOUNT']}</td>
  350. <td>{$totalAmount}</td>
  351. </tr>
  352. EOT;
  353. }
  354. // 订单基本信息
  355. $orderBase = <<<ORDER
  356. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  357. <tr>
  358. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员编号</td>
  359. <td width="70%">{$userId}</td>
  360. </tr>
  361. <tr>
  362. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员名字</td>
  363. <td width="70%">{$userName}</td>
  364. </tr>
  365. <tr>
  366. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员地址</td>
  367. <td width="70%">{$address}</td>
  368. </tr>
  369. <tr>
  370. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员电话</td>
  371. <td width="70%">{$mobile}</td>
  372. </tr>
  373. <tr>
  374. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单编号</td>
  375. <td width="70%">{$orderSn}</td>
  376. </tr>
  377. <tr>
  378. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单日期</td>
  379. <td width="70%">{$orderAt}</td>
  380. </tr>
  381. <tr>
  382. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">订单明细</td>
  383. <td class="bg"></td>
  384. </tr>
  385. </table>
  386. ORDER;
  387. $l['a_meta_charset'] = 'UTF-8';
  388. $l['a_meta_dir'] = 'ltr';
  389. $l['a_meta_language'] = 'zh';
  390. $l['w_page'] = '页面';
  391. $context = <<<ORDER
  392. <!doctype html>
  393. <html lang="en">
  394. <head>
  395. <meta charset="UTF-8" />
  396. <title>订单详情</title>
  397. <style>
  398. table {
  399. border-collapse: collapse;
  400. }
  401. table td, table th {
  402. border: 1px solid #ccc;
  403. padding: 10px 30px;
  404. border-collapse: collapse;
  405. }
  406. td {
  407. padding: 120px;
  408. }
  409. .bg {
  410. background-color: #ccc;
  411. }
  412. </style>
  413. </head>
  414. <body>
  415. <div class="content">
  416. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>订单详情</b><br></p>
  417. <div>
  418. <div style="display: block; width: 100%;">
  419. {$orderBase}
  420. <table border="1" width="100%" style="padding: 10px 20px; text-align: center;">
  421. <tr>
  422. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">商品编号</th>
  423. <th width="30%" style="font-size: 14px; font-weight: bold; text-align: center;">商品名称</th>
  424. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">数量</th>
  425. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">单价</th>
  426. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">总价</th>
  427. </tr>
  428. {$orderDetails}
  429. <tr>
  430. <td colspan="2">合计</td>
  431. <td>{$orderNums}</td>
  432. <td></td>
  433. <td>{$orderAmount}</td>
  434. </tr>
  435. </table>
  436. </div>
  437. <div style="width: 100%; margin-top: 50px; height: 30px;">
  438. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  439. <tr style="border: none;">
  440. <td width="70%" style="border: none;"></td>
  441. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">签名:</td>
  442. </tr>
  443. <tr style="border: none;">
  444. <td width="70%" style="border: none;"></td>
  445. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">日期:</td>
  446. </tr>
  447. </table>
  448. </div>
  449. </div>
  450. </div>
  451. </body>
  452. </html>
  453. ORDER;
  454. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  455. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  456. // 设置打印模式
  457. $pdf->SetCreator(PDF_CREATOR);
  458. $pdf->SetAuthor('DaZe');
  459. $pdf->SetTitle($orderSn);
  460. $pdf->SetSubject('TCPDF Tutorial');
  461. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  462. // 是否显示页眉
  463. $pdf->setPrintHeader(false);
  464. // 设置页眉字体
  465. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  466. // 页眉距离顶部的距离
  467. $pdf->SetHeaderMargin('5');
  468. // 是否显示页脚
  469. $pdf->setPrintFooter(false);
  470. // 设置默认等宽字体
  471. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  472. // 设置行高
  473. $pdf->setCellHeightRatio(1);
  474. // 设置左、上、右的间距
  475. $pdf->SetMargins('10', '10', '10');
  476. // 设置是否自动分页 距离底部多少距离时分页
  477. $pdf->SetAutoPageBreak(TRUE, '15');
  478. // 设置图像比例因子
  479. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  480. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  481. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  482. $pdf->setLanguageArray($l);
  483. }
  484. $pdf->setFontSubsetting(true);
  485. $pdf->AddPage();
  486. // 设置字体
  487. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  488. $pdf->writeHTML($context);
  489. ob_clean();
  490. $file_name = $orderSn . '.pdf';
  491. $path = '/pdfs/' . $file_name;
  492. $pdf->Output(Yii::$app->basePath . '/web' . $path, 'F');
  493. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  494. }
  495. /**
  496. * 导出订单.
  497. * @return mixed
  498. * @throws \yii\web\HttpException
  499. * @throws \Mpdf\MpdfException
  500. */
  501. public function actionDecOrderExport()
  502. {
  503. $orderSn = \Yii::$app->request->get('orderSn');
  504. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  505. $params = [
  506. ':USER_ID' => \Yii::$app->user->id,
  507. ':ORDER_SN' => $orderSn,
  508. ];
  509. $data = DecOrder::lists($condition, $params, [
  510. 'select' => 'DO.*,U.USER_NAME USER_NAME,U.REAL_NAME REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,CU.USER_NAME CON_USER_NAME,CU.REAL_NAME CON_REAL_NAME,OG.*,OD.PROVINCE,OD.CITY,OD.COUNTY,OD.ADDRESS,OD.MOBILE,OD.PAY_AT,OD.ORDER_AMOUNT',
  511. 'orderBy' => 'DO.CREATED_AT DESC',
  512. 'from' => DecOrder::tableName() . ' AS DO',
  513. 'join' => [
  514. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  515. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  516. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  517. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  518. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  519. ],
  520. ]);
  521. $userId = '';
  522. $userName = '';
  523. $address = '';
  524. $mobile = '';
  525. $orderAt = '';
  526. $orderDetails = '';
  527. $orderAmount = 0; // 合计总额
  528. $orderNums = 0; // 合计总数
  529. foreach ($data['list'] as $key => $value) {
  530. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  531. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  532. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  533. $userId = $value['USER_NAME'];
  534. $userName = $value['REAL_NAME'];
  535. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  536. $mobile = $value['MOBILE'];
  537. $orderAt = Date::convert($value['PAY_AT'],'Y-m-d H:i:s');
  538. // 总价
  539. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  540. $orderAmount += $totalAmount;
  541. $orderNums += $value['BUY_NUMS'];
  542. // 订单详情
  543. $orderDetails .= <<<EOT
  544. <tr>
  545. <td>{$value['SKU_CODE']}</td>
  546. <td>{$value['GOODS_TITLE']}</td>
  547. <td>{$value['BUY_NUMS']}</td>
  548. <td>{$value['REAL_PRICE']}</td>
  549. <td>{$totalAmount}</td>
  550. </tr>
  551. EOT;
  552. }
  553. // 订单基本信息
  554. $orderBase = <<<ORDER
  555. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  556. <tr>
  557. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员编号</td>
  558. <td width="70%">{$userId}</td>
  559. </tr>
  560. <tr>
  561. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员名字</td>
  562. <td width="70%">{$userName}</td>
  563. </tr>
  564. <tr>
  565. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员地址</td>
  566. <td width="70%">{$address}</td>
  567. </tr>
  568. <tr>
  569. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员电话</td>
  570. <td width="70%">{$mobile}</td>
  571. </tr>
  572. <tr>
  573. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单编号</td>
  574. <td width="70%">{$orderSn}</td>
  575. </tr>
  576. <tr>
  577. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单日期</td>
  578. <td width="70%">{$orderAt}</td>
  579. </tr>
  580. <tr>
  581. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">订单明细</td>
  582. <td class="bg"></td>
  583. </tr>
  584. </table>
  585. ORDER;
  586. $l['a_meta_charset'] = 'UTF-8';
  587. $l['a_meta_dir'] = 'ltr';
  588. $l['a_meta_language'] = 'zh';
  589. $l['w_page'] = '页面';
  590. $context = <<<ORDER
  591. <!doctype html>
  592. <html lang="en">
  593. <head>
  594. <meta charset="UTF-8" />
  595. <title>订单详情</title>
  596. <style>
  597. table {
  598. border-collapse: collapse;
  599. }
  600. table td, table th {
  601. border: 1px solid #ccc;
  602. padding: 10px 30px;
  603. border-collapse: collapse;
  604. }
  605. td {
  606. padding: 120px;
  607. }
  608. .bg {
  609. background-color: #ccc;
  610. }
  611. </style>
  612. </head>
  613. <body>
  614. <div class="content">
  615. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>订单详情</b><br></p>
  616. <div>
  617. <div style="display: block; width: 100%;">
  618. {$orderBase}
  619. <table border="1" width="100%" style="padding: 10px 20px; text-align: center;">
  620. <tr>
  621. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">商品编号</th>
  622. <th width="30%" style="font-size: 14px; font-weight: bold; text-align: center;">商品名称</th>
  623. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">数量</th>
  624. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">单价</th>
  625. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">总价</th>
  626. </tr>
  627. {$orderDetails}
  628. <tr>
  629. <td colspan="2">合计</td>
  630. <td>{$orderNums}</td>
  631. <td></td>
  632. <td>{$orderAmount}</td>
  633. </tr>
  634. </table>
  635. </div>
  636. <div style="width: 100%; margin-top: 50px; height: 30px;">
  637. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  638. <tr style="border: none;">
  639. <td width="70%" style="border: none;"></td>
  640. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">签名:</td>
  641. </tr>
  642. <tr style="border: none;">
  643. <td width="70%" style="border: none;"></td>
  644. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">日期:</td>
  645. </tr>
  646. </table>
  647. </div>
  648. </div>
  649. </div>
  650. </body>
  651. </html>
  652. ORDER;
  653. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  654. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  655. // 设置打印模式
  656. $pdf->SetCreator(PDF_CREATOR);
  657. $pdf->SetAuthor('DaZe');
  658. $pdf->SetTitle($orderSn);
  659. $pdf->SetSubject('TCPDF Tutorial');
  660. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  661. // 是否显示页眉
  662. $pdf->setPrintHeader(false);
  663. // 设置页眉字体
  664. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  665. // 页眉距离顶部的距离
  666. $pdf->SetHeaderMargin('5');
  667. // 是否显示页脚
  668. $pdf->setPrintFooter(false);
  669. // 设置默认等宽字体
  670. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  671. // 设置行高
  672. $pdf->setCellHeightRatio(1);
  673. // 设置左、上、右的间距
  674. $pdf->SetMargins('10', '10', '10');
  675. // 设置是否自动分页 距离底部多少距离时分页
  676. $pdf->SetAutoPageBreak(TRUE, '15');
  677. // 设置图像比例因子
  678. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  679. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  680. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  681. $pdf->setLanguageArray($l);
  682. }
  683. $pdf->setFontSubsetting(true);
  684. $pdf->AddPage();
  685. // 设置字体
  686. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  687. $pdf->writeHTML($context);
  688. ob_clean();
  689. $file_name = $orderSn . '.pdf';
  690. $path = '/pdfs/' . $file_name;
  691. $pdf->Output(Yii::$app->basePath . '/web' . $path, 'F');
  692. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  693. }
  694. }