unit U_CIList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, cxDropDownEdit;


type
  TfrmCIList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label8: TLabel;
    CINO: TEdit;
    ComboBox2: TComboBox;
    Label3: TLabel;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    Label5: TLabel;
    ConNo: TEdit;
    Label7: TLabel;
    CustomerNoName: TEdit;
    ADOQueryPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    cxGridPopupMenu3: TcxGridPopupMenu;
    v1Column5: TcxGridDBColumn;
    ClientDataSet3: TClientDataSet;
    DataSource3: TDataSource;
    Panel2: TPanel;
    Label4: TLabel;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    VDate: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    VNote: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    ToolButton2: TToolButton;
    Label6: TLabel;
    Panel4: TPanel;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure v1Column15CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v1Column9CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure CINOKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWsql(fsj:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCIList: TfrmCIList;

implementation
uses
  U_DataLink,U_RTFun,U_FHInPut,U_ModuleNote, U_ZDYHelp,U_CIInPut,
  U_SKCRFenPei,U_ConDJFenPei;

{$R *.dfm}

procedure TfrmCIList.FormDestroy(Sender: TObject);
begin
  frmCIList:=nil;
end;

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

procedure TfrmCIList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('销售发票列表',Tv1,'CI管理');
end;

procedure TfrmCIList.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select A.*,ConNO=dbo.F_Get_Order_SubStr(A.CIID,''CIConNO'') ');
      sql.Add(',PS=(select Sum(CSPS) from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      sql.Add(',Qty=(select Sum(CSQty) from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      sql.Add(',QtyUnit=(select Top 1 CSQtyUnit from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      SQL.Add(' from JYOrder_CI A');
      //sql.Add(' inner join JYOrderCon_Main C on A.PIMainId=C.MainId');
      sql.Add(' ) AA');
      sql.Add(' where 1=1 ');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add('and (Filler in(select UserName from  SY_User where DPID in');
        sql.Add('              (select DPID from SY_User where UserName='''+Trim(DName)+''')))');
      end;
      fsj:=TA(ComboBox2.Items.Objects[ComboBox2.Items.IndexOf(Trim(ComboBox2.Text))]).S;
      sql.Add(' and AA.'+Trim(fsj)+'>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and AA.'+Trim(fsj)+'<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmCIList.InitForm();
begin
  FNowDate:=SGetServerDate(ADOQueryTemp);
  EndDate.Date:=FNowDate;
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('销售发票列表',Tv1,'CI管理');
  //InitGrid();
end;

procedure TfrmCIList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
    begin
      Application.MessageBox('不能操作他人的信息!','提示',0);
      Exit;
    end;
  end;

  try
    frmCIInPut:=TfrmCIInPut.Create(Application);
    with frmCIInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('CIID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCIInPut.Free;
  end;
end;

procedure TfrmCIList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SK_Money_FP where WBID='''+Trim(Order_Main.fieldbyname('CIId').AsString)+'''');
    sql.Add(' and FPType in(''货款'',''CI定金'') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已分配数据不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmCIList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_CI  where CIId='''+Trim(Order_Main.fieldbyname('CIId').AsString)+'''');
      sql.Add('delete JYOrder_CI_Sub  where CIId='''+Trim(Order_Main.fieldbyname('CIId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmCIList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('销售发票',cxGrid1);
end;

procedure TfrmCIList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCIList.TBAddClick(Sender: TObject);
begin
  try
    frmCIInPut:=TfrmCIInPut.Create(Application);
    with frmCIInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCIInPut.Free;
  end;
end;

procedure TfrmCIList.FormShow(Sender: TObject);
var
  i:Integer;
begin
  InitForm();
  SInitComBoxByTvColumns(ComboBox2,Tv1,999,True,True);
end;

procedure TfrmCIList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCIList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='CI录入';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmCIList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmCIList.InitGridWsql(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select A.*,ConNO=dbo.F_Get_Order_SubStr(A.CIID,''CIConNO'') ');
      sql.Add(',PS=(select Sum(CSPS) from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      sql.Add(',Qty=(select Sum(CSQty) from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      sql.Add(',QtyUnit=(select Top 1 CSQtyUnit from JYOrder_CI_Sub CS where CS.CIID=A.CIID)');
      SQL.Add(' from JYOrder_CI A');
      //sql.Add(' inner join JYOrderCon_Main C on A.PIMainId=C.MainId');
      sql.Add(' ) AA');
      sql.Add(' where 1=1 ');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add('and (Filler in(select UserName from  SY_User where DPID in');
        sql.Add('              (select DPID from SY_User where UserName='''+Trim(DName)+'''))');
      end;
      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
function TfrmCIList.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    //保存主表
    if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'MR','Module_Prompt_Result',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取工作子流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(Order_Main.fieldbyname('MRID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Module_Prompt_Result where MRId='''+Trim(Order_Main.fieldbyname('MRID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MPId').Value:=Trim(Order_Main.fieldbyname('MPId').AsString);
      FieldByName('MRId').Value:=Trim(maxno);
      FieldByName('MRStatus').Value:='已处理';
      FieldByName('MRDate').Value:=SGetServerDateTime(ADOQueryTemp);
      if Trim(Order_Main.fieldbyname('MRId').AsString)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
procedure TfrmCIList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCIList.WorkerChange(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 TfrmCIList.v1Column15CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate1').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clYellow;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
     // ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmCIList.v1Column9CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate2').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clRed;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
      //ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmCIList.ToolButton1Click(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 TfrmCIList.ToolButton4Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        {with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('NotPrint').AsBoolean=True then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('不能打印《不打印》的缸条码!','提示',0);
          Exit;
        end;  }
        if FileExists(fPrintFile) then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select B.OrderNo,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,C.PRTHX,C.PRTColorNo,C.PRTColor,GangNo=A.GangNo,A.PS,A.Qty,A.QtyUnit,A.MDID ');
            SQL.Add(',B.MPRTCode,B.MPRTGY,C.PRTColorEng,A.SelfGangNo');
            sql.Add('from JYOrder_Main_MD A');
            sql.Add('inner join JYOrder_Main B on A.OrdMainid=B.Mainid');
            sql.Add(' inner join JYOrder_Sub C on A.OrdSubid=C.SubId');
            sql.Add(' where A.MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
            Open;
          end;
          RM1.LoadFromFile(fPrintFile);
          //RM1.ShowReport;
          RM1.PrintReport;
        end else
        begin
          Order_Main.DisableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
          Exit;
        end;
      end;

      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmCIList.CINOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:String;
begin
  if Length(Trim(CINO.Text))<3 then Exit;
  fsj:=' and AA.CINO like '''+'%'+Trim(CINO.Text)+'%'+'''';
  InitGridWsql(fsj);
end;

procedure TfrmCIList.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:String;
begin
  if Length(Trim(ConNo.Text))<3 then Exit;
  fsj:=' and AA.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''';
  InitGridWsql(fsj);
end;


procedure TfrmCIList.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.CIID,A.CINO,A.CIDate,A.OtherFeeName,A.OtherFee, ');
    sql.Add(' B.CSID,C.ConNo,C.CustomerNoName,D.FHQty,D.FHPS,D.FHDate,D.FHType,D.QtyUnit,');
    sql.Add(' E.OrderNo,PRTCode=isnull(E.MPRTCode,'''')+isnull(E.MPRTGY,''''),');
    sql.Add(' PRTColor=isnull(F.PRTColorNo,'''')+'' ''+isnull(F.PRTColor,'''')+'' ''+isnull(F.PRTColorEng,''''), ');
    sql.Add(' F.PRTHX,G.PriceUnit,G.PRTPrice,FHMoney=G.PRTPrice*D.FHQty,PIMainId=G.MainId,PISubId=G.SubId,');
    sql.Add(' F.MainId,F.SubId,D.FHID');
    sql.Add(' from JYOrder_CI A');
    sql.Add(' inner join JYOrder_CI_Sub B on A.CIId=B.CIId');
    sql.Add(' inner join JYOrder_FH D on B.FHID=D.FHID');
    sql.Add(' inner join JYOrder_Main E on D.MainId=E.MainId');
    sql.Add(' inner join JYOrder_Sub F on D.SubId=F.SubId');
    sql.Add(' inner join JYOrderCon_Sub G on F.PISubId=G.SubId');
    sql.Add(' inner join JYOrderCon_Main C on G.Mainid=C.MainId');
    sql.Add(' where A.CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3); }
end;

procedure TfrmCIList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select YS.CRTime,YS.BZType,YS.Money,FP.* ');
    sql.Add(' from SK_Money_FP FP');
    sql.Add(' inner join YF_Money_CR YS on FP.YFID=YS.YFID');
    sql.Add(' where FP.WBID='''+Trim(Order_Main.fieldbyname('CIId').AsString)+'''');
    sql.Add(' and isnull(FPType,'''')<>''CI定金'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select YS.PriceUnit,YS.DJMoney,FP.* ');
    sql.Add(' from SK_Money_FP FP');
    sql.Add(' inner join JYOrderCon_Sub YS on FP.YFID=YS.SubId');
    sql.Add(' where FP.WBID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
    sql.Add(' and FPType=''CI定金'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

procedure TfrmCIList.Button1Click(Sender: TObject);
begin
  try
    frmSKCRFenPei:=TfrmSKCRFenPei.Create(Application);
    with frmSKCRFenPei do
    begin
      KHName:=Trim(Order_Main.fieldbyname('CustomerNo').AsString);
      if ShowModal=1 then
      begin
        with Self.ClientDataSet2 do
        begin
          Append;
          FieldByName('YFID').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('YFID').Value;
          FieldByName('CRTime').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('CRTime').Value;
          FieldByName('BZType').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('BZType').Value;
          FieldByName('Money').Value:=frmSKCRFenPei.CDS_HZ.fieldbyname('Money').Value;
          FieldByName('FPDate').Value:=Now;
          Post;
        end;
      end;
    end;
  finally
    frmSKCRFenPei.Free;
  end;
end;

procedure TfrmCIList.Button3Click(Sender: TObject);
var
  maxsubno:String;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('FPDate',Null,[]) then
  begin
    Application.MessageBox('分配日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPMoney',Null,[]) then
  begin
    Application.MessageBox('分配金额不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPType',Null,[]) then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxsubno,'FP','SK_Money_FP',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet2.fieldbyname('FPId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SK_Money_FP where FPId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;

          FieldByName('FPId').Value:=Trim(maxsubno);
          FieldByName('YFID').Value:=Trim(ClientDataSet2.fieldbyname('YFID').AsString);
          FieldByName('FPTYpe').Value:=ClientDataSet2.fieldbyname('FPTYpe').Value;
          FieldByName('FPDate').Value:=ClientDataSet2.fieldbyname('FPDate').Value;
          FieldByName('WBID').Value:=Trim(Order_Main.fieldbyname('CIID').AsString);
          if Trim(ClientDataSet2.fieldbyname('FPMoney').AsString)='' then
          begin
            fieldbyname('FPMoney').Value:=0
          end else
          begin
            fieldbyname('FPMoney').Value:=Trim(ClientDataSet2.fieldbyname('FPMoney').AsString);
          end;
          FieldByName('Note').Value:=Trim(ClientDataSet2.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_CI Set YKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''余款'' )');
          sql.Add(', QKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''全款'' )');
          sql.Add(', YJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''佣金'' )');
          sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)-isnull(DJMoney,0)');
          sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)+isnull(KKMoney,0)');
          sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPYKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''余款'' )');
          sql.Add(',FPQKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''全款'' )');
          sql.Add(',FPYJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''佣金'' )');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)');
          sql.Add('-isnull(FPYKMoney,0)-isnull(FPQKMoney,0)-isnull(FPYJMoney,0)');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('FPId').Value:=Trim(maxsubno);
        //Order_Sub.Post;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmCIList.Button2Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('FPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SK_Money_FP where FPID='''+Trim(ClientDataSet2.fieldbyname('FPID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YF_Money_CR Set FPYKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''余款'' )');
      sql.Add(',FPQKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''全款'' )');
      sql.Add(',FPYJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''佣金'' )');
      sql.Add(' where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)');
      sql.Add('-isnull(FPYKMoney,0)-isnull(FPQKMoney,0)-isnull(FPYJMoney,0)');   //-isnull(FPKKMoney,0)
      sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_CI Set YKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''余款'' )');
      sql.Add(', QKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''全款'' )');
      sql.Add(', YJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''佣金'' )');
      sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)-isnull(DJMoney,0)');
      sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)+isnull(KKMoney,0)');
      sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmCIList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  {if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end; }
  try
    frmCIInPut:=TfrmCIInPut.Create(Application);
    with frmCIInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('CIID').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCIInPut.Free;
  end;
end;

procedure TfrmCIList.Button4Click(Sender: TObject);
begin
  try
    frmConDJFenPei:=TfrmConDJFenPei.Create(Application);
    with frmConDJFenPei do
    begin
      FCIID:=Trim(Order_Main.fieldbyname('CIID').AsString);
      if ShowModal=1 then
      begin
        with Self.ClientDataSet3 do
        begin
          Append;
          FieldByName('YFID').Value:=frmConDJFenPei.CDS_HZ.fieldbyname('SubId').Value;
         // FieldByName('CRTime').Value:=frmConDJFenPei.CDS_HZ.fieldbyname('CRTime').Value;
          FieldByName('PriceUnit').Value:=frmConDJFenPei.CDS_HZ.fieldbyname('PriceUnit').Value;
          FieldByName('DJMoney').Value:=frmConDJFenPei.CDS_HZ.fieldbyname('DJMoney').Value;
          FieldByName('FPDate').Value:=Now;
          Post;
        end;
      end;
    end;
  finally
    frmConDJFenPei.Free;
  end;
end;

procedure TfrmCIList.Button6Click(Sender: TObject);
var
  maxsubno:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('FPDate',Null,[]) then
  begin
    Application.MessageBox('分配日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('FPMoney',Null,[]) then
  begin
    Application.MessageBox('分配定金不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('FPId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxsubno,'FP','SK_Money_FP',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet3.fieldbyname('FPId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SK_Money_FP where FPId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('FPId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;

          FieldByName('FPId').Value:=Trim(maxsubno);
          FieldByName('YFID').Value:=Trim(ClientDataSet3.fieldbyname('YFID').AsString);
          FieldByName('FPTYpe').Value:='CI定金';
          FieldByName('FPDate').Value:=ClientDataSet3.fieldbyname('FPDate').Value;
          FieldByName('WBID').Value:=Trim(Order_Main.fieldbyname('CIID').AsString);
          if Trim(ClientDataSet3.fieldbyname('FPMoney').AsString)='' then
          begin
            fieldbyname('FPMoney').Value:=0
          end else
          begin
            fieldbyname('FPMoney').Value:=Trim(ClientDataSet3.fieldbyname('FPMoney').AsString);
          end;
          FieldByName('Note').Value:=Trim(ClientDataSet3.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_CI Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''CI定金'' )');
          sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)+isnull(KKMoney,0)-isnull(DJMoney,0)');
          sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)');
          sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrderCon_Sub Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=JYOrderCon_Sub.SubID and A.FPType=''CI定金'' )');
          sql.Add('where SubID='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrderCon_Sub Set WFPDJMoney=isnull(DJMoney,0)-isnull(FPDJMoney,0)');
          sql.Add('where SubID='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet3.Edit;
        ClientDataSet3.FieldByName('FPId').Value:=Trim(maxsubno);
        //Order_Sub.Post;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmCIList.Button5Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if Trim(ClientDataSet3.fieldbyname('FPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SK_Money_FP where FPID='''+Trim(ClientDataSet3.fieldbyname('FPID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrderCon_Sub Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.YFID=JYOrderCon_Sub.SubId and A.FPType=''CI定金'' )');
      sql.Add('where SubId='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrderCon_Sub Set WFPDJMoney=isnull(DJMoney,0)-isnull(FPDJMoney,0)');
      sql.Add('where SubId='''+Trim(ClientDataSet3.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_CI Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
      sql.Add(' where A.WBID=JYOrder_CI.CIID and A.FPType=''CI定金'' )');
      sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)+isnull(KKMoney,0)-isnull(DJMoney,0)');
      sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)');
      sql.Add('where CIID='''+Trim(Order_Main.fieldbyname('CIID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet3.Delete;
end;

end.
