ShopController.php 32 KB

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