ShopController.php 43 KB

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