ShopController.php 40 KB

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