ShopController.php 42 KB

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