ShopController.php 102 KB

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