xj qian
2024-07-05 6e93e717799723de98287f5a036ab34367bd08cd
jrj/project/business/AR/ExchangeSignFast.cpp
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,422 @@
#include <wobject/xstring.hpp>
#include <xcontrol/xtreeview.hpp>
#include <xcontrol/xdwgrid.hpp>
#include "vbusiness/vframe/listwin.vframe.vbusiness.hpp"
#include "viewobject/view.base.hpp"
using xml = KXMLDOMDocument;
   class __declspec(dllexport) ExchangeSignFastWin : public xframe
   {
   public:
      xdwgrid   dw_unexch;
      xdwgrid   dw_list;
      xdwgrid  dw_obj;
      xdwtable dw_arg;
      xnode   m_agentNode;   //Agent Condition
   public:
      ExchangeSignFastWin(void* implPtr, HWND hWnd) :xframe(implPtr, hWnd) {}
   public:
      static ExchangeSignFastWin* CreateInstance(void* implPtr, void* hWnd)
      {
         ExchangeSignFastWin* pWin = new ExchangeSignFastWin(implPtr, (HWND)hWnd);
         return pWin;
      }
      int SetAgent()
      {
         xstring xfNodeAgentArea = L"agentarea";
         xnode anode = GetAgentNode(xfNodeAgentArea);
         if (m_agentNode)
         {
            SetAgentNode(anode, m_agentNode);
         }
         else
         {
            KXMLDOMElement xframeElement = GetElement();
            KXMLDOMElement agent = xframeElement.selectSingleNode(L"agent/" + xfNodeAgentArea + L"[1]/*");
            if (agent)
            {
               xstring s = agent.xml();
               m_agentNode = SetAgentNode(anode, s);
            }
         }
         return 1;
      }
      //焦点激活处理函数
      int OnSetFocus(TEvent* evt, LPARAM param)
      {
         //重置工具条
         SetAgent();
         return 1;
      }
      bool IsNoEmptyValue(xstring val)
      {
         if (val != L"" && val != L".00" && val != L"0.00" && val != L"-0.00")  return true;
         return false;
      }
      int OnSave()
      {
         for (int row = dw_list.GetRowCount(); row >= 1; row--)
         {
            if (dw_list.GetItemDouble(row, L"ExchAmt") != 0.0) continue;
            if (dw_list.GetItemDouble(row, L"BankDeduction") != 0.0) continue;
            if (dw_list.GetItemDouble(row, L"ForeignBankDeduction") != 0.0)continue;
            dw_list.DeleteRow(row);
            dw_list.Redraw();
         }
         xml x;
         dw_list.AcceptText();
         dw_list.DwUpdateAllToEx(x);
         //trace(x.xml());
         xml x0 = ViewObject::MakeXml();
         x0.loadXML(L"<data/>");
         KXMLDOMNodeList t = x.selectNodes(L"data/Item[@update.modify or @update.delete]");
         KXMLDOMElement e1 = x0.documentElement();
         int len = t.length();
         for (int i = 0; i < len; i++)
         {
            e1.appendChild(t.item(i));
         }
         xaserverarg arg = ViewObject::MakeArg();
         arg.AddArg(L"content", x0.xml());
         if (xurl::get(L"/sale/data/AR/update/item", arg.GetString(), x) != 1)
         {
            xstring error = x.text();
            alert(L"err:" + error);
            return 1;
         }
         xstring str = x.documentElement().getAttribute(L"text");
         if (str == L"true")
         {
            dw_list.ResetUpdateStatus();
            alert(L"保存成功!");
         }
         else
         {
            alert(L"保存失败!");
         }
         return 1;
      }
      //命令发布函数
      int OnCmdDispatch(xstring comdid)
      {
         if (comdid == L"xmSave")
         {
            return OnSave();
         }
         else if (comdid == L"xmClose")
         {
            CloseWindow();
            return 1;
         }
         else if (comdid == L"xmSearch")
         {
            return   OnRetrieve(0);
         }
         return 0;
      }
      int OnCombboChanged(ref TCommandEvent evt, int lParam)
      {
         xstring id = L"";
         xstring name = L"";
         int h = xcombobox::GetCurSel(evt.hCtrl);
         if (h > -1)
         {
            xstring txt = xcombobox::GetLBText(evt.hCtrl, h);
            if (txt == L"限定客户")
            {
               OnRetrieve(1);
            }
            else if (txt == L"不限定客户")
            {
               OnRetrieve(0);
            }
         }
         return 1;
      }
      int OnDoubleClicked(TEvent* evt, int p)
      {
         dw_unexch.AcceptText();
         int row = dw_unexch.GetRow();
         int hCursor = xutil::SetCursorWait();
         double AdjustAmount = dw_unexch.GetItemDouble(row, L"AdjustAmount");//调整数
         double amted = dw_unexch.GetItemDouble(row, L"实收汇金额");//往次
         double samt = dw_unexch.GetItemDouble(row, L"应收汇金额");//应收汇金额
         double amt = samt - amted - AdjustAmount;
         if (dw_obj)
         {
            xstring  val = dw_unexch.GetItemDouble(row, L"ExchAmt");
            if (IsNoEmptyValue(val))
            {
               dw_unexch.ItemChangeTo(row, L"ExchAmt", L"");
            }
            double amt1 = dw_obj.GetItemDouble(dw_obj.GetRow(), L"BalanceAmt");
            int v1 = (amt * 100.0).toString().toInt();
            int v2 = (amt1 * 100.0).toString().toInt();
            if (v2 > v1)
            {
               dw_unexch.ItemChangeTo(row, L"Amount", xstring(amt));
               //dw_unexch.ItemChangeTo(row,L"ExchAmt",amt.toString());
            }
            else
            {
               dw_unexch.ItemChangeTo(row, L"Amount", dw_obj.GetItemString(dw_obj.GetRow(), L"BalanceAmt"));
               //dw_unexch.ItemChangeTo(row,L"ExchAmt",dw_obj.GetItemString(dw_obj.GetRow(),L"BalanceAmt"));
            }
         }
         else
         {
            //dw_unexch.ItemChangeTo(row,L"ExchAmt",amt.toString());
            dw_unexch.ItemChangeTo(row, L"Amount", xstring(amt));
         }
         dw_unexch.Redraw();
         xutil::RestoreCursor(hCursor);
         return 1;
      }
      int evalExchAmt()
      {
         double amt = 0;
         xstring currencyCode = dw_obj.GetItemString(dw_obj.GetRow(), L"CurrencyCode");
         for (int i = 1; i <= dw_list.GetRowCount(); i++)
         {
            double amt0 = dw_list.GetItemDouble(i, L"Amount");
            xstring currencyCode1 = dw_list.GetItemString(i, L"CurrencyCode");
            if (currencyCode == L"CNY" || currencyCode == L"RMB")
            {
               if (currencyCode1 == L"USD") amt0 = amt0 * dw_list.GetItemDouble(i, L"ExchRate");
            }
            amt += amt0;
            //amt += dw_list.GetItemDouble(i,L"BankDeduction");
            //amt += dw_list.GetItemDouble(i,L"ForeignBankDeduction");
         }
         if (dw_obj)
         {
            dw_obj.SetItemDouble(dw_obj.GetRow(), L"SignAmt", amt);
            double amt1 = dw_obj.GetItemDouble(dw_obj.GetRow(), L"Amount");
            double BalanceAmt = amt1 - amt;
            dw_obj.SetItemDouble(dw_obj.GetRow(), L"BalanceAmt", BalanceAmt);
            dw_obj.Redraw();
         }
         return 1;
      }
      //命令处理事件
      int OnXCommand(TEvent* evt, LPARAM param)
      {
         return OnCmdDispatch(evt->xcommand.pStrID);
      }
      int Found(xstring InvoiceNo)
      {
         for (int row = 1; row <= dw_list.GetRowCount(); row++)
         {
            if (dw_list.GetItemString(row, L"InvoiceNo") == InvoiceNo) return row;
         }
         return 0;
      }
      int ChangedListData(xstring colname, int row, xstring value)
      {
         if (colname == L"ExchRate" || colname == L"AcctAmount" || colname == L"CustomerCapitalExpense" || colname == L"BankRMBDeduction" || colname == L"Amount" || colname == L"ExchAmt" || colname == L"BankDeduction" || colname == L"ForeignBankDeduction")
         {
            int crow = Found(dw_unexch.GetItemString(row, L"InvoiceNo"));
            if (crow < 1)
            {
               crow = dw_list.InsertRow(0);
               dw_list.SetItemString(crow, L"InvoiceNo", dw_unexch.GetItemString(row, L"InvoiceNo"));
               dw_list.SetItemString(crow, L"SEItemID", publiccode::GetGuid());
               if (dw_obj)
               {
                  if (dw_obj.GetRowCount() > 0)
                     dw_list.SetItemString(crow, L"SEID", dw_obj.GetGuid(dw_obj.GetRow()));
               }
               dw_list.SetItemString(crow, colname, value);
            }
            else
            {
               dw_list.SetItemString(crow, colname, value);
            }
            dw_list.Redraw();
            evalExchAmt();
         }
         return 1;
      }
      int OnItemChanged(TEvent* evt, LPARAM p)
      {
         DWNMHDR& hdr = *(DWNMHDR*)evt->notify.pnmh;
         xstring colname = hdr.colname;
         xstring value = hdr.data;
         int row = hdr.row;
         if (colname == L"ExchRate" || colname == L"BankDeduction" || colname == L"BankRMBDeduction" || colname == L"ForeignBankDeduction" || colname == L"ExchAmt" || colname == L"Amount" || colname == L"CustomerCapitalExpense") {
            double ExchAmt = dw_unexch.GetItemDouble(row, L"ExchAmt");
            double Amt = dw_unexch.GetItemDouble(row, L"Amount");
            double BankDeduction = dw_unexch.GetItemDouble(row, L"BankDeduction");
            double ForeignBankDeduction = dw_unexch.GetItemDouble(row, L"ForeignBankDeduction");
            double CustomerCapitalExpense = dw_unexch.GetItemDouble(row, L"CustomerCapitalExpense");
            if (colname == L"BankDeduction" || colname == L"ForeignBankDeduction" || colname == L"ExchAmt" || colname == L"CustomerCapitalExpense")
            {
               dw_unexch.SetItemDouble(row, L"ExchAmt", Amt + BankDeduction + ForeignBankDeduction + CustomerCapitalExpense);
               ChangedListData(L"ExchAmt", row, dw_unexch.GetItemString(row, L"ExchAmt"));
            }
            if (colname == L"Amount")
            {
               dw_unexch.SetItemDouble(row, L"ExchAmt", Amt + BankDeduction + ForeignBankDeduction + CustomerCapitalExpense);
               ChangedListData(L"ExchAmt", row, dw_unexch.GetItemString(row, L"ExchAmt"));
            }
            Amt = dw_unexch.GetItemDouble(row, L"Amount");
            double exchRate = dw_unexch.GetItemDouble(row, L"ExchRate");
            double rmbDeduction = dw_unexch.GetItemDouble(row, L"BankRMBDeduction");
            dw_unexch.ItemChangeTo(row, L"AcctAmount", xstring((Amt * exchRate /*- rmbDeduction*/)));
            //cala balance
            double ttlamt = dw_unexch.GetItemDouble(row, L"应收汇金额");
            double AdjustAmount = dw_unexch.GetItemDouble(row, L"AdjustAmount");
            ExchAmt = dw_unexch.GetItemDouble(row, L"ExchAmt");
            double ExchAmted = dw_unexch.GetItemDouble(row, L"实收汇金额");
            dw_unexch.SetItemDouble(row, L"未收汇金额", ttlamt - AdjustAmount - ExchAmt - ExchAmted);
            dw_unexch.Redraw();
         }
         ChangedListData(colname, row, value);
         return 1;
      }
      int OnAttachEvent()
      {
         //绑定工具条点击事件
         AttachEvent(L"WM_XCOMMAND", (FEvent)&ExchangeSignFastWin::OnXCommand);
         //获取焦点事件,用于重置工具条
         //AttachEvent(L"WM_SETFOCUS",OnSetFocus);
         AttachEvent(L"dw_unexch", L"DWV_DOUBLECLICKED", (FEvent)&ExchangeSignFastWin::OnDoubleClicked);//行双击
         AttachEvent(L"cbx_customer", L"CBN_SELCHANGE", (FEvent)&ExchangeSignFastWin::OnCombboChanged);//属性组切换list中列的数量变化
         AttachEvent(L"dw_unexch", L"DWV_ITEMCHANGED", (FEvent)&ExchangeSignFastWin::OnItemChanged);
         return 1;
      }
      xstring GetQueryArg()
      {
         dw_arg.AcceptText();
         xml x;
         dw_arg.DwUpdateAllTo(x);
         return x.xml();
      }
      int  OnRetrieve(int customer = 1)
      {
         HCURSOR  hCursor = xutil::SetCursorWait();
         xml x;
         xaserverarg arg ;
         arg.setNativePointer(arg.CreateInstance());
         int row = dw_obj.GetRow();
         if (row < 1) return 1;
         xstring CustomerID = dw_obj.GetItemString(row, L"CustomerID");
         //alert(CustomerID);
         xstring SEID = dw_obj.GetGuid(dw_obj.GetRow());
         trace(L"+++L" + CustomerID + L"+++L" + SEID);
         arg.AddArg(L"CustomerID", CustomerID);
         arg.AddArg(L"SEID", SEID);
         xstring path = L"/sale/data/AR/item/noexch/listwithcustomer";
         if (customer == 0)  path = L"/sale/data/AR/item/noexch/list";
         xstring path1 = L"/sale/data/AR/item/exch/listwithcustomer";
         if (customer == 0)  path1 = L"/sale/data/AR/item/exch/list";
         arg.AddArg(L"QueryTxt", GetControl(L"sle_search").GetText());
         arg.AddArg(L"QueryArg", GetQueryArg());
         trace(L"SEID+++L" + SEID + L"+++QueryTxt+++L" + GetControl(L"sle_search").GetText() + L"+++QueryArg+++L" + GetQueryArg());
         if (getUrl(path, arg.GetString(), x) != 1)
         {
            trace(L"错误信息1=L" + xstring(x.text()));
            xutil::RestoreCursor(hCursor);
            return -1;
         }
         else
         {
            dw_unexch.Retrieve(x);
            dw_unexch.Redraw();
         }
         //trace(x.xml());
         if (getUrl(path1, arg.GetString(), x) != 1)
         {
            trace(L"错误信息2=L" + xstring(x.text()));
            xutil::RestoreCursor(hCursor);
            return -1;
         }
         else
         {
            dw_list.Retrieve(x);
            dw_list.Redraw();
            dw_list.SetReadOnly(true);
         }
         xutil::RestoreCursor(hCursor);
         return 1;
      }
      int onload()
      {
         dw_list = GetControl(L"dw_list");
         dw_list.openUrl(L"/sale/view/AR/template/unexch");
         dw_unexch = GetControl(L"dw_unexch");
         dw_unexch.openUrl(L"/sale/view/AR/template/unexch");
         dw_list.SetColumnState(L"SEID", false);
         dw_list.SetColumnState(L"SEItemID", false);
         dw_unexch.SetColumnState(L"SEID", false);
         dw_unexch.SetColumnState(L"SEItemID", false);
         dw_arg = GetControl(L"dw_arg");
         dw_arg.openUrl(L"/sale/view/AR/template/queryarg");
         dw_arg.SetColHeaderHeight(0);
         dw_arg.SetRowSelectorWidth(0);
         dw_arg.SetHScrollState(false);
         dw_arg.SetVScrollState(false);
         //dw_arg.SetItemString(1,L"DateType",L"本年");
         /*xaserverarg arg = GetParam();
         xstring CustomerIDOne = arg.GetArgString(L"CustomerID");
         xstring SEIDOne = arg.GetArgString(L"SEID");*/
         if (GetWinParam())dw_obj = GetParam();
         OnAttachEvent();
         return 1;
      }
      int onloaded()
      {
         //SetAgent();
         if (dw_obj)OnRetrieve(1);
         return 1;
      }
   };