ShopController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use Codeception\PHPUnit\ResultPrinter\HTML;
  10. use common\helpers\Date;
  11. use common\helpers\Form;
  12. use common\helpers\user\Info;
  13. use common\models\ApproachOrder;
  14. use common\models\ApproachOrderGoods;
  15. use common\models\DealType;
  16. use common\models\DecOrder;
  17. use common\models\forms\ApproachOrderForm;
  18. use common\models\forms\DeclarationForm;
  19. use common\models\forms\OrderForm;
  20. use common\models\Order;
  21. use common\models\OrderDec;
  22. use common\models\OrderGoods;
  23. use common\models\OrderGoodsDec;
  24. use common\models\ReceiveAddress;
  25. use common\models\Region;
  26. use common\models\ShopGoods;
  27. use common\models\User;
  28. use common\models\UserBonus;
  29. use common\models\UserWallet;
  30. use Yii;
  31. class ShopController extends BaseController {
  32. public $modelClass = DecOrder::class;
  33. /**
  34. * 商品列表
  35. * @return mixed
  36. * @throws \yii\web\HttpException
  37. */
  38. public function actionIndex() {
  39. // 商品分类
  40. $categoryType = \Yii::$app->request->get('categoryType', 1);
  41. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  42. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  43. if($isStudio==1){
  44. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  45. }
  46. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  47. $params[':CATEGORY_TYPE'] = intval($categoryType);
  48. $data = ShopGoods::lists($condition, $params, [
  49. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  50. 'from' => ShopGoods::tableName(),
  51. ]);
  52. foreach ($data['list'] as $key => $value) {
  53. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  54. $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  55. } else {
  56. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  57. }
  58. // $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  59. }
  60. return static::notice($data);
  61. }
  62. /**
  63. * 获取商品详情
  64. * @return mixed
  65. * @throws \yii\web\HttpException
  66. */
  67. public function actionGoodsDetail(){
  68. $id = \Yii::$app->request->get('id');
  69. $data = null;
  70. if($id){
  71. $data = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id]);
  72. }
  73. return static::notice($data);
  74. }
  75. /**
  76. * 购物车订单展示
  77. * @throws \yii\web\HttpException
  78. */
  79. public function actionShowCart(){
  80. $userId = \Yii::$app->user->id;
  81. $payList = ShopGoods::payTypes();
  82. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  83. if($allAddress) {
  84. foreach ($allAddress as $key => $row) {
  85. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  86. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  87. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  88. }
  89. }
  90. $userBalance = [
  91. // 'points' => 0,
  92. 'cash' => 0,
  93. // 'exchange' => 0,
  94. 'tourism_points' => 0,
  95. 'garage_points' => 0,
  96. ];
  97. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  98. // $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
  99. // $userBalance['exchange'] = $userBonusResult['EXCHANGE_POINTS'];
  100. $userBalance['tourism_points'] = $userBonusResult['TOURISM_POINTS'];
  101. $userBalance['garage_points'] = $userBonusResult['GARAGE_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 ORDER_TYPE='FX' 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 House Bonus Products',//名车积分商品
  323. ],
  324. ];
  325. return static::notice($data);
  326. }
  327. /**
  328. * 导出订单.
  329. * @return mixed
  330. * @throws \yii\web\HttpException
  331. * @throws \Mpdf\MpdfException
  332. */
  333. public function actionOrderExport()
  334. {
  335. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  336. $orderSn = \Yii::$app->request->get('orderSn');
  337. $condition = " AND ORDER_TYPE='FX' AND (USER_ID=:USER_ID OR CREATE_USER='$uname') AND SN=:SN";
  338. $params = [
  339. ':USER_ID' => \Yii::$app->user->id,
  340. ':SN' => $orderSn,
  341. ];
  342. $data = Order::lists($condition, $params, [
  343. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  344. 'orderBy' => 'O.CREATED_AT DESC',
  345. 'from' => Order::tableName() . ' AS O',
  346. 'join' => [
  347. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  348. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  349. ],
  350. ]);
  351. $userId = '';
  352. $userName = '';
  353. $address = '';
  354. $mobile = '';
  355. $orderAt = '';
  356. $orderDetails = '';
  357. $orderAmount = 0; // 合计总额
  358. $orderNums = 0; // 合计总数
  359. foreach ($data['list'] as $key => $value) {
  360. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  361. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  362. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  363. $userId = $value['USER_NAME'];
  364. $userName = $value['REAL_NAME'];
  365. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  366. $mobile = $value['MOBILE'];
  367. $orderAt = Date::convert($value['PAY_AT'],'Y-m-d H:i:s');
  368. // 总价
  369. $totalAmount = $value['BUY_NUMS'] * $value['ORDER_AMOUNT'];
  370. $orderAmount += $totalAmount;
  371. $orderNums += $value['BUY_NUMS'];
  372. // 订单详情
  373. $orderDetails .= <<<EOT
  374. <tr>
  375. <td>{$value['SKU_CODE']}</td>
  376. <td>{$value['GOODS_TITLE']}</td>
  377. <td>{$value['BUY_NUMS']}</td>
  378. <td>{$value['ORDER_AMOUNT']}</td>
  379. <td>{$totalAmount}</td>
  380. </tr>
  381. EOT;
  382. }
  383. // 订单基本信息
  384. $orderBase = <<<ORDER
  385. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  386. <tr>
  387. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员编号</td>
  388. <td width="70%">{$userId}</td>
  389. </tr>
  390. <tr>
  391. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员名字</td>
  392. <td width="70%">{$userName}</td>
  393. </tr>
  394. <tr>
  395. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员地址</td>
  396. <td width="70%">{$address}</td>
  397. </tr>
  398. <tr>
  399. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员电话</td>
  400. <td width="70%">{$mobile}</td>
  401. </tr>
  402. <tr>
  403. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单编号</td>
  404. <td width="70%">{$orderSn}</td>
  405. </tr>
  406. <tr>
  407. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单日期</td>
  408. <td width="70%">{$orderAt}</td>
  409. </tr>
  410. <tr>
  411. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">订单明细</td>
  412. <td class="bg"></td>
  413. </tr>
  414. </table>
  415. ORDER;
  416. $l['a_meta_charset'] = 'UTF-8';
  417. $l['a_meta_dir'] = 'ltr';
  418. $l['a_meta_language'] = 'zh';
  419. $l['w_page'] = '页面';
  420. $context = <<<ORDER
  421. <!doctype html>
  422. <html lang="en">
  423. <head>
  424. <meta charset="UTF-8" />
  425. <title>订单详情</title>
  426. <style>
  427. table {
  428. border-collapse: collapse;
  429. }
  430. table td, table th {
  431. border: 1px solid #ccc;
  432. padding: 10px 30px;
  433. border-collapse: collapse;
  434. }
  435. td {
  436. padding: 120px;
  437. }
  438. .bg {
  439. background-color: #ccc;
  440. }
  441. </style>
  442. </head>
  443. <body>
  444. <div class="content">
  445. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>订单详情</b><br></p>
  446. <div>
  447. <div style="display: block; width: 100%;">
  448. {$orderBase}
  449. <table border="1" width="100%" style="padding: 10px 20px; text-align: center;">
  450. <tr>
  451. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">商品编号</th>
  452. <th width="30%" style="font-size: 14px; font-weight: bold; text-align: center;">商品名称</th>
  453. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">数量</th>
  454. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">单价</th>
  455. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">总价</th>
  456. </tr>
  457. {$orderDetails}
  458. <tr>
  459. <td colspan="2">合计</td>
  460. <td>{$orderNums}</td>
  461. <td></td>
  462. <td>{$orderAmount}</td>
  463. </tr>
  464. </table>
  465. </div>
  466. <div style="width: 100%; margin-top: 50px; height: 30px;">
  467. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  468. <tr style="border: none;">
  469. <td width="70%" style="border: none;"></td>
  470. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">签名:</td>
  471. </tr>
  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. </table>
  477. </div>
  478. </div>
  479. </div>
  480. </body>
  481. </html>
  482. ORDER;
  483. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  484. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  485. // 设置打印模式
  486. $pdf->SetCreator(PDF_CREATOR);
  487. $pdf->SetAuthor('DaZe');
  488. $pdf->SetTitle($orderSn);
  489. $pdf->SetSubject('TCPDF Tutorial');
  490. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  491. // 是否显示页眉
  492. $pdf->setPrintHeader(false);
  493. // 设置页眉字体
  494. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  495. // 页眉距离顶部的距离
  496. $pdf->SetHeaderMargin('5');
  497. // 是否显示页脚
  498. $pdf->setPrintFooter(false);
  499. // 设置默认等宽字体
  500. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  501. // 设置行高
  502. $pdf->setCellHeightRatio(1);
  503. // 设置左、上、右的间距
  504. $pdf->SetMargins('10', '10', '10');
  505. // 设置是否自动分页 距离底部多少距离时分页
  506. $pdf->SetAutoPageBreak(TRUE, '15');
  507. // 设置图像比例因子
  508. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  509. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  510. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  511. $pdf->setLanguageArray($l);
  512. }
  513. $pdf->setFontSubsetting(true);
  514. $pdf->AddPage();
  515. // 设置字体
  516. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  517. $pdf->writeHTML($context);
  518. ob_clean();
  519. $file_name = $orderSn . '.pdf';
  520. $path = 'pdfs/' . $file_name;
  521. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  522. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  523. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  524. }
  525. /**
  526. * 导出订单.
  527. * @return mixed
  528. * @throws \yii\web\HttpException
  529. * @throws \Mpdf\MpdfException
  530. */
  531. public function actionDecOrderExport()
  532. {
  533. $orderSn = \Yii::$app->request->get('orderSn');
  534. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  535. $params = [
  536. ':USER_ID' => \Yii::$app->user->id,
  537. ':ORDER_SN' => $orderSn,
  538. ];
  539. $data = DecOrder::lists($condition, $params, [
  540. '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',
  541. 'orderBy' => 'DO.CREATED_AT DESC',
  542. 'from' => DecOrder::tableName() . ' AS DO',
  543. 'join' => [
  544. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  545. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  546. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  547. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  548. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  549. ],
  550. ]);
  551. $userId = '';
  552. $userName = '';
  553. $address = '';
  554. $mobile = '';
  555. $orderAt = '';
  556. $orderDetails = '';
  557. $orderAmount = 0; // 合计总额
  558. $orderNums = 0; // 合计总数
  559. foreach ($data['list'] as $key => $value) {
  560. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  561. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  562. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  563. $userId = $value['USER_NAME'];
  564. $userName = $value['REAL_NAME'];
  565. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  566. $mobile = $value['MOBILE'];
  567. $orderAt = Date::convert($value['PAY_AT'],'Y-m-d H:i:s');
  568. // 总价
  569. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  570. $orderAmount += $totalAmount;
  571. $orderNums += $value['BUY_NUMS'];
  572. // 订单详情
  573. $orderDetails .= <<<EOT
  574. <tr>
  575. <td>{$value['SKU_CODE']}</td>
  576. <td>{$value['GOODS_TITLE']}</td>
  577. <td>{$value['BUY_NUMS']}</td>
  578. <td>{$value['REAL_PRICE']}</td>
  579. <td>{$totalAmount}</td>
  580. </tr>
  581. EOT;
  582. }
  583. // 订单基本信息
  584. $orderBase = <<<ORDER
  585. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  586. <tr>
  587. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员编号</td>
  588. <td width="70%">{$userId}</td>
  589. </tr>
  590. <tr>
  591. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员名字</td>
  592. <td width="70%">{$userName}</td>
  593. </tr>
  594. <tr>
  595. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员地址</td>
  596. <td width="70%">{$address}</td>
  597. </tr>
  598. <tr>
  599. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">会员电话</td>
  600. <td width="70%">{$mobile}</td>
  601. </tr>
  602. <tr>
  603. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单编号</td>
  604. <td width="70%">{$orderSn}</td>
  605. </tr>
  606. <tr>
  607. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">订单日期</td>
  608. <td width="70%">{$orderAt}</td>
  609. </tr>
  610. <tr>
  611. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">订单明细</td>
  612. <td class="bg"></td>
  613. </tr>
  614. </table>
  615. ORDER;
  616. $l['a_meta_charset'] = 'UTF-8';
  617. $l['a_meta_dir'] = 'ltr';
  618. $l['a_meta_language'] = 'zh';
  619. $l['w_page'] = '页面';
  620. $context = <<<ORDER
  621. <!doctype html>
  622. <html lang="en">
  623. <head>
  624. <meta charset="UTF-8" />
  625. <title>订单详情</title>
  626. <style>
  627. table {
  628. border-collapse: collapse;
  629. }
  630. table td, table th {
  631. border: 1px solid #ccc;
  632. padding: 10px 30px;
  633. border-collapse: collapse;
  634. }
  635. td {
  636. padding: 120px;
  637. }
  638. .bg {
  639. background-color: #ccc;
  640. }
  641. </style>
  642. </head>
  643. <body>
  644. <div class="content">
  645. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>订单详情</b><br></p>
  646. <div>
  647. <div style="display: block; width: 100%;">
  648. {$orderBase}
  649. <table border="1" width="100%" style="padding: 10px 20px; text-align: center;">
  650. <tr>
  651. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">商品编号</th>
  652. <th width="30%" style="font-size: 14px; font-weight: bold; text-align: center;">商品名称</th>
  653. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">数量</th>
  654. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">单价</th>
  655. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">总价</th>
  656. </tr>
  657. {$orderDetails}
  658. <tr>
  659. <td colspan="2">合计</td>
  660. <td>{$orderNums}</td>
  661. <td></td>
  662. <td>{$orderAmount}</td>
  663. </tr>
  664. </table>
  665. </div>
  666. <div style="width: 100%; margin-top: 50px; height: 30px;">
  667. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  668. <tr style="border: none;">
  669. <td width="70%" style="border: none;"></td>
  670. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">签名:</td>
  671. </tr>
  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. </table>
  677. </div>
  678. </div>
  679. </div>
  680. </body>
  681. </html>
  682. ORDER;
  683. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  684. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  685. // 设置打印模式
  686. $pdf->SetCreator(PDF_CREATOR);
  687. $pdf->SetAuthor('DaZe');
  688. $pdf->SetTitle($orderSn);
  689. $pdf->SetSubject('TCPDF Tutorial');
  690. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  691. // 是否显示页眉
  692. $pdf->setPrintHeader(false);
  693. // 设置页眉字体
  694. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  695. // 页眉距离顶部的距离
  696. $pdf->SetHeaderMargin('5');
  697. // 是否显示页脚
  698. $pdf->setPrintFooter(false);
  699. // 设置默认等宽字体
  700. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  701. // 设置行高
  702. $pdf->setCellHeightRatio(1);
  703. // 设置左、上、右的间距
  704. $pdf->SetMargins('10', '10', '10');
  705. // 设置是否自动分页 距离底部多少距离时分页
  706. $pdf->SetAutoPageBreak(TRUE, '15');
  707. // 设置图像比例因子
  708. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  709. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  710. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  711. $pdf->setLanguageArray($l);
  712. }
  713. $pdf->setFontSubsetting(true);
  714. $pdf->AddPage();
  715. // 设置字体
  716. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  717. $pdf->writeHTML($context);
  718. ob_clean();
  719. $file_name = $orderSn . '.pdf';
  720. $path = 'pdfs/' . $file_name;
  721. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  722. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  723. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  724. }
  725. }