ShopController.php 33 KB

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