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.OldGold;
import com.topsun.posclient.sales.MessageResources;
/**
* 旧金鉴定
*
* @author Dong
*
*/
public class OldGoldIndentifyFacade extends CommonFacade {
/**
* 重置旧金鉴定界面
* @throws POSException
*/
public static void restOldGoldIndentifyView() throws POSException{
IBaseService baseService = new BaseServiceImpl();
TableViewer tableViewer = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
tableViewer.setInput(null);
tableViewer.refresh();
Text text = OldGoldIdentifyManager.getInstance().getDocNum();
text.setText(baseService.createNo(AppConstants.DOC_TYPE_OLDGOLD));
Text mantr = OldGoldIdentifyManager.getInstance().getMantrCode();
mantr.setText("");
mantr.forceFocus();
caculatorOldGoldTotalPrice();
}
/**
* 获取当前窗口选中的OldGold
* @return
*/
public static OldGold getCurrentSelectionItem() {
TableViewer tableViewer = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
ISelection iSelection = tableViewer.getSelection();
if (iSelection instanceof StructuredSelection) {
StructuredSelection selection = (StructuredSelection) iSelection;
Object obj = selection.getFirstElement();
if (obj instanceof OldGold) {
OldGold og = (OldGold) obj;
return og;
}
}
return null;
}
public static List<OldGold> getCurrentOldGoldList() {
TableViewer tableViewer = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
List<OldGold> oldList = (List<OldGold>)tableViewer.getInput();
for(OldGold og : oldList){
BigDecimal sum = og.getSum();
sum = sum.setScale(2, BigDecimal.ROUND_HALF_UP);
og.setSum(sum);
}
return oldList;
}
/**
* 计算总价
*/
public static void caculatorOldGoldTotalPrice() {
Label salesPriceLable = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTotlePrice();
TableViewer tableViewer = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
TableItem[] items = tableViewer.getTable().getItems();
BigDecimal totle = new BigDecimal(0);
for (TableItem tableItem : items) {
String price = tableItem.getText(11);
BigDecimal bigDecimal = ProjectUtil.formatString(price);
totle = totle.add(bigDecimal);
}
salesPriceLable.setText(ProjectUtil.formatAmount(null, totle));
}
/**
* 删除OldGold
*
* @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 = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
ISelection iSelection = tableViewer.getSelection();
if (iSelection instanceof IStructuredSelection) {
IStructuredSelection iStructuredSelection = (IStructuredSelection) iSelection;
Object obj = iStructuredSelection.getFirstElement();
if (obj instanceof OldGold) {
OldGold item = (OldGold) obj;
Object itemList = tableViewer.getInput();
if (itemList == null) {
return;
} else {
if (itemList instanceof List) {
List list = (List) itemList;
list.remove(item);
}
}
tableViewer.refresh();
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
/**
* 物料编码回车事件
* @param control
*/
public static void addOldGoldListener(final Text matnr, final String inputDocNum) {
matnr.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent e) {
Text text = null;
if (e.keyCode == SWT.CR) {
TableViewer tableViewer = OldGoldIdentifyManager.getInstance().getOldGoldIdentifyTableView();
String inputMatnr = "";
if (e.getSource() instanceof Text) {
text = (Text) e.getSource();
inputMatnr = text.getText();
}
if(null == inputMatnr || "".equals(inputMatnr)){
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, "【物料编码】不能为空");
matnr.forceFocus();
matnr.setText("");
return;
}
List<OldGold> ogList = (List<OldGold>)tableViewer.getInput();
if(null != ogList && ogList.size() > 0){
for(OldGold og : ogList){
if(og.getItemCode().equals(inputMatnr)){
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, "物料编码为【"+inputMatnr+"】的旧金数据已存在");
matnr.forceFocus();
matnr.setText("");
return;
}
}
}
IBaseService baseService = new BaseServiceImpl();
String matklCode;
try {
matklCode = baseService.getMatklByMatnr(inputMatnr);
} catch (POSException e1) {
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
matnr.forceFocus();
matnr.setText("");
return;
}
OldGold og = new OldGold();
og.setDocNum(inputDocNum);
try {
og.setName(baseService.getNameByMatnr(inputMatnr));
} catch (POSException e1) {
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
matnr.forceFocus();
matnr.setText("");
return;
}
og.setIdfCode(inputDocNum);
og.setItemCode("");
og.setLoss(CommonFacade.getLossPoint(matklCode));
String loss = CommonFacade.getLossPointString(matklCode);
if("0.01".equals(loss)){
og.setT_Loss(0);
}else if("0.12".equals(loss)){
og.setT_Loss(1);
}else if("0.15".equals(loss)){
og.setT_Loss(2);
}else{
og.setT_Loss(3);
}
og.setMtartCode(inputMatnr);
og.setNeatness(new BigDecimal(0));
try {
og.setPrice_G(baseService.getGoldPriceByMtartCode(matklCode));
} catch (POSException e1) {
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, e1.getErrorMessage());
matnr.forceFocus();
matnr.setText("");
return;
}
og.setMatkl(matklCode);
og.setRefine(new BigDecimal(0));
og.setService(new BigDecimal(0));
og.setT_ServiceFee(3);
og.setSum(new BigDecimal(0));
og.setSuppliser(og.getSuppliser());
og.setType(og.getType());
og.setWeight(new BigDecimal(0));
og.setWeight_F(new BigDecimal(0));
og.setWeight_G(new BigDecimal(0));
og.setWeight_R(new BigDecimal(0));
Object obj = tableViewer.getInput();
if (obj == null) {
List<OldGold> list = new ArrayList<OldGold>();
list.add(og);
tableViewer.setInput(list);
} else {
if (obj instanceof List) {
List<OldGold> list = (List<OldGold>) obj;
list.add(og);
tableViewer.setInput(list);
}
}
tableViewer.refresh();
matnr.forceFocus();
matnr.setText("");
}
}
public void keyPressed(KeyEvent e) {
}
});
}
/**
* 添加旧金按钮事件
* @param inputMatnr
* @param tableViewer
* @throws POSException
*/
public static void addTableOldGold(String inputMatnr, String inputDocNum, TableViewer tableViewer) throws POSException{
List<OldGold> ogList = (List<OldGold>)tableViewer.getInput();
if(null != ogList && ogList.size() > 0){
for(OldGold og : ogList){
if(og.getItemCode().equals(inputMatnr)){
MessageDialog.openInformation(tableViewer.getControl().getShell(), MessageResources.message_ui_tips, "物料编码为【"+inputMatnr+"】的旧金数据已存在");
return;
}
}
}
IBaseService baseService = new BaseServiceImpl();
String matklCode = baseService.getMatklByMatnr(inputMatnr);
OldGold og = new OldGold();
og.setDocNum(inputDocNum);
og.setName(baseService.getNameByMatnr(inputMatnr));
og.setIdfCode(inputDocNum);
og.setItemCode("");
og.setLoss(CommonFacade.getLossPoint(matklCode));
String loss = CommonFacade.getLossPointString(matklCode);
if("0.01".equals(loss)){
og.setT_Loss(0);
}else if("0.12".equals(loss)){
og.setT_Loss(1);
}else if("0.15".equals(loss)){
og.setT_Loss(2);
}else{
og.setT_Loss(3);
}
og.setMtartCode(inputMatnr);
og.setNeatness(new BigDecimal(0));
og.setPrice_G(baseService.getGoldPriceByMtartCode(matklCode));
og.setMatkl(matklCode);
og.setRefine(new BigDecimal(0));
og.setService(new BigDecimal(0));
og.setT_ServiceFee(3);
og.setSum(new BigDecimal(0));
og.setSuppliser(og.getSuppliser());
og.setType(og.getType());
og.setWeight(new BigDecimal(0));
og.setWeight_F(new BigDecimal(0));
og.setWeight_G(new BigDecimal(0));
og.setWeight_R(new BigDecimal(0));
Object obj = tableViewer.getInput();
if (obj == null) {
List<OldGold> list = new ArrayList<OldGold>();
list.add(og);
tableViewer.setInput(list);
} else {
if (obj instanceof List) {
List<OldGold> list = (List<OldGold>) obj;
list.add(og);
tableViewer.setInput(list);
}
}
tableViewer.refresh();
}
}