AuxiliaryAccountService.php 15 KB

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