package com.cwbuyer.lib;

import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Calendar;

import com.cwbuyer.format.IDateDialog;
import com.cwbuyer.format.IDialog;
import com.cwbuyer.format.PItemData;
import com.cwbuyer.main.APos.PItemKind;
import com.cwbuyer.main.APos.PItemMode;
import com.pwbuyer.main.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("InflateParams")
public class QPosSave extends Activity{

	public interface PsaveWhere{  //未來擴充 廠商進貨單維護,門市出貨單 櫃員自購單 專櫃出貨單
		public int Qposf = 0;  //銷貨總表單
		public int  Apos= 1;  //會員主檔
		public int  Acust= 2;  //會員檔
		public int DataManage=3;
		public int Repeat = 5;  //銷貨總表單

	}

	private interface isLTB{  //結算的對象   %%%需參數設定化%%%
		public int  last= 0;  //只結算前期未結
		public int  tot= 1;  //全部結算(預設)
		public int  bsum= 2;  //只結算本次交易

	}

	final String myImportPath =android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+Utilis.getIni(this, "SYS", "YEAR",7);
	private int mMode=PItemMode.ADD;//預設新增
	private int mWhere=PsaveWhere.Apos;//預設來自銷貨主檔
	private int nKind=PItemKind.TR30;		//預設為銷貨單別
	private String mKind=null;
	private String mFno=null;
	private String[] compStr =Utilis.getIni(this,"USER","COMPANY",9).split("!&!");
	private String[] devStr =Utilis.getIni(this,"SYS","DEV",9).split("!&!");
	private String[] barStr =Utilis.getIni(this,"SYS","BAR_IP",9).split("!&!");
	private String[] deptStr =Utilis.getIni(this,"SYS","DEPT",9).split("!&!");
	private String mBankn=deptStr[0]+deptStr[1];//門市代號+機號
	private int PSWD=Utilis.toInt(deptStr[4]);
	private int mDealer=Utilis.toInt(Utilis.getIni(this,"SYS","BACKOUT",1));
	private int LTB=isLTB.bsum; //結帳的對象
	private int isChf=0;  //isconfirm="N"(0) "Y"(1)
	private int isChk=0;  //ischeck="N"(0) "Y"(1)
	//	private int nPart=0;
//	private int nGroup=0;
	private int modify=0;
	private int Loca=0;
	private String sepa="\r\n";
	private int endLeng=0;
	private int Page=1;

	private StringBuffer sPrintB = new StringBuffer();
	private String [] menu_S={"印存根聯","印收執聯","印存根聯+收執聯","印存根聯+收執聯+貨運名條","印貨運名條","不印","輸出","","",""};
	private String TitleName="";
	private double owlLast=0;
	private double owlTot=0;

	private int mYear = 0;
	private int mMonth = 0;
	private int mDay = 0;

	private String mMobil="";
	private String mAddress="";

	int [] locas=new int [18];  //for printer 80 &100
	String underLine="---------------------------------------";

	PItemData data = new PItemData(); //##格式化需考量內容增刪

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		nKind = getIntent().getIntExtra("pos_qkind", PItemKind.TR30); //單別 這是一個重要的關鍵值
		mKind=""+nKind;
		if (nKind==10){
			setContentView(R.layout.posmain10);
			TitleName="廠商進貨單";
			if (PSWD==1) ((LinearLayout)findViewById(R.id.line_now)).setVisibility(View.GONE);
		}else if (nKind==20 || nKind==21){
			setContentView(R.layout.posmain20);
			if (nKind==20) TitleName="總倉出貨單"; else TitleName="門市轉貨單";
			if (PSWD==1) ((LinearLayout)findViewById(R.id.line_now)).setVisibility(View.GONE);
		}else if (nKind==30 || nKind==31){
			setContentView(R.layout.posmain);
			TitleName="銷貨單";
		}else{
			setContentView(R.layout.posmain);
			TitleName="員工自購單";
		}

		((ListView)findViewById(R.id.list_pitem)).setVisibility(View.GONE);
		((LinearLayout)findViewById(R.id.layout_btn)).setVisibility(View.GONE);
		((LinearLayout)findViewById(R.id.layout_top)).setVisibility(View.GONE);

		((LinearLayout)findViewById(R.id.line_saves)).setVisibility(View.VISIBLE);
		//line_ord
		if (nKind!=30 && nKind!=31)
			((LinearLayout)findViewById(R.id.line_ord)).setVisibility(View.INVISIBLE);

		mMode = getIntent().getIntExtra("pos_mode", PItemMode.ADD);  //預設新增

		mWhere =getIntent().getIntExtra("pos_where",PsaveWhere.Apos); //來自何方軟件
		mFno = getIntent().getStringExtra("pos_formno"); //單號 這是一個最重要的關鍵值
//    	nPart = getIntent().getIntExtra("pos_npart",0); //0總公司 1門市
//    	nGroup= getIntent().getIntExtra("pos_group",0); //0銷貨1預購2暫存3.留貨
		//if(nGroup==GroupMode.NUMBER)   ;
		//取得該單頭的DATA
		data=DBUtil.PItem.getData(QPosSave.this, mFno, mKind,true);

		//O預購 R留貨
		if (data.mIsconfirm.equalsIgnoreCase("Y")) {
			isChf=1;
			((Button)findViewById(R.id.now_save)).setText("重新確認結帳");
		}
		if (data.mIscheck.equalsIgnoreCase("Y")){
			isChk=1;
			((Button)findViewById(R.id.owl_save)).setText("重新預購結帳");
		}
		devStr[0]=Utilis.getIni(this,"SYS","DEV",1);
		devStr[1]=Utilis.getIni(this,"SYS","DEV",2);
		devStr[2]=Utilis.getIni(this,"SYS","DEV",3);
		devStr[3]=Utilis.getIni(this,"SYS","DEV",4);
		devStr[4]=Utilis.getIni(this,"SYS","DEV",5);
		devStr[5]=Utilis.getIni(this,"SYS","DEV",6);
		devStr[6]=Utilis.getIni(this,"SYS","DEV",7);
		devStr[7]=Utilis.getIni(this,"SYS","DEV",8);


		((Button)findViewById(R.id.btn_fno)).setText(mFno);
		((Button)findViewById(R.id.btn_date)).setText(data.mFormdate);
		((Button)findViewById(R.id.btn_emp)).setText(data.mEmpid+"/"+data.mEmpname);
		((Button)findViewById(R.id.btn_custp)).setText(data.mUser+"/"+data.mUsername);
		((Button)findViewById(R.id.btn_dept)).setText(data.mDeptno);
		((Button)findViewById(R.id.btn_receive)).setText("$" + new BigDecimal(data.dAsum).toString());
		

		((Button)findViewById(R.id.btn_inc)).setText("");
		((Button)findViewById(R.id.btn_mic)).setText("");

		String[] arrayW = data.mHandno.split("-");
		if (arrayW.length>0) ((Button)findViewById(R.id.btn_inc)).setText(arrayW[0]);
		if (arrayW.length>1) ((Button)findViewById(R.id.btn_mic)).setText(arrayW[1]);

		((Button)findViewById(R.id.owl_bsum)).setText(new BigDecimal(data.dBsum).toString());

		if ((int)data.dDismoney!=0) ((EditText)findViewById(R.id.dismoney)).setText(new BigDecimal(data.dDismoney).toString());
		if ((int)data.dOrdcash!=0) ((EditText)findViewById(R.id.ord_cash)).setText(new BigDecimal(data.dOrdcash).toString());
		if ((int)data.dOrdcard!=0) ((EditText)findViewById(R.id.ord_card)).setText(new BigDecimal(data.dOrdcard).toString());
		if ((int)data.dCash!=0)		((EditText)findViewById(R.id.now_cash)).setText(new BigDecimal(data.dCash).toString());
		if ((int)data.dCard!=0)		((EditText)findViewById(R.id.now_card)).setText(new BigDecimal(data.dCard).toString());
		if ((int)data.dAtm!=0)		((EditText)findViewById(R.id.now_atm)).setText(new BigDecimal(data.dAtm).toString());
		if ((int)data.dTrcash!=0)		((EditText)findViewById(R.id.now_trcash)).setText(new BigDecimal(data.dTrcash).toString());
		//屬新增單據,應交給USER因恰當操作,才給值
		((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
		((Button)findViewById(R.id.now_date)).setText(data.mFinedate);

//		if (mMode !=2) { //不是查詢,才可以進一步觸控
		//前帳//餘額//本次應收的全額收款選擇
		((Button)findViewById(R.id.owl_ilast)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_itot)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_ibsum)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_last)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_tot)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_bsum)).setOnClickListener(new MainClick());

		if(PSWD==1 && (nKind==10 ||nKind==20 || nKind==21 )) //進貨權限不符
		{
			((Button)findViewById(R.id.owl_last)).setVisibility(View.INVISIBLE);
			((Button)findViewById(R.id.owl_tot)).setVisibility(View.INVISIBLE);
			((Button)findViewById(R.id.owl_bsum)).setVisibility(View.INVISIBLE);
		}
		//尾數折讓 賒帳
		((Button)findViewById(R.id.idismoney)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.dismoney)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.owl_save)).setOnClickListener(new MainClick());

		((Button)findViewById(R.id.now_icash)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.now_cash)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_iatm)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.now_atm)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_itrcash)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.now_trcash)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_icard)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.now_card)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_save)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_exit)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_izero)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.now_date)).setOnClickListener(new MainClick());

		((Button)findViewById(R.id.ord_icash)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.ord_cash)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.ord_icard)).setOnClickListener(new MainClick());
		((EditText)findViewById(R.id.ord_card)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.ord_exit)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.ord_izero)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.ord_date)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.ord_save)).setOnClickListener(new MainClick());

		((Button)findViewById(R.id.idismoney)).setOnLongClickListener(new MainLongClick());
		((Button)findViewById(R.id.ord_icash)).setOnLongClickListener(new MainLongClick());
		((Button)findViewById(R.id.ord_icard)).setOnLongClickListener(new MainLongClick());
		((Button)findViewById(R.id.now_save)).setOnLongClickListener(new MainLongClick());

		((EditText)findViewById(R.id.dismoney)).setSelected(true);
//		}

//    	((EditText) findViewById(R.id.textViewg8)).addTextChangedListener(watcherPrice);


		try{
			SQLiteDatabase db = Utilis.getDB(QPosSave.this);
			StringBuffer mBuf = new StringBuffer();
			mBuf.append("select sum(BSUM) as BSUMS,")
					.append("sum(CASH) as CASHS,sum(CARD) as CARDS,")
					.append("sum(ATM) as ATMS,sum(TRCASH) as TRCASHS,")
					.append("sum(ORDCASH) as ORDCASHS,sum(ORDCARD) as ORDCARDS")
					.append(" from " + TbName.QHEAD);

			int ORS=Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","DEPT_SEND", 6));
			if (ORS!=2) //含預購帳
				mBuf.append(" where (ISCONFIRM='Y' or (ISCHECK='Y' and ISCONFIRM='N'))");
			else  //不含預購結帳
				mBuf.append(" where ISCONFIRM='Y'");

			//ISCHECK='Y'  涵蓋 ISCONFIRM=N預購(不計O預購,W網購,R留貨)

			mBuf.append(" and QKIND='"+mKind+"'");
			mBuf.append(" and USER='"+data.mUser+"'");
			//一律排除本單合計 就是前帳
			mBuf.append(" and FORMNO not in ('"+mFno+"')");

			Cursor cursor = db.rawQuery(mBuf.toString(), null);
			if (cursor.getCount()>0) {
				cursor.moveToFirst();
				//一律算出前帳
				owlLast=cursor.getDouble(cursor.getColumnIndex("CASHS"))+
						cursor.getDouble(cursor.getColumnIndex("ATMS"))+
						cursor.getDouble(cursor.getColumnIndex("CARDS"))+
						cursor.getDouble(cursor.getColumnIndex("ORDCASHS"))+
						cursor.getDouble(cursor.getColumnIndex("ORDCARDS"))
						-cursor.getDouble(cursor.getColumnIndex("BSUMS"));
			} else  //表示資料庫尚未有任何資料,不可能存在,但防萬一
				owlLast=0;

			owlTot=owlLast+data.dBsum-data.dCash-data.dAtm-data.dCard-data.dOrdcash-data.dOrdcard;

			//廠商 10  非廠商　取得電話地址
			String SQLC="select MOBIL,ADDRESS from qcust where CUSTNO='"+data.mUser+"'";
			if (mKind.equalsIgnoreCase("10")) SQLC="select MOBIL,ADDRESS from qfact where FACTNO='"+data.mUser+"'";
			cursor=db.rawQuery(SQLC, null);
			if (cursor.getCount()>0){
				cursor.moveToFirst();
				mMobil=cursor.getString(0);
				mAddress=cursor.getString(1);
			}

			db.close();
			cursor.close();

		} catch (SQLException e) {

		}

		((Button)findViewById(R.id.owl_last)).setText(new BigDecimal(owlLast).toString());
		((Button)findViewById(R.id.owl_tot)).setText(new BigDecimal(owlTot).toString());

		recountOwl( mMode);

		((EditText)findViewById(R.id.dismoney)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.now_cash)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.now_trcash)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.now_card)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.now_atm)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.ord_cash)).addTextChangedListener(watcherPrice);
		((EditText)findViewById(R.id.ord_card)).addTextChangedListener(watcherPrice);

		if (mWhere==PsaveWhere.Repeat) PrintSel();
	}

	class MainLongClick implements OnLongClickListener { //所有的觸發事件,在此集中管理
		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			Utilis.runVibrate(QPosSave.this);
			switch (id) {
				case R.id.idismoney:  //檢查四捨五入到百位的規則,手動預設為無條件捨去法,自動切換
				{
					if (data.dAsum != data.dBsum) {
						data.dDismoney=0;
						((EditText)findViewById(R.id.dismoney)).setText("");
					} else {
						data.dDismoney=(data.dAsum % 100);
						((EditText)findViewById(R.id.dismoney)).setText(new BigDecimal(data.dDismoney).toString());
					}
					clearNow();
					return true;
				}

				case R.id.ord_icash:
				{
					data.dOrdcash=data.dOrdcash+500;
					((EditText)findViewById(R.id.ord_cash)).setText(new BigDecimal(data.dOrdcash).toString());
					if (data.mOrddate.length()<=0){
						data.mOrddate=data.mFormdate;
						((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
					}
					clearNow();
					return true;
				}

				case R.id.ord_icard:
				{
					data.dOrdcard=data.dOrdcard+500;
					((EditText)findViewById(R.id.ord_card)).setText(new BigDecimal(data.dOrdcard).toString());
					if (data.mOrddate.length()<=0){
						data.mOrddate=data.mFormdate;
						((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
					}
					clearNow();
					return true;
				}
				case R.id.now_save:
				{
					if (!data.mFno.equalsIgnoreCase(mBankn+"5"+data.mUser) && !data.mFno.equalsIgnoreCase(mBankn+"6"+data.mUser)){
						//對話框 自動折讓,尾數折讓,返回
						//加權平均自動折讓,規則及流程
						//1.取得最低折數 2.算出qitems的原始小計  3.取得預購現金+預購票刷 +取得現金,票刷(為實收)
						//4.用 實收 / 原始金額 算出平均折數 @@@5.原始金額-平均折數 存入 尾數折讓@@@不可以,運算會大亂
						//6.檢查平均折數是否>=最低折數 否則警示低於最低折數 不予結帳
						//7.以迴圈一一將qitems的discount存入平均折數,並求出整數的小計,並累計扣減(實收)
						//8.最後一筆qitems 如果實收扣減後有差額,交給最後一筆小計,使湊成QITEMS的SUBPRICE 會等於QHEAD實收
						DialogUtilis.showDialog(QPosSave.this, "請選擇折讓方式[尾數折讓][加權平均折讓]", -1,
								new String[] { "加權平均折讓","尾數折讓", "返回" }, new IDialog() {
									@Override
									public void onDialogFinish(int pos, String choice) {
										switch (pos) {
											case 2: {break;}
											case 0: // 加權平均折讓(預設方式)
											{

												int lowDisc=Utilis.toInt(Utilis.getIni(QPosSave.this,"SYS","DEPT",4)); //0零售1批發>1最低折數
												if (lowDisc<2) lowDisc=10;  //沒有最低折數的限制
												SQLiteDatabase db = Utilis.getDB(QPosSave.this);
												StringBuffer mBuf = new StringBuffer();
												mBuf.append("select GOODSNOS,P0,P1,P2,P3,P4,P5,UNIT"+mKind.substring(0, 1)+"0 from qitems");
												mBuf.append(" where FORMNO='"+mFno+"'");
												Cursor csI=db.rawQuery(mBuf.toString(), null);
												double sumI=0; //=正確的原價X正確的數量
												double disI=0; //平均折數
												if (csI.getCount()>0) {
													getFormdate();
													csI.moveToFirst();
													for (int i=0;i<csI.getCount();i++){
														sumI=sumI+csI.getDouble(csI.getColumnIndex("P"+data.nTrade))*csI.getDouble(csI.getColumnIndex("UNIT"+mKind.substring(0, 1)+"0"));
														csI.moveToNext();
													}
													data.dBsum=data.dCash+data.dAtm+data.dCard+data.dOrdcash+data.dOrdcard-data.dTrcash;
													data.dAsum=data.dBsum;
													//Toast.makeText(QPosSave.this, "原始小計=="+sumI+"before_BSUM="+data.dBsum, Toast.LENGTH_SHORT).show();
													if (sumI==0 && data.dBsum==0){  //表示有交易,但金額為0,等同賒帳
														String[] args = {mFno};
														ContentValues value = new ContentValues();
														value.put("DEPTNO",data.mDeptno);
														value.put("BSUM", 0);
														value.put("ASUM", 0);
														value.put("DISMONEY", 0);
														data.mIsconfirm="Y";	value.put("ISCONFIRM", "Y");
														data.mIscheck="N";	value.put("ISCHECK", "N");
														value.put("FINEDATE", data.mFinedate);
														if (data.mTR !=null &&  data.mTR.equalsIgnoreCase("Y0")) {
															value.put("TR","Y1");
															value.put("ID","1006");
														} else {value.put("TR",mKind);value.put("ID",mKind);}
														value.put("BANKNAME",mBankn);
														value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
														int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
														if (success!=0) {
															maintainQItems();
															//createdatetime也要存入現在的時間
															//@@cloud_update
														}
														modify=1;
														Intent intentS = new Intent();
														intentS.putExtra("QPosSave_modify",modify);
														QPosSave.this.setResult(RESULT_OK,intentS);
														//錢櫃
														if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
														//印製收據
														if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
													}else if (sumI>0 && data.dBsum>0 ){ //原始小計
														if ((data.dBsum*100/sumI)>=lowDisc) {
															disI=(data.dBsum*10000/sumI)/100;  //取到小數第2位
															//Toast.makeText(QPosSave.this, "最新折數=="+disI, Toast.LENGTH_SHORT).show();
															db.execSQL("update "+TbName.QITEMS+" set DISCOUNT= '"+disI+"',SUBPRICE= round( P"+data.nTrade+" * UNIT"+mKind.substring(0, 1)+"0 * "+disI+"/100 ),UNITPRICE="+"P"+data.nTrade+",PS=round("+disI+")  where FORMNO='"+mFno+"'");

															//回歸結帳的正常程序,但不用演算
															String[] args = {mFno};
															ContentValues value = new ContentValues();
															value.put("DEPTNO",data.mDeptno);
															value.put("BSUM", data.dBsum);
															value.put("ASUM", data.dAsum);
															value.put("DISMONEY", 0);

															//現金部分
															if ((data.dCash !=0 || data.dCard !=0)) {
																value.put("CASH", data.dCash);
																value.put("CARD", data.dCard);
																value.put("FINEDATE", data.mFinedate);
															} else {
																value.put("CASH", 0);
																value.put("CARD", 0);
																value.put("FINEDATE", "");
																if ((data.dOrdcash !=0 || data.dOrdcard !=0) && data.mOrddate.length()>0) {
																	value.put("ORDCASH", data.dOrdcash);
																	value.put("ORDCARD", data.dOrdcard);
																	value.put("ORDDATE", data.mOrddate);
																} else {
																	value.put("ORDCASH", 0);
																	value.put("ORDCARD", 0);
																	value.put("ORDDATE", "");
																}
															}
															data.mIsconfirm="Y";	value.put("ISCONFIRM", "Y");
															data.mIscheck="N";	value.put("ISCHECK", "N");
															value.put("ATM",data.dAtm);
															value.put("TRCASH",data.dTrcash);
															value.put("BANKNAME",mBankn);
															if (data.mTR !=null && data.mTR.equalsIgnoreCase("Y0")) {
																value.put("TR","Y1");
																value.put("ID","1006");
															} else {value.put("TR",mKind);value.put("ID",mKind);}
															value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
															int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
															if (success!=0) {  //可以直接返回 再考慮回傳值 或可以進入詢問列印帳單 或 詢問傳送資訊
																maintainQItems();
																//createdatetime也要存入現在的時間
																//@@cloud_update

																if (isChf==1){
																	Toast.makeText(QPosSave.this, mFno+"已經成功更新!!", Toast.LENGTH_SHORT).show();
																}else {
																	Toast.makeText(QPosSave.this, mFno+"已經成功結帳!!", Toast.LENGTH_SHORT).show();
																}

																modify=1;
																Intent intentS = new Intent();
																intentS.putExtra("QPosSave_modify",modify);
																QPosSave.this.setResult(RESULT_OK,intentS);

																if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
																if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
															}


														}else { //結帳低於最低折數,提出示警
															Toast.makeText(QPosSave.this, "請改以手動結帳!!", Toast.LENGTH_SHORT).show();
														}
													}else {//退貨不得以加權平均折讓結帳,提出示警
														Toast.makeText(QPosSave.this, "請改以手動結帳!!", Toast.LENGTH_SHORT).show();
													}
												}
												db.close();
												csI.close();
												break;
											}
											case 1:{  //尾數折讓
												SQLiteDatabase db = Utilis.getDB(QPosSave.this);
												String[] args = {mFno};
												ContentValues value = new ContentValues();

												double DD=data.dCash+data.dAtm+data.dCard+data.dOrdcash+data.dOrdcard-data.dTrcash;
												data.dBsum=DD;
												data.dDismoney=(data.dAsum-DD);
												value.put("DEPTNO",data.mDeptno);
												value.put("BSUM", data.dBsum);
												if (DD!=0)	value.put("DISMONEY", data.dDismoney);  else	value.put("DISMONEY", 0);

												if ((data.dOrdcash !=0 || data.dOrdcard !=0) ) {
													value.put("ORDCASH", data.dOrdcash);
													value.put("ORDCARD", data.dOrdcard);
													value.put("ORDDATE", data.mOrddate);
												} else {
													value.put("ORDCASH", 0);
													value.put("ORDCARD", 0);
													value.put("ORDDATE", "");
												}
												if ((data.dCash !=0 || data.dCard !=0)  || data.dAtm!=0 || data.dTrcash!=0) {
													value.put("CASH", data.dCash);
													value.put("CARD", data.dCard);
													value.put("FINEDATE", data.mFinedate);
												} else {
													value.put("CASH", 0);
													value.put("CARD", 0);
													value.put("FINEDATE", "");
													//data.mIsconfirm="Y";
													//value.put("ISCONFIRM", "Y");
												}
												data.mIsconfirm="Y";value.put("ISCONFIRM", "Y");
												data.mIscheck="N";	value.put("ISCHECK", "N");
												value.put("ATM",data.dAtm);
												value.put("TRCASH",data.dTrcash);
												value.put("BANKNAME",mBankn);
												if (data.mTR !=null && data.mTR.equalsIgnoreCase("Y0")) {
													value.put("TR","Y1");
													value.put("ID","1006");
												} else {value.put("TR",mKind);value.put("ID",mKind);}
												value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
												int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
												db.close();

												if (success!=0) {  //可以直接返回 再考慮回傳值 或可以進入詢問列印帳單 或 詢問傳送資訊
													maintainQItems();
													modify=1;
													Intent intentS = new Intent();
													intentS.putExtra("QPosSave_modify",modify);
													QPosSave.this.setResult(RESULT_OK,intentS);

													if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
													if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
												}

												break;
											}
										}
									}
								});

					}else{
						Toast.makeText(QPosSave.this, "保留單不可以直接結帳!!", Toast.LENGTH_SHORT).show();
					}
					return true;
				}
			}
			return false;
		}
	}

	class MainClick implements OnClickListener { //所有的觸發事件,在此集中管理
		@Override
		public void onClick(View v) {
			int id = v.getId();
			Utilis.runVibrate(QPosSave.this);
			switch (id) {
				case R.id.dismoney:
				case R.id.now_cash:
				case R.id.now_atm:
				case R.id.now_card:
				case R.id.now_trcash:
				case R.id.ord_cash:
				case R.id.ord_card:
					showComputer(id);
					break;
				case R.id.owl_last:
				case R.id.owl_ilast:
					LTB=isLTBS(isLTB.last);
					break;

				case R.id.owl_tot:
				case R.id.owl_itot:
					LTB=isLTBS(isLTB.tot);
					break;

				case R.id.owl_bsum:
				case R.id.owl_ibsum:
					LTB=isLTBS(isLTB.bsum);
					break;

				case R.id.idismoney:  //檢查四捨五入的規則,手動預設為無條件捨去法,自動切換
				{
					if (data.dAsum != data.dBsum) {
						data.dDismoney=0;
						((EditText)findViewById(R.id.dismoney)).setText("");
					} else {
						data.dDismoney=(data.dAsum % 10);
						((EditText)findViewById(R.id.dismoney)).setText(new BigDecimal(data.dDismoney).toString());
					}
					clearNow();

					break;
				}
				case R.id.ord_icash:  //預購現金鈕
				{
					data.dOrdcash=data.dOrdcash+100;
					((EditText)findViewById(R.id.ord_cash)).setText(new BigDecimal(data.dOrdcash).toString());
					if (data.mOrddate.length()<=0){
						data.mOrddate=data.mFormdate;
						((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
					}
					clearNow();
					break;
				}
				case R.id.ord_icard:  //預購刷卡鈕
				{
					data.dOrdcard=data.dOrdcard+100;
					((EditText)findViewById(R.id.ord_card)).setText(new BigDecimal(data.dOrdcard).toString());
					if (data.mOrddate.length()<=0){
						data.mOrddate=data.mFormdate;
						((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
					}
					clearNow();
					break;
				}
				case R.id.ord_date:  //預購訂金日期 (本日期關係到營業日結帳的範圍)
					if(mDealer==0 || (PSWD!=1 && mDealer==1)){
						//批發 OR 零售且授權
						if  (data.mOrddate.length()<=0)	data.mOrddate=data.mFormdate;
						mYear=Utilis.toInt(data.mOrddate.substring(0, 4));
						mMonth=Utilis.toInt(data.mOrddate.substring(5, 7));
						mDay=Utilis.toInt(data.mOrddate.substring(8, 10));
						Calendar c = Calendar.getInstance();
						c.set(Calendar.YEAR, mYear);
						c.set(Calendar.MONTH, mMonth - 1);
						c.set(Calendar.DAY_OF_MONTH, mDay);
	
						DateUtil.showDateDialog(QPosSave.this, "請輸入正確預購訂金日期", c, new IDateDialog(){
							@Override
							public void onDateDialogFinish(String date, int year, int month, int day) {
								((Button)findViewById(R.id.ord_date)).setText(date);
								data.mOrddate=date;
							}
						});
					}
					break;
				case R.id.ord_izero:  //預購歸零
				{
					//如果isconfirm='Y' 理論上不可以做修改,只有only ischeck='Y' 為已結帳,則須詢問,是否重新結帳
					data.dOrdcash=0;
					((EditText)findViewById(R.id.ord_cash)).setText("");
					data.dOrdcard=0;
					((EditText)findViewById(R.id.ord_card)).setText("");
					data.mOrddate="";
					((Button)findViewById(R.id.ord_date)).setText("");
					clearNow();
					break;
				}
				case R.id.owl_save:  //賒帳 沒有平衡帳問題,可能有餘額
					//需檢查如果預購3有資料時,ischeck="Y"  如果空値則 ischeck="N"
					//isconfirm="Y" always 且無條件存入NOW3
				{

					int ordchk=0;
					int nowchk=0;

						SQLiteDatabase db = Utilis.getDB(QPosSave.this);
						String[] args = {mFno};
						ContentValues value = new ContentValues();
						value.put("DEPTNO",data.mDeptno);
						value.put("BSUM", data.dBsum);

						if (data.dDismoney!=0){
							value.put("DISMONEY", data.dDismoney);
						} else {
							value.put("DISMONEY", 0);
						}

						if ((data.dOrdcash !=0 || data.dOrdcard !=0) )	ordchk=1;
						if ((data.dCash !=0 || data.dCard !=0 || data.dAtm !=0 || data.dTrcash !=0) )	nowchk=1;

						if (data.dCash !=0 ) value.put("CASH", data.dCash); else value.put("CASH", 0);
						if (data.dTrcash !=0 ) value.put("TRCASH", data.dTrcash); else value.put("TRCASH", 0);
						if (data.dCard !=0 ) value.put("CARD", data.dCard); else value.put("CARD", 0);
						if (data.dAtm !=0 ) value.put("ATM", data.dAtm); else value.put("ATM", 0);
						if (data.dOrdcash !=0 ) value.put("ORDCASH", data.dOrdcash); else value.put("ORDCASH", 0);
						if (data.dOrdcard !=0 ) value.put("ORDCARD", data.dOrdcard); else value.put("ORDCARD", 0);

						if (ordchk==1 && nowchk!=1){
							//表示只有訂金
							data.mIscheck="Y";value.put("ISCHECK", "Y");
							data.mIsconfirm="N";value.put("ISCONFIRM", "N");
						}else{
							data.mIscheck="N";value.put("ISCHECK", "N");
							data.mIsconfirm="Y";value.put("ISCONFIRM", "Y");
						}
						value.put("FINEDATE", data.mFinedate);
						value.put("ORDDATE", data.mOrddate);

						value.put("BANKNAME",mBankn);

						if (data.mTR !=null && data.mTR.equalsIgnoreCase("Y0")) {
							value.put("TR","Y1");
							value.put("ID","1006");
						} else {value.put("TR",mKind);value.put("ID",mKind);}
						value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
						int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
						db.close();
						if (success!=0) {  //可以直接返回 再考慮回傳值 或可以進入詢問列印帳單 或 詢問傳送資訊
							maintainQItems();
							if (isChk==1 || isChf==1){
								Toast.makeText(QPosSave.this, mFno+"已經成功更新!!", Toast.LENGTH_SHORT).show();
							}else {
								Toast.makeText(QPosSave.this, mFno+"已經成功結帳!!", Toast.LENGTH_SHORT).show();
							}
						}
						modify=1;
						Intent intentS = new Intent();
						intentS.putExtra("QPosSave_modify",modify);
						QPosSave.this.setResult(RESULT_OK,intentS);

						if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
						if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
					break;
				}
				case R.id.ord_save:  //預購結帳
					//@@@如何解決預購不付款的結帳
				{
					SQLiteDatabase db = Utilis.getDB(QPosSave.this);
					String[] args = {mFno};
					ContentValues value = new ContentValues();
					value.put("DEPTNO",data.mDeptno);
					value.put("BSUM", data.dBsum);
					if (data.dDismoney !=0){
						value.put("DISMONEY", data.dDismoney);
					} else {
						value.put("DISMONEY", 0);
					}

					if ((data.dOrdcash !=0 || data.dOrdcard !=0) || (data.dBsum>0) ) {  //20130411加入未付款也可以結帳
						value.put("ORDCASH", data.dOrdcash);
						value.put("ORDCARD", data.dOrdcard);
						value.put("ORDDATE", data.mOrddate);
					} else {
						value.put("ORDCASH", 0);
						value.put("ORDCARD", 0);
						if (data.mOrddate.length()<=0) data.mOrddate=data.mFormdate;
						value.put("ORDDATE", data.mOrddate);
					}
					data.mIscheck="Y";	value.put("ISCHECK", "Y");
					data.mIsconfirm="N";	value.put("ISCONFIRM", "N");

					value.put("BANKNAME",mBankn);
					if (data.mTR !=null && data.mTR.equalsIgnoreCase("Y0")) {
						value.put("TR","Y1");
						value.put("ID","1006");
					} else {value.put("TR",mKind);value.put("ID",mKind);}
					value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
					int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
					db.close();
					if (success!=0) {
						maintainQItems();
						//createdatetime也要存入現在的時間
						//@@cloud_update
					}
						Toast.makeText(QPosSave.this, mFno+"已經成功完成預購結帳!!  "+success, Toast.LENGTH_SHORT).show();
					//關鍵就在如果NOW區有值,則不可以結束畫面,表示尚未完成結帳
					if (isChf==0 ) { //表示本單來自於單純的預購結帳
						if (data.mFinedate.length()>0) { //就是還直接輸入了NOW區的資料
							Toast.makeText(QPosSave.this, mFno+"請繼續完成即時結帳!!", Toast.LENGTH_SHORT).show();
						} else {
							modify=1;
							Intent intentS = new Intent();
							intentS.putExtra("QPosSave_modify",modify);
							QPosSave.this.setResult(RESULT_OK,intentS);

							if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
							if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
						}
					} else { //本單已經結算過,需再度讓NOW區確認是否合法正確
						Toast.makeText(QPosSave.this, mFno+"請繼續完成即時結帳!!", Toast.LENGTH_SHORT).show();
					}

					break;
				}
				case R.id.ord_exit:  //預購部返回
				{
					modify=0;
					Intent intentS = new Intent();
					intentS.putExtra("QPosSave_modify",modify);
					QPosSave.this.setResult(RESULT_CANCELED,intentS);
					finish();

					break;
				}
				case R.id.now_icash:  //現金全額
					switch(LTB){
						case isLTB.last:
							data.dCash=owlTot*-1+data.dCash-data.dBsum;
							break;
						case isLTB.tot:
							data.dCash=owlTot*-1+data.dCash;
							break;
						case isLTB.bsum:
							data.dCash=owlTot*-1+data.dCash+owlLast;
							break;
					}
					((EditText)findViewById(R.id.now_cash)).setText(new BigDecimal(data.dCash).toString());
					getFormdate();
					((Button)findViewById(R.id.now_date)).setText(data.mFinedate);
					break;

				//now_iatm
				case R.id.now_iatm:  //匯款全額
					switch(LTB){
						case isLTB.last:
							data.dAtm=owlTot*-1+data.dAtm-data.dBsum;
							break;
						case isLTB.tot:
							data.dAtm=owlTot*-1+data.dAtm;
							break;
						case isLTB.bsum:
							data.dAtm=owlTot*-1+data.dAtm+owlLast;
							break;
					}
					((EditText)findViewById(R.id.now_atm)).setText(new BigDecimal(data.dAtm).toString());
					getFormdate();
					((Button)findViewById(R.id.now_date)).setText(data.mFinedate);
					break;

				case R.id.now_icard:  //票刷全額
					switch(LTB){
						case isLTB.last:
							data.dCard=owlTot*-1+data.dCard-data.dBsum;
							break;
						case isLTB.tot:
							data.dCard=owlTot*-1+data.dCard;
							break;
						case isLTB.bsum:
							data.dCard=owlTot*-1+data.dCard+owlLast;
							break;
					}
					((EditText)findViewById(R.id.now_card)).setText(new BigDecimal(data.dCard).toString());
					getFormdate();
					((Button)findViewById(R.id.now_date)).setText(data.mFinedate);

					break;

				case R.id.now_izero:  //歸零
				{
					//如果isconfirm='Y' 為已結帳,則須詢問,是否重新結帳
					clearNow();
					break;
				}

				case R.id.now_date:  //結帳日期 (本日期關係到營業日結帳的範圍)
				{
					if(mDealer==0 || (PSWD!=1 && mDealer==1)){
						//批發 OR 零售且授權
						getFormdate();
						mYear=Utilis.toInt(data.mFinedate.substring(0, 4));
						mMonth=Utilis.toInt(data.mFinedate.substring(5, 7));
						mDay=Utilis.toInt(data.mFinedate.substring(8, 10));
						Calendar cc = Calendar.getInstance();
						cc.set(Calendar.YEAR, mYear);
						cc.set(Calendar.MONTH, mMonth - 1);
						cc.set(Calendar.DAY_OF_MONTH, mDay);
	
						DateUtil.showDateDialog(QPosSave.this, "請輸入正確結帳日期", cc, new IDateDialog(){
							@Override
							public void onDateDialogFinish(String date, int year, int month, int day) {
								((Button)findViewById(R.id.now_date)).setText(date);
								data.mFinedate=date;
	
							}
						});
					}
					break;
				}

				case R.id.now_save:  //一定要平衡,才能確實存入
				{
					String[] args = {mFno};
					ContentValues value = new ContentValues();
					int oOk=0;
					value.put("DEPTNO",data.mDeptno);
					value.put("BSUM", data.dBsum);
					if (data.dDismoney !=0){
						value.put("DISMONEY", data.dDismoney);
					} else {
						value.put("DISMONEY", 0);
					}

					switch(LTB) {
						case isLTB.last: 		//只結算前帳,可以不必平衡帳,表示入金功能
						{
							oOk=1;
							break;
						}
						case isLTB.tot: 		//結清所有的帳,一定要平衡,全部清
						{
							if (owlTot==0) { //表示結算金額正確
								oOk=1;
							}
							break;
						}

						case isLTB.bsum:	//只結算本單,一定要平衡
						{
							if (owlTot==owlLast) { //只有前帳尚未結清,結算正確
								oOk=1;
							}
							break;
						}

					}
					if (oOk==1) {
						SQLiteDatabase db = Utilis.getDB(QPosSave.this);
						if ((data.dCash !=0 || data.dCard !=0 || data.dAtm !=0 || data.dTrcash !=0) && data.mFinedate.length()>0) {
							value.put("CASH", data.dCash);
							value.put("TRCASH", data.dTrcash);
							value.put("CARD", data.dCard);
							value.put("ATM", data.dAtm);
							value.put("FINEDATE", data.mFinedate);
						} else {
							data.dCash=0;value.put("CASH", 0);
							data.dCard=0;value.put("CARD", 0);
							data.dTrcash=0;value.put("TRCASH", 0);
							data.dAtm=0;value.put("ATM", 0);
							data.mFinedate="";value.put("FINEDATE", data.mFinedate);
						}
						//畢竟是帳目一起連動,所以也要維護
						if ((data.dOrdcash !=0 || data.dOrdcard !=0) && data.mOrddate.length()>0) {
							value.put("ORDCASH", data.dOrdcash);
							value.put("ORDCARD", data.dOrdcard);
							value.put("ORDDATE", data.mOrddate);
						} else {
							data.dOrdcash=0;value.put("ORDCASH", 0);
							data.dOrdcard=0;value.put("ORDCARD", 0);
							data.mOrddate="";value.put("ORDDATE", "");
						}

						data.mIsconfirm="Y";value.put("ISCONFIRM", "Y");
						data.mIscheck="N";value.put("ISCHECK", "N");
						value.put("BANKNAME",mBankn);
						if (data.mTR !=null && data.mTR.equalsIgnoreCase("Y0")) {
							value.put("TR","Y1");
							value.put("ID","1006");
						} else {value.put("TR",mKind);value.put("ID",mKind);}
						value.put("CREATEDATETIME", DateUtil.getSystemTime().substring(2,14));
						int success = db.update(TbName.QHEAD, value,"FORMNO=?", args);
						db.close();
						if (success!=0) {  //可以直接返回 再考慮回傳值 或可以進入詢問列印帳單 或 詢問傳送資訊
							maintainQItems();
							//createdatetime也要存入現在的時間
							//@@cloud_update
							if (isChf==1){
								Toast.makeText(QPosSave.this, mFno+"已經成功更新!!", Toast.LENGTH_SHORT).show();
							}else {
								Toast.makeText(QPosSave.this, mFno+"已經成功結帳!!", Toast.LENGTH_SHORT).show();
							}

							modify=1;
							Intent intentS = new Intent();
							intentS.putExtra("QPosSave_modify",modify);
							QPosSave.this.setResult(RESULT_OK,intentS);

							if((data.dCash!=0 || data.dOrdcash!=0 || data.dTrcash!=0) && (Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9600 || Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 7))==9100)) Utilis.openCashDraw(QPosSave.this);
							if (!compStr[7].equalsIgnoreCase("0")) PrintSel(); else finish();
						}

					} else {
						Toast.makeText(QPosSave.this, mFno+"請檢查結帳金額是否正確!!", Toast.LENGTH_SHORT).show();
					}
					break;
				}
				case R.id.now_exit:  //只能在銷貨/預購/暫存狀態 進行新增
					modify=0;
					Intent intentS = new Intent();
					intentS.putExtra("QPosSave_modify",modify);
					QPosSave.this.setResult(RESULT_CANCELED,intentS);
					finish();
			}
		}
	}

	private void showComputer(final int vid){
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		final View root = this.getLayoutInflater().inflate(R.layout.dlg_computer, null);
		final Calculator calu = new Calculator(QPosSave.this, root);
		final TextView print = (TextView)root.findViewById(R.id.text_print);
		String strValue = ((EditText)QPosSave.this.findViewById(vid)).getText().toString();
		if(strValue != null && strValue.length() > 0){
			print.setText(strValue);
		}

		dialog.setView(root);
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				calu.resultNumber();
				EditText edit = (EditText)QPosSave.this.findViewById(vid);
				try{
					String result = print.getText().toString();
					if(result == null || result.length() <=0){
						print.setText("0");
					}else if(result.indexOf("+") > 0){
						print.setText("0");
					}else if(result.indexOf("-") > 0){
						print.setText("0");
					}else if(result.indexOf("×") > 0){
						print.setText("0");
					}else if(result.indexOf("÷") > 0){
						print.setText("0");
					}
					edit.setText(print.getText().toString());

					recountOwl(mMode);

				}catch(Exception e){
					e.printStackTrace();
				}
				calu.clear();
			}
		});
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialog.show();
	}



	private TextWatcher watcherPrice = new TextWatcher(){
		@Override
		public void afterTextChanged(Editable s) {
		}
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
								  int count) {
			data.dDismoney=Utilis.toDouble(((EditText)findViewById(R.id.dismoney)).getText().toString());
			data.dCash=Utilis.toDouble(((EditText)findViewById(R.id.now_cash)).getText().toString());
			data.dAtm=Utilis.toDouble(((EditText)findViewById(R.id.now_atm)).getText().toString());
			data.dCard=Utilis.toDouble(((EditText)findViewById(R.id.now_card)).getText().toString());
			data.dTrcash=Utilis.toDouble(((EditText)findViewById(R.id.now_trcash)).getText().toString());
			data.dOrdcash=Utilis.toDouble(((EditText)findViewById(R.id.ord_cash)).getText().toString());
			data.dOrdcard=Utilis.toDouble(((EditText)findViewById(R.id.ord_card)).getText().toString());

			recountOwl(mMode);
		}
	};

	private void clearNow(){
		if (data.mFinedate.length()>0) {
			data.mFinedate=""; ((Button)findViewById(R.id.now_date)).setText("");
			data.dCash=0; ((EditText)findViewById(R.id.now_cash)).setText("");
			data.dTrcash=0; ((EditText)findViewById(R.id.now_trcash)).setText("");
			data.dCard=0; ((EditText)findViewById(R.id.now_card)).setText("");
			data.dAtm=0; ((EditText)findViewById(R.id.now_atm)).setText("");
		}

	}

	private int isLTBS(int check){

		((Button)findViewById(R.id.owl_last)).setBackgroundResource(R.drawable.keypad_gray);
		((Button)findViewById(R.id.owl_tot)).setBackgroundResource(R.drawable.keypad_gray);
		((Button)findViewById(R.id.owl_bsum)).setBackgroundResource(R.drawable.keypad_gray);
		((Button)findViewById(R.id.owl_ilast)).setBackgroundResource(R.drawable.keypad_gray);
		((Button)findViewById(R.id.owl_itot)).setBackgroundResource(R.drawable.keypad_gray);
		((Button)findViewById(R.id.owl_ibsum)).setBackgroundResource(R.drawable.keypad_gray);


		switch(check) {
			case isLTB.last:
			{
				((Button)findViewById(R.id.owl_last)).setBackgroundResource(R.drawable.keypad_green);
				((Button)findViewById(R.id.owl_ilast)).setBackgroundResource(R.drawable.keypad_green);
				break;
			}
			case isLTB.tot:
			{
				((Button)findViewById(R.id.owl_tot)).setBackgroundResource(R.drawable.keypad_green);
				((Button)findViewById(R.id.owl_itot)).setBackgroundResource(R.drawable.keypad_green);
				break;
			}
			case isLTB.bsum:
			{
				((Button)findViewById(R.id.owl_bsum)).setBackgroundResource(R.drawable.keypad_green);
				((Button)findViewById(R.id.owl_ibsum)).setBackgroundResource(R.drawable.keypad_green);
				break;
			}

		}
		if (data.mFinedate.length()>0) {
			data.mFinedate=""; ((Button)findViewById(R.id.now_date)).setText("");
			data.dCash=0; ((EditText)findViewById(R.id.now_cash)).setText("");
			data.dTrcash=0; ((EditText)findViewById(R.id.now_trcash)).setText("");
			data.dCard=0; ((EditText)findViewById(R.id.now_card)).setText("");
			data.dAtm=0; ((EditText)findViewById(R.id.now_atm)).setText("");
		}

		return check;
	}

	private void recountOwl(int mode){

		data.dBsum=data.dAsum-data.dDismoney+data.dTrcash;

		owlTot=owlLast-data.dBsum+data.dCash+data.dAtm+data.dCard+data.dOrdcash+data.dOrdcard;

		((Button)findViewById(R.id.owl_tot)).setText(new BigDecimal(owlTot).toString());
		((Button)findViewById(R.id.owl_bsum)).setText(new BigDecimal(data.dBsum).toString());


		if ((data.dCash !=0 || data.dCard !=0 || data.dAtm !=0 || data.dTrcash !=0)) {
			getFormdate();
			((Button)findViewById(R.id.now_date)).setText(data.mFinedate);
		}
		if ((data.dOrdcash !=0 || data.dOrdcard !=0)){
			if (data.mOrddate.length()<=0)	data.mOrddate=data.mFormdate;
			((Button)findViewById(R.id.ord_date)).setText(data.mOrddate);
		}

	}

	private void PrintSel(){
		double RB=data.dCash;
		menu_S[6]="找零建議(1)$"+String.valueOf(1000-(RB%1000));
		if ((RB%500)<=500) menu_S[7]="找零建議(2)$"+String.valueOf(500-(RB%500));
		else menu_S[7]="找零建議(2)$"+String.valueOf(100-(RB%100));
		if (RB<=100)
			if (((RB%100)<=50))	menu_S[8]="找零建議(3)$"+String.valueOf(50-(RB%50));
			else menu_S[8]="找零建議(3)$"+String.valueOf(100-(RB%100));
		else  menu_S[8]="找零建議(3)$"+String.valueOf(100-(RB%100));

		Utilis.runVibrate(QPosSave.this);
		DialogUtilis.showDialog(QPosSave.this, "請選擇列印的項目", -1,
				menu_S, new IDialog() {
					@Override
					public void onDialogFinish(int pos, String choice) {
						switch (pos) {
							case 0: // 印存根聯
							{
								//connetNet("192.168.1.97" );
								Printer("(存根聯)",0);
								finish();
								break;
							}
							case 1: // 印收執聯
							{
								//connetNet("192.168.1.97" );
								Printer("(收執聯)",0);
								finish();
								break;
							}
							case 2: // 印存根聯+收執聯
							{
								Printer("(收執聯);(存根聯)",0);
								finish();
								break;
							}
							case 3: // 印存根聯+收執聯+名條
							{
								Printer("(存根聯);(收執聯)",1);
								finish();
								break;
							}
							case 4: // 印名條
							{
								Printer("",1);
								finish();
								break;
							}
							case 5: // 返回
							{
								finish();
								break;
							}
							case 6: // @@輸出
							{
								Intent intent = new Intent();
								intent.setClass(QPosSave.this,MakeTransDetail.class);
								intent.putExtra("make_batch",mFno);
								intent.putExtra("make_mode",1);
								intent.putExtra("make_kind",nKind);
								intent.putExtra("make_user","");
								intent.putExtra("make_01",0);
								startActivity(intent);
								finish();
								break;
							}
							case 7: // 返回
							{
								finish();
								break;
							}
							case 8: // 返回
							{
								finish();
								break;
							}
							case 9: // 返回
							{
								finish();
								break;
							}

						}
					}
				});
	}

	private void getFormdate(){ //2019-04-18
		if ((data.mFinedate.length()<=0 || data.mFinedate==null) && (data.mOrddate.length()<=0 || data.mOrddate==null) && !data.mIscheck.equalsIgnoreCase("Y")){
			data.mFinedate=data.mFormdate;
		}else if (data.mFinedate.length()<=0 || data.mFinedate==null){
			String FormD=DateUtil.getCurrentDate();  //2012-10-22
			int Ttime=Utilis.toInt(DateUtil.getSystemTime().substring(8, 10));  //取得目前幾點
			if (Ttime<=5)  FormD = DateUtil.addDate(FormD, -1);
			FormD = FormD.replace("/", "-");
			data.mFinedate=FormD;
		}
	}

	private void Printer(String xLst,int Ptitle){
		//來自 賒帳 預購結帳 確認結帳
		//Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 8))
		//0.不印 1.自動判斷(收執/存根) 2.全印(無條件)
		//文字太長時,須跳行嗎
		//輸出->如下程式碼
		//先寫簡式

		String [] xList;
		xList=xLst.split(";");
		StringBuffer mBuf = new StringBuffer();

		if (xLst.length()>0){
			if (Utilis.toInt(compStr[8])<=2){//57mm no_pic

				if (xList.length>0){
					SQLiteDatabase db = Utilis.getDB(QPosSave.this);
					mBuf.append("select GOODSNO,GOODSNAME,UNIT"+mKind.substring(0,1)+"0,UNITPRICE,SUBPRICE,DISCOUNT,UNIT1,SIZE,PS,SOURCENO,ORDDATE");
					mBuf.append(" from qitems where FORMNO= '"+mFno+"' and QKIND LIKE '"+mKind.substring(0,1)+"%' order by GOODSNO,UNITPRICE,DISCOUNT,UNIT1,SIZE");
					Cursor cs=db.rawQuery(mBuf.toString(), null);

					if (sPrintB.toString().length()>0) sPrintB.delete(0, sPrintB.toString().length());

					sPrintB.append("^QXXXX,0,0"+sepa);
					sPrintB.append("^W56"+sepa+"^H7"+sepa+"^P1"+sepa+"^S4"+sepa+"^AD"+sepa+"^C1"+sepa+"^R0"+sepa+"~Q+0"+sepa
							+"^O0"+sepa+"^D0"+sepa+"^E11"+sepa+"~R20"+sepa+"^L"+sepa+"Dy2-me-dd"+sepa+"Th:m:s"+sepa);
					Loca=0;

					for (int jj=0;jj<xList.length;jj++){
						Loca=Loca+20;
						if (Utilis.toInt(compStr[8])==0){
							Loca=Loca+2;sPrintB.append("AZ,8,"+Loca+",2,2,0,0,"+compStr[0]+sepa);
							Loca=Loca+70;sPrintB.append("AZ,8,"+Loca+",2,2,0,0,"+data.mFormdate+xList[jj]+sepa);
						}else{//1
							Loca=Loca+2;sPrintB.append("Y8,"+Loca+",STYLEKEY"+sepa);
							Loca=Loca+230;sPrintB.append("AZ,8,"+Loca+",2,2,0,0,"+data.mFormdate+xList[jj]+sepa);
						}
						Loca=Loca+61;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,單號:"+data.mFno+sepa);
						sPrintB.append("AZ,212,"+Loca+",1,1,0,0,"+"經手: "+data.mEmpid+"/"+data.mEmpname+sepa);
						if (data.dOrdcard>0 || data.dOrdcash>0){
							Loca=Loca+35;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,會員:"+data.mUser+" / "+data.mUsername+" 訂購"+sepa);
						}else{
							Loca=Loca+35;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,會員:"+data.mUser+" / "+data.mUsername+sepa);
						}
						String PS="TR_"+data.mhPs1+" PS_"+data.mhPs;
						if ((data.mhPs1!=null && data.mhPs1.length()>0) || (data.mhPs!=null && data.mhPs.length()>0))	{Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+PS+sepa);} else Loca=Loca+20;
						if (mMobil!=null && mMobil.length()>0){Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,電話:"+mMobil+sepa);}
						if (mAddress!=null && mAddress.length()>0){Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,地址:"+mAddress+sepa);}
						Loca=Loca+2;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,--------------------------------------"+sepa);
						Loca=Loca+39;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,貨號-品名"+sepa);
						sPrintB.append("AZ,212,"+Loca+",1,1,0,0,數量"+sepa);
						sPrintB.append("AZ,300,"+Loca+",1,1,0,0,單價"+sepa);
						sPrintB.append("AZ,360,"+Loca+",1,1,0,0,小計"+sepa);
						sPrintB.append("AZ,420,"+Loca+",1,1,0,0,註"+sepa);
						sPrintB.append("AZ,8,"+Loca+",1,2,0,0,--------------------------------------"+sepa);
						Loca=Loca+12;
						if(cs.getCount()>0){
							cs.moveToFirst();
							StringBuffer  TotI=new StringBuffer();

							int ColCNT=0;
							int TotU=0;
							double TotS=0;
							int TotC=0;
							String mGOODS=cs.getString(0);
							String mSOURCENO=cs.getString(9);
							String mGOODN=cs.getString(1);
							String mUNITP=cs.getString(3);
							String mCOL="";
							int nDISC=cs.getInt(5);
							String mPS="";
							if (Utilis.toInt(cs.getString(10))>1){
								mPS=cs.getString(10)+"段";
							}else mPS=cs.getString(8);
							//}else if(nDISC<100) mPS=mPS+nDISC;

							for (int ii=0;ii<cs.getCount();ii++){

								if (!mGOODS.equalsIgnoreCase(cs.getString(0)) || !mUNITP.equalsIgnoreCase(cs.getString(3)) || nDISC!=cs.getInt(5)){ //
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2")){
										if(mGOODS.length()<=7 && mGOODS.length()>5){
											if (mGOODN.length()>5) mGOODN=mGOODN.substring(0,5);
										}else if ((mGOODS.length()+mGOODN.length())>12 && mGOODN.length()>4 ){
											mGOODN=mGOODN.substring(0,4);
										}
										Loca=Loca+25;
										sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+mGOODS+"-"+mGOODN+sepa);
									}else if (barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
										if(mSOURCENO.length()<=5 && mGOODN.length()>7){
											mGOODN=mGOODN.substring(0,7);
										}else if ((mSOURCENO.length()+mGOODN.length())>12 && mGOODN.length()>4 ){
											mGOODN=mGOODN.substring(0,4);
										}
										Loca=Loca+25;

										sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+mSOURCENO+"-"+mGOODN+sepa);
									}else if (barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("3")){
										Loca=Loca+12;
										sPrintB.append("AZ,8,"+Loca+",1,1,0,0,---------------------------------------"+sepa);
										Loca=Loca+14;
										sPrintB.append("AC,8,"+Loca+",1,1,0,0,"+mGOODS+sepa);
									}
									sPrintB.append("AZ,"+(252-(""+(int)TotU).length()*10)+","+Loca+",1,2,0,0,"+TotU+sepa);
									sPrintB.append("AZ,"+(320-(mUNITP).length()*10)+","+Loca+",1,2,0,0,x"+mUNITP+sepa);
									sPrintB.append("AZ,"+(400-(new BigDecimal(TotS)).toString().length()*10)+","+Loca+",1,2,0,0,"+new BigDecimal(TotS).toString()+sepa);

									sPrintB.append("AZ,417,"+Loca+",1,2,0,0,"+mPS+sepa);

									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2") || barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
										if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1")  || barStr[8].equalsIgnoreCase("4")) Loca=Loca+50; else Loca=Loca+25;
									}else{
										Loca=Loca+29;  //
										sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+mGOODN+sepa);
										if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1")) Loca=Loca+24;
									}
									String iList[]=TotI.toString().split(";");
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("4")){
										for (int ij=0;ij<iList.length;ij++){
											sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+iList[ij]+sepa);
											if (ij<(iList.length-1)) Loca=Loca+25;
										}
									}
									TotI.delete(0, TotI.toString().length());
									String iUnit=cs.getString(2);
									if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
									TotI.append(cs.getString(6)+cs.getString(7)+"("+iUnit+")");
									TotC=1;
									ColCNT=1;
									TotU=cs.getInt(2);
									TotS=cs.getDouble(4);
									mGOODS=cs.getString(0);
									mSOURCENO=cs.getString(9);
									mGOODN=cs.getString(1);
									mUNITP=cs.getString(3);
									mCOL=cs.getString(6);
									nDISC=cs.getInt(5);
									mPS="";
									if (Utilis.toInt(cs.getString(10))>1){
										mPS=cs.getString(10)+"段";
									}else mPS=cs.getString(8);
									//}else if(nDISC<100) mPS=mPS+nDISC;
								}else {
									if (mCOL.equalsIgnoreCase(cs.getString(6))){
										String iUnit=cs.getString(2);
										if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
										TotI.append(cs.getString(7)+"("+iUnit+")");
										TotC=TotC+1;
										if ((ColCNT>2 && TotC>3) || TotC>4){
											TotI.append(";");
											TotC=0;
											ColCNT=1;
										}
									}else{
										String iUnit=cs.getString(2);
										if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
										TotI.append(cs.getString(6)+cs.getString(7)+"("+iUnit+")");
										TotC=TotC+1;
										mCOL=cs.getString(6) ;
										ColCNT=ColCNT+1;
										if ((ColCNT>2 && TotC>3) || TotC>4){
											TotI.append(";");
											TotC=0;
											ColCNT=1;
										}
									}
									TotU=TotU+cs.getInt(2);
									TotS=TotS+cs.getDouble(4);
								}
								cs.moveToNext();
							}

							if (TotI.length()>0){

								if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2")){
									Loca=Loca+25;
									sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+mGOODS+"-"+mGOODN+sepa);
								}else if (barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
									Loca=Loca+25;
									sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+mSOURCENO+"-"+mGOODN+sepa);
								}else{
									Loca=Loca+12;
									sPrintB.append("AZ,8,"+Loca+",1,1,0,0,---------------------------------------"+sepa);
									Loca=Loca+12;
									sPrintB.append("AC,8,"+Loca+",1,1,0,0,"+mGOODS+sepa);
								}

								sPrintB.append("AZ,"+(252-(""+(int)TotU).length()*10)+","+Loca+",1,2,0,0,"+TotU+sepa);
								sPrintB.append("AZ,"+(320-(mUNITP).length()*10)+","+Loca+",1,2,0,0,x"+mUNITP+sepa);
								sPrintB.append("AZ,"+(400-(new BigDecimal(TotS).toString()).length()*10)+","+Loca+",1,2,0,0,"+new BigDecimal(TotS).toString()+sepa);

								sPrintB.append("AZ,417,"+Loca+",1,2,0,0,"+mPS+sepa);

								if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2") || barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1")  || barStr[8].equalsIgnoreCase("4")) Loca=Loca+50; else Loca=Loca+25;
								}else{
									Loca=Loca+29;  //
									sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+mGOODN+sepa);
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1")) Loca=Loca+24;
								}


								String iList[]=TotI.toString().split(";");
								if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("4")){
									for (int ij=0;ij<iList.length;ij++){
										sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+iList[ij]+sepa);
										if (ij<(iList.length-1)) Loca=Loca+25;				}
								}
							}
						}
						sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"--------------------------------------"+sepa);

						if (Utilis.toInt(compStr[8])!=2){
							Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"(=)前帳:"+(owlLast*-1)+sepa);
							sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(+)應收:"+new BigDecimal(data.dAsum+data.dTrcash).toString()+sepa);
						}else{
							Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"( )數量("+data.mHandno+"件)"+sepa);
							if (data.dDismoney!=0)
								sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(+)合計:"+new BigDecimal(data.dAsum+data.dTrcash).toString()+"(折)"+data.dDismoney+sepa);
							else
								sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(+)合計:"+new BigDecimal(data.dAsum+data.dTrcash).toString()+sepa);
						}
						if (data.dCash!=0 || data.dCard!=0){
							Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"(-)現收:"+new BigDecimal(data.dCash).toString()+sepa);
							sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(-)票刷:"+new BigDecimal(data.dCard).toString()+sepa);
						}
						if (data.dAtm!=0 || data.dTrcash!=0){
							Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"(+)代運:"+data.dTrcash+sepa);
							sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(-)匯款:"+new BigDecimal(data.dAtm).toString()+sepa);
						}
						if (data.dOrdcash!=0 || data.dOrdcard!=0){
							Loca=Loca+50;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"(-)定金:"+new BigDecimal(data.dOrdcash).toString()+sepa);
							sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"(-)票刷:"+new BigDecimal(data.dOrdcard).toString()+sepa);
						}
						Loca=Loca+25;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"--------------------------------------"+sepa);

						if (Utilis.toInt(compStr[8])!=2){
							Loca=Loca+40;sPrintB.append("AZ,8,"+Loca+",1,2,0,0,"+"(=)未結:"+new BigDecimal(owlTot*-1).toString()+sepa);
							if (data.dDismoney!=0)
								sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"數量:"+data.mHandno+"件(折)"+data.dDismoney+sepa);
							else
								sPrintB.append("AZ,212,"+Loca+",1,2,0,0,"+"數量("+data.mHandno+"件)"+sepa);
						}
						Loca=Loca+35;
						if (compStr[3].length()>0) {Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+"TEL:"+compStr[3]+sepa);}
						if (compStr[4].length()>0) {Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+compStr[4]+sepa);}
						if (compStr[2].length()>0) {Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+"地址:"+compStr[2]+sepa);}
						if (compStr[1].length()>0) {Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+compStr[1]+sepa);}
						Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,印製:"+DateUtil.getFormatDate()+" 開單:"+data.mTime+sepa);
						if (devStr[0].length()>0)	{Loca=Loca+40;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[0]+sepa);}
						if (devStr[1].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[1]+sepa);}
						if (devStr[2].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[2]+sepa);}
						if (devStr[3].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[3]+sepa);}

						Loca=Loca+110;sPrintB.append("AZ,20,"+Loca+",1,2,0,0,"+"簽收人:____________________"+sepa);
						Loca=Loca+90;

						StartToPrint();
						if (jj<(xList.length-1)) SystemClock.sleep(3000);
					}
					db.close();
					cs.close();
				}

			}else if (Utilis.toInt(compStr[8])==9){//雲端印表機
				Bundle bundle = new Bundle();
				Intent intent = new Intent();
				//bundle = new Bundle();
				//bundle.putString("Mkqrs",""+data.mFno+","+mKind+",0,"+data.mDeptno);
				intent.putExtra("SERCH",data.mFno);
				intent.putExtra("QKIND",mKind);
				intent.putExtra("MODE",0);
				intent.putExtra("DEPTNO",data.mDeptno);
				intent.putExtra("BUFSTR","");
				intent.putExtra("BUFTITLE","1;2;3");
				intent.putExtra("STARTDAY",data.mFinedate);
				intent.putExtra("ENDDAY",data.mFinedate);

				intent.setClass(QPosSave.this, MkqrSt.class);
				intent.putExtras(bundle);
				startActivity(intent);

			}else if (Utilis.toInt(compStr[8])==3 || Utilis.toInt(compStr[8])==4 || Utilis.toInt(compStr[8])==31 || Utilis.toInt(compStr[8])==41){//80mm no_pic
				//31,41 顏色尺寸字放大
				if (xList.length>0){
					SQLiteDatabase db = Utilis.getDB(QPosSave.this);
					Cursor cs=null;
					//如果為出貨單及devStr.length()>0 表示指定單價
					String UPRICE="UNITPRICE";
					if ((nKind==20 || nKind==21) && devStr[8] !=null && devStr[8].length()>0 && !devStr[8].equalsIgnoreCase("0") ) UPRICE="P"+devStr[8];
					mBuf.append("select GOODSNO,GOODSNAME,UNIT"+mKind.substring(0,1)+"0,"+UPRICE+",SUBPRICE,DISCOUNT,UNIT1,SIZE,PS,SOURCENO,ORDDATE");
					mBuf.append(" from qitems where FORMNO= '"+mFno+"' order by CREATEDATETIME,GOODSNO,UNITPRICE,DISCOUNT,UNIT1,SIZE");
					cs=db.rawQuery(mBuf.toString(), null);
					endLeng=30;//取得尾頁長度
					if ((compStr[3]+compStr[4]).length()>0)	endLeng=endLeng+65;
					if (compStr[2].length()>0) endLeng=endLeng+30;
					if (compStr[1].length()>0)endLeng=endLeng+30;
					endLeng=endLeng+30;
					if (devStr[0].length()>0) endLeng=endLeng+40;
					if (devStr[1].length()>0) endLeng=endLeng+30;
					if (devStr[2].length()>0) endLeng=endLeng+30;
					if (devStr[3].length()>0) endLeng=endLeng+30;
					endLeng=endLeng+110;

					for (int jj=0;jj<xList.length;jj++){//幾聯
						printTitle(xList, jj,0);
						Page=1;
						if(cs.getCount()>0){
							cs.moveToFirst();
							StringBuffer TotI=new StringBuffer();
							int ColCNT=0;
							int TotU=0;
							double TotS=0;
							int TotC=0;
							String mGOODS=cs.getString(0);
							String mSOURCENO=cs.getString(9);
							String mGOODN=cs.getString(1);
							String mUNITP=cs.getString(3);
							String mCOL="";
							int nDISC=cs.getInt(5);
							String mPS="";
							if (Utilis.toInt(cs.getString(10))>1){
								mPS=cs.getString(10)+"段";
							}else mPS=cs.getString(8);
							//}else if(nDISC<100) mPS=mPS+nDISC;


							for (int ii=0;ii<cs.getCount();ii++){

								if (!mGOODS.equalsIgnoreCase(cs.getString(0)) || !mUNITP.equalsIgnoreCase(cs.getString(3)) || nDISC!=cs.getInt(5)){ //
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2")){
										Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+mGOODS+"-"+mGOODN+sepa);
									}else if (barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
										Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+mSOURCENO+"-"+mGOODN+sepa);
									}else if (barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("3")){
										Loca=Loca+25;sPrintB.append("AZ,30,"+(Loca+3)+",1,1,0,0,"+mGOODS+"-"+mGOODN+sepa);
									}

									if (!barStr[8].equalsIgnoreCase("1") && !barStr[8].equalsIgnoreCase("3")){
										sPrintB.append("AZ,"+(392-(""+(int)TotU).length()*10)+","+Loca+",1,2,0,0,"+TotU+sepa);
										sPrintB.append("AZ,"+(460-(mUNITP).length()*10)+","+Loca+",1,2,0,0,x"+mUNITP+sepa);
										if ((nKind==20 || nKind==21) && devStr[8] !=null && devStr[8].length()>0 ) ;else sPrintB.append("AZ,"+(550-(new BigDecimal(TotS).toString()).length()*10)+","+Loca+",1,2,0,0,"+new BigDecimal(TotS).toString()+sepa);
										sPrintB.append("AZ,580,"+Loca+",1,2,0,0,"+mPS+sepa);
										Loca=Loca+48;
									}else{
										sPrintB.append("AZ,"+(392-(""+(int)TotU).length()*10)+","+(Loca+3)+",1,1,0,0,"+TotU+sepa);
										sPrintB.append("AZ,"+(460-(mUNITP).length()*10)+","+(Loca+3)+",1,1,0,0,x"+mUNITP+sepa);
										if ((nKind==20 || nKind==21) && devStr[8] !=null && devStr[8].length()>0 ) ;else sPrintB.append("AZ,"+(550-(new BigDecimal(TotS).toString()).length()*10)+","+(Loca+3)+",1,1,0,0,"+new BigDecimal(TotS).toString()+sepa);
										sPrintB.append("AZ,580,"+(Loca+3)+",1,1,0,0,"+mPS+sepa);
										Loca=Loca+25;
									}
									String iList[]=TotI.toString().split(";");
									if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("4")){
										for (int ij=0;ij<iList.length;ij++){
											sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+iList[ij]+sepa);
											if (ij<(iList.length-1)) Loca=Loca+25;
										}
									}else{
										Loca=Loca-15;
									}
									sPrintB.append("AZ,30,"+(Loca+10)+",1,1,0,0,------------------------------------------------"+sepa);
									Loca=Loca+3;
									//如果 頁長>設定
									if (Utilis.toInt(compStr[8])==4){
										if ((Loca+85)>=Utilis.toInt(compStr[7])){
											printEndg(xList, jj,Page,1);
											Page=Page+1;
										}
									}

									TotI.delete(0, TotI.toString().length());
									String iUnit=cs.getString(2);
									if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
									TotI.append(cs.getString(6)+cs.getString(7)+"("+iUnit+")");
									TotC=1;
									ColCNT=1;
									TotU=cs.getInt(2);
									TotS=cs.getDouble(4);
									mGOODS=cs.getString(0);
									mSOURCENO=cs.getString(9);
									mGOODN=cs.getString(1);
									mUNITP=cs.getString(3);
									mCOL=cs.getString(6);
									nDISC=cs.getInt(5);
									mPS="";
									if (Utilis.toInt(cs.getString(10))>1){
										mPS=cs.getString(10)+"段";
									}else mPS=cs.getString(8);
									//}else if(nDISC<100) mPS=mPS+nDISC;
								}else {
									if (mCOL.equalsIgnoreCase(cs.getString(6))){
										String iUnit=cs.getString(2);
										if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
										TotI.append(cs.getString(7)+"("+iUnit+")");
										TotC=TotC+1;
										//
										//童裝特殊屬性,還要印(手)
										if (Utilis.toInt(cs.getString(10))>1){
											if (ColCNT>=3){
												TotI.append(";");
												TotC=0;
												ColCNT=1;
											}

										}else{
											if ((ColCNT>3 && TotC>5) || TotC>6){
												TotI.append(";");
												TotC=0;
												ColCNT=1;
											}
										}

									}else{//不一樣的顏色
										String iUnit=cs.getString(2);
										if (cs.getInt(10)>1 && (cs.getInt(2) % cs.getInt(10)==0)) iUnit=""+(cs.getInt(2)/cs.getInt(10))+"手";
										TotI.append(cs.getString(6)+cs.getString(7)+"("+iUnit+")");
										TotC=TotC+1;
										mCOL=cs.getString(6) ;
										ColCNT=ColCNT+1;
										//童裝特殊屬性,還要印(手)
										if (Utilis.toInt(cs.getString(10))>1){
											if (ColCNT>=3){
												TotI.append(";");
												TotC=0;
												ColCNT=1;
											}

										}else{
											if ((ColCNT>3 && TotC>5) || TotC>6){
												TotI.append(";");
												TotC=0;
												ColCNT=1;
											}
										}
									}
									TotU=TotU+cs.getInt(2);
									TotS=TotS+cs.getDouble(4);
								}
								cs.moveToNext();
							}

							if (TotI.toString().length()>0){

								if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("2")){
									Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+mGOODS+"-"+mGOODN+sepa);
								}else if (barStr[8].equalsIgnoreCase("4") || barStr[8].equalsIgnoreCase("5")){
									Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+mSOURCENO+"-"+mGOODN+sepa);
								}else{
									Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+mGOODS+"-"+mGOODN+sepa);
								}

								if (!barStr[8].equalsIgnoreCase("1") && !barStr[8].equalsIgnoreCase("3")){
									sPrintB.append("AZ,"+(392-(""+(int)TotU).length()*10)+","+Loca+",1,2,0,0,"+TotU+sepa);
									sPrintB.append("AZ,"+(460-(mUNITP).length()*10)+","+Loca+",1,2,0,0,x"+mUNITP+sepa);
									if((nKind==20 || nKind==21) && devStr[8] !=null && devStr[8].length()>0 ) ;else sPrintB.append("AZ,"+(550-(new BigDecimal(TotS).toString()).length()*10)+","+Loca+",1,2,0,0,"+new BigDecimal(TotS).toString()+sepa);
									sPrintB.append("AZ,580,"+Loca+",1,2,0,0,"+mPS+sepa);
									Loca=Loca+50;
								}else{
									sPrintB.append("AZ,"+(392-(""+(int)TotU).length()*10)+","+Loca+",1,1,0,0,"+TotU+sepa);
									sPrintB.append("AZ,"+(460-(mUNITP).length()*10)+","+Loca+",1,1,0,0,x"+mUNITP+sepa);
									if((nKind==20 || nKind==21) && devStr[8] !=null && devStr[8].length()>0) ;else sPrintB.append("AZ,"+(550-(new BigDecimal(TotS).toString()).length()*10)+","+Loca+",1,1,0,0,"+new BigDecimal(TotS).toString()+sepa);
									sPrintB.append("AZ,580,"+Loca+",1,1,0,0,"+mPS+sepa);
									Loca=Loca+25;
								}
								String iList[]=TotI.toString().split(";");
								if (barStr[8].equalsIgnoreCase("0") || barStr[8].equalsIgnoreCase("1") || barStr[8].equalsIgnoreCase("4")){
									for (int ij=0;ij<iList.length;ij++){
										if (Utilis.toInt(compStr[8])==31 || Utilis.toInt(compStr[8])==41){
											sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+iList[ij]+sepa);
											if (ij<(iList.length-1)) Loca=Loca+50;
										}else{
											sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+iList[ij]+sepa);
											if (ij<(iList.length-1)) Loca=Loca+25;
										}
									}
								}else{
									Loca=Loca-15;
								}
								sPrintB.append("AZ,30,"+(Loca+10)+",1,2,0,0,"+"------------------------------------------------"+sepa);
							}
						}
						if (Loca>50) printEndg(xList, jj,0,0);
						//StartToPrint();
						if (jj<(xList.length-1)) SystemClock.sleep(3000);
					}
					db.close();
					cs.close();
				}
			}


		}

		if (Ptitle!=0){ //印名條
			SystemClock.sleep(3000);
			PosUtilis.startPostPrint(QPosSave.this,data.mUser,compStr);
		}


	}

	private void printTitle(String [] xList, int Rjj,int mode){
		Loca=20;
		sPrintB.delete(0, sPrintB.toString().length());

		sPrintB.append("^QXXXX,0,0"+sepa);
		sPrintB.append("^W80"+sepa+"^H7"+sepa+"^P1"+sepa+"^S4"+sepa+"^AD"+sepa+"^C1"+sepa+"^R0"+sepa+"~Q+0"+sepa
				+"^O0"+sepa+"^D0"+sepa+"^E11"+sepa+"~R20"+sepa+"^L"+sepa+"Dy2-me-dd"+sepa+"Th:m:s"+sepa);

		Loca=Loca+2;sPrintB.append("Y10,"+Loca+",STYLEKEY"+sepa);
		Loca=Loca+210;sPrintB.append("AZ,400,"+Loca+",1,2,0,0,"+data.mFormdate+xList[Rjj]+sepa);
		sPrintB.append("AZ,30,"+Loca+",1,1,0,0,單號:"+data.mFno+sepa);
		Loca=Loca+30;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+"經手: "+data.mEmpid+"/"+data.mEmpname+sepa);
		if (data.dOrdcard>0 || data.dOrdcash>0){
			Loca=Loca+35;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,會員:"+data.mUser+" / "+data.mUsername+" 訂購"+sepa);
		}else{
			Loca=Loca+35;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,會員:"+data.mUser+" / "+data.mUsername+sepa);
		}
		String TitleN=TitleName;
		if (data.mIsconfirm.equalsIgnoreCase("N")){
			if (data.mIscheck.equalsIgnoreCase("Y"))
				TitleN+="-預購單";
			else
				TitleN+="-暫存單";
		}else if (data.mIscheck.equalsIgnoreCase("Y")){
			if (data.mIsconfirm.equalsIgnoreCase("O"))
				TitleN+="-預購單";
			else if (data.mIsconfirm.equalsIgnoreCase("R"))
				TitleN+="-留貨單";
		}
		int leftT=400;if (TitleN.length()<4)leftT=520;
		sPrintB.append("AZ,"+leftT+","+Loca+",1,2,0,0,"+TitleN+sepa);
		//印單別

		String PS="TR_"+data.mhPs1+" PS_"+data.mhPs;
		if ((data.mhPs1!=null && data.mhPs1.length()>0) || (data.mhPs!=null && data.mhPs.length()>0))	{Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+PS+sepa);} else Loca=Loca+20;
		PS="M_"+mMobil+" A_"+mAddress;
		if ((mMobil!=null && mMobil.length()>0) || (mAddress!=null && mAddress.length()>0) ){Loca=Loca+30;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+PS+sepa);}
		Loca=Loca+2;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,------------------------------------------------"+sepa);
		Loca=Loca+6;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,------------------------------------------------"+sepa);
		Loca=Loca+37;

		if (nKind!=20 && nKind!=21){
			sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"(+)應收:"+new BigDecimal(data.dAsum+data.dTrcash).toString()+sepa);

			sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(=)前帳:"+new BigDecimal(owlLast*-1).toString()+sepa);

			if (data.dCash!=0 || data.dCard!=0){
				Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(-)現收:"+new BigDecimal(data.dCash).toString()+sepa);
				sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"(-)票刷:"+new BigDecimal(data.dCard).toString()+sepa);
			}
			if (data.dTrcash!=0 || data.dAtm!=0){
				Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(+)代運:"+data.dTrcash+sepa);
				sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"(-)匯款:"+new BigDecimal(data.dAtm).toString()+sepa);
			}
			if (data.dOrdcash!=0 || data.dOrdcard!=0){
				Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(-)定金:"+new BigDecimal(data.dOrdcash).toString()+sepa);
				sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"(-)票刷:"+new BigDecimal(data.dOrdcard).toString()+sepa);
			}

			Loca=Loca+25;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"--------------------------------------------------"+sepa);
			Loca=Loca+40;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(=)未結:"+new BigDecimal(owlTot*-1).toString()+sepa);
			sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"數量("+data.mHandno+"件)");
			if (data.dDismoney!=0) sPrintB.append("(折)"+data.dDismoney+")"+sepa); else sPrintB.append(sepa);
		}else{
			if (devStr[8] !=null && devStr[8].length()>0 && !devStr[8].equalsIgnoreCase("0"))
				sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(+)應收:(略)"+sepa);
			else
				sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"(+)應收:"+new BigDecimal(data.dAsum+data.dTrcash).toString()+sepa);

			sPrintB.append("AZ,290,"+Loca+",1,2,0,0,"+"數量("+data.mHandno+"件)"+sepa);
		}

		Loca=Loca+21;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,------------------------------------------------"+sepa);
		Loca=Loca+6;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,------------------------------------------------"+sepa);

		Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,貨號/品名"+sepa);
		sPrintB.append("AZ,342,"+Loca+",1,1,0,0,數量"+sepa);
		sPrintB.append("AZ,435,"+Loca+",1,1,0,0,單價"+sepa);
		sPrintB.append("AZ,520,"+Loca+",1,1,0,0,小計"+sepa);
		sPrintB.append("AZ,580,"+Loca+",1,1,0,0,註"+sepa);
		sPrintB.append("AZ,30,"+(Loca-2)+",1,2,0,0,------------------------------------------------"+sepa);
		Loca=Loca+5;
	}

	private void printEndg(String [] xList, int Rjj,int page,int SB){

		if (SB==1 ){//一定(Utilis.toInt(compStr[8])==4
			Loca=Loca+50;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,印製:"+DateUtil.getFormatDate());
			sPrintB.append("__ 第 "+page+" 頁 __ 續 __"+sepa);
			Loca=Loca+35;sPrintB.append("AZ,30,"+(Loca-2)+",1,2,0,0,------------------------------------------------"+sepa);
			Loca=Utilis.toInt(compStr[7]);
			StartToPrint();
		}else{
			if (Utilis.toInt(compStr[8])==4){
				if (Loca>(Utilis.toInt(compStr[7])-endLeng)) { Loca=Utilis.toInt(compStr[7]);StartToPrint();}
			}
			Loca=Loca+30;
			if ((compStr[3]+compStr[4]).length()>0)	{Loca=Loca+65;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+"TEL:"+compStr[3]+compStr[4]+sepa);}
			if (compStr[2].length()>0){ Loca=Loca+30;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+"地址:"+compStr[2]+sepa);}
			if (compStr[1].length()>0){Loca=Loca+30;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,"+compStr[1]+sepa);}
			Loca=Loca+30;sPrintB.append("AZ,30,"+Loca+",1,1,0,0,印製:"+DateUtil.getFormatDate()+" 開單:"+data.mTime+sepa);
			if (devStr[0].length()>0)	{Loca=Loca+40;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[0]+sepa);}
			if (devStr[1].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[1]+sepa);}
			if (devStr[2].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[2]+sepa);}
			if (devStr[3].length()>0)	{Loca=Loca+30;sPrintB.append("AZ,8,"+Loca+",1,1,0,0,"+devStr[3]+sepa);}

			Loca=Loca+110;sPrintB.append("AZ,30,"+Loca+",1,2,0,0,"+"簽收人:__________________________________"+sepa);

			if (Utilis.toInt(compStr[8])==4)	Loca=Utilis.toInt(compStr[7]);
			else Loca=Loca+90;
			StartToPrint();
			//connetNet("192.168.1.97" );
		}
	}

	@SuppressWarnings("unused")
	private void StartToPrint(){

		String lenS="0000"+(int)(Loca/7.2);

		sPrintB.replace(2, 6,lenS.substring(lenS.length()-4,lenS.length()));
		sPrintB.append("E"+sepa);

		String address = Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 4);// 連線的ip
		int port =Utilis.toInt(Utilis.getIni(QPosSave.this, "SYS","BAR_IP", 5));// 連線的port

		int success=1;

		InetAddress serverAddr = null;
		SocketAddress sc_add = null;
		Socket socket = null;

		try {
			serverAddr = InetAddress.getByName(address);
			sc_add= new InetSocketAddress(serverAddr,port);
			socket = new Socket();
			socket.connect(sc_add,2000);

			DataOutputStream out = new DataOutputStream(socket.getOutputStream());
			byte data[] = new String(sPrintB.toString().getBytes("UTF8")).getBytes("BIG5");
			out.write(data, 0, data.length);
			out.flush();
			socket.close();
			Toast.makeText(QPosSave.this, "已成功傳送資訊到條碼機!!", Toast.LENGTH_LONG).show();
			success=1;
			SystemClock.sleep(2000);
		} catch (UnknownHostException e) {
			success=0;
		} catch (SocketException e) {
			success=0;
		} catch(IOException e) {
			success=0;
		}
		sPrintB.delete(0, sPrintB.toString().length());
		sPrintB.append("^QXXXX,0,0"+sepa);
		if (Utilis.toInt(compStr[8])<=2) sPrintB.append("^W56"); else sPrintB.append("^W80");
		sPrintB.append(sepa+"^H7"+sepa+"^P1"+sepa+"^S4"+sepa+"^AD"+sepa+"^C1"+sepa+"^R0"+sepa+"~Q+0"+sepa
				+"^O0"+sepa+"^D0"+sepa+"^E11"+sepa+"~R20"+sepa+"^L"+sepa+"Dy2-me-dd"+sepa+"Th:m:s"+sepa);
		Loca=0;
	}



	private void maintainQItems(){

		SQLiteDatabase db = Utilis.getDB(QPosSave.this);
		StringBuffer mBuf = new StringBuffer();
		
		mBuf.append(" update "+TbName.QITEMS).append(" set ");
		mBuf.append(" ISCHECK='"+data.mIscheck+"' ,ISCONFIRM='"+data.mIsconfirm+"'");
		if (data.mIscheck.equalsIgnoreCase("Y") && data.mIsconfirm.equalsIgnoreCase("N") &&  (nKind==30 || nKind==31)){ //一律將數量UNIT??複製到UNIT
			mBuf.append(" ,UNIT=UNIT"+mKind.substring(0,1)+"0");
		}else{
			mBuf.append(" ,UNIT=0");
		}
		mBuf.append(", FORMDATE='"+data.mFormdate+"', DEPTNO='"+data.mDeptno+"'");
		mBuf.append(" where  FORMNO= '"+mFno+"' and QKIND = '"+mKind+"'");
		db.execSQL(mBuf.toString());

		db.close();

	}
}
