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.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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import com.topsun.posclient.common.AppConstants;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.ProjectUtil;
import com.topsun.posclient.common.core.BarcodeListenetManager;
import com.topsun.posclient.common.core.IBarcodeListener;
import com.topsun.posclient.common.listener.IKeyListener;
import com.topsun.posclient.common.listener.KeyListenerManager;
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.Item;
import com.topsun.posclient.datamodel.PartSales;
import com.topsun.posclient.sales.MessageResources;
import com.topsun.posclient.sales.core.service.IGoldBuyBackService;
import com.topsun.posclient.sales.core.service.impl.GoldBuyBackServiceImpl;
import com.topsun.posclient.sales.ui.gold.GoldBuyBackFacade;
import com.topsun.posclient.sales.ui.gold.GoldBuyBackManager;
import com.topsun.posclient.sales.ui.table.GoldBuyBackCellModify;
import com.topsun.posclient.sales.ui.table.GoldBuyBackTableContentProvider;
import com.topsun.posclient.sales.ui.table.GoldBuyBackTableLableProvider;
/**
* 投资金条回购
*
* @author Dong
*
*/
public class GoldBuyBackView extends ViewPart implements IKeyListener,IBarcodeListener {
public IGoldBuyBackService goldBuyBackService = new GoldBuyBackServiceImpl();
List<Item> itemsList = new ArrayList<Item>();
public PartSales partSales;
public Text salesDate;
public Text docNum;//单据号
public TableViewer tableViewer;
public Text numberTotal;
public Text priceTotal;
public int controlVerticalAlignment = 3;
public Button cleanAll;
public TableViewer promotionTableView;
private Label totlePrice;
public Text getNumberTotal() {
return numberTotal;
}
public void setNumberTotal(Text numberTotal) {
this.numberTotal = numberTotal;
}
public Text getPriceTotal() {
return priceTotal;
}
public void setPriceTotal(Text priceTotal) {
this.priceTotal = priceTotal;
}
public GoldBuyBackView() {
}
/**
* 商品信息
* @param parent
*/
private void buildProductInfo(Composite parent){
KeyListenerManager.getInstance().addKeyListener(this);
BarcodeListenetManager.getInstance().addKeyListener(this);
Group productInfo = new Group(parent, SWT.NONE);
productInfo.setText("单品列表");
productInfo.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
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 GoldBuyBackTableContentProvider());
tableViewer.setLabelProvider(new GoldBuyBackTableLableProvider());
String[] cloumsProperties = new String[]{"invoceCode","docNum","MATNR","matnrName","ZDPYZL","price","ZMDLSBQJ"};
tableViewer.setColumnProperties(cloumsProperties);
final Table table = tableViewer.getTable();
TableUtils.setTabelHeight(table);
table.setBackground(ThemeUtils.getCurrent().getTableBackGround());
table.setFont(CommonCss.getTableHeaderFont(parent.getDisplay()));
CellEditor[] editors = new CellEditor[6];
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);
tableViewer.setCellEditors(editors);
tableViewer.setCellModifier(new GoldBuyBackCellModify(tableViewer));
table.setLinesVisible(false);
table.setHeaderVisible(true);
{
GridData tableData = new GridData(GridData.FILL_HORIZONTAL);
tableData.heightHint = 270;
tableData.horizontalSpan = 4;
table.setLayoutData(tableData);
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(180);
column.setText("发票号");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(180);
column.setText("订单号");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(180);
column.setText("物料号");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(180);
column.setText("物料名称");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(130);
column.setText("克重(g)");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(130);
column.setText("回购价");
}
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(130);
column.setText("金额");
}
}
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);
GridData data1 = new GridData();
data1.heightHint = 45;
data1.widthHint = 80;
button.setLayoutData(data1);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("删除");
GoldBuyBackFacade.delGoldSaleItemListener(button);
}
{
Button button = new Button(operationComposite, SWT.NONE);
GridData data1 = new GridData();
data1.heightHint = 45;
data1.widthHint = 80;
button.setLayoutData(data1);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
button.setText("清空");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
tableViewer.setInput(null);
GoldBuyBackFacade.caculatorGoldSalesPrice();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
{
Button button = new Button(operationComposite, SWT.NONE);
button.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data1 = new GridData();
data1.heightHint = 45;
data1.widthHint = 100;
button.setLayoutData(data1);
button.setText("刷新金价");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button saveButton = (Button)e.getSource();
List<Item> items = (List<Item>)tableViewer.getInput() ;
if(null == items || items.size() == 0){
MessageDialog.openInformation(saveButton.getShell(), MessageResources.message_ui_tips, "单品数据为空,请录入单品数据");
return;
}
List<Item> newItems = null;
try {
newItems = goldBuyBackService.refeshGoldPrice(items);
} catch (POSException e1) {
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
return;
}
tableViewer.setInput(newItems);
tableViewer.refresh();
GoldBuyBackFacade.caculatorGoldSalesPrice();
MessageDialog.openInformation(saveButton.getShell(), MessageResources.message_ui_tips, "刷新成功");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
public void onChange(String operationType) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
if(tableViewer.getInput() != null){
List<Item> items = (List<Item>)tableViewer.getInput() ;
Item addItem = new Item();
items.add(addItem);
tableViewer.setInput(items);
tableViewer.editElement(addItem, 0);
tableViewer.setSelection(new StructuredSelection(addItem));
}else{
List<Item> items = new ArrayList<Item>();
Item addItem = new Item();
items.add(addItem);
tableViewer.setInput(items);
tableViewer.editElement(addItem, 0);
}
caculatorNumAndPrice();
}
});
}
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);
GoldBuyBackManager.getInstance().setGoldBuyBackTableView(tableViewer);
GoldBuyBackManager.getInstance().setGoldBuyBackTotlePrice(totlePrice);
GoldBuyBackManager.getInstance().setDocNum(docNum);
GoldBuyBackFacade.addSalesItemListener(docNum);
}
private void buildOperation(final Composite parent){
Composite operation = new Composite(parent, SWT.NONE);
operation.setLayout(new GridLayout(5,false));
operation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
{
Label label = new Label(operation, SWT.NONE);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data1 = new GridData();
label.setLayoutData(data1);
label.setText("金额合计:");
}
{
totlePrice = new Label(operation, SWT.BORDER);
totlePrice.setLayoutData(CommonCss.getTextData(165));
totlePrice.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
totlePrice.setText("0.00");
}
{
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() {
public void widgetSelected(SelectionEvent e) {
Button saveButton = (Button)e.getSource();
{
if(tableViewer.getInput() == null){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "商品信息不能为空");
return;
}
List list = (List)tableViewer.getInput() ;
if(list.size() == 0){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "商品信息不能为空");
return;
}
}
String price = totlePrice.getText();
partSales = new PartSales();
partSales.setDocNum(docNum.getText());
List<Item> itemList = GoldBuyBackFacade.getCurrentItemList();
for(Item iii : itemList){
if(null == iii.getInvoceCode() || "".equals(iii.getInvoceCode())){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【发票号】不能为空值");
return;
}
if(null == iii.getMATNR() || "".equals(iii.getMATNR())){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【物料号】不能为空值");
return;
}
if(iii.getZDPYZL().compareTo(new BigDecimal(0)) != 1){
MessageDialog.openError(saveButton.getShell(), MessageResources.message_ui_tips, "【克重】必须大于零");
return;
}
}
partSales.setItemList(itemList);
partSales.setCountAmount(ProjectUtil.formatString(price));
partSales.setSalesType(AppConstants.SALES_TYPE_GOLDBACK);
try {
//回购单据号校验
goldBuyBackService.checkPartSalesDocNum(partSales);
goldBuyBackService.saveGoldBuyBackData(partSales);
MessageDialog.openInformation(parent.getShell(), "成功", "交易成功");
GoldBuyBackFacade.restGoldBuyBackView();
} catch (POSException e1) {
e1.printStackTrace();
MessageDialog.openError(parent.getShell(), "错误", e1.getErrorMessage());
tableViewer.setInput(null);
GoldBuyBackFacade.caculatorGoldSalesPrice();
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
private void caculatorNumAndPrice() {
int totalNum = 0;
double totalPrice = 0;
for (Item item : (List<Item>)tableViewer.getInput()) {
totalNum = 0;
}
numberTotal.setText(String.valueOf(totalNum));
for (Item item : (List<Item>)tableViewer.getInput()) {
totalPrice = 0;
}
priceTotal.setText(String.valueOf(totalPrice));
}
private void buildBaseInfo(Composite parent) {
Group baseInfo = new Group(parent, SWT.NONE);
baseInfo.setText(MessageResources.message_ui_group_baseinfo);
baseInfo.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
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(9,false));
leftComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
{
Label label = new Label(leftComposite, SWT.NONE);
label.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
GridData data = new GridData();
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 1;
label.setLayoutData(data);
label.setText("单据号:");
}
{
String no = "";
try {
no = goldBuyBackService.createNo(AppConstants.DOC_TYPE_GOLDBUYBACK);
} catch (POSException e1) {
MessageDialog.openError(parent.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
}
docNum = new Text(leftComposite, SWT.BORDER);
GridData data = CommonCss.getTextData(165);
data.verticalIndent = controlVerticalAlignment;
data.horizontalSpan = 3;
docNum.setLayoutData(data);
docNum.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
docNum.setEnabled(false);
docNum.setText(no);
}
{
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);
}
public void widgetDefaultSelected(SelectionEvent e) {}
});
}
{
Button btn1 = new Button(leftComposite, SWT.NONE);
btn1.setFont(CommonCss.getDefaultFont(parent.getDisplay()));
btn1.setText("添加");
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) {
String inputDocNum = docNum.getText();
GoldBuyBackFacade.addTableItem(inputDocNum, tableViewer);
GoldBuyBackFacade.caculatorGoldSalesPrice();
tableViewer.refresh();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
// {
// Button btn1 = new Button(leftComposite, SWT.NONE);
// btn1.setText("重置");
// btn1.setImage(ImageUtils.createImage(SalesActivator.PLUGIN_ID, "icons//nook.png"));
// GridData data = new GridData();
// data.heightHint = 38;
// data.widthHint = 60;
// data.horizontalSpan = 1;
// btn1.setLayoutData(data);
// btn1.addSelectionListener(new SelectionListener() {
// public void widgetSelected(SelectionEvent e) {
// docNum.setText("");
// }
// public void widgetDefaultSelected(SelectionEvent e) {
//
// }
// });
// }
}
public void setFocus() {
}
@Override
public void onChangeBarcode(final String operationType) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
IWorkbenchPage page = window.getActivePage();
String viewid = page.getActivePartReference().getId();
if(!"com.topsun.posclient.sales.ui.menu.ReturnedView".equals(viewid)){
return;
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
if(tableViewer.getInput() != null){
List<Item> items = (List<Item>)tableViewer.getInput() ;
List<String> nameList = new ArrayList<String>();
for (Item item : items) {
String itemCode = item.getItemCode();
nameList.add(itemCode);
}
if(nameList.contains(operationType)){
Item selectedItem = null;
for (Item t_item : items) {
String itemCode = t_item.getItemCode();
if(itemCode.equals(operationType)){
selectedItem = t_item;
}
}
tableViewer.refresh();
tableViewer.setSelection(new StructuredSelection(selectedItem));
tableViewer.editElement(selectedItem, 2);
}else{
try {
Item addItem = goldBuyBackService.getItemByBarCode(operationType);
items.add(addItem);
tableViewer.setInput(items);
tableViewer.editElement(addItem, 2);
tableViewer.setSelection(new StructuredSelection(addItem));
} catch (POSException e) {
e.printStackTrace();
}
}
}else{
List<Item> items = new ArrayList<Item>();
Item addItem = new Item();
addItem.setItemCode(operationType);
items.add(addItem);
tableViewer.setInput(items);
tableViewer.editElement(addItem, 2);
}
caculatorNumAndPrice();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}