コード例 #1
0
        public async Task <ActionResult <PaySlipViewModel> > PostPaySlip(PaySlipViewModel paySlip)
        {
            if (paySlip != null)
            {
                try
                {
                    await Task.Run(() =>
                    {
                        paySlip.DateCreated = DateTime.Now;
                        _payslipService.Add(paySlip);
                        _payslipService.SaveChanges();
                        return(Ok("Thêm phiếu chi thành công!"));
                    });
                }
                catch
                {
                    throw new Exception(string.Format("Lỗi khi thêm dữ liệu"));
                }
            }

            return(CreatedAtAction("GetPaySlip", new { id = paySlip.Id }, paySlip));
        }
コード例 #2
0
        public void ApplyPeriod(PayrollPeriodApply request)
        {
            var trans = UnitOfWork.CreateTransac();

            try
            {
                // get period by Id
                var entity = FirstOrDefaultE(r => r.Id == request.Id);
                // get template
                var template      = paySlipTemplateService.GetEById(entity.PaySlipTemplateId.Value);
                var payrolDetails = templateDetailMappingService.GetPayrollByTemplateId(template.Id);
                if (request.GroupId.HasValue)
                {
                    request.ListEmp = employeeService.GetByGroupId(request.GroupId.Value).Select(p => p.Id).ToList();
                }



                foreach (var empId in request.ListEmp)
                {
                    var employee = employeeService.FindEById(empId);
                    if (employee == null)
                    {
                        throw new Exception();
                    }

                    var paySlip = new PaySlip()
                    {
                        EmployeeId      = empId,
                        PayrollPeriodId = entity.Id,
                        FromDate        = entity.FromDate.Value,
                        ToDate          = entity.ToDate.Value,
                    };

                    var listAllPaySlipAttribute = paySlipAttributeService.Get();
                    foreach (var attribute in listAllPaySlipAttribute)
                    {
                        switch (attribute.Code)
                        {
                        case "NBD":
                            paySlip.PayslipAttributeMapping.Add(new PayslipAttributeMapping
                            {
                                PayslipId   = paySlip.Id,
                                AttributeId = attribute.Id,
                                Value       = entity.FromDate.Value.Day
                            });
                            break;

                        case "NKT":
                            paySlip.PayslipAttributeMapping.Add(new PayslipAttributeMapping
                            {
                                PayslipId   = paySlip.Id,
                                AttributeId = attribute.Id,
                                Value       = entity.ToDate.Value.Day
                            });
                            break;

                        case "LCB":
                            paySlip.PayslipAttributeMapping.Add(new PayslipAttributeMapping
                            {
                                PayslipId   = paySlip.Id,
                                AttributeId = attribute.Id,
                                Value       = 0
                            });
                            break;

                        case "TKL":
                            paySlip.PayslipAttributeMapping.Add(new PayslipAttributeMapping
                            {
                                PayslipId   = paySlip.Id,
                                AttributeId = attribute.Id,
                                Value       = entity.ToDate.Value.Month
                            });
                            break;

                        default:
                            break;
                        }
                    }
                    // create PaySlip
                    var payslipId = paySlipService.Add(paySlip);

                    // Create List PaySlipItem
                    if (payrolDetails.Count() == 0)
                    {
                        throw new Exception();
                    }
                    foreach (var payrollDetailSelected in payrolDetails)
                    {
                        var paySlipItem = new PaySlipItem()
                        {
                            Name            = payrollDetailSelected.Name,
                            PayrollDetailId = payrollDetailSelected.Id,
                            PaySlipId       = payslipId
                        };

                        paySlipItemService.Add(paySlipItem);
                    }
                }
                trans.Commit();
            }
            catch (Exception e)
            {
                trans.Dispose();
                throw e;
            }
        }