ShopController.php 96 KB

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