CustomClass.php 60 KB

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