ShopController.php 40 KB

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