ShopController.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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 actionOrderInvoiceMethod() {
  440. return static::notice(['method'=>['Cash', 'Credit Card', 'Direct Banking', 'Cheque']]);
  441. }
  442. /**
  443. * 订单表
  444. * @return mixed
  445. * @throws Exception
  446. * @throws HttpException
  447. */
  448. public function actionOrderList() {
  449. $filter = $this->filterCondition([
  450. 'SN'=> 'O.SN',
  451. 'USER_NAME'=> 'U.USER_NAME',
  452. 'MOBILE'=> 'O.MOBILE',
  453. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  454. 'CREATED_AT'=> 'O.CREATED_AT',
  455. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  456. 'STATUS' => 'O.STATUS',
  457. 'COUNTRY' => 'O.COUNTRY_ID',
  458. 'AUTO_MAINTENANCE' => 'O.AUTO_MAINTENANCE'
  459. ]);
  460. $condition = ' 1=1 ' . $filter['condition'];
  461. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  462. if (!$isSuper) {
  463. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  464. $adminCountry = AdminCountry::getCountry($adminId);
  465. $quotedAdminCountry = array_map(function($item) {
  466. return "'" . addslashes($item) . "'";
  467. }, $adminCountry);
  468. $condition = ' 1=1 ' . $filter['condition'] . " AND O.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
  469. }
  470. $params = $filter['params'];
  471. $condition .= $condition ? ' AND O.IS_DELETE=0' : ' O.IS_DELETE=0';
  472. $listObj = new OrderList();
  473. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  474. return static::notice($data);
  475. }
  476. /**
  477. * 订单表导出
  478. * @return mixed
  479. * @throws \yii\db\Exception
  480. * @throws HttpException
  481. */
  482. public function actionOrderListExport()
  483. {
  484. $filter = $this->filterCondition([
  485. 'SN'=> 'O.SN',
  486. 'USER_NAME'=> 'U.USER_NAME',
  487. 'MOBILE'=> 'O.MOBILE',
  488. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  489. 'CREATED_AT'=> 'O.CREATED_AT',
  490. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  491. 'STATUS' => 'O.STATUS',
  492. 'AUTO_MAINTENANCE' => 'O.AUTO_MAINTENANCE'
  493. ]);
  494. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  495. if (!$isSuper) {
  496. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  497. $adminCountry = AdminCountry::getCountry($adminId);
  498. $quotedAdminCountry = array_map(function($item) {
  499. return "'" . addslashes($item) . "'";
  500. }, $adminCountry);
  501. $filter['condition'] = ('O.IS_DELETE=0 ' . $filter['condition']) . " AND O.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
  502. } else {
  503. $filter['condition'] = ('O.IS_DELETE=0 ' . $filter['condition']);
  504. }
  505. $form = new ShopExportForm();
  506. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExport')); // 订单列表
  507. if (!$result) {
  508. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  509. }
  510. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  511. }
  512. /**
  513. * 外部商城订单列表
  514. * @return mixed
  515. * @throws Exception
  516. * @throws HttpException
  517. */
  518. public function actionOrderShopList() {
  519. $filter = $this->filterCondition([
  520. 'SN'=> 'O.SN',
  521. 'USER_NAME'=> 'U.USER_NAME',
  522. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  523. 'CREATED_AT'=> 'O.CREATED_AT',
  524. ]);
  525. $condition = $filter['condition'];
  526. $params = $filter['params'];
  527. $condition .= ' AND O.IS_DELETE=0';
  528. $listObj = new OrderShopList();
  529. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  530. return static::notice($data);
  531. }
  532. /**
  533. * 外部商城报单列表
  534. * @return mixed
  535. * @throws Exception
  536. * @throws HttpException
  537. */
  538. public function actionOrderDecList() {
  539. $filter = $this->filterCondition([
  540. 'SN'=> 'O.SN',
  541. 'USER_NAME'=> 'U.USER_NAME',
  542. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  543. 'CREATED_AT'=> 'O.CREATED_AT',
  544. ]);
  545. $condition = $filter['condition'];
  546. $params = $filter['params'];
  547. $condition .= ' AND O.IS_DELETE=0';
  548. $listObj = new OrderDecList();
  549. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  550. return static::notice($data);
  551. }
  552. /**
  553. * 分页导入excel文件到待导入数据的表中
  554. * @return mixed
  555. */
  556. public function actionImportOrderShopToExcelTable()
  557. {
  558. // 先上传到服务器文件
  559. if (\Yii::$app->request->isPost) {
  560. $excelImportId = \Yii::$app->request->post('excelImportId');
  561. $rowCount = \Yii::$app->request->post('rowCount');
  562. //$periodNum = \Yii::$app->request->post('periodNum');
  563. $orderDay = \Yii::$app->request->post('orderDay');
  564. $orderType = \Yii::$app->request->post('orderType');
  565. $startRow = \Yii::$app->request->post('startRow');
  566. $limit = \Yii::$app->request->post('limit', 1000);
  567. $errorMsg = '';
  568. try {
  569. if( $startRow == 1 ) {
  570. $excelOrderShop = new ExcelOrderShopForm();
  571. $excelOrderShop->checkStatus($orderDay,$orderType);
  572. }
  573. $excel = new \common\helpers\Excel();
  574. $result = $excel->pageImportCustomDataFromExcel('orderShop', $excelImportId, $rowCount, $startRow, $limit,$orderDay,$orderType);
  575. } catch (\Exception $e) {
  576. $result = false;
  577. $errorMsg = $e->getMessage();
  578. }
  579. // 还有数据
  580. if ($result === 1) {
  581. return static::notice(['finish' => false]);
  582. } elseif ($result === 0) {
  583. return static::notice(['finish' => true]);
  584. } else {
  585. return static::notice('Error:' . $errorMsg, 400);
  586. }
  587. }
  588. }
  589. /**
  590. * 导入完成标记
  591. * @return mixed
  592. * @throws HttpException
  593. */
  594. public function actionImportOrderShop() {
  595. //$periodNum = \Yii::$app->request->post('periodNum');
  596. $orderDay = \Yii::$app->request->post('orderDay');
  597. $orderType = \Yii::$app->request->post('orderType');
  598. $excelOrderShop = new ExcelOrderShopForm();
  599. $finishStatus = $excelOrderShop->finished($orderDay,$orderType);
  600. if ($finishStatus) {
  601. return static::notice(['finish' => true]);
  602. } else {
  603. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  604. }
  605. }
  606. /**
  607. * 报单订单——分页导入excel文件到待导入数据的表中
  608. * @return mixed
  609. */
  610. public function actionImportOrderDecToExcelTable()
  611. {
  612. // 先上传到服务器文件
  613. if (\Yii::$app->request->isPost) {
  614. $excelImportId = \Yii::$app->request->post('excelImportId');
  615. $rowCount = \Yii::$app->request->post('rowCount');
  616. //$periodNum = \Yii::$app->request->post('periodNum');
  617. $orderDay = \Yii::$app->request->post('orderDay');
  618. $startRow = \Yii::$app->request->post('startRow');
  619. $limit = \Yii::$app->request->post('limit', 1000);
  620. $errorMsg = '';
  621. try {
  622. if( $startRow == 1 ) {
  623. $excelOrderShop = new ExcelOrderDecForm();
  624. $excelOrderShop->checkStatus($orderDay);
  625. }
  626. $excel = new \common\helpers\Excel();
  627. $result = $excel->pageImportCustomDataFromExcel('orderDec', $excelImportId, $rowCount, $startRow, $limit,$orderDay);
  628. } catch (\Exception $e) {
  629. $result = false;
  630. $errorMsg = $e->getMessage();
  631. }
  632. // 还有数据
  633. if ($result === 1) {
  634. return static::notice(['finish' => false]);
  635. } elseif ($result === 0) {
  636. return static::notice(['finish' => true]);
  637. } else {
  638. return static::notice('Error:' . $errorMsg, 400);
  639. }
  640. }
  641. }
  642. /**
  643. * 报单订单导入完成标记
  644. * @return mixed
  645. * @throws HttpException
  646. */
  647. public function actionImportOrderDec() {
  648. //$periodNum = \Yii::$app->request->post('periodNum');
  649. $orderDay = \Yii::$app->request->post('orderDay');
  650. $excelOrderShop = new ExcelOrderDecForm();
  651. $finishStatus = $excelOrderShop->finished($orderDay);
  652. if ($finishStatus) {
  653. return static::notice(['finish' => true]);
  654. } else {
  655. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  656. }
  657. }
  658. /**
  659. * 见习达标订单列表
  660. * @return mixed
  661. * @throws Exception
  662. * @throws HttpException
  663. */
  664. public function actionOrderStandardList() {
  665. $filter = $this->filterCondition([
  666. 'SN'=> 'O.SN',
  667. 'USER_NAME'=> 'U.USER_NAME',
  668. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  669. 'CREATED_AT'=> 'O.CREATED_AT',
  670. ]);
  671. $condition = $filter['condition'];
  672. $params = $filter['params'];
  673. $condition .= ' AND O.IS_DELETE=0';
  674. $listObj = new OrderStandardList();
  675. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  676. return static::notice($data);
  677. }
  678. /**
  679. * 分页导入excel文件到待导入数据的表中
  680. * @return mixed
  681. * @throws HttpException
  682. */
  683. public function actionImportOrderStandardToExcelTable()
  684. {
  685. // 先上传到服务器文件
  686. if (\Yii::$app->request->isPost) {
  687. $excelImportId = \Yii::$app->request->post('excelImportId');
  688. $rowCount = \Yii::$app->request->post('rowCount');
  689. //$periodNum = \Yii::$app->request->post('periodNum');
  690. $orderMonth = \Yii::$app->request->post('orderMonth');
  691. // $orderType = \Yii::$app->request->post('orderType');
  692. $orderType = 'cash';
  693. $startRow = \Yii::$app->request->post('startRow');
  694. $limit = \Yii::$app->request->post('limit', 1000);
  695. $errorMsg = '';
  696. try {
  697. if( $startRow == 1 ) {
  698. //清空已经导入的表数据
  699. $excelOrderStandard = new ExcelOrderStandardForm();
  700. $excelOrderStandard->checkStatus($orderMonth);
  701. }
  702. $excel = new \common\helpers\Excel();
  703. $result = $excel->pageImportCustomDataFromExcel('orderShopStandard', $excelImportId, $rowCount, $startRow, $limit,$orderMonth,$orderType);
  704. } catch (Exception $e) {
  705. $result = false;
  706. $errorMsg = $e->getMessage();
  707. }
  708. // 还有数据
  709. if ($result === 1) {
  710. return static::notice(['finish' => false]);
  711. } elseif ($result === 0) {
  712. return static::notice(['finish' => true]);
  713. } else {
  714. return static::notice('Error:' . $errorMsg, 400);
  715. }
  716. }
  717. }
  718. /**
  719. * 达标订单导入完成标记
  720. * @return mixed
  721. * @throws HttpException
  722. */
  723. public function actionImportOrderStandard() {
  724. $orderMonth = \Yii::$app->request->post('orderMonth');
  725. $excelOrderStandard = new ExcelOrderStandardForm();
  726. $finishStatus = $excelOrderStandard->finished($orderMonth);
  727. if ($finishStatus) {
  728. return static::notice(['finish' => true]);
  729. } else {
  730. return static::notice(\Yii::t('ctx', 'shopStatusUpdateErrorNotice'), 400);
  731. }
  732. }
  733. /**
  734. * 管理员发货
  735. * @return mixed
  736. * @throws HttpException
  737. */
  738. public function actionOrderDelivery() {
  739. if(\Yii::$app->request->isPost) {
  740. return parent::edit(OrderForm::class, '发货成功', 'adminDelivery', ['adminDelivery']);
  741. }
  742. return static::notice(\Yii::t('ctx', 'illegalRequest'), 400); // 非法请求
  743. }
  744. /**
  745. * 报单表导出
  746. * @return mixed
  747. * @throws \yii\db\Exception
  748. * @throws HttpException
  749. */
  750. public function actionOrderListExportPdf()
  751. {
  752. $orderSn = \Yii::$app->request->get('orderSn');
  753. $filter = $this->filterCondition([
  754. 'SN'=> 'O.SN',
  755. 'USER_NAME'=> 'U.USER_NAME',
  756. 'MOBILE'=> 'O.MOBILE',
  757. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  758. 'CREATED_AT'=> 'O.CREATED_AT',
  759. ]);
  760. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  761. $filter['params'] = [':SN' => $orderSn];
  762. $form = new ShopExportForm();
  763. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExportPdf'));
  764. if (!$result) {
  765. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  766. }
  767. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  768. }
  769. public function actionOrderInvoiceExport()
  770. {
  771. $orderSn = \Yii::$app->request->get('orderSn');
  772. $filter = $this->filterCondition([
  773. 'SN'=> 'O.SN',
  774. ]);
  775. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  776. $filter['params'] = [':SN' => $orderSn];
  777. // 生成发票编号
  778. $order = Order::find()
  779. ->alias('O')
  780. ->select('O.*,CS.CODE AS COUNTRY_CODE')
  781. ->where(['SN' => $orderSn])
  782. ->join('LEFT JOIN', Countries::tableName() . ' AS CS', 'CS.ID=O.COUNTRY_ID')
  783. ->asArray()->one();
  784. if (!$order) {
  785. return static::notice('订单不存在', 400);
  786. }
  787. if (!$order['INVOICE_NO']) {
  788. $invoiceNo = $this->generateInvoiceNo($order['COUNTRY_CODE']);
  789. Order::updateAll(['INVOICE_NO' => $invoiceNo], 'SN = :SN', [':SN' => $orderSn]);
  790. }
  791. $form = new ShopExportForm();
  792. $result = $form->run($filter, \Yii::t('ctx', 'actionOrderInvoiceExport'));
  793. if (!$result) {
  794. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  795. }
  796. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  797. }
  798. /**
  799. * 报单表导出
  800. * @return mixed
  801. * @throws \yii\db\Exception
  802. * @throws HttpException
  803. */
  804. public function actionDecOrderListExportPdf()
  805. {
  806. $orderSn = \Yii::$app->request->get('orderSn');
  807. $filter = $this->filterCondition([
  808. 'SN'=> 'O.SN',
  809. 'USER_NAME'=> 'U.USER_NAME',
  810. 'MOBILE'=> 'O.MOBILE',
  811. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  812. 'CREATED_AT'=> 'O.CREATED_AT',
  813. ]);
  814. $filter['condition'] = ' 1=1 AND O.IS_DELETE=0 AND O.SN=:SN';
  815. $filter['params'] = [':SN' => $orderSn];
  816. $form = new ShopExportForm();
  817. $result = $form->run($filter, \Yii::t('ctx', 'shopOrderListExportPdf'));
  818. if (!$result) {
  819. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  820. }
  821. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  822. }
  823. /**
  824. * 管理员订单退款
  825. * @return mixed
  826. * @throws HttpException
  827. */
  828. public function actionOrderRefund()
  829. {
  830. if(\Yii::$app->request->isPost) {
  831. return parent::edit(OrderForm::class, \Yii::t('ctx', 'shopadminRefund'), 'adminRefund', ['adminRefund']);
  832. }
  833. return static::notice(\Yii::t('ctx', 'illegalRequest'), 400); // 非法请求
  834. }
  835. /*
  836. * 剩余BV页
  837. *
  838. */
  839. public function actionRemainPv()
  840. {
  841. $filter = $this->filterCondition([
  842. 'USER_NAME' => 'U.USER_NAME',
  843. ]);
  844. $condition = $filter['condition'];
  845. $params = $filter['params'];
  846. $listObj = new RemainPvList();
  847. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  848. return static::notice($data);
  849. }
  850. /*
  851. * 剩余BV流水
  852. *
  853. */
  854. public function actionFlowRemainPv()
  855. {
  856. $filter = $this->filterCondition([
  857. 'USER_NAME' => 'U.USER_NAME',
  858. ]);
  859. $condition = $filter['condition'];
  860. $params = $filter['params'];
  861. $listObj = new FlowRemainPvList();
  862. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  863. return static::notice($data);
  864. }
  865. /**
  866. * BA订单表
  867. * @return mixed
  868. * @throws Exception
  869. * @throws HttpException
  870. */
  871. public function actionBaOrderList() {
  872. $filter = $this->filterCondition([
  873. 'SN'=> 'O.SN',
  874. 'USER_NAME'=> 'U.USER_NAME',
  875. 'MOBILE'=> 'O.MOBILE',
  876. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  877. 'CREATED_AT'=> 'O.CREATED_AT',
  878. 'ORDER_TYPE'=> 'O.ORDER_TYPE',
  879. 'STATUS' => 'O.STATUS',
  880. ]);
  881. $condition = ' 1=1 ' . $filter['condition'];
  882. $params = $filter['params'];
  883. $condition .= $condition ? ' AND O.IS_DELETE=0' : ' O.IS_DELETE=0';
  884. $listObj = new BaOrderList();
  885. $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
  886. return static::notice($data);
  887. }
  888. /**
  889. * BA订单表导出
  890. * @return mixed
  891. * @throws \yii\db\Exception
  892. * @throws HttpException
  893. */
  894. public function actionBaOrderListExport()
  895. {
  896. $filter = $this->filterCondition([
  897. 'SN'=> 'O.SN',
  898. 'USER_NAME'=> 'U.USER_NAME',
  899. 'MOBILE'=> 'O.MOBILE',
  900. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  901. 'CREATED_AT'=> 'O.CREATED_AT',
  902. 'STATUS' => 'O.STATUS',
  903. ]);
  904. $filter['condition'] = !$filter['condition'] ? '1=1 AND O.IS_DELETE=0' : ('O.IS_DELETE=0 ' . $filter['condition']);
  905. $form = new BaShopExportForm();
  906. $result = $form->run($filter, \Yii::t('ctx', 'shopBaOrderListExport')); // 订单列表
  907. if (!$result) {
  908. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  909. }
  910. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  911. }
  912. /**
  913. * 报单表导出
  914. * @return mixed
  915. * @throws \yii\db\Exception
  916. * @throws HttpException
  917. */
  918. public function actionBaOrderListExportPdf()
  919. {
  920. $orderSn = \Yii::$app->request->get('orderSn');
  921. $filter = $this->filterCondition([
  922. 'SN'=> 'O.SN',
  923. 'USER_NAME'=> 'U.USER_NAME',
  924. 'MOBILE'=> 'O.MOBILE',
  925. 'PERIOD_NUM'=> 'O.PERIOD_NUM',
  926. 'CREATED_AT'=> 'O.CREATED_AT',
  927. ]);
  928. $filter['condition'] = ' O.IS_DELETE=0 AND O.SN=:SN';
  929. $filter['params'] = [':SN' => $orderSn];
  930. $form = new BaShopExportForm();
  931. $result = $form->run($filter, \Yii::t('ctx', 'shopBrandAmbassadorOrderExport'));
  932. if (!$result) {
  933. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  934. }
  935. return static::notice(\Yii::t('ctx', 'startExporting')); // 导出开始,请到文件管理-导出文件查看
  936. }
  937. /**
  938. * 调整订单期数
  939. * @return mixed
  940. * @throws Exception
  941. * @throws \yii\db\Exception
  942. * @throws \yii\web\HttpException
  943. */
  944. public function actionOrderPeriodAdjust()
  945. {
  946. if (\Yii::$app->request->isPost) {
  947. $formModel = new OrderPeriodAdjustForm();
  948. $formModel->scenario = 'orderPeriodAdjust';
  949. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->periodAdjust()) {
  950. return static::notice(Yii::t('ctx', 'successfully'));
  951. } else {
  952. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  953. }
  954. } else {
  955. $filter = $this->filterCondition([
  956. 'ORDER_SN' => 'P.ORDER_SN',
  957. 'ORIGIN_PERIOD'=> 'P.ORIGIN_PERIOD',
  958. 'MODERN_PERIOD'=> 'P.MODERN_PERIOD',
  959. 'CREATED_AT'=> 'P.CREATED_AT',
  960. ]);
  961. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  962. if (!$isSuper) {
  963. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  964. $adminCountry = AdminCountry::getCountry($adminId);
  965. $quotedAdminCountry = array_map(function($item) {
  966. return "'" . addslashes($item) . "'";
  967. }, $adminCountry);
  968. $filter['condition'] .= ' AND O.COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')';
  969. }
  970. $listObj = new OrderPeriodAdjustList();
  971. $data = $listObj->getList(['condition' => $filter['condition'], 'params' => $filter['params']]);
  972. return static::notice($data);
  973. }
  974. }
  975. /**
  976. * 订单详情
  977. * @return mixed
  978. * @throws \yii\web\HttpException
  979. */
  980. public function actionOrderDetail()
  981. {
  982. $orderSn = \Yii::$app->request->get('orderSn');
  983. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  984. if (!$isSuper) {
  985. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  986. $adminCountry = AdminCountry::getCountry($adminId);
  987. $quotedAdminCountry = array_map(function ($item) {
  988. return "'" . addslashes($item) . "'";
  989. }, $adminCountry);
  990. $order = Order::findOneAsArray('SN=:SN AND COUNTRY_ID IN (' . implode(',', $quotedAdminCountry) . ')', [':SN' => $orderSn]);
  991. } else {
  992. $order = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  993. }
  994. if (!$order) {
  995. return static::notice(Yii::t('ctx', 'orderNotExist'), 400);
  996. }
  997. $order['ORDER_TYPE'] = $order['ORDER_TYPE'] === 'ZC' ? Yii::t('ctx', 'shopOrderListOrderTypeZc') : Yii::t('ctx', 'shopOrderListOrderTypeFx');
  998. $order['COUNTRY'] = Countries::getById($order['COUNTRY_ID'])['NAME'] ?? '';
  999. // 已挂网,不可调整
  1000. $period = Period::getInfoByPeriodNum($order['PERIOD_NUM']);
  1001. if (!$period) {
  1002. return static::notice(Yii::t('ctx', 'invalidPcNo'), 400);
  1003. }
  1004. if ($period['IS_SENT'] > 0) {
  1005. return static::notice(Yii::t('ctx', 'orderHasBeenConnected'), 400);
  1006. }
  1007. // 可调整期数
  1008. $availablePeriod = [];
  1009. if ($order['PERIOD_NUM']) {
  1010. $availablePeriod = Period::getPeriodList(+$order['PERIOD_NUM'] - 1);
  1011. foreach ($availablePeriod as $k => $item) {
  1012. $availablePeriod[$k]['disabled'] = (+$item['IS_SENT'] > 0) || ($order['PERIOD_NUM'] == $item['PERIOD_NUM']);
  1013. }
  1014. }
  1015. return static::notice(['order' => $order, 'availablePeriod' => $availablePeriod]);
  1016. }
  1017. public function actionOrderPeriodAdjustBatch()
  1018. {
  1019. $db = \Yii::$app->db;
  1020. $transaction = $db->beginTransaction();
  1021. $orderList = [];
  1022. $orderErr = [];
  1023. try {
  1024. $modernPeriod = 150; // todo: 修改后期数
  1025. $oldPeriod = 151; // todo: 修改前期数
  1026. foreach ($orderList as $orderSn) {
  1027. $orderObj = Order::findOneAsArray('SN=:SN', [':SN' => $orderSn]);
  1028. if (!$orderObj) {
  1029. $orderErr[] = $orderSn;
  1030. continue;
  1031. }
  1032. // 调整订单期数
  1033. Order::updateAll(['PERIOD_NUM' => $modernPeriod], 'SN = :SN', [':SN' => $orderSn]);
  1034. if ($orderObj['ORDER_TYPE'] === 'ZC') {
  1035. // 调整报单期数
  1036. DecOrder::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  1037. // 调整会员的期数
  1038. User::updateAll(['PERIOD_AT' => $modernPeriod], 'ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  1039. UserInfo::updateAll(['HIGHEST_EMP_LV_PERIOD' => $modernPeriod], 'USER_ID = :USER_ID', ['USER_ID' => $orderObj['USER_ID']]);
  1040. }
  1041. // 调整流水的期数
  1042. switch ($orderObj['PAY_TYPE']){
  1043. case 'cash':
  1044. FlowWallet::updateAll(['PERIOD_NUM' => $modernPeriod], 'ORDER_SN = :ORDER_SN', ['ORDER_SN' => $orderSn]);
  1045. break;
  1046. default:
  1047. break;
  1048. }
  1049. // 写入调整记录
  1050. $orderModel = new OrderPeriodAdjust();
  1051. $orderModel->ORDER_SN = $orderSn;
  1052. $orderModel->ORIGIN_PERIOD = $oldPeriod;
  1053. $orderModel->MODERN_PERIOD = $modernPeriod;
  1054. $orderModel->ADMIN_ID = 'admin';
  1055. $orderModel->CREATED_AT = Date::nowTime();
  1056. if (!$orderModel->save()) {
  1057. return static::notice($orderModel->getErrors(), 400);
  1058. }
  1059. }
  1060. $transaction->commit();
  1061. } catch(\Exception $e) {
  1062. $transaction->rollBack();
  1063. return static::notice($e->getMessage(), 400);
  1064. }
  1065. return static::notice('成功. ' . json_encode($orderErr));
  1066. }
  1067. public function generateInvoiceNo($country_code = '')
  1068. {
  1069. $prefix = 'Inv'.$country_code;
  1070. $currentDate = date('dmY');
  1071. // 查询今天生成的最新发票号(包含当天日期格式的)
  1072. $order = Order::find()
  1073. ->where(['STATUS' => 1, 'IS_DELETE' => 0])
  1074. ->andWhere(['like', 'INVOICE_NO', $prefix . $currentDate])
  1075. ->orderBy(['INVOICE_NO' => SORT_DESC])
  1076. ->one();
  1077. if (!$order || !$order->INVOICE_NO) {
  1078. // 如果今天没有生成过发票号或没有符合条件的订单,从00001开始
  1079. $no = '00001';
  1080. } else {
  1081. $invoiceNo = $order->INVOICE_NO;
  1082. // 截取最后5位数字部分并+1
  1083. $no = sprintf('%05d', intval(substr($invoiceNo, -5)) + 1);
  1084. }
  1085. return $prefix . $currentDate . $no;
  1086. }
  1087. }