ShopController.php 33 KB

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