Auto change or populate date based on duration value

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Auto change or populate date based on duration value

Post by zibrahim » 2020-04-05 01:28

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Auto change or populate date based on duration value

Post by onoehring » 2020-04-05 07:16

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Auto change or populate date based on duration value

Post by pbottcher » 2020-04-05 11:41

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);
					}
			
			});
		}
		});
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Auto change or populate date based on duration value

Post by zibrahim » 2020-04-05 14:03

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Auto change or populate date based on duration value

Post by pbottcher » 2020-04-05 19:59

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);
	});
	
	
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Auto change or populate date based on duration value

Post by zibrahim » 2020-04-06 01:14

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Auto change or populate date based on duration value

Post by zibrahim » 2020-07-29 00:55

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Auto change or populate date based on duration value

Post by onoehring » 2020-07-29 06:11

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Auto change or populate date based on duration value

Post by pbottcher » 2020-07-29 07:50

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;
	}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Auto change or populate date based on duration value

Post by zibrahim » 2020-07-29 08:19

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

Post Reply