SettleClass.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace bizHd\order\classes;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\shop\classes\MainClass;
  5. use bizHd\shop\classes\ShopMoneyChangeClass;
  6. use bizHd\work\classes\WorkClass;
  7. use common\components\dict;
  8. use common\components\orderSn;
  9. use common\components\util;
  10. use Yii;
  11. use bizHd\base\classes\BaseClass;
  12. class SettleClass extends BaseClass
  13. {
  14. public static $baseFile = '\bizHd\order\models\Settle';
  15. //结清尾款 ssh 20220708
  16. public static function confirmSettle($order, $payWay, $staff)
  17. {
  18. $mainId = $order->mainId ?? 0;
  19. $orderId = $order->id ?? 0;
  20. $settleId = $order->settleId ?? 0;
  21. if (!empty($settleId)) {
  22. util::fail('已经结过了');
  23. }
  24. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  25. if ($remainDebtPrice <= 0) {
  26. $orderSn = $order->orderSn ?? '';
  27. util::fail('已经结过了哦(单号 ' . $orderSn . ')');
  28. }
  29. $shopId = $order->shopId ?? 0;
  30. $settleOrderSn = orderSn::getSettleSn();
  31. $customId = $order->customId ?? 0;
  32. $customName = $order->customName ?? '';
  33. $staffId = $staff->id ?? 0;
  34. $staffName = $staff->name ?? '';
  35. $settleData = [
  36. 'prePrice' => $remainDebtPrice,
  37. 'actPrice' => $remainDebtPrice,
  38. 'realPrice' => $remainDebtPrice,
  39. 'payWay' => $payWay,
  40. 'hdShopId' => $shopId,
  41. 'shopId' => $shopId,
  42. 'mainId' => $mainId,
  43. 'purchaseIds' => $orderId,
  44. 'saleIds' => $orderId,
  45. 'num' => 1,
  46. 'status' => 2,
  47. 'customId' => $customId,
  48. 'customName' => $customName,
  49. 'payTime' => date("Y-m-d H:i:s"),
  50. 'hdShopAdminId' => $staffId,
  51. 'hdShopName' => $staffName,
  52. 'orderSn' => $settleOrderSn,
  53. ];
  54. $settleReturn = self::addData($settleData);
  55. $settleId = $settleReturn->id ?? 0;
  56. $order->settleId = $settleId;
  57. $order->debt = 0;
  58. $order->remainDebtPrice = 0;
  59. $order->save();
  60. if ($payWay == dict::getDict('payWay', 'cash')) {
  61. $mainId = $order->mainId ?? 0;
  62. $main = MainClass::getLockById($mainId);
  63. if (empty($main)) {
  64. util::fail('门店信息错误');
  65. }
  66. $moneyBalance = bcadd($main->money, $remainDebtPrice, 2);
  67. $main->money = $moneyBalance;
  68. $main->save();
  69. $moneyEvent = $customName . "现金结尾款" . floatval($remainDebtPrice) . "元(单号 {$settleOrderSn})";
  70. $moneyRemark = '';
  71. $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
  72. $change = [
  73. 'relateId' => $orderId,
  74. 'amount' => $remainDebtPrice,
  75. 'balance' => $moneyBalance,
  76. 'io' => 1,
  77. 'mainId' => $mainId,
  78. 'capitalType' => $capitalType,
  79. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  80. 'event' => $moneyEvent,
  81. 'remark' => $moneyRemark,
  82. ];
  83. ShopMoneyChangeClass::addData($change);
  84. }
  85. //客户欠款金额减少
  86. $custom = CustomClass::getLockById($customId);
  87. CustomClass::clearDebtAmountReduce($custom, $remainDebtPrice, $settleReturn);
  88. //制作单上的尾款也去掉
  89. WorkClass::updateByCondition(['orderId' => $orderId], ['orderDebtPrice' => 0]);
  90. }
  91. public static function addData($data)
  92. {
  93. return self::add($data, true);
  94. }
  95. }