ShopController.php 43 KB

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