Esempio n. 1
0
        public async Task <IActionResult> Apply2Invoices([FromBody] CashReceiptApply2InvoiceRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var cashReceiptApply2InvoiceResponse = await cashReceiptService.apply2Invoice(request, userService.getUserName(User.Claims));

            if (!cashReceiptApply2InvoiceResponse.isApplied)
            {
                return(BadRequest(cashReceiptApply2InvoiceResponse));
            }

            return(Ok(cashReceiptApply2InvoiceResponse));
        }
Esempio n. 2
0
        public async Task <CashReceiptApply2InvoiceResponse> apply2Invoice(CashReceiptApply2InvoiceRequest request, string userName)
        {
            var cashReceiptApply2InvoiceResponse = new CashReceiptApply2InvoiceResponse();

            if (!userService.isUserMemberOfGroup(_config.UserServiceConfig.UserRoutes[4].group, userName))
            {
                cashReceiptApply2InvoiceResponse.isApplied = false;
                cashReceiptApply2InvoiceResponse.message.Add(String.Format(_config.UserServiceConfig.accountMessages[2], userName, _config.UserServiceConfig.UserRoutes[4].header));
            }

            if (cashReceiptApply2InvoiceResponse.isApplied)
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        var response = await client.PostAsync(_config.CashReceiptConfig.GPServiceURLs[0],
                                                              new StringContent(JsonConvert.SerializeObject(request),
                                                                                Encoding.UTF8,
                                                                                _config.CashReceiptConfig.contentType));

                        if (response.IsSuccessStatusCode)
                        {
                            cashReceiptApply2InvoiceResponse.isApplied = true;
                            cashReceiptApply2InvoiceResponse.message.Add(_config.CashReceiptConfig.messages[2]);
                        }
                        else
                        {
                            cashReceiptApply2InvoiceResponse.isApplied = false;
                            var cashReceiptGPResponses = JsonConvert.DeserializeObject <List <CashReceiptGPResponse> >(response.Content.ReadAsStringAsync().Result);
                            foreach (CashReceiptGPResponse gPResponse in cashReceiptGPResponses)
                            {
                                cashReceiptApply2InvoiceResponse.message.Add(String.Format(_config.CashReceiptConfig.messages[1], gPResponse.ErrorNumber, gPResponse.Description));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    cashReceiptApply2InvoiceResponse.isApplied = false;
                    cashReceiptApply2InvoiceResponse.message.Add(_config.CashReceiptConfig.messages[1]);
                }
            }

            return(cashReceiptApply2InvoiceResponse);
        }