ShopController.php 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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::find()
  611. ->alias('OG')
  612. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  613. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  614. ->where(['OG.ORDER_SN' => $value['SN']])
  615. ->asArray()
  616. ->all();
  617. } else {
  618. // 订单表
  619. $orderGoods = OrderGoods::find()
  620. ->alias('OG')
  621. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  622. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  623. ->where(['OG.ORDER_SN' => $value['SN']])
  624. ->asArray()
  625. ->all();
  626. }
  627. $data['list'][$key]['hasChildren'] = $orderGoods;
  628. $data['list'][$key]['ORDER_TYPE'] = $value['ORDER_TYPE']=='ZC' ? 'Welcome Pack' : (in_array($value['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points');
  629. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  630. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  631. $data['list'][$key]['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  632. $data['list'][$key]['SHOW_BV'] = !in_array($value['PAY_TYPE'], [5, 6]);
  633. $data['list'][$key]['COUNTRY'] = Countries::getById($value['COUNTRY_ID'])['NAME'];
  634. $data['list'][$key]['CURRENCY'] = Currency::getById($value['CURRENCY_ID'])['CODE'];
  635. unset($data['list'][$key]['ADDRESS'], $data['list'][$key]['CITY_NAME'], $data['list'][$key]['LGA_NAME'], $data['list'][$key]['PROVINCE']);
  636. }
  637. return static::notice($data);
  638. }
  639. /**
  640. * 会员复消
  641. */
  642. public function actionReconsume() {
  643. // 会员信息
  644. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  645. if (!$user) {
  646. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  647. }
  648. $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';
  649. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  650. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  651. 'from' => ShopGoods::tableName() . ' AS S',
  652. 'join' => [
  653. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  654. ],
  655. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  656. 'limit' => 100,
  657. ]);
  658. $currencies = Currency::getAllData();
  659. $currencies = array_column($currencies, NULL, 'ID');
  660. foreach ($data['list'] as $key => $value) {
  661. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  662. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  663. }
  664. return static::notice($data);
  665. }
  666. /**
  667. * 帮会员复消购物车
  668. * @throws HttpException
  669. */
  670. public function actionReconsumeCart(){
  671. // 收货会员
  672. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  673. if (!$user) {
  674. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  675. }
  676. // 汇率
  677. $afterCountry = Countries::getById($user['COUNTRY_ID']);
  678. $afterCurrency = CurrencyConversions::getToUSDRate($afterCountry['LOCAL_CURRENCY_ID']);
  679. // 登录会员
  680. $userId = \Yii::$app->user->id;
  681. $userData = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  682. // 会员不能给自己复销
  683. if (\Yii::$app->request->get('userId') == $userId) {
  684. return static::notice(Yii::t('app', 'memberCanNotPayoutOfSelf'), 400);
  685. }
  686. // 汇率
  687. $beforeCountry = Countries::getById($userData['COUNTRY_ID']);
  688. $beforeCurrency = CurrencyConversions::getToUSDRate($beforeCountry['LOCAL_CURRENCY_ID']);
  689. $payList = ['cash'=>['name'=>'Account Balance'],];//余额支付
  690. $userBalance = [
  691. 'points' => 0,
  692. 'cash' => 0
  693. ];
  694. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  695. $userBalance['points'] = Tool::convertAmount($userBonusResult['RECONSUME_POINTS'], $beforeCurrency, $afterCurrency);
  696. }
  697. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  698. $userBalance['cash'] = Tool::convertAmount($userCashResult['CASH'], $beforeCurrency, $afterCurrency);
  699. }
  700. return static::notice(['payList'=>$payList,'userBalance'=>$userBalance]);
  701. }
  702. /**
  703. * 帮会员复消确认订单
  704. */
  705. public function actionReconsumeSureOrder(){
  706. if (\Yii::$app->request->isPost) {
  707. $formModel = new OrderForm();
  708. $formModel->scenario = 'reconsumeOrder';
  709. $formModel->remark = Yii::t('app', 'reconsume');
  710. $post = \Yii::$app->request->post();
  711. $post['type'] = DeclarationForm::TYPE_FX;
  712. $stockist = $post['decUserName'] ?? '';
  713. if (!$stockist) {
  714. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  715. }
  716. if ($formModel->load($post, '') && $formModel->reconsumeAdd()) {
  717. return static::notice(Yii::t('app', 'reconsumeSuccessfully'));
  718. } else {
  719. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  720. }
  721. }
  722. }
  723. /**
  724. * 商品列表tabs分类
  725. * @return mixed
  726. * @throws HttpException
  727. */
  728. public function actionGoodsActive()
  729. {
  730. $data = [
  731. // [
  732. // 'name' => 'bonus',
  733. // 'label' => '奖金商品',
  734. // ],
  735. [
  736. 'name' => '1',
  737. 'label' => Yii::t('app', 'standardProducts'),
  738. ],
  739. // [
  740. // 'name' => '4',
  741. // 'label' => Yii::t('app', 'travelFundProducts'),
  742. // ],
  743. [
  744. 'name' => '5',
  745. 'label' => Yii::t('app', 'carFundProducts'),
  746. ],
  747. [
  748. 'name' => '6',
  749. 'label' => Yii::t('app', 'villaFundProducts'),
  750. ],
  751. ];
  752. return static::notice($data);
  753. }
  754. /**
  755. * 导出订单.
  756. * @return mixed
  757. * @throws HttpException
  758. * @throws \Mpdf\MpdfException
  759. */
  760. public function actionOrderExport()
  761. {
  762. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  763. $orderSn = \Yii::$app->request->get('orderSn');
  764. $condition = " AND SN=:SN AND O.IS_DELETE = 0";
  765. $params = [
  766. ':SN' => $orderSn,
  767. ];
  768. $data = Order::lists($condition, $params, [
  769. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  770. 'orderBy' => 'O.CREATED_AT DESC',
  771. 'from' => Order::tableName() . ' AS O',
  772. 'join' => [
  773. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  774. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  775. ],
  776. ]);
  777. if (!$data['list']) {
  778. $condition .= ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)';
  779. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  780. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  781. $data = ApproachOrder::lists($condition, $params, [
  782. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  783. 'orderBy' => 'O.CREATED_AT DESC',
  784. 'from' => ApproachOrder::tableName() . ' AS O',
  785. 'join' => [
  786. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  787. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  788. ],
  789. ]);
  790. }
  791. $userId = '';
  792. $userName = '';
  793. $address = '';
  794. $mobile = '';
  795. $orderAt = '';
  796. $orderDetails = '';
  797. $orderAmount = 0; // 合计总额
  798. $orderNums = 0; // 合计总数
  799. $totalTaxAmount = 0; // 合计税额
  800. $totalAmount = 0;
  801. foreach ($data['list'] as $key => $value) {
  802. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  803. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  804. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  805. $userId = $value['USER_NAME'];
  806. $userName = $value['REAL_NAME'];
  807. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  808. $mobile = $value['MOBILE'];
  809. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  810. // 总价
  811. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  812. $orderAmount += $totalAmount;
  813. $orderNums += $value['BUY_NUMS'];
  814. // 税额
  815. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  816. $totalTaxAmount += $taxAmount;
  817. $productAmount = Tool::formatAmount($value['REAL_PRICE']);
  818. $taxAmount = Tool::formatAmount($taxAmount);
  819. $totalAmount = Tool::formatAmount($totalAmount);
  820. // 订单详情
  821. $orderDetails .= <<<EOT
  822. <tr>
  823. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  824. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  825. <td style="text-align: right;">{$productAmount}</td>
  826. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  827. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  828. <td style="text-align: right;">{$taxAmount}</td>
  829. <td style="text-align: right;">{$totalAmount}</td>
  830. </tr>
  831. EOT;
  832. }
  833. $memberCode = Yii::t('app', 'memberCode');
  834. $memberName = Yii::t('app', 'memberName');
  835. $memberAddress = Yii::t('app', 'memberAddress');
  836. $memberPhone = Yii::t('app', 'memberPhone');
  837. $orderCode = Yii::t('app', 'orderCode');
  838. $orderDetail = Yii::t('app', 'orderDetail');
  839. $productCode = Yii::t('app', 'productCode');
  840. $productName = Yii::t('app', 'productName');
  841. $productPrice = Yii::t('app', 'productPrice');
  842. $quantity = Yii::t('app', 'qty');
  843. $taxRate = Yii::t('app', 'taxRate');
  844. $totalTax = Yii::t('app', 'totalTax');
  845. $totalAmount = Yii::t('app', 'totalAmount');
  846. $total = Yii::t('app', 'total');
  847. $signature = Yii::t('app', 'signature');
  848. $date = Yii::t('app', 'date');
  849. $createAt = Yii::t('app', 'createAt');
  850. // 订单基本信息
  851. $orderBase = <<<ORDER
  852. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  853. <tr>
  854. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  855. <td width="70%">{$userId}</td>
  856. </tr>
  857. <tr>
  858. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  859. <td width="70%">{$userName}</td>
  860. </tr>
  861. <tr>
  862. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  863. <td width="70%">{$address}</td>
  864. </tr>
  865. <tr>
  866. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  867. <td width="70%">{$mobile}</td>
  868. </tr>
  869. <tr>
  870. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  871. <td width="70%">{$orderSn}</td>
  872. </tr>
  873. <tr>
  874. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  875. <td width="70%">{$orderAt}</td>
  876. </tr>
  877. <tr>
  878. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  879. <td class="bg"></td>
  880. </tr>
  881. </table>
  882. ORDER;
  883. $l['a_meta_charset'] = 'UTF-8';
  884. $l['a_meta_dir'] = 'ltr';
  885. $l['a_meta_language'] = 'zh';
  886. $l['w_page'] = '页面';
  887. $orderAmount = Tool::formatAmount($orderAmount);
  888. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  889. $context = <<<ORDER
  890. <!doctype html>
  891. <html lang="en">
  892. <head>
  893. <meta charset="UTF-8" />
  894. <title>{$orderDetail}</title>
  895. <style>
  896. table {
  897. border-collapse: collapse;
  898. }
  899. table td, table th {
  900. border: 1px solid #ccc;
  901. /*padding: 5px 5px;*/
  902. border-collapse: collapse;
  903. }
  904. /*td {*/
  905. /* padding: 120px;*/
  906. /*}*/
  907. .bg {
  908. background-color: #ccc;
  909. }
  910. </style>
  911. </head>
  912. <body>
  913. <div class="content">
  914. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  915. <div>
  916. <div style="display: block; width: 100%;">
  917. {$orderBase}
  918. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  919. <tr>
  920. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  921. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  922. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  923. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  924. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  925. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  926. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  927. </tr>
  928. {$orderDetails}
  929. <tr>
  930. <td colspan="3">{$total}</td>
  931. <td style="text-align: right;">{$orderNums}</td>
  932. <td></td>
  933. <td style="text-align: right;">{$totalTaxAmount}</td>
  934. <td style="text-align: right;">{$orderAmount}</td>
  935. </tr>
  936. </table>
  937. </div>
  938. <div style="width: 100%; margin-top: 50px; height: 30px;">
  939. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  940. <tr style="border: none;">
  941. <td width="70%" style="border: none;"></td>
  942. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  943. </tr>
  944. <tr style="border: none;">
  945. <td width="70%" style="border: none;"></td>
  946. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  947. </tr>
  948. </table>
  949. </div>
  950. </div>
  951. </div>
  952. </body>
  953. </html>
  954. ORDER;
  955. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  956. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  957. // 设置打印模式
  958. $pdf->SetCreator(PDF_CREATOR);
  959. $pdf->SetAuthor('DaZe');
  960. $pdf->SetTitle($orderSn);
  961. $pdf->SetSubject('TCPDF Tutorial');
  962. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  963. // 是否显示页眉
  964. $pdf->setPrintHeader(false);
  965. // 设置页眉字体
  966. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  967. // 页眉距离顶部的距离
  968. $pdf->SetHeaderMargin('5');
  969. // 是否显示页脚
  970. $pdf->setPrintFooter(false);
  971. // 设置默认等宽字体
  972. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  973. // 设置行高
  974. $pdf->setCellHeightRatio(1);
  975. // 设置左、上、右的间距
  976. $pdf->SetMargins('10', '0', '10');
  977. // 设置是否自动分页 距离底部多少距离时分页
  978. $pdf->SetAutoPageBreak(TRUE, '15');
  979. // 设置图像比例因子
  980. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  981. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  982. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  983. $pdf->setLanguageArray($l);
  984. }
  985. $pdf->setFontSubsetting(true);
  986. $pdf->AddPage();
  987. // 设置字体
  988. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  989. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  990. // if (!file_exists($image_file)) {
  991. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  992. // }
  993. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  994. $image = file_get_contents($image_file);
  995. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  996. $pdf->writeHTML($context);
  997. ob_clean();
  998. $file_name = $orderSn . '.pdf';
  999. $path = 'pdfs/' . $file_name;
  1000. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1001. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1002. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1003. }
  1004. /**
  1005. * 导出订单.
  1006. * @return mixed
  1007. * @throws HttpException
  1008. */
  1009. public function actionDecOrderExport()
  1010. {
  1011. $orderSn = \Yii::$app->request->get('orderSn');
  1012. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1013. $params = [
  1014. ':USER_ID' => \Yii::$app->user->id,
  1015. ':ORDER_SN' => $orderSn,
  1016. ];
  1017. $data = DecOrder::lists($condition, $params, [
  1018. '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',
  1019. 'orderBy' => 'DO.CREATED_AT DESC',
  1020. 'from' => DecOrder::tableName() . ' AS DO',
  1021. 'join' => [
  1022. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1023. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1024. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1025. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1026. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1027. ],
  1028. ]);
  1029. if (!$data['list']) {
  1030. $data = ApproachDecOrder::lists($condition, $params, [
  1031. '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',
  1032. 'orderBy' => 'DO.CREATED_AT DESC',
  1033. 'from' => ApproachDecOrder::tableName() . ' AS DO',
  1034. 'join' => [
  1035. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1036. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1037. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1038. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1039. ['LEFT JOIN', ApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1040. ],
  1041. ]);
  1042. }
  1043. $userId = '';
  1044. $userName = '';
  1045. $address = '';
  1046. $mobile = '';
  1047. $orderAt = '';
  1048. $orderDetails = '';
  1049. $orderAmount = 0; // 合计总额
  1050. $orderNums = 0; // 合计总数
  1051. $totalTaxAmount = 0; // 合计税额
  1052. $totalAmount = 0;
  1053. foreach ($data['list'] as $key => $value) {
  1054. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1055. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1056. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1057. $userId = $value['USER_NAME'];
  1058. $userName = $value['REAL_NAME'];
  1059. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1060. $mobile = $value['MOBILE'];
  1061. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1062. // 总价
  1063. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1064. $orderAmount += $totalAmount;
  1065. $orderNums += $value['BUY_NUMS'];
  1066. // 税额
  1067. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1068. $totalTaxAmount += $taxAmount;
  1069. $taxAmount = sprintf('%.2f', $taxAmount);
  1070. $totalAmount = sprintf('%.2f', $totalAmount);
  1071. // 订单详情
  1072. $orderDetails .= <<<EOT
  1073. <tr>
  1074. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1075. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1076. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1077. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1078. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1079. <td style="text-align: right;">{$taxAmount}</td>
  1080. <td style="text-align: right;">{$totalAmount}</td>
  1081. </tr>
  1082. EOT;
  1083. }
  1084. $memberCode = Yii::t('app', 'memberCode');
  1085. $memberName = Yii::t('app', 'memberName');
  1086. $memberAddress = Yii::t('app', 'memberAddress');
  1087. $memberPhone = Yii::t('app', 'memberPhone');
  1088. $orderCode = Yii::t('app', 'orderCode');
  1089. $orderDetail = Yii::t('app', 'orderDetail');
  1090. $productCode = Yii::t('app', 'productCode');
  1091. $productName = Yii::t('app', 'productName');
  1092. $productPrice = Yii::t('app', 'productPrice');
  1093. $quantity = Yii::t('app', 'qty');
  1094. $taxRate = Yii::t('app', 'taxRate');
  1095. $totalTax = Yii::t('app', 'totalTax');
  1096. $totalAmount = Yii::t('app', 'totalAmount');
  1097. $total = Yii::t('app', 'total');
  1098. $signature = Yii::t('app', 'signature');
  1099. $date = Yii::t('app', 'date');
  1100. $createAt = Yii::t('app', 'createAt');
  1101. // 订单基本信息
  1102. $orderBase = <<<ORDER
  1103. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1104. <tr>
  1105. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1106. <td width="70%">{$userId}</td>
  1107. </tr>
  1108. <tr>
  1109. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1110. <td width="70%">{$userName}</td>
  1111. </tr>
  1112. <tr>
  1113. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1114. <td width="70%">{$address}</td>
  1115. </tr>
  1116. <tr>
  1117. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1118. <td width="70%">{$mobile}</td>
  1119. </tr>
  1120. <tr>
  1121. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1122. <td width="70%">{$orderSn}</td>
  1123. </tr>
  1124. <tr>
  1125. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1126. <td width="70%">{$orderAt}</td>
  1127. </tr>
  1128. <tr>
  1129. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1130. <td class="bg"></td>
  1131. </tr>
  1132. </table>
  1133. ORDER;
  1134. $l['a_meta_charset'] = 'UTF-8';
  1135. $l['a_meta_dir'] = 'ltr';
  1136. $l['a_meta_language'] = 'zh';
  1137. $l['w_page'] = '页面';
  1138. $orderAmount = sprintf('%.2f', $orderAmount);
  1139. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1140. $context = <<<ORDER
  1141. <!doctype html>
  1142. <html lang="en">
  1143. <head>
  1144. <meta charset="UTF-8" />
  1145. <title>{$orderDetail}</title>
  1146. <style>
  1147. table {
  1148. border-collapse: collapse;
  1149. }
  1150. table td, table th {
  1151. border: 1px solid #ccc;
  1152. /*padding: 5px 5px;*/
  1153. border-collapse: collapse;
  1154. }
  1155. /*td {*/
  1156. /* padding: 120px;*/
  1157. /*}*/
  1158. .bg {
  1159. background-color: #ccc;
  1160. }
  1161. </style>
  1162. </head>
  1163. <body>
  1164. <div class="content">
  1165. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1166. <div>
  1167. <div style="display: block; width: 100%;">
  1168. {$orderBase}
  1169. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1170. <tr>
  1171. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1172. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1173. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1174. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1175. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1176. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1177. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1178. </tr>
  1179. {$orderDetails}
  1180. <tr>
  1181. <td colspan="3">{$total}</td>
  1182. <td style="text-align: right;">{$orderNums}</td>
  1183. <td></td>
  1184. <td style="text-align: right;">{$totalTaxAmount}</td>
  1185. <td style="text-align: right;">{$orderAmount}</td>
  1186. </tr>
  1187. </table>
  1188. </div>
  1189. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1190. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1191. <tr style="border: none;">
  1192. <td width="70%" style="border: none;"></td>
  1193. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1194. </tr>
  1195. <tr style="border: none;">
  1196. <td width="70%" style="border: none;"></td>
  1197. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1198. </tr>
  1199. </table>
  1200. </div>
  1201. </div>
  1202. </div>
  1203. </body>
  1204. </html>
  1205. ORDER;
  1206. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1207. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1208. // 设置打印模式
  1209. $pdf->SetCreator(PDF_CREATOR);
  1210. $pdf->SetAuthor('DaZe');
  1211. $pdf->SetTitle($orderSn);
  1212. $pdf->SetSubject('TCPDF Tutorial');
  1213. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1214. // 是否显示页眉
  1215. $pdf->setPrintHeader(false);
  1216. // 设置页眉字体
  1217. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1218. // 页眉距离顶部的距离
  1219. $pdf->SetHeaderMargin('5');
  1220. // 是否显示页脚
  1221. $pdf->setPrintFooter(false);
  1222. // 设置默认等宽字体
  1223. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1224. // 设置行高
  1225. $pdf->setCellHeightRatio(1);
  1226. // 设置左、上、右的间距
  1227. $pdf->SetMargins('10', '0', '10');
  1228. // 设置是否自动分页 距离底部多少距离时分页
  1229. $pdf->SetAutoPageBreak(TRUE, '15');
  1230. // 设置图像比例因子
  1231. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1232. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1233. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1234. $pdf->setLanguageArray($l);
  1235. }
  1236. $pdf->setFontSubsetting(true);
  1237. $pdf->AddPage();
  1238. // 设置字体
  1239. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1240. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1241. // if (!file_exists($image_file)) {
  1242. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1243. // }
  1244. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1245. $image = file_get_contents($image_file);
  1246. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1247. $pdf->writeHTML($context);
  1248. ob_clean();
  1249. $file_name = $orderSn . '.pdf';
  1250. $path = 'pdfs/' . $file_name;
  1251. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1252. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1253. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1254. }
  1255. /**
  1256. * 下载申请退货单
  1257. */
  1258. public function actionOrderBackingOut()
  1259. {
  1260. $file_name = 'orderBackingOut.xlsx';
  1261. return static::notice(['fileUrl' => $file_name]);
  1262. }
  1263. /**
  1264. * BA商品列表
  1265. * @return mixed
  1266. * @throws HttpException
  1267. */
  1268. public function actionBaProductList() {
  1269. // 商品分类
  1270. $categoryType = \Yii::$app->request->get('categoryType', 1);
  1271. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  1272. $isStudio = BaUser::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  1273. if($isStudio==1){
  1274. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  1275. }
  1276. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  1277. $params[':CATEGORY_TYPE'] = intval($categoryType);
  1278. $data = ShopGoods::lists($condition, $params, [
  1279. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1280. 'from' => ShopGoods::tableName(),
  1281. ]);
  1282. foreach ($data['list'] as $key => $value) {
  1283. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  1284. $data['list'][$key]['DISCOUNT'] = ShopGoods::getGoodType()[$value['TYPE']]['discount'];
  1285. } else {
  1286. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  1287. }
  1288. }
  1289. return static::notice($data);
  1290. }
  1291. /**
  1292. * BA购物车订单展示
  1293. * @throws HttpException
  1294. */
  1295. public function actionBaShoppingCart()
  1296. {
  1297. // 收货地址
  1298. $allAddress = BaReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1299. if($allAddress) {
  1300. foreach ($allAddress as $key => $row) {
  1301. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1302. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1303. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1304. }
  1305. }
  1306. // 运费.奈拉
  1307. $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
  1308. // 普通商品免运费阈值.奈拉
  1309. $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
  1310. return static::notice([
  1311. 'freight' => $freight,
  1312. 'freeShipping' => $freeShipping,
  1313. 'allAddress' => $allAddress,
  1314. ]);
  1315. }
  1316. /**
  1317. * 确认BA订单
  1318. * @throws HttpException|\yii\base\Exception
  1319. */
  1320. public function actionBaSureApproachOrder()
  1321. {
  1322. if (\Yii::$app->request->isPost) {
  1323. $formModel = new BaApproachOrderForm();
  1324. $formModel->scenario = 'userOrder';
  1325. $formModel->remark = Yii::t('app', 'reconsumeRemark');
  1326. $post = \Yii::$app->request->post();
  1327. $post['type'] = DeclarationForm::TYPE_FX;
  1328. if ($formModel->load($post, '') && $order = $formModel->add()) {
  1329. return static::notice($order);
  1330. } else {
  1331. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1332. }
  1333. }
  1334. }
  1335. /**
  1336. * 删除BA准订单
  1337. * @throws HttpException
  1338. */
  1339. public function actionBaDeleteApproachOrder()
  1340. {
  1341. $orderSn = \Yii::$app->request->post('orderSn');
  1342. // 删除订单中间表
  1343. BaApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  1344. // 删除订单商品中间表
  1345. BaApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  1346. return static::notice('');
  1347. }
  1348. /**
  1349. * BA订单
  1350. * @return mixed
  1351. * @throws HttpException
  1352. */
  1353. public function actionBaOrderList()
  1354. {
  1355. $uname = Info::getBaUserNameByUserId(\Yii::$app->user->id);
  1356. $condition = " (USER_ID=:USER_ID OR CREATE_USER='$uname') AND O.IS_DELETE = 0";
  1357. $params[':USER_ID'] = \Yii::$app->user->id;
  1358. $orderQuery = BaOrder::find()
  1359. ->alias('O')
  1360. ->where($condition, $params)
  1361. ->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')
  1362. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1363. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1364. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1365. ->orderBy('O.CREATED_AT DESC');
  1366. // 订单中间表只查询待支付和支付失败的订单
  1367. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1368. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1369. $orderStandardQuery = BaApproachOrder::find()
  1370. ->alias('O')
  1371. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1372. ->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')
  1373. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1374. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1375. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1376. ->orderBy('O.CREATED_AT DESC');
  1377. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1378. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1379. $totalCount = $query->count();
  1380. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1381. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1382. $data = [
  1383. 'list' => $lists,
  1384. 'currentPage'=>$pagination->page,
  1385. 'totalPages'=>$pagination->pageCount,
  1386. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1387. 'totalCount' => $pagination->totalCount,
  1388. 'pageSize' => $pagination->pageSize,
  1389. ];
  1390. foreach ($data['list'] as $key => $value) {
  1391. $data['list'][$key]['ORDER_TYPE'] = 'Reselling';
  1392. $data['list'][$key]['PAY_AT'] = $value['PAY_AT'] ? Date::convert($value['PAY_AT'],'Y-m-d H:i:s') : '';
  1393. $data['list'][$key]['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1394. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1395. $data['list'][$key]['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  1396. $data['list'][$key]['FULL_ADDRESS'] = $value['ADDRESS'];
  1397. $data['list'][$key]['TAX_AMOUNT'] = Tool::formatPrice(($value['REAL_PRICE'] - $value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100)) * $value['BUY_NUMS']);
  1398. $data['list'][$key]['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1399. if ($value['LGA_NAME']) {
  1400. $data['list'][$key]['FULL_ADDRESS'] .= ', ' . $value['LGA_NAME'];
  1401. }
  1402. }
  1403. return static::notice($data);
  1404. }
  1405. /**
  1406. * @throws HttpException
  1407. */
  1408. public function actionBaDecOrderList() {
  1409. $version = \Yii::$app->request->get('v');
  1410. $data = $version == 2 ? $this->baDecOrderListV2() : $this->baDecOrderListV1();
  1411. return static::notice($data);
  1412. }
  1413. /**
  1414. * 我的BA报单
  1415. * @return mixed
  1416. * @throws HttpException
  1417. */
  1418. public function baDecOrderListV1() {
  1419. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1420. $params[':USER_ID'] = \Yii::$app->user->id;
  1421. $orderQuery = BaDecOrder::find()
  1422. ->alias('DO')
  1423. ->where($condition, $params)
  1424. ->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')
  1425. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1426. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1427. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1428. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1429. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1430. ->orderBy('DO.CREATED_AT DESC');
  1431. // 订单中间表只查询待支付和支付失败的订单
  1432. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1433. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1434. $orderStandardQuery = BaApproachDecOrder::find()
  1435. ->alias('DO')
  1436. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1437. ->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')
  1438. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1439. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1440. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1441. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1442. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1443. ->orderBy('DO.CREATED_AT DESC');
  1444. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1445. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1446. $totalCount = $query->count();
  1447. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1448. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1449. $data = [
  1450. 'list' => $lists,
  1451. 'currentPage'=>$pagination->page,
  1452. 'totalPages'=>$pagination->pageCount,
  1453. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1454. 'totalCount' => $pagination->totalCount,
  1455. 'pageSize' => $pagination->pageSize,
  1456. ];
  1457. foreach ($data['list'] as &$value) {
  1458. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  1459. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1460. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1461. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  1462. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1463. }
  1464. return $data;
  1465. }
  1466. /**
  1467. * 我的BA报单
  1468. * @return mixed
  1469. * @throws HttpException
  1470. */
  1471. public function baDecOrderListV2() {
  1472. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1473. $params[':USER_ID'] = \Yii::$app->user->id;
  1474. $orderQuery = BaDecOrder::find()
  1475. ->alias('DO')
  1476. ->where($condition, $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', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1482. ->orderBy('DO.CREATED_AT DESC');
  1483. // 订单中间表只查询待支付和支付失败的订单
  1484. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1485. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1486. $orderStandardQuery = BaApproachDecOrder::find()
  1487. ->alias('DO')
  1488. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1489. ->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')
  1490. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1491. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1492. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1493. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1494. ->orderBy('DO.CREATED_AT DESC');
  1495. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1496. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1497. $totalCount = $query->count();
  1498. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1499. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1500. $data = [
  1501. 'list' => $lists,
  1502. 'currentPage'=>$pagination->page,
  1503. 'totalPages'=>$pagination->pageCount,
  1504. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1505. 'totalCount' => $pagination->totalCount,
  1506. 'pageSize' => $pagination->pageSize,
  1507. ];
  1508. foreach ($data['list'] as &$value) {
  1509. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  1510. // 订单中间表
  1511. $orderGoods = BaApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1512. } else {
  1513. // 订单表
  1514. $orderGoods = BaOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1515. }
  1516. $value['hasChildren'] = $orderGoods;
  1517. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1518. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1519. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1520. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  1521. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  1522. }
  1523. return $data;
  1524. }
  1525. /**
  1526. * 导出订单.
  1527. * @return mixed
  1528. * @throws HttpException
  1529. */
  1530. public function actionBaDecOrderExport()
  1531. {
  1532. $orderSn = \Yii::$app->request->get('orderSn');
  1533. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1534. $params = [
  1535. ':USER_ID' => \Yii::$app->user->id,
  1536. ':ORDER_SN' => $orderSn,
  1537. ];
  1538. $data = BaDecOrder::lists($condition, $params, [
  1539. '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',
  1540. 'orderBy' => 'DO.CREATED_AT DESC',
  1541. 'from' => BaDecOrder::tableName() . ' AS DO',
  1542. 'join' => [
  1543. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1544. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1545. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1546. ['LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1547. ['LEFT JOIN', BaOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1548. ],
  1549. ]);
  1550. if (!$data['list']) {
  1551. $data = BaApproachDecOrder::lists($condition, $params, [
  1552. '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',
  1553. 'orderBy' => 'DO.CREATED_AT DESC',
  1554. 'from' => BaApproachDecOrder::tableName() . ' AS DO',
  1555. 'join' => [
  1556. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1557. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1558. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1559. ['LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1560. ['LEFT JOIN', BaApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1561. ],
  1562. ]);
  1563. }
  1564. $userId = '';
  1565. $userName = '';
  1566. $address = '';
  1567. $mobile = '';
  1568. $orderAt = '';
  1569. $orderDetails = '';
  1570. $orderAmount = 0; // 合计总额
  1571. $orderNums = 0; // 合计总数
  1572. $totalTaxAmount = 0; // 合计税额
  1573. $totalAmount = 0;
  1574. foreach ($data['list'] as $key => $value) {
  1575. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1576. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1577. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1578. $userId = $value['USER_NAME'];
  1579. $userName = $value['REAL_NAME'];
  1580. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1581. $mobile = $value['MOBILE'];
  1582. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1583. // 总价
  1584. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1585. $orderAmount += $totalAmount;
  1586. $orderNums += $value['BUY_NUMS'];
  1587. // 税额
  1588. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1589. $totalTaxAmount += $taxAmount;
  1590. $taxAmount = sprintf('%.2f', $taxAmount);
  1591. $totalAmount = sprintf('%.2f', $totalAmount);
  1592. // 订单详情
  1593. $orderDetails .= <<<EOT
  1594. <tr>
  1595. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1596. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1597. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1598. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1599. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1600. <td style="text-align: right;">{$taxAmount}</td>
  1601. <td style="text-align: right;">{$totalAmount}</td>
  1602. </tr>
  1603. EOT;
  1604. }
  1605. $memberCode = Yii::t('app', 'memberCode');
  1606. $memberName = Yii::t('app', 'memberName');
  1607. $memberAddress = Yii::t('app', 'memberAddress');
  1608. $memberPhone = Yii::t('app', 'memberPhone');
  1609. $orderCode = Yii::t('app', 'orderCode');
  1610. $orderDetail = Yii::t('app', 'orderDetail');
  1611. $productCode = Yii::t('app', 'productCode');
  1612. $productName = Yii::t('app', 'productName');
  1613. $productPrice = Yii::t('app', 'productPrice');
  1614. $quantity = Yii::t('app', 'qty');
  1615. $taxRate = Yii::t('app', 'taxRate');
  1616. $totalTax = Yii::t('app', 'totalTax');
  1617. $totalAmount = Yii::t('app', 'totalAmount');
  1618. $total = Yii::t('app', 'total');
  1619. $signature = Yii::t('app', 'signature');
  1620. $date = Yii::t('app', 'date');
  1621. $createAt = Yii::t('app', 'createAt');
  1622. // 订单基本信息
  1623. $orderBase = <<<ORDER
  1624. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1625. <tr>
  1626. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1627. <td width="70%">{$userId}</td>
  1628. </tr>
  1629. <tr>
  1630. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1631. <td width="70%">{$userName}</td>
  1632. </tr>
  1633. <tr>
  1634. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1635. <td width="70%">{$address}</td>
  1636. </tr>
  1637. <tr>
  1638. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1639. <td width="70%">{$mobile}</td>
  1640. </tr>
  1641. <tr>
  1642. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1643. <td width="70%">{$orderSn}</td>
  1644. </tr>
  1645. <tr>
  1646. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1647. <td width="70%">{$orderAt}</td>
  1648. </tr>
  1649. <tr>
  1650. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1651. <td class="bg"></td>
  1652. </tr>
  1653. </table>
  1654. ORDER;
  1655. $l['a_meta_charset'] = 'UTF-8';
  1656. $l['a_meta_dir'] = 'ltr';
  1657. $l['a_meta_language'] = 'zh';
  1658. $l['w_page'] = '页面';
  1659. $orderAmount = sprintf('%.2f', $orderAmount);
  1660. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1661. $context = <<<ORDER
  1662. <!doctype html>
  1663. <html lang="en">
  1664. <head>
  1665. <meta charset="UTF-8" />
  1666. <title>{$orderDetail}</title>
  1667. <style>
  1668. table {
  1669. border-collapse: collapse;
  1670. }
  1671. table td, table th {
  1672. border: 1px solid #ccc;
  1673. /*padding: 5px 5px;*/
  1674. border-collapse: collapse;
  1675. }
  1676. /*td {*/
  1677. /* padding: 120px;*/
  1678. /*}*/
  1679. .bg {
  1680. background-color: #ccc;
  1681. }
  1682. </style>
  1683. </head>
  1684. <body>
  1685. <div class="content">
  1686. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1687. <div>
  1688. <div style="display: block; width: 100%;">
  1689. {$orderBase}
  1690. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1691. <tr>
  1692. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1693. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1694. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1695. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1696. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1697. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1698. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1699. </tr>
  1700. {$orderDetails}
  1701. <tr>
  1702. <td colspan="3">{$total}</td>
  1703. <td style="text-align: right;">{$orderNums}</td>
  1704. <td></td>
  1705. <td style="text-align: right;">{$totalTaxAmount}</td>
  1706. <td style="text-align: right;">{$orderAmount}</td>
  1707. </tr>
  1708. </table>
  1709. </div>
  1710. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1711. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1712. <tr style="border: none;">
  1713. <td width="70%" style="border: none;"></td>
  1714. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1715. </tr>
  1716. <tr style="border: none;">
  1717. <td width="70%" style="border: none;"></td>
  1718. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1719. </tr>
  1720. </table>
  1721. </div>
  1722. </div>
  1723. </div>
  1724. </body>
  1725. </html>
  1726. ORDER;
  1727. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1728. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1729. // 设置打印模式
  1730. $pdf->SetCreator(PDF_CREATOR);
  1731. $pdf->SetAuthor('DaZe');
  1732. $pdf->SetTitle($orderSn);
  1733. $pdf->SetSubject('TCPDF Tutorial');
  1734. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1735. // 是否显示页眉
  1736. $pdf->setPrintHeader(false);
  1737. // 设置页眉字体
  1738. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1739. // 页眉距离顶部的距离
  1740. $pdf->SetHeaderMargin('5');
  1741. // 是否显示页脚
  1742. $pdf->setPrintFooter(false);
  1743. // 设置默认等宽字体
  1744. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1745. // 设置行高
  1746. $pdf->setCellHeightRatio(1);
  1747. // 设置左、上、右的间距
  1748. $pdf->SetMargins('10', '0', '10');
  1749. // 设置是否自动分页 距离底部多少距离时分页
  1750. $pdf->SetAutoPageBreak(TRUE, '15');
  1751. // 设置图像比例因子
  1752. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1753. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1754. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1755. $pdf->setLanguageArray($l);
  1756. }
  1757. $pdf->setFontSubsetting(true);
  1758. $pdf->AddPage();
  1759. // 设置字体
  1760. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1761. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1762. // if (!file_exists($image_file)) {
  1763. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1764. // }
  1765. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1766. $image = file_get_contents($image_file);
  1767. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1768. $pdf->writeHTML($context);
  1769. ob_clean();
  1770. $file_name = $orderSn . '.pdf';
  1771. $path = 'pdfs/' . $file_name;
  1772. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1773. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1774. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1775. }
  1776. /**
  1777. * AMP
  1778. */
  1779. public function actionAutoMaintenance()
  1780. {
  1781. // 会员信息
  1782. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  1783. if (!$user) {
  1784. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  1785. }
  1786. $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';
  1787. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  1788. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  1789. 'from' => ShopGoods::tableName() . ' AS S',
  1790. 'join' => [
  1791. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  1792. ],
  1793. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1794. 'limit' => 100,
  1795. ]);
  1796. $currencies = Currency::getAllData();
  1797. $currencies = array_column($currencies, NULL, 'ID');
  1798. foreach ($data['list'] as $key => $value) {
  1799. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  1800. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  1801. }
  1802. return static::notice($data);
  1803. }
  1804. /**
  1805. * AMP
  1806. */
  1807. public function actionAutoMaintenanceSureOrder()
  1808. {
  1809. if (\Yii::$app->request->isPost) {
  1810. $formModel = new OrderForm();
  1811. $formModel->scenario = 'autoMaintenanceOrder';
  1812. $formModel->remark = Yii::t('app', 'autoMaintenance');
  1813. $post = \Yii::$app->request->post();
  1814. $post['type'] = DeclarationForm::TYPE_FX;
  1815. $stockist = $post['decUserName'] ?? '';
  1816. if (!$stockist) {
  1817. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1818. }
  1819. try {
  1820. if ($formModel->load($post, '') && $formModel->autoMaintenanceSureOrder()) {
  1821. return static::notice(Yii::t('app', 'successfully'));
  1822. } else {
  1823. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1824. }
  1825. } catch (HttpException|Exception|\Throwable $e) {
  1826. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1827. }
  1828. }
  1829. }
  1830. /**
  1831. * AMP
  1832. */
  1833. public function actionAutoMaintenanceApproachSureOrder(){
  1834. if (\Yii::$app->request->isPost) {
  1835. $formModel = new ApproachAutoMaintenanceOrderForm();
  1836. $formModel->scenario = 'autoMaintenanceOrder';
  1837. $formModel->remark = 'Auto Maintenance Order';
  1838. $post = \Yii::$app->request->post();
  1839. $post['type'] = DeclarationForm::TYPE_FX;
  1840. $stockist = $post['decUserName'] ?? '';
  1841. if (!$stockist) {
  1842. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1843. }
  1844. try {
  1845. if ($formModel->load($post, '') && $order = $formModel->autoMaintenanceSureOrder()) {
  1846. return static::notice($order);
  1847. } else {
  1848. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1849. }
  1850. } catch (\yii\db\Exception|HttpException|Exception $e) {
  1851. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1852. }
  1853. }
  1854. }
  1855. }