ShopController.php 94 KB

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