AuxiliaryAccountService.php 14 KB

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