public DefaultApiResponse ImportPayment(PaymentCreateModel payment, string token, out BusinessRulesApiResponse businessRulesApiResponse)
        {
            var _data = JsonConvert.SerializeObject(payment, Newtonsoft.Json.Formatting.None,
                                                    new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            var _address = ApiHelper.Instance.SiteUrl + ApiHelper.Instance.PaymentCreateEndpoint;

            businessRulesApiResponse = null;

            try
            {
                var _jsonResult = ApiHelper.Instance.WebClient(token).UploadString(_address, "POST", _data);

                if (_jsonResult != "null")
                {
                    return(new DefaultApiResponse(200, "OK", new string[] { }));
                }

                return(new DefaultApiResponse(500, "Internal Application Error: Fail to Import Project", new string[] { }));
            }
            catch (WebException _webEx)
            {
                using StreamReader _r = new StreamReader(_webEx.Response.GetResponseStream());
                string _responseContent = _r.ReadToEnd();

                return(ApiHelper.Instance.ProcessApiResponseContent(_webEx, _responseContent, out businessRulesApiResponse));
            }
        }
        public async Task <Response> Create(PaymentCreateModel model, ClaimsPrincipal claimsPrincipal)
        {
            using (var context = _applicationDbContextFactory.Create())
            {
                var Entity = Mapper.Map <Payment>(model);
                var Group  = context.GetGroupStudentId(model.StudentId, model.GroupId);
                var User   = await _userManager.FindByNameAsync(claimsPrincipal.Identity.Name);

                Entity.UserId = User.Id;
                if (Entity.Sum > Group.OneMounthSum)
                {
                    return(new Response {
                        Status = 500, Message = "Сумма взноса больше суммы контракта"
                    });
                }
                if (context.StudentGroups.Any(i => i.GroupId == model.GroupId && i.StudentId == model.GroupId))
                {
                    return new Response {
                               Status = 500, Message = "Студент не зарегистрирован в данной группе"
                    }
                }
                ;
                var result = context.Add(Entity);
                context.SaveChanges();
                context.PaymentHistories.Add(new PaymentHistory {
                    Action = "Создание", PaymentId = result.Entity.Id, DateTime = DateTime.Now, UserId = User.Id
                });
                context.SaveChanges();
                return(new Response {
                    Status = 100, Message = "Запрос прошел успешно"
                });
            }
        }
 public async Task <ActionResult <Response> > Create(PaymentCreateModel model)
 {
     return(await paymentService.Create(model, User));
 }
        private void WorkerFetcherDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            if (dataGridView_payment != null && dataGridView_payment.RowCount > 1)
            {
                _errorRowCount = 0;

                //while validating, deactivate other buttons
                Invoke((MethodInvoker)(() => button_validate.Enabled = false));
                Invoke((MethodInvoker)(() => button_import.Enabled = false));
                Invoke((MethodInvoker)(() => button_clear.Enabled = false));
                Invoke((MethodInvoker)(() => button_paymentSelectFile.Enabled = false));

                Invoke((MethodInvoker)(() => textBox_paymentImportMessages.AppendText("Start time: " + DateTime.Now)));

                try
                {
                    foreach (DataGridViewRow _row in dataGridView_payment.Rows)
                    {
                        if (WorkerFetcher.CancellationPending)
                        {
                            break;
                        }

                        _isMappingFieldValueToIDCorrect = true;
                        _isFirstTimeInvalidMapping      = true;

                        if (_row.DataBoundItem != null)
                        {
                            PaymentCreateModel _newPayment = new PaymentCreateModel
                            {
                                Name                 = PaymentHandler.Instance.CheckAndGetString(dataGridView_payment, _paymentName, _row),
                                ProjectID            = (int)MapFieldValueToID(_projectNo, _row, false),
                                ProjectSubContractID = (int)MapFieldValueToID(_contractName, _row, false),
                                TaskID               = (int)MapFieldValueToID(_taskNo, _row, false),
                                InvoiceDate          = PaymentHandler.Instance.CheckAndGetDate(dataGridView_payment, _invoiceDate, _row),
                                Amount               = PaymentHandler.Instance.CheckAndGetDouble(dataGridView_payment, _amount, _row),
                                Quantity             = PaymentHandler.Instance.CheckAndGetDouble(dataGridView_payment, _quantity, _row),
                                UnitTypeID           = (int)MapFieldValueToID(_unitType, _row, false),
                                ProductNumber        = PaymentHandler.Instance.CheckAndGetString(dataGridView_payment, _productNo, _row),
                                DiscountPercentage   = PaymentHandler.Instance.CheckAndGetDouble(dataGridView_payment, _discountPercentage, _row),
                                Comment              = PaymentHandler.Instance.CheckAndGetString(dataGridView_payment, _comment, _row),
                                IsReadyForInvoicing  = PaymentHandler.Instance.CheckAndGetBoolean(dataGridView_payment, _isReadyForInvoicing, _row),
                                IsFixedPricePayment  = PaymentHandler.Instance.CheckAndGetBoolean(dataGridView_payment, _isFixedPricePayment, _row)
                            };

                            if (_isMappingFieldValueToIDCorrect)
                            {
                                if (_senderButton.Name == button_validate.Name)
                                {
                                    var _defaultApiResponse = PaymentHandler.Instance.ValidatePayment(_newPayment,
                                                                                                      AuthenticationHandler.Instance.Token, out var _businessRulesApiResponse);

                                    _errorRowCount = ApiHelper.Instance.HandleApiResponse(_defaultApiResponse, _row, _businessRulesApiResponse,
                                                                                          textBox_paymentImportMessages, _errorRowCount, WorkerFetcher, this);
                                }
                                else
                                {
                                    var _defaultApiResponse = PaymentHandler.Instance.ImportPayment(_newPayment,
                                                                                                    AuthenticationHandler.Instance.Token, out var _businessRulesApiResponse);

                                    _errorRowCount = ApiHelper.Instance.HandleApiResponse(_defaultApiResponse, _row, _businessRulesApiResponse,
                                                                                          textBox_paymentImportMessages, _errorRowCount, WorkerFetcher, this);
                                }
                            }
                        }
                    }

                    PaymentHandler.Instance.DisplayErrorRowCountAndSuccessMessage(_errorRowCount, button_import, button_validate, _senderButton, textBox_paymentImportMessages, this);
                }
                catch (FormatException _ex)
                {
                    MessageBox.Show(_ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                catch (Exception _ex)
                {
                    MessageBox.Show(_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                //reactivate buttons after work is done
                Invoke((MethodInvoker)(() => button_validate.Enabled = true));
                Invoke((MethodInvoker)(() => button_clear.Enabled = true));
                Invoke((MethodInvoker)(() => button_paymentSelectFile.Enabled = true));
            }
        }
 public PaymentCommand(PaymentCreateModel payment)
 {
     Payment = payment;
 }