DHIS2 custom form scripting example for Stock calculation

DHIS2 custom form scripting example for Stock calculation



                

Example of Custom form calculation:

<-script type="text/javascript"> dhis2.util.on( 'dhis2.de.event.formReady', function( event,iReVg2xgFPL ) { var periodN= $( '#selectedPeriodId' ).val();// From DHIS2 Selection Param var nextPeriod=(+periodN) + (+1);//201611,201612,201613 var nextPeriodSeperate=periodN.toString().substring(0, 4); var year=parseInt(nextPeriodSeperate.trim()); var month=parseInt(periodN % 100); if(month==12){ year++; month='01'; nextPeriod=year+'01'; //console.log("Next period: "+nextPeriod); } $('.selectedDate').val(nextPeriod); }); function validateStockOutNumber(stockoutDays){ var sOut=$(stockoutDays).val(); if(sOut>31){ $(stockoutDays).val(' '); $(stockoutDays).val(0); alert('Sorry the number of stockout days can not be greater than 31 '); } } function closingBL(op,re,adplus,adminus,con,closingid,opnextId,period){ // All id pick var opbalance= new Number(document.getElementById(op).value); var receipts= new Number(document.getElementById(re).value); var adjustPlus= new Number(document.getElementById(adplus).value); var adjustMinus= new Number(document.getElementById(adminus).value); var consump= new Number(document.getElementById(con).value); var nextPeriod= $(period).val(); // Closing Balance text data set var clobalance=opbalance+receipts+adjustPlus-adjustMinus-consump; $(closingid).val(clobalance);// Display closing balance at the text field $(opnextId).val(clobalance);// Display closing balance at the text field $(closingid).change(); $(opnextId).change(); $(closingid).prop("readonly",true); $(opnextId).prop("readonly",true); // Spliting Element ID var ArrayFromString = op.split("-"); var elementId=ArrayFromString[0]; //console.log('Element Id: '+elementId); //console.log('nextPeriod Id: '+nextPeriod); var valueSaver = new ValueSaver( elementId,nextPeriod, 'dCWAvZ8hcrs', clobalance, null, 'red' ); valueSaver.save(); } <-/script>