CustomClass.php 61 KB

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