package com.topsun.posclient.sales.dialog; import java.math.BigDecimal; import java.math.MathContext; import java.text.NumberFormat; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.CellEditor; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TextCellEditor; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.Text; import com.topsun.posclient.common.LoggerUtil; import com.topsun.posclient.common.POSClientApp; import com.topsun.posclient.common.POSException; import com.topsun.posclient.common.ProjectUtil; import com.topsun.posclient.common.service.IBaseService; import com.topsun.posclient.common.service.impl.BaseServiceImpl; import com.topsun.posclient.common.ui.style.CommonCss; import com.topsun.posclient.datamodel.CashierModel; import com.topsun.posclient.datamodel.Invoice; import com.topsun.posclient.datamodel.Item; import com.topsun.posclient.datamodel.PartSales; import com.topsun.posclient.datamodel.dto.CashierModeDTO; import com.topsun.posclient.sales.MessageResources; import com.topsun.posclient.sales.core.PartSalesPrintData; import com.topsun.posclient.sales.core.service.IGoldSalesService; import com.topsun.posclient.sales.core.service.impl.GoldSalesServiceImpl; import com.topsun.posclient.sales.ui.gold.CommonFacade; import com.topsun.posclient.sales.ui.gold.GoldSalesFacade; import com.topsun.posclient.sales.ui.gold.GoldSalesManager; import com.topsun.posclient.sales.ui.gold.PartSalesManager; import com.topsun.posclient.sales.ui.menu.SalesActivator; import com.topsun.posclient.sales.ui.table.CashierModelItemCellModify; import com.topsun.posclient.sales.ui.table.CashierModelTableContentProvider; import com.topsun.posclient.sales.ui.table.CashierModelTableLableProvider; /** * * 投资金条预订-结算对话框 * @author Dong * */ public class GoldSalesPayDialog extends Dialog{ int buttonWidth = 80; int buttonHight = 45; IGoldSalesService goldSalesService = new GoldSalesServiceImpl(); CashierModelItemCellModify cashierModelItemCellModify; IBaseService commonService = new BaseServiceImpl(); public TableViewer tableViewer; private List<CashierModel> cashierModels ; private String price; public Text cashBack; public String docNum; public PartSales partSales;//投资金条现货信息 public Text invoiceNumber;//发票号码 public Text payCompany;//付款单位 // public Text custDocNum;//手工单号 private Text tradeOrder; public final PartSales getPartSales() { return partSales; } public final void setPartSales(PartSales partSales) { this.partSales = partSales; } public String getDocNum() { return docNum; } public void setDocNum(String docNum) { this.docNum = docNum; } public List<CashierModel> getCashierModels() { return cashierModels; } public void setCashierModels(List<CashierModel> cashierModels) { this.cashierModels = cashierModels; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public GoldSalesPayDialog(Shell parent) { super(parent); } @Override protected void okPressed() { BigDecimal count = ProjectUtil.formatString(price); BigDecimal payCount = new BigDecimal(0); List<CashierModel> cmList = (List<CashierModel>)getTableViewer().getInput(); for(CashierModel cmodel : cmList){ payCount = payCount.add(cmodel.getAmount()); } if(payCount.compareTo(count) == -1){ MessageDialog.openError(this.getShell(), "错误", "收款金额不能小于合计金额"); return; } partSales.setCashierModelList(cmList); partSales.setInvoiceCode(POSClientApp.get().getSysConfig().getInvoiceCode()); partSales.setInvoiceNumber(POSClientApp.get().getSysConfig().getInvoiceNumber()); partSales.setCountAmount(count); partSales.setDocNum(tradeOrder.getText()); try { goldSalesService.checkPartSalesDocNum(partSales); } catch (POSException e1) { MessageDialog.openWarning(this.getShell(), "提示", "单据号校验失败,请修改"); return; } PartSales pPartSales = new PartSalesPrintData().convertPartSalesForChaiPiao(partSales); Item firstItem = pPartSales.getItemList().get(0); PrintConfirmDialog printDialog = new PrintConfirmDialog(this.getShell()); printDialog.setPartSales(pPartSales); printDialog.setPrice(price); printDialog.setTotalPage(CommonFacade.getTotalPageFromPartSales(pPartSales)); printDialog.setIndexPage(1); printDialog.setItemName(firstItem.getItemName()); printDialog.setCurrentInvoiceAmount(new PartSalesPrintData().getPrintInvoiceAmount(firstItem, pPartSales, 1)); printDialog.setOrderBean(new PartSalesPrintData().getOrderBeanPrintData(1, "个人", pPartSales)); try{ Invoice currentInvoice = goldSalesService.getInvoice(); if(null == currentInvoice){ MessageDialog.openError(this.getShell(), "错误", "发票已用完,请同步发票数据"); return; } printDialog.setCurrentInvoice(currentInvoice); printDialog.setCurrentInvoiceNumber(currentInvoice.getFPH()); printDialog.setCurrentInvoiceCode(currentInvoice.getFPDM()); PartSalesManager.getInstance().setPrintNum(0); int retrunType =printDialog.open(); if(retrunType == 1){ return; } }catch(Exception ee){ LoggerUtil.logError(SalesActivator.PLUGIN_ID, "打印出错", ee); MessageDialog.openError(this.getShell(), "错误", "打印出错"); return; } partSales = printDialog.getPartSales(); PartSales newPartSales = printDialog.getPartSales(); partSales.setRetailFPList(newPartSales.getRetailFPList()); partSales.setCashBackAmount(ProjectUtil.formatString(cashBack.getText())); try { goldSalesService.saveGoldSalesData(partSales); // MessageDialog.openInformation(this.getShell(), "成功", "交易成功"); GoldSalesFacade.restGoldSalesView(); GoldSalesManager.getInstance().getGoldSalesBtn().setEnabled(true); GoldSalesManager.getInstance().getGoldBookingBtn().setEnabled(true); GoldSalesManager.getInstance().getGoldTimeoutBtn().setEnabled(true); GoldSalesManager.getInstance().getQueryBtn().setEnabled(true); GoldSalesManager.getInstance().getAddBookingBtn().setEnabled(true); GoldSalesManager.getInstance().getAddCurrentProductBtn().setEnabled(true); } catch (POSException e1) { if(null != e1.getErrorCode() && e1.getErrorCode().equals("15")){//错误码=15,单据号重复,不保存且不重置表单 MessageDialog.openError(this.getShell(), "错误", e1.getErrorMessage()); return; }else{ MessageDialog.openError(this.getShell(), "错误", e1.getErrorMessage()); try { GoldSalesFacade.restGoldSalesView(); } catch (POSException e) { MessageDialog.openError(this.getShell(), "错误", e.getErrorMessage()); this.close(); } this.close(); } } close(); } @Override protected void configureShell(Shell newShell) { newShell.setText("结算"); super.configureShell(newShell); } @Override protected Control createContents(Composite parent) { Composite compsite = new Composite(parent, SWT.BORDER); compsite.setLayout(new GridLayout(3, false)); GridData layoutData = new GridData(GridData.FILL_BOTH); compsite.setLayoutData(layoutData); { Label label = new Label(compsite, SWT.NONE); GridData data = new GridData(GridData.FILL_HORIZONTAL); label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); label.setLayoutData(data); label.setText("交易单号(F6):"); } { tradeOrder = new Text(compsite, SWT.BORDER); GridData data = CommonCss.getDefaultTextData(); tradeOrder.setFont(CommonCss.getDefaultFont(parent.getDisplay())); tradeOrder.setLayoutData(data); tradeOrder.setEditable(false); tradeOrder.setText(docNum); } { Button btn = new Button(compsite, SWT.NONE); // btn.setFont(CommonCss.getDefaultFont(parent.getDisplay())); GridData data = new GridData(); data.widthHint = buttonWidth; data.heightHint = buttonHight; btn.setLayoutData(data); btn.setFont(CommonCss.getPlusButtonFont(org.eclipse.swt.widgets.Display.getCurrent())); btn.setText("+"); btn.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { String tradeOrderNum = tradeOrder.getText(); String subStr = tradeOrderNum.substring(tradeOrderNum.length()-4,tradeOrderNum.length()); NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMinimumIntegerDigits(4); formatter.setGroupingUsed(false); BigDecimal bigDecimal = new BigDecimal(subStr,MathContext.DECIMAL32); BigDecimal value = bigDecimal.add(new BigDecimal(1)); String regex = "^([0]+)([\\d]*)"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(new StringBuffer(subStr)); String zeroPrefix = ""; if(matcher.find()) { zeroPrefix = matcher.group(1); subStr = matcher.group(2);//除0后的值 } String prifix = tradeOrderNum.substring(0,tradeOrderNum.length()-4); if(value.compareTo(new BigDecimal(9999)) == 1){ MessageDialog.openError(tableViewer.getTable().getShell(), "错误", "单据号允许的最大序号为9999"); return; } String setStr = prifix + formatter.format(value); tradeOrder.setText(setStr); // custDocNum.setText(setStr); } public void widgetDefaultSelected(SelectionEvent e) { } }); } // { // Label label = new Label(compsite, SWT.NONE); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // label.setLayoutData(data); // label.setText("手工单号(F5):"); // } // { // custDocNum = new Text(compsite, SWT.BORDER); // GridData data = CommonCss.getDefaultTextData(); // data.horizontalSpan = 2; // custDocNum.setLayoutData(data); // custDocNum.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // custDocNum.setText(docNum); // } { Label label = new Label(compsite, SWT.NONE); GridData data = new GridData(GridData.FILL_HORIZONTAL); label.setLayoutData(data); label.setText("合计金额:"); label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); } { Text text = new Text(compsite, SWT.BORDER); GridData data = CommonCss.getDefaultTextData(); data.horizontalSpan = 2; text.setLayoutData(data); text.setFont(CommonCss.getDefaultFont(parent.getDisplay())); text.setEditable(false); text.setText(getPrice()); } // { // Label label = new Label(compsite, SWT.NONE); // label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setLayoutData(data); // label.setText("发票号码:"); // } // { // invoiceNumber = new Text(compsite, SWT.BORDER); // GridData data = CommonCss.getDefaultTextData(); // data.horizontalSpan = 2; // invoiceNumber.setLayoutData(data); // invoiceNumber.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // invoiceNumber.setText(POSClientApp.get().getSysConfig().getInvoiceNumber()); // } // { // Label label = new Label(compsite, SWT.NONE); // label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setLayoutData(data); // label.setText("付款单位:"); // } // { // payCompany = new Text(compsite, SWT.BORDER); // GridData data = CommonCss.getDefaultTextData(); // data.horizontalSpan = 2; // payCompany.setLayoutData(data); // payCompany.setFont(CommonCss.getDefaultFont(parent.getDisplay())); // payCompany.setText("个人"); // } // { // Label label = new Label(compsite, SWT.NONE); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setLayoutData(data); // label.setText("积分抵扣:"); // } // // { // Text text = new Text(compsite, SWT.BORDER); // GridData data = CommonCss.getDefaultTextData(); // data.horizontalSpan = 2; // text.setLayoutData(data); // } // // { // Label label = new Label(compsite, SWT.NONE); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setLayoutData(data); // label.setText("积分抵扣:"); // } // // { // Text text = new Text(compsite, SWT.BORDER); // GridData data = CommonCss.getDefaultTextData(); // text.setLayoutData(data); // } // // { // Label label = new Label(compsite, SWT.NONE); // GridData data = new GridData(GridData.FILL_HORIZONTAL); // label.setLayoutData(data); // label.setText("元"); // } { tableViewer = new TableViewer(compsite,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION); tableViewer.setContentProvider(new CashierModelTableContentProvider()); tableViewer.setLabelProvider(new CashierModelTableLableProvider()); String[] cloumsProperties = new String[]{"typeName","amount","cardno"}; tableViewer.setColumnProperties(cloumsProperties); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 3; data.heightHint = 200; Table table = tableViewer.getTable(); table.setFont(CommonCss.getDefaultFont(parent.getDisplay())); CellEditor[] editors = new CellEditor[3]; editors[0] = new TextCellEditor(table); editors[1] = new TextCellEditor(table); editors[2] = new TextCellEditor(table); tableViewer.setCellEditors(editors); table.setLayoutData(data); cashierModelItemCellModify = new CashierModelItemCellModify(tableViewer,cashBack,getPrice()); tableViewer.setCellModifier(cashierModelItemCellModify); table.setLinesVisible(false); table.setHeaderVisible(true); { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("收银方式(F3)"); column.setWidth(100); } { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("收款金额"); column.setWidth(100); } { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("卡号"); column.setWidth(170); } List<CashierModel> cashierModeList = null; try { CashierModeDTO dto = commonService.getAllCashierMode(); if(null != dto){ cashierModeList = dto.getCashierModeList(); } } catch (POSException e) { e.printStackTrace(); MessageDialog.openError(parent.getShell(), MessageResources.message_ui_tips, e.getErrorMessage()); } tableViewer.setInput(cashierModeList); } { Label label = new Label(compsite, SWT.NONE); GridData data = new GridData(GridData.FILL_HORIZONTAL); label.setLayoutData(data); label.setText("现金找补:"); label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); } { cashBack = new Text(compsite, SWT.BORDER); GridData data = CommonCss.getDefaultTextData(); data.horizontalSpan = 2; cashBack.setLayoutData(data); cashBack.setFont(CommonCss.getDefaultFont(parent.getDisplay())); cashBack.setEditable(false); cashBack.setText(""); } cashierModelItemCellModify.setCashBack(cashBack); return super.createContents(parent); } public TableViewer getTableViewer() { return tableViewer; } public void setTableViewer(TableViewer tableViewer) { this.tableViewer = tableViewer; } protected void createButtonsForButtonBar(Composite parent) { int buttonWidth = 80; int buttonHight = 45; parent.setLayout(new GridLayout(5,false)); Button okbtn = createButton(parent, IDialogConstants.OK_ID, "确认",true); okbtn.setFont(CommonCss.getDefaultFont(parent.getDisplay())); GridData btnLayout = new GridData(); btnLayout.widthHint = buttonWidth; btnLayout.heightHint = buttonHight; okbtn.setLayoutData(btnLayout); Button cancelBtn = createButton(parent, IDialogConstants.CANCEL_ID, "取消", false); cancelBtn.setFont(CommonCss.getDefaultFont(parent.getDisplay())); cancelBtn.setLayoutData(btnLayout); } protected Button createButton(Composite parent, int id, String label, boolean defaultButton) { return super.createButton(parent, id, label, defaultButton); } }