ShopController.php 43 KB

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