unit U_JYOrderYS;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, ExtCtrls, StdCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxCalendar, cxButtonEdit, cxTextEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxGridBandedTableView, cxGridDBBandedTableView,
  DBClient, ADODB, cxCurrencyEdit;

type
  TFrmJYOrderYS = class(TForm)
    Panel1: TPanel;
    Label2: TLabel;
    KHName: TEdit;
    Label3: TLabel;
    YWY: TEdit;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    TV1: TcxGridDBBandedTableView;
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    V1CRTime: TcxGridDBBandedColumn;
    V1P_CodeName: TcxGridDBBandedColumn;
    V1TTPS: TcxGridDBBandedColumn;
    V1TTQty: TcxGridDBBandedColumn;
    V1Price: TcxGridDBBandedColumn;
    V1PBMoney: TcxGridDBBandedColumn;
    Panel3: TPanel;
    Panel4: TPanel;
    V1KCPBQty: TcxGridDBBandedColumn;
    V1KCPBPS: TcxGridDBBandedColumn;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DataSource4: TDataSource;
    DataSource5: TDataSource;
    Order_PB: TClientDataSet;
    Order_RF: TClientDataSet;
    Order_XS: TClientDataSet;
    Order_FYZC: TClientDataSet;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery4: TADOQuery;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    V2Column1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn4: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    cxGridDBBandedColumn6: TcxGridDBBandedColumn;
    V2SH: TcxGridDBBandedColumn;
    V2FactoryName: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    Panel5: TPanel;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBBandedTableView;
    V5CRTime: TcxGridDBBandedColumn;
    V5YFName: TcxGridDBBandedColumn;
    V5RMBMoney: TcxGridDBBandedColumn;
    cxGridLevel5: TcxGridLevel;
    Label1: TLabel;
    ConNo: TEdit;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBBandedTableView;
    cxGridDBBandedColumn15: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    V4PRTColor: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridDBBandedColumn18: TcxGridDBBandedColumn;
    cxGridDBBandedColumn19: TcxGridDBBandedColumn;
    cxGridDBBandedColumn20: TcxGridDBBandedColumn;
    V4Column9: TcxGridDBBandedColumn;
    V4Column11: TcxGridDBBandedColumn;
    cxGridLevel4: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBBandedTableView;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    cxGridDBBandedColumn9: TcxGridDBBandedColumn;
    cxGridDBBandedColumn10: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    DataSource3: TDataSource;
    ADOQuery3: TADOQuery;
    Order_SK: TClientDataSet;
    V5BZType: TcxGridDBBandedColumn;
    V3BZType: TcxGridDBBandedColumn;
    V3Huilv: TcxGridDBBandedColumn;
    Label4: TLabel;
    MaoLMoney: TEdit;
    Label5: TLabel;
    YGMoney: TEdit;
    V1PBChang: TcxGridDBBandedColumn;
    V5Note: TcxGridDBBandedColumn;
    TDJCR: TToolButton;
    ToolButton2: TToolButton;
    TJGCK: TToolButton;
    V4QtyUnit: TcxGridDBBandedColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TDJCRClick(Sender: TObject);
  private
    procedure initgrid();
    { Private declarations }
  public
    FMainid,FConnO,FYWY,FKHName,FMaoLMoney,FYGMoney,FormID: string;       //关联合同
    { Public declarations }
  end;

var
  FrmJYOrderYS: TFrmJYOrderYS;

implementation
Uses
    U_DataLink,U_ZDYHelp,U_Fun,U_DJBCKOutList_CX;
{$R *.dfm}

procedure TFrmJYOrderYS.initgrid();
var FSKMoney,FPBMoney,FJGMoney,FZCMoney:double;
begin
  with Adoquery1 do
  begin
    close;
    sql.clear;
    sql.add('select AAA.PBChang,AAA.ConNo,AAA.CRTime,AAA.Price,AAA.P_CodeName,TTPS=cast(sum(Qty/DJRKQty*PiQty) as decimal(18,0)) ');
    sql.add(',TTQty=cast(sum(Qty/DJRKQty*PBQty) as decimal(18,2)),PBMoney=cast(sum(Qty/DJRKQty*PBQty)*Price as decimal(18,2)) ');
    sql.add('from (select AA.PBChang,C.ConNo,AA.CRTime,AA.Price,AA.P_CodeName,C.Qty,AA.PiQty,AA.Qty PBQty,AA.DJRKQty ');
    sql.add('from (select B.PBChang,B.CRTime,B.Price,isnull(M.Mainid,W.Mainid) Mainid,B.P_CodeName,isnull(Q.PiQty,B.PS) PiQty,isnull(Q.Qty,B.Qty) Qty ');
    sql.add(',DJRKQty=(select Sum(P.Qty) from CK_SXPB_CR B inner join CK_SXPB_CR P on P.FZSPID=B.SPID where B.ORDMainIdRK=isnull(M.MainId,W.Mainid) ');
    sql.add(' and P.CRFlag=''出库'' and isnull(B.CKName,'''')=''待检布'' and isnull(B.CRType,'''') in (''加工完成'',''采购入库'')) ');
    sql.add('from YF_Money_CR B inner join JYOrder_Main W on B.OrderNo=W.OrderNo ');
    sql.add('left join CK_SXPB_CR Q on B.YFTypeId=Q.FZSPID and isnull(Q.CKName,'''')=''坯布'' and Q.CRType=''加工出库'' ');
    sql.add('left join JYOrder_Main M on Q.OrdMainidCK=M.Mainid ');
    sql.add('where isnull(B.YFName,'''')=''坯布虚拟费'' ');
    if FormID='0' then
    sql.Add('and isnull(B.status,''0'')<>''0'' ');
    if FormID='1' then
    sql.Add('and isnull(B.status,''0'')=''2'' ');
    sql.add('union all ');
    sql.add('select D.FactoryName PBChang,D.CRTime,D.GCPrice Price,isnull(M.Mainid,W.Mainid) Mainid,D.P_CodeName,isnull(F.PiQty,D.ps) piQty,isnull(F.Qty,D.Qty) Qty ');
    sql.add(',DJRKQty=(select Sum(P.Qty) from CK_SXPB_CR B inner join CK_SXPB_CR P on P.FZSPID=B.SPID where B.ORDMainIdRK=isnull(M.MainId,W.Mainid) ');
    sql.add(' and P.CRFlag=''出库'' and isnull(B.CKName,'''')=''待检布'' and isnull(B.CRType,'''') in (''加工完成'',''采购入库'')) ');
    sql.add('from YF_Money_CR D inner join JYOrder_Main W on D.OrderNo=W.OrderNo ');
    sql.add('left join CK_SXPB_CR F on D.YFTypeId=F.FZSPID and isnull(F.CRType,'''') in (''加工出库'',''检验出库'') ');
    sql.add('left join JYOrder_Main M on F.OrdMainidCK=M.Mainid ');
    sql.add('where isnull(D.YFName,'''')=''坯布费'' ');
    if FormID='0' then
    sql.Add('and isnull(D.status,''0'')<>''0'' ');
    if FormID='1' then
    sql.Add('and isnull(D.status,''0'')=''2'' ');
    sql.add(') AA inner join CK_SXPB_CR C on C.ORDMainidRK=AA.Mainid and isnull(C.CKName,'''')=''待检布'' and isnull(C.CRFlag,'''')=''出库'' ');
    sql.add('and exists(select Q.SPID from CK_SXPB_CR Q where Q.SPID=C.FZSPID and isnull(Q.CRType,'''') in (''加工完成'',''采购入库''))) AAA  ');
    sql.add('where AAA.ConNo='''+trim(FConNo)+''' ');
    sql.add('group by AAA.PBChang,AAA.ConNo,AAA.CRTime,AAA.Price,AAA.P_CodeName');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_PB);
  SInitCDSData20(Adoquery1,Order_PB);
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select AAA.*,SH=(case when PBQty=0 then 0 else cast((PBQty-JGQty)/PBQty as decimal(18,2)) end) ');
    sql.add('from (select D.ConNo,AA.CRTime,AA.P_CodeName,AA.P,AA.PRTColor,JGPS=cast(sum(D.Qty/DJRKQty*AA.PS) as decimal(18,0)),JGQty=cast(sum(D.Qty/DJRKQty*AA.Qty) as decimal(18,2)),');
    sql.add('AA.GCPrice,AA.FactoryName,JGMoney=cast(sum(GCPrice*D.Qty/DJRKQty*AA.Qty) as decimal(18,2)),PBQty=cast(sum(D.Qty/DJRKQty*TPQty) as decimal(18,2)) ');
    sql.add('from (select convert(varchar(10),A.CRTime,120) CRTime,A.OrderNo,A.P_CodeName,A.PS P,Sum(A.ps) PS,sum(A.Qty) Qty,A.GCPrice,A.FactoryName,(select P.PRTColor from JYOrder_Sub P where P.SubId=A.ZSDSubid) PRTColor,A.ZSDSubid Subid,');
    sql.add('TPQty=(select SUM(Qty) from CK_SXPB_CR F where F.ORDSubIdCK=A.ZSDSubid and isnull(F.CKName,'''')=''坯布'' and isnull(F.CRType,'''')=''加工出库''),');
    sql.add('DJRKQty=(select SUM(P.Qty) from CK_SXPB_CR F inner join CK_SXPB_CR P on P.FZSPID=F.SPID where F.ORDSubIdRK=A.ZSDSubid and P.CRFlag=''出库'' and F.FactoryName=A.FactoryName ');
    sql.add('and isnull(F.CKName,'''')=''待检布'' and isnull(F.CRType,'''') in (''加工完成'',''回修完成'')) ');
    sql.add('from YF_Money_CR A ');
    sql.add('where isnull(A.YFName,'''') in (''加工费'',''回修完成费'') ');
    if FormID='0' then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if FormID='1' then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    Sql.add('and not exists (select N.SPID from CK_SXPB_CR N where N.SPID=A.Mainid and isnull(N.CKName,'''') in (''坯布'',''纱线'',''外加工'')) ');
    sql.add('group by convert(varchar(10),A.CRTime,120),A.OrderNo,A.P_CodeName,A.PS,A.GCPrice,A.FactoryName,A.ZSDSubid ');
    sql.add('union all ');
    sql.add('select convert(varchar(10),CRTime,120) CRTime,OrderNo,P_CodeName,cast(0 as decimal(18,2)) P,PS=cast(0 as decimal(18,0)),Qty=cast(0 as decimal(18,2)) ');
    sql.add(',cast(0 as decimal(18,2)) GCPrice,cast('''' as varchar(50)) FactoryName,cast('''' as varchar(50)) PRTColor,cast('''' as varchar(50)) Subid,');
    sql.add('TPQty=cast(0 as decimal(18,2)),DJRKQty=cast(0 as decimal(18,2)) ');
    sql.add('from YF_Money_CR where 1=2 ');
    sql.Add(') AA ');
    sql.add('inner join CK_SXPB_CR D on D.ordSubidRK=AA.Subid and isnull(D.CKName,'''')=''待检布'' and isnull(D.CRFlag,'''')=''出库'' and D.FactoryName=AA.FactoryName ');
    sql.add('inner join CK_SXPB_CR Q on D.FZSPID=Q.SPID and isnull(Q.CRFlag,'''')=''入库'' and isnull(Q.CRType,'''') in (''加工完成'',''回修完成'') ');
    sql.add('where D.ConNo='''+trim(FConNo)+''' ');
    sql.add('group by D.ConNo,AA.CRTime,AA.P_CodeName,AA.P,AA.PRTColor,AA.GCPrice,AA.FactoryName) AAA');
    sql.Add(' order by AAA.CRTime,AAA.GCPrice');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(Adoquery1,Order_RF);
  SInitCDSData20(Adoquery1,Order_RF);
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select A.ConNo,A.CRTime,A.QtyUnit,isnull(A.P_CodeName,E.PrtCodeName) P_CodeName,isnull(E.PRTColor,E.SOrdDefStr4) PRTColor,A.BZType,A.Price,sum(A.ps) PS,sum(A.Qty) Qty');
    sql.add(',RMBMoney=(case when (A.BZType<>''$'') and (A.BZType<>''USD'') then sum(Money) else 0 end)');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(Money) else 0 end) ');
    sql.add('from YF_Money_CR A ');
    sql.add('left join JYOrderCon_Sub E on E.Subid=A.Conid ');
    sql.add('where A.ConNo='''+trim(FConNo)+''' and A.CRType=''应收款登记'' ');
    sql.add(' and (((A.YFName=''成品销售金额'' or Isnull(A.FeeType,'''')=''其他费用'') ');
    if FormID='0' then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if FormID='1' then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    sql.add(' ) or (A.YFName<>''成品销售金额'' and A.YFName=''坯布销售金额'' ');
    if FormID='0' then
    sql.Add('and isnull(A.status,''0'')<>''0'' ');
    if FormID='1' then
    sql.Add('and isnull(A.status,''0'')=''2'' ');
    sql.Add('))');
    sql.add('group by A.ConNo,A.CRTime,A.QtyUnit,A.P_CodeName,E.PrtCodeName,E.PRTColor,E.SOrdDefStr4,A.BZType,A.Price'); //待观察
    open;
  end;
  SCreateCDS20(Adoquery1,Order_XS);
  SInitCDSData20(Adoquery1,Order_XS);
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select B.ConNo,A.CRTime,A.BZType');
    sql.add(',Huilv=isnull((select top 1 C.HuiLV from JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)');
    sql.add(',RMBMoney=cast((sum(E.RLMoney)*isnull((select TOP 1 C.HuiLV From JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)) as decimal(18,2))');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(E.RLMoney) else 0 end)');
    sql.add('from YF_Money_CR A ');
    sql.add('inner join JYOrderYSK E on E.YFID=A.YFID ');
    sql.add('inner join JYOrderCon_Main B on B.Mainid=E.Mainid ');
    sql.add('where B.ConNo='''+trim(FConNo)+''' and A.CRType=''收款登记'' ');
    sql.add('group by B.ConNo,A.CRTime,A.BZType,A.GSType');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_SK);
  SInitCDSData20(Adoquery1,Order_SK);
  with AdoQuery1 do
  begin
    close;
    sql.clear;
    sql.add('select A.ConNo,A.CRTime,A.YFName,A.BZType,A.Note ');
    sql.add(',Huilv=isnull((select top 1 C.HuiLV from JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)');
    //sql.add(',RMBMoney=(case when (A.BZType<>''$'') and (A.BZType<>''USD'') then sum(A.Money) else 0 end)');
    sql.add(',RMBMoney=cast((sum(A.Money)*isnull((select TOP 1 C.HuiLV From JYOrder_HuiLV C where A.CRTime>=C.BegDate and A.CRTime<isnull(C.EndDate,A.CRTime+1) and A.BZType<>''￥'' and A.BZType<>''RMB'' and C.GSType=A.GSType),1)) as decimal(18,2))');
    sql.add(',USDMoney=(case when (A.BZType<>''￥'') and (A.BZType<>''RMB'') then sum(A.Money) else 0 end) ');
    sql.add('from YF_Money_CR A ');
    sql.add('where A.ConNo='''+trim(FConNo)+''' and A.CRType=''应付款登记'' and isnull(FeeType,'''')=''虚拟费用'' ');
    sql.add('group by A.ConNo,A.CRTime,A.YFName,A.BZType,A.GSType,A.Note');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_FYZC);
  SInitCDSData20(Adoquery1,Order_FYZC);
end;

procedure TFrmJYOrderYS.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TFrmJYOrderYS.FormDestroy(Sender: TObject);
begin
  FrmJYOrderYS:=nil;
end;

procedure TFrmJYOrderYS.FormShow(Sender: TObject);
begin
  ReadCxBandedGrid('坯布',Tv1,'订单收付款明细');
  ReadCxBandedGrid('染费',Tv2,'订单收付款明细');
  ReadCxBandedGrid('收款',Tv3,'订单收付款明细');
  ReadCxBandedGrid('费用支出1',Tv5,'订单收付款明细');
  ReadCxBandedGrid('销售出库',Tv4,'订单收付款明细');
  ConNo.Text:=trim(FConnO);
  YWY.Text:=trim(FYWY);
  KHName.Text:=trim(FKHName);
  MaoLMoney.Text:=trim(FMaoLMoney);
  YGMoney.Text:=trim(FYGMoney);
  initgrid();
end;

procedure TFrmJYOrderYS.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid('坯布',Tv1,'订单收付款明细');
  WriteCxBandedGrid('染费',Tv2,'订单收付款明细');
  WriteCxBandedGrid('收款',Tv3,'订单收付款明细');
  WriteCxBandedGrid('费用支出1',Tv5,'订单收付款明细');
  WriteCxBandedGrid('销售出库',Tv4,'订单收付款明细');
  Close;
end;

procedure TFrmJYOrderYS.TDJCRClick(Sender: TObject);
begin
  frmDJBCKOutList_CX:=TfrmDJBCKOutList_CX.Create(self);
  with frmDJBCKOutList_CX do
  begin
    FDJConNO:=trim(self.ConNo.Text);
    if showmodal=1 then
    begin
      
    end;
    free;
  end;
end;

end.
