ShopController.php 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  944. if (!file_exists($image)) {
  945. $image = file_get_contents(\Yii::$app->runtimePath . '/uploads/ngds-logo.jpg');
  946. }
  947. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  948. $pdf->writeHTML($context);
  949. ob_clean();
  950. $file_name = $orderSn . '.pdf';
  951. $path = 'pdfs/' . $file_name;
  952. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  953. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  954. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  955. }
  956. /**
  957. * 导出订单.
  958. * @return mixed
  959. * @throws HttpException
  960. */
  961. public function actionDecOrderExport()
  962. {
  963. $orderSn = \Yii::$app->request->get('orderSn');
  964. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  965. $params = [
  966. ':USER_ID' => \Yii::$app->user->id,
  967. ':ORDER_SN' => $orderSn,
  968. ];
  969. $data = DecOrder::lists($condition, $params, [
  970. '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',
  971. 'orderBy' => 'DO.CREATED_AT DESC',
  972. 'from' => DecOrder::tableName() . ' AS DO',
  973. 'join' => [
  974. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  975. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  976. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  977. ['LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  978. ['LEFT JOIN', Order::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  979. ],
  980. ]);
  981. if (!$data['list']) {
  982. $data = ApproachDecOrder::lists($condition, $params, [
  983. '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',
  984. 'orderBy' => 'DO.CREATED_AT DESC',
  985. 'from' => ApproachDecOrder::tableName() . ' AS DO',
  986. 'join' => [
  987. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  988. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  989. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  990. ['LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  991. ['LEFT JOIN', ApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  992. ],
  993. ]);
  994. }
  995. $userId = '';
  996. $userName = '';
  997. $address = '';
  998. $mobile = '';
  999. $orderAt = '';
  1000. $orderDetails = '';
  1001. $orderAmount = 0; // 合计总额
  1002. $orderNums = 0; // 合计总数
  1003. $totalTaxAmount = 0; // 合计税额
  1004. $totalAmount = 0;
  1005. foreach ($data['list'] as $key => $value) {
  1006. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1007. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1008. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1009. $userId = $value['USER_NAME'];
  1010. $userName = $value['REAL_NAME'];
  1011. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1012. $mobile = $value['MOBILE'];
  1013. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1014. // 总价
  1015. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1016. $orderAmount += $totalAmount;
  1017. $orderNums += $value['BUY_NUMS'];
  1018. // 税额
  1019. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1020. $totalTaxAmount += $taxAmount;
  1021. $taxAmount = sprintf('%.2f', $taxAmount);
  1022. $totalAmount = sprintf('%.2f', $totalAmount);
  1023. // 订单详情
  1024. $orderDetails .= <<<EOT
  1025. <tr>
  1026. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1027. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1028. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1029. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1030. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1031. <td style="text-align: right;">{$taxAmount}</td>
  1032. <td style="text-align: right;">{$totalAmount}</td>
  1033. </tr>
  1034. EOT;
  1035. }
  1036. $memberCode = Yii::t('app', 'memberCode');
  1037. $memberName = Yii::t('app', 'memberName');
  1038. $memberAddress = Yii::t('app', 'memberAddress');
  1039. $memberPhone = Yii::t('app', 'memberPhone');
  1040. $orderCode = Yii::t('app', 'orderCode');
  1041. $orderDetail = Yii::t('app', 'orderDetail');
  1042. $productCode = Yii::t('app', 'productCode');
  1043. $productName = Yii::t('app', 'productName');
  1044. $productPrice = Yii::t('app', 'productPrice');
  1045. $quantity = Yii::t('app', 'qty');
  1046. $taxRate = Yii::t('app', 'taxRate');
  1047. $totalTax = Yii::t('app', 'totalTax');
  1048. $totalAmount = Yii::t('app', 'totalAmount');
  1049. $total = Yii::t('app', 'total');
  1050. $signature = Yii::t('app', 'signature');
  1051. $date = Yii::t('app', 'date');
  1052. $createAt = Yii::t('app', 'createAt');
  1053. // 订单基本信息
  1054. $orderBase = <<<ORDER
  1055. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1056. <tr>
  1057. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1058. <td width="70%">{$userId}</td>
  1059. </tr>
  1060. <tr>
  1061. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1062. <td width="70%">{$userName}</td>
  1063. </tr>
  1064. <tr>
  1065. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1066. <td width="70%">{$address}</td>
  1067. </tr>
  1068. <tr>
  1069. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1070. <td width="70%">{$mobile}</td>
  1071. </tr>
  1072. <tr>
  1073. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1074. <td width="70%">{$orderSn}</td>
  1075. </tr>
  1076. <tr>
  1077. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1078. <td width="70%">{$orderAt}</td>
  1079. </tr>
  1080. <tr>
  1081. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1082. <td class="bg"></td>
  1083. </tr>
  1084. </table>
  1085. ORDER;
  1086. $l['a_meta_charset'] = 'UTF-8';
  1087. $l['a_meta_dir'] = 'ltr';
  1088. $l['a_meta_language'] = 'zh';
  1089. $l['w_page'] = '页面';
  1090. $orderAmount = sprintf('%.2f', $orderAmount);
  1091. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1092. $context = <<<ORDER
  1093. <!doctype html>
  1094. <html lang="en">
  1095. <head>
  1096. <meta charset="UTF-8" />
  1097. <title>{$orderDetail}</title>
  1098. <style>
  1099. table {
  1100. border-collapse: collapse;
  1101. }
  1102. table td, table th {
  1103. border: 1px solid #ccc;
  1104. /*padding: 5px 5px;*/
  1105. border-collapse: collapse;
  1106. }
  1107. /*td {*/
  1108. /* padding: 120px;*/
  1109. /*}*/
  1110. .bg {
  1111. background-color: #ccc;
  1112. }
  1113. </style>
  1114. </head>
  1115. <body>
  1116. <div class="content">
  1117. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1118. <div>
  1119. <div style="display: block; width: 100%;">
  1120. {$orderBase}
  1121. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1122. <tr>
  1123. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1124. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1125. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1126. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1127. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1128. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1129. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1130. </tr>
  1131. {$orderDetails}
  1132. <tr>
  1133. <td colspan="3">{$total}</td>
  1134. <td style="text-align: right;">{$orderNums}</td>
  1135. <td></td>
  1136. <td style="text-align: right;">{$totalTaxAmount}</td>
  1137. <td style="text-align: right;">{$orderAmount}</td>
  1138. </tr>
  1139. </table>
  1140. </div>
  1141. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1142. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1143. <tr style="border: none;">
  1144. <td width="70%" style="border: none;"></td>
  1145. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1146. </tr>
  1147. <tr style="border: none;">
  1148. <td width="70%" style="border: none;"></td>
  1149. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1150. </tr>
  1151. </table>
  1152. </div>
  1153. </div>
  1154. </div>
  1155. </body>
  1156. </html>
  1157. ORDER;
  1158. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1159. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1160. // 设置打印模式
  1161. $pdf->SetCreator(PDF_CREATOR);
  1162. $pdf->SetAuthor('DaZe');
  1163. $pdf->SetTitle($orderSn);
  1164. $pdf->SetSubject('TCPDF Tutorial');
  1165. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1166. // 是否显示页眉
  1167. $pdf->setPrintHeader(false);
  1168. // 设置页眉字体
  1169. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1170. // 页眉距离顶部的距离
  1171. $pdf->SetHeaderMargin('5');
  1172. // 是否显示页脚
  1173. $pdf->setPrintFooter(false);
  1174. // 设置默认等宽字体
  1175. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1176. // 设置行高
  1177. $pdf->setCellHeightRatio(1);
  1178. // 设置左、上、右的间距
  1179. $pdf->SetMargins('10', '0', '10');
  1180. // 设置是否自动分页 距离底部多少距离时分页
  1181. $pdf->SetAutoPageBreak(TRUE, '15');
  1182. // 设置图像比例因子
  1183. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1184. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1185. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1186. $pdf->setLanguageArray($l);
  1187. }
  1188. $pdf->setFontSubsetting(true);
  1189. $pdf->AddPage();
  1190. // 设置字体
  1191. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1192. $image = file_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  1193. if (!file_exists($image)) {
  1194. $image = file_get_contents(\Yii::$app->runtimePath . '/uploads/ngds-logo.jpg');
  1195. }
  1196. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1197. $pdf->writeHTML($context);
  1198. ob_clean();
  1199. $file_name = $orderSn . '.pdf';
  1200. $path = 'pdfs/' . $file_name;
  1201. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1202. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1203. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1204. }
  1205. /**
  1206. * 下载申请退货单
  1207. */
  1208. public function actionOrderBackingOut()
  1209. {
  1210. $file_name = 'orderBackingOut.xlsx';
  1211. return static::notice(['fileUrl' => $file_name]);
  1212. }
  1213. /**
  1214. * BA商品列表
  1215. * @return mixed
  1216. * @throws HttpException
  1217. */
  1218. public function actionBaProductList() {
  1219. // 商品分类
  1220. $categoryType = \Yii::$app->request->get('categoryType', 1);
  1221. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  1222. $isStudio = BaUser::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  1223. if($isStudio==1){
  1224. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  1225. }
  1226. $condition .= ") AND CATEGORY_TYPE = :CATEGORY_TYPE";
  1227. $params[':CATEGORY_TYPE'] = intval($categoryType);
  1228. $data = ShopGoods::lists($condition, $params, [
  1229. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1230. 'from' => ShopGoods::tableName(),
  1231. ]);
  1232. foreach ($data['list'] as $key => $value) {
  1233. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  1234. $data['list'][$key]['DISCOUNT'] = ShopGoods::getGoodType()[$value['TYPE']]['discount'];
  1235. } else {
  1236. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  1237. }
  1238. }
  1239. return static::notice($data);
  1240. }
  1241. /**
  1242. * BA购物车订单展示
  1243. * @throws HttpException
  1244. */
  1245. public function actionBaShoppingCart()
  1246. {
  1247. // 收货地址
  1248. $allAddress = BaReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1249. if($allAddress) {
  1250. foreach ($allAddress as $key => $row) {
  1251. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1252. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1253. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1254. }
  1255. }
  1256. // 运费.奈拉
  1257. $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
  1258. // 普通商品免运费阈值.奈拉
  1259. $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
  1260. return static::notice([
  1261. 'freight' => $freight,
  1262. 'freeShipping' => $freeShipping,
  1263. 'allAddress' => $allAddress,
  1264. ]);
  1265. }
  1266. /**
  1267. * 确认BA订单
  1268. * @throws HttpException|\yii\base\Exception
  1269. */
  1270. public function actionBaSureApproachOrder()
  1271. {
  1272. if (\Yii::$app->request->isPost) {
  1273. $formModel = new BaApproachOrderForm();
  1274. $formModel->scenario = 'userOrder';
  1275. $formModel->remark = Yii::t('app', 'reconsumeRemark');
  1276. $post = \Yii::$app->request->post();
  1277. $post['type'] = DeclarationForm::TYPE_FX;
  1278. if ($formModel->load($post, '') && $order = $formModel->add()) {
  1279. return static::notice($order);
  1280. } else {
  1281. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1282. }
  1283. }
  1284. }
  1285. /**
  1286. * 删除BA准订单
  1287. * @throws HttpException
  1288. */
  1289. public function actionBaDeleteApproachOrder()
  1290. {
  1291. $orderSn = \Yii::$app->request->post('orderSn');
  1292. // 删除订单中间表
  1293. BaApproachOrder::deleteAll('SN = :SN', [':SN' => $orderSn]);
  1294. // 删除订单商品中间表
  1295. BaApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $orderSn]);
  1296. return static::notice('');
  1297. }
  1298. /**
  1299. * BA订单
  1300. * @return mixed
  1301. * @throws HttpException
  1302. */
  1303. public function actionBaOrderList()
  1304. {
  1305. $uname = Info::getBaUserNameByUserId(\Yii::$app->user->id);
  1306. $condition = " (USER_ID=:USER_ID OR CREATE_USER='$uname') AND O.IS_DELETE = 0";
  1307. $params[':USER_ID'] = \Yii::$app->user->id;
  1308. $orderQuery = BaOrder::find()
  1309. ->alias('O')
  1310. ->where($condition, $params)
  1311. ->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')
  1312. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1313. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1314. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1315. ->orderBy('O.CREATED_AT DESC');
  1316. // 订单中间表只查询待支付和支付失败的订单
  1317. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1318. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1319. $orderStandardQuery = BaApproachOrder::find()
  1320. ->alias('O')
  1321. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1322. ->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')
  1323. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1324. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1325. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1326. ->orderBy('O.CREATED_AT DESC');
  1327. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1328. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1329. $totalCount = $query->count();
  1330. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1331. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1332. $data = [
  1333. 'list' => $lists,
  1334. 'currentPage'=>$pagination->page,
  1335. 'totalPages'=>$pagination->pageCount,
  1336. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1337. 'totalCount' => $pagination->totalCount,
  1338. 'pageSize' => $pagination->pageSize,
  1339. ];
  1340. foreach ($data['list'] as $key => $value) {
  1341. $data['list'][$key]['ORDER_TYPE'] = 'Reselling';
  1342. $data['list'][$key]['PAY_AT'] = $value['PAY_AT'] ? Date::convert($value['PAY_AT'],'Y-m-d H:i:s') : '';
  1343. $data['list'][$key]['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1344. $data['list'][$key]['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1345. $data['list'][$key]['STATUS'] = Tool::paramConvert(\Yii::$app->params['orderStatus'])[$value['STATUS']]['label'] ?? '';
  1346. $data['list'][$key]['FULL_ADDRESS'] = $value['ADDRESS'];
  1347. $data['list'][$key]['TAX_AMOUNT'] = Tool::formatPrice(($value['REAL_PRICE'] - $value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100)) * $value['BUY_NUMS']);
  1348. $data['list'][$key]['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1349. if ($value['LGA_NAME']) {
  1350. $data['list'][$key]['FULL_ADDRESS'] .= ', ' . $value['LGA_NAME'];
  1351. }
  1352. }
  1353. return static::notice($data);
  1354. }
  1355. /**
  1356. * @throws HttpException
  1357. */
  1358. public function actionBaDecOrderList() {
  1359. $version = \Yii::$app->request->get('v');
  1360. $data = $version == 2 ? $this->baDecOrderListV2() : $this->baDecOrderListV1();
  1361. return static::notice($data);
  1362. }
  1363. /**
  1364. * 我的BA报单
  1365. * @return mixed
  1366. * @throws HttpException
  1367. */
  1368. public function baDecOrderListV1() {
  1369. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1370. $params[':USER_ID'] = \Yii::$app->user->id;
  1371. $orderQuery = BaDecOrder::find()
  1372. ->alias('DO')
  1373. ->where($condition, $params)
  1374. ->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')
  1375. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1376. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1377. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1378. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1379. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1380. ->orderBy('DO.CREATED_AT DESC');
  1381. // 订单中间表只查询待支付和支付失败的订单
  1382. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1383. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1384. $orderStandardQuery = BaApproachDecOrder::find()
  1385. ->alias('DO')
  1386. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1387. ->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')
  1388. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1389. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1390. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1391. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN')
  1392. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1393. ->orderBy('DO.CREATED_AT DESC');
  1394. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1395. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1396. $totalCount = $query->count();
  1397. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1398. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1399. $data = [
  1400. 'list' => $lists,
  1401. 'currentPage'=>$pagination->page,
  1402. 'totalPages'=>$pagination->pageCount,
  1403. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1404. 'totalCount' => $pagination->totalCount,
  1405. 'pageSize' => $pagination->pageSize,
  1406. ];
  1407. foreach ($data['list'] as &$value) {
  1408. $value['TAX_AMOUNT'] = Tool::calculateTax($value['REAL_PRICE'], floatval($value['TAX_RATE']), intval($value['BUY_NUMS']));
  1409. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1410. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1411. $value['REAL_PV'] = Tool::formatPrice($value['REAL_PV'] * $value['BUY_NUMS']);
  1412. $value['TOTAL_AMOUNT'] = Tool::formatPrice($value['REAL_PRICE'] * $value['BUY_NUMS']);
  1413. }
  1414. return $data;
  1415. }
  1416. /**
  1417. * 我的BA报单
  1418. * @return mixed
  1419. * @throws HttpException
  1420. */
  1421. public function baDecOrderListV2() {
  1422. $condition = ' DO.USER_ID=:USER_ID AND DO.IS_DEL=0';
  1423. $params[':USER_ID'] = \Yii::$app->user->id;
  1424. $orderQuery = BaDecOrder::find()
  1425. ->alias('DO')
  1426. ->where($condition, $params)
  1427. ->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')
  1428. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1429. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1430. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1431. ->join('LEFT JOIN', BaOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1432. ->orderBy('DO.CREATED_AT DESC');
  1433. // 订单中间表只查询待支付和支付失败的订单
  1434. $params[':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1435. $params[':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1436. $orderStandardQuery = BaApproachDecOrder::find()
  1437. ->alias('DO')
  1438. ->where($condition . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $params)
  1439. ->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')
  1440. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID')
  1441. ->join('LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID')
  1442. ->join('LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID')
  1443. ->join('LEFT JOIN', BaApproachOrder::tableName() . ' AS O', 'O.SN=DO.ORDER_SN')
  1444. ->orderBy('DO.CREATED_AT DESC');
  1445. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1446. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1447. $totalCount = $query->count();
  1448. $pagination = new Pagination(['totalCount' => $totalCount, 'pageSize' => \Yii::$app->request->get('pageSize')]);
  1449. $lists = $query->offset($pagination->offset)->limit($pagination->limit)->all();
  1450. $data = [
  1451. 'list' => $lists,
  1452. 'currentPage'=>$pagination->page,
  1453. 'totalPages'=>$pagination->pageCount,
  1454. 'startNum' => $pagination->page * $pagination->pageSize + 1,
  1455. 'totalCount' => $pagination->totalCount,
  1456. 'pageSize' => $pagination->pageSize,
  1457. ];
  1458. foreach ($data['list'] as &$value) {
  1459. if (($value['PAY_TYPE'] == 'pay_stack') && in_array($value['STATUS'], [\Yii::$app->params['orderStatus']['notPaid']['value'], \Yii::$app->params['orderStatus']['failPaid']['value']])) {
  1460. // 订单中间表
  1461. $orderGoods = BaApproachOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1462. } else {
  1463. // 订单表
  1464. $orderGoods = BaOrderGoods::findAllAsArray('ORDER_SN=:ORDER_SN', [':ORDER_SN' => $value['ORDER_SN']], 'SKU_CODE,GOODS_TITLE,BUY_NUMS,TAX_RATE,REAL_PV,REAL_PRICE');
  1465. }
  1466. $value['hasChildren'] = $orderGoods;
  1467. $value['STATUS'] = \Yii::$app->params['orderStatus'][$value['STATUS']]['label'] ?? '';
  1468. $value['CREATED_AT'] = $value['CREATED_AT'] ? Date::convert($value['CREATED_AT'],'Y-m-d H:i:s') : '';
  1469. $value['PAY_TYPE'] = array_column(ShopGoods::getSaleType(), NULL, 'label')[$value['PAY_TYPE']]['name'] ?? '';
  1470. $value['FULL_ADDRESS'] = trim(implode(', ', [$value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], Region::getCnName($value['PROVINCE'])]), ', ');
  1471. unset($value['ADDRESS'], $value['CITY_NAME'], $value['LGA_NAME'], $value['PROVINCE']);
  1472. }
  1473. return $data;
  1474. }
  1475. /**
  1476. * 导出订单.
  1477. * @return mixed
  1478. * @throws HttpException
  1479. */
  1480. public function actionBaDecOrderExport()
  1481. {
  1482. $orderSn = \Yii::$app->request->get('orderSn');
  1483. $condition = ' AND DO.USER_ID=:USER_ID AND IS_DEL=0 AND DO.ORDER_SN=:ORDER_SN';
  1484. $params = [
  1485. ':USER_ID' => \Yii::$app->user->id,
  1486. ':ORDER_SN' => $orderSn,
  1487. ];
  1488. $data = BaDecOrder::lists($condition, $params, [
  1489. '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',
  1490. 'orderBy' => 'DO.CREATED_AT DESC',
  1491. 'from' => BaDecOrder::tableName() . ' AS DO',
  1492. 'join' => [
  1493. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1494. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1495. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1496. ['LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1497. ['LEFT JOIN', BaOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1498. ],
  1499. ]);
  1500. if (!$data['list']) {
  1501. $data = BaApproachDecOrder::lists($condition, $params, [
  1502. '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',
  1503. 'orderBy' => 'DO.CREATED_AT DESC',
  1504. 'from' => BaApproachDecOrder::tableName() . ' AS DO',
  1505. 'join' => [
  1506. ['LEFT JOIN', BaUser::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  1507. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  1508. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  1509. ['LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=DO.ORDER_SN'],
  1510. ['LEFT JOIN', BaApproachOrder::tableName() . ' AS OD', 'OD.SN=DO.ORDER_SN'],
  1511. ],
  1512. ]);
  1513. }
  1514. $userId = '';
  1515. $userName = '';
  1516. $address = '';
  1517. $mobile = '';
  1518. $orderAt = '';
  1519. $orderDetails = '';
  1520. $orderAmount = 0; // 合计总额
  1521. $orderNums = 0; // 合计总数
  1522. $totalTaxAmount = 0; // 合计税额
  1523. $totalAmount = 0;
  1524. foreach ($data['list'] as $key => $value) {
  1525. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1526. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1527. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1528. $userId = $value['USER_NAME'];
  1529. $userName = $value['REAL_NAME'];
  1530. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1531. $mobile = $value['MOBILE'];
  1532. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1533. // 总价
  1534. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1535. $orderAmount += $totalAmount;
  1536. $orderNums += $value['BUY_NUMS'];
  1537. // 税额
  1538. $taxAmount = floatval(Tool::formatPrice(($value['REAL_PRICE'] - ($value['REAL_PRICE'] / (1 + $value['TAX_RATE'] / 100))) * $value['BUY_NUMS']));
  1539. $totalTaxAmount += $taxAmount;
  1540. $taxAmount = sprintf('%.2f', $taxAmount);
  1541. $totalAmount = sprintf('%.2f', $totalAmount);
  1542. // 订单详情
  1543. $orderDetails .= <<<EOT
  1544. <tr>
  1545. <td style="text-align: center;">{$value['SKU_CODE']}</td>
  1546. <td style="text-align: center;">{$value['GOODS_TITLE']}</td>
  1547. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1548. <td style="text-align: right;">{$value['BUY_NUMS']}</td>
  1549. <td style="text-align: right;">{$value['TAX_RATE']}%</td>
  1550. <td style="text-align: right;">{$taxAmount}</td>
  1551. <td style="text-align: right;">{$totalAmount}</td>
  1552. </tr>
  1553. EOT;
  1554. }
  1555. $memberCode = Yii::t('app', 'memberCode');
  1556. $memberName = Yii::t('app', 'memberName');
  1557. $memberAddress = Yii::t('app', 'memberAddress');
  1558. $memberPhone = Yii::t('app', 'memberPhone');
  1559. $orderCode = Yii::t('app', 'orderCode');
  1560. $orderDetail = Yii::t('app', 'orderDetail');
  1561. $productCode = Yii::t('app', 'productCode');
  1562. $productName = Yii::t('app', 'productName');
  1563. $productPrice = Yii::t('app', 'productPrice');
  1564. $quantity = Yii::t('app', 'qty');
  1565. $taxRate = Yii::t('app', 'taxRate');
  1566. $totalTax = Yii::t('app', 'totalTax');
  1567. $totalAmount = Yii::t('app', 'totalAmount');
  1568. $total = Yii::t('app', 'total');
  1569. $signature = Yii::t('app', 'signature');
  1570. $date = Yii::t('app', 'date');
  1571. $createAt = Yii::t('app', 'createAt');
  1572. // 订单基本信息
  1573. $orderBase = <<<ORDER
  1574. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1575. <tr>
  1576. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  1577. <td width="70%">{$userId}</td>
  1578. </tr>
  1579. <tr>
  1580. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  1581. <td width="70%">{$userName}</td>
  1582. </tr>
  1583. <tr>
  1584. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  1585. <td width="70%">{$address}</td>
  1586. </tr>
  1587. <tr>
  1588. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  1589. <td width="70%">{$mobile}</td>
  1590. </tr>
  1591. <tr>
  1592. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  1593. <td width="70%">{$orderSn}</td>
  1594. </tr>
  1595. <tr>
  1596. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  1597. <td width="70%">{$orderAt}</td>
  1598. </tr>
  1599. <tr>
  1600. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  1601. <td class="bg"></td>
  1602. </tr>
  1603. </table>
  1604. ORDER;
  1605. $l['a_meta_charset'] = 'UTF-8';
  1606. $l['a_meta_dir'] = 'ltr';
  1607. $l['a_meta_language'] = 'zh';
  1608. $l['w_page'] = '页面';
  1609. $orderAmount = sprintf('%.2f', $orderAmount);
  1610. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  1611. $context = <<<ORDER
  1612. <!doctype html>
  1613. <html lang="en">
  1614. <head>
  1615. <meta charset="UTF-8" />
  1616. <title>{$orderDetail}</title>
  1617. <style>
  1618. table {
  1619. border-collapse: collapse;
  1620. }
  1621. table td, table th {
  1622. border: 1px solid #ccc;
  1623. /*padding: 5px 5px;*/
  1624. border-collapse: collapse;
  1625. }
  1626. /*td {*/
  1627. /* padding: 120px;*/
  1628. /*}*/
  1629. .bg {
  1630. background-color: #ccc;
  1631. }
  1632. </style>
  1633. </head>
  1634. <body>
  1635. <div class="content">
  1636. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  1637. <div>
  1638. <div style="display: block; width: 100%;">
  1639. {$orderBase}
  1640. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1641. <tr>
  1642. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  1643. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  1644. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  1645. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  1646. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  1647. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  1648. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  1649. </tr>
  1650. {$orderDetails}
  1651. <tr>
  1652. <td colspan="3">{$total}</td>
  1653. <td style="text-align: right;">{$orderNums}</td>
  1654. <td></td>
  1655. <td style="text-align: right;">{$totalTaxAmount}</td>
  1656. <td style="text-align: right;">{$orderAmount}</td>
  1657. </tr>
  1658. </table>
  1659. </div>
  1660. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1661. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1662. <tr style="border: none;">
  1663. <td width="70%" style="border: none;"></td>
  1664. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  1665. </tr>
  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;">{$date}:</td>
  1669. </tr>
  1670. </table>
  1671. </div>
  1672. </div>
  1673. </div>
  1674. </body>
  1675. </html>
  1676. ORDER;
  1677. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1678. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1679. // 设置打印模式
  1680. $pdf->SetCreator(PDF_CREATOR);
  1681. $pdf->SetAuthor('DaZe');
  1682. $pdf->SetTitle($orderSn);
  1683. $pdf->SetSubject('TCPDF Tutorial');
  1684. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1685. // 是否显示页眉
  1686. $pdf->setPrintHeader(false);
  1687. // 设置页眉字体
  1688. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1689. // 页眉距离顶部的距离
  1690. $pdf->SetHeaderMargin('5');
  1691. // 是否显示页脚
  1692. $pdf->setPrintFooter(false);
  1693. // 设置默认等宽字体
  1694. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1695. // 设置行高
  1696. $pdf->setCellHeightRatio(1);
  1697. // 设置左、上、右的间距
  1698. $pdf->SetMargins('10', '0', '10');
  1699. // 设置是否自动分页 距离底部多少距离时分页
  1700. $pdf->SetAutoPageBreak(TRUE, '15');
  1701. // 设置图像比例因子
  1702. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1703. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1704. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1705. $pdf->setLanguageArray($l);
  1706. }
  1707. $pdf->setFontSubsetting(true);
  1708. $pdf->AddPage();
  1709. // 设置字体
  1710. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1711. $image = file_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  1712. if (!file_exists($image)) {
  1713. $image = file_get_contents(\Yii::$app->runtimePath . '/uploads/ngds-logo.jpg');
  1714. }
  1715. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1716. $pdf->writeHTML($context);
  1717. ob_clean();
  1718. $file_name = $orderSn . '.pdf';
  1719. $path = 'pdfs/' . $file_name;
  1720. $pdf->Output(Yii::$app->basePath . '/web/' . $path, 'F');
  1721. @exec('chmod -R 777 /' . Yii::$app->basePath . '/web' . $path);
  1722. return static::notice(['fileUrl' => $path, 'targetName' => $file_name]);
  1723. }
  1724. /**
  1725. * AMP
  1726. */
  1727. public function actionAutoMaintenance()
  1728. {
  1729. // 会员信息
  1730. $user = User::findOneAsArray('ID=:ID', [':ID' => \Yii::$app->request->get('userId')]);
  1731. if (!$user) {
  1732. return static::notice(Yii::t('app', 'memberDoesNotExist'), 400);
  1733. }
  1734. $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';
  1735. $data = ShopGoods::lists($condition, [':COUNTRY_ID' => $user['COUNTRY_ID']], [
  1736. 'select' => 'S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID',
  1737. 'from' => ShopGoods::tableName() . ' AS S',
  1738. 'join' => [
  1739. ['INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID = S.ID'],
  1740. ],
  1741. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  1742. 'limit' => 100,
  1743. ]);
  1744. $currencies = Currency::getAllData();
  1745. $currencies = array_column($currencies, NULL, 'ID');
  1746. foreach ($data['list'] as $key => $value) {
  1747. $data['list'][$key]['DISCOUNT'] = in_array($value['TYPE'], [1, 2]) ? ShopGoods::getGoodType()[$value['TYPE']]['discount'] : $value['SELL_DISCOUNT'] * 100;
  1748. $data['list'][$key]['COIN'] = $currencies[$value['LOCAL_CURRENCY_ID']]['CODE'] ?? '';
  1749. }
  1750. return static::notice($data);
  1751. }
  1752. /**
  1753. * AMP
  1754. */
  1755. public function actionAutoMaintenanceSureOrder()
  1756. {
  1757. if (\Yii::$app->request->isPost) {
  1758. $formModel = new OrderForm();
  1759. $formModel->scenario = 'autoMaintenanceOrder';
  1760. $formModel->remark = Yii::t('app', 'autoMaintenance');
  1761. $post = \Yii::$app->request->post();
  1762. $post['type'] = DeclarationForm::TYPE_FX;
  1763. $stockist = $post['decUserName'] ?? '';
  1764. if (!$stockist) {
  1765. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1766. }
  1767. try {
  1768. if ($formModel->load($post, '') && $formModel->autoMaintenanceSureOrder()) {
  1769. return static::notice(Yii::t('app', 'successfully'));
  1770. } else {
  1771. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1772. }
  1773. } catch (HttpException|Exception|\Throwable $e) {
  1774. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1775. }
  1776. }
  1777. }
  1778. /**
  1779. * AMP
  1780. */
  1781. public function actionAutoMaintenanceApproachSureOrder(){
  1782. if (\Yii::$app->request->isPost) {
  1783. $formModel = new ApproachAutoMaintenanceOrderForm();
  1784. $formModel->scenario = 'autoMaintenanceOrder';
  1785. $formModel->remark = 'Auto Maintenance Order';
  1786. $post = \Yii::$app->request->post();
  1787. $post['type'] = DeclarationForm::TYPE_FX;
  1788. $stockist = $post['decUserName'] ?? '';
  1789. if (!$stockist) {
  1790. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  1791. }
  1792. try {
  1793. if ($formModel->load($post, '') && $order = $formModel->autoMaintenanceSureOrder()) {
  1794. return static::notice($order);
  1795. } else {
  1796. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  1797. }
  1798. } catch (\yii\db\Exception|HttpException|Exception $e) {
  1799. return static::notice(Form::formatErrorsForApi($e->getMessage()), 400);
  1800. }
  1801. }
  1802. }
  1803. }