GhsClass.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace bizHd\ghs\classes;
  3. /**
  4. * 花店侧 xhGhs 关系账户(与 xhGhsCustom 成对,custom.ghsId 关联)
  5. *
  6. * 金额与 bizGhs\custom\classes\AccountMoneyClass 一致:写前 ensureGhsMoneyReady(仅行锁);
  7. * 挂账=balance 减少,结账/退款=balance 增加;只记 GhsBalanceChange,不再新增 GhsDebtChange(历史仍可查)。
  8. */
  9. use bizGhs\custom\classes\AccountMoneyClass;
  10. use common\components\dict;
  11. use common\components\noticeUtil;
  12. use common\components\util;
  13. use Yii;
  14. use bizHd\base\classes\BaseClass;
  15. class GhsClass extends BaseClass
  16. {
  17. public static $baseFile = '\bizHd\ghs\models\Ghs';
  18. /** 采购挂账开单:balance 减少(原 debtAmount 增加) */
  19. public static function cgDebtAmountAdd($ghs, $order, $bookOrder = false)
  20. {
  21. // hd 采购挂账:净 balance 减少
  22. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  23. // 开单余额抵挂账后 remainDebtPrice 变小,balance 仍按整单 debtPrice 扣减
  24. $amount = bcadd((string)($order->debtPrice ?? '0'), '0', 2);
  25. if (bccomp($amount, '0', 2) <= 0) {
  26. $amount = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  27. }
  28. if (bccomp($amount, '0', 2) <= 0) {
  29. return;
  30. }
  31. $beforeBalance = $ghs->balance ?? '0.00';
  32. $ghs->debt = 2;
  33. $newBalance = bcsub($beforeBalance, $amount, 2);
  34. $ghs->balance = $newBalance;
  35. $ghs->save(false, ['balance', 'debt']);
  36. $relateId = $order->id ?? 0;
  37. $orderSn = $order->orderSn ?? '';
  38. $ghsId = $ghs->id ?? 0;
  39. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  40. $event = '购买花材,采购单号:' . $orderSn;
  41. if ($bookOrder) {
  42. $event = '预订单多退少补,单号' . $orderSn;
  43. }
  44. $change = [
  45. 'relateId' => $relateId,
  46. 'ghsId' => $ghsId,
  47. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  48. 'capitalType' => $capitalType,
  49. 'amount' => $amount,
  50. 'balance' => $newBalance,
  51. 'io' => 0,
  52. 'payWay' => $order->payWay,
  53. 'event' => $event,
  54. 'sjId' => $order->sjId ?? 0,
  55. 'shopId' => $order->shopId ?? 0,
  56. ];
  57. GhsBalanceChangeClass::add($change, true);
  58. }
  59. /** 采购结账:balance 增加(原 debtAmount 减少) */
  60. public static function clearDebtAmountReduce($ghs, $custom, $amount, $clear)
  61. {
  62. // hd 采购结账:净 balance 增加
  63. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  64. $beforeBalance = $ghs->balance ?? '0.00';
  65. $newBalance = bcadd($beforeBalance, $amount, 2);
  66. if (bccomp($beforeBalance, '0', 2) < 0 && bccomp($newBalance, '0', 2) > 0) {
  67. noticeUtil::push("结账没有成功,账户余额:{$beforeBalance} 本次结账金额:{$amount} 供货商ID:{$ghs->id}", '15280215347');
  68. util::fail('结账没有成功');
  69. }
  70. $ghs->balance = $newBalance;
  71. $ghs->debt = bccomp($newBalance, '0', 2) < 0 ? 2 : 1;
  72. $ghs->save(false, ['balance', 'debt']);
  73. $relateId = $clear->id ?? 0;
  74. $ghsId = $ghs->id ?? 0;
  75. $orderSn = $clear->orderSn ?? '';
  76. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  77. $change = [
  78. 'relateId' => $relateId,
  79. 'ghsId' => $ghsId,
  80. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  81. 'capitalType' => $capitalType,
  82. 'amount' => $amount,
  83. 'balance' => $newBalance,
  84. 'io' => 1,
  85. 'payWay' => 1,
  86. 'event' => "结账,单号:{$orderSn}",
  87. 'sjId' => $custom->sjId ?? 0,
  88. 'shopId' => $custom->shopId ?? 0,
  89. ];
  90. GhsBalanceChangeClass::add($change, true);
  91. }
  92. /** 采购退款:balance 增加 */
  93. public static function refundDebtAmountReduce($ghs, $refund, $cg)
  94. {
  95. // hd 采购退款:净 balance 增加
  96. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  97. $refundPrice = $refund->refundPrice ?? 0;
  98. if (bccomp($refundPrice, '0', 2) <= 0) {
  99. return;
  100. }
  101. $newBalance = bcadd($ghs->balance ?? '0.00', $refundPrice, 2);
  102. $ghs->balance = $newBalance;
  103. $ghs->save(false, ['balance']);
  104. $relateId = $refund->id ?? 0;
  105. $ghsId = $ghs->id ?? 0;
  106. $capitalType = dict::getDict('capitalType', 'cgRefund', 'id');
  107. $cgOrderSn = $cg->orderSn ?? '';
  108. $change = [
  109. 'relateId' => $relateId,
  110. 'ghsId' => $ghsId,
  111. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  112. 'capitalType' => $capitalType,
  113. 'amount' => $refundPrice,
  114. 'balance' => $newBalance,
  115. 'io' => 1,
  116. 'payWay' => 0,
  117. 'event' => "采购单:{$cgOrderSn} 退款",
  118. 'sjId' => $refund->sjId ?? 0,
  119. 'shopId' => $refund->shopId ?? 0,
  120. ];
  121. GhsBalanceChangeClass::add($change, true);
  122. }
  123. //验证供货商 ssh 20220307
  124. public static function valid($ghs, $shopId)
  125. {
  126. if (empty($ghs)) {
  127. util::fail('没有找到供货商');
  128. }
  129. if ($ghs->ownShopId != $shopId) {
  130. util::fail('不是您的供货商');
  131. }
  132. return true;
  133. }
  134. }