diff --git a/FarmmapsApi/Services/GeneralService.cs b/FarmmapsApi/Services/GeneralService.cs index 6b452a0..ce0a792 100644 --- a/FarmmapsApi/Services/GeneralService.cs +++ b/FarmmapsApi/Services/GeneralService.cs @@ -22,14 +22,20 @@ namespace FarmmapsApi.Services } public async Task CreateCropfieldItemAsync(string parentItemCode, string name, int year, - string fieldGeomJson, string data = "{}") { - var currentYear = new DateTime(year, 1, 1); + string fieldGeomJson, string data = "{}", DateTime startDate = new DateTime(), DateTime endDate = new DateTime() ) + { + //If user provides no startDate or endDate, then set startDate and endDate to full year provided through 'year' + if (startDate == new DateTime() || endDate == new DateTime()) + { + startDate = new DateTime(year, 1, 1); + endDate = new DateTime(year, 12, 31); + } var cropfieldItemRequest = new ItemRequest() { ParentCode = parentItemCode, ItemType = CROPFIELD_ITEMTYPE, Name = name, - DataDate = currentYear, - DataEndDate = currentYear.AddYears(1).AddDays(-1), + DataDate = startDate, + DataEndDate = endDate, Data = JObject.Parse(data), Geometry = JObject.Parse(fieldGeomJson) }; @@ -38,10 +44,14 @@ namespace FarmmapsApi.Services } public async Task CreateOperationItemAsync(string cropRecordingItemCode, int year, - string fieldGeomJson, string data = "{}") + string fieldGeomJson, string data = "{}", DateTime startDate = new DateTime(), DateTime endDate = new DateTime()) { - // not sure if here we also need to specify DataDate, DataEndDate & Geometry. Do it just in case - var currentYear = new DateTime(year, 1, 1); + //If user provides no startDate or endDate, then set startDate and endDate to full year provided through 'year' + if (startDate == new DateTime() || endDate == new DateTime()) + { + startDate = new DateTime(year, 1, 1); + endDate = new DateTime(year, 12, 31); + } JObject jdata = JObject.Parse(data); string name = string.Format($"CrpRec Operation, {jdata.GetValue("name")}"); @@ -50,8 +60,8 @@ namespace FarmmapsApi.Services ParentCode = cropRecordingItemCode, ItemType = CROPOP_ITEMTYPE, Name = name, - DataDate = currentYear, - DataEndDate = currentYear.AddYears(1).AddDays(-1), + DataDate = startDate, + DataEndDate = endDate, Data = jdata, Geometry = JObject.Parse(fieldGeomJson) }; @@ -59,18 +69,23 @@ namespace FarmmapsApi.Services return await _farmmapsApiService.CreateItemAsync(operationItemRequest); } public async Task CreateCropfieldCharacteristicItemAsync(string cropfieldItemCode, int year, - string fieldGeomJson, string data = "{}") + string fieldGeomJson, string data = "{}", DateTime startDate = new DateTime(), DateTime endDate = new DateTime()) { + //If user provides no startDate or endDate, then set startDate and endDate to full year provided through 'year' + if (startDate == new DateTime() || endDate == new DateTime()) + { + startDate = new DateTime(year, 1, 1); + endDate = new DateTime(year, 12, 31); + } // not sure if here we also need to specify DataDate, DataEndDate & Geometry. Do it just in case string name = "Cropfield characteristic"; - var currentYear = new DateTime(year, 1, 1); ItemRequest cropfieldCharactericsticItemRequest = new ItemRequest() { ParentCode = cropfieldItemCode, ItemType = CROPCHAR_ITEMTYPE, Name = name, - DataDate = currentYear, - DataEndDate = currentYear.AddYears(1).AddDays(-1), + DataDate = startDate, + DataEndDate = endDate, Data = JObject.Parse(data), Geometry = JObject.Parse(fieldGeomJson) }; diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index a64f442..1fa53f6 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -42,11 +42,12 @@ namespace FarmmapsKPI KPIInput input; string fnKPIinput; - //Console.WriteLine("Type name of input json file. Example: KPIinput.json (in same directory as FarmmapsKPI.exe) or C:/temp/KPIinputChemieTmp.json"); - //fnKPIinput = Console.ReadLine(); - //if (string.IsNullOrEmpty(fnKPIinput)) - - fnKPIinput = "KPIinput.json"; + Console.WriteLine("Type name of input json file. Example: KPIinput.json (in same directory as FarmmapsKPI.exe) or C:/temp/KPIinputChemieTmp.json"); + fnKPIinput = Console.ReadLine(); + if (string.IsNullOrEmpty(fnKPIinput)) + { + fnKPIinput = "KPIinput.json"; + } var fieldsInputJson = File.ReadAllText(fnKPIinput); @@ -153,7 +154,7 @@ namespace FarmmapsKPI { _logger.LogInformation($"Creating cropfield with name '{fieldName}'"); cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDriveRoot.Code, - $"{fieldName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfield.ToString(Formatting.None)); + $"{fieldName}", cropYear, input.GeometryJson.ToString(Formatting.None), input.DataCropfield.ToString(Formatting.None), input.StartDate, input.EndDate); _settings.CropfieldItemCode = cropfieldItem.Code; SaveSettings(settingsfile); } diff --git a/FarmmapsKPI/Models/KPIInput.cs b/FarmmapsKPI/Models/KPIInput.cs index 2fef5d9..8d2df12 100644 --- a/FarmmapsKPI/Models/KPIInput.cs +++ b/FarmmapsKPI/Models/KPIInput.cs @@ -7,6 +7,7 @@ namespace FarmmapsKPI.Models { public bool UseExistingCropfieldWithChildren { get; set; } public bool DeleteNewlyCreatedAfterCalc { get; set; } + public string fieldName { get; set; } public string CropfieldItemCode { get; set; } public JObject DataCropfield { get; set; } public string CropRecordingItemCode { get; set; } @@ -17,8 +18,9 @@ namespace FarmmapsKPI.Models public JObject DataCropfieldCharacteristic { get; set; } public string DownloadFolder { get; set; } public int CropYear { get; set; } + public DateTime StartDate { get; set; } + public DateTime EndDate { get; set; } public JObject GeometryJson { get; set; } - public string fieldName { get; set; } } } \ No newline at end of file