ShopController.php 47 KB

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