DeductClass.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace bizHd\deduct\classes;
  3. use bizHd\balance\classes\BalanceChangeClass;
  4. use common\components\dict;
  5. use common\components\util;
  6. use Yii;
  7. use bizHd\base\classes\BaseClass;
  8. class DeductClass extends BaseClass
  9. {
  10. public static $baseFile = '\bizHd\deduct\models\Deduct';
  11. public static function doDeduct($shop, $custom, $hd, $amount, $params)
  12. {
  13. $currentBalance = $custom->balance;
  14. if ($amount > $currentBalance) {
  15. util::fail('余额不够扣');
  16. }
  17. $custom->balance = bcsub($custom->balance, $amount, 2);
  18. $custom->save();
  19. $hd->balance = bcsub($hd->balance, $amount, 2);
  20. $hd->save();
  21. if (floatval($hd->balance) != floatval($custom->balance)) {
  22. util::fail('余额有问题');
  23. }
  24. $remark = $params['remark'] ?? '';
  25. $staffId = $params['staffId'] ?? 0;
  26. $staffName = $params['staffName'] ?? '';
  27. $shopId = $shop->id;
  28. $mainId = $shop->mainId;
  29. $hdId = $hd->id;
  30. $hdName = $hd->name;
  31. $customId = $custom->id;
  32. $customName = $custom->name;
  33. $data = [
  34. 'shopId' => $shopId,
  35. 'mainId' => $mainId,
  36. 'hdId' => $hdId,
  37. 'hdName' => $hdName,
  38. 'amount' => $amount,
  39. 'balance' => $custom->balance,
  40. 'customId' => $customId,
  41. 'customName' => $customName,
  42. 'staffId' => $staffId,
  43. 'staffName' => $staffName,
  44. 'status' => 1,
  45. 'remark' => $remark,
  46. ];
  47. $result = self::add($data, true);
  48. $relateId = $result->id;
  49. $event = '手动减少';
  50. $capitalType = dict::getDict('capitalType', 'deduct', 'id');
  51. $change = [
  52. 'customId' => $customId,
  53. 'customName' => $customName,
  54. 'hdId' => $hdId,
  55. 'hdName' => $hdName,
  56. 'relateId' => $relateId,
  57. 'onlinePay' => 1,
  58. 'capitalType' => $capitalType,
  59. 'amount' => $amount,
  60. 'balance' => $hd->balance,
  61. 'io' => 0,
  62. 'side' => 0,
  63. 'payWay' => 0,
  64. 'event' => $event,
  65. 'staffId' => $staffId,
  66. 'staffName' => $staffName,
  67. 'shopId' => $shopId,
  68. 'mainId' => $mainId,
  69. 'remark' => $remark,
  70. ];
  71. BalanceChangeClass::add($change, true);
  72. }
  73. }