unit U_CKProductBCPOutHZList_HZN;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxCheckBox, Menus,
  MovePanel, cxSplitter, BtnEdit, cxTextEdit, cxPC;

type
  TfrmCKProductBCPOutHZList_HZN = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    Label3: TLabel;
    Label5: TLabel;
    C_CodeName: TEdit;
    ConNo: TEdit;
    CDS_DH: TClientDataSet;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv0: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet0: TClientDataSet;
    DataSource0: TDataSource;
    v0Column1: TcxGridDBColumn;
    v0Column2: TcxGridDBColumn;
    v0Column3: TcxGridDBColumn;
    Label6: TLabel;
    CustomerNoName: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    ToKHName: TEdit;
    ToOrderNo: TEdit;
    v0Column4: TcxGridDBColumn;
    Label9: TLabel;
    ToGYSName: TEdit;
    cxTabControl1: TcxTabControl;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure RadioGroup1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;
    { Private declarations }
  public
    FYear:string;
    { Public declarations }
  end;

var
  frmCKProductBCPOutHZList_HZN: TfrmCKProductBCPOutHZList_HZN;

implementation
uses
  U_DataLink,U_Fun, U_ZdyAttachGYS;

{$R *.dfm}

procedure TfrmCKProductBCPOutHZList_HZN.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutHZList_HZN:=nil;
end;

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

procedure TfrmCKProductBCPOutHZList_HZN.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(',B.ConNo,B.ComTaiTou,C.C_CodeName,XSConNo=JCM.ConNO,JCM.CustomerNoName');
      sql.Add(',ToKHNameJC=(select zdyNameZ from KH_Zdy_Attachment KZA where KZA.ZdyName=A.ToKHName and KZA.Type=''KHName'' )');
      sql.Add(',ToGYSNameJC=(select zdyNameZ from KH_Zdy_Attachment KZA where KZA.ZdyName=A.ToGYSName and KZA.Type=''GYS'' )');
      sql.Add(',CustomerNoNameJC=(select zdyNameZ  from KH_Zdy_Attachment KZA where KZA.ZdyName=JCM.CustomerNoName and KZA.Type=''KHName'' )');
      sql.Add('from( ');
      sql.Add('select MainId,SubId,CKOrdNo,ToOrderNo,ToKHName,ToGYSName,CRNote,CRTime=Convert(varchar(10),CRTime,120),HZPS=Count(*),HZQty=Sum(KGQty)');
      sql.Add(' from CK_PBCP_CR PB');
      sql.add(' where CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
      sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      SQL.Add(' and CRFlag=''出库'' ');
      sql.Add('Group by MainId,SubId,CKOrdNo,ToOrderNo,ToKHName,ToGYSName,CRNote,Convert(varchar(10),CRTime,120) )A');
      Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
      Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
      sql.Add(' left join JYOrderCon_Main JCM on B.ConMainId=JCM.MainId');
      sql.Add(' left join JYOrderCon_Sub JCS on B.ConSubId=JCS.SubId');
      if CxTabControl1.TabIndex=0 then
      begin
        sql.Add(' where (isnull(A.ToKHName,'''')<>'''' or isnull(JCM.Mainid,'''')<>'''')');
      end;
      if CxTabControl1.TabIndex=1 then
      begin
        sql.Add(' where isnull(A.ToKHName,'''')='''' and isnull(JCM.Mainid,'''')=''''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet0);
    SInitCDSData20(ADOQueryMain,ClientDataSet0);
  finally
    ADOQueryMain.DisableControls;
  end;
end;

procedure TfrmCKProductBCPOutHZList_HZN.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKProductBCPOutHZList_HZN.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCPOutHZList_HZN.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库出库列表FY4',Tv0,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutHZList_HZN.FormShow(Sender: TObject);
begin
  ReadCxGrid('仓库出库列表FY4',Tv0,'成品仓库');
  if FYear<>'' then
  begin
    BegDate.DateTime:=strtodate(FYear);
    EndDate.DateTime:=strtodate(FYear);
  end
  else
  begin
    BegDate.Date:=SGetServerDateTime(ADOQueryTemp);
    Enddate.Date:=SGetServerDateTime(ADOQueryTemp);
  end;
  InitGrid();
end;

procedure TfrmCKProductBCPOutHZList_HZN.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('坯布出库列表',cxGrid1);
  //SelExportData(Tv0,ADOQueryMain,'');
end;

procedure TfrmCKProductBCPOutHZList_HZN.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,ClientDataSet0);
    SInitCDSData20(ADOQueryMain,ClientDataSet0);
  end;
end;

procedure TfrmCKProductBCPOutHZList_HZN.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutHZList_HZN.orderNoChange(Sender: TObject);
begin
  if Length(ConNo.Text)<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutHZList_HZN.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCKProductBCPOutHZList_HZN.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCKProductBCPOutHZList_HZN.TBCKCXClick(Sender: TObject);
var
  FFMainId,FPrice,FHZ:String;
begin
  if CDS_Main.IsEmpty then Exit;
  
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryDH do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select SubId=Cast('''' as varchar(20)) ,SHCompany=Cast('''' as varchar(40)),HZ=Cast('''' as varchar(100)) ');
    Open;
  end;
  SCreateCDS20(ADOQueryDH,CDS_DH);
  SInitCDSData20(ADOQueryDH,CDS_DH);
  if CDS_DH.IsEmpty=False then
  begin
    CDS_DH.Delete;
  end;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      //First;
      while CDS_Main.Locate('SSel',True,[])=True do
      begin
        if Trim(FHZ)='' then
        begin
          with CDS_DH do
          begin
            Append;
            FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
            FieldByName('SHCompany').Value:=Trim(CDS_Main.fieldbyname('SHCompany').AsString);
            FieldByName('HZ').Value:=Trim(CDS_Main.fieldbyname('SubId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
            Post;
          end;
          FHZ:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
        end else
        begin
          if Trim(FHZ)<>Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString) then
          begin
            with CDS_DH do
            begin
              Append;
              FieldByName('SubId').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString);
              FieldByName('SHCompany').Value:=Trim(CDS_Main.fieldbyname('SHCompany').AsString);
              FieldByName('HZ').Value:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
              Post;
            end;
            FHZ:=Trim(CDS_Main.fieldbyname('MainId').AsString)+Trim(CDS_Main.fieldbyname('SHCompany').AsString);
          end;
        end;
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into CK_PBCP_CR_Log select * from CK_PBCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add(' update CK_PBCP_CR_Log Set Note='''+Trim(DName)+'''+convert(varchar(20),GETDATE(),120)');
            sql.Add(' where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add('delete CK_PBCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add('UPdate CK_PBCP_KC Set KCKgQty=(select KgQty from CK_PBCP_CR A where  A.CRID=CK_PBCP_KC.CRID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_PBCP_CR A where  A.CRID=CK_PBCP_KC.CRID and A.CRType=''检验入库'') ');
            SQL.Add(' where CRID='+CDS_Main.fieldbyname('CRID').AsString);
            ExecSQL;
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;
end;

function TfrmCKProductBCPOutHZList_HZN.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from JYOrder_Sub Where Mainid='''+Trim(Order_Main10.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
  OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main10.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' and Price='''+Trim(Order_Main10.fieldbyname('PRTPrice').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取坯布应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price)');
    sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price )');
    sql.Add(' where YFTypeId='''+Trim(Order_Main10.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' and Price='+Order_Main10.fieldbyname('PRTPrice').AsString);
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('Qty').Value=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
      ExecSQL;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where CRId='+CRID);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_KC where CRId='+CRID);
      ExecSQL;
    end;
  end;
  Result:=True;
end;

procedure TfrmCKProductBCPOutHZList_HZN.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutHZList_HZN.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.* ');
        sql.Add(',B.ConNo,B.ComTaiTou,C.C_CodeName,XSConNo=JCM.ConNO,JCM.CustomerNoName');
        sql.Add(',C.MFQty,C.KZQty,C.C_Note,C.HuaWei,JCM.XSType');
        sql.Add(',Case when isnull(C.HuaWei,'''')='''' then null else ''花位:''+RTrim(C.HuaWei) end as HuaWei');
        sql.Add(',ToKHNameJC=(select zdyNameZ  from KH_Zdy_Attachment KZA where KZA.ZdyName=A.ToKHName and KZA.Type=''KHName'' )');
        sql.Add(',ToGYSNameJC=(select zdyNameZ  from KH_Zdy_Attachment KZA where KZA.ZdyName=A.ToGYSName and KZA.Type=''GYS'' )');
        sql.Add(',CustomerNoNameJC=(select zdyNameZ  from KH_Zdy_Attachment KZA where KZA.ZdyName=JCM.CustomerNoName and KZA.Type=''KHName'' )');
        sql.Add('from(');
        sql.Add('select PB.MainId,PB.SubId,PB.CKOrdNo,PB.ToOrderNo,PB.ToKHName,PB.ToGYSName,PB.CRNote,CRTime=Convert(varchar(10),CRTime,120),HZPS=Count(*),HZQty=Sum(KGQty)');
        sql.Add(' from CK_PBCP_CR PB');
        sql.Add(' inner join Cloth_Main PBCM on PB.MainId=PBCM.MainId');
        sql.add('where PBCM.ConNo='''+Trim(ConNo.Text)+'''');
        SQL.Add(' and PB.CRFlag=''出库'' ');
        sql.Add('Group by PB.MainId,PB.SubId,PB.CKOrdNo,PB.ToOrderNo,PB.ToKHName,PB.ToGYSName,PB.CRNote,Convert(varchar(10),PB.CRTime,120) )A');
        Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
        Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
        sql.Add(' left join JYOrderCon_Main JCM on B.ConMainId=JCM.MainId');
        sql.Add(' left join JYOrderCon_Sub JCS on B.ConSubId=JCS.SubId');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,ClientDataSet0);
      SInitCDSData20(ADOQueryMain,ClientDataSet0);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutHZList_HZN.RadioGroup1Click(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmCKProductBCPOutHZList_HZN.cxTabControl1Change(
  Sender: TObject);
begin
  initgrid();
end;

end.
