ShopController.php 102 KB

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