ShopController.php 95 KB

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