ShopController.php 29 KB

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