ShopController.php 117 KB

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