package com.csc.library.databean;

import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyLog;
import java.rmi.RemoteException;

import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.RecordCmp;

public class Totapprove_nstda extends RecordCmp {
			
	public Totapprove_nstda() {
		super("Totapprove_nstda", "Totapprove_nstda Table");
	}	

	protected void postInsertTrigger() throws Exception {
		super.postInsertTrigger();				
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")) {
			this.saveOTApprove();
		}		
	}

	protected void postUpdateTrigger() throws Exception {
		super.postUpdateTrigger();
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")) {
			this.saveOTApprove();
		} 
	}
	
	private void saveOTApprove() {
        CscCalendar cs = new CscCalendar();
        
		for (int i=1;i<=15;i++) {
			if (!this.getString("ot"+i+"date").trim().equals(cs.getDDMMYYYY()) && !this.getString("ot"+i+"timein").trim().equals("0.0") && !this.getString("ot"+i+"timeout").trim().equals("0.0")) {
				try {
                    this.param.put("__screen", "");
					DbInquiry inq = new InitialInquiry(this.userPro).getDbInquiry();
					inq.setParam(this.param);
					inq.setSchemaName(this.userPro.getSchemaName());		
					inq.initMyTable("Ttime_current1", "employeeid='"+this.getString("employeeid")+"' and dateid='"+this.getString("ot"+i+"date")+"'", "");
					inq.setColumn("*");
					inq.refresh();
					while (inq.next()) {
						DbRecord rec = inq.getCurrentDbRecord().cloneRecord(true);
						rec.set("DOCNO", this.getString("approveid"));
						rec.set("DOCTYPE", this.getString("OTA"));
						rec.set("M_TM_BG", this.getString("ot"+i+"timein"));
						rec.set("M_TM_EN", this.getString("ot"+i+"timeout"));
						rec.set("AC_OT", this.getString("ot"+i+"total"));
						rec.set("AP_OT", this.getString("ot"+i+"total"));
						rec.set("OT1", this.getString("ot"+i+"_1"));
						rec.set("OT5", this.getString("ot"+i+"_15"));
						rec.set("OT2", this.getString("ot"+i+"_2"));
						rec.set("OT3", this.getString("ot"+i+"_3"));
                        rec.set("REMARK", this.getString("result"+i));
                        rec.save();
					}
					
					inq.initMyTable("Ttime_current", "employeeid='"+this.getString("employeeid")+"' and dateid='"+this.getString("ot"+i+"date")+"'", "");
					inq.setColumn("*");
					inq.refresh();
					while (inq.next()) {
						DbRecord rec = inq.getCurrentDbRecord().cloneRecord(true);
						rec.set("OT", this.getString("ot"+i+"total"));
						rec.set("OT1", this.getString("ot"+i+"_1"));
						rec.set("OT5", this.getString("ot"+i+"_15"));
						rec.set("OT2", this.getString("ot"+i+"_2"));
						rec.set("OT3", this.getString("ot"+i+"_3"));
						rec.save();
					}
				} catch (RemoteException e) {
					MyLog.error(this, e);

					// TODO Auto-generated catch block
					
				}
			}
		}
	}
}