ShopController.php 33 KB

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