ShopController.php 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  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 common\helpers\Cache;
  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\ApproachDecOrder;
  16. use common\models\BaApproachDecOrder;
  17. use common\models\ApproachOrder;
  18. use common\models\ApproachOrderGoods;
  19. use common\models\BaApproachOrder;
  20. use common\models\BaApproachOrderGoods;
  21. use common\models\BaOrder;
  22. use common\models\BaOrderGoods;
  23. use common\models\BaReceiveAddress;
  24. use common\models\BaUser;
  25. use common\models\Countries;
  26. use common\models\Currency;
  27. use common\models\CurrencyConversions;
  28. use common\models\DecOrder;
  29. use common\models\BaDecOrder;
  30. use common\models\forms\ApproachAutoMaintenanceOrderForm;
  31. use common\models\forms\ApproachOrderForm;
  32. use common\models\forms\ApproachReconsumeOrderForm;
  33. use common\models\forms\BaApproachOrderForm;
  34. use common\models\forms\DeclarationForm;
  35. use common\models\forms\OrderForm;
  36. use common\models\FreeTemplate;
  37. use common\models\Instalment;
  38. use common\models\Order;
  39. use common\models\OrderGoods;
  40. use common\models\ReceiveAddress;
  41. use common\models\Region;
  42. use common\models\ShopGoods;
  43. use common\models\ShopGoodsNature;
  44. use common\models\User;
  45. use common\models\UserBonus;
  46. use common\models\UserPerformance;
  47. use common\models\UserWallet;
  48. use Yii;
  49. use yii\base\Exception;
  50. use yii\data\Pagination;
  51. use yii\db\Query;
  52. use yii\web\HttpException;
  53. class ShopController extends BaseController {
  54. public $modelClass = DecOrder::class;
  55. /**
  56. * 商品列表
  57. * @return mixed
  58. * @throws HttpException
  59. */
  60. public function actionIndex() {
  61. // 商品分类
  62. $categoryType = \Yii::$app->request->get('categoryType', 1);
  63. // 会员信息
  64. $user = User::getEnCodeInfo(\Yii::$app->user->id);
  65. $condition = ' AND S.STATUS=1 AND (FIND_IN_SET(2, S.GIFT_TYPE)>0';
  66. if ($user['IS_STUDIO'] == 1) {
  67. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  68. }
  69. $condition .= ") AND S.CATEGORY_TYPE = :CATEGORY_TYPE";
  70. $params[':CATEGORY_TYPE'] = intval($categoryType);
  71. $params[':COUNTRY_ID'] = $user['COUNTRY_ID'];
  72. $userId = \Yii::$app->user->id;
  73. // 分期的总期数
  74. $instalmentConfig = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  75. // 查询是否有BD的分期订单
  76. $stageBD = Instalment::getOneStage($userId, 'BD');
  77. if ($stageBD && $stageBD < $instalmentConfig) {
  78. $condition .= " AND INSTALMENT = 0";
  79. } else {
  80. // 查询是否有FX的分期订单
  81. $stageFX = Instalment::getOneStage($userId, 'FX');
  82. if ($stageFX && $stageFX < $instalmentConfig) {
  83. $condition .= " AND (INSTALMENT = 0 OR INSTALMENT=" . ($stageFX + 1) . ")";
  84. } else {
  85. $condition .= " AND (INSTALMENT = 0 OR INSTALMENT=1)";
  86. }
  87. }
  88. $condition .= ' AND N.COUNTRY_ID=:COUNTRY_ID ';
  89. $data = ShopGoods::lists($condition, $params, [
  90. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  91. 'from' => ShopGoods::tableName() . ' AS S',
  92. 'join' => [
  93. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  94. ],
  95. 'orderBy' => 'S.SORT ASC, S.CREATED_AT DESC',
  96. ]);
  97. $currencies = Currency::getAllData();
  98. $currencies = array_column($currencies, NULL, 'ID');
  99. foreach ($data['list'] as $key => $value) {
  100. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  101. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  102. }
  103. return static::notice($data);
  104. }
  105. /**
  106. * 获取商品详情
  107. * @return mixed
  108. * @throws HttpException
  109. */
  110. public function actionGoodsDetail(){
  111. $id = \Yii::$app->request->get('id');
  112. $data = null;
  113. if($id){
  114. $data = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id]);
  115. }
  116. return static::notice($data);
  117. }
  118. /**
  119. * 购物车订单展示
  120. * @throws HttpException
  121. */
  122. public function actionShowCart(){
  123. // 会员信息
  124. $user = User::getEnCodeInfo(\Yii::$app->user->id);
  125. $payList = ShopGoods::payTypes();
  126. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID AND COUNTRY_ID=:COUNTRY_ID',
  127. [':USER_ID' => $user['ID'], ':COUNTRY_ID' => $user['COUNTRY_ID']]);
  128. if($allAddress) {
  129. foreach ($allAddress as $key => $row) {
  130. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  131. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  132. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  133. }
  134. }
  135. // 账户币种
  136. $country = Countries::getById($user['COUNTRY_ID']);
  137. $coin = Currency::getById($country['LOCAL_CURRENCY_ID']);
  138. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  139. $decCountry = Countries::getById($decCountryId);
  140. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  141. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  142. $userBalance = [
  143. 'cash' => 0,
  144. 'prp' => 0,
  145. 'localCash' => 0,
  146. 'localRrp' => 0,
  147. ];
  148. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $user['ID']])) {
  149. $userBalance['cash'] = $userCashResult['CASH'];
  150. $userBalance['localCash'] = Tool::convertAmount($userCashResult['CASH'], $decUserCurrencyRate, $currencyRate);
  151. }
  152. if ($userPerformance = UserPerformance::getAmounts($user['ID'])) {
  153. $userBalance['prp'] = $userPerformance;
  154. $userBalance['localCash'] = Tool::convertAmount($userPerformance, $decUserCurrencyRate, $currencyRate);
  155. }
  156. // 运费模板
  157. $freeTemplate = FreeTemplate::getByCountryId($user['COUNTRY_ID']);
  158. // 运费
  159. $freight = $freeTemplate['freight'] ?? 0;
  160. // 免运费阈值
  161. $freeShipping = $freeTemplate['free_shipping'] ?? 0;
  162. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  163. // 账户币种
  164. $country = Countries::getById($user['COUNTRY_ID']);
  165. $coin = Currency::getById($country['LOCAL_CURRENCY_ID']);
  166. // AMP
  167. $sysConfig = Cache::getSystemConfig();
  168. $ampDivideLine = (int)$sysConfig['ampBVCondition']['VALUE'];
  169. return static::notice(
  170. [
  171. 'payList'=>$payList,
  172. 'allAddress'=>$allAddress,
  173. 'userBalance'=>$userBalance,
  174. 'sellType' => ShopGoods::getCategoryType(),
  175. 'freight' => $freight,
  176. 'freeShipping' => $freeShipping,
  177. 'isDec' => $isDec,
  178. 'coin' => $coin['CODE'] ?? '',
  179. 'ampDivideLine' => floatval($ampDivideLine),
  180. ]);
  181. }
  182. /**
  183. * 确认订单
  184. */
  185. public function actionSureOrder(){
  186. if (\Yii::$app->request->isPost) {
  187. $formModel = new OrderForm();
  188. $formModel->scenario = 'userOrder';
  189. $formModel->remark = 'FX';//复销备注
  190. $post = \Yii::$app->request->post();
  191. $post['type'] = DeclarationForm::TYPE_FX;
  192. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  193. $stockist = $post['userName'] ?? '';
  194. if (!$stockist) {
  195. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  196. }
  197. if($loginUserInfo['IS_DEC'] == 1 && $stockist != $loginUserInfo['USER_NAME']){
  198. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  199. }
  200. if($loginUserInfo['IS_DEC'] == 0){
  201. //查询报单编号是否存在
  202. $stockistId = Info::getUserIdByUserName($stockist);
  203. if(!$stockistId){
  204. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  205. }
  206. $isDec = User::getEnCodeInfo($stockistId)['IS_DEC'];
  207. if(!$isDec){
  208. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  209. }
  210. }
  211. $formModel->decUserName = $stockist;
  212. if ($formModel->load($post, '') && $order = $formModel->add()) {
  213. return static::notice($order);
  214. } else {
  215. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  216. }
  217. }
  218. }
  219. /**
  220. * 订单支付成功
  221. * @throws HttpException
  222. */
  223. public function actionPaySuccess(){
  224. $orderSn = \Yii::$app->request->get('orderSn');
  225. $data = null;
  226. if($orderSn){
  227. $data = Order::findOneAsArray('SN=:SN', [':SN'=>$orderSn]);
  228. }
  229. return static::notice($data);
  230. }
  231. /**
  232. * 确认订单
  233. */
  234. public function actionSureApproachOrder(){
  235. if (\Yii::$app->request->isPost) {
  236. $formModel = new ApproachOrderForm();
  237. $formModel->scenario = 'userOrder';
  238. $formModel->remark = '复销备注';
  239. $post = \Yii::$app->request->post();
  240. $post['type'] = DeclarationForm::TYPE_FX;
  241. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  242. $stockist = $post['userName'] ?? '';
  243. if (!$stockist) {
  244. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  245. }
  246. if($loginUserInfo['IS_DEC'] == 1 && !empty($post['userName']) && $post['userName'] != $loginUserInfo['USER_NAME']){
  247. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  248. }
  249. //查询报单编号是否存在
  250. if($loginUserInfo['IS_DEC'] == 0){
  251. $userId = Info::getUserIdByUserName($post['userName']);
  252. if(!$userId){
  253. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  254. }
  255. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  256. if(!$isDec){
  257. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  258. }
  259. }
  260. $formModel->decUserName = $post['userName'];
  261. if ($formModel->load($post, '') && $order = $formModel->add()) {
  262. return static::notice($order);
  263. } else {
  264. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  265. }
  266. }
  267. }
  268. /**
  269. * 确认复消订单
  270. */
  271. public function actionSureApproachReconsumeOrder(){
  272. if (\Yii::$app->request->isPost) {
  273. $formModel = new ApproachReconsumeOrderForm();
  274. $formModel->scenario = 'reconsumeOrder';
  275. $formModel->remark = Yii::t('app', 'reconsume');
  276. $post = \Yii::$app->request->post();
  277. $post['type'] = DeclarationForm::TYPE_FX;
  278. $stockist = $post['userName'] ?? '';
  279. if (!$stockist) {
  280. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  281. }
  282. if ($formModel->load($post, '') && $order = $formModel->add()) {
  283. return static::notice($order);
  284. } else {
  285. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  286. }
  287. }
  288. }
  289. /**
  290. * PayStack支付成功的webhook.
  291. * @throws HttpException
  292. * @throws \Exception
  293. */
  294. public function actionVerifyApproachOrder() {
  295. if (\Yii::$app->request->isPost) {
  296. $request = \Yii::$app->request->post();
  297. LoggerTool::notice($request);
  298. // 支付webhook回调日志
  299. Tool::approachOrderCall($request);
  300. try {
  301. $data = $request['data'];
  302. $status = $request['data']['status'] ?? 'false';
  303. // 订单状态
  304. $orderStatus = ($status == 'success') ? \Yii::$app->params['orderStatus']['paid']['value'] : \Yii::$app->params['orderStatus']['failPaid']['value'];
  305. $customFields = $data['metadata']['custom_fields'] ?? [];
  306. $oderSn = $customFields[0]['value'] ?? '';
  307. $orderType = $customFields[1]['value'] ?? false;
  308. $formModel = new ApproachOrderForm();
  309. $formModel->scenario = 'verifyPayStack';
  310. $load = [
  311. 'sn' => $oderSn,
  312. 'scenario' => 'verifyPayStack',
  313. 'status' => $orderStatus,
  314. 'note' => [
  315. 'id' => $data['id'],
  316. 'domain' => $data['domain'],
  317. 'status' => $data['status'],
  318. 'reference' => $data['reference'],
  319. 'amount' => $data['amount'],
  320. 'created_at' => $data['created_at'],
  321. 'paid_at' => $data['paid_at'],
  322. 'channel' => $data['channel'],
  323. 'currency' => $data['currency'],
  324. 'ip_address' => $data['ip_address'],
  325. 'metadata' => $data['metadata'],
  326. 'customer' => $data['customer'],
  327. 'email' => $data['customer']['email'] ?? '',
  328. ],
  329. ];
  330. if ($formModel->load($load, '') && $order = $formModel->verifyPayStack()) {
  331. LoggerTool::error($order);
  332. return http_response_code(200);
  333. } else {
  334. LoggerTool::error(Form::formatErrorsForApi($formModel->getErrors()));
  335. return http_response_code(500);
  336. }
  337. } catch (\Exception $e) {
  338. LoggerTool::error(sprintf('actionVerifyApproachOrderError: File[%s], Line:[%s], Message[%s]', $e->getFile(), $e->getLine(), $e->getMessage()));
  339. return http_response_code(500);
  340. }
  341. }
  342. return http_response_code(200);
  343. }
  344. /**
  345. * 删除准订单
  346. */
  347. public function actionDeleteApproachOrder()
  348. {
  349. $orderSn = \Yii::$app->request->post('orderSn');
  350. // 删除订单中间表
  351. ApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  352. // 删除订单商品中间表
  353. ApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  354. // 删除报单中间表
  355. ApproachDecOrder::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  356. return static::notice('');
  357. }
  358. /**
  359. * @throws HttpException
  360. */
  361. public function actionDecOrderList() {
  362. $version = \Yii::$app->request->get('v');
  363. $data = $version == 2 ? $this->decOrderListV2() : $this->decOrderListV1();
  364. return static::notice($data);
  365. }
  366. /**
  367. * 我的报单
  368. * @return mixed
  369. * @throws HttpException
  370. */
  371. public function decOrderListV1() {
  372. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  373. $params[':USER_ID'] = \Yii::$app->user->id;
  374. $orderQuery = DecOrder::find()
  375. ->alias('DO')
  376. ->where($condition, $params)
  377. ->select('DO.*,O.STATUS,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.SKU_CODE,OG.GOODS_TITLE,OG.BUY_NUMS,OG.REAL_PRICE,OG.REAL_PV,OG.TAX_RATE')
  378. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  379. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  380. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  381. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  382. ->join('LEFT JOIN', Order::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  383. ->orderBy('DO.CREATED_AT DESC');
  384. // 订单中间表只查询待支付和支付失败的订单
  385. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  386. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  387. $orderStandardQuery = ApproachDecOrder::find()
  388. ->alias('DO')
  389. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  390. ->select('DO.*,O.STATUS,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.SKU_CODE,OG.GOODS_TITLE,OG.BUY_NUMS,OG.REAL_PRICE,OG.REAL_PV,OG.TAX_RATE')
  391. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  392. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  393. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  394. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  395. ->join('LEFT JOIN', ApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  396. ->orderBy('DO.CREATED_AT DESC');
  397. $queryAll = $orderQuery->union($orderStandardQuery, true);
  398. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  399. $totalCount = $query->count();
  400. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  401. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  402. $data = [
  403. 'list' => $lists,
  404. 'currentPage'=>$pagination->page,
  405. 'totalPages'=>$pagination->pageCount,
  406. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  407. 'totalCount' => $pagination->totalCount,
  408. 'pageSize' => $pagination->pageSize,
  409. ];
  410. foreach ($data['list'] as &$value) {
  411. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  412. $value['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  413. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  414. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  415. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  416. }
  417. return $data;
  418. }
  419. /**
  420. * 我的报单
  421. * @return mixed
  422. * @throws HttpException
  423. */
  424. public function decOrderListV2() {
  425. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  426. $params[':USER_ID'] = \Yii::$app->user->id;
  427. $orderQuery = DecOrder::find()
  428. ->alias('DO')
  429. ->where($condition, $params)
  430. ->select('DO.DEC_SN,DO.ORDER_SN,DO.CREATED_AT,O.STATUS,O.CONSIGNEE,O.MOBILE,O.PAY_TYPE,O.PAY_AT,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,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')
  431. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  432. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  433. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  434. ->join('LEFT JOIN', Order::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  435. ->orderBy('DO.CREATED_AT DESC');
  436. // 订单中间表只查询待支付和支付失败的订单
  437. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  438. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  439. $orderStandardQuery = ApproachDecOrder::find()
  440. ->alias('DO')
  441. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  442. ->select('DO.DEC_SN,DO.ORDER_SN,DO.CREATED_AT,O.STATUS,O.CONSIGNEE,O.MOBILE,O.PAY_TYPE,O.PAY_AT,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,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')
  443. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  444. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  445. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  446. ->join('LEFT JOIN', ApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  447. ->orderBy('DO.CREATED_AT DESC');
  448. $queryAll = $orderQuery->union($orderStandardQuery, true);
  449. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  450. $totalCount = $query->count();
  451. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  452. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  453. $data = [
  454. 'list' => $lists,
  455. 'currentPage'=>$pagination->page,
  456. 'totalPages'=>$pagination->pageCount,
  457. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  458. 'totalCount' => $pagination->totalCount,
  459. 'pageSize' => $pagination->pageSize,
  460. ];
  461. foreach ($data['list'] as &$value) {
  462. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  463. // 订单中间表
  464. $orderGoods = ApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  465. } else {
  466. // 订单表
  467. $orderGoods = OrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  468. }
  469. $value['hasChildren'] = $orderGoods;
  470. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  471. $value['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  472. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  473. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  474. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  475. }
  476. return $data;
  477. }
  478. /**
  479. * 我的订单.(自己复消+自己加入时的首单)
  480. * 创建人是自己+USER_ID是自己的复消单、创建人不是自己+USER_ID是自己的报单
  481. * @throws HttpException
  482. */
  483. public function actionOwnOrder() {
  484. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  485. $condition = " O.USER_ID=:USER_ID AND O.USER_NAME='{$uname}' AND O.IS_DELETE = 0";
  486. $params[':USER_ID'] = \Yii::$app->user->id;
  487. $orderQuery = Order::find()
  488. ->alias('O')
  489. ->where($condition, $params)
  490. ->select('O.ID,O.SN,O.USER_NAME,O.ORDER_TYPE,O.ORDER_AMOUNT,O.STATUS,O.CREATED_AT,O.PAY_TYPE,O.PAY_AT,O.CONSIGNEE,O.MOBILE,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,U.REAL_NAME,O.COUNTRY_ID,O.CURRENCY_ID,C.CODE')
  491. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  492. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  493. // 订单中间表只查询待支付和支付失败的订单
  494. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  495. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  496. $orderStandardQuery = ApproachOrder::find()
  497. ->alias('O')
  498. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  499. ->select('O.ID,O.SN,O.USER_NAME,O.ORDER_TYPE,O.ORDER_AMOUNT,O.STATUS,O.CREATED_AT,O.PAY_TYPE,O.PAY_AT,O.CONSIGNEE,O.MOBILE,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,U.REAL_NAME,O.COUNTRY_ID,O.CURRENCY_ID,C.CODE')
  500. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  501. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  502. $queryAll = $orderQuery->union($orderStandardQuery, true);
  503. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  504. $totalCount = $query->count();
  505. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  506. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  507. $data = [
  508. 'list' => $lists,
  509. 'currentPage'=>$pagination->page,
  510. 'totalPages'=>$pagination->pageCount,
  511. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  512. 'totalCount' => $pagination->totalCount,
  513. 'pageSize' => $pagination->pageSize,
  514. ];
  515. foreach ($data['list'] as $key => $value) {
  516. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  517. // 订单中间表
  518. $orderGoods = ApproachOrderGoods::findAllAsArray('ORDER_SN=:SN', [':SN' => $value['SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  519. } else {
  520. // 订单表
  521. $orderGoods = OrderGoods::findAllAsArray('ORDER_SN=:SN', [':SN' => $value['SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  522. }
  523. $data['list'][$key]['hasChildren'] = $orderGoods;
  524. $data['list'][$key]['ORDER_TYPE'] = $value['ORDER_TYPE']=='ZC' ? 'Welcome Pack' : (in_array($value['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points');
  525. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  526. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  527. $data['list'][$key]['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  528. $data['list'][$key]['SHOW_BV'] = !in_array($value['PAY_TYPE'], [5, 6]);
  529. $data['list'][$key]['COUNTRY'] = Countries::getById($value['COUNTRY_ID'])['NAME'];
  530. $data['list'][$key]['CURRENCY'] = Currency::getById($value['CURRENCY_ID'])['CODE'];
  531. unset($data['list'][$key]['ADDRESS'], $data['list'][$key]['CITY_NAME'], $data['list'][$key]['LGA_NAME'], $data['list'][$key]['PROVINCE']);
  532. }
  533. return static::notice($data);
  534. }
  535. /**
  536. * 会员订单.(给别人复消、给别人报单、给别人升级)
  537. * 给别人复消:创建人是自己、USER_ID不是自己、ORDER_TYPE=FX
  538. * 给别人报单:创建人是自己、USER_ID是自己、ORDER_TYPE=ZC、收货人不是自己
  539. * 给别人升级:创建人是自己、USER_ID不是自己、ORDER_TYPE=ZC
  540. */
  541. public function actionMemberOrder() {
  542. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  543. $condition = " O.USER_ID<>:USER_ID AND O.USER_NAME<>'{$uname}' AND O.CREATE_USER='{$uname}' AND O.IS_DELETE = 0";
  544. $params[':USER_ID'] = \Yii::$app->user->id;
  545. $orderQuery = Order::find()
  546. ->alias('O')
  547. ->where($condition, $params)
  548. ->select('O.ID,O.SN,O.USER_NAME,O.ORDER_TYPE,O.ORDER_AMOUNT,O.STATUS,O.CREATED_AT,O.PAY_TYPE,O.PAY_AT,O.CONSIGNEE,O.MOBILE,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,U.REAL_NAME,O.COUNTRY_ID,O.CURRENCY_ID,C.CODE')
  549. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  550. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  551. // 订单中间表只查询待支付和支付失败的订单
  552. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  553. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  554. $orderStandardQuery = ApproachOrder::find()
  555. ->alias('O')
  556. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  557. ->select('O.ID,O.SN,O.USER_NAME,O.ORDER_TYPE,O.ORDER_AMOUNT,O.STATUS,O.CREATED_AT,O.PAY_TYPE,O.PAY_AT,O.CONSIGNEE,O.MOBILE,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,U.REAL_NAME,O.COUNTRY_ID,O.CURRENCY_ID,C.CODE')
  558. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  559. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  560. $queryAll = $orderQuery->union($orderStandardQuery, true);
  561. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  562. $totalCount = $query->count();
  563. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  564. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  565. $data = [
  566. 'list' => $lists,
  567. 'currentPage'=>$pagination->page,
  568. 'totalPages'=>$pagination->pageCount,
  569. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  570. 'totalCount' => $pagination->totalCount,
  571. 'pageSize' => $pagination->pageSize,
  572. ];
  573. foreach ($data['list'] as $key => $value) {
  574. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  575. // 订单中间表
  576. $orderGoods = ApproachOrderGoods::findAllAsArray('ORDER_SN=:SN', [':SN' => $value['SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  577. } else {
  578. // 订单表
  579. $orderGoods = OrderGoods::findAllAsArray('ORDER_SN=:SN', [':SN' => $value['SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  580. }
  581. $data['list'][$key]['hasChildren'] = $orderGoods;
  582. $data['list'][$key]['ORDER_TYPE'] = $value['ORDER_TYPE']=='ZC' ? 'Welcome Pack' : (in_array($value['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points');
  583. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  584. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  585. $data['list'][$key]['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  586. $data['list'][$key]['SHOW_BV'] = !in_array($value['PAY_TYPE'], [5, 6]);
  587. $data['list'][$key]['COUNTRY'] = Countries::getById($value['COUNTRY_ID'])['NAME'];
  588. $data['list'][$key]['CURRENCY'] = Currency::getById($value['CURRENCY_ID'])['CODE'];
  589. unset($data['list'][$key]['ADDRESS'], $data['list'][$key]['CITY_NAME'], $data['list'][$key]['LGA_NAME'], $data['list'][$key]['PROVINCE']);
  590. }
  591. return static::notice($data);
  592. }
  593. /**
  594. * 会员复消
  595. */
  596. public function actionReconsume() {
  597. // 会员信息
  598. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  599. if (!$user) {
  600. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  601. }
  602. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0 OR FIND_IN_SET(4,GIFT_TYPE)>0) AND N.COUNTRY_ID=:COUNTRY_ID';
  603. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  604. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  605. 'from' => ShopGoods::tableName() . ' AS S',
  606. 'join' => [
  607. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  608. ],
  609. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  610. 'limit' => 100,
  611. ]);
  612. $currencies = Currency::getAllData();
  613. $currencies = array_column($currencies, NULL, 'ID');
  614. foreach ($data['list'] as $key => $value) {
  615. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  616. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  617. }
  618. return static::notice($data);
  619. }
  620. /**
  621. * 帮会员复消购物车
  622. * @throws HttpException
  623. */
  624. public function actionReconsumeCart(){
  625. // 收货会员
  626. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  627. if (!$user) {
  628. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  629. }
  630. // 汇率
  631. $afterCountry = Countries::getById($user['COUNTRY_ID']);
  632. $afterCurrency = CurrencyConversions::getToUSDRate($afterCountry['LOCAL_CURRENCY_ID']);
  633. // 登录会员
  634. $userId = \Yii::$app->user->id;
  635. $userData = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  636. // 会员不能给自己复销
  637. if (\Yii::$app->request->get('userId') == $userId) {
  638. return static::notice(Yii::t('app', 'memberCanNotPayoutOfSelf'), 400);
  639. }
  640. // 汇率
  641. $beforeCountry = Countries::getById($userData['COUNTRY_ID']);
  642. $beforeCurrency = CurrencyConversions::getToUSDRate($beforeCountry['LOCAL_CURRENCY_ID']);
  643. $payList = ['cash'=>['name'=>'Account Balance'],];//余额支付
  644. $userBalance = [
  645. 'points' => 0,
  646. 'cash' => 0
  647. ];
  648. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  649. $userBalance['points'] = Tool::convertAmount($userBonusResult['RECONSUME_POINTS'], $beforeCurrency, $afterCurrency);
  650. }
  651. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  652. $userBalance['cash'] = Tool::convertAmount($userCashResult['CASH'], $beforeCurrency, $afterCurrency);
  653. }
  654. return static::notice(['payList'=>$payList,'userBalance'=>$userBalance]);
  655. }
  656. /**
  657. * 帮会员复消确认订单
  658. */
  659. public function actionReconsumeSureOrder(){
  660. if (\Yii::$app->request->isPost) {
  661. $formModel = new OrderForm();
  662. $formModel->scenario = 'reconsumeOrder';
  663. $formModel->remark = Yii::t('app', 'reconsume');
  664. $post = \Yii::$app->request->post();
  665. $post['type'] = DeclarationForm::TYPE_FX;
  666. $stockist = $post['decUserName'] ?? '';
  667. if (!$stockist) {
  668. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  669. }
  670. if ($formModel->load($post, '') && $formModel->reconsumeAdd()) {
  671. return static::notice(Yii::t('app', 'reconsumeSuccessfully'));
  672. } else {
  673. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  674. }
  675. }
  676. }
  677. /**
  678. * 商品列表tabs分类
  679. * @return mixed
  680. * @throws HttpException
  681. */
  682. public function actionGoodsActive()
  683. {
  684. $data = [
  685. // [
  686. // 'name' => 'bonus',
  687. // 'label' => '奖金商品',
  688. // ],
  689. [
  690. 'name' => '1',
  691. 'label' => Yii::t('app', 'standardProducts'),
  692. ],
  693. // [
  694. // 'name' => '4',
  695. // 'label' => Yii::t('app', 'travelFundProducts'),
  696. // ],
  697. [
  698. 'name' => '5',
  699. 'label' => Yii::t('app', 'carFundProducts'),
  700. ],
  701. [
  702. 'name' => '6',
  703. 'label' => Yii::t('app', 'villaFundProducts'),
  704. ],
  705. ];
  706. return static::notice($data);
  707. }
  708. /**
  709. * 导出订单.
  710. * @return mixed
  711. * @throws HttpException
  712. * @throws \Mpdf\MpdfException
  713. */
  714. public function actionOrderExport()
  715. {
  716. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  717. $orderSn = \Yii::$app->request->get('orderSn');
  718. $condition = " AND SN=:SN AND O.IS_DELETE = 0";
  719. $params = [
  720. ':SN' => $orderSn,
  721. ];
  722. $data = Order::lists($condition, $params, [
  723. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  724. 'orderBy' => 'O.CREATED_AT DESC',
  725. 'from' => Order::tableName() . ' AS O',
  726. 'join' => [
  727. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  728. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  729. ],
  730. ]);
  731. if (!$data['list']) {
  732. $condition .= ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)';
  733. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  734. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  735. $data = ApproachOrder::lists($condition, $params, [
  736. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  737. 'orderBy' => 'O.CREATED_AT DESC',
  738. 'from' => ApproachOrder::tableName() . ' AS O',
  739. 'join' => [
  740. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  741. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  742. ],
  743. ]);
  744. }
  745. $userId = '';
  746. $userName = '';
  747. $address = '';
  748. $mobile = '';
  749. $orderAt = '';
  750. $orderDetails = '';
  751. $orderAmount = 0; // 合计总额
  752. $orderNums = 0; // 合计总数
  753. $totalTaxAmount = 0; // 合计税额
  754. $totalAmount = 0;
  755. foreach ($data['list'] as $key => $value) {
  756. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  757. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  758. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  759. $userId = $value['USER_NAME'];
  760. $userName = $value['REAL_NAME'];
  761. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  762. $mobile = $value['MOBILE'];
  763. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  764. // 总价
  765. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  766. $orderAmount += $totalAmount;
  767. $orderNums += $value['BUY_NUMS'];
  768. // 税额
  769. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  770. $totalTaxAmount += $taxAmount;
  771. $productAmount = Tool::formatAmount($value['REAL_PRICE']);
  772. $taxAmount = Tool::formatAmount($taxAmount);
  773. $totalAmount = Tool::formatAmount($totalAmount);
  774. // 订单详情
  775. $orderDetails .= <<<EOT
  776. <tr>
  777. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  778. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  779. <td style="text-align: right;">{$productAmount}</td>
  780. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  781. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  782. <td style="text-align: right;">{$taxAmount}</td>
  783. <td style="text-align: right;">{$totalAmount}</td>
  784. </tr>
  785. EOT;
  786. }
  787. $memberCode = Yii::t('app', 'memberCode');
  788. $memberName = Yii::t('app', 'memberName');
  789. $memberAddress = Yii::t('app', 'memberAddress');
  790. $memberPhone = Yii::t('app', 'memberPhone');
  791. $orderCode = Yii::t('app', 'orderCode');
  792. $orderDetail = Yii::t('app', 'orderDetail');
  793. $productCode = Yii::t('app', 'productCode');
  794. $productName = Yii::t('app', 'productName');
  795. $productPrice = Yii::t('app', 'productPrice');
  796. $quantity = Yii::t('app', 'qty');
  797. $taxRate = Yii::t('app', 'taxRate');
  798. $totalTax = Yii::t('app', 'totalTax');
  799. $totalAmount = Yii::t('app', 'totalAmount');
  800. $total = Yii::t('app', 'total');
  801. $signature = Yii::t('app', 'signature');
  802. $date = Yii::t('app', 'date');
  803. $createAt = Yii::t('app', 'createAt');
  804. // 订单基本信息
  805. $orderBase = <<<ORDER
  806. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  807. <tr>
  808. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  809. <td width="70%">{$userId}</td>
  810. </tr>
  811. <tr>
  812. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  813. <td width="70%">{$userName}</td>
  814. </tr>
  815. <tr>
  816. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  817. <td width="70%">{$address}</td>
  818. </tr>
  819. <tr>
  820. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  821. <td width="70%">{$mobile}</td>
  822. </tr>
  823. <tr>
  824. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  825. <td width="70%">{$orderSn}</td>
  826. </tr>
  827. <tr>
  828. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  829. <td width="70%">{$orderAt}</td>
  830. </tr>
  831. <tr>
  832. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  833. <td class="bg"></td>
  834. </tr>
  835. </table>
  836. ORDER;
  837. $l['a_meta_charset'] = 'UTF-8';
  838. $l['a_meta_dir'] = 'ltr';
  839. $l['a_meta_language'] = 'zh';
  840. $l['w_page'] = '页面';
  841. $orderAmount = Tool::formatAmount($orderAmount);
  842. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  843. $context = <<<ORDER
  844. <!doctype html>
  845. <html lang="en">
  846. <head>
  847. <meta charset="UTF-8" />
  848. <title>{$orderDetail}</title>
  849. <style>
  850. table {
  851. border-collapse: collapse;
  852. }
  853. table td, table th {
  854. border: 1px solid #ccc;
  855. /*padding: 5px 5px;*/
  856. border-collapse: collapse;
  857. }
  858. /*td {*/
  859. /* padding: 120px;*/
  860. /*}*/
  861. .bg {
  862. background-color: #ccc;
  863. }
  864. </style>
  865. </head>
  866. <body>
  867. <div class="content">
  868. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  869. <div>
  870. <div style="display: block; width: 100%;">
  871. {$orderBase}
  872. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  873. <tr>
  874. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  875. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  876. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  877. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  878. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  879. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  880. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  881. </tr>
  882. {$orderDetails}
  883. <tr>
  884. <td colspan="3">{$total}</td>
  885. <td style="text-align: right;">{$orderNums}</td>
  886. <td></td>
  887. <td style="text-align: right;">{$totalTaxAmount}</td>
  888. <td style="text-align: right;">{$orderAmount}</td>
  889. </tr>
  890. </table>
  891. </div>
  892. <div style="width: 100%; margin-top: 50px; height: 30px;">
  893. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  894. <tr style="border: none;">
  895. <td width="70%" style="border: none;"></td>
  896. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  897. </tr>
  898. <tr style="border: none;">
  899. <td width="70%" style="border: none;"></td>
  900. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  901. </tr>
  902. </table>
  903. </div>
  904. </div>
  905. </div>
  906. </body>
  907. </html>
  908. ORDER;
  909. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  910. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  911. // 设置打印模式
  912. $pdf->SetCreator(PDF_CREATOR);
  913. $pdf->SetAuthor('DaZe');
  914. $pdf->SetTitle($orderSn);
  915. $pdf->SetSubject('TCPDF Tutorial');
  916. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  917. // 是否显示页眉
  918. $pdf->setPrintHeader(false);
  919. // 设置页眉字体
  920. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  921. // 页眉距离顶部的距离
  922. $pdf->SetHeaderMargin('5');
  923. // 是否显示页脚
  924. $pdf->setPrintFooter(false);
  925. // 设置默认等宽字体
  926. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  927. // 设置行高
  928. $pdf->setCellHeightRatio(1);
  929. // 设置左、上、右的间距
  930. $pdf->SetMargins('10', '0', '10');
  931. // 设置是否自动分页 距离底部多少距离时分页
  932. $pdf->SetAutoPageBreak(TRUE, '15');
  933. // 设置图像比例因子
  934. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  935. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  936. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  937. $pdf->setLanguageArray($l);
  938. }
  939. $pdf->setFontSubsetting(true);
  940. $pdf->AddPage();
  941. // 设置字体
  942. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  943. $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  944. if (!file_exists($image_file)) {
  945. $image_file = \Yii::$app->runtimePath . '/uploads/ngds-logo.jpg';
  946. }
  947. $image = file_get_contents($image_file);
  948. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  949. $pdf->writeHTML($context);
  950. ob_clean();
  951. $file_name = $orderSn . '.pdf';
  952. $path = 'pdfs/' . $file_name;
  953. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  954. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  955. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  956. }
  957. /**
  958. * 导出订单.
  959. * @return mixed
  960. * @throws HttpException
  961. */
  962. public function actionDecOrderExport()
  963. {
  964. $orderSn = \Yii::$app->request->get('orderSn');
  965. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  966. $params = [
  967. ':USER_ID' => \Yii::$app->user->id,
  968. ':ORDER_SN' => $orderSn,
  969. ];
  970. $data = DecOrder::lists($condition, $params, [
  971. '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',
  972. 'orderBy' => 'DO.CREATED_AT DESC',
  973. 'from' => DecOrder::tableName() . ' AS DO',
  974. 'join' => [
  975. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  976. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  977. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  978. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  979. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  980. ],
  981. ]);
  982. if (!$data['list']) {
  983. $data = ApproachDecOrder::lists($condition, $params, [
  984. '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',
  985. 'orderBy' => 'DO.CREATED_AT DESC',
  986. 'from' => ApproachDecOrder::tableName() . ' AS DO',
  987. 'join' => [
  988. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  989. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  990. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  991. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  992. ['LEFT JOIN', ApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  993. ],
  994. ]);
  995. }
  996. $userId = '';
  997. $userName = '';
  998. $address = '';
  999. $mobile = '';
  1000. $orderAt = '';
  1001. $orderDetails = '';
  1002. $orderAmount = 0; // 合计总额
  1003. $orderNums = 0; // 合计总数
  1004. $totalTaxAmount = 0; // 合计税额
  1005. $totalAmount = 0;
  1006. foreach ($data['list'] as $key => $value) {
  1007. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1008. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1009. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1010. $userId = $value['USER_NAME'];
  1011. $userName = $value['REAL_NAME'];
  1012. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1013. $mobile = $value['MOBILE'];
  1014. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1015. // 总价
  1016. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1017. $orderAmount += $totalAmount;
  1018. $orderNums += $value['BUY_NUMS'];
  1019. // 税额
  1020. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1021. $totalTaxAmount += $taxAmount;
  1022. $taxAmount = sprintf('%.2f', $taxAmount);
  1023. $totalAmount = sprintf('%.2f', $totalAmount);
  1024. // 订单详情
  1025. $orderDetails .= <<<EOT
  1026. <tr>
  1027. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1028. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1029. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1030. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1031. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1032. <td style="text-align: right;">{$taxAmount}</td>
  1033. <td style="text-align: right;">{$totalAmount}</td>
  1034. </tr>
  1035. EOT;
  1036. }
  1037. $memberCode = Yii::t('app', 'memberCode');
  1038. $memberName = Yii::t('app', 'memberName');
  1039. $memberAddress = Yii::t('app', 'memberAddress');
  1040. $memberPhone = Yii::t('app', 'memberPhone');
  1041. $orderCode = Yii::t('app', 'orderCode');
  1042. $orderDetail = Yii::t('app', 'orderDetail');
  1043. $productCode = Yii::t('app', 'productCode');
  1044. $productName = Yii::t('app', 'productName');
  1045. $productPrice = Yii::t('app', 'productPrice');
  1046. $quantity = Yii::t('app', 'qty');
  1047. $taxRate = Yii::t('app', 'taxRate');
  1048. $totalTax = Yii::t('app', 'totalTax');
  1049. $totalAmount = Yii::t('app', 'totalAmount');
  1050. $total = Yii::t('app', 'total');
  1051. $signature = Yii::t('app', 'signature');
  1052. $date = Yii::t('app', 'date');
  1053. $createAt = Yii::t('app', 'createAt');
  1054. // 订单基本信息
  1055. $orderBase = <<<ORDER
  1056. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1057. <tr>
  1058. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1059. <td width="70%">{$userId}</td>
  1060. </tr>
  1061. <tr>
  1062. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1063. <td width="70%">{$userName}</td>
  1064. </tr>
  1065. <tr>
  1066. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1067. <td width="70%">{$address}</td>
  1068. </tr>
  1069. <tr>
  1070. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1071. <td width="70%">{$mobile}</td>
  1072. </tr>
  1073. <tr>
  1074. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1075. <td width="70%">{$orderSn}</td>
  1076. </tr>
  1077. <tr>
  1078. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1079. <td width="70%">{$orderAt}</td>
  1080. </tr>
  1081. <tr>
  1082. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1083. <td class="bg"></td>
  1084. </tr>
  1085. </table>
  1086. ORDER;
  1087. $l['a_meta_charset'] = 'UTF-8';
  1088. $l['a_meta_dir'] = 'ltr';
  1089. $l['a_meta_language'] = 'zh';
  1090. $l['w_page'] = '页面';
  1091. $orderAmount = sprintf('%.2f', $orderAmount);
  1092. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1093. $context = <<<ORDER
  1094. <!doctype html>
  1095. <html lang="en">
  1096. <head>
  1097. <meta charset="UTF-8" />
  1098. <title>{$orderDetail}</title>
  1099. <style>
  1100. table {
  1101. border-collapse: collapse;
  1102. }
  1103. table td, table th {
  1104. border: 1px solid #ccc;
  1105. /*padding: 5px 5px;*/
  1106. border-collapse: collapse;
  1107. }
  1108. /*td {*/
  1109. /* padding: 120px;*/
  1110. /*}*/
  1111. .bg {
  1112. background-color: #ccc;
  1113. }
  1114. </style>
  1115. </head>
  1116. <body>
  1117. <div class="content">
  1118. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1119. <div>
  1120. <div style="display: block; width: 100%;">
  1121. {$orderBase}
  1122. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1123. <tr>
  1124. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1125. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1126. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1127. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1128. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1129. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1130. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1131. </tr>
  1132. {$orderDetails}
  1133. <tr>
  1134. <td colspan="3">{$total}</td>
  1135. <td style="text-align: right;">{$orderNums}</td>
  1136. <td></td>
  1137. <td style="text-align: right;">{$totalTaxAmount}</td>
  1138. <td style="text-align: right;">{$orderAmount}</td>
  1139. </tr>
  1140. </table>
  1141. </div>
  1142. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1143. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1144. <tr style="border: none;">
  1145. <td width="70%" style="border: none;"></td>
  1146. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1147. </tr>
  1148. <tr style="border: none;">
  1149. <td width="70%" style="border: none;"></td>
  1150. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1151. </tr>
  1152. </table>
  1153. </div>
  1154. </div>
  1155. </div>
  1156. </body>
  1157. </html>
  1158. ORDER;
  1159. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1160. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1161. // 设置打印模式
  1162. $pdf->SetCreator(PDF_CREATOR);
  1163. $pdf->SetAuthor('DaZe');
  1164. $pdf->SetTitle($orderSn);
  1165. $pdf->SetSubject('TCPDF Tutorial');
  1166. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1167. // 是否显示页眉
  1168. $pdf->setPrintHeader(false);
  1169. // 设置页眉字体
  1170. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1171. // 页眉距离顶部的距离
  1172. $pdf->SetHeaderMargin('5');
  1173. // 是否显示页脚
  1174. $pdf->setPrintFooter(false);
  1175. // 设置默认等宽字体
  1176. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1177. // 设置行高
  1178. $pdf->setCellHeightRatio(1);
  1179. // 设置左、上、右的间距
  1180. $pdf->SetMargins('10', '0', '10');
  1181. // 设置是否自动分页 距离底部多少距离时分页
  1182. $pdf->SetAutoPageBreak(TRUE, '15');
  1183. // 设置图像比例因子
  1184. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1185. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1186. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1187. $pdf->setLanguageArray($l);
  1188. }
  1189. $pdf->setFontSubsetting(true);
  1190. $pdf->AddPage();
  1191. // 设置字体
  1192. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1193. $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1194. if (!file_exists($image_file)) {
  1195. $image_file = \Yii::$app->runtimePath . '/uploads/ngds-logo.jpg';
  1196. }
  1197. $image = file_get_contents($image_file);
  1198. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1199. $pdf->writeHTML($context);
  1200. ob_clean();
  1201. $file_name = $orderSn . '.pdf';
  1202. $path = 'pdfs/' . $file_name;
  1203. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1204. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1205. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1206. }
  1207. /**
  1208. * 下载申请退货单
  1209. */
  1210. public function actionOrderBackingOut()
  1211. {
  1212. $file_name = 'orderBackingOut.xlsx';
  1213. return static::notice(['fileUrl' => $file_name]);
  1214. }
  1215. /**
  1216. * BA商品列表
  1217. * @return mixed
  1218. * @throws HttpException
  1219. */
  1220. public function actionBaProductList() {
  1221. // 商品分类
  1222. $categoryType = \Yii::$app->request->get('categoryType', 1);
  1223. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  1224. $isStudio = BaUser::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  1225. if($isStudio==1){
  1226. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  1227. }
  1228. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  1229. $params[':CATEGORY_TYPE'] = intval($categoryType);
  1230. $data = ShopGoods::lists($condition, $params, [
  1231. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1232. 'from' => ShopGoods::tableName(),
  1233. ]);
  1234. foreach ($data['list'] as $key => $value) {
  1235. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  1236. $data['list'][$key]['DISCOUNT'] = ShopGoods::getGoodType()[$value['TYPE']]['discount'];
  1237. } else {
  1238. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  1239. }
  1240. }
  1241. return static::notice($data);
  1242. }
  1243. /**
  1244. * BA购物车订单展示
  1245. * @throws HttpException
  1246. */
  1247. public function actionBaShoppingCart()
  1248. {
  1249. // 收货地址
  1250. $allAddress = BaReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1251. if($allAddress) {
  1252. foreach ($allAddress as $key => $row) {
  1253. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1254. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1255. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1256. }
  1257. }
  1258. // 运费.奈拉
  1259. $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
  1260. // 普通商品免运费阈值.奈拉
  1261. $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
  1262. return static::notice([
  1263. 'freight' => $freight,
  1264. 'freeShipping' => $freeShipping,
  1265. 'allAddress' => $allAddress,
  1266. ]);
  1267. }
  1268. /**
  1269. * 确认BA订单
  1270. * @throws HttpException|\yii\base\Exception
  1271. */
  1272. public function actionBaSureApproachOrder()
  1273. {
  1274. if (\Yii::$app->request->isPost) {
  1275. $formModel = new BaApproachOrderForm();
  1276. $formModel->scenario = 'userOrder';
  1277. $formModel->remark = Yii::t('app', 'reconsumeRemark');
  1278. $post = \Yii::$app->request->post();
  1279. $post['type'] = DeclarationForm::TYPE_FX;
  1280. if ($formModel->load($post, '') && $order = $formModel->add()) {
  1281. return static::notice($order);
  1282. } else {
  1283. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1284. }
  1285. }
  1286. }
  1287. /**
  1288. * 删除BA准订单
  1289. * @throws HttpException
  1290. */
  1291. public function actionBaDeleteApproachOrder()
  1292. {
  1293. $orderSn = \Yii::$app->request->post('orderSn');
  1294. // 删除订单中间表
  1295. BaApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  1296. // 删除订单商品中间表
  1297. BaApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  1298. return static::notice('');
  1299. }
  1300. /**
  1301. * BA订单
  1302. * @return mixed
  1303. * @throws HttpException
  1304. */
  1305. public function actionBaOrderList()
  1306. {
  1307. $uname = Info::getBaUserNameByUserId(\Yii::$app->user->id);
  1308. $condition = " (USER_ID=:USER_ID OR CREATE_USER='$uname') AND O.IS_DELETE = 0";
  1309. $params[':USER_ID'] = \Yii::$app->user->id;
  1310. $orderQuery = BaOrder::find()
  1311. ->alias('O')
  1312. ->where($condition, $params)
  1313. ->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')
  1314. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1315. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1316. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1317. ->orderBy('O.CREATED_AT DESC');
  1318. // 订单中间表只查询待支付和支付失败的订单
  1319. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1320. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1321. $orderStandardQuery = BaApproachOrder::find()
  1322. ->alias('O')
  1323. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1324. ->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')
  1325. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1326. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1327. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1328. ->orderBy('O.CREATED_AT DESC');
  1329. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1330. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1331. $totalCount = $query->count();
  1332. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1333. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1334. $data = [
  1335. 'list' => $lists,
  1336. 'currentPage'=>$pagination->page,
  1337. 'totalPages'=>$pagination->pageCount,
  1338. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1339. 'totalCount' => $pagination->totalCount,
  1340. 'pageSize' => $pagination->pageSize,
  1341. ];
  1342. foreach ($data['list'] as $key => $value) {
  1343. $data['list'][$key]['ORDER_TYPE'] = 'Reselling';
  1344. $data['list'][$key]['PAY_AT'] = $value['PAY_AT'] ? Date::convert($value['PAY_AT'],'Y-m-d H:i:s') : '';
  1345. $data['list'][$key]['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1346. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1347. $data['list'][$key]['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  1348. $data['list'][$key]['FULL_ADDRESS'] = $value['ADDRESS'];
  1349. $data['list'][$key]['TAX_AMOUNT'] = Tool::formatPrice(($value['REAL_PRICE'] - $value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100)) * $value['BUY_NUMS']);
  1350. $data['list'][$key]['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1351. if ($value['LGA_NAME']) {
  1352. $data['list'][$key]['FULL_ADDRESS'] .= ', ' . $value['LGA_NAME'];
  1353. }
  1354. }
  1355. return static::notice($data);
  1356. }
  1357. /**
  1358. * @throws HttpException
  1359. */
  1360. public function actionBaDecOrderList() {
  1361. $version = \Yii::$app->request->get('v');
  1362. $data = $version == 2 ? $this->baDecOrderListV2() : $this->baDecOrderListV1();
  1363. return static::notice($data);
  1364. }
  1365. /**
  1366. * 我的BA报单
  1367. * @return mixed
  1368. * @throws HttpException
  1369. */
  1370. public function baDecOrderListV1() {
  1371. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1372. $params[':USER_ID'] = \Yii::$app->user->id;
  1373. $orderQuery = BaDecOrder::find()
  1374. ->alias('DO')
  1375. ->where($condition, $params)
  1376. ->select('DO.*,O.STATUS,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.SKU_CODE,OG.GOODS_TITLE,OG.BUY_NUMS,OG.REAL_PRICE,OG.REAL_PV,OG.TAX_RATE')
  1377. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1378. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1379. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1380. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1381. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1382. ->orderBy('DO.CREATED_AT DESC');
  1383. // 订单中间表只查询待支付和支付失败的订单
  1384. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1385. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1386. $orderStandardQuery = BaApproachDecOrder::find()
  1387. ->alias('DO')
  1388. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1389. ->select('DO.*,O.STATUS,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.SKU_CODE,OG.GOODS_TITLE,OG.BUY_NUMS,OG.REAL_PRICE,OG.REAL_PV,OG.TAX_RATE')
  1390. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1391. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1392. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1393. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1394. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1395. ->orderBy('DO.CREATED_AT DESC');
  1396. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1397. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1398. $totalCount = $query->count();
  1399. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1400. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1401. $data = [
  1402. 'list' => $lists,
  1403. 'currentPage'=>$pagination->page,
  1404. 'totalPages'=>$pagination->pageCount,
  1405. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1406. 'totalCount' => $pagination->totalCount,
  1407. 'pageSize' => $pagination->pageSize,
  1408. ];
  1409. foreach ($data['list'] as &$value) {
  1410. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  1411. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1412. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1413. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  1414. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1415. }
  1416. return $data;
  1417. }
  1418. /**
  1419. * 我的BA报单
  1420. * @return mixed
  1421. * @throws HttpException
  1422. */
  1423. public function baDecOrderListV2() {
  1424. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1425. $params[':USER_ID'] = \Yii::$app->user->id;
  1426. $orderQuery = BaDecOrder::find()
  1427. ->alias('DO')
  1428. ->where($condition, $params)
  1429. ->select('DO.DEC_SN,DO.ORDER_SN,DO.CREATED_AT,O.STATUS,O.CONSIGNEE,O.MOBILE,O.PAY_TYPE,O.PAY_AT,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,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')
  1430. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1431. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1432. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1433. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1434. ->orderBy('DO.CREATED_AT DESC');
  1435. // 订单中间表只查询待支付和支付失败的订单
  1436. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1437. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1438. $orderStandardQuery = BaApproachDecOrder::find()
  1439. ->alias('DO')
  1440. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1441. ->select('DO.DEC_SN,DO.ORDER_SN,DO.CREATED_AT,O.STATUS,O.CONSIGNEE,O.MOBILE,O.PAY_TYPE,O.PAY_AT,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,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')
  1442. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1443. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1444. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1445. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1446. ->orderBy('DO.CREATED_AT DESC');
  1447. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1448. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1449. $totalCount = $query->count();
  1450. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1451. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1452. $data = [
  1453. 'list' => $lists,
  1454. 'currentPage'=>$pagination->page,
  1455. 'totalPages'=>$pagination->pageCount,
  1456. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1457. 'totalCount' => $pagination->totalCount,
  1458. 'pageSize' => $pagination->pageSize,
  1459. ];
  1460. foreach ($data['list'] as &$value) {
  1461. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  1462. // 订单中间表
  1463. $orderGoods = BaApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1464. } else {
  1465. // 订单表
  1466. $orderGoods = BaOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1467. }
  1468. $value['hasChildren'] = $orderGoods;
  1469. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1470. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1471. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1472. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  1473. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  1474. }
  1475. return $data;
  1476. }
  1477. /**
  1478. * 导出订单.
  1479. * @return mixed
  1480. * @throws HttpException
  1481. */
  1482. public function actionBaDecOrderExport()
  1483. {
  1484. $orderSn = \Yii::$app->request->get('orderSn');
  1485. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1486. $params = [
  1487. ':USER_ID' => \Yii::$app->user->id,
  1488. ':ORDER_SN' => $orderSn,
  1489. ];
  1490. $data = BaDecOrder::lists($condition, $params, [
  1491. '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',
  1492. 'orderBy' => 'DO.CREATED_AT DESC',
  1493. 'from' => BaDecOrder::tableName() . ' AS DO',
  1494. 'join' => [
  1495. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1496. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1497. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1498. ['LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1499. ['LEFT JOIN', BaOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1500. ],
  1501. ]);
  1502. if (!$data['list']) {
  1503. $data = BaApproachDecOrder::lists($condition, $params, [
  1504. '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',
  1505. 'orderBy' => 'DO.CREATED_AT DESC',
  1506. 'from' => BaApproachDecOrder::tableName() . ' AS DO',
  1507. 'join' => [
  1508. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1509. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1510. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1511. ['LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1512. ['LEFT JOIN', BaApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1513. ],
  1514. ]);
  1515. }
  1516. $userId = '';
  1517. $userName = '';
  1518. $address = '';
  1519. $mobile = '';
  1520. $orderAt = '';
  1521. $orderDetails = '';
  1522. $orderAmount = 0; // 合计总额
  1523. $orderNums = 0; // 合计总数
  1524. $totalTaxAmount = 0; // 合计税额
  1525. $totalAmount = 0;
  1526. foreach ($data['list'] as $key => $value) {
  1527. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1528. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1529. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1530. $userId = $value['USER_NAME'];
  1531. $userName = $value['REAL_NAME'];
  1532. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1533. $mobile = $value['MOBILE'];
  1534. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1535. // 总价
  1536. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1537. $orderAmount += $totalAmount;
  1538. $orderNums += $value['BUY_NUMS'];
  1539. // 税额
  1540. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1541. $totalTaxAmount += $taxAmount;
  1542. $taxAmount = sprintf('%.2f', $taxAmount);
  1543. $totalAmount = sprintf('%.2f', $totalAmount);
  1544. // 订单详情
  1545. $orderDetails .= <<<EOT
  1546. <tr>
  1547. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1548. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1549. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1550. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1551. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1552. <td style="text-align: right;">{$taxAmount}</td>
  1553. <td style="text-align: right;">{$totalAmount}</td>
  1554. </tr>
  1555. EOT;
  1556. }
  1557. $memberCode = Yii::t('app', 'memberCode');
  1558. $memberName = Yii::t('app', 'memberName');
  1559. $memberAddress = Yii::t('app', 'memberAddress');
  1560. $memberPhone = Yii::t('app', 'memberPhone');
  1561. $orderCode = Yii::t('app', 'orderCode');
  1562. $orderDetail = Yii::t('app', 'orderDetail');
  1563. $productCode = Yii::t('app', 'productCode');
  1564. $productName = Yii::t('app', 'productName');
  1565. $productPrice = Yii::t('app', 'productPrice');
  1566. $quantity = Yii::t('app', 'qty');
  1567. $taxRate = Yii::t('app', 'taxRate');
  1568. $totalTax = Yii::t('app', 'totalTax');
  1569. $totalAmount = Yii::t('app', 'totalAmount');
  1570. $total = Yii::t('app', 'total');
  1571. $signature = Yii::t('app', 'signature');
  1572. $date = Yii::t('app', 'date');
  1573. $createAt = Yii::t('app', 'createAt');
  1574. // 订单基本信息
  1575. $orderBase = <<<ORDER
  1576. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1577. <tr>
  1578. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1579. <td width="70%">{$userId}</td>
  1580. </tr>
  1581. <tr>
  1582. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1583. <td width="70%">{$userName}</td>
  1584. </tr>
  1585. <tr>
  1586. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1587. <td width="70%">{$address}</td>
  1588. </tr>
  1589. <tr>
  1590. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1591. <td width="70%">{$mobile}</td>
  1592. </tr>
  1593. <tr>
  1594. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1595. <td width="70%">{$orderSn}</td>
  1596. </tr>
  1597. <tr>
  1598. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1599. <td width="70%">{$orderAt}</td>
  1600. </tr>
  1601. <tr>
  1602. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1603. <td class="bg"></td>
  1604. </tr>
  1605. </table>
  1606. ORDER;
  1607. $l['a_meta_charset'] = 'UTF-8';
  1608. $l['a_meta_dir'] = 'ltr';
  1609. $l['a_meta_language'] = 'zh';
  1610. $l['w_page'] = '页面';
  1611. $orderAmount = sprintf('%.2f', $orderAmount);
  1612. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1613. $context = <<<ORDER
  1614. <!doctype html>
  1615. <html lang="en">
  1616. <head>
  1617. <meta charset="UTF-8" />
  1618. <title>{$orderDetail}</title>
  1619. <style>
  1620. table {
  1621. border-collapse: collapse;
  1622. }
  1623. table td, table th {
  1624. border: 1px solid #ccc;
  1625. /*padding: 5px 5px;*/
  1626. border-collapse: collapse;
  1627. }
  1628. /*td {*/
  1629. /* padding: 120px;*/
  1630. /*}*/
  1631. .bg {
  1632. background-color: #ccc;
  1633. }
  1634. </style>
  1635. </head>
  1636. <body>
  1637. <div class="content">
  1638. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1639. <div>
  1640. <div style="display: block; width: 100%;">
  1641. {$orderBase}
  1642. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1643. <tr>
  1644. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1645. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1646. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1647. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1648. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1649. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1650. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1651. </tr>
  1652. {$orderDetails}
  1653. <tr>
  1654. <td colspan="3">{$total}</td>
  1655. <td style="text-align: right;">{$orderNums}</td>
  1656. <td></td>
  1657. <td style="text-align: right;">{$totalTaxAmount}</td>
  1658. <td style="text-align: right;">{$orderAmount}</td>
  1659. </tr>
  1660. </table>
  1661. </div>
  1662. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1663. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1664. <tr style="border: none;">
  1665. <td width="70%" style="border: none;"></td>
  1666. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1667. </tr>
  1668. <tr style="border: none;">
  1669. <td width="70%" style="border: none;"></td>
  1670. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1671. </tr>
  1672. </table>
  1673. </div>
  1674. </div>
  1675. </div>
  1676. </body>
  1677. </html>
  1678. ORDER;
  1679. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1680. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1681. // 设置打印模式
  1682. $pdf->SetCreator(PDF_CREATOR);
  1683. $pdf->SetAuthor('DaZe');
  1684. $pdf->SetTitle($orderSn);
  1685. $pdf->SetSubject('TCPDF Tutorial');
  1686. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1687. // 是否显示页眉
  1688. $pdf->setPrintHeader(false);
  1689. // 设置页眉字体
  1690. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1691. // 页眉距离顶部的距离
  1692. $pdf->SetHeaderMargin('5');
  1693. // 是否显示页脚
  1694. $pdf->setPrintFooter(false);
  1695. // 设置默认等宽字体
  1696. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1697. // 设置行高
  1698. $pdf->setCellHeightRatio(1);
  1699. // 设置左、上、右的间距
  1700. $pdf->SetMargins('10', '0', '10');
  1701. // 设置是否自动分页 距离底部多少距离时分页
  1702. $pdf->SetAutoPageBreak(TRUE, '15');
  1703. // 设置图像比例因子
  1704. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1705. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1706. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1707. $pdf->setLanguageArray($l);
  1708. }
  1709. $pdf->setFontSubsetting(true);
  1710. $pdf->AddPage();
  1711. // 设置字体
  1712. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1713. $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1714. if (!file_exists($image_file)) {
  1715. $image_file = \Yii::$app->runtimePath . '/uploads/ngds-logo.jpg';
  1716. }
  1717. $image = file_get_contents($image_file);
  1718. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1719. $pdf->writeHTML($context);
  1720. ob_clean();
  1721. $file_name = $orderSn . '.pdf';
  1722. $path = 'pdfs/' . $file_name;
  1723. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1724. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1725. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1726. }
  1727. /**
  1728. * AMP
  1729. */
  1730. public function actionAutoMaintenance()
  1731. {
  1732. // 会员信息
  1733. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  1734. if (!$user) {
  1735. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  1736. }
  1737. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0 OR FIND_IN_SET(4,GIFT_TYPE)>0) AND N.COUNTRY_ID=:COUNTRY_ID AND AUTO_MAINTENANCE=1';
  1738. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  1739. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  1740. 'from' => ShopGoods::tableName() . ' AS S',
  1741. 'join' => [
  1742. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  1743. ],
  1744. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1745. 'limit' => 100,
  1746. ]);
  1747. $currencies = Currency::getAllData();
  1748. $currencies = array_column($currencies, NULL, 'ID');
  1749. foreach ($data['list'] as $key => $value) {
  1750. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  1751. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  1752. }
  1753. return static::notice($data);
  1754. }
  1755. /**
  1756. * AMP
  1757. */
  1758. public function actionAutoMaintenanceSureOrder()
  1759. {
  1760. if (\Yii::$app->request->isPost) {
  1761. $formModel = new OrderForm();
  1762. $formModel->scenario = 'autoMaintenanceOrder';
  1763. $formModel->remark = Yii::t('app', 'autoMaintenance');
  1764. $post = \Yii::$app->request->post();
  1765. $post['type'] = DeclarationForm::TYPE_FX;
  1766. $stockist = $post['decUserName'] ?? '';
  1767. if (!$stockist) {
  1768. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1769. }
  1770. try {
  1771. if ($formModel->load($post, '') && $formModel->autoMaintenanceSureOrder()) {
  1772. return static::notice(Yii::t('app', 'successfully'));
  1773. } else {
  1774. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1775. }
  1776. } catch (HttpException|Exception|\Throwable $e) {
  1777. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * AMP
  1783. */
  1784. public function actionAutoMaintenanceApproachSureOrder(){
  1785. if (\Yii::$app->request->isPost) {
  1786. $formModel = new ApproachAutoMaintenanceOrderForm();
  1787. $formModel->scenario = 'autoMaintenanceOrder';
  1788. $formModel->remark = 'Auto Maintenance Order';
  1789. $post = \Yii::$app->request->post();
  1790. $post['type'] = DeclarationForm::TYPE_FX;
  1791. $stockist = $post['decUserName'] ?? '';
  1792. if (!$stockist) {
  1793. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1794. }
  1795. try {
  1796. if ($formModel->load($post, '') && $order = $formModel->autoMaintenanceSureOrder()) {
  1797. return static::notice($order);
  1798. } else {
  1799. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1800. }
  1801. } catch (\yii\db\Exception|HttpException|Exception $e) {
  1802. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1803. }
  1804. }
  1805. }
  1806. }