ShopController.php 101 KB

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