ShopController.php 101 KB

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