CustomClass.php 64 KB

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