ShopController.php 31 KB

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