ShopController.php 39 KB

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