WorkerAccountLogLogic.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\enum\worker\WorkerAccountLogEnum;
  4. use app\common\model\financial\MasterSettlementDetails;
  5. use app\common\model\master_worker\MasterWorker;
  6. use app\common\model\master_worker\MasterWorkerAccountLog;
  7. use think\Exception;
  8. class WorkerAccountLogLogic extends BaseLogic
  9. {
  10. /**
  11. * @param $work //工单对象
  12. * @param $change_amount //变动数量
  13. * @param $change_type //变动类型
  14. * @param $action //动作 1-增加 2-减少
  15. * @return bool|void
  16. * @throws Exception
  17. */
  18. public static function addAccountLog($work,$changeAmount,$changeType,$action, string $remark = '', array $extra = [],int $appoint_worker_id = 0)
  19. {
  20. $worker = MasterWorker::findOrFail($appoint_worker_id?:$work->master_worker_id);
  21. if($worker->isEmpty()) {
  22. throw new Exception('工程师不存在');
  23. }
  24. $changeObject = WorkerAccountLogEnum::getChangeObject($changeType);
  25. if(!$changeObject) {
  26. throw new Exception('结算错误');
  27. }
  28. switch ($changeObject) {
  29. // 用户余额
  30. case WorkerAccountLogEnum::UM:
  31. $left_amount = $worker->user_money;
  32. break;
  33. // 其他
  34. }
  35. $data = [
  36. 'sn' => generate_sn(MasterWorkerAccountLog::class, 'sn', 20),
  37. 'title'=>$work->title,
  38. 'worker_id' => $appoint_worker_id?:$work->master_worker_id,
  39. 'change_object' => $changeObject,
  40. 'change_type' => $changeType,
  41. 'action' => $action,
  42. 'left_amount' => $left_amount,
  43. 'change_amount' => $changeAmount,
  44. 'work_sn' => $work->work_sn,
  45. 'remark' => $remark,
  46. 'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
  47. ];
  48. MasterWorkerAccountLog::create($data);
  49. $worker->user_money = $left_amount + ($action==1?$changeAmount:-$changeAmount);
  50. $worker->save();
  51. return true;
  52. }
  53. public static function masterAccountLog(int $worker_id,$changeAmount,$changeType,$action, string $title = '', string $remark = '', array $extra = [])
  54. {
  55. $worker = MasterWorker::findOrFail($worker_id);
  56. if($worker->isEmpty()) {
  57. throw new Exception('工程师不存在');
  58. }
  59. $changeObject = WorkerAccountLogEnum::getChangeObject($changeType);
  60. if(!$changeObject) {
  61. throw new Exception('结算错误');
  62. }
  63. switch ($changeObject) {
  64. // 用户余额
  65. case WorkerAccountLogEnum::UM:
  66. $left_amount = $worker->user_money;
  67. break;
  68. // 其他
  69. }
  70. $worker->user_money = $left_amount + ($action==1?$changeAmount:-$changeAmount);
  71. $worker->save();
  72. $data = [
  73. 'sn' => generate_sn(MasterWorkerAccountLog::class, 'sn', 20),
  74. 'title'=>$title,
  75. 'worker_id' => $worker_id,
  76. 'change_object' => $changeObject,
  77. 'change_type' => $changeType,
  78. 'action' => $action,
  79. 'left_amount' => $worker->user_money,
  80. 'change_amount' => $changeAmount,
  81. 'work_sn' => '',
  82. 'remark' => $remark,
  83. 'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
  84. ];
  85. return MasterWorkerAccountLog::create($data);
  86. }
  87. }