unit U_XiaoShouDataOldListQB;

interface

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

type
  TfrmXiaoShouDataOldListQB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MPRTCodeName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label5: TLabel;
    PRTColor: TEdit;
    labMYType: TLabel;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Label6: TLabel;
    PRTHX: TEdit;
    v1Column8: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column2: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1SSel: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_Sub: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    ToolButton2: TToolButton;
    Panel3: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    KHName: TBtnEditC;
    Label7: TLabel;
    CustomerNoName: TEdit;
    ToolButton3: TToolButton;
    Button1: TButton;
    Panel10: TPanel;
    Image2: TImage;
    Label8: TLabel;
    KHDept: TComboBox;
    Label9: TLabel;
    KHLXR: TComboBox;
    v1Column18: TcxGridDBColumn;
    Label10: TLabel;
    KHLXFS: TEdit;
    Label11: TLabel;
    RadioGroup1: TRadioGroup;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    RMXLSExport2: TRMXLSExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1SSelPropertiesEditValueChanged(Sender: TObject);
    procedure KHNameBtnUpClick(Sender: TObject);
    procedure KHNameBtnDnClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure KHLXRChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function SaveData():Boolean;
    procedure initDataMX(FCKNO:String);
    function YFData():Boolean;
    { Private declarations }
  public
    FKHName:String;
    { Public declarations }
  end;
                 
var
  frmXiaoShouDataOldListQB: TfrmXiaoShouDataOldListQB;

implementation
uses
  U_DataLink,U_RTFun,U_KHListSelJJ;

{$R *.dfm}

procedure TfrmXiaoShouDataOldListQB.FormDestroy(Sender: TObject);
begin
  frmXiaoShouDataOldListQB:=nil;
end;

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

procedure TfrmXiaoShouDataOldListQB.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmXiaoShouDataOldListQB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('老系统销售信息QBYX',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmXiaoShouDataOldListQB.InitGrid();
begin
  Panel2.Visible:=True;
  Panel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      Sql.Add('Select CRTime,CKOrdNo,QtyUnit,PS,Qty,ConNo,CustomerNoName,MPRTCodeName ');
      sql.Add(',PRTColor,MPRTMF,PRTKZ,SOrdDefStr1,PRTHX,PRTPrice,HZFee,FHMoney,BZType');
      sql.Add(',YWY=(select DefStr5 from KH_Zdy_Attachment KZA where KZA.ZdyName=AAA.CustomerNoName and KZA.Type=''KHName'' )');
      sql.Add('from(');
      sql.Add('select AA.CRTime,AA.CKOrdNo,AA.QtyUnit,AA.PS,AA.Qty,JM.ConNo,JM.CustomerNoName,JM.MPRTCodeName ');
      sql.Add(',JS.PRTColor,JM.MPRTMF,JS.PRTKZ,JS.SOrdDefStr1,JS.PRTHX');
      sql.Add(',JS.PRTPrice,HZFee=isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
      sql.Add(',FHMoney=Cast(JS.PRTPrice*AA.Qty as decimal(18,2))+isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
      sql.Add(',Case when isnull(JS.PriceUnit,'''')=''人民币'' then ''￥''  ');
      sql.Add(' when isnull(PriceUnit,'''')=''RMB'' then ''￥''  ');
      sql.Add(' when isnull(PriceUnit,'''')='''' then ''￥''  ');
      sql.Add(' else ''$'' end as BZType ');
      sql.Add('from (');
      sql.Add(' select MainId,SubId,CRTime,CKOrdNo,QtyUnit,PS=Count(*),Qty=sum(Qty) ');
      sql.Add(' from CK_BanCP_CR A  ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' where CRTime>=''2020-02-15'' ');
      end else
      begin
        sql.Add(' where CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      sql.Add(' and isnull(A.CRType,'''')=''销售出库'' ');
      sql.Add(' and isnull(A.CKOrdNo,'''')<>'''' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and  exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
      end;
      sql.Add('Group by MainId,SubId,CRTime,CKOrdNo,QtyUnit)AA');
      sql.Add('inner join JYOrder_Sub JS on AA.SubId=JS.SubId ');
      sql.Add('inner join JYOrder_Main JM on JS.MainId=JM.MainId ');
      sql.Add('Union All ');

      sql.Add('select CRTime,CKOrdNo=InOutNo,QtyUnit,PS=Sum(RollNum),Qty=Sum(Qty),ConNo,CustomerNoName=CustName');
      sql.Add(',MPRTCodeName=P_CodeName,PRTColor=P_Color,MPRTMF=Cast(P_MF as varchar(20)),PRTKZ=Cast(P_KZ as varchar(20)),SOrdDefStr1=P_ColNo,PRTHX=P_HX');
      sql.Add(',PRTPrice=Price,HZFee=0.00,FHMoney=sum(Money),BZType=''￥'' ');
      sql.Add(' from CP_InOut1 A ');
      SQL.Add(' where CRType=''销售出库'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and CRTime>=''2020-02-15'' ');
        sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.InOutNo,'''') )');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        sql.Add(' and  exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.InOutNo,'''') )');
      end;
      sql.Add('Group by CRTime,InOutNo,QtyUnit,ConNo,CustName,P_CodeName,P_Color,P_MF,P_KZ,P_ColNo,P_HX,Price');
      sql.Add(')AAA order by CRTime,CKOrdNo');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
  Panel2.Visible:=False;
end;


procedure TfrmXiaoShouDataOldListQB.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('老系统销售信息QBYX',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmXiaoShouDataOldListQB.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmXiaoShouDataOldListQB.TBRafreshClick(Sender: TObject);
begin
  InitGrid();

end;

procedure TfrmXiaoShouDataOldListQB.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmXiaoShouDataOldListQB.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmXiaoShouDataOldListQB.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmXiaoShouDataOldListQB.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmXiaoShouDataOldListQB.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmXiaoShouDataOldListQB.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmXiaoShouDataOldListQB.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if cxTabControl1.TabIndex=0 then
  begin
    if CustomerNoName.Text='' then
    begin
      v1SSel.Options.Focusing:=False;
    end else
    begin
      v1SSel.Options.Focusing:=True;
    end;
  end;

end;

procedure TfrmXiaoShouDataOldListQB.v1SSelPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,Fmvalue:Boolean;
  FCKOrdNo:string;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
    end;
    Fmvalue:=False;
  end else
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
    end;
    mvalue:=False;
    Fmvalue:=True;
  end;
  FCKOrdNo:=Trim(Order_Main.fieldbyname('CKOrdNo').AsString);
  with Order_Main do
  begin
    First;
    while Locate('CKOrdNo;SSel', VarArrayOf([FCKOrdNo, Fmvalue]), [loPartialKey]) do
    begin
      Edit;
      FieldByName('SSel').Value:=mvalue;
    end;
  end;
end;

procedure TfrmXiaoShouDataOldListQB.KHNameBtnUpClick(Sender: TObject);
var
  fsj:string;
begin
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.KHName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHLXR').AsString);
        Self.KHName.Hint:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNo').AsString);
        Self.KHDept.Clear;
        Self.KHLXR.Clear;
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
  fsj:='select Name=KHDept from CK_YCL_CR_Main where isnull(FactoryName,'''')='''+Trim(KHName.Text)+''' and isnull(KHDept,'''')<>''''  Group by KHDept';
  SInitComBoxBySql(ADOQueryTemp,KHDept,False,fsj);
  fsj:='select Name=KHLXR from CK_YCL_CR_Main where isnull(FactoryName,'''')='''+Trim(KHName.Text)+''' and isnull(KHLXR,'''')<>''''  Group by KHLXR';
  SInitComBoxBySql(ADOQueryTemp,KHLXR,False,fsj);
  if KHLXR.Items.Count<1 then
  begin
    KHLXR.Text:=Trim(KHName.TxtCode);
  end;
end;

procedure TfrmXiaoShouDataOldListQB.KHNameBtnDnClick(Sender: TObject);
begin
  KHName.Text:='';
  KHName.TxtCode:='';
end;

procedure TfrmXiaoShouDataOldListQB.N1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then
  begin
    if CustomerNoName.Text='' then Exit;
  end;
  SelOKNo(Order_Main,True);
end;

procedure TfrmXiaoShouDataOldListQB.N2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then
  begin
    if CustomerNoName.Text='' then Exit;
  end;
  SelOKNo(Order_Main,False);
end;
procedure TfrmXiaoShouDataOldListQB.initDataMX(FCKNO:String);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.CRTime,AA.CKOrdNo,AA.QtyUnit,AA.JQty,AA.HSQty,JM.ConNo,JM.CustomerNoName ');
    sql.Add(',YWY=(select DefStr5 from KH_Zdy_Attachment KZA where KZA.ZdyName=JM.CustomerNoName and Type=''KHName'' )');
    sql.Add(',SPName=JM.MPRTCodeName,SPColor=JS.PRTColor,SPMF=JM.MPRTMF,SPKZ=JS.PRTKZ,SPColorNo=JS.SOrdDefStr1,SPHX=JS.PRTHX,Note=SOrdDefNote8 ');
    sql.Add(',SPID=SOrdDefNote8');
    sql.Add(',Case when isnull(JS.PriceUnit,'''')=''人民币'' then ''￥''  ');
    sql.Add(' when isnull(PriceUnit,'''')=''RMB'' then ''￥''  ');
    sql.Add(' when isnull(PriceUnit,'''')='''' then ''￥''  ');
    sql.Add(' else ''$'' end as BZType ');
    sql.Add(',Price=JS.PRTPrice,HZFee=isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
    sql.Add(',Money=Cast(JS.PRTPrice*AA.HSQty as decimal(18,2))+isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
    sql.Add('from (');
    sql.Add(' select MainId,SubId,CRTime,CKOrdNo,QtyUnit,JQty=Count(*),HSQty=sum(Qty) ');
    sql.Add(' from CK_BanCP_CR A  ');
    sql.Add(' where isnull(A.CKOrdNo,'''')='''+Trim(FCKNO)+'''');
    sql.Add(' and isnull(A.CKOrdNo,'''')<>'''' ');
    sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
    sql.Add('Group by MainId,SubId,CRTime,CKOrdNo,QtyUnit)AA');
    sql.Add('inner join JYOrder_Sub JS on AA.SubId=JS.SubId ');
    sql.Add('inner join JYOrder_Main JM on JS.MainId=JM.MainId ');

    sql.Add('Union All ');

    sql.Add('select CRTime,CKOrdNo=InOutNo,QtyUnit,JQty=Sum(RollNum),HSQty=Sum(Qty),ConNo,CustomerNoName=CustName');
    sql.Add(',YWY=(select DefStr5 from KH_Zdy_Attachment KZA where KZA.ZdyName=A.CustName and Type=''KHName'' )');
    sql.Add(',SPName=P_CodeName,SPColor=P_Color,SPMF=Cast(P_MF as varchar(20)),SPKZ=Cast(P_KZ as varchar(20)),SPColorNo=P_ColNo,SPHX=P_HX');
    sql.Add(',Note=Cast(Null as varchar(200)),SPID=Cast(Null as varchar(200)),BZType=''￥'' ');
    sql.Add(',Price,HZFee=0.00,Money=sum(Money)');
    sql.Add(' from CP_InOut1 A ');
    sql.Add(' where  isnull(A.InOutNo,'''')='''+Trim(FCKNO)+'''');
    SQL.Add(' and CRType=''销售出库'' ');
    sql.Add('Group by CRTime,InOutNo,QtyUnit,ConNo,CustName,P_CodeName,P_Color,P_MF,P_KZ,P_ColNo,P_HX,Price');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;
function TfrmXiaoShouDataOldListQB.SaveData():Boolean;
var
  FCRID,Maxno,FFSPID,MMaxNo:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      //while Locate('SSel',True,[]) do
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQuery1 do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_YCL_CR_Main where CKNO='''+Trim(Order_Main.fieldbyname('CKOrdNo').AsString)+'''');
            Open;
          end;
          if ADOQuery1.IsEmpty=False then
          begin
            Order_Main.Delete;
          end else
          begin
            initDataMX(Trim(Order_Main.fieldbyname('CKOrdNo').AsString));
            if GetLSNo(ADOQueryCmd,MMaxNo,'SM','CK_YCL_CR_Main',4,1)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取销售码单登记号失败!','提示',0);
              Exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from CK_YCL_CR_Main where SMID='''+Trim(MMaxNo)+'''');
              Open;
            end;
            with ADOQueryCmd do
            begin
              Append;
              FieldByName('Filler').Value:=Trim(DName);
              FieldByName('SMID').Value:=Trim(MMaxNo);
              FieldByName('CKName').Value:='销售出库';
              FieldByName('CRFlag').Value:='出库';
              FieldByName('CRType').Value:='老系统销售';
              FieldByName('CRQtyFlag').Value:=-1;
              FieldByName('FactoryName').Value:=Trim(KHName.Text);
              FieldByName('FactoryNo').Value:=Trim(KHName.Hint);
              FieldByName('KHLXR').Value:=Trim(KHLXR.Text);
              FieldByName('KHDept').Value:=Trim(KHDept.Text);
              FieldByName('KHLXFS').Value:=Trim(KHLXFS.Text);
              FieldByName('KHNameOld').Value:=Trim(Order_Main.fieldbyname('CustomerNoName').AsString);
              FieldByName('YWYOld').Value:=Trim(Order_Main.fieldbyname('YWY').AsString);
              FieldByName('CKNO').Value:=Order_Main.fieldbyname('CKOrdNo').Value;
              FieldByName('CRTime').Value:=Order_Main.fieldbyname('CRTime').Value;
              Post;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update CK_YCL_CR_Main Set YWY=(select YWY from KH_Main where KHNO='''+Trim(KHName.Hint)+''')');
              sql.Add(' where SMID='''+Trim(MMaxNo)+'''');
              ExecSQL;
            end;
            CDS_Sub.DisableControls;
            with CDS_Sub do
            begin
              First;
              while not eof do
              begin
                if GetLSNo(ADOQueryCmd,Maxno,'SC','CK_YCL_CR',4,1)=False then
                begin
                  Result:=False;
                  ADOQueryCmd.Connection.RollbackTrans;
                  Application.MessageBox('取成品出库子编号失败!','提示',0);
                  Exit;
                end;
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('select * from CK_YCL_CR where SPID='''+Trim(Maxno)+'''');
                  Open;                   
                end;
                with ADOQueryCmd do
                begin
                  Append;
                  FieldByName('SMID').Value:=Trim(MMaxno);
                  FieldByName('SPID').Value:=Trim(Maxno);
                  RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_Sub,'CK_YCL_CR',2);
                  if RadioGroup1.ItemIndex=0 then
                  begin
                    FieldByName('HanShui').Value:='含税';
                  end else
                  begin
                    FieldByName('HanShui').Value:='不含税';
                  end;
                  FieldByName('Filler').Value:=Trim(DName);
                  Post;
                end;

                Edit;
                FieldByName('SPID').Value:=Trim(Maxno);
                Post;
                YFData();
                Next;
              end;
            end;
            CDS_Sub.EnableControls;
          end;
        end;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;


procedure TfrmXiaoShouDataOldListQB.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    exit;
  end;
  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('指定客户不能为空!','提示',0);
    exit;
  end;
  if Trim(KHName.Hint)='' then
  begin
    Application.MessageBox('请重新选择客户!','提示',0);
    exit;
  end;
  if Trim(KHLXR.Text)='' then
  begin
    Application.MessageBox('联系人不能为空!','提示',0);
    exit;
  end;
  if Application.MessageBox('确定要登记数据吗？','提示',32+4)<>IDYES then Exit;
  Panel2.Visible:=True;
  Panel2.Refresh;
  if SaveData() then
  begin
    Panel2.Visible:=False;
    KHName.Text:='';
    KHName.TxtCode:='';
    KHName.Hint:='';
    Panel3.Visible:=False;
    Application.MessageBox('登记成功!','提示',0);
    Exit;
  end;
end;
function TfrmXiaoShouDataOldListQB.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName,FFactoryNo:String;
begin
  Result:=False;
  FFactoryName:=Trim(KHName.Text);
  FFactoryNo:=Trim(KHName.Hint);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YS_Money_KC where FactoryName='+quotedstr(Trim(FFactoryName)));
    sql.Add(' and KCType=''KH'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YS_Money_CRID select 0 ');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end;

    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(FFactoryName);
      FieldByName('FactoryNo').Value:=Trim(FFactoryNo);
      FieldByName('ZdyStr1').Value:='应收收';
      FieldByName('KCType').Value:='KH';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where FactoryNo='+quotedstr(Trim(FFactoryNo)));
    sql.Add(' and YFTypeId='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'YS','YS_Money_CR',4,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('select * from YS_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      Append;
      FieldByName('SCQty').Value:=CDS_Sub.fieldbyname('HSQty').AsFloat;
      FieldByName('SCPS').Value:=CDS_Sub.fieldbyname('JQty').AsFloat;
    end
    else
    Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(CDS_Sub.fieldbyname('SPID').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应收款登记';
    FieldByName('CRFlag').Value:='应收收';
    FieldByName('ChkStatusP').Value:='未审核';
    FieldByName('ChkStatusQ').Value:='未审核';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryName').Value:=Trim(FFactoryName);
    FieldByName('FactoryNo').Value:=Trim(FFactoryNo);
    FieldByName('CRTime').Value:=Order_Main.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value:=CDS_Sub.fieldbyname('HSQty').AsFloat;
    FieldByName('PS').Value:=CDS_Sub.fieldbyname('JQty').AsFloat;
    FieldByName('ConNo').Value:=CDS_Sub.fieldbyname('ConNo').Value;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('Price').Value:=CDS_Sub.fieldbyname('Price').Value;
    FieldByName('HSPrice').Value:=CDS_Sub.fieldbyname('Price').Value;
    //FieldByName('JSPrice').Value:=CDS_Sub.fieldbyname('Price').Value;
    if RadioGroup1.ItemIndex=0 then
    begin
      FieldByName('HanShui').Value:='含税'
    end else
    begin
      FieldByName('HanShui').Value:='不含税'
    end;
    FieldByName('CKNO').Value:=Order_Main.fieldbyname('CKOrdNo').Value;
    FieldByName('SJQty').Value:=CDS_Sub.fieldbyname('HSQty').Value;
    FieldByName('HZFee').Value:=CDS_Sub.fieldbyname('HZFee').Value;
    FieldByName('money').Value:=CDS_Sub.fieldbyname('money').Value;
    FieldByName('JSMoney').Value:=CDS_Sub.fieldbyname('money').Value;
    FieldByName('HSMoney').Value:=CDS_Sub.fieldbyname('money').Value;
    FieldByName('QtyUnit').Value:=Trim(CDS_Sub.fieldbyname('QtyUnit').AsString);
    FieldByName('P_CodeName').Value:=Trim(CDS_Sub.fieldbyname('SPName').AsString);
    FieldByName('P_MF').Value:=Trim(CDS_Sub.fieldbyname('SPMF').AsString);
    FieldByName('P_KZ').Value:=Trim(CDS_Sub.fieldbyname('SPKZ').AsString);
    FieldByName('P_Color').Value:=Trim(CDS_Sub.fieldbyname('SPColor').AsString);
    FieldByName('P_HX').Value:=Trim(CDS_Sub.fieldbyname('SPHX').AsString);
    FieldByName('BZType').Value:=Trim(CDS_Sub.fieldbyname('BZType').AsString);
    FieldByName('status').Value:='0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_KC Set KCMoney=(select isnull(Sum(HSMoney*QtyFlag),0) from YS_Money_CR A where A.CRID=YS_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;

procedure TfrmXiaoShouDataOldListQB.ToolButton2Click(Sender: TObject);
var
  FSMID:String;
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_YCL_CR_Main where CKNO='''+Trim(Order_Main.fieldbyname('CKOrdNo').AsString)+'''');
          Open;
        end;
        FSMID:=Trim(ADOQueryTemp.fieldbyname('SMID').AsString);
        ADOQueryTemp.Close;
        if Trim(FSMID)<>'' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YS_Money_CR where YFTypeId in');
            sql.Add('(select SPID from CK_YCL_CR where SMID='''+Trim(FSMID)+''')');
            sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
            sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' delete YS_Money_CR where YFTypeId in');
              sql.Add('(select SPID from CK_YCL_CR where SMID='''+Trim(FSMID)+''')');
              sql.Add(' delete CK_YCL_CR where SMID='''+Trim(FSMID)+'''');
              sql.Add(' delete CK_YCL_CR_Main where SMID='''+Trim(FSMID)+'''');
              ExecSQL;
            end;
          end
          else
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('财务应收已审核,不能撤销','提示');
            exit;
          end;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常!','提示',0);
  end;
end;

procedure TfrmXiaoShouDataOldListQB.Image2Click(Sender: TObject);
begin
  Panel3.Visible:=False;
end;

procedure TfrmXiaoShouDataOldListQB.ToolButton3Click(Sender: TObject);
var
  fsj:string;
begin
  if Order_Main.Locate('ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  Self.KHDept.Clear;
  Self.KHLXR.Clear;
  Panel3.Visible:=True;
  Panel3.Refresh;
  if Order_Main.Locate('SSel',True,[]) then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1 * from CK_YCL_CR_Main where isnull(KHNameOld,'''')='''+Trim(Order_Main.fieldbyname('CustomerNoName').AsString)+'''');
      sql.Add(' order by CRTime desc ');
      Open;
    end;
    KHName.Text:=Trim(ADOQueryTemp.fieldbyname('FactoryName').AsString);
    KHName.Hint:=Trim(ADOQueryTemp.fieldbyname('FactoryNo').AsString);
  end;
  if Trim(KHName.Text)<>'' then
  begin
    fsj:='select Name=KHDept from CK_YCL_CR_Main where isnull(FactoryName,'''')='''+Trim(KHName.Text)+''' and isnull(KHDept,'''')<>''''  Group by KHDept';
    SInitComBoxBySql(ADOQueryTemp,KHDept,False,fsj);
    fsj:='select Name=KHLXR from CK_YCL_CR_Main where isnull(FactoryName,'''')='''+Trim(KHName.Text)+''' and isnull(KHLXR,'''')<>''''  Group by KHLXR';
    SInitComBoxBySql(ADOQueryTemp,KHLXR,False,fsj);
    if KHLXR.Items.Count<1 then
    begin
      KHLXR.Text:=Trim(KHName.TxtCode);
    end;
  end;
end;

procedure TfrmXiaoShouDataOldListQB.Button1Click(Sender: TObject);
begin
  ToolButton1.Click;
  if application.MessageBox('是否打印码单','提示',1)=2 then
  begin
    InitGrid();
    exit;
  end
  else
  begin
    ToolButton4.Click;
    InitGrid();
  end;
end;

procedure TfrmXiaoShouDataOldListQB.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel3).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmXiaoShouDataOldListQB.KHLXRChange(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1* from CK_YCL_CR_Main where isnull(FactoryName,'''')='''+Trim(KHName.Text)+'''');
    sql.Add(' and isnull(KHLXR,'''')='''+Trim(KHLXR.Text)+'''');
    sql.Add(' order by CRTime desc ');
    Open;
  end;
  KHLXFS.Text:=Trim(ADOQueryTemp.fieldbyname('KHLXFS').AsString);
end;

procedure TfrmXiaoShouDataOldListQB.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if CustomerNoName.Text='' then
  begin
    application.MessageBox('查询条件客户不能为空','提示');
    exit;
  end;
  if Order_Main.Locate('ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\销售码单新.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with Order_Main do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQuery1 do
        begin
          Filtered:=False;
          Close;
          sql.Clear;
          sql.Add(' select distinct isnull(D.MJID,C.MJID) MJID');
          sql.Add(' from CK_YCL_CR A ');
          sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
          sql.Add(' left join CP_InOut1 C on C.InOutNo=B.CKNo');
          sql.Add(' left join CK_BanCP_CR D on D.CKOrdNo=B.CKNo');
          sql.add(' where B.CKNO='''+Trim(Order_Main.fieldbyname('CKOrdNo').AsString)+'''');
          Open;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM TBSubID where 1=2 ');
          open;
        end;
        with ADOQuery1 do
        begin
          first;
          while not eof do
          begin
            ADOQueryTemp.append;
            ADOQueryTemp.fieldbyname('SubId').Value:=Trim(ADOQuery1.fieldbyname('MJID').AsString);
            ADOQueryTemp.fieldbyname('Dname').Value:=Trim(DCode);
            ADOQueryTemp.post;
            next;
          end;
        end;
      end;
      next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD3 ');
    sql.add('@mainID='+quotedstr(Trim('')));
    sql.add(',@DName='+quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\销售码单新.rmf'),'提示',0);
  end;
end;

end.
