public async Task <IActionResult> ChargePaymentInvoice([FromBody] ChargePaymentInvoiceDto chargePaymentInvoice) { var paymentInvoiceItem = await Executor.GetHandler <GetPaymentInvoiceByIdQuery>().Process(h => h.ExecuteAsync(chargePaymentInvoice.PaymentInvoiceId)); var ownerType = (Owner.Type)Enum.ToObject(typeof(Owner.Type), chargePaymentInvoice.OwnerType); var paymentInvoiceDto = new PaymentInvoiceDto(); switch (ownerType) { case Owner.Type.Request: paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(new PaymentInvoice[] { paymentInvoiceItem }, opt => opt.Items["RequestCustomers"] = paymentInvoiceItem.Request.RequestCustomers).FirstOrDefault(); break; case Owner.Type.ProtectionDoc: paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(new PaymentInvoice[] { paymentInvoiceItem }, opt => opt.Items["ProtectionDocCustomers"] = paymentInvoiceItem.ProtectionDoc.ProtectionDocCustomers).First(); break; case Owner.Type.Contract: paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(new PaymentInvoice[] { paymentInvoiceItem }, opt => opt.Items["ContractCustomers"] = paymentInvoiceItem.Contract.ContractCustomers).First(); break; default: throw new ApplicationException(string.Empty, new ArgumentException($"{nameof(ownerType)}: {ownerType}")); } var notPaidStatus = Executor.GetQuery <GetDicPaymentStatusByCodeQuery>() .Process(q => q.Execute(DicPaymentStatusCodes.Notpaid)); if (paymentInvoiceItem.StatusId == notPaidStatus.Id) { if (paymentInvoiceDto.Remainder > 100) { return(Ok("Невозможно списать оплату. Сумма фактической оплаты меньше Суммы с НДС, выставленной оплаты, более чем на 100 тенге. Оплатите счёт и повторите попытку")); } } DateTimeOffset date; if (!DateTimeOffset.TryParse(chargePaymentInvoice.ChargeDate, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.None, out date)) { return(Ok("Невозможно преобразовать дату:" + chargePaymentInvoice.ChargeDate)); } ; await Executor.GetHandler <ChargePaymentInvoiceByIdOutgoingDateHandler>().Process(h => h.ExecuteAsync(ownerType, chargePaymentInvoice.PaymentInvoiceId, date)); return(Ok("Оплата успешно списана")); }
public async Task <IActionResult> Post(Owner.Type ownerType, [FromBody] PaymentInvoiceDto invoiceDto) { var result = await _mediator.Send(new Create.Command(invoiceDto, ownerType)); return(Ok(result)); }
public async Task <IActionResult> Post(Owner.Type ownerType, [FromBody] PaymentInvoiceDto invoiceDto) { var newPaymentInvoice = _mapper.Map <PaymentInvoiceDto, PaymentInvoice>(invoiceDto, opt => opt.Items["OwnerType"] = ownerType); newPaymentInvoice.CreateUserId = NiisAmbientContext.Current.User.Identity.UserId; var paymentInvoiceId = await _executor.GetCommand <CreatePaymentInvoiceCommand>() .Process(c => c.ExecuteAsync(newPaymentInvoice)); var paymentInvoice = await _executor.GetQuery <GetPaymentInvoiceByIdQuery>() .Process(q => q.ExecuteAsync(paymentInvoiceId)); PaymentInvoiceDto paymentInvoiceDto; switch (ownerType) { case Owner.Type.Request: if (!paymentInvoice.RequestId.HasValue) { throw new ArgumentNullException(nameof(paymentInvoice.RequestId)); } var request = await _executor.GetQuery <GetRequestByIdQuery>() .Process(q => q.ExecuteAsync(paymentInvoice.RequestId.Value)); paymentInvoiceDto = _mapper.Map <PaymentInvoice, PaymentInvoiceDto>(paymentInvoice, opt => opt.Items["RequestCustomers"] = request.RequestCustomers); break; case Owner.Type.Contract: if (!paymentInvoice.ContractId.HasValue) { throw new ArgumentNullException(nameof(paymentInvoice.ContractId)); } var contract = await _executor.GetQuery <GetContractByIdQuery>() .Process(q => q.ExecuteAsync(paymentInvoice.ContractId.Value)); paymentInvoiceDto = _mapper.Map <PaymentInvoice, PaymentInvoiceDto>(paymentInvoice, opt => opt.Items["ContractCustomers"] = contract.ContractCustomers); break; case Owner.Type.ProtectionDoc: if (!paymentInvoice.ProtectionDocId.HasValue) { throw new ArgumentNullException(nameof(paymentInvoice.ProtectionDocId)); } var protectionDoc = await _executor.GetQuery <GetProtectionDocByIdQuery>() .Process(q => q.ExecuteAsync(paymentInvoice.ProtectionDocId.Value)); paymentInvoiceDto = _mapper.Map <PaymentInvoice, PaymentInvoiceDto>(paymentInvoice, opt => opt.Items["ProtectionDocCustomers"] = protectionDoc.ProtectionDocCustomers); break; default: throw new ApplicationException(string.Empty, new ArgumentException($"{nameof(ownerType)}: {ownerType}")); } return(Ok(paymentInvoiceDto)); }
public async Task <bool> ExecuteAsync(Owner.Type ownerType, int paymentInvoiceId, PaymentInvoiveChangeFlag changeFlag = PaymentInvoiveChangeFlag.NewChargedPaymentInvoice, string status = null, DateTimeOffset?chargedDate = null) { bool returnValue = false; try { var paymentInvoice = await Executor.GetQuery <GetPaymentInvoiceByIdQuery>() .Process(q => q.ExecuteAsync(paymentInvoiceId)); var paymentInvoices = new PaymentInvoice[] { paymentInvoice }; var RequestNums = string.Empty; var RequestTypes = string.Empty; var LinkedProtectionDocNums = string.Empty; var ProtectionDocTypes = string.Empty; var ContractNums = string.Empty; var ContractTypes = string.Empty; var paymentInvoiceDto = new PaymentInvoiceDto(); var DepartmentName = string.Empty; var ProtectionDocTypeNumber = string.Empty; switch (ownerType) { case Owner.Type.Contract: var contractItem = await Executor.GetQuery <GetContractByIdQuery>().Process(q => q.ExecuteAsync(paymentInvoice.ContractId.Value)); var contractDetailDto = _mapper.Map <Contract, ContractDetailDto>(contractItem, opt => opt.Items[nameof(contractItem.ContractCustomers)] = contractItem.ContractCustomers); ContractNums = contractItem.GosNumber; ContractTypes = contractItem.Type.NameRu; DepartmentName = contractItem.Department.NameRu; var requestIds = contractDetailDto.RequestRelations.Select(p => p.Request.Id); var requests = Executor.GetQuery <GetRequestsByIdsQuery>() .Process(q => q.Execute(requestIds.ToArray())).ToArray(); RequestNums = contractItem.GosNumber; RequestTypes = string.Join("; ", requests.Select(r => r.RequestType.NameRu)); var projectedDocIds = contractDetailDto.ProtectionDocRelations.Select(p => p.ProtectionDoc.Id); var protecedDocs = Executor.GetQuery <GetProtectionDocsByIdsQuery>() .Process(q => q.Execute(projectedDocIds.ToArray())).ToArray(); LinkedProtectionDocNums = string.Join("; ", protecedDocs.Select(pd => pd.GosNumber)); ProtectionDocTypes = string.Join("; ", protecedDocs.Select(pd => pd.Type.NameRu)); paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(paymentInvoices, opt => opt.Items["ContractCustomers"] = paymentInvoice.Contract.ContractCustomers).First(); break; case Owner.Type.ProtectionDoc: ProtectionDocTypes = paymentInvoice.ProtectionDoc.Type.NameRu; RequestNums = paymentInvoice.ProtectionDoc.GosNumber; ProtectionDocTypeNumber = paymentInvoice.ProtectionDoc.GosNumber; RequestTypes = paymentInvoice.ProtectionDoc.Request?.RequestType?.NameRu; DepartmentName = paymentInvoice.ProtectionDoc.Request?.Department.NameRu; paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(paymentInvoices, opt => opt.Items["ProtectionDocCustomers"] = paymentInvoice.ProtectionDoc.ProtectionDocCustomers).First(); break; case Owner.Type.Request: if (paymentInvoice.Request.Contracts.Any()) { var contractId = paymentInvoice.Request.Contracts.First().ContractId; contractItem = Executor.GetQuery <GetContractByIdQuery>().Process(q => q.ExecuteAsync(contractId)).Result; ContractNums = contractItem.GosNumber; ContractTypes = contractItem.Type.NameRu; } RequestNums = paymentInvoice.Request.RequestNum; RequestTypes = paymentInvoice.Request.RequestType?.NameRu; DepartmentName = paymentInvoice.Request.Department.NameRu; paymentInvoiceDto = _mapper.Map <IEnumerable <PaymentInvoiceDto> >(paymentInvoices, opt => opt.Items["RequestCustomers"] = paymentInvoice.Request.RequestCustomers).First(); break; default: throw new NotImplementedException(); } var exportTo1CList = new List <ExoprtPaymentTo1CModel>(); if (((status != null && status == DicPaymentStatusCodes.Charged) || paymentInvoice.Status.Code == DicPaymentStatusCodes.Charged) && (paymentInvoice.DateExportedTo1C == null || changeFlag != PaymentInvoiveChangeFlag.NewChargedPaymentInvoice)) { var paymentsLinkedToInvoive = paymentInvoice.PaymentUses.GroupBy(p => p.PaymentId); foreach (var paymentUse in paymentsLinkedToInvoive) { var paymentID = paymentUse.First().PaymentId.Value; var paymentUseID = paymentUse.First().Id; var amount = paymentUse.Sum(p => p.Amount); var paymentItem = await Executor.GetQuery <GetPaymentByIdQuery>() .Process(q => q.ExecuteAsync(paymentID)); var exportTo1C = new ExoprtPaymentTo1CModel(); exportTo1C.PaymentUseId = paymentUseID.ToString(); exportTo1C.Payment1CNumber = paymentItem.Payment1CNumber; exportTo1C.PaymentDate = paymentItem.PaymentDate; exportTo1C.PaymentAmount = paymentItem.Amount.Value; exportTo1C.PurposeDescription = paymentItem.PurposeDescription; exportTo1C.Payer = paymentItem.Payer; exportTo1C.PaymentID = paymentItem.Id; exportTo1C.PaymentCNumberBVU = paymentItem.PaymentCNumberBVU; exportTo1C.PayerBinOrInn = paymentItem.PayerBinOrInn; exportTo1C.PayerRNN = paymentItem.PayerRNN; exportTo1C.TariffCode = paymentInvoice.Tariff.Code; exportTo1C.TariffName = paymentInvoice.Tariff.NameRu; exportTo1C.LinkedProtectionDocNums = LinkedProtectionDocNums; exportTo1C.ProtectionDocTypeNumber = ProtectionDocTypeNumber; exportTo1C.RequestNum = RequestNums; exportTo1C.ProtectionDocType = ProtectionDocTypes; exportTo1C.RequestType = RequestTypes; exportTo1C.ContractNum = ContractNums; exportTo1C.ContractType = ContractTypes; exportTo1C.TotalAmountNds = amount; var statusName = paymentInvoiceDto.StatusNameRu; if (status != null) { statusName = Executor.GetQuery <GetDicPaymentStatusByCodeQuery>() .Process(q => q.Execute(status)).NameRu; } exportTo1C.StatusName = statusName; exportTo1C.CreditDate = paymentInvoiceDto.CreditDate; exportTo1C.CreditUser = paymentInvoiceDto.CreditUser; exportTo1C.WriteOffDate = paymentInvoiceDto.WriteOffDate; if (changeFlag == PaymentInvoiveChangeFlag.NewChargedPaymentInvoice) { exportTo1C.WriteOffDate = chargedDate.Value; } exportTo1C.WriteOffUser = paymentInvoiceDto.WriteOffUser; exportTo1C.DepartmentName = DepartmentName; exportTo1C.ChangeFlag = changeFlag; if (changeFlag == PaymentInvoiveChangeFlag.PaymentInvoiceChargedDateIsChanged) { exportTo1C.DeleteChangeDate = paymentInvoice.DateOfChangingChargedPaymentInvoice; exportTo1C.EmployeeAndPositonWhoChangedRecord = paymentInvoice.EmployeeAndPositonWhoChangedChargedPaymentInvoice; exportTo1C.DeleteChangeReason = paymentInvoice.ReasonOfChangingChargedPaymentInvoice; } else if (changeFlag == PaymentInvoiveChangeFlag.PaymentInvoiceChargedDateIsDeleted) { exportTo1C.DeleteChangeDate = paymentInvoice.DateOfDeletingChargedPaymentInvoice; exportTo1C.EmployeeAndPositonWhoChangedRecord = paymentInvoice.EmployeeAndPositonWhoDeleteChargedPaymentInvoice; exportTo1C.DeleteChangeReason = paymentInvoice.ReasonOfDeletingChargedPaymentInvoice; } exportTo1CList.Add(exportTo1C); } } dynamic connector = null; dynamic connection = null; dynamic newRecord = null; try { var connectionString = GetConnectionString(); connector = Activator.CreateInstance(Type.GetTypeFromProgID("V83.COMConnector")); connection = connector.Connect(connectionString); foreach (var item in exportTo1CList) { newRecord = connection.егистрыСведений.ИнтеграцияНИИС.СоздатьМенеджерЗаписи(); newRecord.КодПодраз = item.DepartmentName; newRecord.PaymentID = item.PaymentID.ToString(); newRecord.PaymentUseId = item.PaymentUseId; newRecord.БИН = item.PayerBinOrInn; if (item.WriteOffDate.HasValue) { newRecord.ДатаСписания = item.WriteOffDate.Value.ToLocalTime().ToString("dd.MM.yyyy HH:mm"); } newRecord.Сумма = item.TotalAmountNds; newRecord.НН = item.PayerRNN; newRecord.ИмяКонтры = item.Payer; newRecord.СтатусОплаты = item.StatusName; newRecord.ВидЗаявления = item.ContractType; newRecord.НомерДоговора = item.ContractNum; newRecord.ВидОПС = item.ProtectionDocType; newRecord.НомерЗаявки = item.RequestNum; newRecord.КодГруппыЗаявки = item.RequestType; newRecord.СвязанныеОбъекты = item.LinkedProtectionDocNums; newRecord.НазваниеУслуги = item.TariffName; newRecord.КодГруппыУслуги = item.TariffCode; newRecord.НомерДокумента1С = item.PaymentCNumberBVU; newRecord.НомерПлатежа = item.Payment1CNumber; newRecord.НомерОД = item.ProtectionDocTypeNumber; if (item.PaymentDate.HasValue) { newRecord.ДатаПлатежа = item.PaymentDate.Value.ToLocalTime().ToString("dd.MM.yyyy"); } newRecord.Назначение = item.PurposeDescription; newRecord.ПричинаУдаленияИзмененияЗаписи = item?.DeleteChangeReason; newRecord.ФИО_ДолжностьСотрудника_УдалившегоИзменившегоЗапись = item?.EmployeeAndPositonWhoChangedRecord; if (item.DeleteChangeDate.HasValue) { newRecord.ДатаУдаленияИзмененияСписаннойОплаты = item.DeleteChangeDate.Value.ToLocalTime().ToString("dd.MM.yyyy HH:mm"); } newRecord.Признак = (int)item.ChangeFlag; newRecord.ИсполнительСписал = item.WriteOffUser; newRecord.ИсполнительЗачел = item.CreditUser; if (item.CreditDate.HasValue) { newRecord.ДатаЗачтенияОплаты = item.CreditDate.Value.ToLocalTime().ToString("dd.MM.yyyy HH:mm"); } newRecord.Записать(); string message = $"Данные о списанном платеже переданы: {DateTimeOffset.UtcNow}, " + $"{item.PaymentCNumberBVU}, " + $"{item.PayerBinOrInn}, " + $"{item.PayerRNN}, " + $"{item.PaymentAmount}, " + $"{item.PaymentDate.Value}, " + $"{item.TariffCode}, " + $"{item.ProtectionDocTypeNumber}, " + $"{item.RequestNum}, " + $"{item.ProtectionDocType}, " + $"{item.ContractNum}, " + $"{item.ContractType}, " + $"{item.TotalAmountNds}, " + $"{item.StatusName}, " + $"{(int)item.ChangeFlag }."; var log = new LogRecord(); log.LogType = LogType.ExportTo1C; log.LogErrorType = LogErrorType.Information; log.Message = message; var logId = await Executor.GetCommand <CreateLogRecordCommand>().Process(q => q.ExecuteAsync(log)); } } catch (Exception exc) { string message = $"Ошибка подключения к БД 1С {DateTimeOffset.UtcNow}, {exc.Message}, {exc.InnerException} ,{exc.StackTrace}"; var log = new LogRecord(); log.LogType = LogType.ExportTo1C; log.LogErrorType = LogErrorType.Error; log.Message = message; var logId = await Executor.GetCommand <CreateLogRecordCommand>().Process(q => q.ExecuteAsync(log)); if (newRecord != null) { Marshal.ReleaseComObject(newRecord); } if (connection != null) { Marshal.ReleaseComObject(connection); } if (connector != null) { Marshal.ReleaseComObject(connector); } returnValue = false; throw; } if (newRecord != null) { Marshal.ReleaseComObject(newRecord); } if (connection != null) { Marshal.ReleaseComObject(connection); } if (connector != null) { Marshal.ReleaseComObject(connector); } returnValue = true; } catch (Exception ex) { var log = new LogRecord(); log.LogType = LogType.General; log.LogErrorType = LogErrorType.Error; log.Message = "Error 1C Export" + ex.StackTrace; var logId = await Executor.GetCommand <CreateLogRecordCommand>().Process(q => q.ExecuteAsync(log)); } return(returnValue); }