AuxiliaryAccountService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace App\Service;
  3. use App\Model\AuxiliaryAccount;
  4. use App\Model\AuxiliaryAccountDetails;
  5. use App\Model\Employee;
  6. use App\Model\ExpenseClaims;
  7. use App\Model\ExpenseClaimsDetails;
  8. use App\Model\Fee;
  9. use App\Model\MonthlyDdOrder;
  10. use App\Model\MonthlyDdOrderDetails;
  11. use App\Model\MonthlyPsOrder;
  12. use App\Model\MonthlyPsOrderDetails;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. class AuxiliaryAccountService extends Service
  16. {
  17. public function auxiliaryAccountList($data,$user){
  18. $model = $this->setCommon($data, $user);
  19. $list = $this->limit($model,'',$data);
  20. $list = $this->fillData($list);
  21. return [true, $list];
  22. }
  23. public function setCommon($data,$user, $field = []){
  24. if(empty($field)) $field = AuxiliaryAccount::$field;
  25. $model = AuxiliaryAccount::Clear($user,$data);
  26. $model = $model->where('del_time',0)
  27. ->select($field)
  28. ->orderby('month', 'desc');
  29. if(! empty($data['month'])) $model->where('month', $data['month']);
  30. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  31. return $model;
  32. }
  33. public function fillData($data){
  34. if(empty($data['data'])) return $data;
  35. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  36. foreach ($data['data'] as $key => $value){
  37. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  38. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  39. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  40. }
  41. return $data;
  42. }
  43. public function auxiliaryAccountSummary($data,$user){
  44. //传参月份
  45. if(!isset($data['month'])) return [201,'月份必传'];
  46. $month = $data['month'];
  47. $monthStart = strtotime($month);
  48. $monthEnd = strtotime("$month +1 month") - 1;
  49. //获取员工汇总月份工资
  50. list($status,$employee_month_amount) = $this->employeeMonthAmount($data,$user,$monthStart,$monthEnd);
  51. if (!$status) return [$status,$employee_month_amount];
  52. //获取设备折旧费用
  53. list($status,$device_month_amount) = $this->deviceMonthAmount($data,$user,$monthStart,$monthEnd);
  54. if (!$status) return [$status,$device_month_amount];
  55. //获取费用汇总的费用数据
  56. list($status,$fee_month_amount) = $this->feeMonthAmount($data,$user,$monthStart,$monthEnd);
  57. if (!$status) return [$status,$fee_month_amount];
  58. return [true,[
  59. "employee_month_amount" => $employee_month_amount,
  60. "device_month_amount" => $device_month_amount,
  61. "fee_month_amount" => $fee_month_amount,
  62. ]];
  63. }
  64. private function employeeMonthAmount($month,$user,$monthStart,$monthEnd){
  65. $data['top_depart_id'] = $user['top_depart_id'];
  66. $month_ps_order = MonthlyPsOrder::Clear($user,$data);
  67. $month_ps_order_id = $month_ps_order->where('del_time',0)->where("month",$monthStart)->first();
  68. if(empty($month_ps_order_id)){
  69. return [false,"请补充对应月份人员月度工资信息"];
  70. }else{
  71. $month_ps_order_id = $month_ps_order_id->id;
  72. }
  73. $model = MonthlyPsOrderDetails::Clear($user,$data);
  74. //总金额
  75. $total_amount = $model->where('main_id',$month_ps_order_id)->where('del_time',0)->sum("salary");
  76. //委内
  77. $entrust_in_amount = $model->where('main_id',$month_ps_order_id)->where("entrust_type",1)->sum("salary");
  78. //委外
  79. $entrust_out_amount = $model->where('main_id',$month_ps_order_id)->where("entrust_type",2)->sum("salary");
  80. return [true,[
  81. "total_amount" => $total_amount,
  82. "entrust_in_amount" => $entrust_in_amount,
  83. "entrust_out_amount" => $entrust_out_amount,
  84. ]];
  85. }
  86. private function deviceMonthAmount($month,$user,$monthStart,$monthEnd){
  87. $data['top_depart_id'] = $user['top_depart_id'];
  88. $month_ps_order = MonthlyDdOrder::Clear($user,$data);
  89. $month_ps_order_id = $month_ps_order->where('del_time',0)->where("month",$monthStart)->first();
  90. if(empty($month_ps_order_id)){
  91. return [false,"请补充对应月份设备月度折旧信息"];
  92. }else{
  93. $month_ps_order_id = $month_ps_order_id->id;
  94. }
  95. $model = MonthlyDdOrderDetails::Clear($user,$data);
  96. //总金额
  97. $total_amount = $model->where('main_id',$month_ps_order_id)->sum("depreciation_amount");
  98. return [true,[
  99. "total_amount" => $total_amount,
  100. ]];
  101. }
  102. private function feeMonthAmount($month,$user,$monthStart,$monthEnd){
  103. $data['top_depart_id'] = $user['top_depart_id'];
  104. $month_ps_order = ExpenseClaims::Clear($user,$data);
  105. $month_ps_order_id = $month_ps_order->where('del_time',0)->where("month",$monthStart)->first();
  106. if(empty($month_ps_order_id)){
  107. return [false,"请补充对应月份费用信息"];
  108. }else{
  109. $month_ps_order_id = $month_ps_order_id->id;
  110. }
  111. $model = new ExpenseClaimsDetails();
  112. //总金额
  113. $list = $model->where('expense_claims_id',$month_ps_order_id)->where('del_time',0)->select("fee_id","amount as total_amount","remark","entrust_type","item_id")->get()->toArray();
  114. ///分组
  115. $fee_type_list = Fee::Clear($user,$data)->where('del_time',0)->select("title","id","parent_id")->get()->toArray();
  116. return [true,$this->groupListByRoot($list,$fee_type_list)];
  117. }
  118. /**
  119. * 将报销明细按照一级费用分类进行分组
  120. * * @param array $list 报销明细列表 (含 fee_id, amount 等)
  121. * @param array $fee_type_list 费用类型树 (含 id, parent_id, title)
  122. * @return array
  123. */
  124. public function groupListByRoot(array $list, array $fee_type_list)
  125. {
  126. // 1. 建立 ID 索引,方便快速查找
  127. $idMap = array_column($fee_type_list, null, 'id');
  128. // 2. 预处理映射表:让所有子 ID 直接指向它的最顶层“祖宗” ID
  129. $childToRoot = [];
  130. $grouped = [];
  131. foreach ($fee_type_list as $type) {
  132. $current = $type;
  133. // 向上追溯直到 parent_id 为 0,即找到一级分类
  134. while ($current['parent_id'] != 0) {
  135. $current = $idMap[$current['parent_id']];
  136. }
  137. $childToRoot[$type['id']] = [
  138. 'id' => $current['id'],
  139. 'title' => $current['title']
  140. ];
  141. // if (!isset($grouped[$current['id']])) {
  142. // $grouped[$current['id']] = [
  143. // 'first_level_id' => $current['id'],
  144. // 'first_level_title' => $current['title'],
  145. // 'details' => []
  146. // ];
  147. // }
  148. }
  149. // 3. 遍历明细数据进行分组
  150. foreach ($list as $item) {
  151. $feeId = $item['fee_id'];
  152. // 获取该费用对应的一级分类信息
  153. $rootInfo = $childToRoot[$feeId] ?? ['id' => 0, 'title' => '其他费用'];
  154. $rootId = $rootInfo['id'];
  155. if (!isset($grouped[$rootId])) {
  156. $grouped[$rootId] = [
  157. 'first_level_id' => $rootId,
  158. 'first_level_title' => $rootInfo['title'],
  159. 'details' => []
  160. ];
  161. }
  162. $grouped[$rootId]['details'][] = $item;
  163. }
  164. // 4. 重置数组索引并返回
  165. return array_values($grouped);
  166. }
  167. public function auxiliaryAccountAdd($data,$user){
  168. list($status,$msg) = $this->auxiliaryAccountRule($data, $user);
  169. if(!$status) return [$status,$msg];
  170. try {
  171. DB::beginTransaction();
  172. $model = new AuxiliaryAccount();
  173. $model->code = $this->generateBillNo([
  174. 'top_depart_id' => $user['top_depart_id'],
  175. 'type' => ExpenseClaims::Order_type,
  176. 'period' => date("Ym", strtotime($data['month']))
  177. ]);
  178. $model->month = $data['month'];
  179. $model->crt_id = $user['id'];
  180. $model->top_depart_id = $user['top_depart_id'];
  181. $model->save();
  182. $this->saveDetail($model->id, time(), $data,$user['id']);
  183. DB::commit();
  184. }catch (\Exception $exception){
  185. DB::rollBack();
  186. return [false,$exception->getMessage()];
  187. }
  188. return [true, ''];
  189. }
  190. private function saveDetail($id, $time, $data,$crt_id){
  191. if(! empty($data['details'])){
  192. $unit = [];
  193. foreach ($data['details'] as $value){
  194. $unit[] = [
  195. 'auxiliary_account_id' => $id,
  196. 'voucher_date' => strtotime($value['voucher_date']),
  197. 'voucher_type' => $value['voucher_type'],
  198. 'voucher_no' => $value['voucher_no'],
  199. 'voucher_remark' => $value['voucher_remark'],
  200. 'voucher_amount' => $value['voucher_amount'],
  201. 'entrust_type' => $value['entrust_type']??"",
  202. 'type' => $value['type'],
  203. 'fee_id' => $value['fee_id']??0,
  204. 'remark' => $value['remark']??"",
  205. 'entrust1_amount' => $value['entrust1_amount']??0,
  206. 'entrust2_amount' => $value['entrust2_amount']??0,
  207. 'aggregation_amount' => $value['aggregation_amount']??00,
  208. 'total_amount' => $value['total_amount'],
  209. 'top_depart_id' => $data['top_depart_id'],
  210. 'item_id' => $value['item_id'],
  211. 'crt_time' => $time,
  212. 'crt_id' => $crt_id,
  213. ];
  214. }
  215. if(!empty($unit)) AuxiliaryAccountDetails::insert($unit);
  216. }
  217. }
  218. private function auxiliaryAccountRule(&$data, $user, $is_add = true){
  219. if (empty($data['month'])) return [false, '月份不能为空'];
  220. $data['month'] = $this->changeDateToDate($data['month']);
  221. $monthStart = $data['month'];
  222. $monthEnd = strtotime('+1 month', $monthStart) - 1;
  223. if(empty($data['details'])) return [false, '研发支出辅助账单明细不能为空'];
  224. foreach ($data['details'] as $index => $item) {
  225. if (!isset($item['voucher_date'])) {
  226. return [false, "第" . ($index + 1) . "凭证日期缺失"];
  227. }
  228. if (!isset($item['voucher_no'])) {
  229. return [false, "第" . ($index + 1) . "凭证号数缺失"];
  230. }
  231. if (!isset($item['voucher_amount'])) {
  232. return [false, "第" . ($index + 1) . "凭证记载金额缺失"];
  233. }
  234. $res = $this->checkNumber($item['voucher_amount'], 2, 'non-negative');
  235. if (! $res['valid']) return [false, "第" . ($index + 1) . "凭证记载金额" . $res['error']];
  236. if (!isset($item['voucher_type'])) {
  237. return [false, "第" . ($index + 1) . "凭证类型缺失"];
  238. }
  239. if (!isset($item['aggregation_amount'])) {
  240. return [false, "第" . ($index + 1) . "凭证归集金额缺失"];
  241. }
  242. $res = $this->checkNumber($item['aggregation_amount'], 2, 'non-negative');
  243. if (! $res['valid']) return [false, "第" . ($index + 1) . "凭证归集金额" . $res['error']];
  244. if (!isset($item['voucher_remark'])) {
  245. return [false, "第" . ($index + 1) . "凭证摘要缺失"];
  246. }
  247. // 将报销日期转换为时间戳
  248. $claimTime = strtotime($item['voucher_date']);
  249. // 判断:voucher_date 必须早于 month
  250. // 如果 voucher_date 是 2026-02-28,而 month 是 2026-03-01,则校验通过
  251. if ($claimTime < $monthStart || $claimTime > $monthEnd) return [false, "第" . ($index + 1) . "凭证日期必须早于当前结算月份(" . date("Y-m", $data['month']) . ")"];
  252. }
  253. $query = AuxiliaryAccount::where('top_depart_id', $data['top_depart_id'])
  254. ->where('month', $monthStart)
  255. ->where('del_time', 0);
  256. if (!$is_add) {
  257. if (empty($data['id'])) return [false, 'ID不能为空'];
  258. $query->where('id', '<>', $data['id']);
  259. }
  260. if ($query->exists()) return [false, date("Y-m", $monthStart) . '已存在研发支出辅助账单'];
  261. return [true, ''];
  262. }
  263. public function auxiliaryAccountEdit($data,$user){
  264. list($status,$msg) = $this->auxiliaryAccountRule($data, $user, false);
  265. if(!$status) return [$status,$msg];
  266. try {
  267. DB::beginTransaction();
  268. $model = AuxiliaryAccount::where('id',$data['id'])->first();
  269. // $model->save();
  270. $time = time();
  271. AuxiliaryAccountDetails::where('del_time',0)
  272. ->where('auxiliary_account_id', $model->id)
  273. ->update(['del_time' => $time]);
  274. $this->saveDetail($model->id, $time, $data,$user['id']);
  275. DB::commit();
  276. }catch (\Exception $exception){
  277. DB::rollBack();
  278. return [false,$exception->getMessage()];
  279. }
  280. return [true, ''];
  281. }
  282. public function auxiliaryAccountDel($data){
  283. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  284. try {
  285. DB::beginTransaction();
  286. $time = time();
  287. AuxiliaryAccount::where('del_time',0)
  288. ->whereIn('id',$data['id'])
  289. ->update(['del_time' => $time]);
  290. AuxiliaryAccountDetails::where('del_time',0)
  291. ->whereIn('auxiliary_account_id', $data['id'])
  292. ->update(['del_time' => $time]);
  293. DB::commit();
  294. }catch (\Exception $exception){
  295. DB::rollBack();
  296. return [false,$exception->getMessage()];
  297. }
  298. return [true, ''];
  299. }
  300. public function auxiliaryAccountDetail($data, $user){
  301. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  302. $customer = AuxiliaryAccount::where('del_time',0)
  303. ->where('id',$data['id'])
  304. ->first();
  305. if(empty($customer)) return [false,'单据不存在或已被删除'];
  306. $customer = $customer->toArray();
  307. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  308. $customer['crt_time'] =$this->utcTime($customer['crt_time']);
  309. $customer['month'] =$this->utcTime($customer['month']);
  310. $details = $this->getDetail($data['id']);
  311. $customer["details"] = $details;
  312. return [true, $customer];
  313. }
  314. private function getDetail($id){
  315. $data = AuxiliaryAccountDetails::where('del_time',0)
  316. ->where('auxiliary_account_id', $id)
  317. ->select('*')
  318. ->get()->toArray();
  319. foreach ($data as &$v){
  320. $v['voucher_date'] = $this->utcTime($v['voucher_date']);
  321. }
  322. return $data;
  323. }
  324. private function utcTime($time){
  325. return Carbon::createFromTimestamp($time, 'UTC')
  326. ->toIso8601ZuluString();
  327. }
  328. }