CustomClass.php 31 KB

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