ItemController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\item\classes\PtItemClass;
  5. use biz\item\classes\PtItemClassClass;
  6. use biz\product\classes\XjClass;
  7. use biz\sj\classes\SjClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use biz\shop\classes\ShopClass;
  10. use bizGhs\item\classes\ItemClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\order\classes\OrderClass;
  13. use bizGhs\order\classes\OrderItemClass;
  14. use bizGhs\order\classes\PurchaseOrderItemClass;
  15. use bizGhs\order\classes\StockWastageOrderClass;
  16. use bizGhs\order\classes\StockWastageOrderItemClass;
  17. use bizHd\saas\classes\ApplyClass;
  18. use bizHd\saas\models\Apply;
  19. use bizHd\saas\services\ApplyService;
  20. use common\components\dict;
  21. use common\components\imgUtil;
  22. use common\components\stringUtil;
  23. use common\components\util;
  24. use yii\console\Controller;
  25. use Yii;
  26. use bizGhs\product\classes\ProductClass;
  27. /**
  28. * 花材同步脚本 ssh 20230619
  29. */
  30. class ItemController extends Controller
  31. {
  32. public $sjId, $shopId, $sj;
  33. public function actionCQ()
  34. {
  35. $itemList = ItemClass::getAllByCondition(['mainId' => 14128], null, '*', null, true);
  36. if (!empty($itemList)) {
  37. foreach ($itemList as $item) {
  38. $classId = $item->classId;
  39. if (!empty($classId)) {
  40. $class = ItemClassClass::getById($classId, true);
  41. if ($class->mainId != 14128) {
  42. echo $item->name . "\n";
  43. }
  44. }
  45. }
  46. }
  47. }
  48. //【重要】将花材的分类id同步到批发销售单、批发采购单和零售销售单【小丽优先】
  49. public function actionSyncClass()
  50. {
  51. ini_set('memory_limit', '5120M');
  52. set_time_limit(0);
  53. $itemList = ItemClass::getAllByCondition(['mainId' => 10975], null, '*', null, true);
  54. if (empty($itemList)) {
  55. util::stop('没有找到花材');
  56. }
  57. foreach ($itemList as $item) {
  58. $classId = $item->classId ?? 0;
  59. $productId = $item->id ?? 0;
  60. if (!empty($classId) && !empty($productId)) {
  61. OrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  62. \bizHd\order\classes\OrderItemClass::updateByCondition(['itemId' => $productId], ['classId' => $classId]);
  63. PurchaseOrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  64. }
  65. }
  66. echo "ok\n";
  67. }
  68. //包装纸丝带同步 ssh 20230525
  69. public function actionPaperSync()
  70. {
  71. $connection = Yii::$app->db;
  72. $transaction = $connection->beginTransaction();
  73. try {
  74. $oldItemList = ItemClass::getAllByCondition(['classId' => 26013, 'delStatus' => 0], null, '*');
  75. foreach ($oldItemList as $oldItem) {
  76. unset($oldItem['id']);
  77. $oldItem['classId'] = 31106;
  78. $oldItem['stock'] = 0;
  79. $oldItem['status'] = 2;
  80. $oldItem['mainId'] = 4553;
  81. $oldItem['sjId'] = 16936;
  82. $oldItem['shopId'] = 0;
  83. ItemClass::add($oldItem);
  84. }
  85. $transaction->commit();
  86. } catch (\Exception $e) {
  87. $transaction->rollBack();
  88. $msg = $e->getMessage();
  89. echo "报错了:" . $msg;
  90. }
  91. }
  92. public function actionWaste()
  93. {
  94. ini_set('memory_limit', '2045M');
  95. set_time_limit(0);
  96. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  97. if (!empty($list)) {
  98. foreach ($list as $order) {
  99. $orderSn = $order->orderSn ?? '';
  100. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  101. $totalCost = 0;
  102. if (!empty($itemList)) {
  103. foreach ($itemList as $v) {
  104. $num = $v->itemNum ?? 0;
  105. $itemCost = $v->itemCost ?? 0;
  106. $totalCost = bcadd($totalCost, bcmul($num, $itemCost, 2), 2);
  107. }
  108. }
  109. $order->cost = $totalCost;
  110. $order->save();
  111. }
  112. }
  113. }
  114. public function actionWastage()
  115. {
  116. ini_set('memory_limit', '2045M');
  117. set_time_limit(0);
  118. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  119. if (!empty($list)) {
  120. foreach ($list as $order) {
  121. $orderSn = $order->orderSn ?? '';
  122. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  123. $totalPrice = 0;
  124. if (!empty($itemList)) {
  125. foreach ($itemList as $v) {
  126. $num = $v->itemNum ?? 0;
  127. $itemPrice = $v->itemPrice ?? 0;
  128. $totalPrice = bcadd($totalPrice, bcmul($num, $itemPrice, 2), 2);
  129. }
  130. }
  131. $order->price = $totalPrice;
  132. $order->save();
  133. }
  134. }
  135. }
  136. //初见花季 ssh 20230223
  137. public function actionCj()
  138. {
  139. if (getenv('YII_ENV') == 'production') {
  140. $mtMainId = 10449;
  141. $ttMainId = 8502;
  142. } else {
  143. $mtMainId = 762;
  144. $ttMainId = 644;
  145. util::stop('已取消');
  146. }
  147. $list = ItemClass::getAllByCondition(['mainId' => $mtMainId], null, '*');
  148. if (!empty($list)) {
  149. echo '已经有花材了';
  150. exit();
  151. }
  152. $classList = ItemClassClass::getAllByCondition(['mainId' => $ttMainId], null, '*', null, true);
  153. if (empty($classList)) {
  154. echo '没有找到目标门店的分类';
  155. exit();
  156. }
  157. $connection = Yii::$app->db;
  158. $transaction = $connection->beginTransaction();
  159. try {
  160. foreach ($classList as $class) {
  161. $oldId = $class->id ?? 0;
  162. $cover = $class->cover ?? '';
  163. $name = $class->name ?? '';
  164. $inTurn = $class->inTurn ?? 0;
  165. $isDefault = $class->isDefault ?? 0;
  166. $newClass = [
  167. 'mainId' => $mtMainId,
  168. 'cover' => $cover,
  169. 'name' => $name,
  170. 'inTurn' => $inTurn,
  171. 'isDefault' => $isDefault,
  172. ];
  173. $newRespond = ItemClassClass::add($newClass, true);
  174. $newClassId = $newRespond->id ?? 0;
  175. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  176. foreach ($oldItemList as $oldItem) {
  177. $oldItemId = $oldItem['id'];
  178. unset($oldItem['id']);
  179. $oldItem['classId'] = $newClassId;
  180. $oldItem['stock'] = 0;
  181. $oldItem['status'] = 2;
  182. $oldItem['mainId'] = $mtMainId;
  183. $oldItem['sjId'] = 0;
  184. $oldItem['shopId'] = 0;
  185. $new = ItemClass::add($oldItem, true);
  186. if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
  187. $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
  188. if (!empty($xjList)) {
  189. foreach ($xjList as $oneXj) {
  190. unset($oneXj['id']);
  191. unset($oneXj['sjId']);
  192. unset($oneXj['shopId']);
  193. unset($oneXj['mainId']);
  194. unset($oneXj['itemId']);
  195. unset($oneXj['ptItemId']);
  196. unset($oneXj['addTime']);
  197. unset($oneXj['updateTime']);
  198. $oneXj['mainId'] = $mtMainId;
  199. $oneXj['itemId'] = $new->id ?? 0;
  200. $oneXj['ptItemId'] = $new->itemId ?? 0;
  201. XjClass::add($oneXj);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. $transaction->commit();
  208. } catch (\Exception $e) {
  209. $transaction->rollBack();
  210. $msg = $e->getMessage();
  211. echo "报错了:" . $msg;
  212. }
  213. }
  214. //美天鲜花 ssh 20230223
  215. public function actionMt()
  216. {
  217. if (getenv('YII_ENV') == 'production') {
  218. $mtMainId = 13495;
  219. $ttMainId = 52;
  220. } else {
  221. //$mtMainId = 762;
  222. //$ttMainId = 644;
  223. util::stop('测试环境未开通');
  224. }
  225. $list = ItemClass::getAllByCondition(['mainId' => $mtMainId], null, '*');
  226. if (!empty($list)) {
  227. echo '已经有花材了';
  228. exit();
  229. }
  230. $classList = ItemClassClass::getAllByCondition(['mainId' => $ttMainId], null, '*', null, true);
  231. if (empty($classList)) {
  232. echo '没有找到天天鲜花的分类';
  233. exit();
  234. }
  235. $connection = Yii::$app->db;
  236. $transaction = $connection->beginTransaction();
  237. try {
  238. foreach ($classList as $class) {
  239. $oldId = $class->id ?? 0;
  240. $cover = $class->cover ?? '';
  241. $name = $class->name ?? '';
  242. $inTurn = $class->inTurn ?? 0;
  243. $isDefault = $class->isDefault ?? 0;
  244. $newClass = [
  245. 'mainId' => $mtMainId,
  246. 'cover' => $cover,
  247. 'name' => $name,
  248. 'inTurn' => $inTurn,
  249. 'isDefault' => $isDefault,
  250. ];
  251. $newRespond = ItemClassClass::add($newClass, true);
  252. $newClassId = $newRespond->id ?? 0;
  253. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  254. foreach ($oldItemList as $oldItem) {
  255. $oldItemId = $oldItem['id'];
  256. unset($oldItem['id']);
  257. $oldItem['classId'] = $newClassId;
  258. $oldItem['stock'] = 0;
  259. $oldItem['status'] = 2;
  260. $oldItem['mainId'] = $mtMainId;
  261. $oldItem['sjId'] = 0;
  262. $oldItem['shopId'] = 0;
  263. $new = ItemClass::add($oldItem, true);
  264. if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
  265. $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
  266. if (!empty($xjList)) {
  267. foreach ($xjList as $oneXj) {
  268. unset($oneXj['id']);
  269. unset($oneXj['sjId']);
  270. unset($oneXj['shopId']);
  271. unset($oneXj['mainId']);
  272. unset($oneXj['itemId']);
  273. unset($oneXj['ptItemId']);
  274. unset($oneXj['addTime']);
  275. unset($oneXj['updateTime']);
  276. $oneXj['mainId'] = $mtMainId;
  277. $oneXj['itemId'] = $new->id ?? 0;
  278. $oneXj['ptItemId'] = $new->itemId ?? 0;
  279. XjClass::add($oneXj);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. $transaction->commit();
  286. } catch (\Exception $e) {
  287. $transaction->rollBack();
  288. $msg = $e->getMessage();
  289. echo "报错了:" . $msg;
  290. }
  291. }
  292. //花材反向更新 ./yii item/reverse-update
  293. public function actionReverseUpdate()
  294. {
  295. $shopId = 10;
  296. if (getenv('YII_ENV') != 'production') {
  297. $shopId = 36225;
  298. }
  299. $list = ProductClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
  300. if (empty($list)) {
  301. return false;
  302. }
  303. foreach ($list as $cKey => $product) {
  304. $ptItemId = $product->itemId ?? 0;
  305. $ptItem = PtItemClass::getById($ptItemId, true);
  306. if (empty($ptItem)) {
  307. continue;
  308. }
  309. $ptItem->name = $product->name ?? '';
  310. $ptItem->py = $product->py ?? '';
  311. $ptItem->cover = $product->cover ?? '';
  312. $ptItem->ratio = $product->ratio ?? 1;
  313. $ptItem->shelfLife = $product->shelfLife ?? 7;
  314. $ptItem->weight = $product->weight ?? 0.1;
  315. $ptItem->stockWarning = $product->stockWarning ?? 5;;
  316. $ptItem->cost = $product->cost ?? 1;
  317. $ptItem->addPrice = 5;
  318. $ptItem->bigUnit = $product->bigUnit;
  319. $ptItem->smallUnit = $product->smallUnit;
  320. $ptItem->bigUnitId = $product->bigUnitId;
  321. $ptItem->smallUnitId = $product->smallUnitId;
  322. $ptItem->save();
  323. echo $product->id . " " . $product->name . " 已同步\n";
  324. }
  325. }
  326. //命令: ./yii item/sync name,cover 同步name 和cover
  327. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  328. public function actionSync($field)
  329. {
  330. $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning'];
  331. $fields = explode(",", $field);
  332. $syncFields = array_intersect($allFiled, $fields);
  333. if (empty($syncFields)) {
  334. echo "参数不支持";
  335. die;
  336. }
  337. $item = PtItemClass::getAllList("id", "");
  338. $itemIds = [];
  339. foreach ($item as $v) {
  340. $itemIds[] = $v['id'];
  341. }
  342. if (!empty($itemIds)) {
  343. foreach ($itemIds as $v) {
  344. $itemInfo = PtItemClass::getById($v);
  345. $data = [];
  346. foreach ($syncFields as $f) {
  347. $data[$f] = $itemInfo[$f];
  348. }
  349. ProductClass::updateByCondition(['itemId' => $v], $data);
  350. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data);
  351. }
  352. }
  353. echo "done";
  354. }
  355. //平台花材初始化导入 lqh 2021-05-08
  356. //命令: ./yii item/init-pt-item
  357. public function actionInitPtItem($clear = false)
  358. {
  359. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  360. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  361. $dir = Yii::$app->basePath;
  362. $filename = $dir . '/item.xls';
  363. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  364. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  365. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  366. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  367. $highestRow = $sheet->getHighestRow();//取得总行数
  368. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  369. $classData = [];
  370. $itemData = [];
  371. $classIdMap = [];
  372. for ($j = 2; $j <= $highestRow; $j++) {
  373. $str = '';
  374. for ($k = 'A'; $k <= $highestColumn; $k++) {
  375. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  376. }
  377. //explode:函数把字符串分割为数组。
  378. $string = explode("\\", $str);
  379. array_pop($string);
  380. $itemName = $string[0] ?? ''; //花材名称
  381. $className = $string[1] ?? ''; //分类名称
  382. $weight = $string[2] ?? 0; //重量 kg
  383. $bigUnit = $string[3] ?? ''; //大单位名称
  384. $smallUnit = $string[4] ?? ''; //小单位名称
  385. $ratio = $string[5] ?? 0; //比例
  386. $cost = $string[6] ?? 0;//成本价
  387. $addPrice = $string[7] ?? 0; //加价
  388. $stockWarning = $string[8] ?? 0;
  389. $alias = $string[9] ?? '';
  390. $cover = $string[10] ?? '';
  391. $inTurn = $string[11] ?? 0;
  392. if ($itemName) {
  393. $py = stringUtil::py($itemName);
  394. $item = [
  395. 'name' => $itemName,
  396. 'alias' => $alias,
  397. 'py' => $py,
  398. 'cover' => $cover,
  399. 'classId' => 0,
  400. 'ratio' => $ratio,
  401. 'weight' => $weight,
  402. 'stockWarning' => $stockWarning,
  403. 'cost' => $cost,
  404. 'addPrice' => $addPrice,
  405. 'bigUnit' => $bigUnit,
  406. 'smallUnit' => $smallUnit,
  407. 'inTurn' => $inTurn,
  408. ];
  409. $itemData[$className][] = $item;
  410. $classData[] = $className;
  411. }
  412. }
  413. $classData = array_unique($classData);
  414. //写入class
  415. if ($clear) {
  416. //清空数据
  417. Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute();
  418. Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute();
  419. }
  420. foreach ($classData as $v) {
  421. $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]);
  422. $classIdMap[$v] = $addClass['id'];
  423. }
  424. $insertItemData = [];
  425. foreach ($itemData as $k => $v) {
  426. $classId = $classIdMap[$k] ?? 0;
  427. if ($classId) {
  428. foreach ($v as $i) {
  429. $i['classId'] = $classId;
  430. $insertItemData[] = $i;
  431. }
  432. }
  433. }
  434. if (!empty($insertItemData)) {
  435. PtItemClass::batchAdd($insertItemData);
  436. }
  437. echo "done";
  438. }
  439. //供货商初始化 ./yii item/gys ./yii.bat item/gys
  440. public function actionGys()
  441. {
  442. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  443. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  444. $dir = Yii::$app->basePath;
  445. $filename = $dir . '/gys.xls';
  446. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  447. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  448. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  449. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  450. $highestRow = $sheet->getHighestRow();//取得总行数
  451. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  452. $changeData = [];
  453. for ($j = 2; $j <= $highestRow; $j++) {
  454. $str = '';
  455. for ($k = 'A'; $k <= $highestColumn; $k++) {
  456. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  457. }
  458. //explode:函数把字符串分割为数组。
  459. $string = explode("\\", $str);
  460. array_pop($string);
  461. array_push($changeData, $string);
  462. }
  463. if (!empty($changeData)) {
  464. $mobileList = array_values(array_column($changeData, 1));
  465. $ptStyle = dict::getDict('ptStyle', 'kmGhs');
  466. Yii::$app->params['ptStyle'] = $ptStyle;
  467. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  468. if (!empty($has)) {
  469. echo "这些已经添加过了,请先删除:\n";
  470. foreach ($has as $item) {
  471. echo $item->name . ' ' . $item->mobile . "\n";
  472. }
  473. exit();
  474. }
  475. if (getenv('YII_ENV') == 'production') {
  476. $this->sjId = 12367;
  477. $this->shopId = 10;
  478. } else {
  479. $this->sjId = 12615;
  480. $this->shopId = 36119;
  481. }
  482. foreach ($changeData as $currentKey => $currentData) {
  483. $mobile = $currentData[1] ?? '';
  484. $name = $currentData[0] ?? '';
  485. $address = $currentData[2] ?? '';
  486. if (stringUtil::isMobile($mobile) == false) {
  487. echo "请填写正确的手机号 \n";
  488. continue;
  489. }
  490. if (empty($name)) {
  491. echo "{$mobile} 名称不能为空 \n";
  492. continue;
  493. }
  494. if (stringUtil::getWordNum($name) > 8) {
  495. echo "{$mobile} 名称不能超过8个汉字 \n";
  496. continue;
  497. }
  498. $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  499. if (!empty($has)) {
  500. echo "{$mobile} 名称已经存在 \n";
  501. continue;
  502. }
  503. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  504. if (!empty($has)) {
  505. echo "{$mobile} 手机号已经添加过 \n";
  506. continue;
  507. }
  508. $connection = Yii::$app->db;
  509. $transaction = $connection->beginTransaction();
  510. try {
  511. $sjId = $this->sjId;
  512. $shopId = $this->shopId;
  513. $sjName = $this->sj->name ?? '';
  514. $applyData = [
  515. 'name' => $name,
  516. 'licenseNo' => $mobile,
  517. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  518. 'mobile' => $mobile,
  519. 'introSjId' => $sjId,
  520. 'introSjName' => $sjName,
  521. 'introShopId' => $shopId,
  522. 'introStyle' => SjClass::STYLE_SUPPLIER,
  523. 'from' => ApplyClass::FROM_GHS,
  524. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  525. 'address' => $address,
  526. 'status' => ApplyClass::STATUS_PASS,
  527. 'replace' => 1,//1表示供货商添加的客户或供货商
  528. ];
  529. $apply = ApplyService::replaceKmGhs($applyData);
  530. $id = $apply['id'] ?? 0;
  531. $respond = ApplyService::pass($id);
  532. $currentShop = $respond['shop'] ?? [];
  533. $sj = $respond['sj'] ?? [];
  534. $currentSjId = $sj['id'] ?? 0;
  535. $currentShopId = $currentShop->id ?? 0;
  536. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  537. if (empty($currentShopId)) {
  538. echo "{$mobile} 供货商添加失败 \n ";
  539. continue;
  540. }
  541. GhsClass::build($currentShopId, $shopId, 1);
  542. $transaction->commit();
  543. echo "{$mobile} 供货商添加成功!\n";
  544. } catch (\Exception $exception) {
  545. $transaction->rollBack();
  546. echo "{$mobile} 供货商添加失败,原因:" . $exception->getMessage() . "\n";
  547. }
  548. }
  549. }
  550. }
  551. //客户初始化 ./yii item/custom ./yii.bat item/custom
  552. public function actionCustom()
  553. {
  554. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  555. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  556. $dir = Yii::$app->basePath;
  557. $filename = $dir . '/custom.xls';
  558. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  559. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  560. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  561. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  562. $highestRow = $sheet->getHighestRow();//取得总行数
  563. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  564. $changeData = [];
  565. for ($j = 2; $j <= $highestRow; $j++) {
  566. $str = '';
  567. for ($k = 'A'; $k <= $highestColumn; $k++) {
  568. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  569. }
  570. //explode:函数把字符串分割为数组。
  571. $string = explode("\\", $str);
  572. array_pop($string);
  573. array_push($changeData, $string);
  574. }
  575. if (!empty($changeData)) {
  576. $mobileList = array_values(array_column($changeData, 1));
  577. $ptStyle = dict::getDict('ptStyle', 'hd');
  578. Yii::$app->params['ptStyle'] = $ptStyle;
  579. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  580. if (!empty($has)) {
  581. echo "这些已经添加过了,请先删除:\n";
  582. foreach ($has as $item) {
  583. echo $item->name . ' ' . $item->mobile . "\n";
  584. }
  585. exit();
  586. }
  587. if (getenv('YII_ENV') == 'production') {
  588. $this->sjId = 12367;
  589. $this->shopId = 10;
  590. } else {
  591. $this->sjId = 12615;
  592. $this->shopId = 36119;
  593. }
  594. $shop = ShopClass::getById($this->shopId, true);
  595. $sj = SjClass::getById($this->sjId, true);
  596. $this->sj = $sj;
  597. foreach ($changeData as $currentKey => $currentData) {
  598. $mobile = $currentData[1] ?? '';
  599. $name = $currentData[0] ?? '';
  600. $address = $currentData[2] ?? '';
  601. if (empty($name)) {
  602. echo "{$mobile} 名称不能为空 \n";
  603. continue;
  604. }
  605. if (stringUtil::getWordNum($name) > 8) {
  606. echo "{$mobile} 名称不能超过8个汉字 \n";
  607. continue;
  608. }
  609. if (stringUtil::isMobile($mobile) == false) {
  610. echo "{$mobile} 手机号错误 \n";
  611. continue;
  612. }
  613. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  614. if (!empty($has)) {
  615. echo "{$mobile} 名称已经存在 \n";
  616. continue;
  617. }
  618. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  619. if (!empty($has)) {
  620. echo "{$mobile} 手机号已经添加过 \n";
  621. continue;
  622. }
  623. $connection = Yii::$app->db;
  624. $transaction = $connection->beginTransaction();
  625. try {
  626. $sjId = $this->sjId;
  627. $shopId = $this->shopId;
  628. $sjName = $this->sj->name ?? '';
  629. $city = $shop->city ?? '';
  630. $dist = $shop->dist ?? '';
  631. $applyData = [
  632. 'name' => $name,
  633. 'licenseNo' => $mobile,
  634. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  635. 'mobile' => $mobile,
  636. 'introSjId' => $sjId,
  637. 'introSjName' => $sjName,
  638. 'introShopId' => $shopId,
  639. 'introStyle' => SjClass::STYLE_SUPPLIER,
  640. 'from' => ApplyClass::FROM_GHS,
  641. 'style' => SjClass::STYLE_RETAIL,
  642. 'province' => $shop->province ?? '',
  643. 'city' => $city,
  644. 'dist' => $dist,
  645. 'address' => $address,
  646. 'status' => ApplyClass::STATUS_PASS,
  647. 'replace' => 1,//1表示供货商添加的客户
  648. ];
  649. $apply = ApplyService::replaceRetail($applyData);
  650. $id = $apply['id'] ?? 0;
  651. $respond = ApplyService::pass($id);
  652. $currentShop = $respond['shop'] ?? [];
  653. $sj = $respond['sj'] ?? [];
  654. $hdSjId = $sj['id'] ?? 0;
  655. $hdShopId = $currentShop->id ?? 0;
  656. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  657. if (empty($hdShopId)) {
  658. echo "{$mobile} 客户添加失败 \n";
  659. continue;
  660. }
  661. $custom = CustomClass::build($this->shopId, $hdShopId);
  662. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  663. $transaction->commit();
  664. echo "{$mobile} 添加成功! \n";
  665. } catch (\Exception $exception) {
  666. $transaction->rollBack();
  667. echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
  668. }
  669. }
  670. }
  671. }
  672. //更新特价 ssh 20230630
  673. public function actionDiscount()
  674. {
  675. ini_set('memory_limit', '2045M');
  676. set_time_limit(0);
  677. $list = ItemClass::getAllByCondition(['discountPrice>' => 0,], null, '*', null, true);
  678. if (!empty($list)) {
  679. foreach ($list as $item) {
  680. $discountPrice = $item->discountPrice ?? 0;
  681. $skMore = $item->skMore ?? 0;
  682. $addPrice = $item->addPrice ?? 0;
  683. $diff = bcsub($skMore, $addPrice, 2);
  684. if ($diff > 0) {
  685. $item->skDiscountPrice = bcadd($discountPrice, $diff, 2);
  686. } else {
  687. $item->skDiscountPrice = $discountPrice;
  688. }
  689. $item->hjDiscountPrice = $discountPrice;
  690. $item->save();
  691. }
  692. }
  693. }
  694. }