AuxiliaryAccountService.php 14 KB

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