CustomClass.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. //超时未下单
  356. $recentExpend = $val['recentExpend'] ?? '';
  357. $list[$key]['overTimeUnExpend'] = 0;
  358. if ($recentExpend == '0000-00-00 00:00:00') {
  359. $list[$key]['overTimeUnExpend'] = 10000;
  360. } else {
  361. $recentExpendTime = strtotime($recentExpend);
  362. $seven = bcmul(86400, 7);
  363. $sevenTime = bcadd($recentExpendTime, $seven);
  364. if ($sevenTime < time()) {
  365. $list[$key]['overTimeUnExpend'] = 7;
  366. }
  367. $month = bcmul(86400, 30);
  368. $monthTime = bcadd($recentExpendTime, $month);
  369. if ($monthTime < time()) {
  370. $list[$key]['overTimeUnExpend'] = 30;
  371. }
  372. $halfYear = bcmul(86400, 180);
  373. $halfYearTime = bcadd($recentExpendTime, $halfYear);
  374. if ($halfYearTime < time()) {
  375. $list[$key]['overTimeUnExpend'] = 180;
  376. }
  377. $year = bcmul(86400, 365);
  378. $yearTime = bcmul($recentExpendTime, $year);
  379. if ($yearTime < time()) {
  380. $list[$key]['overTimeUnExpend'] = 365;
  381. }
  382. }
  383. }
  384. return $list;
  385. }
  386. //欠款权限开关 ssh 2021.1.8
  387. public static function debt($custom, $debt)
  388. {
  389. $id = isset($custom['id']) ? $custom['id'] : 0;
  390. self::updateById($id, ['debt' => $debt]);
  391. return true;
  392. }
  393. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  394. public static function black($custom, $black)
  395. {
  396. $id = isset($custom['id']) ? $custom['id'] : 0;
  397. self::updateById($id, ['black' => $black]);
  398. return true;
  399. }
  400. //权限判断 ssh 2021.1.8
  401. public static function valid($custom, $shopId)
  402. {
  403. if (empty($custom)) {
  404. util::fail('没有找到客户');
  405. }
  406. if ($custom['ownShopId'] != $shopId) {
  407. util::fail('您无法操作此客户');
  408. }
  409. }
  410. //客户采购欠款金额增加 ssh 20220306
  411. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
  412. {
  413. $amount = $order->remainDebtPrice ?? 0;
  414. $custom->isDebt = CustomClass::IS_DEBT_YES;
  415. $debtLimit = $custom->debtLimit ?? 0;
  416. if (!$bookOrder) {
  417. if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
  418. //客户自己下单要受额度限制
  419. if ($custom->debtAmount > $debtLimit) {
  420. //虚拟供货商和客户关系不受影响
  421. if($custom->live == 1){
  422. util::fail("已欠款{$custom->debtAmount},请先结账哦");
  423. }
  424. }
  425. }
  426. }
  427. $balance = bcadd($custom->debtAmount, $amount, 2);
  428. $mustNotice = false;
  429. if (getenv('YII_ENV') == 'production') {
  430. //王品牛排 黄先生
  431. if ($custom->id == 11726) {
  432. $mustNotice = true;
  433. }
  434. } else {
  435. if ($custom->id == 577) {
  436. $mustNotice = true;
  437. }
  438. }
  439. if (getenv('YII_ENV', 'local') == 'production') {
  440. if ($mustNotice) {
  441. noticeUtil::push("【开单动作 | 客户】变化前欠款:{$custom->debtAmount} 变化金额:{$amount} 变化后欠款:{$balance}", '15280215347');
  442. }
  443. }
  444. $custom->debtAmount = $balance;
  445. $custom->save();
  446. $relateId = $order->id ?? 0;
  447. $customId = $custom->id ?? 0;
  448. $customName = $custom->name ?? '';
  449. $orderSn = $order->orderSn ?? '';
  450. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  451. $event = '购买花材,单号:' . $orderSn;
  452. if ($bookOrder) {
  453. $event = '预订单多退少补,单号:' . $orderSn;
  454. }
  455. $change = [
  456. 'relateId' => $relateId,
  457. 'customId' => $customId,
  458. 'customName' => $customName,
  459. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  460. 'capitalType' => $capitalType,
  461. 'amount' => $amount,
  462. 'balance' => $balance,
  463. 'io' => 1,
  464. 'payWay' => $order->payWay,
  465. 'event' => $event,
  466. 'sjId' => $order->sjId,
  467. 'shopId' => $order->shopId,
  468. 'mainId' => $order->mainId,
  469. ];
  470. CustomDebtChangeClass::addChange($change);
  471. $mainId = $order->mainId ?? 0;
  472. $main = MainClass::getLockById($mainId);
  473. if (empty($main)) {
  474. util::fail('没有main信息8');
  475. }
  476. $debt = $main->debt ?? 0;
  477. $remain = bcadd($debt, $amount, 2);
  478. if (getenv('YII_ENV', 'local') == 'production') {
  479. if ($mustNotice) {
  480. noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
  481. }
  482. }
  483. $main->debt = $remain;
  484. $main->save();
  485. $event = $custom->name . ' 购买花材,单号:' . $orderSn;
  486. $change['balance'] = $remain;
  487. $change['event'] = $event;
  488. TotalDebtChangeClass::addChange($change);
  489. }
  490. //客户结账欠款金额减少 ssh 20220306
  491. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  492. {
  493. $debtAmount = $custom->debtAmount ?? 0.00;
  494. $remain = bcsub($debtAmount, $amount, 2);
  495. if ($remain < 0) {
  496. noticeUtil::push("客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  497. util::fail('收款失败,结账后欠款金额会出现负数');
  498. }
  499. $custom->isDebt = $remain <= 0 ? 0 : 1;
  500. $custom->debtAmount = $remain;
  501. $custom->save();
  502. $relateId = $clear->id ?? 0;
  503. $customId = $custom->id ?? 0;
  504. $customName = $custom->name ?? '';
  505. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  506. $orderSn = $clear->orderSn ?? '';
  507. $change = [
  508. 'relateId' => $relateId,
  509. 'customId' => $customId,
  510. 'customName' => $customName,
  511. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  512. 'capitalType' => $capitalType,
  513. 'amount' => $amount,
  514. 'balance' => $remain,
  515. 'io' => 0,
  516. 'payWay' => 0,
  517. 'event' => "结账,单号:{$orderSn}",
  518. 'sjId' => $ghs->sjId,
  519. 'shopId' => $ghs->shopId,
  520. 'mainId' => $ghs->mainId,
  521. ];
  522. CustomDebtChangeClass::addChange($change);
  523. $mainId = $ghs->mainId ?? 0;
  524. $main = MainClass::getLockById($mainId);
  525. if (empty($main)) {
  526. util::fail('没有main信息9');
  527. }
  528. $debt = $main->debt ?? 0;
  529. $remain = bcsub($debt, $amount, 2);
  530. $main->debt = $remain;
  531. $main->save();
  532. $event = $custom->name . " 结账,单号:{$orderSn}";
  533. $change['balance'] = $remain;
  534. $change['event'] = $event;
  535. TotalDebtChangeClass::addChange($change);
  536. }
  537. //客户退款欠款金额减少 ssh 20220306
  538. public static function refundDebtAmountReduce($custom, $refund, $order)
  539. {
  540. $refundPrice = $refund->refundPrice ?? 0;
  541. $balance = bcsub($custom->debtAmount, $refundPrice, 2);
  542. $custom->debtAmount = $balance;
  543. $custom->save();
  544. $relateId = $refund->id ?? 0;
  545. $customId = $custom->id ?? 0;
  546. $customName = $custom->name ?? '';
  547. $saleOrderSn = $order->orderSn ?? '';
  548. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  549. $payTime = $order->payTime;
  550. $change = [
  551. 'relateId' => $relateId,
  552. 'customId' => $customId,
  553. 'customName' => $customName,
  554. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  555. 'capitalType' => $capitalType,
  556. 'amount' => $refundPrice,
  557. 'balance' => $balance,
  558. 'io' => 0,
  559. 'payWay' => 0,
  560. 'event' => "订单号:{$saleOrderSn} 退款",
  561. 'sjId' => $refund->sjId,
  562. 'shopId' => $refund->shopId,
  563. 'mainId' => $order->mainId,
  564. 'payTime' => $payTime,
  565. ];
  566. CustomDebtChangeClass::addChange($change);
  567. $mainId = $order->mainId ?? 0;
  568. $main = MainClass::getLockById($mainId);
  569. if (empty($main)) {
  570. util::fail('没有main信息10');
  571. }
  572. $debt = $main->debt ?? 0;
  573. $remain = bcsub($debt, $refundPrice, 2);
  574. $main->debt = $remain;
  575. $main->save();
  576. $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
  577. $change['balance'] = $remain;
  578. $change['event'] = $event;
  579. TotalDebtChangeClass::addChange($change);
  580. }
  581. public static function exportCustom($where, $sort, $mainId)
  582. {
  583. $list = self::getAllByCondition($where, $sort, '*', null);
  584. if (empty($list)) {
  585. util::fail('没有找到客户');
  586. }
  587. $list = self::groupBaseInfo($list);
  588. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  589. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  590. $objPHPExcel = new \PHPExcel();
  591. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  592. ->setLastModifiedBy("Maarten Balliauw")
  593. ->setTitle("Office 2007 XLSX Document")
  594. ->setSubject("Office 2007 XLSX Document")
  595. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  596. ->setKeywords("office 2007 openxml php")
  597. ->setCategory("file");
  598. $ghsTitle = '供货商';
  599. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  600. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  601. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  602. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  603. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  604. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  605. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  606. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  607. $objPHPExcel->getActiveSheet()->setCellValue('B1', '客户');
  608. $objPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
  609. $objPHPExcel->getActiveSheet()->setCellValue('D1', '欠款');
  610. $objPHPExcel->getActiveSheet()->setCellValue('E1', '余额');
  611. $objPHPExcel->getActiveSheet()->setCellValue('F1', '待结金额');
  612. $objPHPExcel->getActiveSheet()->setCellValue('G1', '最近下单');
  613. $objPHPExcel->getActiveSheet()->setCellValue('H1', '累计消费');
  614. $objPHPExcel->getActiveSheet()->setCellValue('I1', '累计消费次数');
  615. //设置宽度
  616. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  617. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  618. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  619. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  620. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  621. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
  622. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
  623. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
  624. //加粗
  625. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  626. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  627. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  628. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  629. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  630. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  631. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  632. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  633. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  634. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  635. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  636. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  637. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  638. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  639. $baseRow = 2;
  640. foreach ($list as $key => $custom) {
  641. $i = $baseRow + $key;
  642. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  643. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  644. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['mobile'], \PHPExcel_Cell_DataType::TYPE_STRING);
  645. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['debtAmount']);
  646. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['balance']);
  647. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['remainDebtAmount']);
  648. if ($custom['recentExpend'] == '0000-00-00 00:00:00') {
  649. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, '没有下过单');
  650. } else {
  651. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $custom['recentExpend']);
  652. }
  653. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $custom['buyAmount']);
  654. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $custom['buyNum']);
  655. //居左
  656. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  657. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  658. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  659. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  660. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  661. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  662. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  663. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  664. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  665. }
  666. $fileName = '客户-' . date("m-d");
  667. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  668. $objPHPExcel->setActiveSheetIndex(0);
  669. $dir = './priceTable/' . $mainId;
  670. if (file_exists($dir) == false) {
  671. mkdir($dir, 0777, true);
  672. }
  673. $date = $fileName;
  674. $file = $date . '.xls';
  675. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  676. if (file_exists($dir . '/' . $file)) {
  677. unlink($dir . '/' . $file);
  678. }
  679. $objWriter->save($dir . '/' . $file);
  680. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  681. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  682. }
  683. public static function showTotalBalance($pfShopId = 0, $lsShopId = 0)
  684. {
  685. ini_set('memory_limit', '2045M');
  686. set_time_limit(0);
  687. $pfAmount = 0;
  688. $lsAmount = 0;
  689. if (!empty($pfShopId)) {
  690. $custom = CustomClass::getAllByCondition(['ownShopId' => $pfShopId], null, '*', null, true);
  691. if (!empty($custom)) {
  692. foreach ($custom as $c) {
  693. $balance = $c->balance ?? 0;
  694. $debtAmount = $c->debtAmount ?? 0;
  695. $new = bcsub($balance, $debtAmount, 2);
  696. $pfAmount = bcadd($pfAmount, $new, 2);
  697. }
  698. }
  699. }
  700. if (!empty($lsShopId)) {
  701. $lsCustom = \bizHd\custom\classes\CustomClass::getAllByCondition(['shopId' => $lsShopId], null, '*', null, true);
  702. if (!empty($lsCustom)) {
  703. foreach ($lsCustom as $lc) {
  704. $balance = $lc->balance ?? 0;
  705. $lsAmount = bcadd($balance, $lsAmount, 2);
  706. }
  707. }
  708. }
  709. return ['pfAmount' => $pfAmount, 'lsAmount' => $lsAmount];
  710. }
  711. }