ItemController.php 29 KB

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