CustomClass.php 57 KB

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