ShopController.php 32 KB

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