ShopController.php 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  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. * @throws HttpException
  187. */
  188. public function actionNoLoginShowCart(){
  189. $countryId = Yii::$app->request->get('countryId');
  190. if (!filter_var($countryId, FILTER_VALIDATE_INT) || $countryId <= 0) {
  191. return static::notice('countryId is required', 400);
  192. }
  193. $user = User::find()
  194. ->select('ID,COUNTRY_ID')
  195. ->where(['COUNTRY_ID' => $countryId, 'IS_DEC' => 1])
  196. ->orderBy(['USER_NAME' => SORT_ASC])
  197. ->asArray()
  198. ->one();
  199. if (!$user) {
  200. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  201. }
  202. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID AND COUNTRY_ID=:COUNTRY_ID', [
  203. ':USER_ID' => $user['ID'],
  204. ':COUNTRY_ID' => $user['COUNTRY_ID'],
  205. ]);
  206. if ($allAddress) {
  207. foreach ($allAddress as $key => $row) {
  208. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  209. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  210. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  211. }
  212. }
  213. $country = Countries::getById($user['COUNTRY_ID']);
  214. $coin = Currency::getById($country['LOCAL_CURRENCY_ID']);
  215. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  216. $freeTemplate = FreeTemplate::getByCountryId($user['COUNTRY_ID']);
  217. $sysConfig = Cache::getSystemConfig();
  218. return static::notice([
  219. 'payList' => ShopGoods::payTypesNoLogin(),
  220. 'allAddress' => $allAddress,
  221. 'sellType' => ShopGoods::getCategoryTypeNoLogin(),
  222. 'freight' => $freeTemplate['freight'] ?? 0,
  223. 'freeShipping' => $freeTemplate['free_shipping'] ?? 0,
  224. 'isDec' => 1,
  225. 'coin' => 'NGN',
  226. 'ampDivideLine' => (float)($sysConfig['ampBVCondition']['VALUE'] ?? 0),
  227. ]);
  228. }
  229. /**
  230. * 确认订单
  231. */
  232. public function actionSureOrder(){
  233. if (\Yii::$app->request->isPost) {
  234. $formModel = new OrderForm();
  235. $formModel->scenario = 'userOrder';
  236. $formModel->remark = 'FX';//复销备注
  237. $post = \Yii::$app->request->post();
  238. $post['type'] = DeclarationForm::TYPE_FX;
  239. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  240. $stockist = $post['userName'] ?? '';
  241. if (!$stockist) {
  242. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  243. }
  244. if($loginUserInfo['IS_DEC'] == 1 && $stockist != $loginUserInfo['USER_NAME']){
  245. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  246. }
  247. if($loginUserInfo['IS_DEC'] == 0){
  248. //查询报单编号是否存在
  249. $stockistId = Info::getUserIdByUserName($stockist);
  250. if(!$stockistId){
  251. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  252. }
  253. $isDec = User::getEnCodeInfo($stockistId)['IS_DEC'];
  254. if(!$isDec){
  255. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  256. }
  257. }
  258. $formModel->decUserName = $stockist;
  259. if ($formModel->load($post, '') && $order = $formModel->add()) {
  260. return static::notice($order);
  261. } else {
  262. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  263. }
  264. }
  265. }
  266. /**
  267. * 订单支付成功
  268. * @throws HttpException
  269. */
  270. public function actionPaySuccess(){
  271. $orderSn = \Yii::$app->request->get('orderSn');
  272. $data = null;
  273. if($orderSn){
  274. $data = Order::findOneAsArray('SN=:SN', [':SN'=>$orderSn]);
  275. }
  276. return static::notice($data);
  277. }
  278. /**
  279. * 确认订单
  280. */
  281. public function actionSureApproachOrder(){
  282. if (\Yii::$app->request->isPost) {
  283. $formModel = new ApproachOrderForm();
  284. $formModel->scenario = 'userOrder';
  285. $formModel->remark = '复销备注';
  286. $post = \Yii::$app->request->post();
  287. $post['type'] = DeclarationForm::TYPE_FX;
  288. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  289. $stockist = $post['userName'] ?? '';
  290. if (!$stockist) {
  291. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  292. }
  293. if($loginUserInfo['IS_DEC'] == 1 && !empty($post['userName']) && $post['userName'] != $loginUserInfo['USER_NAME']){
  294. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  295. }
  296. //查询报单编号是否存在
  297. if($loginUserInfo['IS_DEC'] == 0){
  298. $userId = Info::getUserIdByUserName($post['userName']);
  299. if(!$userId){
  300. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  301. }
  302. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  303. if(!$isDec){
  304. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  305. }
  306. }
  307. $formModel->decUserName = $post['userName'];
  308. if ($formModel->load($post, '') && $order = $formModel->add()) {
  309. return static::notice($order);
  310. } else {
  311. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  312. }
  313. }
  314. }
  315. /**
  316. * 确认复消订单
  317. */
  318. public function actionSureApproachReconsumeOrder(){
  319. if (\Yii::$app->request->isPost) {
  320. $formModel = new ApproachReconsumeOrderForm();
  321. $formModel->scenario = 'reconsumeOrder';
  322. $formModel->remark = Yii::t('app', 'reconsume');
  323. $post = \Yii::$app->request->post();
  324. $post['type'] = DeclarationForm::TYPE_FX;
  325. $stockist = $post['userName'] ?? '';
  326. if (!$stockist) {
  327. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  328. }
  329. if ($formModel->load($post, '') && $order = $formModel->add()) {
  330. return static::notice($order);
  331. } else {
  332. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  333. }
  334. }
  335. }
  336. /**
  337. * PayStack支付成功的webhook.
  338. * @throws HttpException
  339. * @throws \Exception
  340. */
  341. public function actionVerifyApproachOrder() {
  342. LoggerTool::error('actionVerifyApproachOrder in: ');
  343. if (\Yii::$app->request->isPost) {
  344. $rawBody = \Yii::$app->request->getRawBody();
  345. LoggerTool::error('actionVerifyApproachOrder in: '.$rawBody);
  346. $signature = (string) \Yii::$app->request->headers->get('x-paystack-signature', '');
  347. if (!PayStack::verifyWebhookSignature($rawBody, $signature, PayStack::getSecretKey())) {
  348. return http_response_code(401);
  349. }
  350. $request = json_decode($rawBody, true);
  351. if (!is_array($request) || empty($request['data']) || empty($request['data']['reference'])) {
  352. return http_response_code(400);
  353. }
  354. // 支付webhook回调日志
  355. Tool::approachOrderCall($request);
  356. try {
  357. $webhookData = $request['data'];
  358. $verified = PayStack::transactionVerify((string) $webhookData['reference']);
  359. if (empty($verified['status']) || empty($verified['data']) || !is_array($verified['data'])) {
  360. LoggerTool::error('PayStack transaction verification failed for reference ' . $webhookData['reference']);
  361. return http_response_code(500);
  362. }
  363. $data = $verified['data'];
  364. $status = $data['status'] ?? 'false';
  365. // 订单状态
  366. $orderStatus = ($status == 'success') ? \Yii::$app->params['orderStatus']['paid']['value'] : \Yii::$app->params['orderStatus']['failPaid']['value'];
  367. $customFields = $data['metadata']['custom_fields'] ?? [];
  368. $orderSn = $customFields[0]['value'] ?? '';
  369. $orderType = $customFields[1]['value'] ?? false;
  370. if ($orderType === 'guestUserDec') {
  371. if ($status !== 'success') {
  372. (new GuestDeclarationOrderService())->markPaymentFailed((string) $data['reference'], $data);
  373. return http_response_code(200);
  374. }
  375. (new GuestDeclarationOrderService())->completePaidDraft($orderSn, $data);
  376. return http_response_code(200);
  377. }
  378. $formModel = new ApproachOrderForm();
  379. $formModel->scenario = 'verifyPayStack';
  380. $load = [
  381. 'sn' => $orderSn,
  382. 'scenario' => 'verifyPayStack',
  383. 'status' => $orderStatus,
  384. 'note' => [
  385. 'id' => $data['id'] ?? null,
  386. 'domain' => $data['domain'] ?? null,
  387. 'status' => $status,
  388. 'reference' => $data['reference'],
  389. 'amount' => $data['amount'] ?? null,
  390. 'created_at' => $data['created_at'] ?? null,
  391. 'paid_at' => $data['paid_at'] ?? null,
  392. 'channel' => $data['channel'] ?? null,
  393. 'currency' => $data['currency'] ?? null,
  394. 'metadata' => $data['metadata'] ?? [],
  395. 'email' => $data['customer']['email'] ?? '',
  396. ],
  397. ];
  398. if ($formModel->load($load, '') && $order = $formModel->verifyPayStack()) {
  399. return http_response_code(200);
  400. } else {
  401. LoggerTool::error(Form::formatErrorsForApi($formModel->getErrors()));
  402. return http_response_code(500);
  403. }
  404. } catch (\Throwable $e) {
  405. LoggerTool::error('actionVerifyApproachOrder failed: ' . get_class($e));
  406. return http_response_code(500);
  407. }
  408. }
  409. return http_response_code(200);
  410. }
  411. /**
  412. * 删除准订单
  413. */
  414. public function actionDeleteApproachOrder()
  415. {
  416. $orderSn = \Yii::$app->request->post('orderSn');
  417. // 删除订单中间表
  418. ApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  419. // 删除订单商品中间表
  420. ApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  421. // 删除报单中间表
  422. ApproachDecOrder::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  423. return static::notice('');
  424. }
  425. /**
  426. * @throws HttpException
  427. */
  428. public function actionDecOrderList() {
  429. $version = \Yii::$app->request->get('v');
  430. $data = $version == 2 ? $this->decOrderListV2() : $this->decOrderListV1();
  431. return static::notice($data);
  432. }
  433. /**
  434. * 我的报单
  435. * @return mixed
  436. * @throws HttpException
  437. */
  438. public function decOrderListV1() {
  439. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  440. $params[':USER_ID'] = \Yii::$app->user->id;
  441. $orderQuery = DecOrder::find()
  442. ->alias('DO')
  443. ->where($condition, $params)
  444. ->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')
  445. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  446. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  447. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  448. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  449. ->join('LEFT JOIN', Order::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  450. ->orderBy('DO.CREATED_AT DESC');
  451. // 订单中间表只查询待支付和支付失败的订单
  452. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  453. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  454. $orderStandardQuery = ApproachDecOrder::find()
  455. ->alias('DO')
  456. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  457. ->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')
  458. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  459. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  460. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  461. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  462. ->join('LEFT JOIN', ApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  463. ->orderBy('DO.CREATED_AT DESC');
  464. $queryAll = $orderQuery->union($orderStandardQuery, true);
  465. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  466. $totalCount = $query->count();
  467. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  468. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  469. $data = [
  470. 'list' => $lists,
  471. 'currentPage'=>$pagination->page,
  472. 'totalPages'=>$pagination->pageCount,
  473. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  474. 'totalCount' => $pagination->totalCount,
  475. 'pageSize' => $pagination->pageSize,
  476. ];
  477. foreach ($data['list'] as &$value) {
  478. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  479. $value['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  480. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  481. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  482. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  483. }
  484. return $data;
  485. }
  486. /**
  487. * 我的报单
  488. * @return mixed
  489. * @throws HttpException
  490. */
  491. public function decOrderListV2() {
  492. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  493. $params[':USER_ID'] = \Yii::$app->user->id;
  494. $orderQuery = DecOrder::find()
  495. ->alias('DO')
  496. ->where($condition, $params)
  497. ->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')
  498. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  499. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  500. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  501. ->join('LEFT JOIN', Order::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  502. ->orderBy('DO.CREATED_AT DESC');
  503. // 订单中间表只查询待支付和支付失败的订单
  504. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  505. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  506. $orderStandardQuery = ApproachDecOrder::find()
  507. ->alias('DO')
  508. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  509. ->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')
  510. ->join('LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  511. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  512. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  513. ->join('LEFT JOIN', ApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  514. ->orderBy('DO.CREATED_AT DESC');
  515. $queryAll = $orderQuery->union($orderStandardQuery, true);
  516. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  517. $totalCount = $query->count();
  518. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  519. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  520. $data = [
  521. 'list' => $lists,
  522. 'currentPage'=>$pagination->page,
  523. 'totalPages'=>$pagination->pageCount,
  524. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  525. 'totalCount' => $pagination->totalCount,
  526. 'pageSize' => $pagination->pageSize,
  527. ];
  528. foreach ($data['list'] as &$value) {
  529. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  530. // 订单中间表
  531. $orderGoods = ApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  532. } else {
  533. // 订单表
  534. $orderGoods = OrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  535. }
  536. $value['hasChildren'] = $orderGoods;
  537. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  538. $value['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  539. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  540. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  541. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  542. }
  543. return $data;
  544. }
  545. /**
  546. * 无需登录查看最小编号报单中心账号的订单详情
  547. */
  548. public function actionNoLoginOrderDetail()
  549. {
  550. $orderSn = Yii::$app->request->get('orderSn');
  551. if (!is_scalar($orderSn) || trim((string)$orderSn) === '') {
  552. return static::notice('orderSn is required', 400);
  553. }
  554. $orderSn = trim((string)$orderSn);
  555. $order = Order::find()
  556. ->alias('O')
  557. ->select('O.ID,O.SN,O.USER_ID,O.USER_NAME,O.ORDER_TYPE,O.ORDER_AMOUNT,O.PV,O.PAY_AMOUNT,O.PAY_PV,O.STATUS,O.CREATED_AT,O.PAY_TYPE,O.PAY_AT,O.CONSIGNEE,O.MOBILE,O.TEL,O.ADDRESS,O.CITY_NAME,O.LGA_NAME,O.PROVINCE,O.COUNTRY_ID,O.CURRENCY_ID,O.FREIGHT,O.PAY_FREIGHT,O.DELIVERY_STATUS,O.DELIVERY_PERIOD,O.DELIVERY_AT,O.EXPRESS_COMPANY,O.ORDER_TRACK_NO,O.EXPRESS_TYPE,O.FRONT_REMARK,O.PERIOD_NUM,O.WAREHOUSE,O.EMAIL,O.AUTO_MAINTENANCE,O.INVOICE_REMARK,U.REAL_NAME,C.CODE AS CURRENCY')
  558. ->leftJoin(User::tableName() . ' U', 'U.ID=O.USER_ID')
  559. ->leftJoin(Currency::tableName() . ' C', 'C.ID=O.CURRENCY_ID')
  560. ->where(['O.SN' => $orderSn, 'O.IS_DELETE' => 0])
  561. ->asArray()
  562. ->one();
  563. if (!$order) {
  564. return static::notice(Yii::t('ctx', 'orderNotExist'), 400);
  565. }
  566. // $stockist = User::find()
  567. // ->select('ID')
  568. // ->where(['COUNTRY_ID' => $order['COUNTRY_ID'], 'IS_DEC' => 1])
  569. // ->orderBy(['USER_NAME' => SORT_ASC])
  570. // ->asArray()
  571. // ->one();
  572. // if (!$stockist || $order['USER_ID'] != $stockist['ID']) {
  573. // return static::notice(Yii::t('ctx', 'orderNotExist'), 400);
  574. // }
  575. $goods = OrderGoods::find()
  576. ->alias('OG')
  577. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  578. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  579. ->where(['OG.ORDER_SN' => $orderSn])
  580. ->asArray()
  581. ->all();
  582. $payType = $order['PAY_TYPE'];
  583. $order['ORDER_TYPE'] = $order['ORDER_TYPE'] == 'ZC' ? 'Welcome Pack' : (in_array($payType, ['cash', 'pay_stack']) ? 'Repeat Purchase' : 'Points');
  584. $order['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, null, 'label')[$order['PAY_TYPE']]['name'] ?? '';
  585. $order['STATUS'] = Yii::$app->params['orderStatus'][$order['STATUS']]['label'] ?? '';
  586. $order['FULL_ADDRESS'] = trim(implode(', ', [$order['ADDRESS'], $order['CITY_NAME'], $order['LGA_NAME'], Region::getCnName($order['PROVINCE'])]), ', ');
  587. $order['SHOW_BV'] = !in_array($payType, [5, 6]);
  588. $order['COUNTRY'] = Countries::getById($order['COUNTRY_ID'])['NAME'] ?? '';
  589. return static::notice(['order' => $order, 'goods' => $goods]);
  590. }
  591. /**
  592. * 我的订单.(自己复消+自己加入时的首单)
  593. * 创建人是自己+USER_ID是自己的复消单、创建人不是自己+USER_ID是自己的报单
  594. * @throws HttpException
  595. */
  596. public function actionOwnOrder() {
  597. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  598. $condition = " O.USER_ID=:USER_ID AND O.USER_NAME='{$uname}' AND O.IS_DELETE = 0";
  599. $params[':USER_ID'] = \Yii::$app->user->id;
  600. $orderQuery = Order::find()
  601. ->alias('O')
  602. ->where($condition, $params)
  603. ->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')
  604. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  605. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  606. // 订单中间表只查询待支付和支付失败的订单
  607. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  608. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  609. $orderStandardQuery = ApproachOrder::find()
  610. ->alias('O')
  611. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  612. ->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')
  613. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  614. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  615. $queryAll = $orderQuery->union($orderStandardQuery, true);
  616. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  617. $totalCount = $query->count();
  618. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  619. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  620. $data = [
  621. 'list' => $lists,
  622. 'currentPage'=>$pagination->page,
  623. 'totalPages'=>$pagination->pageCount,
  624. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  625. 'totalCount' => $pagination->totalCount,
  626. 'pageSize' => $pagination->pageSize,
  627. ];
  628. foreach ($data['list'] as $key => $value) {
  629. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  630. // 订单中间表
  631. $orderGoods = ApproachOrderGoods::find()
  632. ->alias('OG')
  633. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  634. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  635. ->where(['OG.ORDER_SN' => $value['SN']])
  636. ->asArray()
  637. ->all();
  638. } else {
  639. // 订单表
  640. $orderGoods = OrderGoods::find()
  641. ->alias('OG')
  642. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  643. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  644. ->where(['OG.ORDER_SN' => $value['SN']])
  645. ->asArray()
  646. ->all();
  647. }
  648. $data['list'][$key]['hasChildren'] = $orderGoods;
  649. $data['list'][$key]['ORDER_TYPE'] = $value['ORDER_TYPE']=='ZC' ? 'Welcome Pack' : (in_array($value['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points');
  650. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  651. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  652. $data['list'][$key]['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  653. $data['list'][$key]['SHOW_BV'] = !in_array($value['PAY_TYPE'], [5, 6]);
  654. $data['list'][$key]['COUNTRY'] = Countries::getById($value['COUNTRY_ID'])['NAME'];
  655. $data['list'][$key]['CURRENCY'] = Currency::getById($value['CURRENCY_ID'])['CODE'];
  656. unset($data['list'][$key]['ADDRESS'], $data['list'][$key]['CITY_NAME'], $data['list'][$key]['LGA_NAME'], $data['list'][$key]['PROVINCE']);
  657. }
  658. return static::notice($data);
  659. }
  660. /**
  661. * 会员订单.(给别人复消、给别人报单、给别人升级)
  662. * 给别人复消:创建人是自己、USER_ID不是自己、ORDER_TYPE=FX
  663. * 给别人报单:创建人是自己、USER_ID是自己、ORDER_TYPE=ZC、收货人不是自己
  664. * 给别人升级:创建人是自己、USER_ID不是自己、ORDER_TYPE=ZC
  665. */
  666. public function actionMemberOrder() {
  667. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  668. $condition = " O.USER_ID<>:USER_ID AND O.USER_NAME<>'{$uname}' AND O.CREATE_USER='{$uname}' AND O.IS_DELETE = 0";
  669. $params[':USER_ID'] = \Yii::$app->user->id;
  670. $orderQuery = Order::find()
  671. ->alias('O')
  672. ->where($condition, $params)
  673. ->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')
  674. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  675. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  676. // 订单中间表只查询待支付和支付失败的订单
  677. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  678. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  679. $orderStandardQuery = ApproachOrder::find()
  680. ->alias('O')
  681. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  682. ->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')
  683. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  684. ->join('LEFT JOIN', Currency::tableName() . ' AS C', 'C.ID=O.CURRENCY_ID');
  685. $queryAll = $orderQuery->union($orderStandardQuery, true);
  686. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  687. $totalCount = $query->count();
  688. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  689. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  690. $data = [
  691. 'list' => $lists,
  692. 'currentPage'=>$pagination->page,
  693. 'totalPages'=>$pagination->pageCount,
  694. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  695. 'totalCount' => $pagination->totalCount,
  696. 'pageSize' => $pagination->pageSize,
  697. ];
  698. foreach ($data['list'] as $key => $value) {
  699. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  700. // 订单中间表
  701. $orderGoods = ApproachOrderGoods::find()
  702. ->alias('OG')
  703. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  704. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  705. ->where(['OG.ORDER_SN' => $value['SN']])
  706. ->asArray()
  707. ->all();
  708. } else {
  709. // 订单表
  710. $orderGoods = OrderGoods::find()
  711. ->alias('OG')
  712. ->select(['OG.SKU_CODE', 'OG.GOODS_TITLE', 'OG.BUY_NUMS', 'OG.TAX_RATE', 'OG.REAL_PV', 'OG.REAL_PRICE', 'SG.COVER'])
  713. ->leftJoin(ShopGoods::tableName() . ' SG', 'SG.ID = OG.GOODS_ID')
  714. ->where(['OG.ORDER_SN' => $value['SN']])
  715. ->asArray()
  716. ->all();
  717. }
  718. $data['list'][$key]['hasChildren'] = $orderGoods;
  719. $data['list'][$key]['ORDER_TYPE'] = $value['ORDER_TYPE']=='ZC' ? 'Welcome Pack' : (in_array($value['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points');
  720. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::SALE_TYPE, NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  721. $data['list'][$key]['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  722. $data['list'][$key]['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  723. $data['list'][$key]['SHOW_BV'] = !in_array($value['PAY_TYPE'], [5, 6]);
  724. $data['list'][$key]['COUNTRY'] = Countries::getById($value['COUNTRY_ID'])['NAME'];
  725. $data['list'][$key]['CURRENCY'] = Currency::getById($value['CURRENCY_ID'])['CODE'];
  726. unset($data['list'][$key]['ADDRESS'], $data['list'][$key]['CITY_NAME'], $data['list'][$key]['LGA_NAME'], $data['list'][$key]['PROVINCE']);
  727. }
  728. return static::notice($data);
  729. }
  730. /**
  731. * 会员复消
  732. */
  733. public function actionReconsume() {
  734. // 会员信息
  735. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  736. if (!$user) {
  737. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  738. }
  739. $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';
  740. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  741. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  742. 'from' => ShopGoods::tableName() . ' AS S',
  743. 'join' => [
  744. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  745. ],
  746. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  747. 'limit' => 100,
  748. ]);
  749. $currencies = Currency::getAllData();
  750. $currencies = array_column($currencies, NULL, 'ID');
  751. foreach ($data['list'] as $key => $value) {
  752. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  753. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  754. }
  755. return static::notice($data);
  756. }
  757. /**
  758. * 根据国家获取最小编号的报单中心账号(无需登录)
  759. */
  760. public function actionNoLoginReconsume() {
  761. $countryId = Yii::$app->request->get('countryId');
  762. if (!filter_var($countryId, FILTER_VALIDATE_INT) || $countryId <= 0) {
  763. return static::notice('countryId is required', 400);
  764. }
  765. $user_one = User::find()
  766. ->select('ID,USER_NAME')
  767. ->where(['COUNTRY_ID' => $countryId, 'IS_DEC' => 1])
  768. ->orderBy(['USER_NAME' => SORT_ASC])
  769. ->asArray()
  770. ->one();
  771. if (!$user_one) {
  772. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  773. }
  774. // var_dump($user_one['ID']);die();
  775. // 会员信息
  776. $user = User::findOneAsArray('ID=:ID', [':ID' => $user_one['ID']]);
  777. if (!$user) {
  778. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  779. }
  780. // $condition = ' AND S.STATUS=1 AND S.CATEGORY_TYPE=1 AND FIND_IN_SET(1,S.GIFT_TYPE)>0 AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID';
  781. // $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  782. // 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  783. // 'from' => ShopGoods::tableName() . ' AS S',
  784. // 'join' => [
  785. // ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  786. // ],
  787. // 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  788. // 'limit' => 100,
  789. // ]);
  790. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0 OR FIND_IN_SET(4,GIFT_TYPE)>0) AND S.INSTALMENT=0 AND N.COUNTRY_ID=:COUNTRY_ID';
  791. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  792. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  793. 'from' => ShopGoods::tableName() . ' AS S',
  794. 'join' => [
  795. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  796. ],
  797. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  798. 'limit' => 100,
  799. ]);
  800. $currencies = Currency::getAllData();
  801. $currencies = array_column($currencies, NULL, 'ID');
  802. foreach ($data['list'] as $key => $value) {
  803. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  804. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  805. }
  806. return static::notice($data);
  807. }
  808. /**
  809. * 帮会员复消购物车
  810. * @throws HttpException
  811. */
  812. public function actionReconsumeCart(){
  813. // 收货会员
  814. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  815. if (!$user) {
  816. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  817. }
  818. // 汇率
  819. $afterCountry = Countries::getById($user['COUNTRY_ID']);
  820. $afterCurrency = CurrencyConversions::getToUSDRate($afterCountry['LOCAL_CURRENCY_ID']);
  821. // 登录会员
  822. $userId = \Yii::$app->user->id;
  823. $userData = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  824. // 会员不能给自己复销
  825. if (\Yii::$app->request->get('userId') == $userId) {
  826. return static::notice(Yii::t('app', 'memberCanNotPayoutOfSelf'), 400);
  827. }
  828. // 汇率
  829. $beforeCountry = Countries::getById($userData['COUNTRY_ID']);
  830. $beforeCurrency = CurrencyConversions::getToUSDRate($beforeCountry['LOCAL_CURRENCY_ID']);
  831. $payList = ['cash'=>['name'=>'Account Balance'],];//余额支付
  832. $userBalance = [
  833. 'points' => 0,
  834. 'cash' => 0
  835. ];
  836. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  837. $userBalance['points'] = Tool::convertAmount($userBonusResult['RECONSUME_POINTS'], $beforeCurrency, $afterCurrency);
  838. }
  839. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  840. $userBalance['cash'] = Tool::convertAmount($userCashResult['CASH'], $beforeCurrency, $afterCurrency);
  841. }
  842. return static::notice(['payList'=>$payList,'userBalance'=>$userBalance]);
  843. }
  844. /**
  845. * 帮会员复消确认订单
  846. */
  847. public function actionReconsumeSureOrder(){
  848. if (\Yii::$app->request->isPost) {
  849. $formModel = new OrderForm();
  850. $formModel->scenario = 'reconsumeOrder';
  851. $formModel->remark = Yii::t('app', 'reconsume');
  852. $post = \Yii::$app->request->post();
  853. $post['type'] = DeclarationForm::TYPE_FX;
  854. $stockist = $post['decUserName'] ?? '';
  855. if (!$stockist) {
  856. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  857. }
  858. if ($formModel->load($post, '') && $formModel->reconsumeAdd()) {
  859. return static::notice(Yii::t('app', 'reconsumeSuccessfully'));
  860. } else {
  861. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  862. }
  863. }
  864. }
  865. /**
  866. * 无需登录的复消确认订单
  867. */
  868. public function actionNoLoginReconsumeSureOrder(){
  869. $post = Yii::$app->request->post();
  870. $countryId = $post['countryId'] ?? null;
  871. if (!filter_var($countryId, FILTER_VALIDATE_INT) || $countryId <= 0) {
  872. return static::notice('countryId is required', 400);
  873. }
  874. $user = User::find()
  875. ->select('ID,USER_NAME')
  876. ->where(['COUNTRY_ID' => $countryId, 'IS_DEC' => 1])
  877. ->orderBy(['USER_NAME' => SORT_ASC])
  878. ->asArray()
  879. ->one();
  880. if (!$user) {
  881. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  882. }
  883. $formModel = new ApproachReconsumeOrderForm();
  884. $formModel->scenario = 'noLoginReconsumeOrder';
  885. $formModel->remark = Yii::t('app', 'reconsume');
  886. $formModel->noLoginUserId = $user['ID'];
  887. $post['type'] = DeclarationForm::TYPE_FX;
  888. $post['payType'] = 'pay_stack';
  889. if ($formModel->load($post, '')) {
  890. $formModel->userName = $user['USER_NAME'];
  891. $formModel->decUserName = $user['USER_NAME'];
  892. if ($order = $formModel->add()) {
  893. return static::notice($order);
  894. }
  895. }
  896. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  897. }
  898. /**
  899. * 商品列表tabs分类
  900. * @return mixed
  901. * @throws HttpException
  902. */
  903. public function actionGoodsActive()
  904. {
  905. $data = [
  906. // [
  907. // 'name' => 'bonus',
  908. // 'label' => '奖金商品',
  909. // ],
  910. [
  911. 'name' => '1',
  912. 'label' => Yii::t('app', 'standardProducts'),
  913. ],
  914. // [
  915. // 'name' => '4',
  916. // 'label' => Yii::t('app', 'travelFundProducts'),
  917. // ],
  918. [
  919. 'name' => '5',
  920. 'label' => Yii::t('app', 'carFundProducts'),
  921. ],
  922. [
  923. 'name' => '6',
  924. 'label' => Yii::t('app', 'villaFundProducts'),
  925. ],
  926. ];
  927. return static::notice($data);
  928. }
  929. /**
  930. * 导出订单.
  931. * @return mixed
  932. * @throws HttpException
  933. * @throws \Mpdf\MpdfException
  934. */
  935. public function actionOrderExport()
  936. {
  937. $orderSn = \Yii::$app->request->get('orderSn');
  938. $condition = " AND SN=:SN AND O.IS_DELETE = 0";
  939. $params = [
  940. ':SN' => $orderSn,
  941. ];
  942. $data = Order::lists($condition, $params, [
  943. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  944. 'orderBy' => 'O.CREATED_AT DESC',
  945. 'from' => Order::tableName() . ' AS O',
  946. 'join' => [
  947. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  948. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  949. ],
  950. ]);
  951. if (!$data['list']) {
  952. $condition .= ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)';
  953. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  954. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  955. $data = ApproachOrder::lists($condition, $params, [
  956. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  957. 'orderBy' => 'O.CREATED_AT DESC',
  958. 'from' => ApproachOrder::tableName() . ' AS O',
  959. 'join' => [
  960. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  961. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  962. ],
  963. ]);
  964. }
  965. $userId = '';
  966. $userName = '';
  967. $address = '';
  968. $mobile = '';
  969. $orderAt = '';
  970. $orderDetails = '';
  971. $orderAmount = 0; // 合计总额
  972. $orderNums = 0; // 合计总数
  973. $totalTaxAmount = 0; // 合计税额
  974. $totalAmount = 0;
  975. foreach ($data['list'] as $key => $value) {
  976. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  977. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  978. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  979. $userId = $value['USER_NAME'];
  980. $userName = $value['REAL_NAME'];
  981. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  982. $mobile = $value['MOBILE'];
  983. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  984. // 总价
  985. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  986. $orderAmount += $totalAmount;
  987. $orderNums += $value['BUY_NUMS'];
  988. // 税额
  989. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  990. $totalTaxAmount += $taxAmount;
  991. $productAmount = Tool::formatAmount($value['REAL_PRICE']);
  992. $taxAmount = Tool::formatAmount($taxAmount);
  993. $totalAmount = Tool::formatAmount($totalAmount);
  994. // 订单详情
  995. $orderDetails .= <<<EOT
  996. <tr>
  997. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  998. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  999. <td style="text-align: right;">{$productAmount}</td>
  1000. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1001. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1002. <td style="text-align: right;">{$taxAmount}</td>
  1003. <td style="text-align: right;">{$totalAmount}</td>
  1004. </tr>
  1005. EOT;
  1006. }
  1007. $memberCode = Yii::t('app', 'memberCode');
  1008. $memberName = Yii::t('app', 'memberName');
  1009. $memberAddress = Yii::t('app', 'memberAddress');
  1010. $memberPhone = Yii::t('app', 'memberPhone');
  1011. $orderCode = Yii::t('app', 'orderCode');
  1012. $orderDetail = Yii::t('app', 'orderDetail');
  1013. $productCode = Yii::t('app', 'productCode');
  1014. $productName = Yii::t('app', 'productName');
  1015. $productPrice = Yii::t('app', 'productPrice');
  1016. $quantity = Yii::t('app', 'qty');
  1017. $taxRate = Yii::t('app', 'taxRate');
  1018. $totalTax = Yii::t('app', 'totalTax');
  1019. $totalAmount = Yii::t('app', 'totalAmount');
  1020. $total = Yii::t('app', 'total');
  1021. $signature = Yii::t('app', 'signature');
  1022. $date = Yii::t('app', 'date');
  1023. $createAt = Yii::t('app', 'createAt');
  1024. // 订单基本信息
  1025. $orderBase = <<<ORDER
  1026. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1027. <tr>
  1028. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1029. <td width="70%">{$userId}</td>
  1030. </tr>
  1031. <tr>
  1032. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1033. <td width="70%">{$userName}</td>
  1034. </tr>
  1035. <tr>
  1036. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1037. <td width="70%">{$address}</td>
  1038. </tr>
  1039. <tr>
  1040. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1041. <td width="70%">{$mobile}</td>
  1042. </tr>
  1043. <tr>
  1044. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1045. <td width="70%">{$orderSn}</td>
  1046. </tr>
  1047. <tr>
  1048. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1049. <td width="70%">{$orderAt}</td>
  1050. </tr>
  1051. <tr>
  1052. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1053. <td class="bg"></td>
  1054. </tr>
  1055. </table>
  1056. ORDER;
  1057. $l['a_meta_charset'] = 'UTF-8';
  1058. $l['a_meta_dir'] = 'ltr';
  1059. $l['a_meta_language'] = 'zh';
  1060. $l['w_page'] = '页面';
  1061. $orderAmount = Tool::formatAmount($orderAmount);
  1062. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1063. $context = <<<ORDER
  1064. <!doctype html>
  1065. <html lang="en">
  1066. <head>
  1067. <meta charset="UTF-8" />
  1068. <title>{$orderDetail}</title>
  1069. <style>
  1070. table {
  1071. border-collapse: collapse;
  1072. }
  1073. table td, table th {
  1074. border: 1px solid #ccc;
  1075. /*padding: 5px 5px;*/
  1076. border-collapse: collapse;
  1077. }
  1078. /*td {*/
  1079. /* padding: 120px;*/
  1080. /*}*/
  1081. .bg {
  1082. background-color: #ccc;
  1083. }
  1084. </style>
  1085. </head>
  1086. <body>
  1087. <div class="content">
  1088. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1089. <div>
  1090. <div style="display: block; width: 100%;">
  1091. {$orderBase}
  1092. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1093. <tr>
  1094. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1095. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1096. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1097. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1098. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1099. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1100. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1101. </tr>
  1102. {$orderDetails}
  1103. <tr>
  1104. <td colspan="3">{$total}</td>
  1105. <td style="text-align: right;">{$orderNums}</td>
  1106. <td></td>
  1107. <td style="text-align: right;">{$totalTaxAmount}</td>
  1108. <td style="text-align: right;">{$orderAmount}</td>
  1109. </tr>
  1110. </table>
  1111. </div>
  1112. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1113. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1114. <tr style="border: none;">
  1115. <td width="70%" style="border: none;"></td>
  1116. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1117. </tr>
  1118. <tr style="border: none;">
  1119. <td width="70%" style="border: none;"></td>
  1120. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1121. </tr>
  1122. </table>
  1123. </div>
  1124. </div>
  1125. </div>
  1126. </body>
  1127. </html>
  1128. ORDER;
  1129. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1130. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1131. // 设置打印模式
  1132. $pdf->SetCreator(PDF_CREATOR);
  1133. $pdf->SetAuthor('DaZe');
  1134. $pdf->SetTitle($orderSn);
  1135. $pdf->SetSubject('TCPDF Tutorial');
  1136. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1137. // 是否显示页眉
  1138. $pdf->setPrintHeader(false);
  1139. // 设置页眉字体
  1140. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1141. // 页眉距离顶部的距离
  1142. $pdf->SetHeaderMargin('5');
  1143. // 是否显示页脚
  1144. $pdf->setPrintFooter(false);
  1145. // 设置默认等宽字体
  1146. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1147. // 设置行高
  1148. $pdf->setCellHeightRatio(1);
  1149. // 设置左、上、右的间距
  1150. $pdf->SetMargins('10', '0', '10');
  1151. // 设置是否自动分页 距离底部多少距离时分页
  1152. $pdf->SetAutoPageBreak(TRUE, '15');
  1153. // 设置图像比例因子
  1154. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1155. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1156. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1157. $pdf->setLanguageArray($l);
  1158. }
  1159. $pdf->setFontSubsetting(true);
  1160. $pdf->AddPage();
  1161. // 设置字体
  1162. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1163. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1164. // if (!file_exists($image_file)) {
  1165. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1166. // }
  1167. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1168. $image = file_get_contents($image_file);
  1169. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1170. $pdf->writeHTML($context);
  1171. ob_clean();
  1172. $file_name = $orderSn . '.pdf';
  1173. $path = 'pdfs/' . $file_name;
  1174. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1175. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1176. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1177. }
  1178. /**
  1179. * 无需登录导出订单.
  1180. * @return mixed
  1181. * @throws HttpException
  1182. * @throws \Mpdf\MpdfException
  1183. */
  1184. public function actionNoLoginOrderExport()
  1185. {
  1186. $orderSn = \Yii::$app->request->get('orderSn');
  1187. $condition = " AND SN=:SN AND O.IS_DELETE = 0";
  1188. $params = [
  1189. ':SN' => $orderSn,
  1190. ];
  1191. $data = Order::lists($condition, $params, [
  1192. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  1193. 'orderBy' => 'O.CREATED_AT DESC',
  1194. 'from' => Order::tableName() . ' AS O',
  1195. 'join' => [
  1196. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  1197. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  1198. ],
  1199. ]);
  1200. if (!$data['list']) {
  1201. $condition .= ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)';
  1202. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1203. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1204. $data = ApproachOrder::lists($condition, $params, [
  1205. 'select' => 'O.*,U.REAL_NAME,OG.*,OG.CATEGORY_TYPE',
  1206. 'orderBy' => 'O.CREATED_AT DESC',
  1207. 'from' => ApproachOrder::tableName() . ' AS O',
  1208. 'join' => [
  1209. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  1210. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN'],
  1211. ],
  1212. ]);
  1213. }
  1214. $userId = '';
  1215. $userName = '';
  1216. $address = '';
  1217. $mobile = '';
  1218. $orderAt = '';
  1219. $orderDetails = '';
  1220. $orderAmount = 0; // 合计总额
  1221. $orderNums = 0; // 合计总数
  1222. $totalTaxAmount = 0; // 合计税额
  1223. $totalAmount = 0;
  1224. foreach ($data['list'] as $key => $value) {
  1225. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1226. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1227. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1228. $userId = $value['USER_NAME'];
  1229. $userName = $value['CONSIGNEE'];
  1230. $address = $provinceName .' '. $cityName .' '. $countyName .' '. $value['ADDRESS'];
  1231. $mobile = $value['MOBILE'];
  1232. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1233. // 总价
  1234. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1235. $orderAmount += $totalAmount;
  1236. $orderNums += $value['BUY_NUMS'];
  1237. // 税额
  1238. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1239. $totalTaxAmount += $taxAmount;
  1240. $productAmount = Tool::formatAmount($value['REAL_PRICE']);
  1241. $taxAmount = Tool::formatAmount($taxAmount);
  1242. $totalAmount = Tool::formatAmount($totalAmount);
  1243. // 订单详情
  1244. $orderDetails .= <<<EOT
  1245. <tr>
  1246. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1247. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1248. <td style="text-align: right;">{$productAmount}</td>
  1249. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1250. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1251. <td style="text-align: right;">{$taxAmount}</td>
  1252. <td style="text-align: right;">{$totalAmount}</td>
  1253. </tr>
  1254. EOT;
  1255. }
  1256. $memberCode = Yii::t('app', 'memberCode');
  1257. $memberName = Yii::t('app', 'memberName');
  1258. $memberAddress = Yii::t('app', 'memberAddress');
  1259. $memberPhone = Yii::t('app', 'memberPhone');
  1260. $orderCode = Yii::t('app', 'orderCode');
  1261. $orderDetail = Yii::t('app', 'orderDetail');
  1262. $productCode = Yii::t('app', 'productCode');
  1263. $productName = Yii::t('app', 'productName');
  1264. $productPrice = Yii::t('app', 'productPrice');
  1265. $quantity = Yii::t('app', 'qty');
  1266. $taxRate = Yii::t('app', 'taxRate');
  1267. $totalTax = Yii::t('app', 'totalTax');
  1268. $totalAmount = Yii::t('app', 'totalAmount');
  1269. $total = Yii::t('app', 'total');
  1270. $signature = Yii::t('app', 'signature');
  1271. $date = Yii::t('app', 'date');
  1272. $createAt = Yii::t('app', 'createAt');
  1273. // 订单基本信息
  1274. $orderBase = <<<ORDER
  1275. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1276. <tr>
  1277. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1278. <td width="70%"></td>
  1279. </tr>
  1280. <tr>
  1281. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1282. <td width="70%">{$userName}</td>
  1283. </tr>
  1284. <tr>
  1285. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1286. <td width="70%">{$address}</td>
  1287. </tr>
  1288. <tr>
  1289. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1290. <td width="70%">{$mobile}</td>
  1291. </tr>
  1292. <tr>
  1293. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1294. <td width="70%">{$orderSn}</td>
  1295. </tr>
  1296. <tr>
  1297. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1298. <td width="70%">{$orderAt}</td>
  1299. </tr>
  1300. <tr>
  1301. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1302. <td class="bg"></td>
  1303. </tr>
  1304. </table>
  1305. ORDER;
  1306. $l['a_meta_charset'] = 'UTF-8';
  1307. $l['a_meta_dir'] = 'ltr';
  1308. $l['a_meta_language'] = 'zh';
  1309. $l['w_page'] = '页面';
  1310. $orderAmount = Tool::formatAmount($orderAmount);
  1311. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1312. $context = <<<ORDER
  1313. <!doctype html>
  1314. <html lang="en">
  1315. <head>
  1316. <meta charset="UTF-8" />
  1317. <title>{$orderDetail}</title>
  1318. <style>
  1319. table {
  1320. border-collapse: collapse;
  1321. }
  1322. table td, table th {
  1323. border: 1px solid #ccc;
  1324. /*padding: 5px 5px;*/
  1325. border-collapse: collapse;
  1326. }
  1327. /*td {*/
  1328. /* padding: 120px;*/
  1329. /*}*/
  1330. .bg {
  1331. background-color: #ccc;
  1332. }
  1333. </style>
  1334. </head>
  1335. <body>
  1336. <div class="content">
  1337. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1338. <div>
  1339. <div style="display: block; width: 100%;">
  1340. {$orderBase}
  1341. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1342. <tr>
  1343. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1344. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1345. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1346. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1347. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1348. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1349. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1350. </tr>
  1351. {$orderDetails}
  1352. <tr>
  1353. <td colspan="3">{$total}</td>
  1354. <td style="text-align: right;">{$orderNums}</td>
  1355. <td></td>
  1356. <td style="text-align: right;">{$totalTaxAmount}</td>
  1357. <td style="text-align: right;">{$orderAmount}</td>
  1358. </tr>
  1359. </table>
  1360. </div>
  1361. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1362. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1363. <tr style="border: none;">
  1364. <td width="70%" style="border: none;"></td>
  1365. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1366. </tr>
  1367. <tr style="border: none;">
  1368. <td width="70%" style="border: none;"></td>
  1369. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1370. </tr>
  1371. </table>
  1372. </div>
  1373. </div>
  1374. </div>
  1375. </body>
  1376. </html>
  1377. ORDER;
  1378. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1379. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1380. // 设置打印模式
  1381. $pdf->SetCreator(PDF_CREATOR);
  1382. $pdf->SetAuthor('DaZe');
  1383. $pdf->SetTitle($orderSn);
  1384. $pdf->SetSubject('TCPDF Tutorial');
  1385. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1386. // 是否显示页眉
  1387. $pdf->setPrintHeader(false);
  1388. // 设置页眉字体
  1389. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1390. // 页眉距离顶部的距离
  1391. $pdf->SetHeaderMargin('5');
  1392. // 是否显示页脚
  1393. $pdf->setPrintFooter(false);
  1394. // 设置默认等宽字体
  1395. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1396. // 设置行高
  1397. $pdf->setCellHeightRatio(1);
  1398. // 设置左、上、右的间距
  1399. $pdf->SetMargins('10', '0', '10');
  1400. // 设置是否自动分页 距离底部多少距离时分页
  1401. $pdf->SetAutoPageBreak(TRUE, '15');
  1402. // 设置图像比例因子
  1403. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1404. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1405. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1406. $pdf->setLanguageArray($l);
  1407. }
  1408. $pdf->setFontSubsetting(true);
  1409. $pdf->AddPage();
  1410. // 设置字体
  1411. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1412. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1413. // if (!file_exists($image_file)) {
  1414. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1415. // }
  1416. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1417. $image = file_get_contents($image_file);
  1418. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1419. $pdf->writeHTML($context);
  1420. ob_clean();
  1421. $file_name = $orderSn . '.pdf';
  1422. $path = 'pdfs/' . $file_name;
  1423. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1424. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1425. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1426. }
  1427. /**
  1428. * 导出订单.
  1429. * @return mixed
  1430. * @throws HttpException
  1431. */
  1432. public function actionDecOrderExport()
  1433. {
  1434. $orderSn = \Yii::$app->request->get('orderSn');
  1435. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1436. $params = [
  1437. ':USER_ID' => \Yii::$app->user->id,
  1438. ':ORDER_SN' => $orderSn,
  1439. ];
  1440. $data = DecOrder::lists($condition, $params, [
  1441. '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',
  1442. 'orderBy' => 'DO.CREATED_AT DESC',
  1443. 'from' => DecOrder::tableName() . ' AS DO',
  1444. 'join' => [
  1445. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1446. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1447. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1448. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1449. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1450. ],
  1451. ]);
  1452. if (!$data['list']) {
  1453. $data = ApproachDecOrder::lists($condition, $params, [
  1454. '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',
  1455. 'orderBy' => 'DO.CREATED_AT DESC',
  1456. 'from' => ApproachDecOrder::tableName() . ' AS DO',
  1457. 'join' => [
  1458. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1459. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1460. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1461. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1462. ['LEFT JOIN', ApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1463. ],
  1464. ]);
  1465. }
  1466. $userId = '';
  1467. $userName = '';
  1468. $address = '';
  1469. $mobile = '';
  1470. $orderAt = '';
  1471. $orderDetails = '';
  1472. $orderAmount = 0; // 合计总额
  1473. $orderNums = 0; // 合计总数
  1474. $totalTaxAmount = 0; // 合计税额
  1475. $totalAmount = 0;
  1476. foreach ($data['list'] as $key => $value) {
  1477. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1478. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1479. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1480. $userId = $value['USER_NAME'];
  1481. $userName = $value['REAL_NAME'];
  1482. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1483. $mobile = $value['MOBILE'];
  1484. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1485. // 总价
  1486. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1487. $orderAmount += $totalAmount;
  1488. $orderNums += $value['BUY_NUMS'];
  1489. // 税额
  1490. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1491. $totalTaxAmount += $taxAmount;
  1492. $taxAmount = sprintf('%.2f', $taxAmount);
  1493. $totalAmount = sprintf('%.2f', $totalAmount);
  1494. // 订单详情
  1495. $orderDetails .= <<<EOT
  1496. <tr>
  1497. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1498. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1499. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1500. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1501. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1502. <td style="text-align: right;">{$taxAmount}</td>
  1503. <td style="text-align: right;">{$totalAmount}</td>
  1504. </tr>
  1505. EOT;
  1506. }
  1507. $memberCode = Yii::t('app', 'memberCode');
  1508. $memberName = Yii::t('app', 'memberName');
  1509. $memberAddress = Yii::t('app', 'memberAddress');
  1510. $memberPhone = Yii::t('app', 'memberPhone');
  1511. $orderCode = Yii::t('app', 'orderCode');
  1512. $orderDetail = Yii::t('app', 'orderDetail');
  1513. $productCode = Yii::t('app', 'productCode');
  1514. $productName = Yii::t('app', 'productName');
  1515. $productPrice = Yii::t('app', 'productPrice');
  1516. $quantity = Yii::t('app', 'qty');
  1517. $taxRate = Yii::t('app', 'taxRate');
  1518. $totalTax = Yii::t('app', 'totalTax');
  1519. $totalAmount = Yii::t('app', 'totalAmount');
  1520. $total = Yii::t('app', 'total');
  1521. $signature = Yii::t('app', 'signature');
  1522. $date = Yii::t('app', 'date');
  1523. $createAt = Yii::t('app', 'createAt');
  1524. // 订单基本信息
  1525. $orderBase = <<<ORDER
  1526. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1527. <tr>
  1528. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1529. <td width="70%">{$userId}</td>
  1530. </tr>
  1531. <tr>
  1532. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1533. <td width="70%">{$userName}</td>
  1534. </tr>
  1535. <tr>
  1536. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1537. <td width="70%">{$address}</td>
  1538. </tr>
  1539. <tr>
  1540. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1541. <td width="70%">{$mobile}</td>
  1542. </tr>
  1543. <tr>
  1544. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1545. <td width="70%">{$orderSn}</td>
  1546. </tr>
  1547. <tr>
  1548. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1549. <td width="70%">{$orderAt}</td>
  1550. </tr>
  1551. <tr>
  1552. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1553. <td class="bg"></td>
  1554. </tr>
  1555. </table>
  1556. ORDER;
  1557. $l['a_meta_charset'] = 'UTF-8';
  1558. $l['a_meta_dir'] = 'ltr';
  1559. $l['a_meta_language'] = 'zh';
  1560. $l['w_page'] = '页面';
  1561. $orderAmount = sprintf('%.2f', $orderAmount);
  1562. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1563. $context = <<<ORDER
  1564. <!doctype html>
  1565. <html lang="en">
  1566. <head>
  1567. <meta charset="UTF-8" />
  1568. <title>{$orderDetail}</title>
  1569. <style>
  1570. table {
  1571. border-collapse: collapse;
  1572. }
  1573. table td, table th {
  1574. border: 1px solid #ccc;
  1575. /*padding: 5px 5px;*/
  1576. border-collapse: collapse;
  1577. }
  1578. /*td {*/
  1579. /* padding: 120px;*/
  1580. /*}*/
  1581. .bg {
  1582. background-color: #ccc;
  1583. }
  1584. </style>
  1585. </head>
  1586. <body>
  1587. <div class="content">
  1588. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1589. <div>
  1590. <div style="display: block; width: 100%;">
  1591. {$orderBase}
  1592. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1593. <tr>
  1594. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1595. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1596. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1597. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1598. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1599. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1600. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1601. </tr>
  1602. {$orderDetails}
  1603. <tr>
  1604. <td colspan="3">{$total}</td>
  1605. <td style="text-align: right;">{$orderNums}</td>
  1606. <td></td>
  1607. <td style="text-align: right;">{$totalTaxAmount}</td>
  1608. <td style="text-align: right;">{$orderAmount}</td>
  1609. </tr>
  1610. </table>
  1611. </div>
  1612. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1613. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1614. <tr style="border: none;">
  1615. <td width="70%" style="border: none;"></td>
  1616. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1617. </tr>
  1618. <tr style="border: none;">
  1619. <td width="70%" style="border: none;"></td>
  1620. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1621. </tr>
  1622. </table>
  1623. </div>
  1624. </div>
  1625. </div>
  1626. </body>
  1627. </html>
  1628. ORDER;
  1629. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1630. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1631. // 设置打印模式
  1632. $pdf->SetCreator(PDF_CREATOR);
  1633. $pdf->SetAuthor('DaZe');
  1634. $pdf->SetTitle($orderSn);
  1635. $pdf->SetSubject('TCPDF Tutorial');
  1636. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1637. // 是否显示页眉
  1638. $pdf->setPrintHeader(false);
  1639. // 设置页眉字体
  1640. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1641. // 页眉距离顶部的距离
  1642. $pdf->SetHeaderMargin('5');
  1643. // 是否显示页脚
  1644. $pdf->setPrintFooter(false);
  1645. // 设置默认等宽字体
  1646. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1647. // 设置行高
  1648. $pdf->setCellHeightRatio(1);
  1649. // 设置左、上、右的间距
  1650. $pdf->SetMargins('10', '0', '10');
  1651. // 设置是否自动分页 距离底部多少距离时分页
  1652. $pdf->SetAutoPageBreak(TRUE, '15');
  1653. // 设置图像比例因子
  1654. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1655. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1656. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1657. $pdf->setLanguageArray($l);
  1658. }
  1659. $pdf->setFontSubsetting(true);
  1660. $pdf->AddPage();
  1661. // 设置字体
  1662. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1663. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1664. // if (!file_exists($image_file)) {
  1665. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1666. // }
  1667. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1668. $image = file_get_contents($image_file);
  1669. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1670. $pdf->writeHTML($context);
  1671. ob_clean();
  1672. $file_name = $orderSn . '.pdf';
  1673. $path = 'pdfs/' . $file_name;
  1674. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1675. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1676. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1677. }
  1678. /**
  1679. * 下载申请退货单
  1680. */
  1681. public function actionOrderBackingOut()
  1682. {
  1683. $file_name = 'orderBackingOut.xlsx';
  1684. return static::notice(['fileUrl' => $file_name]);
  1685. }
  1686. /**
  1687. * BA商品列表
  1688. * @return mixed
  1689. * @throws HttpException
  1690. */
  1691. public function actionBaProductList() {
  1692. // 商品分类
  1693. $categoryType = \Yii::$app->request->get('categoryType', 1);
  1694. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  1695. $isStudio = BaUser::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  1696. if($isStudio==1){
  1697. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  1698. }
  1699. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  1700. $params[':CATEGORY_TYPE'] = intval($categoryType);
  1701. $data = ShopGoods::lists($condition, $params, [
  1702. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1703. 'from' => ShopGoods::tableName(),
  1704. ]);
  1705. foreach ($data['list'] as $key => $value) {
  1706. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  1707. $data['list'][$key]['DISCOUNT'] = ShopGoods::getGoodType()[$value['TYPE']]['discount'];
  1708. } else {
  1709. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  1710. }
  1711. }
  1712. return static::notice($data);
  1713. }
  1714. /**
  1715. * BA购物车订单展示
  1716. * @throws HttpException
  1717. */
  1718. public function actionBaShoppingCart()
  1719. {
  1720. // 收货地址
  1721. $allAddress = BaReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1722. if($allAddress) {
  1723. foreach ($allAddress as $key => $row) {
  1724. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1725. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1726. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1727. }
  1728. }
  1729. // 运费.奈拉
  1730. $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
  1731. // 普通商品免运费阈值.奈拉
  1732. $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
  1733. return static::notice([
  1734. 'freight' => $freight,
  1735. 'freeShipping' => $freeShipping,
  1736. 'allAddress' => $allAddress,
  1737. ]);
  1738. }
  1739. /**
  1740. * 确认BA订单
  1741. * @throws HttpException|\yii\base\Exception
  1742. */
  1743. public function actionBaSureApproachOrder()
  1744. {
  1745. if (\Yii::$app->request->isPost) {
  1746. $formModel = new BaApproachOrderForm();
  1747. $formModel->scenario = 'userOrder';
  1748. $formModel->remark = Yii::t('app', 'reconsumeRemark');
  1749. $post = \Yii::$app->request->post();
  1750. $post['type'] = DeclarationForm::TYPE_FX;
  1751. if ($formModel->load($post, '') && $order = $formModel->add()) {
  1752. return static::notice($order);
  1753. } else {
  1754. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1755. }
  1756. }
  1757. }
  1758. /**
  1759. * 删除BA准订单
  1760. * @throws HttpException
  1761. */
  1762. public function actionBaDeleteApproachOrder()
  1763. {
  1764. $orderSn = \Yii::$app->request->post('orderSn');
  1765. // 删除订单中间表
  1766. BaApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  1767. // 删除订单商品中间表
  1768. BaApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  1769. return static::notice('');
  1770. }
  1771. /**
  1772. * BA订单
  1773. * @return mixed
  1774. * @throws HttpException
  1775. */
  1776. public function actionBaOrderList()
  1777. {
  1778. $uname = Info::getBaUserNameByUserId(\Yii::$app->user->id);
  1779. $condition = " (USER_ID=:USER_ID OR CREATE_USER='$uname') AND O.IS_DELETE = 0";
  1780. $params[':USER_ID'] = \Yii::$app->user->id;
  1781. $orderQuery = BaOrder::find()
  1782. ->alias('O')
  1783. ->where($condition, $params)
  1784. ->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')
  1785. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1786. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1787. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1788. ->orderBy('O.CREATED_AT DESC');
  1789. // 订单中间表只查询待支付和支付失败的订单
  1790. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1791. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1792. $orderStandardQuery = BaApproachOrder::find()
  1793. ->alias('O')
  1794. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1795. ->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')
  1796. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1797. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1798. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1799. ->orderBy('O.CREATED_AT DESC');
  1800. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1801. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1802. $totalCount = $query->count();
  1803. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1804. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1805. $data = [
  1806. 'list' => $lists,
  1807. 'currentPage'=>$pagination->page,
  1808. 'totalPages'=>$pagination->pageCount,
  1809. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1810. 'totalCount' => $pagination->totalCount,
  1811. 'pageSize' => $pagination->pageSize,
  1812. ];
  1813. foreach ($data['list'] as $key => $value) {
  1814. $data['list'][$key]['ORDER_TYPE'] = 'Reselling';
  1815. $data['list'][$key]['PAY_AT'] = $value['PAY_AT'] ? Date::convert($value['PAY_AT'],'Y-m-d H:i:s') : '';
  1816. $data['list'][$key]['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1817. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1818. $data['list'][$key]['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  1819. $data['list'][$key]['FULL_ADDRESS'] = $value['ADDRESS'];
  1820. $data['list'][$key]['TAX_AMOUNT'] = Tool::formatPrice(($value['REAL_PRICE'] - $value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100)) * $value['BUY_NUMS']);
  1821. $data['list'][$key]['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1822. if ($value['LGA_NAME']) {
  1823. $data['list'][$key]['FULL_ADDRESS'] .= ', ' . $value['LGA_NAME'];
  1824. }
  1825. }
  1826. return static::notice($data);
  1827. }
  1828. /**
  1829. * @throws HttpException
  1830. */
  1831. public function actionBaDecOrderList() {
  1832. $version = \Yii::$app->request->get('v');
  1833. $data = $version == 2 ? $this->baDecOrderListV2() : $this->baDecOrderListV1();
  1834. return static::notice($data);
  1835. }
  1836. /**
  1837. * 我的BA报单
  1838. * @return mixed
  1839. * @throws HttpException
  1840. */
  1841. public function baDecOrderListV1() {
  1842. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1843. $params[':USER_ID'] = \Yii::$app->user->id;
  1844. $orderQuery = BaDecOrder::find()
  1845. ->alias('DO')
  1846. ->where($condition, $params)
  1847. ->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')
  1848. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1849. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1850. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1851. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1852. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1853. ->orderBy('DO.CREATED_AT DESC');
  1854. // 订单中间表只查询待支付和支付失败的订单
  1855. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1856. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1857. $orderStandardQuery = BaApproachDecOrder::find()
  1858. ->alias('DO')
  1859. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1860. ->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')
  1861. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1862. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1863. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1864. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1865. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1866. ->orderBy('DO.CREATED_AT DESC');
  1867. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1868. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1869. $totalCount = $query->count();
  1870. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1871. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1872. $data = [
  1873. 'list' => $lists,
  1874. 'currentPage'=>$pagination->page,
  1875. 'totalPages'=>$pagination->pageCount,
  1876. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1877. 'totalCount' => $pagination->totalCount,
  1878. 'pageSize' => $pagination->pageSize,
  1879. ];
  1880. foreach ($data['list'] as &$value) {
  1881. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  1882. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1883. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1884. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  1885. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1886. }
  1887. return $data;
  1888. }
  1889. /**
  1890. * 我的BA报单
  1891. * @return mixed
  1892. * @throws HttpException
  1893. */
  1894. public function baDecOrderListV2() {
  1895. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1896. $params[':USER_ID'] = \Yii::$app->user->id;
  1897. $orderQuery = BaDecOrder::find()
  1898. ->alias('DO')
  1899. ->where($condition, $params)
  1900. ->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')
  1901. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1902. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1903. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1904. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1905. ->orderBy('DO.CREATED_AT DESC');
  1906. // 订单中间表只查询待支付和支付失败的订单
  1907. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1908. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1909. $orderStandardQuery = BaApproachDecOrder::find()
  1910. ->alias('DO')
  1911. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1912. ->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')
  1913. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1914. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1915. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1916. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1917. ->orderBy('DO.CREATED_AT DESC');
  1918. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1919. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1920. $totalCount = $query->count();
  1921. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1922. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1923. $data = [
  1924. 'list' => $lists,
  1925. 'currentPage'=>$pagination->page,
  1926. 'totalPages'=>$pagination->pageCount,
  1927. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1928. 'totalCount' => $pagination->totalCount,
  1929. 'pageSize' => $pagination->pageSize,
  1930. ];
  1931. foreach ($data['list'] as &$value) {
  1932. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  1933. // 订单中间表
  1934. $orderGoods = BaApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1935. } else {
  1936. // 订单表
  1937. $orderGoods = BaOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1938. }
  1939. $value['hasChildren'] = $orderGoods;
  1940. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1941. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1942. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1943. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  1944. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  1945. }
  1946. return $data;
  1947. }
  1948. /**
  1949. * 导出订单.
  1950. * @return mixed
  1951. * @throws HttpException
  1952. */
  1953. public function actionBaDecOrderExport()
  1954. {
  1955. $orderSn = \Yii::$app->request->get('orderSn');
  1956. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1957. $params = [
  1958. ':USER_ID' => \Yii::$app->user->id,
  1959. ':ORDER_SN' => $orderSn,
  1960. ];
  1961. $data = BaDecOrder::lists($condition, $params, [
  1962. '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',
  1963. 'orderBy' => 'DO.CREATED_AT DESC',
  1964. 'from' => BaDecOrder::tableName() . ' AS DO',
  1965. 'join' => [
  1966. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1967. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1968. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1969. ['LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1970. ['LEFT JOIN', BaOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1971. ],
  1972. ]);
  1973. if (!$data['list']) {
  1974. $data = BaApproachDecOrder::lists($condition, $params, [
  1975. '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',
  1976. 'orderBy' => 'DO.CREATED_AT DESC',
  1977. 'from' => BaApproachDecOrder::tableName() . ' AS DO',
  1978. 'join' => [
  1979. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1980. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1981. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1982. ['LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1983. ['LEFT JOIN', BaApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1984. ],
  1985. ]);
  1986. }
  1987. $userId = '';
  1988. $userName = '';
  1989. $address = '';
  1990. $mobile = '';
  1991. $orderAt = '';
  1992. $orderDetails = '';
  1993. $orderAmount = 0; // 合计总额
  1994. $orderNums = 0; // 合计总数
  1995. $totalTaxAmount = 0; // 合计税额
  1996. $totalAmount = 0;
  1997. foreach ($data['list'] as $key => $value) {
  1998. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1999. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  2000. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  2001. $userId = $value['USER_NAME'];
  2002. $userName = $value['REAL_NAME'];
  2003. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  2004. $mobile = $value['MOBILE'];
  2005. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  2006. // 总价
  2007. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  2008. $orderAmount += $totalAmount;
  2009. $orderNums += $value['BUY_NUMS'];
  2010. // 税额
  2011. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  2012. $totalTaxAmount += $taxAmount;
  2013. $taxAmount = sprintf('%.2f', $taxAmount);
  2014. $totalAmount = sprintf('%.2f', $totalAmount);
  2015. // 订单详情
  2016. $orderDetails .= <<<EOT
  2017. <tr>
  2018. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  2019. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  2020. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  2021. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  2022. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  2023. <td style="text-align: right;">{$taxAmount}</td>
  2024. <td style="text-align: right;">{$totalAmount}</td>
  2025. </tr>
  2026. EOT;
  2027. }
  2028. $memberCode = Yii::t('app', 'memberCode');
  2029. $memberName = Yii::t('app', 'memberName');
  2030. $memberAddress = Yii::t('app', 'memberAddress');
  2031. $memberPhone = Yii::t('app', 'memberPhone');
  2032. $orderCode = Yii::t('app', 'orderCode');
  2033. $orderDetail = Yii::t('app', 'orderDetail');
  2034. $productCode = Yii::t('app', 'productCode');
  2035. $productName = Yii::t('app', 'productName');
  2036. $productPrice = Yii::t('app', 'productPrice');
  2037. $quantity = Yii::t('app', 'qty');
  2038. $taxRate = Yii::t('app', 'taxRate');
  2039. $totalTax = Yii::t('app', 'totalTax');
  2040. $totalAmount = Yii::t('app', 'totalAmount');
  2041. $total = Yii::t('app', 'total');
  2042. $signature = Yii::t('app', 'signature');
  2043. $date = Yii::t('app', 'date');
  2044. $createAt = Yii::t('app', 'createAt');
  2045. // 订单基本信息
  2046. $orderBase = <<<ORDER
  2047. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  2048. <tr>
  2049. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  2050. <td width="70%">{$userId}</td>
  2051. </tr>
  2052. <tr>
  2053. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  2054. <td width="70%">{$userName}</td>
  2055. </tr>
  2056. <tr>
  2057. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  2058. <td width="70%">{$address}</td>
  2059. </tr>
  2060. <tr>
  2061. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  2062. <td width="70%">{$mobile}</td>
  2063. </tr>
  2064. <tr>
  2065. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  2066. <td width="70%">{$orderSn}</td>
  2067. </tr>
  2068. <tr>
  2069. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  2070. <td width="70%">{$orderAt}</td>
  2071. </tr>
  2072. <tr>
  2073. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  2074. <td class="bg"></td>
  2075. </tr>
  2076. </table>
  2077. ORDER;
  2078. $l['a_meta_charset'] = 'UTF-8';
  2079. $l['a_meta_dir'] = 'ltr';
  2080. $l['a_meta_language'] = 'zh';
  2081. $l['w_page'] = '页面';
  2082. $orderAmount = sprintf('%.2f', $orderAmount);
  2083. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  2084. $context = <<<ORDER
  2085. <!doctype html>
  2086. <html lang="en">
  2087. <head>
  2088. <meta charset="UTF-8" />
  2089. <title>{$orderDetail}</title>
  2090. <style>
  2091. table {
  2092. border-collapse: collapse;
  2093. }
  2094. table td, table th {
  2095. border: 1px solid #ccc;
  2096. /*padding: 5px 5px;*/
  2097. border-collapse: collapse;
  2098. }
  2099. /*td {*/
  2100. /* padding: 120px;*/
  2101. /*}*/
  2102. .bg {
  2103. background-color: #ccc;
  2104. }
  2105. </style>
  2106. </head>
  2107. <body>
  2108. <div class="content">
  2109. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  2110. <div>
  2111. <div style="display: block; width: 100%;">
  2112. {$orderBase}
  2113. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  2114. <tr>
  2115. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  2116. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  2117. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  2118. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  2119. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  2120. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  2121. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  2122. </tr>
  2123. {$orderDetails}
  2124. <tr>
  2125. <td colspan="3">{$total}</td>
  2126. <td style="text-align: right;">{$orderNums}</td>
  2127. <td></td>
  2128. <td style="text-align: right;">{$totalTaxAmount}</td>
  2129. <td style="text-align: right;">{$orderAmount}</td>
  2130. </tr>
  2131. </table>
  2132. </div>
  2133. <div style="width: 100%; margin-top: 50px; height: 30px;">
  2134. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  2135. <tr style="border: none;">
  2136. <td width="70%" style="border: none;"></td>
  2137. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  2138. </tr>
  2139. <tr style="border: none;">
  2140. <td width="70%" style="border: none;"></td>
  2141. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  2142. </tr>
  2143. </table>
  2144. </div>
  2145. </div>
  2146. </div>
  2147. </body>
  2148. </html>
  2149. ORDER;
  2150. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  2151. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  2152. // 设置打印模式
  2153. $pdf->SetCreator(PDF_CREATOR);
  2154. $pdf->SetAuthor('DaZe');
  2155. $pdf->SetTitle($orderSn);
  2156. $pdf->SetSubject('TCPDF Tutorial');
  2157. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  2158. // 是否显示页眉
  2159. $pdf->setPrintHeader(false);
  2160. // 设置页眉字体
  2161. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  2162. // 页眉距离顶部的距离
  2163. $pdf->SetHeaderMargin('5');
  2164. // 是否显示页脚
  2165. $pdf->setPrintFooter(false);
  2166. // 设置默认等宽字体
  2167. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  2168. // 设置行高
  2169. $pdf->setCellHeightRatio(1);
  2170. // 设置左、上、右的间距
  2171. $pdf->SetMargins('10', '0', '10');
  2172. // 设置是否自动分页 距离底部多少距离时分页
  2173. $pdf->SetAutoPageBreak(TRUE, '15');
  2174. // 设置图像比例因子
  2175. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  2176. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  2177. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  2178. $pdf->setLanguageArray($l);
  2179. }
  2180. $pdf->setFontSubsetting(true);
  2181. $pdf->AddPage();
  2182. // 设置字体
  2183. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  2184. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  2185. // if (!file_exists($image_file)) {
  2186. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  2187. // }
  2188. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  2189. $image = file_get_contents($image_file);
  2190. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  2191. $pdf->writeHTML($context);
  2192. ob_clean();
  2193. $file_name = $orderSn . '.pdf';
  2194. $path = 'pdfs/' . $file_name;
  2195. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  2196. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  2197. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  2198. }
  2199. /**
  2200. * AMP
  2201. */
  2202. public function actionAutoMaintenance()
  2203. {
  2204. // 会员信息
  2205. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  2206. if (!$user) {
  2207. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  2208. }
  2209. $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';
  2210. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  2211. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  2212. 'from' => ShopGoods::tableName() . ' AS S',
  2213. 'join' => [
  2214. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  2215. ],
  2216. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  2217. 'limit' => 100,
  2218. ]);
  2219. $currencies = Currency::getAllData();
  2220. $currencies = array_column($currencies, NULL, 'ID');
  2221. foreach ($data['list'] as $key => $value) {
  2222. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  2223. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  2224. }
  2225. return static::notice($data);
  2226. }
  2227. /**
  2228. * AMP
  2229. */
  2230. public function actionAutoMaintenanceSureOrder()
  2231. {
  2232. if (\Yii::$app->request->isPost) {
  2233. $formModel = new OrderForm();
  2234. $formModel->scenario = 'autoMaintenanceOrder';
  2235. $formModel->remark = Yii::t('app', 'autoMaintenance');
  2236. $post = \Yii::$app->request->post();
  2237. $post['type'] = DeclarationForm::TYPE_FX;
  2238. $stockist = $post['decUserName'] ?? '';
  2239. if (!$stockist) {
  2240. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  2241. }
  2242. try {
  2243. if ($formModel->load($post, '') && $formModel->autoMaintenanceSureOrder()) {
  2244. return static::notice(Yii::t('app', 'successfully'));
  2245. } else {
  2246. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  2247. }
  2248. } catch (HttpException|Exception|\Throwable $e) {
  2249. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  2250. }
  2251. }
  2252. }
  2253. /**
  2254. * AMP
  2255. */
  2256. public function actionAutoMaintenanceApproachSureOrder(){
  2257. if (\Yii::$app->request->isPost) {
  2258. $formModel = new ApproachAutoMaintenanceOrderForm();
  2259. $formModel->scenario = 'autoMaintenanceOrder';
  2260. $formModel->remark = 'Auto Maintenance Order';
  2261. $post = \Yii::$app->request->post();
  2262. $post['type'] = DeclarationForm::TYPE_FX;
  2263. $stockist = $post['decUserName'] ?? '';
  2264. if (!$stockist) {
  2265. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  2266. }
  2267. try {
  2268. if ($formModel->load($post, '') && $order = $formModel->autoMaintenanceSureOrder()) {
  2269. return static::notice($order);
  2270. } else {
  2271. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  2272. }
  2273. } catch (\yii\db\Exception|HttpException|Exception $e) {
  2274. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  2275. }
  2276. }
  2277. }
  2278. }