unit U_CKDuizhangdanList;

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, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  cxCheckBox, RM_Dataset, cxTextEdit, Menus;

type
  TfrmCKDuizhangdanList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: 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;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    YWY: TEdit;
    OrdPerson1: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    conNo: TEdit;
    Label14: TLabel;
    KHCONNO: TEdit;
    Label15: TLabel;
    RM1: TRMGridReport;
    Tv1Column5: TcxGridDBColumn;
    Label16: TLabel;
    CustomerNoName: TEdit;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Jianydj: TToolButton;
    Xiubf: TToolButton;
    Kuaidf: TToolButton;
    Yunf: TToolButton;
    Jincf: TToolButton;
    Baozdj: TToolButton;
    Zhuangxf: TToolButton;
    JiaGe: TEdit;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label17: TLabel;
    ComboBox1: TComboBox;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    RMDBHZ: TRMDBDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    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 v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure JianydjClick(Sender: TObject);
    procedure KuaidfClick(Sender: TObject);
    procedure XiubfClick(Sender: TObject);
    procedure YunfClick(Sender: TObject);
    procedure JincfClick(Sender: TObject);
    procedure BaozdjClick(Sender: TObject);
    procedure ZhuangxfClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1Column18PropertiesEditValueChanged(Sender: TObject);
//    procedure Tv1Column3PropertiesEditValueChanged(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKDuizhangdanList: TfrmCKDuizhangdanList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_JYOrderCDOne, U_CKProductJYHZList;

{$R *.dfm}

procedure TfrmCKDuizhangdanList.FormDestroy(Sender: TObject);
begin
  frmCKDuizhangdanList := nil;
end;

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

procedure TfrmCKDuizhangdanList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := (SGetServerDateTime(ADOQueryTemp) - 30);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmCKDuizhangdanList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;
      sql.Add('select  A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,C.DdPrice,C.OrderPrice,C.Baozfy,C.Jjtype,');
      sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
      sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
      sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
      sql.Add('CKjz=(select sum(MJQty4) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
      sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//      sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' and X.MJType=''正品''),');
      sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
      sql.Add('from WFB_MJJY A ');
      sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
      Sql.add('where A.fillTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and A.fillTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 7) + ''' ');
      Sql.add('group by A.MainId,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Yunf,C.Zhuangxf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,C.DdPrice,C.OrderPrice,C.Baozfy,C.JjType');
//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        if Trim(CDS_main.fieldbyname('JjType').AsString) = '出库长度' then
        begin
          sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select mjlen=isnull((select sum(mjlen) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' ),0)))');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set Baozfy=(Baozdj*(select Zongbs=isnull((select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=''' + Trim(cds_Main.fieldbyname('mainid').Asstring) + ''' and X.ckflag=''已出库'' and X.MJType=''正品''),0)))');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
        end
        else
        begin
          sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select MJQty4=isnull((select sum(MJQty4) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' ),0)))');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set Baozfy=(Baozdj*(select Zongbs=isnull((select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=''' + Trim(cds_Main.fieldbyname('mainid').Asstring) + ''' and X.ckflag=''已出库'' and X.MJType=''正品''),0)))');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
        end;
//        showmessage(sql.Text);
        ExecSQL;
      end;
      next;
    end;
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;
      sql.Add('select  A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,C.DdPrice,C.OrderPrice,C.Baozfy,C.Jjtype,');
      sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
      sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
      sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
      sql.Add('CKjz=(select sum(MJQty4) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
      sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//      sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' and X.MJType=''正品''),');
      sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
      sql.Add('from WFB_MJJY A ');
      sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
      Sql.add('where A.fillTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and A.fillTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 7) + ''' ');
      Sql.add('group by A.MainId,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Yunf,C.Zhuangxf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,C.DdPrice,C.OrderPrice,C.Baozfy,C.JjType');
//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmCKDuizhangdanList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKDuizhangdanList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '对账单');
  Close;
end;

procedure TfrmCKDuizhangdanList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption + tv1.Name, Tv1, '对账单');
  if Trim(DParameters2) = '管理' then
  begin
    //v1Column5.Options.Focusing:=True;
  end
  else
  begin
    //v1Column5.Options.Focusing:=False;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('对账单', cxGrid2);
end;

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

procedure TfrmCKDuizhangdanList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmZDYHelp:=TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag:='SOrdDefStr10';
//      flagname:='库存存放地点';
//      if ShowModal=1 then
//      begin
//        with CDS_Main do
//        begin
//          Edit;
//          FieldByName('SOrdDefStr10').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('Update JYOrder_Sub Set SOrdDefStr10='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
//          sql.Add(' where SubId='''+Trim(Self.CDS_Main.fieldbyname('SubId').AsString)+'''');
//          ExecSQL;
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
end;

procedure TfrmCKDuizhangdanList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKDuizhangdanList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCKDuizhangdanList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmCKDuizhangdanList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKDuizhangdanList.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\东圣对账单.rmf';
  ExportFtErpFile(Trim(ComboBox1.Text) + '.rmf', ADOQueryCmd);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;

  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  sqlStr := '';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean = true then
        begin
          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
          sqlStr := sqlStr + ' values( ';
          sqlStr := sqlStr + ' ''' + Trim(CDS_Main.fieldbyname('mainID').AsString) + ''', ';
          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
          sqlStr := sqlStr + ' ) ';
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(sqlStr);                     
//    showmessage(sql.text);
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMDHZ_copy1 ');
    sql.add('@DName=' + quotedstr(Trim(DCode)));
    Open;
//    showmessage(sql.text);
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Main);
  SInitCDSData20(ADOQueryTemp, CDS_Main);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示', 0);
  end;
//    with ADOQueryTemp do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add('select  A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,DdPrice, ');
//      sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
//      sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//      sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//      sql.Add('CKjz=(select sum(MJQty4) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//      sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
//      sql.Add('from WFB_MJJY A ');
//      sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
//      sql.Add('where A.Mainid=''' + trim(CDS_Main.fieldbyname('Mainid').asstring) + ''' ');
//      Sql.add('group by A.MainId,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Yunf,C.Zhuangxf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,DdPrice');
//      Open;
//
//    end;
//    SCreateCDS20(ADOQueryMain, CDS_Main);
//    SInitCDSData20(ADOQueryMain, CDS_Main);
//  end;
//  CDS_Main.EnableControls;
end;

procedure TfrmCKDuizhangdanList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(orderno.text) < 2 then
      exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        Filtered := False;
        sql.Add('select A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,C.DdPrice,C.OrderPrice,C.Baozfy, ');
        sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
        sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
        sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
        sql.Add('CKjz=(select sum(MJQty4) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
        sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//        sql.Add('Zongbs=(select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' and X.MJType=''正品''),');
        sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
        sql.Add('from WFB_MJJY A ');
        sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
//        sql.Add('inner join JYOrder_Sub B on B.MainId=A.MainId ');
        sql.add('where C.orderno=' + quotedstr(trim(orderno.Text)));
        Sql.add('group by A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,DdPrice,OrderPrice,Baozfy');
//       showmessage(sql.text);
        Open;

      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKDuizhangdanList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(CustomerNoName.text) < 2 then
      exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        Filtered := False;
        sql.Add('select A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,DdPrice,Baozfy, ');
        sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
        sql.Add('SCKROLL=(select sum(mjlen) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
//        sql.Add('SkcROLL=(select sum(mjlen) from WFB_MJJY X where X.SubId=A.SubId and X.ckflag=''未出库'' ),');
        sql.Add('CKROLL=(select count(*) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库'' ),');
//        sql.Add('KCROLL=(select count(*) from WFB_MJJY X where X.SubId=A.SubId and X.ckflag=''未出库'' ),');
        sql.Add('CKjz=(select sum(MJQty4) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
        sql.Add('Zongbs=(select count(baono) from WFB_MJJY X where X.mainid=A.mainid and X.ckflag=''已出库''),');
        sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
        sql.Add('from WFB_MJJY A ');
        sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
//        sql.Add('inner join JYOrder_Sub B on B.MainId=A.MainId ');
        sql.add('where C.CustomerNoName=' + quotedstr(trim(CustomerNoName.Text)));
        Sql.add('group by A.mainid,A.MJTypeother,C.OrderNo,C.CustomerNoName,C.ConNO,C.CustomerNoName,C.Baozdj,C.Zhuangxf,C.Yunf,C.Jianydj,C.Xiubf,C.Jincf,C.Jianmje,C.Kuaidf,DdPrice,Baozfy');
        Open;
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKDuizhangdanList.JianydjClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('检验单价不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('检验单价非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Jianydj=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          if Trim(CDS_main.fieldbyname('JjType').AsString) = '出库长度' then
          begin
            sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select SCKROLL=isnull((select sum(mjlen) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' and X.ckflag=''已出库'' and MJtype=''正品''),0)))');
            sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
            sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
            sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          end
          else
          begin
            sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select CKjz=isnull((select sum(MJQty4) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' and X.ckflag=''已出库''),0)))');
            sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
            sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
            sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          end;
//          showmessage(sql.Text);
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Jianydj').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.KuaidfClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('快递费不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('快递费非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Kuaidf=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Kuaidf').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.XiubfClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('修布费不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('修布费非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Xiubf=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Xiubf').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.YunfClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('运费不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('运费非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Yunf=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Yunf').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.JincfClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('进仓费不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('进仓费非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Jincf=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Jincf').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.BaozdjClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('包装单价不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('包装单价非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Baozdj=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set Baozfy=(Baozdj*(select Zongbs=isnull((select count(DISTINCT Baoid) from WFB_MJJY X where X.mainid=''' + Trim(cds_Main.fieldbyname('mainid').Asstring) + ''' and X.ckflag=''已出库'' and X.MJType=''正品''),0)))');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
          sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Baozfy+Xiubf+Kuaidf+Yunf+Jincf+Zhuangxf)');
          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
//          showmessage(sql.text);
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Baozdj').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.ZhuangxfClick(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('装卸费不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('装卸费非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Zhuangxf=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Zhuangxf').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.ToolButton1Click(Sender: TObject);
var
  FJiaGe: string;
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(JiaGe.Text) = '' then
  begin
    Application.MessageBox('减免金额不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(JiaGe.Text, FReal) = False then
  begin
    Application.MessageBox('减免金额非法数字!', '提示', 0);
    Exit;
  end;
  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      if CDS_Main.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set Jianmje=' + Trim(JiaGe.Text));
          sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
          ExecSQL;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('Jianmje').Value := JiaGe.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
  InitGrid();
end;

procedure TfrmCKDuizhangdanList.orderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKDuizhangdanList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True);
end;

procedure TfrmCKDuizhangdanList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, false);
end;

procedure TfrmCKDuizhangdanList.Tv1Column18PropertiesEditValueChanged(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with CDS_Main do
  begin
    edit;
    fieldbyname('JjType').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set  JjType=''' + trim(mvalues) + ''' ');
    sql.Add('where Mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
end;

//procedure TfrmCKDuizhangdanList.Tv1Column3PropertiesEditValueChanged(Sender: TObject);
//begin
//  with CDS_Main do
//  begin
//
////    if CDS_Main.FieldByName('Ssel').AsBoolean = true then
////    begin
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
////        sql.Add('Update JYOrder_Main Set Jianydj=' + Trim(JiaGe.Text));
////        sql.Add(' where mainid=''' + Trim(CDS_Main.fieldbyname('Mainid').AsString) + '''');
//        if Trim(CDS_main.fieldbyname('JjType').AsString) = '出库长度' then
//        begin
//          sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select SCKROLL=isnull((select sum(mjlen) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' and X.ckflag=''已出库'' and MJtype=''正品''),0)))');
//          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
//          sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
//          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
//        end
//        else
//        begin
//          sql.Add('Update JYOrder_Main Set DdPrice=(Jianydj*(select CKjz=isnull((select sum(MJQty4) from WFB_MJJY X where X.mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + ''' and X.ckflag=''已出库''),0)))');
//          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
//          sql.Add('Update JYOrder_Main Set OrderPrice=(DdPrice+Xiubf+Baozfy+Kuaidf+Yunf+Jincf+Zhuangxf)');
//          sql.Add('where mainid=''' + Trim(CDS_Main.fieldbyname('mainid').AsString) + '''');
//        end;
////          showmessage(sql.Text);
//        ExecSQL;
//      end;
//
////    end;
//  end;
//end;

end.

