ShopController.php 31 KB

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