CustomClass.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. <?php
  2. namespace bizGhs\custom\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\sj\classes\SjClass;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizGhs\book\classes\BookCustomClass;
  7. use bizGhs\book\classes\BookItemCustomClass;
  8. use bizGhs\custom\models\Custom;
  9. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  10. use bizGhs\shop\classes\MainClass;
  11. use bizGhs\shop\classes\ShopMoneyClass;
  12. use bizGhs\shop\classes\TotalDebtChangeClass;
  13. use bizGhs\ghs\classes\GhsBalanceClass;
  14. use bizGhs\ghs\classes\GhsRechargeClass;
  15. use bizGhs\shop\classes\ShopClass;
  16. use bizHd\member\classes\MemberLevelClass;
  17. use bizHd\saas\classes\ApplyClass;
  18. use bizHd\saas\services\ApplyService;
  19. use common\components\dict;
  20. use common\components\imgUtil;
  21. use common\components\noticeUtil;
  22. use common\components\stringUtil;
  23. use common\components\util;
  24. use common\components\orderSn;
  25. use bizHd\base\classes\BaseClass;
  26. use Yii;
  27. class CustomClass extends BaseClass
  28. {
  29. public static $baseFile = '\bizGhs\custom\models\Custom';
  30. const IS_DEBT_NO = 0;
  31. const IS_DEBT_YES = 1;
  32. const NOT_BLACK = 1;
  33. const IS_BLACK = 2;
  34. const LEVEL_SK = 0; //散客
  35. const LEVEL_PT = 1; //花店
  36. const LEVEL_HJ = 2; //同行
  37. const LEVEL_ZS = 3; //金店
  38. public static $levelMap = [
  39. self::LEVEL_SK => '散客',
  40. self::LEVEL_PT => '花店',
  41. self::LEVEL_HJ => '同行',
  42. self::LEVEL_ZS => '金店',
  43. ];
  44. public static $nickNameMap = [
  45. self::LEVEL_SK => '零售',
  46. self::LEVEL_PT => '批发',
  47. self::LEVEL_HJ => '同行',
  48. self::LEVEL_ZS => '金店',
  49. ];
  50. public static $levelPriceKeyMap = [
  51. self::LEVEL_SK => 'skPrice',
  52. self::LEVEL_PT => 'price',
  53. self::LEVEL_HJ => 'hjPrice',
  54. self::LEVEL_ZS => 'zsPrice',
  55. ];
  56. public static $levelAddPriceKeyMap = [
  57. self::LEVEL_SK => 'skMore',
  58. self::LEVEL_PT => 'addPrice',
  59. self::LEVEL_HJ => 'hjAddPrice',
  60. self::LEVEL_ZS => 'zsAddPrice',
  61. ];
  62. public static function pullOtherShopCustom($data)
  63. {
  64. $connection = Yii::$app->db;
  65. $transaction = $connection->beginTransaction();
  66. try {
  67. $toShopId = $data['toShopId'] ?? 0;
  68. $fromShopId = $data['fromShopId'] ?? 0;
  69. //指定当前环境为批发商,重要!!!
  70. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  71. $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
  72. if (!empty($fromCustomList)) {
  73. foreach ($fromCustomList as $fromCustomer) {
  74. $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
  75. if (!$exist) {
  76. CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
  77. }
  78. }
  79. }
  80. $transaction->commit();
  81. return true;
  82. } catch (\Exception $exception) {
  83. $transaction->rollBack();
  84. noticeUtil::push("批量拉取客户时报错了,fromShopId:{$fromShopId} toShopId:{$toShopId},报错内容:" . $exception->getMessage(), '15280215347');
  85. }
  86. return true;
  87. }
  88. public static function generateCustom($data)
  89. {
  90. $connection = Yii::$app->db;
  91. $transaction = $connection->beginTransaction();
  92. try {
  93. // 组织表单数据
  94. $shopName = $data['name'] ?? '';
  95. $mobile = $data['mobile'] ?? '';
  96. $post = [];
  97. $post['introSjId'] = $data['sjId'] ?? '';
  98. $post['introShopId'] = $data['shopId'] ?? '';
  99. $post['introSjName'] = $data['sjName'] ?? '';
  100. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  101. $post['name'] = $shopName;
  102. if (empty($shopName)) {
  103. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  104. $transaction->rollBack();
  105. return true;
  106. }
  107. $post['mobile'] = $mobile;
  108. if (!stringUtil::isMobile($mobile)) {
  109. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  110. $transaction->rollBack();
  111. return true;
  112. }
  113. if (stringUtil::getWordNum($shopName) > 15) {
  114. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
  115. $transaction->rollBack();
  116. return true;
  117. }
  118. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  119. if (!empty($has)) {
  120. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了哦", '15280215347');
  121. $transaction->rollBack();
  122. return true;
  123. }
  124. $hasShop = \biz\shop\classes\ShopClass::getByCondition(['mobile' => $mobile]);
  125. if (!empty($hasShop)) {
  126. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  127. $transaction->rollBack();
  128. return true;
  129. }
  130. $adminName = $post['adminName'] ?? '';
  131. $miniOpenId = $post['miniOpenId'] ?? '';
  132. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  133. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  134. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  135. $adminId = $admin['id'] ?? 0;
  136. $post['adminId'] = $adminId;
  137. $post['adminName'] = $admin['name'] ?? '';
  138. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  139. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  140. $post['from'] = ApplyClass::FROM_RETAIL;
  141. $post['style'] = SjClass::STYLE_RETAIL;
  142. //增加或更新申请
  143. $respond = ApplyService::replaceRetail($post);
  144. $id = $respond['id'] ?? 0;
  145. //后面的代码,要用到此参数,重要
  146. Yii::$app->params['ptStyle'] = 2;
  147. ApplyService::pass($id, '批发商手动添加');
  148. $transaction->commit();
  149. //只要手动添加的都标记为花店,任何情况都不会标记为散客,任何情况都是审核通过的状态
  150. $customList = CustomClass::getAllByCondition(['mobile' => $mobile], null, '*', null, true);
  151. if (!empty($customList)) {
  152. foreach ($customList as $custom) {
  153. $custom->isHd = 1;
  154. $custom->passStatus = 1;
  155. $custom->save();
  156. $ghsId = $custom->ghsId;
  157. GhsClass::updateById($ghsId, ['passStatus' => 1]);
  158. }
  159. }
  160. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  161. return true;
  162. } catch (\Exception $exception) {
  163. $transaction->rollBack();
  164. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  165. }
  166. return true;
  167. }
  168. //预订相关表同步更新货位 ssh 20240729
  169. public static function syncBookSeat($shop, $custom, $seatSn)
  170. {
  171. $bookSn = $shop->bookSn ?? 0;
  172. if (empty($bookSn)) {
  173. return true;
  174. }
  175. $customId = $custom->id ?? 0;
  176. BookItemCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
  177. BookCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
  178. }
  179. //消费余额 ssh 20240311
  180. public static function clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clear)
  181. {
  182. $customId = $custom->id ?? 0;
  183. $customName = $custom->name ?? '';
  184. $ghsId = $ghs->id ?? 0;
  185. $relateId = $clear->id ?? 0;
  186. $staffId = $staff->id ?? 0;
  187. $staffName = $staff->name ?? '';
  188. $clearSn = $clear->orderSn;
  189. $mainId = $shop->mainId ?? 0;
  190. $shopId = $shop->id ?? 0;
  191. $sjId = $shop->sjId ?? 0;
  192. $newGhsBalance = bcsub($ghs->balance, $amount, 2);
  193. $ghs->balance = $newGhsBalance;
  194. $ghs->save();
  195. $newCustomBalance = bcsub($custom->balance, $amount, 2);
  196. $custom->balance = $newCustomBalance;
  197. $custom->save();
  198. if ($newGhsBalance < 0) {
  199. util::fail('余额有问题哦..');
  200. }
  201. if (floatval($newGhsBalance) != floatval($newCustomBalance)) {
  202. util::fail('余额有问题...');
  203. }
  204. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomUseBalanceClear', 'id');
  205. $fromType = dict::getDict('fromType', 'shop');
  206. $cbData = [
  207. 'customId' => $customId,
  208. 'customName' => $customName,
  209. 'relateId' => $relateId,
  210. 'onlinePay' => 1,
  211. 'ptStyle' => 2,
  212. 'capitalType' => $capitalType,
  213. 'amount' => $amount,
  214. 'balance' => $newCustomBalance,
  215. 'staffId' => $staffId,
  216. 'staffName' => $staffName,
  217. 'io' => 0,
  218. 'side' => 0,
  219. 'payWay' => 0,
  220. 'fromType' => $fromType,
  221. 'event' => '结账 ' . $clearSn,
  222. 'mainId' => $mainId,
  223. 'shopId' => $shopId,
  224. 'sjId' => $sjId,
  225. 'remark' => '',
  226. ];
  227. $cbc = CustomBalanceChangeClass::add($cbData, true);
  228. $customShopId = $custom->shopId ?? 0;
  229. $customShop = ShopClass::getById($customShopId, true);
  230. if (empty($customShop)) {
  231. util::fail('没有找到客户门店呢..');
  232. }
  233. $customShopSjId = $customShop->sjId ?? 0;
  234. $customShopMainId = $customShop->mainId ?? 0;
  235. $capitalType = dict::getDict('capitalType', 'customAskGhsUseBalanceClear', 'id');
  236. $gbData = [
  237. 'ghsId' => $ghsId,
  238. 'relateId' => $relateId,
  239. 'ptStyle' => 2,
  240. 'capitalType' => $capitalType,
  241. 'amount' => $amount,
  242. 'balance' => $newGhsBalance,
  243. 'io' => 0,
  244. 'side' => 0,
  245. 'onlinePay' => 1,
  246. 'payWay' => 0,
  247. 'fromType' => $fromType,
  248. 'event' => '结账 ' . $clearSn,
  249. 'sjId' => $customShopSjId,
  250. 'mainId' => $customShopMainId,
  251. 'shopId' => $customShopId,
  252. 'remark' => '',
  253. ];
  254. $gbc = GhsBalanceChangeClass::add($gbData, true);
  255. $clear->customBalanceChangeId = $cbc->id ?? 0;
  256. $clear->ghsBalanceChangeId = $gbc->id ?? 0;
  257. $clear->save();
  258. }
  259. //充值金额,有多个地方要同步修改,请搜索关键词custom_ghs_recharge ssh 20240310
  260. public static function rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params = [])
  261. {
  262. $ghsId = $custom->ghsId ?? 0;
  263. $ghsInfo = GhsClass::getLockById($ghsId);
  264. if (empty($ghsInfo)) {
  265. util::fail('没有找到供货商');
  266. }
  267. $ghsShopId = $ghsInfo->shopId ?? 0;
  268. $ghsName = $ghsInfo->name ?? '';
  269. $customId = $custom->id ?? 0;
  270. $customShopId = $custom->shopId ?? 0;
  271. $customName = $custom->name ?? '';
  272. $customShop = ShopClass::getById($customShopId, true);
  273. if (empty($customShop)) {
  274. util::fail('没有找到客户门店');
  275. }
  276. $payTime = date("Y-m-d H:i:s");
  277. $customShopSjId = $customShop->sjId ?? 0;
  278. $customShopMainId = $customShop->mainId ?? 0;
  279. $custom->balance = bcadd($custom->balance, $amount, 2);
  280. $custom->save();
  281. $orderSn = orderSn::getGhsCustomRechargeSn();
  282. $shopId = $shop->id ?? 0;
  283. $sjId = $shop->sjId ?? 0;
  284. $mainId = $shop->mainId ?? 0;
  285. $staffId = $staff->id ?? 0;
  286. $staffName = $staff->name ?? '';
  287. $remark = $params['remark'] ?? '';
  288. $rechargeType = $params['rechargeType'] ?? 0;
  289. $payMap = dict::getDict('payWayName');
  290. $payName = $payMap[$payWay] ?? '';
  291. $rechargeEvent = $payName . '充值';
  292. if ($rechargeType == 1) {
  293. $rechargeEvent = '售后返充';
  294. }
  295. $cData = [
  296. 'orderSn' => $orderSn,
  297. 'onlinePay' => 1,
  298. 'payWay' => $payWay,
  299. 'shopId' => $shopId,
  300. 'mainId' => $mainId,
  301. 'amount' => $amount,
  302. 'balance' => $custom->balance,
  303. 'side' => 0,
  304. 'staffId' => $staffId,
  305. 'staffName' => $staffName,
  306. 'payStatus' => 1,
  307. 'status' => 1,
  308. 'ghsId' => $ghsId,
  309. 'ghsShopId' => $ghsShopId,
  310. 'ghsName' => $ghsName,
  311. 'customId' => $customId,
  312. 'customShopId' => $customShopId,
  313. 'customName' => $customName,
  314. 'remark' => $remark,
  315. 'payTime' => $payTime,
  316. 'rechargeType' => $rechargeType,
  317. ];
  318. $cRecharge = CustomRechargeClass::add($cData, true);
  319. $cRechargeId = $cRecharge->id ?? 0;
  320. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRecharge', 'id');
  321. if ($rechargeType == 1) {
  322. //售后返充
  323. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRechargeReturn', 'id');
  324. }
  325. $fromType = dict::getDict('fromType', 'shop');
  326. $cbData = [
  327. 'customId' => $customId,
  328. 'customName' => $customName,
  329. 'relateId' => $cRechargeId,
  330. 'onlinePay' => 1,
  331. 'ptStyle' => 2,
  332. 'capitalType' => $capitalType,
  333. 'amount' => $amount,
  334. 'balance' => $custom->balance,
  335. 'staffId' => $staffId,
  336. 'staffName' => $staffName,
  337. 'io' => 1,
  338. 'side' => 0,
  339. 'payWay' => $payWay,
  340. 'fromType' => $fromType,
  341. 'event' => $rechargeEvent,
  342. 'mainId' => $mainId,
  343. 'shopId' => $shopId,
  344. 'sjId' => $sjId,
  345. 'remark' => $remark,
  346. ];
  347. CustomBalanceChangeClass::add($cbData, true);
  348. $ghsInfo->balance = bcadd($ghsInfo->balance, $amount, 2);
  349. $ghsInfo->save();
  350. $orderSn = orderSn::getGhsRechargeSn();
  351. $customMainId = $custom->mainId ?? 0;
  352. $gData = [
  353. 'orderSn' => $orderSn,
  354. 'onlinePay' => 1,
  355. 'payWay' => $payWay,
  356. 'shopId' => $customShopId,
  357. 'mainId' => $customMainId,
  358. 'amount' => $amount,
  359. 'balance' => $ghsInfo->balance,
  360. 'side' => 0,
  361. 'staffId' => 0,
  362. 'staffName' => '',
  363. 'payStatus' => 1,
  364. 'status' => 1,
  365. 'ghsId' => $ghsId,
  366. 'ghsShopId' => $ghsShopId,
  367. 'ghsName' => $ghsName,
  368. 'customId' => $customId,
  369. 'customShopId' => $customShopId,
  370. 'customName' => $customName,
  371. 'remark' => $remark,
  372. 'payTime' => $payTime,
  373. 'rechargeType' => $rechargeType,
  374. ];
  375. $gRecharge = GhsRechargeClass::add($gData, true);
  376. $gRechargeId = $gRecharge->id ?? 0;
  377. $capitalType = dict::getDict('capitalType', 'customAskGhsRecharge', 'id');
  378. if ($rechargeType == 1) {
  379. //售后返充
  380. $capitalType = dict::getDict('capitalType', 'customAskGhsRechargeReturn', 'id');
  381. }
  382. $gbData = [
  383. 'ghsId' => $ghsId,
  384. 'ghsName' => $ghsName,
  385. 'relateId' => $gRechargeId,
  386. 'ptStyle' => 2,
  387. 'capitalType' => $capitalType,
  388. 'amount' => $amount,
  389. 'balance' => $ghsInfo->balance,
  390. 'io' => 1,
  391. 'side' => 0,
  392. 'onlinePay' => 1,
  393. 'payWay' => $payWay,
  394. 'fromType' => $fromType,
  395. 'event' => $rechargeEvent,
  396. 'sjId' => $customShopSjId,
  397. 'mainId' => $customShopMainId,
  398. 'shopId' => $customShopId,
  399. 'remark' => $remark,
  400. ];
  401. GhsBalanceChangeClass::add($gbData, true);
  402. // 现金充值,同步更新表 xhMain 的 money 字段
  403. if ($payWay == 4) {
  404. $m = \bizGhs\shop\classes\MainClass::getById($mainId, true, 'id, money');
  405. $balance = $m->money;
  406. $m->money = bcadd($m->money, $amount);
  407. $m->save();
  408. $event = '客户(' . $customName . ')充值' . $amount . '元';
  409. $data = [
  410. 'amount' => $amount,
  411. 'balance' => $balance,
  412. 'io' => 1,
  413. 'mainId' => $mainId,
  414. 'capitalType' => $capitalType,
  415. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  416. 'event' => $event,
  417. 'remark' => $remark,
  418. ];
  419. ShopMoneyClass::addData($data);
  420. }
  421. return ['custom' => $custom, 'ghs' => $ghsInfo, 'customRecharge' => $cRecharge, 'ghsRecharge' => $gRecharge];
  422. }
  423. public static function createSeatSn($mainId)
  424. {
  425. $max = Custom::find(['mainId' => $mainId])->max('seatSn');
  426. $new = bcadd($max, 1);
  427. return $new;
  428. }
  429. //建立客户和供货商关系
  430. public static function build($ghsShopId, $hdShopId, $changeName = '')
  431. {
  432. return GhsClass::build($ghsShopId, $hdShopId, 2, $changeName);
  433. }
  434. //添加客户 ssh 2021.2.24
  435. public static function addCustom($data)
  436. {
  437. return self::add($data);
  438. }
  439. //获取客户信息 ssh 2021.2.4
  440. public static function getCustom($id)
  441. {
  442. $info = self::getById($id);
  443. if (empty($info)) {
  444. return $info;
  445. }
  446. $respond = self::groupBaseInfo([$info]);
  447. return current($respond);
  448. }
  449. public static function getCustomByIds($ids)
  450. {
  451. $list = self::getByIds($ids, null, 'id');
  452. return self::groupBaseInfo($list);
  453. }
  454. //整合头像等信息 ssh 2021.1.8
  455. public static function groupBaseInfo($list, $mainId = 0)
  456. {
  457. if (empty($list)) {
  458. return $list;
  459. }
  460. //片区划分
  461. $distMap = [];
  462. $custom = current($list);
  463. if (!empty($custom) && !empty($custom['ownMainId'])) {
  464. $distMap = DistClass::getAllByCondition(['mainId' => $custom['ownMainId']], null, '*', 'id');
  465. }
  466. $levelMap = self::$levelMap;
  467. foreach ($list as $key => $val) {
  468. $smallAvatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png') . '?x-oss-process=image/resize,m_fill,h_130,w_130';
  469. $avatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png');
  470. $list[$key]['smallAvatar'] = $smallAvatar;
  471. $list[$key]['avatar'] = $avatar;
  472. $list[$key]['sn'] = $val['id'];
  473. $level = $val['level'] ?? 1;
  474. $levelName = $levelMap[$level] ?? '';
  475. $list[$key]['levelName'] = $levelName;
  476. $debtAmount = $val['debtAmount'] ?? 0;
  477. $balance = $val['balance'] ?? 0;
  478. $remainDebtAmount = bcsub($debtAmount, $balance, 2);
  479. $list[$key]['remainDebtAmount'] = $remainDebtAmount;
  480. //前端使用,走播,预订客户添加新客户
  481. $list[$key]['zbNum'] = '';
  482. $list[$key]['zbPrice'] = '';
  483. //片区划分
  484. $distId = $val['distId'] ?? 0;
  485. $distInfo = $distMap[$distId] ?? [];
  486. $distName = $distInfo['name'] ?? '';
  487. //注意区别 dist 字段,dist字段是地图里的区
  488. $list[$key]['distName'] = $distName;
  489. //超时未下单
  490. $recentExpend = $val['recentExpend'] ?? '';
  491. $list[$key]['overTimeUnExpend'] = 0;
  492. if ($recentExpend == '0000-00-00 00:00:00') {
  493. $list[$key]['overTimeUnExpend'] = 10000;
  494. } else {
  495. $recentExpendTime = strtotime($recentExpend);
  496. $seven = bcmul(86400, 7);
  497. $sevenTime = bcadd($recentExpendTime, $seven);
  498. if ($sevenTime < time()) {
  499. $list[$key]['overTimeUnExpend'] = 7;
  500. }
  501. $month = bcmul(86400, 30);
  502. $monthTime = bcadd($recentExpendTime, $month);
  503. if ($monthTime < time()) {
  504. $list[$key]['overTimeUnExpend'] = 30;
  505. }
  506. $halfYear = bcmul(86400, 180);
  507. $halfYearTime = bcadd($recentExpendTime, $halfYear);
  508. if ($halfYearTime < time()) {
  509. $list[$key]['overTimeUnExpend'] = 180;
  510. }
  511. $year = bcmul(86400, 365);
  512. $yearTime = bcmul($recentExpendTime, $year);
  513. if ($yearTime < time()) {
  514. $list[$key]['overTimeUnExpend'] = 365;
  515. }
  516. }
  517. }
  518. return $list;
  519. }
  520. //欠款权限开关 ssh 2021.1.8
  521. public static function debt($custom, $debt)
  522. {
  523. $id = $custom['id'] ?? 0;
  524. self::updateById($id, ['debt' => $debt]);
  525. return true;
  526. }
  527. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  528. public static function black($custom, $black)
  529. {
  530. $id = $custom['id'] ?? 0;
  531. self::updateById($id, ['black' => $black]);
  532. return true;
  533. }
  534. //权限判断 ssh 2021.1.8
  535. public static function valid($custom, $shopId)
  536. {
  537. if (empty($custom)) {
  538. util::fail('没有找到客户');
  539. }
  540. if ($custom['ownShopId'] != $shopId) {
  541. util::fail('您无法操作此客户');
  542. }
  543. }
  544. //客户采购欠款金额增加 ssh 20220306
  545. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
  546. {
  547. $amount = $order->remainDebtPrice ?? 0;
  548. $custom->isDebt = CustomClass::IS_DEBT_YES;
  549. $debtLimit = $custom->debtLimit ?? 0;
  550. if (!$bookOrder) {
  551. if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
  552. //客户自己下单要受额度限制
  553. if ($custom->debtAmount > $debtLimit) {
  554. //虚拟供货商和客户关系不受影响
  555. if ($custom->live == 1) {
  556. util::fail("已欠款{$custom->debtAmount},请先结账哦");
  557. }
  558. }
  559. }
  560. }
  561. $balance = bcadd($custom->debtAmount, $amount, 2);
  562. $mustNotice = false;
  563. if (getenv('YII_ENV') == 'production') {
  564. //王品牛排 黄先生
  565. if ($custom->id == 11726) {
  566. $mustNotice = true;
  567. }
  568. } else {
  569. if ($custom->id == 577) {
  570. $mustNotice = true;
  571. }
  572. }
  573. if (getenv('YII_ENV', 'local') == 'production') {
  574. if ($mustNotice) {
  575. noticeUtil::push("【开单动作 | 客户】变化前欠款:{$custom->debtAmount} 变化金额:{$amount} 变化后欠款:{$balance}", '15280215347');
  576. }
  577. }
  578. $custom->debtAmount = $balance;
  579. $custom->save();
  580. $relateId = $order->id ?? 0;
  581. $customId = $custom->id ?? 0;
  582. $customName = $custom->name ?? '';
  583. $orderSn = $order->orderSn ?? '';
  584. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  585. $event = '购买花材,单号:' . $orderSn;
  586. if ($bookOrder) {
  587. $event = '预订单多退少补,单号:' . $orderSn;
  588. }
  589. $change = [
  590. 'relateId' => $relateId,
  591. 'customId' => $customId,
  592. 'customName' => $customName,
  593. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  594. 'capitalType' => $capitalType,
  595. 'amount' => $amount,
  596. 'balance' => $balance,
  597. 'io' => 1,
  598. 'payWay' => $order->payWay,
  599. 'event' => $event,
  600. 'sjId' => $order->sjId,
  601. 'shopId' => $order->shopId,
  602. 'mainId' => $order->mainId,
  603. ];
  604. CustomDebtChangeClass::addChange($change);
  605. $mainId = $order->mainId ?? 0;
  606. $main = MainClass::getLockById($mainId);
  607. if (empty($main)) {
  608. util::fail('没有main信息8');
  609. }
  610. $debt = $main->debt ?? 0;
  611. $remain = bcadd($debt, $amount, 2);
  612. if (getenv('YII_ENV', 'local') == 'production') {
  613. if ($mustNotice) {
  614. noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
  615. }
  616. }
  617. $main->debt = $remain;
  618. $main->save();
  619. $event = $custom->name . ' 购买花材,单号:' . $orderSn;
  620. $change['balance'] = $remain;
  621. $change['event'] = $event;
  622. TotalDebtChangeClass::addChange($change);
  623. }
  624. //客户结账欠款金额减少 ssh 20220306
  625. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  626. {
  627. $debtAmount = $custom->debtAmount ?? 0.00;
  628. $remain = bcsub($debtAmount, $amount, 2);
  629. if ($remain < 0) {
  630. noticeUtil::push("客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  631. util::fail('收款失败,结账后欠款金额会出现负数');
  632. }
  633. $custom->isDebt = $remain <= 0 ? 0 : 1;
  634. $custom->debtAmount = $remain;
  635. $custom->save();
  636. $relateId = $clear->id ?? 0;
  637. $customId = $custom->id ?? 0;
  638. $customName = $custom->name ?? '';
  639. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  640. $orderSn = $clear->orderSn ?? '';
  641. $change = [
  642. 'relateId' => $relateId,
  643. 'customId' => $customId,
  644. 'customName' => $customName,
  645. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  646. 'capitalType' => $capitalType,
  647. 'amount' => $amount,
  648. 'balance' => $remain,
  649. 'io' => 0,
  650. 'payWay' => 0,
  651. 'event' => "结账,单号:{$orderSn}",
  652. 'sjId' => $ghs->sjId,
  653. 'shopId' => $ghs->shopId,
  654. 'mainId' => $ghs->mainId,
  655. ];
  656. CustomDebtChangeClass::addChange($change);
  657. $mainId = $ghs->mainId ?? 0;
  658. $main = MainClass::getLockById($mainId);
  659. if (empty($main)) {
  660. util::fail('没有main信息9');
  661. }
  662. $debt = $main->debt ?? 0;
  663. $remain = bcsub($debt, $amount, 2);
  664. $main->debt = $remain;
  665. $main->save();
  666. $event = $custom->name . " 结账,单号:{$orderSn}";
  667. $change['balance'] = $remain;
  668. $change['event'] = $event;
  669. TotalDebtChangeClass::addChange($change);
  670. }
  671. //客户退款欠款金额减少 ssh 20220306
  672. public static function refundDebtAmountReduce($custom, $refund, $order)
  673. {
  674. $refundPrice = $refund->refundPrice ?? 0;
  675. $balance = bcsub($custom->debtAmount, $refundPrice, 2);
  676. $custom->debtAmount = $balance;
  677. $custom->save();
  678. $relateId = $refund->id ?? 0;
  679. $customId = $custom->id ?? 0;
  680. $customName = $custom->name ?? '';
  681. $saleOrderSn = $order->orderSn ?? '';
  682. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  683. $payTime = $order->payTime;
  684. $change = [
  685. 'relateId' => $relateId,
  686. 'customId' => $customId,
  687. 'customName' => $customName,
  688. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  689. 'capitalType' => $capitalType,
  690. 'amount' => $refundPrice,
  691. 'balance' => $balance,
  692. 'io' => 0,
  693. 'payWay' => 0,
  694. 'event' => "订单号:{$saleOrderSn} 退款",
  695. 'sjId' => $refund->sjId,
  696. 'shopId' => $refund->shopId,
  697. 'mainId' => $order->mainId,
  698. 'payTime' => $payTime,
  699. ];
  700. CustomDebtChangeClass::addChange($change);
  701. $mainId = $order->mainId ?? 0;
  702. $main = MainClass::getLockById($mainId);
  703. if (empty($main)) {
  704. util::fail('没有main信息10');
  705. }
  706. $debt = $main->debt ?? 0;
  707. $remain = bcsub($debt, $refundPrice, 2);
  708. $main->debt = $remain;
  709. $main->save();
  710. $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
  711. $change['balance'] = $remain;
  712. $change['event'] = $event;
  713. TotalDebtChangeClass::addChange($change);
  714. }
  715. public static function exportCustom($where, $sort, $mainId)
  716. {
  717. $list = self::getAllByCondition($where, $sort, '*', null);
  718. if (empty($list)) {
  719. util::fail('没有找到客户');
  720. }
  721. $list = self::groupBaseInfo($list);
  722. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  723. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  724. $objPHPExcel = new \PHPExcel();
  725. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  726. ->setLastModifiedBy("Maarten Balliauw")
  727. ->setTitle("Office 2007 XLSX Document")
  728. ->setSubject("Office 2007 XLSX Document")
  729. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  730. ->setKeywords("office 2007 openxml php")
  731. ->setCategory("file");
  732. $ghsTitle = '供货商';
  733. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  734. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  735. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  736. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  737. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  738. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  739. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  740. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  741. $objPHPExcel->getActiveSheet()->setCellValue('B1', '客户');
  742. $objPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
  743. $objPHPExcel->getActiveSheet()->setCellValue('D1', '欠款');
  744. $objPHPExcel->getActiveSheet()->setCellValue('E1', '余额');
  745. $objPHPExcel->getActiveSheet()->setCellValue('F1', '待结金额');
  746. $objPHPExcel->getActiveSheet()->setCellValue('G1', '最近下单');
  747. $objPHPExcel->getActiveSheet()->setCellValue('H1', '累计消费');
  748. $objPHPExcel->getActiveSheet()->setCellValue('I1', '累计消费次数');
  749. //设置宽度
  750. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  751. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  752. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  753. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  754. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  755. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
  756. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
  757. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
  758. //加粗
  759. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  760. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  761. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  762. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  763. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  764. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  765. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  766. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  767. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  768. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  769. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  770. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  771. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  772. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  773. $baseRow = 2;
  774. foreach ($list as $key => $custom) {
  775. $i = $baseRow + $key;
  776. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  777. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  778. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['mobile'], \PHPExcel_Cell_DataType::TYPE_STRING);
  779. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['debtAmount']);
  780. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['balance']);
  781. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['remainDebtAmount']);
  782. if ($custom['recentExpend'] == '0000-00-00 00:00:00') {
  783. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, '没有下过单');
  784. } else {
  785. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $custom['recentExpend']);
  786. }
  787. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $custom['buyAmount']);
  788. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $custom['buyNum']);
  789. //居左
  790. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  791. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  792. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  793. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  794. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  795. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  796. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  797. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  798. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  799. }
  800. $fileName = '客户-' . date("m-d");
  801. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  802. $objPHPExcel->setActiveSheetIndex(0);
  803. $dir = './priceTable/' . $mainId;
  804. if (file_exists($dir) == false) {
  805. mkdir($dir, 0777, true);
  806. }
  807. $date = $fileName;
  808. $file = $date . '.xls';
  809. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  810. if (file_exists($dir . '/' . $file)) {
  811. unlink($dir . '/' . $file);
  812. }
  813. $objWriter->save($dir . '/' . $file);
  814. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  815. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  816. }
  817. public static function showTotalBalance($pfShopId = 0, $lsShopId = 0)
  818. {
  819. ini_set('memory_limit', '2045M');
  820. set_time_limit(0);
  821. $pfAmount = 0;
  822. $lsAmount = 0;
  823. if (!empty($pfShopId)) {
  824. $custom = CustomClass::getAllByCondition(['ownShopId' => $pfShopId], null, '*', null, true);
  825. if (!empty($custom)) {
  826. foreach ($custom as $c) {
  827. $balance = $c->balance ?? 0;
  828. $debtAmount = $c->debtAmount ?? 0;
  829. $new = bcsub($balance, $debtAmount, 2);
  830. $pfAmount = bcadd($pfAmount, $new, 2);
  831. }
  832. }
  833. }
  834. if (!empty($lsShopId)) {
  835. $lsCustom = \bizHd\custom\classes\CustomClass::getAllByCondition(['shopId' => $lsShopId], null, '*', null, true);
  836. if (!empty($lsCustom)) {
  837. foreach ($lsCustom as $lc) {
  838. $balance = $lc->balance ?? 0;
  839. $lsAmount = bcadd($balance, $lsAmount, 2);
  840. }
  841. }
  842. }
  843. return ['pfAmount' => $pfAmount, 'lsAmount' => $lsAmount];
  844. }
  845. }