ShopController.php 33 KB

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