Page 1 of 1

Auto change or populate date based on duration value

Posted: 2020-04-05 01:28
by zibrahim
Hi everyone,
I hope that you all are safe and healthy during this challenging time.
I have created an application for plant growers for them to monitor their plant growth etc.
Image
User will select the plant and the estimated harvesting duration will automatically be populated in the harvest duration field.
My goal is whenever user change or select the "Plant", the "Estimated harvest date" will be automatically populated based on the changes of the "Harvest duration" value (days).
You can check the application at
https://mythinkpositive.com/samples/hydroponic
with Username and Password : demo

Your kind attention and help is highly appreciated.
Thank you and have a nice day.

Zala.

Re: Auto change or populate date based on duration value

Posted: 2020-04-05 07:16
by onoehring
Hi Zala,

hope you and your loved ones are well too.
I suppose you have implemented some JS already to automatically update the Harvest Duration on change of the Plant type (or jsetzes AGHelper).
So, since fiddling with JS (which is not my area usually, but I want to make a suggestion anyways), why not grab the value from the start date field (start_date?) and simply add the Harvest Duration to that?

Probably some workflow like this
On change of Harvest Duration (which equals change of product in your application)
a) Get new value from harvest duration something like

Code: Select all

document.querySelector("#harvest_duration").value
b) write this to variable maybe harvestduration
c) get day from start date

Code: Select all

document.querySelector("#start_date-dd").value
d) get month from start date

Code: Select all

document.querySelector("#start_date-mm").value
e) get year from start date

Code: Select all

document.querySelector("#start_date").value
f) build date from these three
g) use moments.js which is already loaded to calculate new date
h) split values
i) write new est harvest date day, month, year

Again: JS is not my primary (nor secondary) world, so maybe someone with more knowledge in this area is able to do a JS-oneliner.

Maybe see the AppGini Helper from jsetzer, who also has a page how to react on lookup changes (which I did not find right now).

Olaf

Re: Auto change or populate date based on duration value

Posted: 2020-04-05 11:41
by pbottcher
Hi,

you can try to add to your Pots-dv.js

Code: Select all

	function put_date(date_field, dt){
		$j('#' + date_field).val(moment(dt).format("Y"));
		$j('#' + date_field + '-mm').val(moment(dt).format("M"));
		$j('#' + date_field + '-dd').val(moment(dt).format("D"));
		return ;
	}

and change the

/* on changing plant, retrieve harvest duration from plants table and populate harvest duration field */

Code: Select all

		$j('#plant-container').on('change', function(){
		if(pid == '{empty_value}'){
			$j('#harvest_duration').val('');
		}else{
			$j.ajax({
				url: 'test.php',
				data: { pid: 3 },
				success: function(data){
					$j('#harvest_duration').val(data);
					var dt=get_date('start_date');
					var nw =moment(dt).add(data,'d');
					put_date('est_harvest_date', nw);
					}
			
			});
		}
		});

Re: Auto change or populate date based on duration value

Posted: 2020-04-05 14:03
by zibrahim
Hi pböttcher,
Thank you so much for the help. You are brilliant!!! I have changed a bit to become like the following and it is working perfectly.

Code: Select all

	$j('#plant-container').on('change', function(){
		var pid = $j('#plant-container').select2('val');
		if(pid == '{empty_value}'){
			$j('#harvest_duration').val('');
		}else{
			$j.ajax({
				url: 'hooks/ajax-harvest-duration.php',
				data: { pid: pid },
				success: function(data){
					$j('#harvest_duration').val(data);
					var dt=get_date('start_date');
					var nw =moment(dt).add(data,'d');
					put_date('est_harvest_date', nw);
				}
			
			});
		}
	});
I am not good at coding and I hope you don't mind helping me a bit more.
I am also trying to automatically change the est_harvest_date whenever user change the start_date or the harvest_duration as well.
Your kind attention and help is highly appreciated.
Thanking you in advance.

Zala.

Re: Auto change or populate date based on duration value

Posted: 2020-04-05 19:59
by pbottcher
Hi Zala,

well spotted, Sorry I left my test in there while pasting the code.

You may try

Code: Select all

	$j('#harvest_duration, [id^="start_date"]').on('change', function(e){
		var data = parseInt($j('#harvest_duration').val()) || 0;
		var dt=get_date('start_date');
		var nw =moment(dt).add(data,'d');
		put_date('est_harvest_date', nw);
	});
	
	

Re: Auto change or populate date based on duration value

Posted: 2020-04-06 01:14
by zibrahim
Thank you so much pböttcher. It is working as intended. You made my day wonderful during this hard time.
Also not forgetting Olaf for showing the path.
Thanks guys.

Re: Auto change or populate date based on duration value

Posted: 2020-07-29 00:55
by zibrahim
Hi Pböttcher,
I hope you well and safe always.
Need help from you related to this issue.
The solution you provided works as expected for the 'start_date' field as data type "Date".
It didn't work if I change the 'start_date' field's data type to "Datetime".
I am assuming that it has something to do with the get_date function.
Appreciate if you can help me with the solution.
Thanking you in advance.

Zala.

Re: Auto change or populate date based on duration value

Posted: 2020-07-29 06:11
by onoehring
Hi,

why did you change the field type? Is there a reason when we are talking about harvest dates?
If nothing else helps (pbötcher is our man here :-) ) you could cut the time off the field contents and then do the calculation - and add the time back to it afterwards.

Olaf

Re: Auto change or populate date based on duration value

Posted: 2020-07-29 07:50
by pbottcher
Hi,

yes of course. If you change the fieldtype, the script need to be adjusted as well.

you can try ( assuming the your datetime field display as dd-mm-yyyy hh:mm:ss )

Code: Select all

	function get_date(date_field){
		var arr = $j('#' + date_field).val().split(/ |-/);
		var y = arr[2];
		var m = arr[1];
		var d = arr[0];
		var date_object = new Date(y, m - 1, d);
		if(!y) return false;
		return date_object;
	}

Re: Auto change or populate date based on duration value

Posted: 2020-07-29 08:19
by zibrahim
Hi Pböttcher,
You never fail me... Thank you so much!!!

and Olaf, the reason is I need to specify the harvesting time as well due to some time sensitive plants and logistic issue. Hope this explained.

Thanks my friends.

Zala.