ShopController.php 43 KB

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