ShopController.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\models\AdminCountry;
  10. use backendApi\modules\v1\models\AdminRole;
  11. use backendApi\modules\v1\models\exportForms\BaShopExportForm;
  12. use backendApi\modules\v1\models\exportForms\ShopExportForm;
  13. use backendApi\modules\v1\models\lists\shop\DecOrderList;
  14. use backendApi\modules\v1\models\lists\shop\GoodsList;
  15. use backendApi\modules\v1\models\lists\shop\OrderDecList;
  16. use backendApi\modules\v1\models\lists\shop\OrderList;
  17. use backendApi\modules\v1\models\lists\shop\BaOrderList;
  18. use backendApi\modules\v1\models\lists\shop\OrderPeriodAdjustList;
  19. use backendApi\modules\v1\models\lists\shop\OrderShopList;
  20. use backendApi\modules\v1\models\lists\shop\OrderStandardList;
  21. use backendApi\modules\v1\models\lists\shop\PackageList;
  22. use backendApi\modules\v1\models\lists\shop\RemainPvList;
  23. use backendApi\modules\v1\models\lists\shop\FlowRemainPvList;
  24. use common\helpers\Cache;
  25. use common\helpers\Date;
  26. use common\helpers\Form;
  27. use common\helpers\LoggerTool;
  28. use common\models\Countries;
  29. use common\models\CurrencyConversions;
  30. use common\models\DeclarationPackage;
  31. use common\models\DecOrder;
  32. use common\models\FlowWallet;
  33. use common\models\forms\DecPackageForm;
  34. use common\models\forms\ExcelOrderDecForm;
  35. use common\models\forms\ExcelOrderShopForm;
  36. use common\models\forms\ExcelOrderStandardForm;
  37. use common\models\forms\OrderDeleteForm;
  38. use common\models\forms\OrderForm;
  39. use common\models\forms\OrderPeriodAdjustForm;
  40. use common\models\forms\ShopGoodsForm;
  41. use common\models\forms\UploadForm;
  42. use common\models\Order;
  43. use common\models\OrderPeriodAdjust;
  44. use common\models\Period;
  45. use common\models\ShopGoods;
  46. use common\models\ShopGoodsNature;
  47. use common\models\User;
  48. use common\models\UserInfo;
  49. use Yii;
  50. use yii\web\HttpException;
  51. use yii\web\UploadedFile;
  52. use yii\base\Exception;
  53. class ShopController extends BaseController {
  54. public $modelClass = DecOrder::class;
  55. public function behaviors() {
  56. $behaviors = parent::behaviors();
  57. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  58. return $behaviors;
  59. }
  60. /**
  61. * 商品列表
  62. * @return mixed
  63. * @throws Exception
  64. * @throws HttpException
  65. */
  66. public function actionIndex() {
  67. $filter = $this->filterCondition([
  68. 'TYPE'=> 'TYPE',
  69. 'GIFT_TYPE'=> 'GIFT_TYPE',
  70. 'STATUS'=> 'STATUS',
  71. 'GOODS_NAME'=> 'GOODS_NAME',
  72. 'GOODS_NO'=> 'GOODS_NO',
  73. 'SELL_TYPE'=> 'SELL_TYPE',
  74. 'PRICE_PV'=> 'PRICE_PV',
  75. 'CATEGORY_TYPE' => 'CATEGORY_TYPE',
  76. 'AUTO_MAINTENANCE' => 'AUTO_MAINTENANCE',
  77. ]);
  78. $condition = $filter['condition'];
  79. $params = $filter['params'];
  80. $condition .= ' ';
  81. $listObj = new GoodsList();
  82. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  83. return static::notice($data);
  84. }
  85. /**
  86. * 商品属性
  87. * @return mixed
  88. * @throws Exception
  89. * @throws HttpException
  90. */
  91. public function actionGoodsNature() {
  92. $data = ShopGoodsNature::findAllAsArray('GOODS_ID=:GOODS_ID', [':GOODS_ID' => \Yii::$app->request->get('GOOD_ID')]);
  93. return static::notice($data);
  94. }
  95. /**
  96. * 商品列表导出
  97. * @return mixed
  98. * @throws \yii\db\Exception
  99. * @throws HttpException
  100. */
  101. public function actionGoodsListExport()
  102. {
  103. $filter = $this->filterCondition([
  104. 'TYPE'=> 'TYPE',
  105. 'GIFT_TYPE'=> 'GIFT_TYPE',
  106. 'STATUS'=> 'STATUS',
  107. 'GOODS_NAME'=> 'GOODS_NAME',
  108. 'GOODS_NO'=> 'GOODS_NO',
  109. 'SELL_TYPE'=> 'SELL_TYPE',
  110. 'PRICE_PV'=> 'PRICE_PV',
  111. 'CATEGORY_TYPE' => 'CATEGORY_TYPE',
  112. 'AUTO_MAINTENANCE' => 'AUTO_MAINTENANCE',
  113. ]);
  114. $form = new ShopExportForm();
  115. $result = $form->run($filter, \Yii::t('ctx', 'shopExportListName')); // 商品列表
  116. if (!$result) {
  117. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  118. }
  119. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  120. }
  121. /**
  122. * 添加商品
  123. * @return mixed
  124. * @throws Exception
  125. * @throws HttpException
  126. */
  127. public function actionGoodsAdd() {
  128. if (\Yii::$app->request->isPost) {
  129. $formModel = new ShopGoodsForm();
  130. $formModel->scenario = 'add';
  131. if ($formModel->load(\Yii::$app->request->post(), '') && $result = $formModel->add()) {
  132. return static::notice(\Yii::t('ctx', 'shopProductAddSucceededNotice'));
  133. } else {
  134. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  135. }
  136. }
  137. $countries = Cache::getCountries();
  138. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  139. if (!$isSuper) {
  140. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  141. $adminCountry = AdminCountry::getCountry($adminId);
  142. $countries = array_filter($countries, fn($country) => in_array($country['ID'], $adminCountry));
  143. $countries = array_values($countries);
  144. }
  145. return static::notice([
  146. 'sellType' => ShopGoods::getSaleType(),
  147. 'goodsType' => ShopGoods::getGoodType(),
  148. 'giftType' => ShopGoods::getGiftType(),
  149. 'categoryType' => ShopGoods::getCategoryType(),
  150. 'countries' => $countries,
  151. ]);
  152. }
  153. /**
  154. * 编辑商品
  155. * @return mixed
  156. * @throws Exception
  157. * @throws HttpException
  158. */
  159. public function actionGoodsEdit() {
  160. $id = \Yii::$app->request->get('id');
  161. if (\Yii::$app->request->isPost) {
  162. $formModel = new ShopGoodsForm();
  163. $formModel->scenario = 'edit';
  164. $formModel->id = $id;
  165. if ($formModel->load(\Yii::$app->request->post(), '') && $result = $formModel->edit()) {
  166. return static::notice(\Yii::t('ctx', 'shopProductEditSucceed'));
  167. } else {
  168. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  169. }
  170. }
  171. $data = ShopGoods::findOneAsArray('ID=:ID', [':ID' => $id]);
  172. $data['SELL_TYPE'] = explode(',',$data['SELL_TYPE']);
  173. $data['GIFT_TYPE'] = explode(',',$data['GIFT_TYPE']);
  174. if ($data['TYPE'] == 1 || $data['TYPE'] == 2) {
  175. $data['SELL_DISCOUNT'] = ShopGoods::getGoodType()[$data['TYPE']]['discount']/100;
  176. }
  177. // 国家
  178. $countries = Cache::getCountries();
  179. // 货币
  180. $currencies = Cache::getCurrencies();
  181. $currencies = array_column($currencies, NULL, 'ID');
  182. // 货币汇率
  183. $currenciesConversions = CurrencyConversions::getFromCache();
  184. $currenciesConversions = array_column($currenciesConversions, NULL, 'TO_CURRENCY_ID');
  185. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  186. if (!$isSuper) {
  187. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  188. $adminCountry = AdminCountry::getCountry($adminId);
  189. $countries = array_filter($countries, fn($country) => in_array($country['ID'], $adminCountry));
  190. $countries = array_values($countries);
  191. }
  192. foreach ($countries as $key => $country) {
  193. // 货币
  194. $countries[$key]['LOCAL_CURRENCY_NAME'] = $currencies[$country['LOCAL_CURRENCY_ID']]['NAME'] ?? '';
  195. $countries[$key]['CURRENCY_PRODUCT_RATE'] = $currenciesConversions[$country['LOCAL_CURRENCY_ID']]['PRODUCT_RATE'] ?? 0;
  196. // 商品属性
  197. $countries[$key]['SHOP_GOODS_NATURE'] = ShopGoodsNature::findAllAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID', [':GOODS_ID' => $id, ':COUNTRY_ID' => $country['ID']]);
  198. }
  199. $data['SHOP_GOODS_NATURE'] = array_values($countries);
  200. return static::notice(
  201. [
  202. 'goodsInfo'=>$data,
  203. 'sellType' => ShopGoods::getSaleType(),
  204. 'goodsType' => ShopGoods::getGoodType(),
  205. 'giftType' => ShopGoods::getGiftType(),//ShopGoods::GIFT_TYPE,
  206. 'categoryType' => ShopGoods::getCategoryType(),//ShopGoods::CATEGORY_TYPE,
  207. ]
  208. );
  209. }
  210. /**
  211. * 上传图片
  212. * @return mixed
  213. * @throws \yii\base\Exception
  214. * @throws \yii\db\Exception
  215. * @throws HttpException
  216. */
  217. public function actionUpload(){
  218. if(\Yii::$app->request->isPost){
  219. $formModel = new UploadForm();
  220. $formModel->scenario = 'goodsImg';
  221. $formModel->file = UploadedFile::getInstanceByName('file');
  222. $formModel->token = \Yii::$app->request->request('uploadToken');
  223. if($formModel->file && $uploader = $formModel->upload()){
  224. return static::notice($uploader->URL);
  225. } else {
  226. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  227. }
  228. } else {
  229. $token = Cache::setUploadToken();
  230. return static::notice($token);
  231. }
  232. }
  233. /**
  234. * 商品上下架
  235. * @return mixed
  236. * @throws Exception
  237. */
  238. public function actionGoodsStatus() {
  239. $id = \Yii::$app->request->get('id');
  240. if (\Yii::$app->request->isPost) {
  241. $formModel = new ShopGoodsForm();
  242. $formModel->scenario = 'changeStatus';
  243. if ($formModel->load(\Yii::$app->request->post(), '') && $result = $formModel->changeStatus()) {
  244. return static::notice(\Yii::t('ctx', 'shopStatusSetSucceedNotice')); // 状态设置成功
  245. } else {
  246. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  247. }
  248. }
  249. }
  250. /**
  251. * 删除商品
  252. * @return mixed
  253. * @throws \yii\db\Exception
  254. * @throws HttpException
  255. */
  256. public function actionGoodsDelete(){
  257. $result = static::delete(ShopGoods::class);
  258. return $result;
  259. }
  260. /**
  261. * 套餐列表
  262. * @return mixed
  263. * @throws Exception
  264. * @throws HttpException
  265. */
  266. public function actionPackage() {
  267. $filter = $this->filterCondition([
  268. 'LEVEL_NAME'=> 'DP.LEVEL_ID',
  269. 'PACKAGE_NAME'=> 'DP.PACKAGE_NAME',
  270. 'AMOUNT'=> 'DP.AMOUNT',
  271. 'PV'=> 'DP.PV',
  272. 'STATUS'=> 'DP.STATUS',
  273. 'STORE_NUMS'=>'DP.STORE_NUMS',
  274. ]);
  275. $condition = $filter['condition'];
  276. $params = $filter['params'];
  277. $condition .= ' AND DP.IS_DEL=0';
  278. $listObj = new PackageList();
  279. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  280. return static::notice($data);
  281. }
  282. /**
  283. * 添加报单套餐
  284. * @return mixed
  285. * @throws HttpException
  286. */
  287. public function actionPackageAdd() {
  288. if (\Yii::$app->request->isPost) {
  289. return static::edit(DecPackageForm::class, '报单套餐添加成功', 'add', null, null, function () {
  290. // Log::adminHandle('添加报单套餐', 1);
  291. });
  292. }
  293. return static::notice('非法请求', 405);
  294. }
  295. /**
  296. * 报单套餐获取
  297. * @return mixed
  298. * @throws HttpException
  299. */
  300. public function actionPackageGet() {
  301. $id = \Yii::$app->request->get('id');
  302. if (\Yii::$app->request->isPost) {
  303. return static::edit(DecPackageForm::class, '套餐编辑成功', 'edit', null, null, function () {
  304. // Log::adminHandle('编辑报单套餐', 1);
  305. });
  306. }
  307. $package = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $id]);
  308. return static::notice(['id' => $package['ID'],'packageName' => $package['PACKAGE_NAME'],'packageNo' => $package['PACKAGE_NO'], 'amount' => $package['AMOUNT'], 'amountPv' => $package['PV'], 'levelId' => $package['LEVEL_ID'], 'packageContent' => $package['PACKAGE_CONTENT'],'amountStandard' => $package['AMOUNT_STANDARD'],'storenums' => $package['STORE_NUMS']]);
  309. }
  310. /**
  311. * 套餐上下架
  312. * @return mixed
  313. * @throws Exception
  314. */
  315. public function actionPackageStatus() {
  316. $id = \Yii::$app->request->get('id');
  317. if (\Yii::$app->request->isPost) {
  318. $formModel = new DecPackageForm();
  319. $formModel->scenario = 'changeStatus';
  320. if ($formModel->load(\Yii::$app->request->post(), '') && $result = $formModel->changeStatus()) {
  321. return static::notice('Status setting succeeded'); // 状态设置成功
  322. } else {
  323. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  324. }
  325. }
  326. }
  327. /**
  328. * 删除套餐
  329. * @return mixed
  330. * @throws \yii\db\Exception
  331. * @throws HttpException
  332. */
  333. public function actionPackageDelete(){
  334. $result = static::delete(DeclarationPackage::class);
  335. return $result;
  336. }
  337. /**
  338. * 报单表
  339. * @return mixed
  340. * @throws Exception
  341. * @throws HttpException
  342. */
  343. public function actionDecOrderList() {
  344. $filter = $this->filterCondition([
  345. 'DEC_SN'=> 'DO.DEC_SN',
  346. 'ORDER_SN'=> 'DO.ORDER_SN',
  347. 'USER_NAME'=> 'U.USER_NAME',
  348. 'TO_USER_NAME'=> 'TU.USER_NAME',
  349. 'REAL_NAME'=> 'TU.REAL_NAME',
  350. 'ID_CARD'=> 'TU.ID_CARD',
  351. 'MOBILE'=> 'TU.MOBILE',
  352. 'DEC_LV_NAME' => 'TU.DEC_LV',
  353. 'DEC_REAL_NAME'=> 'DU.REAL_NAME',
  354. 'DEC_AMOUNT'=> 'DO.DEC_AMOUNT',
  355. 'DEC_PV'=> 'DO.DEC_PV',
  356. 'PERIOD_NUM'=> 'DO.PERIOD_NUM',
  357. 'REC_USER_NAME'=> 'RU.USER_NAME',
  358. 'CREATED_AT'=> 'DO.CREATED_AT',
  359. 'UPDATED_AT'=> 'DO.UPDATED_AT',
  360. ]);
  361. $condition = $filter['condition'];
  362. $params = $filter['params'];
  363. $condition .= ' AND DO.IS_DEL=0';
  364. $listObj = new DecOrderList();
  365. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  366. return static::notice($data);
  367. }
  368. /**
  369. * 报单表导出
  370. * @return mixed
  371. * @throws \yii\db\Exception
  372. * @throws HttpException
  373. */
  374. public function actionDecOrderListExport()
  375. {
  376. $filter = $this->filterCondition([
  377. 'DEC_SN'=> 'DO.DEC_SN',
  378. 'ORDER_SN'=> 'DO.ORDER_SN',
  379. 'USER_NAME'=> 'U.USER_NAME',
  380. 'TO_USER_NAME'=> 'TU.USER_NAME',
  381. 'DEC_AMOUNT'=> 'DO.DEC_AMOUNT',
  382. 'DEC_PV'=> 'DO.DEC_PV',
  383. 'PERIOD_NUM'=> 'DO.PERIOD_NUM',
  384. 'CREATED_AT'=> 'DO.CREATED_AT',
  385. 'UPDATED_AT'=> 'DO.UPDATED_AT',
  386. ]);
  387. $filter['condition'] .= ' AND DO.IS_DEL=0';
  388. $form = new ShopExportForm();
  389. $result = $form->run($filter, \Yii::t('ctx', 'shopDecOrderListExport'));
  390. if (!$result) {
  391. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  392. }
  393. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  394. }
  395. // 删除订单
  396. public function actionDeleteOrder() {
  397. if (\Yii::$app->request->isPost) {
  398. $formModel = new OrderDeleteForm();
  399. if ($formModel->load(\Yii::$app->request->post(), '') && $result = $formModel->returnOrder()) {
  400. return static::notice(\Yii::t('ctx', 'shopDelOrderSuccedNotice'));
  401. } else {
  402. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  403. }
  404. }
  405. }
  406. /**
  407. * 订单表
  408. * @return mixed
  409. * @throws Exception
  410. * @throws HttpException
  411. */
  412. public function actionOrderList() {
  413. $filter = $this->filterCondition([
  414. 'SN'=> 'O.SN',
  415. 'USER_NAME'=> 'U.USER_NAME',
  416. 'MOBILE'=> 'O.MOBILE',
  417. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  418. 'CREATED_AT'=> 'O.CREATED_AT',
  419. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  420. 'STATUS' => 'O.STATUS',
  421. 'COUNTRY' => 'O.COUNTRY_ID',
  422. 'AUTO_MAINTENANCE' => 'O.AUTO_MAINTENANCE'
  423. ]);
  424. $condition = ' 1=1 ' . $filter['condition'];
  425. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  426. if (!$isSuper) {
  427. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  428. $adminCountry = AdminCountry::getCountry($adminId);
  429. $quotedAdminCountry = array_map(function($item) {
  430. return "'" . addslashes($item) . "'";
  431. }, $adminCountry);
  432. $condition = ' 1=1 ' . $filter['condition'] . " AND O.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
  433. }
  434. $params = $filter['params'];
  435. $condition .= $condition ? ' AND O.IS_DELETE=0' : ' O.IS_DELETE=0';
  436. $listObj = new OrderList();
  437. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  438. return static::notice($data);
  439. }
  440. /**
  441. * 订单表导出
  442. * @return mixed
  443. * @throws \yii\db\Exception
  444. * @throws HttpException
  445. */
  446. public function actionOrderListExport()
  447. {
  448. $filter = $this->filterCondition([
  449. 'SN'=> 'O.SN',
  450. 'USER_NAME'=> 'U.USER_NAME',
  451. 'MOBILE'=> 'O.MOBILE',
  452. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  453. 'CREATED_AT'=> 'O.CREATED_AT',
  454. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  455. 'STATUS' => 'O.STATUS',
  456. 'AUTO_MAINTENANCE' => 'O.AUTO_MAINTENANCE'
  457. ]);
  458. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  459. if (!$isSuper) {
  460. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  461. $adminCountry = AdminCountry::getCountry($adminId);
  462. $quotedAdminCountry = array_map(function($item) {
  463. return "'" . addslashes($item) . "'";
  464. }, $adminCountry);
  465. $filter['condition'] = ('O.IS_DELETE=0 ' . $filter['condition']) . " AND O.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
  466. } else {
  467. $filter['condition'] = ('O.IS_DELETE=0 ' . $filter['condition']);
  468. }
  469. $form = new ShopExportForm();
  470. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExport')); // 订单列表
  471. if (!$result) {
  472. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  473. }
  474. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  475. }
  476. /**
  477. * 外部商城订单列表
  478. * @return mixed
  479. * @throws Exception
  480. * @throws HttpException
  481. */
  482. public function actionOrderShopList() {
  483. $filter = $this->filterCondition([
  484. 'SN'=> 'O.SN',
  485. 'USER_NAME'=> 'U.USER_NAME',
  486. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  487. 'CREATED_AT'=> 'O.CREATED_AT',
  488. ]);
  489. $condition = $filter['condition'];
  490. $params = $filter['params'];
  491. $condition .= ' AND O.IS_DELETE=0';
  492. $listObj = new OrderShopList();
  493. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  494. return static::notice($data);
  495. }
  496. /**
  497. * 外部商城报单列表
  498. * @return mixed
  499. * @throws Exception
  500. * @throws HttpException
  501. */
  502. public function actionOrderDecList() {
  503. $filter = $this->filterCondition([
  504. 'SN'=> 'O.SN',
  505. 'USER_NAME'=> 'U.USER_NAME',
  506. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  507. 'CREATED_AT'=> 'O.CREATED_AT',
  508. ]);
  509. $condition = $filter['condition'];
  510. $params = $filter['params'];
  511. $condition .= ' AND O.IS_DELETE=0';
  512. $listObj = new OrderDecList();
  513. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  514. return static::notice($data);
  515. }
  516. /**
  517. * 分页导入excel文件到待导入数据的表中
  518. * @return mixed
  519. */
  520. public function actionImportOrderShopToExcelTable()
  521. {
  522. // 先上传到服务器文件
  523. if (\Yii::$app->request->isPost) {
  524. $excelImportId = \Yii::$app->request->post('excelImportId');
  525. $rowCount = \Yii::$app->request->post('rowCount');
  526. //$periodNum = \Yii::$app->request->post('periodNum');
  527. $orderDay = \Yii::$app->request->post('orderDay');
  528. $orderType = \Yii::$app->request->post('orderType');
  529. $startRow = \Yii::$app->request->post('startRow');
  530. $limit = \Yii::$app->request->post('limit', 1000);
  531. $errorMsg = '';
  532. try {
  533. if( $startRow == 1 ) {
  534. $excelOrderShop = new ExcelOrderShopForm();
  535. $excelOrderShop->checkStatus($orderDay,$orderType);
  536. }
  537. $excel = new \common\helpers\Excel();
  538. $result = $excel->pageImportCustomDataFromExcel('orderShop', $excelImportId, $rowCount, $startRow, $limit,$orderDay,$orderType);
  539. } catch (\Exception $e) {
  540. $result = false;
  541. $errorMsg = $e->getMessage();
  542. }
  543. // 还有数据
  544. if ($result === 1) {
  545. return static::notice(['finish' => false]);
  546. } elseif ($result === 0) {
  547. return static::notice(['finish' => true]);
  548. } else {
  549. return static::notice('Error:' . $errorMsg, 400);
  550. }
  551. }
  552. }
  553. /**
  554. * 导入完成标记
  555. * @return mixed
  556. * @throws HttpException
  557. */
  558. public function actionImportOrderShop() {
  559. //$periodNum = \Yii::$app->request->post('periodNum');
  560. $orderDay = \Yii::$app->request->post('orderDay');
  561. $orderType = \Yii::$app->request->post('orderType');
  562. $excelOrderShop = new ExcelOrderShopForm();
  563. $finishStatus = $excelOrderShop->finished($orderDay,$orderType);
  564. if ($finishStatus) {
  565. return static::notice(['finish' => true]);
  566. } else {
  567. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  568. }
  569. }
  570. /**
  571. * 报单订单——分页导入excel文件到待导入数据的表中
  572. * @return mixed
  573. */
  574. public function actionImportOrderDecToExcelTable()
  575. {
  576. // 先上传到服务器文件
  577. if (\Yii::$app->request->isPost) {
  578. $excelImportId = \Yii::$app->request->post('excelImportId');
  579. $rowCount = \Yii::$app->request->post('rowCount');
  580. //$periodNum = \Yii::$app->request->post('periodNum');
  581. $orderDay = \Yii::$app->request->post('orderDay');
  582. $startRow = \Yii::$app->request->post('startRow');
  583. $limit = \Yii::$app->request->post('limit', 1000);
  584. $errorMsg = '';
  585. try {
  586. if( $startRow == 1 ) {
  587. $excelOrderShop = new ExcelOrderDecForm();
  588. $excelOrderShop->checkStatus($orderDay);
  589. }
  590. $excel = new \common\helpers\Excel();
  591. $result = $excel->pageImportCustomDataFromExcel('orderDec', $excelImportId, $rowCount, $startRow, $limit,$orderDay);
  592. } catch (\Exception $e) {
  593. $result = false;
  594. $errorMsg = $e->getMessage();
  595. }
  596. // 还有数据
  597. if ($result === 1) {
  598. return static::notice(['finish' => false]);
  599. } elseif ($result === 0) {
  600. return static::notice(['finish' => true]);
  601. } else {
  602. return static::notice('Error:' . $errorMsg, 400);
  603. }
  604. }
  605. }
  606. /**
  607. * 报单订单导入完成标记
  608. * @return mixed
  609. * @throws HttpException
  610. */
  611. public function actionImportOrderDec() {
  612. //$periodNum = \Yii::$app->request->post('periodNum');
  613. $orderDay = \Yii::$app->request->post('orderDay');
  614. $excelOrderShop = new ExcelOrderDecForm();
  615. $finishStatus = $excelOrderShop->finished($orderDay);
  616. if ($finishStatus) {
  617. return static::notice(['finish' => true]);
  618. } else {
  619. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  620. }
  621. }
  622. /**
  623. * 见习达标订单列表
  624. * @return mixed
  625. * @throws Exception
  626. * @throws HttpException
  627. */
  628. public function actionOrderStandardList() {
  629. $filter = $this->filterCondition([
  630. 'SN'=> 'O.SN',
  631. 'USER_NAME'=> 'U.USER_NAME',
  632. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  633. 'CREATED_AT'=> 'O.CREATED_AT',
  634. ]);
  635. $condition = $filter['condition'];
  636. $params = $filter['params'];
  637. $condition .= ' AND O.IS_DELETE=0';
  638. $listObj = new OrderStandardList();
  639. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  640. return static::notice($data);
  641. }
  642. /**
  643. * 分页导入excel文件到待导入数据的表中
  644. * @return mixed
  645. * @throws HttpException
  646. */
  647. public function actionImportOrderStandardToExcelTable()
  648. {
  649. // 先上传到服务器文件
  650. if (\Yii::$app->request->isPost) {
  651. $excelImportId = \Yii::$app->request->post('excelImportId');
  652. $rowCount = \Yii::$app->request->post('rowCount');
  653. //$periodNum = \Yii::$app->request->post('periodNum');
  654. $orderMonth = \Yii::$app->request->post('orderMonth');
  655. // $orderType = \Yii::$app->request->post('orderType');
  656. $orderType = 'cash';
  657. $startRow = \Yii::$app->request->post('startRow');
  658. $limit = \Yii::$app->request->post('limit', 1000);
  659. $errorMsg = '';
  660. try {
  661. if( $startRow == 1 ) {
  662. //清空已经导入的表数据
  663. $excelOrderStandard = new ExcelOrderStandardForm();
  664. $excelOrderStandard->checkStatus($orderMonth);
  665. }
  666. $excel = new \common\helpers\Excel();
  667. $result = $excel->pageImportCustomDataFromExcel('orderShopStandard', $excelImportId, $rowCount, $startRow, $limit,$orderMonth,$orderType);
  668. } catch (Exception $e) {
  669. $result = false;
  670. $errorMsg = $e->getMessage();
  671. }
  672. // 还有数据
  673. if ($result === 1) {
  674. return static::notice(['finish' => false]);
  675. } elseif ($result === 0) {
  676. return static::notice(['finish' => true]);
  677. } else {
  678. return static::notice('Error:' . $errorMsg, 400);
  679. }
  680. }
  681. }
  682. /**
  683. * 达标订单导入完成标记
  684. * @return mixed
  685. * @throws HttpException
  686. */
  687. public function actionImportOrderStandard() {
  688. $orderMonth = \Yii::$app->request->post('orderMonth');
  689. $excelOrderStandard = new ExcelOrderStandardForm();
  690. $finishStatus = $excelOrderStandard->finished($orderMonth);
  691. if ($finishStatus) {
  692. return static::notice(['finish' => true]);
  693. } else {
  694. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  695. }
  696. }
  697. /**
  698. * 管理员发货
  699. * @return mixed
  700. * @throws HttpException
  701. */
  702. public function actionOrderDelivery() {
  703. if(\Yii::$app->request->isPost) {
  704. return parent::edit(OrderForm::class, '发货成功', 'adminDelivery', ['adminDelivery']);
  705. }
  706. return static::notice(\Yii::t('ctx', 'illegalRequest'), 400); // 非法请求
  707. }
  708. /**
  709. * 报单表导出
  710. * @return mixed
  711. * @throws \yii\db\Exception
  712. * @throws HttpException
  713. */
  714. public function actionOrderListExportPdf()
  715. {
  716. $orderSn = \Yii::$app->request->get('orderSn');
  717. $filter = $this->filterCondition([
  718. 'SN'=> 'O.SN',
  719. 'USER_NAME'=> 'U.USER_NAME',
  720. 'MOBILE'=> 'O.MOBILE',
  721. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  722. 'CREATED_AT'=> 'O.CREATED_AT',
  723. ]);
  724. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  725. $filter['params'] = [':SN' => $orderSn];
  726. $form = new ShopExportForm();
  727. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExportPdf'));
  728. if (!$result) {
  729. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  730. }
  731. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  732. }
  733. /**
  734. * 报单表导出
  735. * @return mixed
  736. * @throws \yii\db\Exception
  737. * @throws HttpException
  738. */
  739. public function actionDecOrderListExportPdf()
  740. {
  741. $orderSn = \Yii::$app->request->get('orderSn');
  742. $filter = $this->filterCondition([
  743. 'SN'=> 'O.SN',
  744. 'USER_NAME'=> 'U.USER_NAME',
  745. 'MOBILE'=> 'O.MOBILE',
  746. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  747. 'CREATED_AT'=> 'O.CREATED_AT',
  748. ]);
  749. $filter['condition'] = ' 1=1 AND O.IS_DELETE=0 AND O.SN=:SN';
  750. $filter['params'] = [':SN' => $orderSn];
  751. $form = new ShopExportForm();
  752. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExportPdf'));
  753. if (!$result) {
  754. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  755. }
  756. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  757. }
  758. /**
  759. * 管理员订单退款
  760. * @return mixed
  761. * @throws HttpException
  762. */
  763. public function actionOrderRefund()
  764. {
  765. if(\Yii::$app->request->isPost) {
  766. return parent::edit(OrderForm::class, \Yii::t('ctx', 'shopadminRefund'), 'adminRefund', ['adminRefund']);
  767. }
  768. return static::notice(\Yii::t('ctx', 'illegalRequest'), 400); // 非法请求
  769. }
  770. /*
  771. * 剩余BV页
  772. *
  773. */
  774. public function actionRemainPv()
  775. {
  776. $filter = $this->filterCondition([
  777. 'USER_NAME' => 'U.USER_NAME',
  778. ]);
  779. $condition = $filter['condition'];
  780. $params = $filter['params'];
  781. $listObj = new RemainPvList();
  782. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  783. return static::notice($data);
  784. }
  785. /*
  786. * 剩余BV流水
  787. *
  788. */
  789. public function actionFlowRemainPv()
  790. {
  791. $filter = $this->filterCondition([
  792. 'USER_NAME' => 'U.USER_NAME',
  793. ]);
  794. $condition = $filter['condition'];
  795. $params = $filter['params'];
  796. $listObj = new FlowRemainPvList();
  797. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  798. return static::notice($data);
  799. }
  800. /**
  801. * BA订单表
  802. * @return mixed
  803. * @throws Exception
  804. * @throws HttpException
  805. */
  806. public function actionBaOrderList() {
  807. $filter = $this->filterCondition([
  808. 'SN'=> 'O.SN',
  809. 'USER_NAME'=> 'U.USER_NAME',
  810. 'MOBILE'=> 'O.MOBILE',
  811. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  812. 'CREATED_AT'=> 'O.CREATED_AT',
  813. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  814. 'STATUS' => 'O.STATUS',
  815. ]);
  816. $condition = ' 1=1 ' . $filter['condition'];
  817. $params = $filter['params'];
  818. $condition .= $condition ? ' AND O.IS_DELETE=0' : ' O.IS_DELETE=0';
  819. $listObj = new BaOrderList();
  820. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  821. return static::notice($data);
  822. }
  823. /**
  824. * BA订单表导出
  825. * @return mixed
  826. * @throws \yii\db\Exception
  827. * @throws HttpException
  828. */
  829. public function actionBaOrderListExport()
  830. {
  831. $filter = $this->filterCondition([
  832. 'SN'=> 'O.SN',
  833. 'USER_NAME'=> 'U.USER_NAME',
  834. 'MOBILE'=> 'O.MOBILE',
  835. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  836. 'CREATED_AT'=> 'O.CREATED_AT',
  837. 'STATUS' => 'O.STATUS',
  838. ]);
  839. $filter['condition'] = !$filter['condition'] ? '1=1 AND O.IS_DELETE=0' : ('O.IS_DELETE=0 ' . $filter['condition']);
  840. $form = new BaShopExportForm();
  841. $result = $form->run($filter, \Yii::t('ctx', 'shopBaOrderListExport')); // 订单列表
  842. if (!$result) {
  843. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  844. }
  845. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  846. }
  847. /**
  848. * 报单表导出
  849. * @return mixed
  850. * @throws \yii\db\Exception
  851. * @throws HttpException
  852. */
  853. public function actionBaOrderListExportPdf()
  854. {
  855. $orderSn = \Yii::$app->request->get('orderSn');
  856. $filter = $this->filterCondition([
  857. 'SN'=> 'O.SN',
  858. 'USER_NAME'=> 'U.USER_NAME',
  859. 'MOBILE'=> 'O.MOBILE',
  860. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  861. 'CREATED_AT'=> 'O.CREATED_AT',
  862. ]);
  863. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  864. $filter['params'] = [':SN' => $orderSn];
  865. $form = new BaShopExportForm();
  866. $result = $form->run($filter, \Yii::t('ctx', 'shopBrandAmbassadorOrderExport'));
  867. if (!$result) {
  868. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  869. }
  870. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  871. }
  872. /**
  873. * 调整订单期数
  874. * @return mixed
  875. * @throws Exception
  876. * @throws \yii\db\Exception
  877. * @throws \yii\web\HttpException
  878. */
  879. public function actionOrderPeriodAdjust()
  880. {
  881. if (\Yii::$app->request->isPost) {
  882. $formModel = new OrderPeriodAdjustForm();
  883. $formModel->scenario = 'orderPeriodAdjust';
  884. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->periodAdjust()) {
  885. return static::notice(Yii::t('ctx', 'successfully'));
  886. } else {
  887. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  888. }
  889. } else {
  890. $filter = $this->filterCondition([
  891. 'ORDER_SN' => 'P.ORDER_SN',
  892. 'ORIGIN_PERIOD'=> 'P.ORIGIN_PERIOD',
  893. 'MODERN_PERIOD'=> 'P.MODERN_PERIOD',
  894. 'CREATED_AT'=> 'P.CREATED_AT',
  895. ]);
  896. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  897. if (!$isSuper) {
  898. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  899. $adminCountry = AdminCountry::getCountry($adminId);
  900. $quotedAdminCountry = array_map(function($item) {
  901. return "'" . addslashes($item) . "'";
  902. }, $adminCountry);
  903. $filter['condition'] .= ' AND O.COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')';
  904. }
  905. $listObj = new OrderPeriodAdjustList();
  906. $data = $listObj->getList(['condition' => $filter['condition'], 'params' => $filter['params']]);
  907. return static::notice($data);
  908. }
  909. }
  910. /**
  911. * 订单详情
  912. * @return mixed
  913. * @throws \yii\web\HttpException
  914. */
  915. public function actionOrderDetail()
  916. {
  917. $orderSn = \Yii::$app->request->get('orderSn');
  918. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  919. if (!$isSuper) {
  920. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  921. $adminCountry = AdminCountry::getCountry($adminId);
  922. $quotedAdminCountry = array_map(function ($item) {
  923. return "'" . addslashes($item) . "'";
  924. }, $adminCountry);
  925. $order = Order::findOneAsArray('SN=:SN AND COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')', [':SN' => $orderSn]);
  926. } else {
  927. $order = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  928. }
  929. if (!$order) {
  930. return static::notice(Yii::t('ctx', 'orderNotExist'), 400);
  931. }
  932. $order['ORDER_TYPE'] = $order['ORDER_TYPE'] === 'ZC' ? Yii::t('ctx', 'shopOrderListOrderTypeZc') : Yii::t('ctx', 'shopOrderListOrderTypeFx');
  933. $order['COUNTRY'] = Countries::getById($order['COUNTRY_ID'])['NAME'] ?? '';
  934. // 已挂网,不可调整
  935. $period = Period::getInfoByPeriodNum($order['PERIOD_NUM']);
  936. if (!$period) {
  937. return static::notice(Yii::t('ctx', 'invalidPcNo'), 400);
  938. }
  939. if ($period['IS_SENT'] > 0) {
  940. return static::notice(Yii::t('ctx', 'orderHasBeenConnected'), 400);
  941. }
  942. // 可调整期数
  943. $availablePeriod = [];
  944. if ($order['PERIOD_NUM']) {
  945. $availablePeriod = Period::getPeriodList(+$order['PERIOD_NUM'] - 1);
  946. foreach ($availablePeriod as $k => $item) {
  947. $availablePeriod[$k]['disabled'] = (+$item['IS_SENT'] > 0) || ($order['PERIOD_NUM'] == $item['PERIOD_NUM']);
  948. }
  949. }
  950. return static::notice(['order' => $order, 'availablePeriod' => $availablePeriod]);
  951. }
  952. public function actionOrderPeriodAdjustBatch()
  953. {
  954. $db = \Yii::$app->db;
  955. $transaction = $db->beginTransaction();
  956. $orderList = [];
  957. $orderErr = [];
  958. try {
  959. $modernPeriod = 150; // todo: 修改后期数
  960. $oldPeriod = 151; // todo: 修改前期数
  961. foreach ($orderList as $orderSn) {
  962. $orderObj = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  963. if (!$orderObj) {
  964. $orderErr[] = $orderSn;
  965. continue;
  966. }
  967. // 调整订单期数
  968. Order::updateAll(['PERIOD_NUM' => $modernPeriod], 'SN = :SN', [':SN' => $orderSn]);
  969. if ($orderObj['ORDER_TYPE'] === 'ZC') {
  970. // 调整报单期数
  971. DecOrder::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  972. // 调整会员的期数
  973. User::updateAll(['PERIOD_AT' => $modernPeriod], 'ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  974. UserInfo::updateAll(['HIGHEST_EMP_LV_PERIOD' => $modernPeriod], 'USER_ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  975. }
  976. // 调整流水的期数
  977. switch ($orderObj['PAY_TYPE']){
  978. case 'cash':
  979. FlowWallet::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  980. break;
  981. default:
  982. break;
  983. }
  984. // 写入调整记录
  985. $orderModel = new OrderPeriodAdjust();
  986. $orderModel->ORDER_SN = $orderSn;
  987. $orderModel->ORIGIN_PERIOD = $oldPeriod;
  988. $orderModel->MODERN_PERIOD = $modernPeriod;
  989. $orderModel->ADMIN_ID = 'admin';
  990. $orderModel->CREATED_AT = Date::nowTime();
  991. if (!$orderModel->save()) {
  992. return static::notice($orderModel->getErrors(), 400);
  993. }
  994. }
  995. $transaction->commit();
  996. } catch(\Exception $e) {
  997. $transaction->rollBack();
  998. return static::notice($e->getMessage(), 400);
  999. }
  1000. return static::notice('成功. ' . json_encode($orderErr));
  1001. }
  1002. }