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