/// <summary> /// Generate invoice of milestone which has status "Approved for Payment" and approved /// in text file as per Open Account format /// </summary> /// <param name="companyId">Company id</param> /// <param name="divisionId">Division id</param> /// <param name="invoiceCustomerId">Invoice customer id</param> /// <param name="fromDate">The from date</param> /// <param name="toDate">The end date</param> /// <param name="userId"></param> /// <param name="invoiceDate"></param> public void GenerateInvoice(int companyId, int divisionId, int invoiceCustomerId, DateTime fromDate, DateTime toDate, int?userId, DateTime invoiceDate) { // Get contract details based on filtered criteria List <InvoiceHeaderVO> invoiceHeaderVos = approveMaintenanceDAL.GenerateInvoice(companyId, divisionId, invoiceCustomerId, fromDate, toDate, userId); if (invoiceHeaderVos.Count <= 0) { throw new ApplicationException(Constants.NO_MILESTONE_FOUND_FOR_AP); } // List of milestone all milestones var invoiceHeaderVOList = new List <InvoiceHeaderVO>(); // Already processed contract ids List <string> processedInvoice = new List <string>(); //Process invoice by group foreach (var invoiceHeaderVo in invoiceHeaderVos) { //Check generate invoice by group bool isCustomerGroupBy = CheckCustomerGroupBy(invoiceHeaderVo.CompanyId, invoiceHeaderVo.InvoiceCustomerId); if (isCustomerGroupBy) { var invoiceHeaderByGroup = new List <InvoiceHeaderVO>(); if (!processedInvoice.Contains(Convert.ToString(invoiceHeaderVo.ContractId + "-" + invoiceHeaderVo.DocumentTypeID))) { invoiceHeaderByGroup = invoiceHeaderVos.Where(m => m.InvoiceCustomerId == invoiceHeaderVo.InvoiceCustomerId && m.CompanyId == invoiceHeaderVo.CompanyId && m.Currency == invoiceHeaderVo.Currency && m.DocumentTypeID == invoiceHeaderVo.DocumentTypeID).ToList(); List <InvoiceHeaderVO> groupByCustomer = ProcessInvoiceByGroup(invoiceHeaderByGroup, fromDate, toDate, invoiceDate); // Set posting periad and year foreach (var invoiceHeaderVO in groupByCustomer) { // Set posting perios & posting year for invoice header GetPostingPeriodAndYear(invoiceHeaderVO); } invoiceHeaderVOList.AddRange(groupByCustomer); //Process invoice by grouping of customer details //InvoiceHeaderVOList.Add(ProcessInvoiceByGroup(InvoiceHeaderByGroup, fromDate, toDate)); // Add contract id in already generated invoice list so next time it will not generate invoice processedInvoice.AddRange(invoiceHeaderByGroup.Select(invoice => invoice.ContractId + "-" + invoice.DocumentTypeID)); } } } // Process each invoice by contract id foreach (var invoiceHeaderVo in invoiceHeaderVos) { // Check is invoice already processed in group by customer if (!processedInvoice.Contains(Convert.ToString(invoiceHeaderVo.ContractId + "-" + invoiceHeaderVo.DocumentTypeID))) { //Generate invoice by contract, company, customer InvoiceHeaderVO invoiceHeaderVO = approveMaintenanceDAL.GetContarctDetails(invoiceHeaderVo, invoiceDate); // Set posting perios & posting year for invoice header GetPostingPeriodAndYear(invoiceHeaderVO); // Process each invoice detail List <InvoiceGLDetailVO> invoiceDetails = approveMaintenanceDAL.GetInvoiceDetails(invoiceHeaderVO, fromDate, toDate); // Process each invoice line Generate N & X List <InvoiceDetailVO> invoiceDetailVos = ProcessInvoiceDetails(invoiceDetails, invoiceHeaderVO, fromDate, toDate); decimal totalAmount = 0, crTotalAmount = 0; InvoiceHeaderVO crHeaderVO = null; foreach (var invoiceDetailVo in invoiceDetailVos) { foreach (var detailVo in invoiceDetailVo.NominalLinesList) { // Check milestone is credit or not if (detailVo.Value < 0) { // Create new header for creadit invoice if (crHeaderVO == null) { crHeaderVO = new InvoiceHeaderVO(invoiceHeaderVO, invoiceDate); } crTotalAmount += detailVo.Value; } else { // Set total amount for invoice header totalAmount += detailVo.Value; } } if (invoiceDetailVo.isCreaditInvoice) { if (crHeaderVO != null) { crHeaderVO.InvoiceDetailVos.Add(invoiceDetailVo); } } else { invoiceHeaderVO.InvoiceDetailVos.Add(invoiceDetailVo); // Check if invoice or credit note, negative value = credit invoiceHeaderVO.Description = "Maintenance Invoice"; } //// Check milestone is credit or not //if (invoiceDetailVo.InvoiceGlDetails.Value < 0) //{ // // Create new header for creadit invoice // if (crHeaderVO == null) // { // crHeaderVO = new InvoiceHeaderVO(invoiceHeaderVO, invoiceDate); // } // crTotalAmount += invoiceDetailVo.InvoiceGlDetails.Value; // crHeaderVO.InvoiceDetailVos.Add(invoiceDetailVo); // //ValidateInvoiceHeader(crHeaderVO); // //InvoiceHeaderVOList.Add(crHeaderVO); // //creaditInvoiceHeaderVOs.Add(crHeaderVO); //} //else //{ // // Set total amount for invoice header // totalAmount += invoiceDetailVo.InvoiceGlDetails.Value; // invoiceHeaderVO.InvoiceDetailVos.Add(invoiceDetailVo); // // Check if invoice or credit note, negative value = credit // invoiceHeaderVO.Description = "Maintenance Invoice"; //} } // Validate credit note invoice // CR-ARBS-46 if (crHeaderVO != null && crHeaderVO.InvoiceDetailVos.Count > 0) { crHeaderVO.Description = Constants.MAINTENANCE_CREDIT_NOTE; crHeaderVO.TotalAmount = crTotalAmount; ValidateInvoiceHeader(crHeaderVO); // Add item in invoice list invoiceHeaderVOList.Add(crHeaderVO); } // If invoice header is not credit & has invoice deatils //then add into invoice header list for print if (invoiceHeaderVO.InvoiceDetailVos.Count > 0) { // Set aggregate amount of invoice header invoiceHeaderVO.TotalAmount = totalAmount; ValidateInvoiceHeader(invoiceHeaderVO); invoiceHeaderVOList.Add(invoiceHeaderVO); } } } //Throw Error message if any error is there after completion whole process if (errorList.Count > 0) { TextFileUtility textFile = new TextFileUtility(); string errorLogPath = ApplicationConfiguration.GetBillToOAErrorLogPath(); string errorLogFileName = Constants.BILL_TO_OA_ERROR_LOG_FILENAME; // Add user id in file name so for each user unique gets created errorLogFileName += userId + ".txt"; // wrire Bill to OA error log textFile.WriteLog(errorLogPath, errorLogFileName, errorList); throw new ApplicationException(Constants.INVALID_MILESTONE_BILL_TO_OA); } else { //Create flat file of invoice details CreateInvoiceFile(invoiceHeaderVOList); } }