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.AppConstants; import com.topsun.posclient.common.LoggerUtil; import com.topsun.posclient.common.POSException; import com.topsun.posclient.common.ProjectUtil; 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.IReturnedService; import com.topsun.posclient.sales.core.service.impl.ReturnedServiceImpl; import com.topsun.posclient.sales.ui.gold.CommonFacade; import com.topsun.posclient.sales.ui.gold.PartSalesManager; import com.topsun.posclient.sales.ui.gold.ReturnedFacade; 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 ExechangePayDialog extends Dialog{ int buttonWidth = 80; int buttonHight = 45; CashierModelItemCellModify cashierModelItemCellModify; IReturnedService returnedService = new ReturnedServiceImpl(); public TableViewer tableViewer; private List<CashierModel> cashierModels ; private String price; public Text cashBack; public PartSales partSales;//退换货信息 private Text tradeOrder; public Text factTotalAmount;// 实收金额 public ExechangePayDialog(Shell parent) { super(parent); } protected void okPressed() { Object data = getTableViewer().getInput(); if(data instanceof List){ List<CashierModel> cashierModels = (List<CashierModel>)data; partSales.setCashierModelList(cashierModels); } //如果是换货,将发票查出的原始单品的单品状态设置为换货 for(Item item : partSales.getItemList()){ if(item.getItemType().equals(AppConstants.ITEM_TYPE_RETURNED)){ item.setItemStatus(AppConstants.ITEM_STATU_EXCHANGE); } } try { partSales.setCashBackAmount(ProjectUtil.formatString(cashBack.getText())); returnedService.checkPartSalesDocNum(partSales); returnedService.saveReturnedData(partSales); ReturnedFacade.restReturnedView(); } catch (POSException e1) { if(null != e1.getErrorCode() && e1.getErrorCode().equals("15")){//错误码=15,单据号重复,不保存且不重置表单 MessageDialog.openError(this.getShell(), "错误", e1.getErrorMessage()); return; }else{ try { ReturnedFacade.restReturnedView(); MessageDialog.openError(this.getShell(), "错误", e1.getErrorMessage()); super.okPressed(); return; } catch (POSException e) { } super.okPressed(); return; } } this.close(); super.okPressed(); } @Override protected void configureShell(Shell newShell) { newShell.setText("结算"); super.configureShell(newShell); } 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.setLayoutData(data); label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); 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(partSales.getDocNum()); } { Button btn = new Button(compsite, SWT.NONE); GridData data = new GridData(); data.widthHint = buttonWidth; data.heightHint = buttonHight; btn.setFont(CommonCss.getPlusButtonFont(org.eclipse.swt.widgets.Display.getCurrent())); btn.setLayoutData(data); 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); } public void widgetDefaultSelected(SelectionEvent e) { } }); } { 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("合计金额:"); } { 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()); } { 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()); cashierModelItemCellModify.setFactTotalAmount(factTotalAmount); 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 = returnedService.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); label.setFont(CommonCss.getDefaultFont(parent.getDisplay())); GridData data = new GridData(GridData.FILL_HORIZONTAL); label.setLayoutData(data); label.setText("现金找补:"); } { 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("0.00"); } 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 button = new Button(parent, SWT.NONE); button.setFont(CommonCss.getDefaultFont(parent.getDisplay())); button.setText("确认"); GridData data = new GridData(); data.widthHint = buttonWidth; data.heightHint = buttonHight; button.setLayoutData(data); button.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent e) { Button printBtn = (Button)e.getSource(); 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()); } //合计金额小于0,换货要退钱 if(count.compareTo(new BigDecimal(0)) == -1){ if(payCount.subtract(count).compareTo(new BigDecimal(0)) != 0){ MessageDialog.openError(printBtn.getShell(), "错误", "收款金额必须等于合计金额!"); return; } }else{//合计金额大于零,客户换货补差价 if(payCount.compareTo(count) == -1){ MessageDialog.openError(printBtn.getShell(), "错误", "收款金额不能小于合计金额!"); return; } } partSales.setCashierModelList(cmList); partSales.setCountAmount(ProjectUtil.formatString(price)); partSales.setFactTotalAmount(partSales.getCountAmount()); try { partSales.setDocNum(tradeOrder.getText()); returnedService.checkPartSalesDocNum(partSales); } catch (POSException e1) { MessageDialog.openWarning(printBtn.getShell(), "提示", e1.getErrorMessage()); return; } PartSales pPartSales = new PartSalesPrintData().convertPartSalesForChaiPiao(partSales); Item firstItem = pPartSales.getItemList().get(0); PrintConfirmDialog printDialog = new PrintConfirmDialog(printBtn.getShell()); printDialog.setPartSales(partSales); printDialog.setPrice(price); printDialog.setTotalPage(CommonFacade.getTotalPageFromPartSales(pPartSales)); printDialog.setIndexPage(1); printDialog.setItemName(firstItem.getItemName()); printDialog.setCurrentInvoiceAmount(new PartSalesPrintData().getPrintInvoiceAmountForExechange(firstItem, partSales, 1)); printDialog.setOrderBean(new PartSalesPrintData().getOrderBeanPrintData(1, "个人", partSales)); try{ Invoice currentInvoice = returnedService.getInvoice(); if(null == currentInvoice){ MessageDialog.openError(printBtn.getShell(), "错误", "发票已用完,请同步发票数据"); return; } printDialog.setCurrentInvoice(currentInvoice); printDialog.setNextInvoiceNumber(currentInvoice.getNextFPH()); 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(printBtn.getShell(), "错误", "打印出错"); return; } partSales = printDialog.getPartSales(); PartSales newPartSales = printDialog.getPartSales(); partSales.setRetailFPList(newPartSales.getRetailFPList()); okPressed(); okPressed(); } public void widgetDefaultSelected(SelectionEvent e) { } }); } GridData btnLayout = new GridData(); btnLayout.widthHint = buttonWidth; btnLayout.heightHint = buttonHight; 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); } public final PartSales getPartSales() { return partSales; } public final void setPartSales(PartSales partSales) { this.partSales = partSales; } 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; } }