package p2.application.tmaxbillnum;

import baseclass.PaoCust;
import p2.pao.PaoSet;
import p2.util.P2Exception;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class tmaxbillnum extends PaoCust {

    public tmaxbillnum(PaoSet arg0) {
        super(arg0);
    }

    public String getBillNum(boolean withsiteid) throws P2Exception {
        // 是否添加时间
        boolean addtime = getBoolean("FADDTIME");
        String time = "";
        if (addtime) {
            // yyyyMMdd
            String ftimetype = getString("FTIMETYPE");
            time = new SimpleDateFormat(ftimetype, Locale.CHINESE)
                    .format(getDateTime());
            int foldnowtime = getInt("fnowtime");
            if (foldnowtime != Integer.parseInt(time)) {
                setValue("fnowtime", Integer.parseInt(time));
                setValue("FSERIALNUM", 1);
            }
        }
        // 当前序号
        int serialnum = getInt("FSERIALNUM");
        String s = String.valueOf(serialnum);
        String recnum = s;
        // 长度
        int flength = getInt("flength");
        if (flength - s.length() - time.length() > 0) {
            recnum = String.format("%0" + (flength - time.length()) + "d",
                    new Object[]{Integer.valueOf(serialnum)});
        }
        setValue("FSERIALNUM", serialnum + 1);
        String siteid = getPaoSet("site").getPao(0).getString("prefix");
        String ffixed = getString("ffixed");
        if (!"".equals(siteid) && withsiteid) {
            return siteid + "-" + ffixed + time + recnum;
        } else {
            return ffixed + time + recnum;
        }
    }

}
