package com.topsun.posclient.sales.dialog;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.NumberFormat;
import java.util.ArrayList;
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.KeyEvent;
import org.eclipse.swt.events.KeyListener;
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.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.VipUser;
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.SalesDataUtil;
import com.topsun.posclient.sales.core.service.IPartSaleService;
import com.topsun.posclient.sales.core.service.impl.PartSaleServiceImpl;
import com.topsun.posclient.sales.ui.gold.CommonFacade;
import com.topsun.posclient.sales.ui.gold.PartSalesFacade;
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 yujie creator
*
*/
public class SalesPayDialog extends Dialog{
int buttonWidth = 80;
int buttonHight = 45;
CashierModelItemCellModify cashierModelItemCellModify;
IPartSaleService partSaleService = new PartSaleServiceImpl();
public TableViewer tableViewer;
private List<CashierModel> cashierModels ;
public String price;//合计金额
// public Text custDocNum;//手工单号
public Text totalAmount;// 整单金额
public Text factTotalAmount;// 实收金额
public Text cashBack;//现金找补
public PartSales partSales = PartSalesManager.getInstance().getPartSales();//信息
public Text salesOffPoint;//折扣
public Text delZero;//抹零
private Text tradeOrder;//交易单号
private Text poiontAmount;//积分抵扣金额
private Text usePoint;//积分抵扣
private String cardNo;//会员卡号
private BigDecimal salesValue;//折扣值
protected void configureShell(Shell newShell) {
newShell.setText("结算");
super.configureShell(newShell);
}
/*
* @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
*/
protected void cancelPressed() {
super.cancelPressed();
PartSales newPartSales = PartSalesManager.getInstance().getBeforeZeroAndDiscountPartSales();
newPartSales.setMlimit(false);
newPartSales.setMratio(false);
newPartSales.setDiscountToZero(new BigDecimal(0));
newPartSales.setDiscountRate(new BigDecimal(1));
newPartSales.setDiscountAccount(0);
newPartSales.setDiscountAmount(new BigDecimal(0));
PartSalesManager.getInstance().setPartSales(newPartSales);
}
protected void okPressed() {
BigDecimal count = ProjectUtil.formatString(factTotalAmount.getText());
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;
}
int printNum = PartSalesManager.getInstance().getPrintNum();
int needPrintNum = SalesDataUtil.getNeedPrintNum(partSales);
if(printNum < needPrintNum){
MessageDialog.openError(this.getShell(), "错误", "请先打印发票");
return;
}
partSales.setCashierModelList(cmList);
partSales.setDocNum(tradeOrder.getText());
partSales.setCashBackAmount(ProjectUtil.formatString(cashBack.getText()));
if(null != usePoint && null != usePoint.getText()){
partSales.setUsePoint(ProjectUtil.formatString(usePoint.getText()).intValue());//设置使用积分
}
try {
partSaleService.savePartSaleData(partSales);
} 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 {
PartSalesFacade.restSalesView();
} catch (POSException e) {
MessageDialog.openError(this.getShell(), "错误", e.getErrorMessage());
return;
}
super.okPressed();
return;
}
}
try {
if(partSales.isVip()){
VipUser vipuser = partSales.getVipUser();
BigDecimal oldPoint = vipuser.getPoint();
BigDecimal usePointVal = ProjectUtil.formatString(usePoint.getText());
oldPoint = oldPoint.subtract(usePointVal);
vipuser.setPoint(oldPoint);
PartSalesFacade.restSalesView();
}
PartSalesFacade.restSalesView();
} catch (POSException e) {
try {
PartSalesFacade.restSalesView();
} catch (POSException e1) {
}
MessageDialog.openError(this.getShell(), "错误", e.getErrorMessage());
super.okPressed();
}
super.okPressed();
}
@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);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData(GridData.FILL_HORIZONTAL);
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(partSales.getDocNum());
}
{
Button btn = new Button(compsite, SWT.NONE);
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() {
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);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("整单金额:");
}
{
totalAmount = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
data.horizontalSpan = 2;
totalAmount.setLayoutData(data);
totalAmount.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
totalAmount.setEditable(false);
totalAmount.setText(price);
}
{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("折扣金额:");
}{
salesOffPoint = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
data.horizontalSpan = 2;
salesOffPoint.setLayoutData(data);
salesOffPoint.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
salesOffPoint.setEditable(false);
salesOffPoint.setText("0.00");
}{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("抹零金额:");
}{
delZero = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
data.horizontalSpan = 2;
delZero.setLayoutData(data);
delZero.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
delZero.setEditable(false);
delZero.setText("0.00");
}
if(partSales.isVip()){
{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("积分抵扣:");
}
{
usePoint = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
data.horizontalSpan = 1;
usePoint.setLayoutData(data);
usePoint.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
usePoint.setText("0.00");
usePoint.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CR) {
String point = usePoint.getText();
if(null == point || "".equals(point)){
MessageDialog.openError(((Text)e.getSource()).getShell(), "错误", "【抵扣积分】不能为空");
usePoint.setFocus();
return;
}
if(new BigDecimal(point).compareTo(new BigDecimal(0)) == -1){
MessageDialog.openError(((Text)e.getSource()).getShell(), "错误", "【抵扣积分】不能小于零");
usePoint.setFocus();
return;
}
BigDecimal pointValue = partSales.getVipUser().getPoint();
if(new BigDecimal(point).compareTo(pointValue) == 1){
MessageDialog.openError(((Text)e.getSource()).getShell(), "错误", "积分余额不足,当前可使用积分为 "+pointValue);
usePoint.setFocus();
return;
}
PasswordDialog dialog = new PasswordDialog(((Text)e.getSource()).getShell());
dialog.setCardNo(cardNo);
int flag = dialog.open();
if(flag ==0){
IBaseService baseService = new BaseServiceImpl();
BigDecimal inputPoint = new BigDecimal(point);
try {
BigDecimal bigDecimal = baseService.getAmountByPoint(inputPoint);
partSales.setMemberAmount(bigDecimal);
poiontAmount.setText(ProjectUtil.formatAmount(null, bigDecimal));
String facetAmount = ProjectUtil.formatString(price).subtract(partSales.getDiscountAmount()).subtract(partSales.getDiscountToZero()).subtract(partSales.getMemberAmount()).toString();
factTotalAmount.setText(ProjectUtil.formatAmount(null,facetAmount));
List<CashierModel> cms = (List<CashierModel>)PartSalesManager.getInstance().getCashierModelTableViewer().getInput();
BigDecimal cmAmountCount = new BigDecimal(0);
for(CashierModel cm : cms){
cmAmountCount = cmAmountCount.add(cm.getAmount());
}
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1){
cmAmountCount = new BigDecimal(0);
}
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1 || cmAmountCount.compareTo(new BigDecimal(0)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == -1){
cmAmountCount = new BigDecimal(0);
PartSalesManager.getInstance().getCashBack().setText("0.00");
}else{
BigDecimal cashBackAmount = cmAmountCount.subtract(ProjectUtil.formatString(facetAmount));
PartSalesManager.getInstance().getCashBack().setText(ProjectUtil.formatAmount(null, cashBackAmount).toString());
}
} catch (POSException e1) {
MessageDialog.openError(((Text)e.getSource()).getShell(), "错误", e1.getErrorMessage());
usePoint.setFocus();
return;
}
}
}
}
public void keyPressed(KeyEvent e) {}
});
}
{
Button btn = new Button(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = buttonWidth;
data.heightHint = buttonHight;
btn.setLayoutData(data);
btn.setText("确认");
btn.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
String point = usePoint.getText();
if(null == point || "".equals(point)){
MessageDialog.openError(((Button)e.getSource()).getShell(), "错误", "【抵扣积分】不能为空");
usePoint.setFocus();
return;
}
if(new BigDecimal(point).compareTo(new BigDecimal(0)) == -1){
MessageDialog.openError(((Button)e.getSource()).getShell(), "错误", "【抵扣积分】不能小于零");
usePoint.setFocus();
return;
}
BigDecimal pointValue = partSales.getVipUser().getPoint();
if(new BigDecimal(point).compareTo(pointValue) == 1){
MessageDialog.openError(((Button)e.getSource()).getShell(), "错误", "积分余额不足,当前可使用积分为 "+pointValue);
usePoint.setFocus();
return;
}
PasswordDialog dialog = new PasswordDialog(((Button)e.getSource()).getShell());
dialog.setCardNo(cardNo);
int flag = dialog.open();
if(flag ==0){
IBaseService baseService = new BaseServiceImpl();
BigDecimal inputPoint = new BigDecimal(point);
try {
BigDecimal bigDecimal = baseService.getAmountByPoint(inputPoint);
partSales.setMemberAmount(bigDecimal);
poiontAmount.setText(ProjectUtil.formatAmount(null, bigDecimal));
String facetAmount = ProjectUtil.formatString(price).subtract(partSales.getDiscountAmount()).subtract(partSales.getDiscountToZero()).subtract(partSales.getMemberAmount()).toString();
factTotalAmount.setText(ProjectUtil.formatAmount(null,facetAmount));
List<CashierModel> cms = (List<CashierModel>)PartSalesManager.getInstance().getCashierModelTableViewer().getInput();
BigDecimal cmAmountCount = new BigDecimal(0);
for(CashierModel cm : cms){
cmAmountCount = cmAmountCount.add(cm.getAmount());
}
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1){
cmAmountCount = new BigDecimal(0);
}
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1 || cmAmountCount.compareTo(new BigDecimal(0)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == -1){
cmAmountCount = new BigDecimal(0);
PartSalesManager.getInstance().getCashBack().setText("0.00");
}else{
BigDecimal cashBackAmount = cmAmountCount.subtract(ProjectUtil.formatString(facetAmount));
PartSalesManager.getInstance().getCashBack().setText(ProjectUtil.formatAmount(null, cashBackAmount).toString());
}
} catch (POSException e1) {
MessageDialog.openError(((Text)e.getSource()).getShell(), "错误", e1.getErrorMessage());
usePoint.setFocus();
return;
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("积分抵扣:");
}
{
poiontAmount = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
poiontAmount.setLayoutData(data);
poiontAmount.setEditable(false);
poiontAmount.setEnabled(false);
poiontAmount.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
poiontAmount.setText("0.00");
}
{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("元");
}
}
{
Label label = new Label(compsite, SWT.NONE);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data);
label.setText("实收金额:");
}
{
factTotalAmount = new Text(compsite, SWT.BORDER);
GridData data = CommonCss.getDefaultTextData();
data.horizontalSpan = 2;
factTotalAmount.setLayoutData(data);
factTotalAmount.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
factTotalAmount.setEditable(false);
factTotalAmount.setText(price);
}
{
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,totalAmount.getText());
cashierModelItemCellModify.setDiscountAmount(salesOffPoint);
cashierModelItemCellModify.setZeroAmount(delZero);
cashierModelItemCellModify.setPointAmount(poiontAmount);
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 = partSaleService.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();
cashBack.setLayoutData(data);
data.horizontalSpan = 2;
cashBack.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
cashBack.setEditable(false);
cashBack.setText("");
}
cashierModelItemCellModify.setCashBack(cashBack);
PartSalesManager.getInstance().setTotalAmount(totalAmount);
PartSalesManager.getInstance().setCashierModelTableViewer(tableViewer);
PartSalesManager.getInstance().setCashBack(cashBack);
return super.createContents(parent);
}
public TableViewer getTableViewer() {
return tableViewer;
}
public void setTableViewer(TableViewer tableViewer) {
this.tableViewer = tableViewer;
}
protected void createButtonsForButtonBar(Composite parent) {
parent.setLayout(new GridLayout(5,false));
GridData btnLayout = new GridData();
btnLayout.widthHint = buttonWidth;
btnLayout.heightHint = buttonHight;
{
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 btn = (Button)e.getSource();
//如果已存在折扣金额,则不允许重复折扣
if(null != delZero.getText() && ProjectUtil.formatString(delZero.getText()).compareTo(new BigDecimal(0)) == 1){
MessageDialog.openError(btn.getShell(), "错误", "不能重复抹零");
return;
}
AuthDialog dialog = new AuthDialog(btn.getShell(), 1);
try{
int retrunType =dialog.open();
if(retrunType == 1){
return;
}
}catch(Exception eeee){
eeee.printStackTrace();
return;
}
MAuthConfirmDialog mauthDialog = new MAuthConfirmDialog(btn.getShell());
mauthDialog.setAuthUser(dialog.getUser());
mauthDialog.setOpFlag(1);
mauthDialog.setSalesOffPoint(salesOffPoint);
mauthDialog.setDelZero(delZero);
mauthDialog.setFactTotalAmount(factTotalAmount);
int retrunType =mauthDialog.open();
if(retrunType == 1){
return;
}
delZero.setText(ProjectUtil.formatAmount(null, partSales.getDiscountToZero()));
String facetAmount = ProjectUtil.formatString(price).subtract(partSales.getDiscountAmount()).subtract(partSales.getDiscountToZero()).subtract(partSales.getMemberAmount()).toString();
factTotalAmount.setText(ProjectUtil.formatAmount(null,facetAmount));
List<CashierModel> cms = (List<CashierModel>)PartSalesManager.getInstance().getCashierModelTableViewer().getInput();
BigDecimal cmAmountCount = new BigDecimal(0);
for(CashierModel cm : cms){
cmAmountCount = cmAmountCount.add(cm.getAmount());
}
//|| cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == -1 修改找补
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1 || cmAmountCount.compareTo(new BigDecimal(0)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == 0 ){
cmAmountCount = new BigDecimal(0);
PartSalesManager.getInstance().getCashBack().setText("0.00");
}else{
List<CashierModel> models = (List<CashierModel>)tableViewer.getInput();
BigDecimal totalPay = new BigDecimal(0);
BigDecimal makeUpPay = new BigDecimal(0);
BigDecimal noMakeUpPay = new BigDecimal(0);
for(CashierModel cm : models){
totalPay = totalPay.add(cm.getAmount());
if(cm.getIsNoMakeup() == 0){
makeUpPay = makeUpPay.add(cm.getAmount());
}else{
noMakeUpPay = noMakeUpPay.add(cm.getAmount());
}
}
BigDecimal factAmount = ProjectUtil.formatString(factTotalAmount.getText()) ;
if(!(makeUpPay.compareTo(new BigDecimal(0)) == 0)){
//实收金额 > 不找补项金额合计
if(factAmount.compareTo(noMakeUpPay) == 1){
// BigDecimal big_CashBack = factAmount.subtract(noMakeUpPay).subtract(makeUpPay);
BigDecimal big_CashBack = noMakeUpPay.add(makeUpPay).subtract(factAmount);
// if(big_CashBack.compareTo(new BigDecimal(0)) == -1){
// big_CashBack = big_CashBack.abs();
// }
cashBack.setText(ProjectUtil.formatAmount(null, big_CashBack));
//实收金额 <= 不找补项金额合计
}else if(factAmount.compareTo(noMakeUpPay) == -1 || factAmount.compareTo(noMakeUpPay) == 0){
BigDecimal big_CashBack = makeUpPay;
if(big_CashBack.compareTo(new BigDecimal(0)) == -1){
big_CashBack = big_CashBack.abs();
}
cashBack.setText(ProjectUtil.formatAmount(null, big_CashBack));
}
}
// BigDecimal cashBackAmount = cmAmountCount.subtract(ProjectUtil.formatString(facetAmount));
// PartSalesManager.getInstance().getCashBack().setText(ProjectUtil.formatAmount(null, cashBackAmount).toString());
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
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 btn = (Button)e.getSource();
//如果已存在折扣金额,则不允许重复折扣
if(null != salesOffPoint.getText() && ProjectUtil.formatString(salesOffPoint.getText()).compareTo(new BigDecimal(0)) == 1){
MessageDialog.openError(btn.getShell(), "错误", "不能重复折扣");
return;
}
//权限认证对话框
AuthDialog dialog = new AuthDialog(btn.getShell(), 0);
try{
int retrunType =dialog.open();
if(retrunType == 1){
return;
}
}catch(Exception ee){
LoggerUtil.logError(SalesActivator.PLUGIN_ID, "折扣出错", ee);
MessageDialog.openError(btn.getShell(), "错误", "折扣出错");
return;
}
//折扣输入对话框
MAuthConfirmDialog mauthDialog = new MAuthConfirmDialog(btn.getShell());
mauthDialog.setAuthUser(dialog.getUser());
mauthDialog.setOpFlag(0);
mauthDialog.setSalesOffPoint(salesOffPoint);
mauthDialog.setDelZero(delZero);
mauthDialog.setFactTotalAmount(factTotalAmount);
int retrunType =mauthDialog.open();
if(retrunType == 1){
return;
}
salesOffPoint.setText(ProjectUtil.formatAmount(null, partSales.getDiscountAmount()));
String facetAmount = ProjectUtil.formatString(price).subtract(partSales.getDiscountAmount()).subtract(partSales.getDiscountToZero()).subtract(partSales.getMemberAmount()).toString();
factTotalAmount.setText(ProjectUtil.formatAmount(null,facetAmount));
List<CashierModel> cms = (List<CashierModel>)PartSalesManager.getInstance().getCashierModelTableViewer().getInput();
BigDecimal cmAmountCount = new BigDecimal(0);
for(CashierModel cm : cms){
cmAmountCount = cmAmountCount.add(cm.getAmount());
}
// || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == -1 找补修改负数
if(cmAmountCount.compareTo(new BigDecimal(0)) == -1 || cmAmountCount.compareTo(new BigDecimal(0)) == 0 || cmAmountCount.compareTo(ProjectUtil.formatString(facetAmount)) == 0){
cmAmountCount = new BigDecimal(0);
PartSalesManager.getInstance().getCashBack().setText("0.00");
}else{
List<CashierModel> models = (List<CashierModel>)tableViewer.getInput();
BigDecimal totalPay = new BigDecimal(0);
BigDecimal makeUpPay = new BigDecimal(0);
BigDecimal noMakeUpPay = new BigDecimal(0);
for(CashierModel cm : models){
totalPay = totalPay.add(cm.getAmount());
if(cm.getIsNoMakeup() == 0){
makeUpPay = makeUpPay.add(cm.getAmount());
}else{
noMakeUpPay = noMakeUpPay.add(cm.getAmount());
}
}
BigDecimal factAmount = ProjectUtil.formatString(factTotalAmount.getText()) ;
if(!(makeUpPay.compareTo(new BigDecimal(0)) == 0)){
//实收金额 > 不找补项金额合计
if(factAmount.compareTo(noMakeUpPay) == 1){
// BigDecimal big_CashBack = factAmount.subtract(noMakeUpPay).subtract(makeUpPay);
BigDecimal big_CashBack = noMakeUpPay.add(makeUpPay).subtract(factAmount);
// if(big_CashBack.compareTo(new BigDecimal(0)) == -1){
// big_CashBack = big_CashBack.abs();
// }
cashBack.setText(ProjectUtil.formatAmount(null, big_CashBack));
//实收金额 <= 不找补项金额合计
}else if(factAmount.compareTo(noMakeUpPay) == -1 || factAmount.compareTo(noMakeUpPay) == 0){
BigDecimal big_CashBack = makeUpPay;
if(big_CashBack.compareTo(new BigDecimal(0)) == -1){
big_CashBack = big_CashBack.abs();
}
cashBack.setText(ProjectUtil.formatAmount(null, big_CashBack));
}
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
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(factTotalAmount.getText());
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(printBtn.getShell(), "错误", "收款金额不能小于合计金额");
return;
}
partSales.setCashierModelList(cmList);
partSales.setCountAmount(ProjectUtil.formatString(price));
partSales.setFactTotalAmount(ProjectUtil.formatString(factTotalAmount.getText()));
if(poiontAmount != null){
partSales.setMemberAmount(ProjectUtil.formatString(poiontAmount.getText()));//设置积分抵扣金额
}
//拆票处理,不做别的
PartSales pPartSales = new PartSalesPrintData().convertPartSalesForChaiPiao(partSales);
//Filter print items
List<Item> printItems = new ArrayList<Item>();
for(int i=0; i<pPartSales.getItemList().size(); i++){
if(pPartSales.getItemList().get(i).getItemType().equals(AppConstants.ITEM_TYPE_OLDGOLD)){
continue;
}
if(pPartSales.getItemList().get(i).getItemType().equals(AppConstants.ITEM_TYPE_RETURNED)){
continue;
}
printItems.add(pPartSales.getItemList().get(i));
}
try {
pPartSales.setDocNum(tradeOrder.getText());
partSaleService.checkPartSalesDocNum(pPartSales);
} catch (POSException e1) {
MessageDialog.openWarning(printBtn.getShell(), "提示", e1.getErrorMessage());
return;
}
PrintConfirmDialog printDialog = new PrintConfirmDialog(printBtn.getShell());
Item firstItem = printItems.get(0);
printDialog.setPartSales(pPartSales);
printDialog.setPrice(price);
printDialog.setTotalPage(CommonFacade.getTotalPageFromPartSales(pPartSales));
printDialog.setIndexPage(1);
printDialog.setItemName(firstItem.getItemName());
printDialog.setCurrentInvoiceAmount(new PartSalesPrintData().getPrintInvoiceAmount(firstItem, partSales, 1));
String payCompany = "个人";
if(pPartSales.isVip()){
payCompany = pPartSales.getVipUser().getMenberName();
}
printDialog.setOrderBean(new PartSalesPrintData().getOrderBeanPrintData(1, payCompany, pPartSales));
printDialog.setSalesValue(getSalesValue());//设置折扣
printDialog.setZeroAmount(ProjectUtil.formatString(delZero.getText()));//设置抹零
if(poiontAmount != null){
printDialog.setPointAmount(ProjectUtil.formatString(poiontAmount.getText()));//设置积分抵扣金额
}
try{
Invoice currentInvoice = partSaleService.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(POSException ee){
MessageDialog.openError(printBtn.getShell(), "错误", ee.getErrorMessage());
return;
}catch(Exception ee){
MessageDialog.openError(printBtn.getShell(), "错误", "打印出错");
return;
}
PartSales newPartSales = printDialog.getPartSales();
partSales.setRetailFPList(newPartSales.getRetailFPList());
okPressed();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
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 String getPrice() {
return price;
}
public final void setPrice(String price) {
this.price = price;
}
public BigDecimal getSalesValue() {
return salesValue;
}
public void setSalesValue(BigDecimal salesValue) {
this.salesValue = salesValue;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public Text getUsePoint() {
return usePoint;
}
public void setUsePoint(Text usePoint) {
this.usePoint = usePoint;
}
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 SalesPayDialog(Shell parent) {
super(parent);
}
/*
* Add by TanDong 2014-01-14 17:03
*
* 重写关闭方法,点击X关闭需要清空折扣的抹零操作,否则下次折扣和抹零会重复,从而影响结算
* @see org.eclipse.jface.dialogs.Dialog#close()
*/
public boolean close() {
PartSales newPartSales = PartSalesManager.getInstance().getBeforeZeroAndDiscountPartSales();
newPartSales.setMlimit(false);
newPartSales.setMratio(false);
newPartSales.setDiscountToZero(new BigDecimal(0));
newPartSales.setDiscountRate(new BigDecimal(1));
newPartSales.setDiscountAccount(0);
newPartSales.setDiscountAmount(new BigDecimal(0));
PartSalesManager.getInstance().setPartSales(newPartSales);
return super.close();
}
}