ShopController.php 36 KB

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