package com.topsun.posclient.sales.ui.view;
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.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.StructuredSelection;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import com.topsun.posclient.common.AppConstants;
import com.topsun.posclient.common.POSClientApp;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.core.keyboard.SoftKeyUtil;
import com.topsun.posclient.common.ui.style.CommonCss;
import com.topsun.posclient.common.ui.utils.TableUtils;
import com.topsun.posclient.common.ui.utils.ThemeUtils;
import com.topsun.posclient.datamodel.OldGold;
import com.topsun.posclient.datamodel.dto.OldGoldDTO;
import com.topsun.posclient.sales.MessageResources;
import com.topsun.posclient.sales.core.service.IOldGoldSaleService;
import com.topsun.posclient.sales.core.service.impl.OldGoldSaleServiceImpl;
import com.topsun.posclient.sales.ui.gold.OldGoldIdentifyManager;
import com.topsun.posclient.sales.ui.gold.OldGoldIndentifyFacade;
import com.topsun.posclient.sales.ui.table.OldGoldIdentifyCellModify;
import com.topsun.posclient.sales.ui.table.OldGoldIdentifyTableContentProvider;
import com.topsun.posclient.sales.ui.table.OldGoldIdentifyTableLableProvider;
/**
* 旧金鉴定界面
*
* @author Dong
*
*/
public class OldGoldIdentifyView extends ViewPart {
public IOldGoldSaleService oldGoldSaleService = new OldGoldSaleServiceImpl();
List<OldGold> itemsList = new ArrayList<OldGold>();
public Text salesDate;
public Text docNum;//单据号
public Text indentifyName;//鉴定员
public Text matnr;//物料编码
public TableViewer tableViewer;
public Text numberTotal;
public int controlVerticalAlignment = 3;
public Button cleanAll;
private Label totlePrice;
private TableColumn last;
private TableColumn first;
public Text getNumberTotal() {
return numberTotal;
}
public void setNumberTotal(Text numberTotal) {
this.numberTotal = numberTotal;
}
public OldGoldIdentifyView() {
}
/**
* 商品信息
* @param parent
*/
private void buildProductInfo(Composite parent){
Group productInfo = new Group(parent, SWT.NONE);
productInfo.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
productInfo.setText("旧金列表");
GridLayout gridLayout = new GridLayout(1,false);
productInfo.setLayout(gridLayout);
GridData data = new GridData(GridData.FILL_BOTH);
productInfo.setLayoutData(data);
buildPrintInfo(productInfo);
tableViewer = new TableViewer(productInfo,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER|SWT.FULL_SELECTION);
tableViewer.setContentProvider(new OldGoldIdentifyTableContentProvider());
tableViewer.setLabelProvider(new OldGoldIdentifyTableLableProvider());
String[] cols = new String[]{"mtart","name","num","Neatness","Weight_G","Weight","Loss","Weight_R","Weight_F","Price_G","Service","Sum"};
tableViewer.setColumnProperties(cols);
final Table table = tableViewer.getTable();
TableUtils.setTabelHeight(table);
table.setBackground(ThemeUtils.getCurrent().getTableBackGround());
table.setFont(CommonCss.getTableHeaderFont(parent.getDisplay()));
CellEditor[] editors = new CellEditor[12];
editors[0] = new TextCellEditor(table);
editors[1] = new TextCellEditor(table);
editors[2] = new TextCellEditor(table);
editors[3] = new TextCellEditor(table);
editors[4] = new TextCellEditor(table);
editors[5] = new TextCellEditor(table);
editors[6] = new ComboBoxCellEditor(table,new String[]{"1%","12%","15%","0%"},SWT.READ_ONLY);
editors[7] = new TextCellEditor(table);
editors[8] = new TextCellEditor(table);
editors[9] = new TextCellEditor(table);
editors[10] = new ComboBoxCellEditor(table,new String[]{"6元/g","10元/g","12元/g","0元/g"},SWT.READ_ONLY);
editors[11] = new TextCellEditor(table);
tableViewer.setCellEditors(editors);
tableViewer.setCellModifier(new OldGoldIdentifyCellModify(tableViewer));
table.setLinesVisible(false);
table.setHeaderVisible(true);
{
GridData tableData = new GridData(GridData.FILL_HORIZONTAL);
tableData.heightHint = 270;
tableData.horizontalSpan = 4;
table.setLayoutData(tableData);
}
{
first = new TableColumn(table, SWT.NONE);
first.setWidth(80);
first.setText("物料编码");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(150);
column.setText("旧品名称");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(50);
column.setText("件数");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(70);
column.setText("成色");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("称重(g)");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("纯金重(g)");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(70);
column.setText("耗损比");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("耗损重(g)");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("折金重(g)");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("单价");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(120);
column.setText("服务费");
}
{
last = new TableColumn(table, SWT.NONE);
last.setWidth(120);
last.setText("抵扣金额");
}
table.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height =CommonCss.TABLE_HEIGHT;
}
});
table.layout();
tableViewer.refresh();
}
private void buildPrintInfo(Composite parent){
Composite operationComposite = new Composite(parent, SWT.NONE);
operationComposite.setLayout(new GridLayout(4,false));
{
Button button = new Button(operationComposite, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data1 = new GridData();
data1.heightHint = 45;
data1.widthHint = 80;
button.setLayoutData(data1);
button.setText("删除");
OldGoldIndentifyFacade.delGoldSaleItemListener(button);
}
{
Button button = new Button(operationComposite, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data1 = new GridData();
data1.heightHint = 45;
data1.widthHint = 80;
button.setLayoutData(data1);
button.setText("清空");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
tableViewer.setInput(null);
OldGoldIndentifyFacade.caculatorOldGoldTotalPrice();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
public void onChange(String operationType) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
if(tableViewer.getInput() != null){
List<OldGold> items = (List<OldGold>)tableViewer.getInput() ;
OldGold addOldGold = new OldGold();
items.add(addOldGold);
tableViewer.setInput(items);
tableViewer.editElement(addOldGold, 0);
tableViewer.setSelection(new StructuredSelection(addOldGold));
}else{
List<OldGold> items = new ArrayList<OldGold>();
OldGold addOldGold = new OldGold();
items.add(addOldGold);
tableViewer.setInput(items);
tableViewer.editElement(addOldGold, 0);
}
OldGoldIndentifyFacade.caculatorOldGoldTotalPrice();
}
});
}
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(1,false));
parent.setBackground(ThemeUtils.getCurrent().getBackGroundColor());
parent.setBackgroundMode(SWT.INHERIT_FORCE);
buildBaseInfo(parent);
buildProductInfo(parent);
buildOperation(parent);
OldGoldIdentifyManager.getInstance().setOldGoldIdentifyTableView(tableViewer);
OldGoldIdentifyManager.getInstance().setOldGoldIdentifyTotlePrice(totlePrice);
OldGoldIdentifyManager.getInstance().setDocNum(docNum);
OldGoldIdentifyManager.getInstance().setMantrCode(matnr);
matnr.setFocus();
}
private void buildOperation(final Composite parent){
Composite operation = new Composite(parent, SWT.NONE);
operation.setLayout(new GridLayout(8,false));
operation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
{
Label label = new Label(operation, SWT.NONE);
GridData data1 = new GridData();
label.setLayoutData(data1);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label.setText("金额合计:");
}
{
totlePrice = new Label(operation, SWT.BORDER);
totlePrice.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data1 = CommonCss.getTextData(165);
totlePrice.setLayoutData(data1);
totlePrice.setText("0.00");
}
{
Button button = new Button(operation, SWT.NONE);
button.setText("上传单据");
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 100;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button saveButton = (Button)e.getSource();
{
if(tableViewer.getInput() == null){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "商品信息不能为空");
return;
}
List<OldGold> list = (List<OldGold>)tableViewer.getInput() ;
if(list.size() == 0){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "商品信息不能为空");
return;
}
for(OldGold og : list){
if(null == og.getNeatness()){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【成色】不能为空值");
return;
}
if(og.getNeatness().compareTo(new BigDecimal(0)) == 0){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【成色】不能为零");
return;
}
if(null == og.getWeight_G()){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【称重】不能为空值");
return;
}
if(og.getWeight_G().compareTo(new BigDecimal(0)) == 0){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【称重】不能为零");
return;
}
}
}
OldGoldDTO olgGoldDto = new OldGoldDTO();
List<OldGold> ogList = OldGoldIndentifyFacade.getCurrentOldGoldList();
olgGoldDto.setOldGoldList(ogList);
olgGoldDto.setDocNum(docNum.getText());
try {
oldGoldSaleService.checkDocNum(docNum.getText());
}catch (POSException e1) {
MessageDialog.openError(parent.getShell(), "错误", e1.getErrorMessage());
return;
}
try{
oldGoldSaleService.saveOldGoldData(olgGoldDto);
MessageDialog.openInformation(parent.getShell(), "成功", "交易成功");
OldGoldIndentifyFacade.restOldGoldIndentifyView();
} catch (POSException e1) {
MessageDialog.openError(parent.getShell(), "错误", e1.getErrorMessage());
try {
OldGoldIndentifyFacade.restOldGoldIndentifyView();
} catch (POSException e2) {
}
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Label label = new Label(operation, SWT.NONE);
GridData data1 = new GridData(GridData.FILL_HORIZONTAL);
label.setLayoutData(data1);
}
{
Button button = new Button(operation, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("<");
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 80;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
tableViewer.getTable().showColumn(first);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
}
{
Button button = new Button(operation, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText(">");
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 80;
button.setLayoutData(data);
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
tableViewer.getTable().showColumn(last);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
private void buildBaseInfo(Composite parent) {
String no = "";
try {
no = oldGoldSaleService.createNo(AppConstants.DOC_TYPE_OLDGOLD);
} catch (POSException e1) {
MessageDialog.openError(parent.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
}
Group baseInfo = new Group(parent, SWT.NONE);
baseInfo.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
baseInfo.setText(MessageResources.message_ui_group_baseinfo);
GridLayout gridLayout = new GridLayout(3,false);
gridLayout.marginLeft = 5;
baseInfo.setLayout(gridLayout);
baseInfo.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite leftComposite = new Composite(baseInfo,SWT.NONE);
leftComposite.setLayout(new GridLayout(18,false));
leftComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
{
Label label = new Label(leftComposite, SWT.NONE);
GridData data = new GridData();
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 1;
label.setLayoutData(data);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label.setText("单据号:");
}
{
docNum = new Text(leftComposite, SWT.BORDER);
GridData data = CommonCss.getTextData(165);
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 3;
docNum.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
docNum.setLayoutData(data);
docNum.setText(no);
docNum.setEditable(false);
}
{
Button btn = new Button(leftComposite, SWT.NONE);
GridData data = new GridData();
data.widthHint = 80;
data.heightHint = 45;
data.horizontalSpan = 1;
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 = docNum.getText();
// String subStr = tradeOrderNum.substring(tradeOrderNum.length()-4,tradeOrderNum.length());
//
// 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);
//
// String setStr = prifix + zeroPrefix + (Integer.valueOf(subStr) + 1);
// docNum.setText(setStr);
String tradeOrderNum = docNum.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);
docNum.setText(setStr);
// custDocNum.setText(setStr);
}
public void widgetDefaultSelected(SelectionEvent e) {}
});
}
{
Label label = new Label(leftComposite, SWT.NONE);
GridData data = new GridData();
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 1;
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label.setLayoutData(data);
label.setText("鉴定员:");
}
{
indentifyName = new Text(leftComposite, SWT.BORDER);
GridData data = CommonCss.getTextData(165);
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 3;
indentifyName.setLayoutData(data);
indentifyName.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
String name = POSClientApp.get().getLoginUser().getEmployeeName();
indentifyName.setText(null == name?POSClientApp.get().getLoginUser().getUserName():name);
indentifyName.setEditable(false);
}
{
Label label = new Label(leftComposite, SWT.NONE);
GridData data = new GridData();
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 1;
label.setLayoutData(data);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
label.setText("物料编码:");
}
{
matnr = new Text(leftComposite, SWT.BORDER);
GridData data = CommonCss.getTextData(165);
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 3;
matnr.setLayoutData(data);
matnr.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
OldGoldIndentifyFacade.addOldGoldListener(matnr, docNum.getText());
SoftKeyUtil.showSoftKey(matnr);
}
{
Button btn1 = new Button(leftComposite, SWT.NONE);
btn1.setText("确定");
btn1.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 80;
data.horizontalSpan = 1;
btn1.setLayoutData(data);
btn1.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button sureBtn = (Button)e.getSource();
String inputIndentifyName = indentifyName.getText();
if(null == inputIndentifyName || "".equals(inputIndentifyName)){
MessageDialog.openInformation(sureBtn.getShell(), MessageResources.message_ui_tips, "请输入【鉴定员】");
matnr.setFocus();
}
String inputMatnr = matnr.getText();
String inputDocNum = docNum.getText();
if(null != inputMatnr && !"".equals(inputMatnr)){
try {
OldGoldIndentifyFacade.addTableOldGold(inputMatnr, inputDocNum, tableViewer);
} catch (POSException e2) {
MessageDialog.openError(sureBtn.getShell(), MessageResources.message_ui_tips, e2.getErrorMessage());
return;
}
}else{
MessageDialog.openInformation(sureBtn.getShell(), MessageResources.message_ui_tips, "请输入【物料编码】");
matnr.setFocus();
}
OldGoldIndentifyFacade.caculatorOldGoldTotalPrice();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Button btn1 = new Button(leftComposite, SWT.NONE);
btn1.setText("重置");
btn1.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData();
data.heightHint = 45;
data.widthHint = 80;
data.horizontalSpan = 1;
btn1.setLayoutData(data);
btn1.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
matnr.setText("");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
public void setFocus() {
}
}