package com.topsun.posclient.sales.ui.gold;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
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.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import com.topsun.posclient.common.AppConstants;
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.datamodel.Item;
import com.topsun.posclient.sales.MessageResources;
/**
* 投资金条回购面板类
*
* @author Dong
*
*/
public class GoldBuyBackFacade {
/**
* 重置投资金条回购
* @throws POSException
*/
public static void restGoldBuyBackView() throws POSException{
IBaseService baseService = new BaseServiceImpl();
TableViewer tableViewer = GoldBuyBackManager.getInstance().getGoldBuyBackTableView();
tableViewer.setInput(null);
tableViewer.refresh();
Text text = GoldBuyBackManager.getInstance().getDocNum();
text.setText(baseService.createNo(AppConstants.DOC_TYPE_GOLDBUYBACK));
caculatorGoldSalesPrice();
}
/**
* 获取当前零售窗口选中的ITEM
* @return
*/
public static Item getCurrentSelectionItem() {
TableViewer tableViewer = GoldBuyBackManager.getInstance().getGoldBuyBackTableView();
ISelection iSelection = tableViewer.getSelection();
if (iSelection instanceof StructuredSelection) {
StructuredSelection selection = (StructuredSelection) iSelection;
Object obj = selection.getFirstElement();
if (obj instanceof Item) {
Item item = (Item) obj;
return item;
}
}
return null;
}
public static List<Item> getCurrentItemList() {
TableViewer tableViewer = GoldBuyBackManager.getInstance().getGoldBuyBackTableView();
return (List<Item>)tableViewer.getInput();
}
/**
* 计算总价
*/
public static void caculatorGoldSalesPrice() {
Label salesPriceLable = GoldBuyBackManager.getInstance().getGoldBuyBackTotlePrice();
TableViewer tableViewer = GoldBuyBackManager.getInstance().getGoldBuyBackTableView();
TableItem[] items = tableViewer.getTable().getItems();
BigDecimal totle = new BigDecimal(0);
for (TableItem tableItem : items) {
String zl = tableItem.getText(4);
String price = tableItem.getText(5);
if((null == price || "".equals(price)) || (null == zl || "".equals(zl))){
totle = totle.add(new BigDecimal(0));
}else{
totle = totle.add(ProjectUtil.formatString(price).multiply(ProjectUtil.formatString(zl)));
}
}
salesPriceLable.setText(ProjectUtil.formatAmount(null, totle).toString());
}
/**
* 删除金品ITEM
*
* @param control
*/
public static void delGoldSaleItemListener(Control control) {
if (control instanceof Button) {
Button button = (Button) control;
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
TableViewer tableViewer = GoldBuyBackManager.getInstance()
.getGoldBuyBackTableView();
ISelection iSelection = tableViewer.getSelection();
if (iSelection instanceof IStructuredSelection) {
IStructuredSelection iStructuredSelection = (IStructuredSelection) iSelection;
Object obj = iStructuredSelection.getFirstElement();
if (obj instanceof Item) {
Item item = (Item) obj;
Object itemList = tableViewer.getInput();
if (itemList == null) {
return;
} else {
if (itemList instanceof List) {
List list = (List) itemList;
list.remove(item);
}
}
tableViewer.refresh();
caculatorGoldSalesPrice();
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
/**
* 添加回购信息
* @param inputDocNum
* @param inputBarCode
* @param tableViewer
* @throws POSException
*/
public static void addTableItem(String inputDocNum, TableViewer tableViewer) {
Item item = new Item();
item.setItemType(AppConstants.ITEM_TYPE_NORMAL);
item.setItemStatus(AppConstants.ITEM_STATU_GOLD_BACK);
item.setDocNum(inputDocNum);
item.setZDPYZL(new BigDecimal(0));
item.setZMDLSBQJ(new BigDecimal(0));
item.setPrice(new BigDecimal(0));
item.setSalesAmount(new BigDecimal(0));
item.setFactAmount(new BigDecimal(0));
item.setZJGF(new BigDecimal(0));
item.setFactZJGF(new BigDecimal(0));
item.setItemName("");
Object obj = tableViewer.getInput();
if (obj == null) {
List<Item> list = new ArrayList<Item>();
list.add(item);
tableViewer.setInput(list);
} else {
if (obj instanceof List) {
List list = (List) obj;
list.add(item);
tableViewer.setInput(list);
}
}
caculatorGoldSalesPrice();
tableViewer.refresh();
}
/**
* 添加退换信息
*
* @param control
* @param tableViewer
*/
public static void addSalesItemListener(Control control) {
control.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CR) {
Item item = null;
TableViewer tableViewer = GoldBuyBackManager.getInstance()
.getGoldBuyBackTableView();
String inputString = "";
if (e.getSource() instanceof Text) {
Text text = (Text) e.getSource();
inputString = text.getText();
}
IBaseService baseService = new BaseServiceImpl();
try {
item = baseService.getItemByBarCode(inputString);
if (item == null) {
return;
}
item.setItemType(AppConstants.ITEM_TYPE_NORMAL);
item.setItemStatus(AppConstants.ITEM_STATU_GOLD_BACK);
} catch (POSException e1) {
e1.printStackTrace();
MessageDialog.openError(((Control)e.getSource()).getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
return;
}
Object obj = tableViewer.getInput();
if (obj == null) {
List<Item> list = new ArrayList<Item>();
list.add(item);
tableViewer.setInput(list);
} else {
if (obj instanceof List) {
List list = (List) obj;
list.add(item);
tableViewer.setInput(list);
}
}
caculatorGoldSalesPrice();
tableViewer.refresh();
}
}
public void keyPressed(KeyEvent e) {
}
});
}
}