ShopController.php 31 KB

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