<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    BaseBean bb = new BaseBean();
    String begindate = "" ;
    String enddate = "" ;
    int year = 0 ;
    User user = null;
    String type = Util.null2String(request.getParameter("type"));
    if("0".equals(type)){
        begindate = Util.null2String(request.getParameter("begindate"));
        enddate =  Util.null2String(request.getParameter("enddate"));
        year = Util.getIntValue(request.getParameter("year"));
        int userid = Util.getIntValue(request.getParameter("userid"));
        user = new User(userid);
    }else{
        begindate = TimeUtil.getFirstDayOfTheYear();
        enddate = TimeUtil.getCurrentDateString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        year = calendar.get(Calendar.YEAR);
        user = HrmUserVarify.getUser(request , response) ;
    }

    int userid = user.getUID();
    Map<String,Object> dataMap = getWCSKDataNew(userid,begindate,enddate);
    JSONObject jsonObject = new JSONObject();
    jsonObject.putAll(dataMap);

%>
<%=jsonObject.toString()  %>

<%!
    /***
     *
     * @param userid
     * @param startdate
     * @param endate
     * @return
     */
    public Map<String,Object> getWCSKDataNew(int userid,String startdate,String endate)
    {
        BaseBean bb = new BaseBean();
        bb.writeLog("getWCSKDataNew");  // 核算公司 只留 泛微网络  项目经理
        RecordSet rs = new RecordSet();
        /******************完成收款****************/

        Map<String,Object> dataMap = new HashMap<String,Object>();
        double totalInAmt = 0.00 ;
        try{
            String userWhere = " and exists( select 1 from lf_prjhrminfo t3t where t3t.hrmid = cbi.projManager and (t3t.hrmid in("+userid+") or t3t.qyjl in("+userid+") or t3t.dqzj in("+userid+") or t3t.qyzj in("+userid+"))) " ;
            String where = getJHSKwhere(userid);
            String datewhere = "" ;
            if(StringUtils.isNotEmpty(startdate) && StringUtils.isNotEmpty(endate))
            {
                datewhere = " and cbiead.inDate >='"+startdate+"' and cbiead.inDate <='"+endate+"'";
            }
            //bb.writeLog("where:"+where);
            String sql =" select Convert(decimal(18,2),isnull((sum(isnull(w.inAmt,0))/10000.0),0)) as inAmt \n " +
                        " from( " +
                        " 	select cbiead.inAmt,cbi.projManager \n" +
                        " 	from proj_CardBaseInfoExec cbie \n" +
                        " 	inner join proj_CardBaseInfoExecDtl cbied on cbied.cbie_id = cbie.id \n" +
                        " 	inner join proj_CardBaseInfoExecAmtDtl cbiead on cbiead.cbied2_id = cbied.id \n" +
                        " 	inner join proj_CardBaseInfo cbi on cbie.cbi_id = cbi.id \n" +
                        " 	where 1=1 "  + userWhere + datewhere + where +
                        " ) w " ;
            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            if(rs.next()){
                totalInAmt = Util.getDoubleValue(Util.null2String(rs.getString("inAmt"),"0"),0.00);
            }
            dataMap.put("totalInAmt",totalInAmt);
            dataMap.put("sql",sql);
        }catch (Exception e){

        }

        return dataMap;
    }


    /***
     *
     * @param userid
     * @return
     */
    public String getJHSKwhere(int userid)
    {
        StringBuffer sqlWhere_t = new StringBuffer();

        try{
            sqlWhere_t.append(  " AND ( 1 = 2 "+
                    "  OR ( cbi.projManager IN ( SELECT  id "+
                    "							FROM  getallchilds("+userid+") ) "+
                    "	   OR cbi.projManager = "+userid+
                    "	 ) "+
                    "  OR ( EXISTS ( SELECT  1 "+
                    "				FROM    proj_CardBaseInfo_members cbim "+
                    "				WHERE   cbim.cbi_id = cbi.id "+
                    "						AND cbim.userId = "+userid+" ) ) "+
                    "  OR ( EXISTS ( SELECT  1 "+
                    "				FROM    CRM_CustomerInfo ci "+
                    "				WHERE   ci.id = cbi.projCustomer "+
                    "						AND ( ci.manager IN ( SELECT "+
                    "												  id "+
                    "											  FROM "+
                    "												  getallchilds("+userid+") ) "+
                    "							  OR ci.manager = "+userid+
                    "							) ) ) "+
                    "  OR ( EXISTS ( SELECT  1 "+
                    "				FROM    proj_default_share pds "+
                    "				JOIN    HrmResource hr ON pds.shared_deptids = hr.departmentid "+
                    "				WHERE   ( pds.share_pid IN ( SELECT "+
                    "												  id "+
                    "											 FROM getallchilds("+userid+") ) "+
                    "						  OR pds.share_pid = "+userid+
                    "						) "+
                    "						AND hr.id = cbi.projManager ) ) "+
                    " ) ") ;

        }catch (Exception e){

        }
        return sqlWhere_t.toString();
    }


    /***
     *
     * @param value
     * @return
     */
    public String formatBigDecimal(String value){
        String formatValue = "" ;
        try{
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            BigDecimal _decimal = new BigDecimal(value);
            formatValue =  decimalFormat.format(_decimal);
        }catch (Exception e){

        }
        return formatValue;
    }
%>



