ShopController.php 32 KB

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