CustomClass.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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\order\classes\OrderClearClass;
  16. use bizGhs\shop\classes\ShopClass;
  17. use bizHd\member\classes\MemberLevelClass;
  18. use bizHd\purchase\classes\PurchaseClass;
  19. use bizHd\saas\classes\ApplyClass;
  20. use bizHd\saas\services\ApplyService;
  21. use common\components\dict;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\stringUtil;
  25. use common\components\util;
  26. use common\components\orderSn;
  27. use bizHd\base\classes\BaseClass;
  28. use Yii;
  29. class CustomClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\custom\models\Custom';
  32. const IS_DEBT_NO = 0;
  33. const IS_DEBT_YES = 1;
  34. const NOT_BLACK = 1;
  35. const IS_BLACK = 2;
  36. const LEVEL_SK = 0; //散客
  37. const LEVEL_PT = 1; //花店
  38. const LEVEL_HJ = 2; //同行
  39. const LEVEL_ZS = 3; //金店
  40. public static $levelMap = [
  41. self::LEVEL_SK => '散客',
  42. self::LEVEL_PT => '花店',
  43. self::LEVEL_HJ => '同行',
  44. self::LEVEL_ZS => '金店',
  45. ];
  46. public static $nickNameMap = [
  47. self::LEVEL_SK => '零售',
  48. self::LEVEL_PT => '批发',
  49. self::LEVEL_HJ => '同行',
  50. self::LEVEL_ZS => '金店',
  51. ];
  52. public static $levelPriceKeyMap = [
  53. self::LEVEL_SK => 'skPrice',
  54. self::LEVEL_PT => 'price',
  55. self::LEVEL_HJ => 'hjPrice',
  56. self::LEVEL_ZS => 'zsPrice',
  57. ];
  58. public static $levelAddPriceKeyMap = [
  59. self::LEVEL_SK => 'skMore',
  60. self::LEVEL_PT => 'addPrice',
  61. self::LEVEL_HJ => 'hjAddPrice',
  62. self::LEVEL_ZS => 'zsAddPrice',
  63. ];
  64. public static function pullOtherShopCustom($data)
  65. {
  66. $connection = Yii::$app->db;
  67. $transaction = $connection->beginTransaction();
  68. try {
  69. $toShopId = $data['toShopId'] ?? 0;
  70. $fromShopId = $data['fromShopId'] ?? 0;
  71. //指定当前环境为批发商,重要!!!
  72. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  73. $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
  74. if (!empty($fromCustomList)) {
  75. foreach ($fromCustomList as $fromCustomer) {
  76. $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
  77. if (!$exist) {
  78. CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
  79. }
  80. }
  81. }
  82. $transaction->commit();
  83. return true;
  84. } catch (\Exception $exception) {
  85. $transaction->rollBack();
  86. noticeUtil::push("批量拉取客户时报错了,fromShopId:{$fromShopId} toShopId:{$toShopId},报错内容:" . $exception->getMessage(), '15280215347');
  87. }
  88. return true;
  89. }
  90. public static function generateCustom($data)
  91. {
  92. $connection = Yii::$app->db;
  93. $transaction = $connection->beginTransaction();
  94. try {
  95. // 组织表单数据
  96. $shopName = $data['name'] ?? '';
  97. $mobile = $data['mobile'] ?? '';
  98. $post = [];
  99. $post['introSjId'] = $data['sjId'] ?? '';
  100. $post['introShopId'] = $data['shopId'] ?? '';
  101. $post['introSjName'] = $data['sjName'] ?? '';
  102. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  103. $post['name'] = $shopName;
  104. if (empty($shopName)) {
  105. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  106. $transaction->rollBack();
  107. return true;
  108. }
  109. $post['mobile'] = $mobile;
  110. if (!stringUtil::isMobile($mobile)) {
  111. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  112. $transaction->rollBack();
  113. return true;
  114. }
  115. if (stringUtil::getWordNum($shopName) > 15) {
  116. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
  117. $transaction->rollBack();
  118. return true;
  119. }
  120. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  121. if (!empty($has)) {
  122. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了哦", '15280215347');
  123. $transaction->rollBack();
  124. return true;
  125. }
  126. $hasShop = \biz\shop\classes\ShopClass::getByCondition(['mobile' => $mobile]);
  127. if (!empty($hasShop)) {
  128. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  129. $transaction->rollBack();
  130. return true;
  131. }
  132. $adminName = $post['adminName'] ?? '';
  133. $miniOpenId = $post['miniOpenId'] ?? '';
  134. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  135. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  136. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  137. $adminId = $admin['id'] ?? 0;
  138. $post['adminId'] = $adminId;
  139. $post['adminName'] = $admin['name'] ?? '';
  140. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  141. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  142. $post['from'] = ApplyClass::FROM_RETAIL;
  143. $post['style'] = SjClass::STYLE_RETAIL;
  144. //增加或更新申请
  145. $respond = ApplyService::replaceRetail($post);
  146. $id = $respond['id'] ?? 0;
  147. //后面的代码,要用到此参数,重要
  148. Yii::$app->params['ptStyle'] = 2;
  149. ApplyService::pass($id, '批发商手动添加');
  150. $transaction->commit();
  151. //只要手动添加的都标记为花店,任何情况都不会标记为散客,任何情况都是审核通过的状态
  152. $customList = CustomClass::getAllByCondition(['mobile' => $mobile], null, '*', null, true);
  153. if (!empty($customList)) {
  154. foreach ($customList as $custom) {
  155. $custom->isHd = 1;
  156. $custom->passStatus = 1;
  157. $custom->save();
  158. $ghsId = $custom->ghsId;
  159. GhsClass::updateById($ghsId, ['passStatus' => 1]);
  160. }
  161. }
  162. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  163. return true;
  164. } catch (\Exception $exception) {
  165. $transaction->rollBack();
  166. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  167. }
  168. return true;
  169. }
  170. //预订相关表同步更新货位 ssh 20240729
  171. public static function syncBookSeat($shop, $custom, $seatSn)
  172. {
  173. $bookSn = $shop->bookSn ?? 0;
  174. if (empty($bookSn)) {
  175. return true;
  176. }
  177. $customId = $custom->id ?? 0;
  178. BookItemCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
  179. BookCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
  180. }
  181. /**
  182. * 用账面正余额支付挂账:扣减净 balance 并记「结账」流水。
  183. * 来款充值后的自动销单不要调本方法(对照 hd:来款已入账,销单只改订单)。
  184. * 调用方:GhsRechargeSettleService::consumePositiveBalanceFifo。
  185. */
  186. public static function clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clear, $options = [])
  187. {
  188. $skipBalanceChangeRecord = !empty($options['skipBalanceChangeRecord']);
  189. // 先合并历史挂账到 balance,再扣减(只记余额变动)
  190. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  191. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  192. $customId = $custom->id ?? 0;
  193. $customName = $custom->name ?? '';
  194. $ghsId = $ghs->id ?? 0;
  195. $relateId = $clear->id ?? 0;
  196. $staffId = $staff->id ?? 0;
  197. $staffName = $staff->name ?? '';
  198. $clearSn = $clear->orderSn;
  199. $mainId = $shop->mainId ?? 0;
  200. $shopId = $shop->id ?? 0;
  201. $sjId = $shop->sjId ?? 0;
  202. $amount = bcadd((string)$amount, '0', 2);
  203. if (bccomp($amount, '0', 2) <= 0) {
  204. util::fail('销账扣款金额须大于0');
  205. }
  206. $newGhsBalance = bcsub((string)($ghs->balance ?? '0'), $amount, 2);
  207. $newCustomBalance = bcsub((string)($custom->balance ?? '0'), $amount, 2);
  208. // 合并后净余额可为负(待结),勿再因扣款后 <0 报错;金额由 CustomPaymentAllocateService 按来款池控制
  209. if (bccomp($newGhsBalance, $newCustomBalance, 2) !== 0) {
  210. util::fail('客户与供货商余额不一致,无法结账,请联系管理员');
  211. }
  212. $ghs->balance = $newGhsBalance;
  213. $ghs->save();
  214. $custom->balance = $newCustomBalance;
  215. $custom->save();
  216. // 充值销账:只记一条充值流水(附 clearAmount),不另记「结账 -X」行(对照 hd settleAmount)
  217. if ($skipBalanceChangeRecord) {
  218. return;
  219. }
  220. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomUseBalanceClear', 'id');
  221. $fromType = dict::getDict('fromType', 'shop');
  222. $cbData = [
  223. 'customId' => $customId,
  224. 'customName' => $customName,
  225. 'relateId' => $relateId,
  226. 'onlinePay' => 1,
  227. 'ptStyle' => 2,
  228. 'capitalType' => $capitalType,
  229. 'amount' => $amount,
  230. 'balance' => $newCustomBalance,
  231. 'staffId' => $staffId,
  232. 'staffName' => $staffName,
  233. 'io' => 0,
  234. 'side' => 0,
  235. 'payWay' => 0,
  236. 'fromType' => $fromType,
  237. 'event' => '结账 ' . $clearSn,
  238. 'mainId' => $mainId,
  239. 'shopId' => $shopId,
  240. 'sjId' => $sjId,
  241. 'remark' => '',
  242. ];
  243. $cbc = CustomBalanceChangeClass::add($cbData, true);
  244. $customShopId = $custom->shopId ?? 0;
  245. $customShop = ShopClass::getById($customShopId, true);
  246. if (empty($customShop)) {
  247. util::fail('没有找到客户门店呢..');
  248. }
  249. $customShopSjId = $customShop->sjId ?? 0;
  250. $customShopMainId = $customShop->mainId ?? 0;
  251. $capitalType = dict::getDict('capitalType', 'customAskGhsUseBalanceClear', 'id');
  252. $gbData = [
  253. 'ghsId' => $ghsId,
  254. 'relateId' => $relateId,
  255. 'ptStyle' => 2,
  256. 'capitalType' => $capitalType,
  257. 'amount' => $amount,
  258. 'balance' => $newGhsBalance,
  259. 'io' => 0,
  260. 'side' => 0,
  261. 'onlinePay' => 1,
  262. 'payWay' => 0,
  263. 'fromType' => $fromType,
  264. 'event' => '结账 ' . $clearSn,
  265. 'sjId' => $customShopSjId,
  266. 'mainId' => $customShopMainId,
  267. 'shopId' => $customShopId,
  268. 'remark' => '',
  269. ];
  270. $gbc = GhsBalanceChangeClass::add($gbData, true);
  271. $clear->customBalanceChangeId = $cbc->id ?? 0;
  272. $clear->ghsBalanceChangeId = $gbc->id ?? 0;
  273. $clear->save();
  274. }
  275. /** 供货商帮充与花店线上充:成对充值单互相关联,供余额明细展示结账单 */
  276. protected static function linkCustomAndGhsRecharge($customRecharge, $ghsRecharge)
  277. {
  278. if (empty($customRecharge) || empty($ghsRecharge)) {
  279. return;
  280. }
  281. $customRechargeId = intval(is_object($customRecharge) ? ($customRecharge->id ?? 0) : 0);
  282. $ghsRechargeId = intval(is_object($ghsRecharge) ? ($ghsRecharge->id ?? 0) : 0);
  283. if ($customRechargeId <= 0 || $ghsRechargeId <= 0) {
  284. return;
  285. }
  286. $cProbe = new \bizGhs\custom\models\CustomRecharge();
  287. if (method_exists($cProbe, 'hasAttribute') && $cProbe->hasAttribute('ghsRechargeId')) {
  288. $customRecharge->ghsRechargeId = $ghsRechargeId;
  289. $customRecharge->save(false, ['ghsRechargeId']);
  290. }
  291. $gProbe = new \bizGhs\ghs\models\GhsRecharge();
  292. if (method_exists($gProbe, 'hasAttribute') && $gProbe->hasAttribute('customRechargeId')) {
  293. $ghsRecharge->customRechargeId = $customRechargeId;
  294. $ghsRecharge->save(false, ['customRechargeId']);
  295. }
  296. }
  297. // 充值/返充(增加净 balance);有多个地方要同步修改,请搜索关键词 custom_ghs_recharge ssh 20240310
  298. public static function rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params = [])
  299. {
  300. // 充值前合并;之后对 balance 做 bcadd(只记余额变动)
  301. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  302. $ghsId = $custom->ghsId ?? 0;
  303. $ghsInfo = GhsClass::getLockById($ghsId);
  304. AccountMoneyClass::ensureGhsMoneyReady($ghsInfo, true);
  305. if (empty($ghsInfo)) {
  306. util::fail('没有找到供货商');
  307. }
  308. $ghsShopId = $ghsInfo->shopId ?? 0;
  309. $ghsName = $ghsInfo->name ?? '';
  310. $customId = $custom->id ?? 0;
  311. $customShopId = $custom->shopId ?? 0;
  312. $customName = $custom->name ?? '';
  313. $customShop = ShopClass::getById($customShopId, true);
  314. if (empty($customShop)) {
  315. util::fail('没有找到客户门店');
  316. }
  317. $payTime = date("Y-m-d H:i:s");
  318. $customShopSjId = $customShop->sjId ?? 0;
  319. $customShopMainId = $customShop->mainId ?? 0;
  320. $custom->balance = bcadd($custom->balance, $amount, 2);
  321. $custom->save();
  322. $orderSn = orderSn::getGhsCustomRechargeSn();
  323. $shopId = $shop->id ?? 0;
  324. $sjId = $shop->sjId ?? 0;
  325. $mainId = $shop->mainId ?? 0;
  326. $staffId = $staff->id ?? 0;
  327. $staffName = $staff->name ?? '';
  328. $remark = $params['remark'] ?? '';
  329. $rechargeType = $params['rechargeType'] ?? 0;
  330. $payMap = dict::getDict('payWayName');
  331. $payName = $payMap[$payWay] ?? '';
  332. $rechargeEvent = $payName . '充值';
  333. if ($rechargeType == 1) {
  334. $rechargeEvent = '售后返充';
  335. }
  336. $cData = [
  337. 'orderSn' => $orderSn,
  338. 'onlinePay' => 1,
  339. 'payWay' => $payWay,
  340. 'shopId' => $shopId,
  341. 'mainId' => $mainId,
  342. 'amount' => $amount,
  343. 'balance' => $custom->balance,
  344. 'side' => 0,
  345. 'staffId' => $staffId,
  346. 'staffName' => $staffName,
  347. 'payStatus' => 1,
  348. 'status' => 1,
  349. 'ghsId' => $ghsId,
  350. 'ghsShopId' => $ghsShopId,
  351. 'ghsName' => $ghsName,
  352. 'customId' => $customId,
  353. 'customShopId' => $customShopId,
  354. 'customName' => $customName,
  355. 'remark' => $remark,
  356. 'payTime' => $payTime,
  357. 'rechargeType' => $rechargeType,
  358. ];
  359. if (!empty($params['clearAudit']) && is_array($params['clearAudit'])) {
  360. $cData = CustomBalanceChangeClass::mergeClearAuditIntoRechargeData($cData, $params['clearAudit']);
  361. }
  362. $cRecharge = CustomRechargeClass::add($cData, true);
  363. $cRechargeId = $cRecharge->id ?? 0;
  364. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRecharge', 'id');
  365. if ($rechargeType == 1) {
  366. //售后返充
  367. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRechargeReturn', 'id');
  368. }
  369. $fromType = dict::getDict('fromType', 'shop');
  370. $cbData = [
  371. 'customId' => $customId,
  372. 'customName' => $customName,
  373. 'relateId' => $cRechargeId,
  374. 'onlinePay' => 1,
  375. 'ptStyle' => 2,
  376. 'capitalType' => $capitalType,
  377. 'amount' => $amount,
  378. 'balance' => $custom->balance,
  379. 'staffId' => $staffId,
  380. 'staffName' => $staffName,
  381. 'io' => 1,
  382. 'side' => 0,
  383. 'payWay' => $payWay,
  384. 'fromType' => $fromType,
  385. 'event' => $rechargeEvent,
  386. 'mainId' => $mainId,
  387. 'shopId' => $shopId,
  388. 'sjId' => $sjId,
  389. 'remark' => $remark,
  390. ];
  391. if (!empty($params['clearAudit']) && is_array($params['clearAudit'])) {
  392. $cbData = CustomBalanceChangeClass::mergeClearAuditIntoRowData($cbData, $params['clearAudit']);
  393. }
  394. CustomBalanceChangeClass::add($cbData, true);
  395. $ghsInfo->balance = bcadd($ghsInfo->balance, $amount, 2);
  396. $ghsInfo->save();
  397. $orderSn = orderSn::getGhsRechargeSn();
  398. $customMainId = $custom->mainId ?? 0;
  399. $gData = [
  400. 'orderSn' => $orderSn,
  401. 'onlinePay' => 1,
  402. 'payWay' => $payWay,
  403. 'shopId' => $customShopId,
  404. 'mainId' => $customMainId,
  405. 'amount' => $amount,
  406. 'balance' => $ghsInfo->balance,
  407. 'side' => 0,
  408. 'staffId' => 0,
  409. 'staffName' => '',
  410. 'payStatus' => 1,
  411. 'status' => 1,
  412. 'ghsId' => $ghsId,
  413. 'ghsShopId' => $ghsShopId,
  414. 'ghsName' => $ghsName,
  415. 'customId' => $customId,
  416. 'customShopId' => $customShopId,
  417. 'customName' => $customName,
  418. 'remark' => $remark,
  419. 'payTime' => $payTime,
  420. 'rechargeType' => $rechargeType,
  421. ];
  422. $gRecharge = GhsRechargeClass::add($gData, true);
  423. $gRechargeId = $gRecharge->id ?? 0;
  424. self::linkCustomAndGhsRecharge($cRecharge, $gRecharge);
  425. $capitalType = dict::getDict('capitalType', 'customAskGhsRecharge', 'id');
  426. if ($rechargeType == 1) {
  427. //售后返充
  428. $capitalType = dict::getDict('capitalType', 'customAskGhsRechargeReturn', 'id');
  429. }
  430. $gbData = [
  431. 'ghsId' => $ghsId,
  432. 'ghsName' => $ghsName,
  433. 'relateId' => $gRechargeId,
  434. 'ptStyle' => 2,
  435. 'capitalType' => $capitalType,
  436. 'amount' => $amount,
  437. 'balance' => $ghsInfo->balance,
  438. 'io' => 1,
  439. 'side' => 0,
  440. 'onlinePay' => 1,
  441. 'payWay' => $payWay,
  442. 'fromType' => $fromType,
  443. 'event' => $rechargeEvent,
  444. 'sjId' => $customShopSjId,
  445. 'mainId' => $customShopMainId,
  446. 'shopId' => $customShopId,
  447. 'remark' => $remark,
  448. ];
  449. GhsBalanceChangeClass::add($gbData, true);
  450. // 现金充值,同步更新表 xhMain 的 money 字段
  451. if ($payWay == 4) {
  452. $m = \bizGhs\shop\classes\MainClass::getById($mainId, true, 'id, money');
  453. $balance = $m->money;
  454. $m->money = bcadd($m->money, $amount);
  455. $m->save();
  456. $event = '客户(' . $customName . ')充值' . $amount . '元';
  457. $data = [
  458. 'amount' => $amount,
  459. 'balance' => $balance,
  460. 'io' => 1,
  461. 'mainId' => $mainId,
  462. 'capitalType' => $capitalType,
  463. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  464. 'event' => $event,
  465. 'remark' => $remark,
  466. ];
  467. ShopMoneyClass::addData($data);
  468. }
  469. return ['custom' => $custom, 'ghs' => $ghsInfo, 'customRecharge' => $cRecharge, 'ghsRecharge' => $gRecharge];
  470. }
  471. public static function createSeatSn($mainId)
  472. {
  473. $max = Custom::find(['mainId' => $mainId])->max('seatSn');
  474. $new = bcadd($max, 1);
  475. return $new;
  476. }
  477. //建立客户和供货商关系
  478. public static function build($ghsShopId, $hdShopId, $changeName = '')
  479. {
  480. return GhsClass::build($ghsShopId, $hdShopId, 2, $changeName);
  481. }
  482. //添加客户 ssh 2021.2.24
  483. public static function addCustom($data)
  484. {
  485. return self::add($data);
  486. }
  487. //获取客户信息 ssh 2021.2.4
  488. public static function getCustom($id)
  489. {
  490. $info = self::getById($id);
  491. if (empty($info)) {
  492. return $info;
  493. }
  494. $respond = self::groupBaseInfo([$info]);
  495. return current($respond);
  496. }
  497. public static function getCustomByIds($ids)
  498. {
  499. $list = self::getByIds($ids, null, 'id');
  500. return self::groupBaseInfo($list);
  501. }
  502. //整合头像等信息 ssh 2021.1.8
  503. public static function groupBaseInfo($list, $mainId = 0)
  504. {
  505. if (empty($list)) {
  506. return $list;
  507. }
  508. //片区划分
  509. $distMap = [];
  510. $custom = current($list);
  511. if (!empty($custom) && !empty($custom['ownMainId'])) {
  512. $distMap = DistClass::getAllByCondition(['mainId' => $custom['ownMainId']], null, '*', 'id');
  513. }
  514. $levelMap = self::$levelMap;
  515. foreach ($list as $key => $val) {
  516. $smallAvatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png') . '?x-oss-process=image/resize,m_fill,h_130,w_130';
  517. $avatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png');
  518. $list[$key]['smallAvatar'] = $smallAvatar;
  519. $list[$key]['avatar'] = $avatar;
  520. $list[$key]['sn'] = $val['id'];
  521. $level = $val['level'] ?? 1;
  522. $levelName = $levelMap[$level] ?? '';
  523. $list[$key]['levelName'] = $levelName;
  524. // 按 appVersion 输出净 balance 或旧版待结+余额字段(合并仅由控制台脚本执行)
  525. $list[$key] = AccountMoneyClass::formatMoneyForClient($list[$key]);
  526. //前端使用,走播,预订客户添加新客户
  527. $list[$key]['zbNum'] = '';
  528. $list[$key]['zbPrice'] = '';
  529. //片区划分
  530. $distId = $val['distId'] ?? 0;
  531. $distInfo = $distMap[$distId] ?? [];
  532. $distName = $distInfo['name'] ?? '';
  533. //注意区别 dist 字段,dist字段是地图里的区
  534. $list[$key]['distName'] = $distName;
  535. //超时未下单
  536. $recentExpend = $val['recentExpend'] ?? '';
  537. $list[$key]['overTimeUnExpend'] = 0;
  538. if ($recentExpend == '0000-00-00 00:00:00') {
  539. $list[$key]['overTimeUnExpend'] = 10000;
  540. } else {
  541. $recentExpendTime = strtotime($recentExpend);
  542. $seven = bcmul(86400, 7);
  543. $sevenTime = bcadd($recentExpendTime, $seven);
  544. if ($sevenTime < time()) {
  545. $list[$key]['overTimeUnExpend'] = 7;
  546. }
  547. $month = bcmul(86400, 30);
  548. $monthTime = bcadd($recentExpendTime, $month);
  549. if ($monthTime < time()) {
  550. $list[$key]['overTimeUnExpend'] = 30;
  551. }
  552. $halfYear = bcmul(86400, 180);
  553. $halfYearTime = bcadd($recentExpendTime, $halfYear);
  554. if ($halfYearTime < time()) {
  555. $list[$key]['overTimeUnExpend'] = 180;
  556. }
  557. $year = bcmul(86400, 365);
  558. $yearTime = bcmul($recentExpendTime, $year);
  559. if ($yearTime < time()) {
  560. $list[$key]['overTimeUnExpend'] = 365;
  561. }
  562. }
  563. }
  564. return $list;
  565. }
  566. //欠款权限开关 ssh 2021.1.8
  567. public static function debt($custom, $debt)
  568. {
  569. $id = $custom['id'] ?? 0;
  570. self::updateById($id, ['debt' => $debt]);
  571. return true;
  572. }
  573. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  574. public static function black($custom, $black)
  575. {
  576. $id = $custom['id'] ?? 0;
  577. self::updateById($id, ['black' => $black]);
  578. return true;
  579. }
  580. //权限判断 ssh 2021.1.8
  581. public static function valid($custom, $shopId)
  582. {
  583. if (empty($custom)) {
  584. util::fail('没有找到客户');
  585. }
  586. if ($custom['ownShopId'] != $shopId) {
  587. util::fail('您无法操作此客户');
  588. }
  589. }
  590. /**
  591. * 调用方已 lockAccountPair 时,校验 custom/ghs 净余额一致。
  592. */
  593. protected static function assertPairBalanceEqual($custom, $ghs)
  594. {
  595. if (empty($ghs)) {
  596. return;
  597. }
  598. $customBal = bcadd((string)($custom->balance ?? '0'), '0', 2);
  599. $ghsBal = bcadd((string)($ghs->balance ?? '0'), '0', 2);
  600. if (bccomp($customBal, $ghsBal, 2) !== 0) {
  601. util::fail('余额不一致,请联系管理员');
  602. }
  603. }
  604. /**
  605. * 开单扣减净 balance:写 custom(及成对 ghs),调用方须已 FOR UPDATE 锁行。
  606. */
  607. protected static function savePairBalanceAfterDeduct($custom, $ghs, $newBalance)
  608. {
  609. $newBalance = bcadd((string)$newBalance, '0', 2);
  610. $custom->balance = $newBalance;
  611. $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  612. $custom->save(false, ['balance', 'isDebt']);
  613. if (empty($ghs)) {
  614. return;
  615. }
  616. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  617. $ghs->balance = $newBalance;
  618. $ghs->debt = bccomp($newBalance, '0', 2) < 0 ? 2 : 1;
  619. $ghs->save(false, ['balance', 'debt']);
  620. self::assertPairBalanceEqual($custom, $ghs);
  621. }
  622. /**
  623. * hdApp 供货商余额明细:事项单号优先用 xhCg.orderSn,无采购单时再退回销售单号。
  624. *
  625. * @param object|array $order 供货商销售单
  626. * @return string
  627. */
  628. protected static function resolveXhCgOrderSnForHdEvent($order)
  629. {
  630. $saleOrderSn = is_object($order) ? ($order->orderSn ?? '') : ($order['orderSn'] ?? '');
  631. $purchaseId = intval(is_object($order) ? ($order->purchaseId ?? 0) : ($order['purchaseId'] ?? 0));
  632. if ($purchaseId <= 0) {
  633. return $saleOrderSn;
  634. }
  635. $cg = PurchaseClass::getById($purchaseId, true);
  636. if (empty($cg)) {
  637. return $saleOrderSn;
  638. }
  639. $cgOrderSn = $cg->orderSn ?? '';
  640. return $cgOrderSn !== '' ? $cgOrderSn : $saleOrderSn;
  641. }
  642. /**
  643. * 花店采购余额付售后:成对账户原路退回(与 payToChangeBalance 对称)。
  644. * 调用方须已 lockAccountPair;同时写客户余额明细与 xhGhs 余额明细(hdApp 可查)。
  645. */
  646. public static function refundBalancePayToPair($custom, $ghs, $refund, $order)
  647. {
  648. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  649. if (!empty($ghs)) {
  650. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  651. self::assertPairBalanceEqual($custom, $ghs);
  652. }
  653. $refundPrice = bcadd((string)($refund->refundPrice ?? '0'), '0', 2);
  654. if (bccomp($refundPrice, '0', 2) <= 0) {
  655. return;
  656. }
  657. $newBalance = bcadd((string)($custom->balance ?? '0'), $refundPrice, 2);
  658. self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
  659. $relateId = $refund->id ?? 0;
  660. $customId = $custom->id ?? 0;
  661. $customName = $custom->name ?? '';
  662. $saleOrderSn = $order->orderSn ?? '';
  663. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  664. $payTime = $order->payTime ?? '';
  665. $change = [
  666. 'relateId' => $relateId,
  667. 'customId' => $customId,
  668. 'customName' => $customName,
  669. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  670. 'capitalType' => $capitalType,
  671. 'amount' => $refundPrice,
  672. 'balance' => $newBalance,
  673. 'io' => 1,
  674. 'payWay' => dict::getDict('payWay', 'balancePay'),
  675. 'event' => "订单售后退款,单号:{$saleOrderSn}",
  676. 'sjId' => $refund->sjId ?? 0,
  677. 'shopId' => $refund->shopId ?? 0,
  678. 'mainId' => $order->mainId ?? 0,
  679. 'payTime' => $payTime,
  680. 'staffId' => 0,
  681. 'staffName' => '',
  682. 'side' => 0,
  683. 'fromType' => dict::getDict('fromType', 'shop'),
  684. 'remark' => '',
  685. ];
  686. CustomBalanceChangeClass::add($change, true);
  687. self::addGhsRefundBalanceChange($custom, $ghs, $refund, $order, $refundPrice, $newBalance);
  688. }
  689. /** 花店采购余额退款:记 xhGhs 余额变动(与 addGhsOrderBalanceChange 对称,hdApp 余额明细展示) */
  690. protected static function addGhsRefundBalanceChange($custom, $ghs, $refund, $order, $amount, $newBalance)
  691. {
  692. if (empty($ghs)) {
  693. return;
  694. }
  695. // hd 端明细展示采购单号 xhCg.orderSn
  696. $orderSn = self::resolveXhCgOrderSnForHdEvent($order);
  697. $customShopId = $custom->shopId ?? 0;
  698. $customShop = ShopClass::getById($customShopId, true);
  699. $gbData = [
  700. 'ghsId' => $ghs->id ?? 0,
  701. 'relateId' => $refund->id ?? 0,
  702. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  703. 'capitalType' => dict::getDict('capitalType', 'saleRefund', 'id'),
  704. 'amount' => $amount,
  705. 'balance' => $newBalance,
  706. 'io' => 1,
  707. 'side' => 0,
  708. 'onlinePay' => 1,
  709. 'payWay' => dict::getDict('payWay', 'balancePay'),
  710. 'fromType' => dict::getDict('fromType', 'shop'),
  711. 'event' => "订单售后退款,单号:{$orderSn}",
  712. 'sjId' => $customShop->sjId ?? ($custom->sjId ?? 0),
  713. 'mainId' => $customShop->mainId ?? 0,
  714. 'shopId' => $customShopId,
  715. 'remark' => '',
  716. ];
  717. GhsBalanceChangeClass::add($gbData, true);
  718. }
  719. protected static function addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder = false)
  720. {
  721. if (empty($ghs)) {
  722. return;
  723. }
  724. // hd 端明细展示采购单号 xhCg.orderSn
  725. $orderSn = self::resolveXhCgOrderSnForHdEvent($order);
  726. $event = '购买花材,单号:' . $orderSn;
  727. if ($bookOrder) {
  728. $event = '预订单多退少补,单号:' . $orderSn;
  729. }
  730. $customShopId = $custom->shopId ?? 0;
  731. $customShop = ShopClass::getById($customShopId, true);
  732. $gbData = [
  733. 'ghsId' => $ghs->id ?? 0,
  734. 'relateId' => $order->id ?? 0,
  735. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  736. 'capitalType' => dict::getDict('capitalType', 'xhGhsOrder', 'id'),
  737. 'amount' => $amount,
  738. 'balance' => $newBalance,
  739. 'io' => 0,
  740. 'side' => 0,
  741. 'onlinePay' => 1,
  742. 'payWay' => $order->payWay ?? 0,
  743. 'fromType' => dict::getDict('fromType', 'shop'),
  744. 'event' => $event,
  745. 'sjId' => $customShop->sjId ?? ($custom->sjId ?? 0),
  746. 'mainId' => $customShop->mainId ?? 0,
  747. 'shopId' => $customShopId,
  748. 'remark' => '',
  749. ];
  750. GhsBalanceChangeClass::add($gbData, true);
  751. }
  752. /**
  753. * 充值退款:扣减成对客户/供货商净余额(调用方须已 lockAccountPair)。
  754. *
  755. * @return string 扣减后的净余额
  756. */
  757. public static function deductPairBalanceForRefund($custom, $ghs, $amount)
  758. {
  759. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  760. if (!empty($ghs)) {
  761. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  762. self::assertPairBalanceEqual($custom, $ghs);
  763. }
  764. $amount = bcadd((string)$amount, '0', 2);
  765. if (bccomp($amount, '0', 2) <= 0) {
  766. util::fail('退款金额有误');
  767. }
  768. $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
  769. if (bccomp($beforeBalance, $amount, 2) < 0) {
  770. util::fail('客户余额不足,本笔充值可能已用于销账或消费');
  771. }
  772. $newBalance = bcsub($beforeBalance, $amount, 2);
  773. self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
  774. return $newBalance;
  775. }
  776. /**
  777. * 后台手动减少客户净余额(只减正余额部分,不增加待结)。
  778. * 调用方须已通过 lockAccountPair 锁定 custom/ghs。
  779. */
  780. public static function manualDeductBalance($shop, $custom, $ghs, $amount, $params = [])
  781. {
  782. if (empty($custom)) {
  783. util::fail('没有找到客户');
  784. }
  785. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  786. if (!empty($ghs)) {
  787. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  788. self::assertPairBalanceEqual($custom, $ghs);
  789. }
  790. $amount = bcadd((string)$amount, '0', 2);
  791. if (bccomp($amount, '0', 2) <= 0) {
  792. util::fail('金额要大于0');
  793. }
  794. $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
  795. if (bccomp($beforeBalance, '0', 2) <= 0) {
  796. util::fail('没有可减余额');
  797. }
  798. if (bccomp($beforeBalance, $amount, 2) < 0) {
  799. util::fail('余额不够扣');
  800. }
  801. $newBalance = bcsub($beforeBalance, $amount, 2);
  802. self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
  803. $remark = $params['remark'] ?? '';
  804. $staffId = intval($params['staffId'] ?? 0);
  805. $staffName = $params['staffName'] ?? '';
  806. $mainId = $shop->mainId ?? 0;
  807. $shopId = $shop->id ?? 0;
  808. $sjId = $shop->sjId ?? 0;
  809. $customId = $custom->id ?? 0;
  810. $customName = $custom->name ?? '';
  811. $capitalType = dict::getDict('capitalType', 'deduct', 'id');
  812. $fromType = dict::getDict('fromType', 'shop');
  813. $event = '手动减少';
  814. CustomBalanceChangeClass::add([
  815. 'customId' => $customId,
  816. 'customName' => $customName,
  817. 'relateId' => 0,
  818. 'onlinePay' => 1,
  819. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  820. 'capitalType' => $capitalType,
  821. 'amount' => $amount,
  822. 'balance' => $newBalance,
  823. 'staffId' => $staffId,
  824. 'staffName' => $staffName,
  825. 'io' => 0,
  826. 'side' => 0,
  827. 'payWay' => 0,
  828. 'fromType' => $fromType,
  829. 'event' => $event,
  830. 'mainId' => $mainId,
  831. 'shopId' => $shopId,
  832. 'sjId' => $sjId,
  833. 'remark' => $remark,
  834. ], true);
  835. if (!empty($ghs)) {
  836. $customShopId = $custom->shopId ?? 0;
  837. $customShop = ShopClass::getById($customShopId, true);
  838. GhsBalanceChangeClass::add([
  839. 'ghsId' => $ghs->id ?? 0,
  840. 'relateId' => 0,
  841. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  842. 'capitalType' => $capitalType,
  843. 'amount' => $amount,
  844. 'balance' => $newBalance,
  845. 'io' => 0,
  846. 'side' => 0,
  847. 'onlinePay' => 1,
  848. 'payWay' => 0,
  849. 'fromType' => $fromType,
  850. 'event' => $event,
  851. 'sjId' => $customShop->sjId ?? 0,
  852. 'mainId' => $customShop->mainId ?? 0,
  853. 'shopId' => $customShopId,
  854. 'remark' => $remark,
  855. ], true);
  856. }
  857. }
  858. /**
  859. * 开单余额支付:扣减净 balance 并记购买流水(须传入 payAfter 已 lockAccountPair 的 custom/ghs)。
  860. */
  861. public static function payToChangeBalance($order, $custom, $ghs = null)
  862. {
  863. $actPrice = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  864. if (bccomp($actPrice, '0', 2) <= 0) {
  865. return;
  866. }
  867. if (empty($custom)) {
  868. util::fail('没有找到客户');
  869. }
  870. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  871. if (!empty($ghs)) {
  872. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  873. self::assertPairBalanceEqual($custom, $ghs);
  874. }
  875. $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
  876. if (bccomp($beforeBalance, $actPrice, 2) < 0) {
  877. util::fail('余额不足');
  878. }
  879. $newBalance = bcsub($beforeBalance, $actPrice, 2);
  880. self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
  881. $orderSn = $order->orderSn ?? '';
  882. $customId = $custom->id ?? 0;
  883. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  884. $change = [
  885. 'relateId' => $order->id ?? 0,
  886. 'customId' => $customId,
  887. 'customName' => $custom->name ?? '',
  888. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  889. 'capitalType' => $capitalType,
  890. 'amount' => $actPrice,
  891. 'balance' => $newBalance,
  892. 'io' => 0,
  893. 'payWay' => dict::getDict('payWay', 'balancePay'),
  894. 'event' => '购买花材,单号:' . $orderSn,
  895. 'sjId' => $order->sjId ?? 0,
  896. 'shopId' => $order->shopId ?? 0,
  897. 'mainId' => $order->mainId ?? 0,
  898. 'staffId' => $order->shopAdminId ?? 0,
  899. 'staffName' => $order->shopAdminName ?? '',
  900. 'side' => 0,
  901. 'fromType' => dict::getDict('fromType', 'shop'),
  902. 'remark' => '',
  903. ];
  904. CustomBalanceChangeClass::add($change, true);
  905. self::addGhsOrderBalanceChange($custom, $ghs, $order, $actPrice, $newBalance, false);
  906. }
  907. /**
  908. * 挂账开单时若账户有正余额且不足本单:用余额先结本单一部分(只改 remainDebtPrice,不另扣 balance)。
  909. */
  910. protected static function inlineOrderBalancePartialClear($custom, $order, $clearAmount)
  911. {
  912. $clearAmount = bcadd((string)$clearAmount, '0', 2);
  913. if (bccomp($clearAmount, '0', 2) <= 0) {
  914. return;
  915. }
  916. $orderId = intval($order->id ?? 0);
  917. if ($orderId <= 0) {
  918. return;
  919. }
  920. $shopId = intval($order->shopId ?? 0);
  921. $shop = ShopClass::getById($shopId, true);
  922. if (empty($shop)) {
  923. util::fail('没有找到门店');
  924. }
  925. $staffId = intval($order->shopAdminId ?? 0);
  926. $staffName = $order->shopAdminName ?? '';
  927. $sjId = $shop->sjId ?? 0;
  928. $orderSn = $order->orderSn ?? '';
  929. OrderClearClass::expireAwaitPayClears(intval($custom->id ?? 0));
  930. $clear = OrderClearClass::clearWithAmountMap([
  931. 'customId' => $custom->id ?? 0,
  932. 'ghsShopAdminId' => $staffId,
  933. 'ghsShopId' => $shopId,
  934. 'ghsShopAdminName' => $staffName,
  935. 'modifyPrice' => $clearAmount,
  936. 'payWay' => dict::getDict('payWay', 'balancePay'),
  937. 'complete' => 0,
  938. 'remark' => '开单余额抵挂账',
  939. ], [
  940. ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn],
  941. ], $sjId, $shopId);
  942. OrderClearClass::applyNetBalanceClear($clear, dict::getDict('payWay', 'balancePay'), ['skipCashMoney' => true]);
  943. }
  944. /**
  945. * 挂账开单:增加客户待结(改造后 balance 减少,只记余额变动,不再记挂账变动)
  946. * ssh 20220306
  947. */
  948. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false, $ghs = null)
  949. {
  950. // 挂账开单:合并后 balance 减少,不再写 CustomDebtChange
  951. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  952. if (!empty($ghs)) {
  953. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  954. self::assertPairBalanceEqual($custom, $ghs);
  955. }
  956. $amount = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  957. if (bccomp($amount, '0', 2) <= 0) {
  958. return;
  959. }
  960. $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
  961. $positiveBalance = bccomp($beforeBalance, '0', 2) > 0 ? $beforeBalance : '0.00';
  962. if (bccomp($positiveBalance, $amount, 2) >= 0) {
  963. util::fail('余额充足,请用余额支付');
  964. }
  965. if (bccomp($positiveBalance, '0', 2) > 0) {
  966. self::inlineOrderBalancePartialClear($custom, $order, $positiveBalance);
  967. }
  968. $custom->isDebt = CustomClass::IS_DEBT_YES;
  969. $debtLimit = $custom->debtLimit ?? 0;
  970. if (!$bookOrder) {
  971. if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
  972. $outstanding = AccountMoneyClass::getOutstandingDebt($custom);
  973. if (bccomp($outstanding, $debtLimit, 2) > 0) {
  974. if ($custom->live == 1) {
  975. util::fail("已欠款{$outstanding},请先结账哦");
  976. }
  977. }
  978. }
  979. }
  980. $newBalance = bcsub($beforeBalance, $amount, 2);
  981. $mustNotice = false;
  982. if (getenv('YII_ENV') == 'production') {
  983. if ($custom->id == 11726) {
  984. $mustNotice = true;
  985. }
  986. } else {
  987. if ($custom->id == 577) {
  988. $mustNotice = true;
  989. }
  990. }
  991. if (getenv('YII_ENV', 'local') == 'production') {
  992. if ($mustNotice) {
  993. noticeUtil::push("【开单动作 | 客户】变化前余额:{$beforeBalance} 变化金额:{$amount} 变化后余额:{$newBalance}", '15280215347');
  994. }
  995. }
  996. self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
  997. $relateId = $order->id ?? 0;
  998. $customId = $custom->id ?? 0;
  999. $customName = $custom->name ?? '';
  1000. $orderSn = $order->orderSn ?? '';
  1001. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  1002. $event = '购买花材,单号:' . $orderSn;
  1003. if ($bookOrder) {
  1004. $event = '预订单多退少补,单号:' . $orderSn;
  1005. }
  1006. $change = [
  1007. 'relateId' => $relateId,
  1008. 'customId' => $customId,
  1009. 'customName' => $customName,
  1010. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  1011. 'capitalType' => $capitalType,
  1012. 'amount' => $amount,
  1013. 'balance' => $newBalance,
  1014. 'io' => 0,
  1015. 'payWay' => $order->payWay,
  1016. 'event' => $event,
  1017. 'sjId' => $order->sjId,
  1018. 'shopId' => $order->shopId,
  1019. 'mainId' => $order->mainId,
  1020. 'staffId' => 0,
  1021. 'staffName' => '',
  1022. 'side' => 0,
  1023. 'fromType' => dict::getDict('fromType', 'shop'),
  1024. 'remark' => '',
  1025. ];
  1026. CustomBalanceChangeClass::add($change, true);
  1027. self::addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder);
  1028. $mainId = $order->mainId ?? 0;
  1029. $main = MainClass::getLockById($mainId);
  1030. if (empty($main)) {
  1031. util::fail('没有main信息8');
  1032. }
  1033. $debt = $main->debt ?? 0;
  1034. $remain = bcadd($debt, $amount, 2);
  1035. if (getenv('YII_ENV', 'local') == 'production') {
  1036. if ($mustNotice) {
  1037. noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
  1038. }
  1039. }
  1040. $main->debt = $remain;
  1041. $main->save();
  1042. $event = $custom->name . ' 购买花材,单号:' . $orderSn;
  1043. $change['balance'] = $remain;
  1044. $change['event'] = $event;
  1045. TotalDebtChangeClass::addChange($change);
  1046. }
  1047. /**
  1048. * 结账收回挂账:balance 增加(原 debtAmount 减少逻辑)
  1049. * ssh 20220306
  1050. */
  1051. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  1052. {
  1053. // 结账收回挂账:balance 增加,只记余额变动
  1054. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  1055. $beforeBalance = $custom->balance ?? '0.00';
  1056. $newBalance = bcadd($beforeBalance, $amount, 2);
  1057. if (bccomp($beforeBalance, '0', 2) < 0 && bccomp($newBalance, '0', 2) > 0) {
  1058. noticeUtil::push("客户账户余额:{$beforeBalance} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  1059. util::fail('收款失败,结账后欠款金额会出现负数');
  1060. }
  1061. $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  1062. $custom->balance = $newBalance;
  1063. $custom->save(false, ['balance', 'isDebt']);
  1064. $relateId = $clear->id ?? 0;
  1065. $customId = $custom->id ?? 0;
  1066. $customName = $custom->name ?? '';
  1067. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  1068. $orderSn = $clear->orderSn ?? '';
  1069. $change = [
  1070. 'relateId' => $relateId,
  1071. 'customId' => $customId,
  1072. 'customName' => $customName,
  1073. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  1074. 'capitalType' => $capitalType,
  1075. 'amount' => $amount,
  1076. 'balance' => $newBalance,
  1077. 'io' => 1,
  1078. 'payWay' => 0,
  1079. 'event' => "结账,单号:{$orderSn}",
  1080. 'sjId' => $ghs->sjId,
  1081. 'shopId' => $ghs->shopId,
  1082. 'mainId' => $ghs->mainId,
  1083. 'staffId' => 0,
  1084. 'staffName' => '',
  1085. 'side' => 0,
  1086. 'fromType' => dict::getDict('fromType', 'shop'),
  1087. 'remark' => '',
  1088. ];
  1089. CustomBalanceChangeClass::add($change, true);
  1090. $mainId = $ghs->mainId ?? 0;
  1091. $main = MainClass::getLockById($mainId);
  1092. if (empty($main)) {
  1093. util::fail('没有main信息9');
  1094. }
  1095. $debt = $main->debt ?? 0;
  1096. $remain = bcsub($debt, $amount, 2);
  1097. $main->debt = $remain;
  1098. $main->save();
  1099. $event = $custom->name . " 结账,单号:{$orderSn}";
  1100. $change['balance'] = $remain;
  1101. $change['event'] = $event;
  1102. TotalDebtChangeClass::addChange($change);
  1103. }
  1104. /**
  1105. * 售后退款:减少待结(balance 增加),只记余额变动
  1106. * ssh 20220306
  1107. */
  1108. public static function refundDebtAmountReduce($custom, $refund, $order)
  1109. {
  1110. // 退款减待结:balance 增加,只记余额变动
  1111. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  1112. $refundPrice = $refund->refundPrice ?? 0;
  1113. if (bccomp($refundPrice, '0', 2) <= 0) {
  1114. return;
  1115. }
  1116. $newBalance = bcadd($custom->balance ?? '0.00', $refundPrice, 2);
  1117. $custom->balance = $newBalance;
  1118. $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  1119. $custom->save(false, ['balance', 'isDebt']);
  1120. $relateId = $refund->id ?? 0;
  1121. $customId = $custom->id ?? 0;
  1122. $customName = $custom->name ?? '';
  1123. $saleOrderSn = $order->orderSn ?? '';
  1124. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  1125. $payTime = $order->payTime;
  1126. $change = [
  1127. 'relateId' => $relateId,
  1128. 'customId' => $customId,
  1129. 'customName' => $customName,
  1130. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  1131. 'capitalType' => $capitalType,
  1132. 'amount' => $refundPrice,
  1133. 'balance' => $newBalance,
  1134. 'io' => 1,
  1135. 'payWay' => 0,
  1136. 'event' => "订单号:{$saleOrderSn} 退款",
  1137. 'sjId' => $refund->sjId,
  1138. 'shopId' => $refund->shopId,
  1139. 'mainId' => $order->mainId,
  1140. 'payTime' => $payTime,
  1141. 'staffId' => 0,
  1142. 'staffName' => '',
  1143. 'side' => 0,
  1144. 'fromType' => dict::getDict('fromType', 'shop'),
  1145. 'remark' => '',
  1146. ];
  1147. CustomBalanceChangeClass::add($change, true);
  1148. $mainId = $order->mainId ?? 0;
  1149. $main = MainClass::getLockById($mainId);
  1150. if (empty($main)) {
  1151. util::fail('没有main信息10');
  1152. }
  1153. $debt = $main->debt ?? 0;
  1154. $remain = bcsub($debt, $refundPrice, 2);
  1155. $main->debt = $remain;
  1156. $main->save();
  1157. $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
  1158. $change['balance'] = $remain;
  1159. $change['event'] = $event;
  1160. TotalDebtChangeClass::addChange($change);
  1161. }
  1162. public static function exportCustom($where, $sort, $mainId)
  1163. {
  1164. $list = self::getAllByCondition($where, $sort, '*', null);
  1165. if (empty($list)) {
  1166. util::fail('没有找到客户');
  1167. }
  1168. $list = self::groupBaseInfo($list);
  1169. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1170. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1171. $objPHPExcel = new \PHPExcel();
  1172. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1173. ->setLastModifiedBy("Maarten Balliauw")
  1174. ->setTitle("Office 2007 XLSX Document")
  1175. ->setSubject("Office 2007 XLSX Document")
  1176. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1177. ->setKeywords("office 2007 openxml php")
  1178. ->setCategory("file");
  1179. $ghsTitle = '供货商';
  1180. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1181. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1182. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1183. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1184. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1185. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1186. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1187. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  1188. $objPHPExcel->getActiveSheet()->setCellValue('B1', '客户');
  1189. $objPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
  1190. $objPHPExcel->getActiveSheet()->setCellValue('D1', '欠款');
  1191. $objPHPExcel->getActiveSheet()->setCellValue('E1', '余额');
  1192. $objPHPExcel->getActiveSheet()->setCellValue('F1', '待结金额');
  1193. $objPHPExcel->getActiveSheet()->setCellValue('G1', '最近下单');
  1194. $objPHPExcel->getActiveSheet()->setCellValue('H1', '累计消费');
  1195. $objPHPExcel->getActiveSheet()->setCellValue('I1', '累计消费次数');
  1196. //设置宽度
  1197. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  1198. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1199. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1200. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1201. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  1202. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
  1203. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
  1204. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
  1205. //加粗
  1206. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1207. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1208. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1209. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1210. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1211. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1212. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  1213. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  1214. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  1215. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1216. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1217. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1218. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1219. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1220. $baseRow = 2;
  1221. foreach ($list as $key => $custom) {
  1222. $i = $baseRow + $key;
  1223. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  1224. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  1225. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['mobile'], \PHPExcel_Cell_DataType::TYPE_STRING);
  1226. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['debtAmount']);
  1227. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['balance']);
  1228. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['remainDebtAmount']);
  1229. if ($custom['recentExpend'] == '0000-00-00 00:00:00') {
  1230. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, '没有下过单');
  1231. } else {
  1232. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $custom['recentExpend']);
  1233. }
  1234. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $custom['buyAmount']);
  1235. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $custom['buyNum']);
  1236. //居左
  1237. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1238. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1239. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1240. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1241. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1242. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1243. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1244. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1245. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1246. }
  1247. $fileName = '客户-' . date("m-d");
  1248. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1249. $objPHPExcel->setActiveSheetIndex(0);
  1250. $dir = './priceTable/' . $mainId;
  1251. if (file_exists($dir) == false) {
  1252. mkdir($dir, 0777, true);
  1253. }
  1254. $date = $fileName;
  1255. $file = $date . '.xls';
  1256. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1257. if (file_exists($dir . '/' . $file)) {
  1258. unlink($dir . '/' . $file);
  1259. }
  1260. $objWriter->save($dir . '/' . $file);
  1261. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1262. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1263. }
  1264. public static function showTotalBalance($pfShopId = 0, $lsShopId = 0)
  1265. {
  1266. ini_set('memory_limit', '2045M');
  1267. set_time_limit(0);
  1268. $pfAmount = 0;
  1269. $lsAmount = 0;
  1270. if (!empty($pfShopId)) {
  1271. $custom = CustomClass::getAllByCondition(['ownShopId' => $pfShopId], null, '*', null, true);
  1272. if (!empty($custom)) {
  1273. foreach ($custom as $c) {
  1274. $new = AccountMoneyClass::getNetBalanceFromRow($c);
  1275. $pfAmount = bcadd($pfAmount, $new, 2);
  1276. }
  1277. }
  1278. }
  1279. if (!empty($lsShopId)) {
  1280. $lsCustom = \bizHd\custom\classes\CustomClass::getAllByCondition(['shopId' => $lsShopId], null, '*', null, true);
  1281. if (!empty($lsCustom)) {
  1282. foreach ($lsCustom as $lc) {
  1283. $balance = $lc->balance ?? 0;
  1284. $lsAmount = bcadd($balance, $lsAmount, 2);
  1285. }
  1286. }
  1287. }
  1288. return ['pfAmount' => $pfAmount, 'lsAmount' => $lsAmount];
  1289. }
  1290. }