ShopController.php 40 KB

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