package com.topsun.posclient.sales.ui.gold;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.co.lane.util.StringUtil;
import com.topsun.posclient.common.AppConstants;
import com.topsun.posclient.common.POSClientApp;
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.datamodel.Material;
import com.topsun.posclient.datamodel.User;
import com.topsun.posclient.sales.MessageResources;
import com.topsun.posclient.sales.core.service.IGoldBookingService;
import com.topsun.posclient.sales.core.service.impl.GoldBookingServiceImpl;
/**
* 投资金条预订面板类
*
* @author Dong
*
*/
public class GoldSalesFacade {
/**
* 添加预订商品
* @param btn
*/
public static void addBookingGoldItem(final Text text){
text.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CR) {
Text text = (Text) e.getSource();
String inputString = "";
if (e.getSource() instanceof Text) {
inputString = text.getText();
}
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
List<Item> items = (List<Item>)tableViewer.getInput();
if(items != null){
Item getItem = items.get(0);
String itemType = getItem.getItemType();
if(!AppConstants.ITEM_TYPE_BOOKING.equals(itemType)){
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, "只能进行购买现货操作!");
text.setText("");
return;
}
}
IBaseService baseService = new BaseServiceImpl();
String matnrName = "";
try {
Material material = baseService.getMaterialByMatnr(inputString);
matnrName = baseService.getNameByMatkl(material.getMatkl());
if(StringUtil.isEmpty(matnrName)){
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, "没有查询到您输入的物料组!请确认后重新输入正确的物料组!");
text.setText("");
return;
}
} catch (POSException e1) {
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
text.setText("");
return;
}
if(items != null){
for (Item item : items) {
if(item.getMATNR().equals(inputString)){
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, "物料号重复,请重新输入!");
text.setText("");
return;
}
}
}
Item item = new Item();
int userId = POSClientApp.get().getLoginUser().getId();
Map<Integer,User> map = POSClientApp.get().getUserRelation();
Iterator<Entry<Integer, User>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, User> value = (Map.Entry<Integer, User>) it.next();
if(value.getValue().getId() == userId){
item.setCashier(value.getValue().getId());
}
}
BigDecimal currentGoldPrice;
try {
Material material = baseService.getMaterialByMatnr(inputString);
currentGoldPrice = baseService.getGoldPriceByMtartCode(inputString);
item.setMatkl(material.getMatkl());
item.setMATNR(null == material.getMatnr()?inputString:material.getMatnr());
item.setMatrnName(matnrName);
item.setItemType(AppConstants.ITEM_TYPE_BOOKING);
item.setItemStatus(AppConstants.ITEM_STATU_GOLD_BOOK);
} catch (POSException e1) {
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
text.setText("");
return;
}
item.setItemCode("");
item.setPrice(currentGoldPrice);
item.setZMDLSBQJ(currentGoldPrice);
item.setZDPZL(new BigDecimal(0));
item.setZDPYZL(new BigDecimal(0));
item.setSalesAmount(currentGoldPrice.multiply(item.getZDPZL()));
if(items == null){
items = new ArrayList<Item>();
}
items.add(item);
tableViewer.setInput(items);
caculatorGoldSalesPrice();
tableViewer.refresh();
GoldSalesManager.getInstance().getGoldSalesBtn().setEnabled(false);
GoldSalesManager.getInstance().getGoldTimeoutBtn().setEnabled(false);
GoldSalesManager.getInstance().getQueryBtn().setEnabled(false);
GoldSalesManager.getInstance().getAddCurrentProductBtn().setEnabled(false);
text.setText("");
}
}
public void keyPressed(KeyEvent e) {
}
});
}
/**
* 输入预订单号回车进行到期取货
* @param btn
*/
public static void addTimeoutGoldItem(final Text text){
text.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
Text text = (Text) e.getSource();
if (e.keyCode == SWT.CR) {
String inputBookDocNum = text.getText();
if(StringUtil.isEmpty(inputBookDocNum)){
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, "【预订单号】不能为空");
text.forceFocus();
return;
}
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
List<Item> itemList = new ArrayList<Item>();
IGoldBookingService goldBookingService = new GoldBookingServiceImpl();
try {
itemList = goldBookingService.queryRetailDetailByDocNum(inputBookDocNum);
} catch (POSException e1) {
MessageDialog.openError(text.getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
text.forceFocus();
text.setText("");
return;
}
tableViewer.setInput(itemList);
tableViewer.refresh();
GoldSalesFacade.caculatorGoldSalesPrice();
text.setText("");
//点击查询按钮进行到期开票,现货结算和预订结算按钮不可用
GoldSalesManager.getInstance().getGoldBookingBtn().setEnabled(false);
GoldSalesManager.getInstance().getGoldSalesBtn().setEnabled(false);
GoldSalesManager.getInstance().getAddBookingBtn().setEnabled(false);
GoldSalesManager.getInstance().getAddCurrentProductBtn().setEnabled(false);
}
}
public void keyPressed(KeyEvent e) {
}
});
}
/**
* /**
* 重置投资金条预订
* @throws POSException
*/
public static void restGoldBookingView() throws POSException{
IBaseService baseService = new BaseServiceImpl();
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
tableViewer.setInput(null);
tableViewer.refresh();
Text text = GoldSalesManager.getInstance().getDocNum();
text.setText(baseService.createNo(AppConstants.DOC_TYPE_BOOKINGGOLD));
Text matnrCode = GoldSalesManager.getInstance().getMatnrCode();
Text bookDocNum = GoldSalesManager.getInstance().getBookDocNum();
Text barCode = GoldSalesManager.getInstance().getBarCode();
bookDocNum.setText("");
barCode.setText("");
matnrCode.setText("");
caculatorGoldSalesPrice();
}
/**
* /**
* 重置投资金条现货
* @throws POSException
*/
public static void restGoldSalesView() throws POSException{
IBaseService baseService = new BaseServiceImpl();
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
tableViewer.setInput(null);
tableViewer.refresh();
Text text = GoldSalesManager.getInstance().getDocNum();
text.setText(baseService.createNo(AppConstants.DOC_TYPE_BOOKINGGOLD));
Text matnrCode = GoldSalesManager.getInstance().getMatnrCode();
Text bookDocNum = GoldSalesManager.getInstance().getBookDocNum();
Text barCode = GoldSalesManager.getInstance().getBarCode();
bookDocNum.setText("");
barCode.setText("");
matnrCode.setText("");
caculatorGoldSalesPrice();
}
/**
* 获取当前零售窗口选中的ITEM
* @return
*/
public static Item getCurrentSelectionItem() {
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
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 = GoldSalesManager.getInstance().getGoldBookingTableView();
return (List<Item>)tableViewer.getInput();
}
/**
* 计算总价
*/
public static void caculatorGoldSalesPrice() {
Label salesPriceLable = GoldSalesManager.getInstance().getGoldBookingTotlePrice();
TableViewer tableViewer = GoldSalesManager.getInstance().getGoldBookingTableView();
TableItem[] items = tableViewer.getTable().getItems();
BigDecimal totle = new BigDecimal(0);
for (TableItem tableItem : items) {
String price = tableItem.getText(7);
BigDecimal bigDecimal = ProjectUtil.formatString(price);
totle = totle.add(bigDecimal);
}
salesPriceLable.setText(ProjectUtil.formatAmount(null, totle));
}
/**
* 删除金品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 = GoldSalesManager.getInstance()
.getGoldBookingTableView();
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 control
* @param tableViewer
*/
public static void addSalesItemListener(Control control) {
control.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
Button button = GoldSalesManager.getInstance().getAddCurrentProductBtn();
boolean flag = button.getEnabled();
if(!flag){
return;
}
if (e.keyCode == SWT.CR) {
Text text = (Text) e.getSource();
Item item = null;
TableViewer tableViewer = GoldSalesManager.getInstance()
.getGoldBookingTableView();
String inputString = "";
if (e.getSource() instanceof Text) {
inputString = text.getText();
}
if (inputString == null || "".equals(inputString)) {
MessageDialog.openError(((Text)e.getSource()).getShell(), MessageResources.message_ui_tips, "【条形码】不能为空");
Text barCode = GoldSalesManager.getInstance().getBarCode();
barCode.forceFocus();
barCode.setText("");
return;
}
IBaseService baseService = new BaseServiceImpl();
try {
item = baseService.getItemByBarCode(inputString);
if (item == null) {
MessageDialog.openError(((Text)e.getSource()).getShell(), MessageResources.message_ui_tips, "没有相应的单品,请重新输入条形码");
restGoldSalesView();
text.setText("");
return;
}
List<Item> items = (List<Item>)tableViewer.getInput();
String barCode = GoldSalesManager.getInstance().getBarCode().getText();
if(items != null){
for (Item t_item : items) {
if(t_item.getItemCode().equals(barCode)){
MessageDialog.openError(((Text)e.getSource()).getShell(), MessageResources.message_ui_tips, "商品重复,请重新输入!");
text.setText("");
return;
}
}
}
BigDecimal currentPrice = baseService.getGoldPriceByMtartCode(item.getMatkl());
int userId = POSClientApp.get().getLoginUser().getId();
Map<Integer,User> map = POSClientApp.get().getUserRelation();
Iterator<Entry<Integer, User>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, User> value = (Map.Entry<Integer, User>) it.next();
if(value.getValue().getId() == userId){
item.setCashier(value.getValue().getId());
}
}
item.setItemType(AppConstants.ITEM_TYPE_CURRENTGOLD);
item.setItemStatus(AppConstants.ITEM_STATU_GOLD_NOW);
if(CommonFacade.isSuGold(item.getMatkl())){
BigDecimal price = item.getZDPZL().multiply(currentPrice);
item.setPrice(currentPrice);
item.setFactAmount(price);
item.setSalesAmount(price);
}else{
item.setPrice(item.getZMDLSBQJ());
item.setFactAmount(item.getZMDLSBQJ());
item.setSalesAmount(item.getZMDLSBQJ());
}
} catch (POSException e1) {
MessageDialog.openError(((Text)e.getSource()).getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
try {
restGoldSalesView();
} catch (POSException e2) {
}
text.setText("");
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();
//点击添加现货,预订结算和到期开票以及查询按钮不可用
GoldSalesManager.getInstance().getGoldBookingBtn().setEnabled(false);
GoldSalesManager.getInstance().getGoldTimeoutBtn().setEnabled(false);
GoldSalesManager.getInstance().getQueryBtn().setEnabled(false);
GoldSalesManager.getInstance().getAddBookingBtn().setEnabled(false);
text.setText("");
}
}
public void keyPressed(KeyEvent e) {
}
});
}
}