CustomClass.php 62 KB

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