ShopController.php 32 KB

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