public async Task <InventoryDocument> GetTestData(string user) { InventoryDocument invDoc = GetNewData(); await facade.Create(invDoc, user); return(invDoc); }
public async Task CreateInventoryDocument(MaterialsRequestNote Model, string Type) { //string inventoryDocumentURI = "inventory/inventory-documents"; string storageURI = "master/storages"; string uomURI = "master/uoms"; HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token); /* Get UOM */ Dictionary <string, object> filterUOM = new Dictionary <string, object> { { "unit", "MTR" } }; var responseUOM = httpClient.GetAsync($@"{APIEndpoint.Core}{uomURI}?filter=" + JsonConvert.SerializeObject(filterUOM)).Result.Content.ReadAsStringAsync(); Dictionary <string, object> resultUOM = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseUOM.Result); var jsonUOM = resultUOM.Single(p => p.Key.Equals("data")).Value; Dictionary <string, object> uom = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(jsonUOM.ToString())[0]; /* Get Storage */ var storageName = Model.UnitName.Equals("PRINTING") ? "Gudang Greige Printing" : "Gudang Greige Finishing"; Dictionary <string, object> filterStorage = new Dictionary <string, object> { { "name", storageName } }; var responseStorage = httpClient.GetAsync($@"{APIEndpoint.Core}{storageURI}?filter=" + JsonConvert.SerializeObject(filterStorage)).Result.Content.ReadAsStringAsync(); Dictionary <string, object> resultStorage = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseStorage.Result); var jsonStorage = resultStorage.Single(p => p.Key.Equals("data")).Value; Dictionary <string, object> storage = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(jsonStorage.ToString())[0]; /* Create Inventory Document */ List <InventoryDocumentItem> inventoryDocumentItems = new List <InventoryDocumentItem>(); List <MaterialsRequestNote_Item> list = Model.MaterialsRequestNote_Items .GroupBy(m => new { m.ProductId, m.ProductCode, m.ProductName }) .Select(s => new MaterialsRequestNote_Item { ProductId = s.First().ProductId, ProductCode = s.First().ProductCode, ProductName = s.First().ProductName, Length = s.Sum(d => d.Length) }).ToList(); foreach (MaterialsRequestNote_Item item in list) { InventoryDocumentItem inventoryDocumentItem = new InventoryDocumentItem(); inventoryDocumentItem.ProductId = int.Parse(item.ProductId); inventoryDocumentItem.ProductCode = item.ProductCode; inventoryDocumentItem.ProductName = item.ProductName; inventoryDocumentItem.Quantity = item.Length; inventoryDocumentItem.UomId = int.Parse(uom["Id"].ToString()); inventoryDocumentItem.UomUnit = uom["Unit"].ToString(); inventoryDocumentItems.Add(inventoryDocumentItem); } InventoryDocument inventoryDocument = new InventoryDocument { Date = DateTimeOffset.UtcNow, ReferenceNo = Model.Code, ReferenceType = "Surat Permintaan Barang", Type = Type, StorageId = int.Parse(storage["_id"].ToString()), StorageCode = storage["code"].ToString(), StorageName = storage["name"].ToString(), Items = inventoryDocumentItems }; InventoryDocumentFacade inventoryDocumentFacade = (InventoryDocumentFacade)ServiceProvider.GetService(typeof(InventoryDocumentFacade)); await inventoryDocumentFacade.Create(inventoryDocument, Username); //var response = httpClient.PostAsync($"{APIEndpoint.Inventory}{inventoryDocumentURI}", new StringContent(JsonConvert.SerializeObject(inventoryDocument).ToString(), Encoding.UTF8, General.JsonMediaType)).Result; //response.EnsureSuccessStatusCode(); }
public async Task <IActionResult> Post([FromBody] InventoryDocumentViewModel vm) { identityService.Token = Request.Headers["Authorization"].First().Replace("Bearer ", ""); identityService.Username = User.Claims.Single(p => p.Type.Equals("username")).Value; //InventoryDocument m = _mapper.Map<InventoryDocument>(vm); List <InventoryDocumentItem> items = new List <InventoryDocumentItem>(); foreach (var item in vm.items) { items.Add(new InventoryDocumentItem { ProductCode = item.productCode, ProductId = item.productId, ProductName = item.productName, ProductRemark = item.remark, Quantity = item.quantity, StockPlanning = item.stockPlanning, UomId = item.uomId, UomUnit = item.uom, }); } InventoryDocument m = new InventoryDocument { ReferenceNo = vm.referenceNo, ReferenceType = vm.referenceType, Remark = vm.remark, StorageCode = vm.storageCode, StorageId = Convert.ToInt32(vm.storageId), StorageName = vm.storageName, Date = vm.date, Type = vm.type, Items = items }; ValidateService validateService = (ValidateService)_facade.serviceProvider.GetService(typeof(ValidateService)); try { validateService.Validate(vm); //int clientTimeZoneOffset = int.Parse(Request.Headers["x-timezone-offset"].First()); int result = await _facade.Create(m, identityService.Username); Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.CREATED_STATUS_CODE, General.OK_MESSAGE) .Ok(); return(Created(String.Concat(Request.Path, "/", 0), Result)); } catch (ServiceValidationExeption e) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.BAD_REQUEST_STATUS_CODE, General.BAD_REQUEST_MESSAGE) .Fail(e); return(BadRequest(Result)); } catch (Exception e) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message) .Fail(); return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result)); } }