ShopController.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. LoggerTool::info('生成发票号SN:' . $orderSn);
  770. $order = Order::find()->where(['SN' => $orderSn])->asArray()->one();
  771. if (!$order) {
  772. return static::notice('订单不存在', 400);
  773. }
  774. if (!$order['INVOICE_NO']) {
  775. $invoiceNo = $this->generateInvoiceNo();
  776. LoggerTool::info('生成发票号:' . $invoiceNo);
  777. Order::updateAll(['INVOICE_NO' => $invoiceNo], 'SN = :SN', [':SN' => $orderSn]);
  778. }
  779. $form = new ShopExportForm();
  780. $result = $form->run($filter, \Yii::t('ctx', 'actionOrderInvoiceExport'));
  781. if (!$result) {
  782. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  783. }
  784. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  785. }
  786. /**
  787. * 报单表导出
  788. * @return mixed
  789. * @throws \yii\db\Exception
  790. * @throws HttpException
  791. */
  792. public function actionDecOrderListExportPdf()
  793. {
  794. $orderSn = \Yii::$app->request->get('orderSn');
  795. $filter = $this->filterCondition([
  796. 'SN'=> 'O.SN',
  797. 'USER_NAME'=> 'U.USER_NAME',
  798. 'MOBILE'=> 'O.MOBILE',
  799. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  800. 'CREATED_AT'=> 'O.CREATED_AT',
  801. ]);
  802. $filter['condition'] = ' 1=1 AND O.IS_DELETE=0 AND O.SN=:SN';
  803. $filter['params'] = [':SN' => $orderSn];
  804. $form = new ShopExportForm();
  805. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExportPdf'));
  806. if (!$result) {
  807. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  808. }
  809. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  810. }
  811. /**
  812. * 管理员订单退款
  813. * @return mixed
  814. * @throws HttpException
  815. */
  816. public function actionOrderRefund()
  817. {
  818. if(\Yii::$app->request->isPost) {
  819. return parent::edit(OrderForm::class, \Yii::t('ctx', 'shopadminRefund'), 'adminRefund', ['adminRefund']);
  820. }
  821. return static::notice(\Yii::t('ctx', 'illegalRequest'), 400); // 非法请求
  822. }
  823. /*
  824. * 剩余BV页
  825. *
  826. */
  827. public function actionRemainPv()
  828. {
  829. $filter = $this->filterCondition([
  830. 'USER_NAME' => 'U.USER_NAME',
  831. ]);
  832. $condition = $filter['condition'];
  833. $params = $filter['params'];
  834. $listObj = new RemainPvList();
  835. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  836. return static::notice($data);
  837. }
  838. /*
  839. * 剩余BV流水
  840. *
  841. */
  842. public function actionFlowRemainPv()
  843. {
  844. $filter = $this->filterCondition([
  845. 'USER_NAME' => 'U.USER_NAME',
  846. ]);
  847. $condition = $filter['condition'];
  848. $params = $filter['params'];
  849. $listObj = new FlowRemainPvList();
  850. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  851. return static::notice($data);
  852. }
  853. /**
  854. * BA订单表
  855. * @return mixed
  856. * @throws Exception
  857. * @throws HttpException
  858. */
  859. public function actionBaOrderList() {
  860. $filter = $this->filterCondition([
  861. 'SN'=> 'O.SN',
  862. 'USER_NAME'=> 'U.USER_NAME',
  863. 'MOBILE'=> 'O.MOBILE',
  864. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  865. 'CREATED_AT'=> 'O.CREATED_AT',
  866. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  867. 'STATUS' => 'O.STATUS',
  868. ]);
  869. $condition = ' 1=1 ' . $filter['condition'];
  870. $params = $filter['params'];
  871. $condition .= $condition ? ' AND O.IS_DELETE=0' : ' O.IS_DELETE=0';
  872. $listObj = new BaOrderList();
  873. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  874. return static::notice($data);
  875. }
  876. /**
  877. * BA订单表导出
  878. * @return mixed
  879. * @throws \yii\db\Exception
  880. * @throws HttpException
  881. */
  882. public function actionBaOrderListExport()
  883. {
  884. $filter = $this->filterCondition([
  885. 'SN'=> 'O.SN',
  886. 'USER_NAME'=> 'U.USER_NAME',
  887. 'MOBILE'=> 'O.MOBILE',
  888. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  889. 'CREATED_AT'=> 'O.CREATED_AT',
  890. 'STATUS' => 'O.STATUS',
  891. ]);
  892. $filter['condition'] = !$filter['condition'] ? '1=1 AND O.IS_DELETE=0' : ('O.IS_DELETE=0 ' . $filter['condition']);
  893. $form = new BaShopExportForm();
  894. $result = $form->run($filter, \Yii::t('ctx', 'shopBaOrderListExport')); // 订单列表
  895. if (!$result) {
  896. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  897. }
  898. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  899. }
  900. /**
  901. * 报单表导出
  902. * @return mixed
  903. * @throws \yii\db\Exception
  904. * @throws HttpException
  905. */
  906. public function actionBaOrderListExportPdf()
  907. {
  908. $orderSn = \Yii::$app->request->get('orderSn');
  909. $filter = $this->filterCondition([
  910. 'SN'=> 'O.SN',
  911. 'USER_NAME'=> 'U.USER_NAME',
  912. 'MOBILE'=> 'O.MOBILE',
  913. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  914. 'CREATED_AT'=> 'O.CREATED_AT',
  915. ]);
  916. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  917. $filter['params'] = [':SN' => $orderSn];
  918. $form = new BaShopExportForm();
  919. $result = $form->run($filter, \Yii::t('ctx', 'shopBrandAmbassadorOrderExport'));
  920. if (!$result) {
  921. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  922. }
  923. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  924. }
  925. /**
  926. * 调整订单期数
  927. * @return mixed
  928. * @throws Exception
  929. * @throws \yii\db\Exception
  930. * @throws \yii\web\HttpException
  931. */
  932. public function actionOrderPeriodAdjust()
  933. {
  934. if (\Yii::$app->request->isPost) {
  935. $formModel = new OrderPeriodAdjustForm();
  936. $formModel->scenario = 'orderPeriodAdjust';
  937. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->periodAdjust()) {
  938. return static::notice(Yii::t('ctx', 'successfully'));
  939. } else {
  940. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  941. }
  942. } else {
  943. $filter = $this->filterCondition([
  944. 'ORDER_SN' => 'P.ORDER_SN',
  945. 'ORIGIN_PERIOD'=> 'P.ORIGIN_PERIOD',
  946. 'MODERN_PERIOD'=> 'P.MODERN_PERIOD',
  947. 'CREATED_AT'=> 'P.CREATED_AT',
  948. ]);
  949. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  950. if (!$isSuper) {
  951. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  952. $adminCountry = AdminCountry::getCountry($adminId);
  953. $quotedAdminCountry = array_map(function($item) {
  954. return "'" . addslashes($item) . "'";
  955. }, $adminCountry);
  956. $filter['condition'] .= ' AND O.COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')';
  957. }
  958. $listObj = new OrderPeriodAdjustList();
  959. $data = $listObj->getList(['condition' => $filter['condition'], 'params' => $filter['params']]);
  960. return static::notice($data);
  961. }
  962. }
  963. /**
  964. * 订单详情
  965. * @return mixed
  966. * @throws \yii\web\HttpException
  967. */
  968. public function actionOrderDetail()
  969. {
  970. $orderSn = \Yii::$app->request->get('orderSn');
  971. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  972. if (!$isSuper) {
  973. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  974. $adminCountry = AdminCountry::getCountry($adminId);
  975. $quotedAdminCountry = array_map(function ($item) {
  976. return "'" . addslashes($item) . "'";
  977. }, $adminCountry);
  978. $order = Order::findOneAsArray('SN=:SN AND COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')', [':SN' => $orderSn]);
  979. } else {
  980. $order = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  981. }
  982. if (!$order) {
  983. return static::notice(Yii::t('ctx', 'orderNotExist'), 400);
  984. }
  985. $order['ORDER_TYPE'] = $order['ORDER_TYPE'] === 'ZC' ? Yii::t('ctx', 'shopOrderListOrderTypeZc') : Yii::t('ctx', 'shopOrderListOrderTypeFx');
  986. $order['COUNTRY'] = Countries::getById($order['COUNTRY_ID'])['NAME'] ?? '';
  987. // 已挂网,不可调整
  988. $period = Period::getInfoByPeriodNum($order['PERIOD_NUM']);
  989. if (!$period) {
  990. return static::notice(Yii::t('ctx', 'invalidPcNo'), 400);
  991. }
  992. if ($period['IS_SENT'] > 0) {
  993. return static::notice(Yii::t('ctx', 'orderHasBeenConnected'), 400);
  994. }
  995. // 可调整期数
  996. $availablePeriod = [];
  997. if ($order['PERIOD_NUM']) {
  998. $availablePeriod = Period::getPeriodList(+$order['PERIOD_NUM'] - 1);
  999. foreach ($availablePeriod as $k => $item) {
  1000. $availablePeriod[$k]['disabled'] = (+$item['IS_SENT'] > 0) || ($order['PERIOD_NUM'] == $item['PERIOD_NUM']);
  1001. }
  1002. }
  1003. return static::notice(['order' => $order, 'availablePeriod' => $availablePeriod]);
  1004. }
  1005. public function actionOrderPeriodAdjustBatch()
  1006. {
  1007. $db = \Yii::$app->db;
  1008. $transaction = $db->beginTransaction();
  1009. $orderList = [];
  1010. $orderErr = [];
  1011. try {
  1012. $modernPeriod = 150; // todo: 修改后期数
  1013. $oldPeriod = 151; // todo: 修改前期数
  1014. foreach ($orderList as $orderSn) {
  1015. $orderObj = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  1016. if (!$orderObj) {
  1017. $orderErr[] = $orderSn;
  1018. continue;
  1019. }
  1020. // 调整订单期数
  1021. Order::updateAll(['PERIOD_NUM' => $modernPeriod], 'SN = :SN', [':SN' => $orderSn]);
  1022. if ($orderObj['ORDER_TYPE'] === 'ZC') {
  1023. // 调整报单期数
  1024. DecOrder::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  1025. // 调整会员的期数
  1026. User::updateAll(['PERIOD_AT' => $modernPeriod], 'ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  1027. UserInfo::updateAll(['HIGHEST_EMP_LV_PERIOD' => $modernPeriod], 'USER_ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  1028. }
  1029. // 调整流水的期数
  1030. switch ($orderObj['PAY_TYPE']){
  1031. case 'cash':
  1032. FlowWallet::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  1033. break;
  1034. default:
  1035. break;
  1036. }
  1037. // 写入调整记录
  1038. $orderModel = new OrderPeriodAdjust();
  1039. $orderModel->ORDER_SN = $orderSn;
  1040. $orderModel->ORIGIN_PERIOD = $oldPeriod;
  1041. $orderModel->MODERN_PERIOD = $modernPeriod;
  1042. $orderModel->ADMIN_ID = 'admin';
  1043. $orderModel->CREATED_AT = Date::nowTime();
  1044. if (!$orderModel->save()) {
  1045. return static::notice($orderModel->getErrors(), 400);
  1046. }
  1047. }
  1048. $transaction->commit();
  1049. } catch(\Exception $e) {
  1050. $transaction->rollBack();
  1051. return static::notice($e->getMessage(), 400);
  1052. }
  1053. return static::notice('成功. ' . json_encode($orderErr));
  1054. }
  1055. public function generateInvoiceNo()
  1056. {
  1057. $prefix = 'Inv';
  1058. $currentDate = date('dmY');
  1059. // 查询今天生成的最新发票号(包含当天日期格式的)
  1060. $order = Order::find()
  1061. ->where(['STATUS' => 1, 'IS_DELETE' => 0])
  1062. ->andWhere(['like', 'INVOICE_NO', $prefix . $currentDate])
  1063. ->orderBy(['INVOICE_NO' => SORT_DESC])
  1064. ->one();
  1065. if (!$order || !$order->INVOICE_NO) {
  1066. // 如果今天没有生成过发票号或没有符合条件的订单,从00001开始
  1067. $no = '00001';
  1068. } else {
  1069. $invoiceNo = $order->INVOICE_NO;
  1070. // 截取最后5位数字部分并+1
  1071. $no = sprintf('%05d', intval(substr($invoiceNo, -5)) + 1);
  1072. }
  1073. return $prefix . $currentDate . $no;
  1074. }
  1075. }