ShopController.php 31 KB

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