public async Task<IntegrationResponse> UploadInventory(InventoryTransferDTO file)
        {


            Task<IntegrationResponse> res = await Task.Factory.StartNew(async () =>
                                                           {
                                                               IntegrationResponse tresponse=new IntegrationResponse();
                                                               
                                                               var httpClient = MiddlewareHttpClient;
                                                               string _url = MiddlewareHttpClient.BaseAddress +
                          "api/Integrations/InventoryTransfer?username={0}&password={1}&integrationModule={2}";

                                                               string url = string.Format(_url, _userName, _otherUtilities.MD5Hash(_password), module);
                                                          
                                                               try
                                                               {
                                                                   file.Credentials.ApiUserName = _userName;
                                                                   file.Credentials.Password =
                                                                       _otherUtilities.MD5Hash(_password);
                                                                   file.Credentials.IntegrationModule = module;
                                                                   FileUtility.LogCommandActivity(string.Format("Posting inventory  {0} files to {1}", file.SalesmanInventoryList.Count, MiddlewareHttpClient.BaseAddress));
                                                                   var response =
                                                                       await httpClient.PostAsJsonAsync(url, file);
                                                                   response.EnsureSuccessStatusCode();
                                                                   tresponse = await response.Content.ReadAsAsync<IntegrationResponse>();

                                                               }
                                                               catch (Exception ex)
                                                               {
                                                                   FileUtility.LogError(ex.Message);
                                                                   MessageBox.Show("Server Error Details\n" + ex.Message);

                                                               }
                                                               return tresponse;
                                                           });
            return res.Result;

        }
        public HttpResponseMessage InventoryTransfer(InventoryTransferDTO data)
        {
            var response=new IntegrationResponse();
            try
            {

                _log.InfoFormat("Login attempt for {0} - {1} Action=>{2}", data.Credentials.ApiUserName, data.Credentials.IntegrationModule,"inventory transfter request");
                CostCentreLoginResponse loginResponse = _costCentreApplicationService.CostCentreLogin(data.Credentials.ApiUserName, data.Credentials.Password,
                                                                                                 "HQAdmin");
                AuditCCHit(loginResponse.CostCentreId, "Login", "Login attempt for " + data.Credentials.IntegrationModule, loginResponse.ErrorInfo);

                if (loginResponse.CostCentreId == Guid.Empty)
                {
                    response.ErrorInfo = "Invalid user credentials";
                    response.Result = "Error";
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, response.ErrorInfo);
                }
                response = _integrationService.ProcessInventory(data);
            }
            catch (Exception ex)
            {
                response.Result = "Error";
                response.ErrorInfo = "Error: An error occurred executing the task.Result details=>" + ex.Message + "Inner Exception:" + (ex.InnerException != null ? ex.InnerException.Message : "");
                _log.Error(string.Format("Error: An error occurred when processing inventory operation for {0}\n", data.Credentials.IntegrationModule), ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, response.ErrorInfo);
            }
            if (string.IsNullOrEmpty(response.Result))
            {
                response.Result = "Error";
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, response.ErrorInfo+"\n"+response.ResultInfo);
            }

            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
        public IntegrationResponse Process(InventoryTransferDTO inventoryTransferDto)
        {
            return Task.Run<IntegrationResponse>(async () =>
                                      {
                                          var response = new IntegrationResponse();
                                          try
                                          {


                                              if (string.IsNullOrEmpty(inventoryTransferDto.DistributorCode) ||
                                                  inventoryTransferDto.DistributorCode == "default")
                                              {
                                                  using (var ctx = new CokeDataContext(Con))
                                                  {
                                                      var distributors =
                                                          ctx.tblCostCentre.Where(
                                                              p =>
                                                              p.CostCentreType == (int)CostCentreType.Distributor &&
                                                              p.IM_Status == (int)EntityStatus.Active).ToList();


                                                      if (distributors.Count > 1 || distributors.Count == 0)
                                                      {
                                                          response.Result = "Error";
                                                          response.ResultInfo =
                                                              "Distributor cannot be determined from data provided";
                                                          response.ErrorInfo =
                                                              "Distributor cannot be determined from data provided";
                                                          return response;
                                                      }
                                                      else
                                                      {
                                                          inventoryTransferDto.DistributorCode =
                                                              distributors.FirstOrDefault().Cost_Centre_Code;
                                                      }
                                                  }
                                              }
                                              else
                                              {
                                                  using (var ctx = new CokeDataContext(Con))
                                                  {
                                                      tblCostCentre distributor = null;

                                                      distributor = ctx.tblCostCentre.FirstOrDefault(
                                                          p =>
                                                          p.CostCentreType == (int) CostCentreType.Distributor &&
                                                          p.Cost_Centre_Code.Trim().ToLower() ==
                                                          inventoryTransferDto.DistributorCode.Trim().ToLower() &&
                                                          p.IM_Status == (int) EntityStatus.Active);
                                                      if (distributor == null)
                                                      {
                                                          response.Result = "Error";
                                                          var msg =
                                                              string.Format(
                                                                  "Distributor with code {0} cannot be determined",
                                                                  inventoryTransferDto.DistributorCode);
                                                          response.ResultInfo = msg;
                                                          response.ErrorInfo = msg;
                                                          return response;
                                                      }

                                                  }
                                              }

                                              if (inventoryTransferDto.DistributorInventory != null &&
                                                  inventoryTransferDto.DistributorInventory.Any())
                                              {
                                                  var res = await AdjustDistributorInventory(inventoryTransferDto);
                                                  if (res != null && res.Any())
                                                  {
                                                      response.Result = "Error";
                                                      response.ResultInfo = string.Join(",", res);
                                                      response.ErrorInfo += string.Join(",", res);
                                                  }
                                                  else
                                                  {
                                                      bool ackwn =
                                                          await
                                                          Acknowledge(inventoryTransferDto.ExternalDocumentRefList, inventoryTransferDto.Credentials.IntegrationModule);
                                                      response.Result = "Success";
                                                      response.ResultInfo = "Task completed successfully";
                                                  }
                                              }
                                              else
                                              {
                                                  var res = await IssueInventoryAsync(inventoryTransferDto);
                                                  if (res != null && res.Any())
                                                  {
                                                      response.Result = "Error";
                                                      response.ResultInfo = string.Join(",", res);
                                                      response.ErrorInfo += ":" + response.ResultInfo;
                                                  }
                                                  else
                                                  {
                                                      bool ackwn =
                                                          await
                                                          Acknowledge(inventoryTransferDto.ExternalDocumentRefList,inventoryTransferDto.Credentials.IntegrationModule);
                                                      response.Result = "Success";
                                                      response.ResultInfo = "Task completed successfully";
                                                  }
                                              }
                                              return response;
                                          }
                                          catch (Exception ex)
                                          {
                                              response.Result = "Error";
                                              response.ResultInfo = ex.Message + "Details=>" + ex.InnerException !=
                                                                    null
                                                                        ? ex.InnerException.Message
                                                                        : "";
                                              response.ErrorInfo += ":" + response.ResultInfo;
                                              return response;
                                          }

                                      }).Result;

        }
示例#4
0
       async Task<IntegrationResponse> UploadInventory(InventoryTransferDTO file)
       {
           Task<IntegrationResponse> res = await Task.Factory.StartNew(async () =>
           {
               IntegrationResponse tresponse = new IntegrationResponse();

               var httpClient = HttpClient;
               httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               string url = HttpClient.BaseAddress + "api/Integrations/InventoryTransfer";

              
               try
               {
                   file.Credentials = new IntegrationCredential()
                                          {
                                              Password = CredentialsManager.GetPassword(),
                                              ApiUserName = CredentialsManager.GetUserName(),
                                              IntegrationModule = CredentialsManager.GetIntegrator()
                                          };
                   var response =
                       await httpClient.PostAsJsonAsync(url, file);
                   response.EnsureSuccessStatusCode();
                   tresponse = await response.Content.ReadAsAsync<IntegrationResponse>();

               }
               catch (Exception ex)
               {
                   FileUtility.LogError(ex.Message);
                   MessageBox.Show("Server Error Details\n" + ex.Message);

               }
               return tresponse;
           });
           return res.Result;

       }