ShopController.php 29 KB

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