unit U_OrderJDList;

interface

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

type
  TfrmOrderJDList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNo: TEdit;
    CustomerNoName: TEdit;
    Label8: TLabel;
    MPRTCode: TEdit;
    Label2: TLabel;
    cxTabControl1: TcxTabControl;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBBandedTableView;
    v1Column1: TcxGridDBBandedColumn;
    v1Column2: TcxGridDBBandedColumn;
    v1Column3: TcxGridDBBandedColumn;
    v1Column4: TcxGridDBBandedColumn;
    v1Column5: TcxGridDBBandedColumn;
    v1Column6: TcxGridDBBandedColumn;
    v1Column7: TcxGridDBBandedColumn;
    v1Column8: TcxGridDBBandedColumn;
    v1Column9: TcxGridDBBandedColumn;
    v1Column10: TcxGridDBBandedColumn;
    v1Column11: TcxGridDBBandedColumn;
    v1Column12: TcxGridDBBandedColumn;
    v1Column13: TcxGridDBBandedColumn;
    v1Column14: TcxGridDBBandedColumn;
    v1Column15: TcxGridDBBandedColumn;
    v1Column16: TcxGridDBBandedColumn;
    ScrollBox1: TScrollBox;
    cxSplitter1: TcxSplitter;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ScrollBox2: TScrollBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    V2QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    Button1: TButton;
    ScrollBox3: TScrollBox;
    ScrollBox4: TScrollBox;
    Panel3: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    DlyDate: TDateTimePicker;
    Label7: TLabel;
    DlyNote: TRichEdit;
    Panel4: TPanel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ScrollBox5: TScrollBox;
    Panel5: TPanel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    YCLBegDate: TDateTimePicker;
    YCLNote: TRichEdit;
    Panel6: TPanel;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Label12: TLabel;
    YCLEndDate: TDateTimePicker;
    ScrollBox6: TScrollBox;
    Panel7: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    JGBegDate: TDateTimePicker;
    JGNote: TRichEdit;
    Panel8: TPanel;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    JGEndDate: TDateTimePicker;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    ScrollBox7: TScrollBox;
    Panel9: TPanel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    CKBegDate: TDateTimePicker;
    CKNote: TRichEdit;
    Panel10: TPanel;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    CKEndDate: TDateTimePicker;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    ScrollBox8: TScrollBox;
    Panel11: TPanel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    OutDate: TDateTimePicker;
    OutNote: TRichEdit;
    Panel12: TPanel;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    cxGrid7: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    Edit1: TEdit;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ComboBox1: TComboBox;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    DataSource4: TDataSource;
    ClientDataSet4: TClientDataSet;
    DataSource5: TDataSource;
    ClientDataSet5: TClientDataSet;
    DataSource6: TDataSource;
    ClientDataSet6: TClientDataSet;
    DataSource7: TDataSource;
    ClientDataSet7: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGridPopupMenu5: TcxGridPopupMenu;
    cxGridPopupMenu6: TcxGridPopupMenu;
    cxGridPopupMenu7: TcxGridPopupMenu;
    LiDanPerson: TComboBox;
    Label24: TLabel;
    v1Column17: TcxGridDBBandedColumn;
    v2LiDanPerson: TcxGridDBColumn;
    v1Column18: TcxGridDBBandedColumn;
    Label25: TLabel;
    Filler: TComboBox;
    ToolButton3: TToolButton;
    v2Column4: TcxGridDBColumn;
    v1Column19: TcxGridDBBandedColumn;
    cxSplitter2: TcxSplitter;
    cxSplitter3: TcxSplitter;
    cxSplitter4: TcxSplitter;
    cxSplitter5: TcxSplitter;
    cxSplitter6: TcxSplitter;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v1Column20: TcxGridDBBandedColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column21: TcxGridDBBandedColumn;
    v1Column22: TcxGridDBBandedColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv3DblClick(Sender: TObject);
    procedure Tv4DblClick(Sender: TObject);
    procedure Tv5DblClick(Sender: TObject);
    procedure Tv6DblClick(Sender: TObject);
    procedure Tv7DblClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    canshu1:string;
    procedure InitGrid();
    procedure InitATGrid(ClientDataSet87:TClientDataSet;ATTYpe:String);
    procedure SaveDateP(Panel87:TPanel;ClientDataSet87:TClientDataSet;FATType:String);
    procedure DelData(ClientDataSet87:TClientDataSet);
    procedure FInitNote(FFAttyp:string);
  public
    { Public declarations }
  end;

var
  frmOrderJDList: TfrmOrderJDList;

implementation
uses
  U_DataLink,U_RTFun,U_OrderJDNote, U_ModuleNote;

{$R *.dfm}

procedure TfrmOrderJDList.FormDestroy(Sender: TObject);
begin
  frmOrderJDList:=nil;
end;

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

procedure TfrmOrderJDList.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid('1',Tv1,'订单进度表');
  WriteCxGrid('2',Tv2,'订单进度表');
  WriteCxGrid('3',Tv3,'订单进度表');
  WriteCxGrid('4',Tv4,'订单进度表');
  WriteCxGrid('5',Tv5,'订单进度表');
  WriteCxGrid('6',Tv6,'订单进度表');
  WriteCxGrid('7',Tv7,'订单进度表');
  close;

end;

procedure TfrmOrderJDList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select isnull(DlyNote10,DlyNote) DlyNote,isnull(YCLNote10,YCLNote) YCLNote,isnull(JGNote10,JGNote) JGNote,');
      sql.Add(' isnull(CKNote10,CKNote) CKNote,isnull(OutNote10,OutNote) OutNote,  ');
      sql.Add(' AA.*  from(select A.*,DlyDate10=isnull(DlyDate,Cast(''2090-01-01'' as Datetime))');
      sql.Add(' ,DlyNote10=(select Top 1 ATNote from JYOrderJD_Main_Att JMA where JMA.MainId=A.MainId  and ATType=''理单'' Order by ATDate Desc)');
      sql.Add(' ,YCLNote10=(select Top 1 ATNote from JYOrderJD_Main_Att JMA where JMA.MainId=A.MainId  and ATType=''原材料'' Order by ATDate Desc)');
      sql.Add(' ,JGNote10=(select Top 1 ATNote from JYOrderJD_Main_Att JMA where JMA.MainId=A.MainId  and ATType=''加工'' Order by ATDate Desc)');
      sql.Add(' ,CKNote10=(select Top 1 ATNote from JYOrderJD_Main_Att JMA where JMA.MainId=A.MainId  and ATType=''仓库'' Order by ATDate Desc)');
      sql.Add(' ,OutNote10=(select Top 1 ATNote from JYOrderJD_Main_Att JMA where JMA.MainId=A.MainId  and ATType=''出货'' Order by ATDate Desc)');
      sql.Add(' from JYOrderJD_Main A where A.OrderNO not like ''KC%'' ');
      if Trim(DParameters1)='理单' then
      begin
        if Trim(ComboBox1.Text)<>'' then
        begin
          sql.Add(' and isnull(LiDanPerson,'''')='''+Trim(LiDanPerson.Text)+'''');
        end;
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and Status=''生产中'' ' );
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''' );
        sql.Add(' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''' );
        sql.Add(' and Status=''完结'' ' );
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and Status=''冻结'' ' );
      end else
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add(' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''' );
        sql.Add(' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''' );
      end;
      sql.Add(' ) AA order by DlyDate10');

      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
  end;
end;

procedure TfrmOrderJDList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

procedure TfrmOrderJDList.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  if Trim(Edit1.Text)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderJD_Main where OrderNo='''+Trim(Edit1.Text)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('订单号重复!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要新增订单吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'JD','JYOrderJD_Main',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('增加订单失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from JYOrderJD_Main where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('OrderNo').Value:=Trim(Edit1.Text);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('LiDanPerson').Value:=Trim(DName);
      FieldByName('Status').Value:='生产中';
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('ORderNo').Value:=Trim(Edit1.Text);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('LiDanPerson').Value:=Trim(DName);
      FieldByName('Status').Value:='生产中';
      Post;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('新增订单异常!','提示',0);
  end;
end;

procedure TfrmOrderJDList.Button1Click(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet1.IsEmpty then Exit;
  {if Trim(ClientDataSet1.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end; }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_User where DPID=(');
    sql.Add('select DPID from SY_User where UserName='''+Trim(DName)+''')');
    sql.Add(' and (UserName='''+Trim(ClientDataSet1.fieldbyname('Filler').AsString)+'''');
    sql.Add(' or UserName='''+Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('不能操作他组的订单!','',0);
    Exit;
  end;
  {if Trim(canshu1)<>'船期' then
  begin
    if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
       or
       (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
    then
    begin
    end else
    begin
      Application.MessageBox('不能操作他人的订单!','提示',0);
      Exit;
    end;
  end;}

  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel2.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from JYOrderJD_Main where MainId='''+Trim(ClientDataSet2.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,ClientDataSet2,'JYOrderJD_Main',0);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('MPRTCodeName').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCodeName').AsString);
      FieldByName('MPRTCode').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCode').AsString);
      Post;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('新增订单异常!','提示',0);
  end;
end;

procedure TfrmOrderJDList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
  DlyDate.Checked:=False;
  DlyNote.Text:='';
  YCLBegDate.Checked:=False;
  YCLEndDate.Checked:=False;
  YCLNote.Text:='';
  JGBegDate.Checked:=False;
  JGEndDate.Checked:=False;
  JGNote.Text:='';
  CKBegDate.Checked:=False;
  CKEndDate.Checked:=False;
  CKNote.Text:='';
  OutDate.Checked:=False;
  OutNote.Text:='';
end;
procedure TfrmOrderJDList.InitATGrid(ClientDataSet87:TClientDataSet;ATTYpe:String);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderJD_Main_Att where Mainid='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and ATType='''+Trim(ATTYpe)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet87);
  SInitCDSData20(ADOQueryTemp,ClientDataSet87);
end;

procedure TfrmOrderJDList.Button2Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
 { if Trim(ClientDataSet1.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end; }
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  SaveDateP(Panel3,ClientDataSet3,'理单');
  SaveDateP(Panel11,ClientDataSet7,'出货');
end;
procedure TfrmOrderJDList.SaveDateP(Panel87:TPanel;ClientDataSet87:TClientDataSet;FATType:String);
var
  FATID:string;
begin
  
  //if ClientDataSet87.IsEmpty then Exit;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from JYOrderJD_Main where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetsavedata(ADOQueryCmd,'JYOrderJD_Main',Panel87,2);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      Post;
    end;
    ClientDataSet87.DisableControls;
    with ClientDataSet87 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet87.fieldbyname('ATID').AsString)<>'' then
        begin
          FATID:=ClientDataSet87.fieldbyname('ATID').AsString;
        end else
        begin
          if GetLSNo(ADOQueryCmd,FATID,'AT','JYOrderJD_Main_Att',4,1)=False then
          begin
            ClientDataSet87.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取订单进度辅助表最大号异常!','提示',0);
            Exit;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrderJD_Main_Att where ATID='''+Trim(ClientDataSet87.fieldbyname('ATID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet87.fieldbyname('ATID').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('ATID').Value:=Trim(FATID);
          FieldByName('MainId').Value:=Trim(ClientDataSet1.fieldbyname('MainId').AsString);
          FieldByName('ATTYpe').Value:=FATType;
          FieldByName('ATDate').Value:=ClientDataSet87.fieldbyname('ATDate').Value;
          FieldByName('ATNote').Value:=ClientDataSet87.fieldbyname('ATNote').Value;
          Post;
        end;
        ClientDataSet87.Edit;
        ClientDataSet87.FieldByName('ATID').Value:=Trim(FATID);
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet87.EnableControls;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderJD_Main where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('DlyDate').Value:=ADOQueryTemp.fieldbyname('DlyDate').Value;
      FieldByName('DlyNote').Value:=ADOQueryTemp.fieldbyname('DlyNote').Value;
      FieldByName('YCLBegDate').Value:=ADOQueryTemp.fieldbyname('YCLBegDate').Value;
      FieldByName('YCLEndDate').Value:=ADOQueryTemp.fieldbyname('YCLEndDate').Value;
      FieldByName('YCLNote').Value:=ADOQueryTemp.fieldbyname('YCLNote').Value;
      FieldByName('JGBegDate').Value:=ADOQueryTemp.fieldbyname('JGBegDate').Value;
      FieldByName('JGEndDate').Value:=ADOQueryTemp.fieldbyname('JGEndDate').Value;
      FieldByName('JGNote').Value:=ADOQueryTemp.fieldbyname('JGNote').Value;
      FieldByName('CKBegDate').Value:=ADOQueryTemp.fieldbyname('CKBegDate').Value;
      FieldByName('CKEndDate').Value:=ADOQueryTemp.fieldbyname('CKEndDate').Value;
      FieldByName('CKNote').Value:=ADOQueryTemp.fieldbyname('CKNote').Value;
      FieldByName('OutDate').Value:=ADOQueryTemp.fieldbyname('OutDate').Value;
      FieldByName('OutNote').Value:=ADOQueryTemp.fieldbyname('OutNote').Value;
      Post;
    end;
    //ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet87.EnableControls;
  except
    ClientDataSet87.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);

  end;
end;

procedure TfrmOrderJDList.Button3Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  {if Trim(ClientDataSet1.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;}
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  ClientDataSet3.Append;
  ClientDataSet3.Post;
end;

procedure TfrmOrderJDList.Button6Click(Sender: TObject);
begin

  ClientDataSet4.Append;
  ClientDataSet4.Post;
end;

procedure TfrmOrderJDList.Button9Click(Sender: TObject);
begin

  ClientDataSet5.Append;
  ClientDataSet5.Post;
end;

procedure TfrmOrderJDList.Button12Click(Sender: TObject);
begin

  ClientDataSet6.Append;
  ClientDataSet6.Post;
end;

procedure TfrmOrderJDList.Button15Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  ClientDataSet7.Append;
  ClientDataSet7.Post;
end;

procedure TfrmOrderJDList.Button5Click(Sender: TObject);
begin
  SaveDateP(Panel5,ClientDataSet4,'原材料');
end;

procedure TfrmOrderJDList.Button8Click(Sender: TObject);
begin
  SaveDateP(Panel7,ClientDataSet5,'加工');  
end;

procedure TfrmOrderJDList.Button11Click(Sender: TObject);
begin
  SaveDateP(Panel9,ClientDataSet6,'仓库');
end;

procedure TfrmOrderJDList.Button14Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  SaveDateP(Panel3,ClientDataSet3,'理单');
  SaveDateP(Panel11,ClientDataSet7,'出货');
end;

procedure TfrmOrderJDList.Button4Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  DelData(ClientDataSet3);
end;
procedure TfrmOrderJDList.DelData(ClientDataSet87:TClientDataSet);
begin
  
  if ClientDataSet87.IsEmpty then Exit;
  if Trim(ClientDataSet87.fieldbyname('ATID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderJD_Main_Att where ATID='''+Trim(ClientDataSet87.fieldbyname('ATID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet87.Delete;
end;

procedure TfrmOrderJDList.Button7Click(Sender: TObject);
begin
  DelData(ClientDataSet4);
end;

procedure TfrmOrderJDList.Button10Click(Sender: TObject);
begin
  DelData(ClientDataSet5);
end;

procedure TfrmOrderJDList.Button13Click(Sender: TObject);
begin
  DelData(ClientDataSet6);
end;

procedure TfrmOrderJDList.Button16Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;
  DelData(ClientDataSet7);
end;

procedure TfrmOrderJDList.FormShow(Sender: TObject);
var
  fsj:string;
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-30;
  DlyDate.Date:=EndDate.Date;
  DlyDate.Checked:=False;
  YCLBegDate.Date:=EndDate.Date;
  YCLBegDate.Checked:=False;
  YCLEndDate.Date:=EndDate.Date;
  YCLEndDate.Checked:=False;
  JGBegDate.Date:=EndDate.Date;
  JGBegDate.Checked:=False;
  JGEndDate.Date:=EndDate.Date;
  JGEndDate.Checked:=False;
  CKBegDate.Date:=EndDate.Date;
  CKBegDate.Checked:=False;
  CKEndDate.Date:=EndDate.Date;
  CKEndDate.Checked:=False;
  OutDate.Date:=EndDate.Date;
  OutDate.Checked:=False;
  ReadCxBandedGrid('1',Tv1,'订单进度表');
  ReadCxGrid('2',Tv2,'订单进度表');
  ReadCxGrid('3',Tv3,'订单进度表');
  ReadCxGrid('4',Tv4,'订单进度表');
  ReadCxGrid('5',Tv5,'订单进度表');
  ReadCxGrid('6',Tv6,'订单进度表');
  ReadCxGrid('7',Tv7,'订单进度表');
  if Trim(DParameters1)='理单' then
  begin
    Panel2.Visible:=True;
    Panel4.Visible:=True;
    Panel12.Visible:=True;
  end else
  begin
    TBAdd.Visible:=False;
    ToolButton1.Visible:=False;
    ToolButton2.Visible:=False;
    ToolButton3.Visible:=False;
    Edit1.Visible:=False;

    ComboBox1.Visible:=False;
    if Trim(DParameters1)='原材料' then
    begin
      Panel6.Visible:=True;
    end else
    if Trim(DParameters1)='加工' then
    begin
      Panel8.Visible:=True;
    end else
    if Trim(DParameters1)='仓库' then
    begin
      Panel10.Visible:=True;
    end;
  end;
  fsj:='select distinct(Filler) Name,Cast('''' as varchar(20)) Code from JYOrderJD_Main';

  SInitComBoxBySql(ADOQueryTemp,Filler,False,fsj);
  fsj:='select distinct(LiDanPerson) Name,Cast('''' as varchar(20)) Code from JYOrderJD_Main';
   SInitComBoxBySql(ADOQueryTemp,LiDanPerson,False,fsj);
  SInitCxGridComboBoxBySql(ADOQueryTemp,v2LiDanPerson,fsj,0,False,'');
  fsj:='select distinct(QtyUnit) Name,Cast('''' as varchar(20)) Code from JYOrderJD_Main';
  SInitCxGridComboBoxBySql(ADOQueryTemp,V2QtyUnit,fsj,0,False,'');
  if Trim(DParameters1)='理单' then
  begin
    LiDanPerson.Text:=Trim(DName);
  end;

end;

procedure TfrmOrderJDList.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  {if Trim(ClientDataSet1.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end; }
  {if (Trim(ClientDataSet1.fieldbyname('Filler').AsString)=Trim(DName))
     or
     (Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)=Trim(DName))
  then
  begin
  end else
  begin
    Application.MessageBox('不能操作他人的订单!','提示',0);
    Exit;
  end;  }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_User where DPID=(');
    sql.Add('select DPID from SY_User where UserName='''+Trim(DName)+''')');
    sql.Add(' and (UserName='''+Trim(ClientDataSet1.fieldbyname('Filler').AsString)+'''');
    sql.Add(' or UserName='''+Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('不能操作他组的订单!','',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderJD_Main_ATT where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已经产生数据，不能删除订单!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete JYOrderJD_Main where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmOrderJDList.ToolButton2Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('状态不能为空!','提示',0);
    Exit;
  end;
  {if Trim(ClientDataSet1.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    if Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)<>Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的订单!','提示',0);
      Exit;
    end;

  end; }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_User where DPID=(');
    sql.Add('select DPID from SY_User where UserName='''+Trim(DName)+''')');
    sql.Add(' and (UserName='''+Trim(ClientDataSet1.fieldbyname('Filler').AsString)+'''');
    sql.Add(' or UserName='''+Trim(ClientDataSet1.fieldbyname('LiDanPerson').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('不能操作他组的订单!','',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrderJD_Main Set Status='''+Trim(ComboBox1.Text)+'''');
    sql.add(' where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  if Trim(ComboBox1.Text)<>Trim(ClientDataSet1.fieldbyname('Status').AsString) then
  begin
    ClientDataSet1.Delete;
  end;
end;

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

procedure TfrmOrderJDList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(OrderNo.Text))<4 then Exit;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderJD_Main where orderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and Status=''生产中'' ' );
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and Status=''完结'' ' );
      end else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and Status=''冻结'' ' );
      end else
      if cxTabControl1.TabIndex=3 then
      begin
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmOrderJDList.ToolButton3Click(Sender: TObject);
var
  maxno:string;
begin
  if Trim(Edit1.Text)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet1.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderJD_Main where OrderNo='''+Trim(Edit1.Text)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('订单号重复!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要复制订单吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'JD','JYOrderJD_Main',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('增加订单失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from JYOrderJD_Main where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('OrderNo').Value:=Trim(Edit1.Text);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('LiDanPerson').Value:=Trim(DName);
      FieldByName('CustomerNoName').Value:=ClientDataSet2.fieldbyname('CustomerNoName').Value;
      FieldByName('MPRTCode').Value:=ClientDataSet2.fieldbyname('MPRTCode').Value;
      FieldByName('MPRTCodeName').Value:=ClientDataSet2.fieldbyname('MPRTCodeName').Value;
      FieldByName('MPRTSpec').Value:=ClientDataSet2.fieldbyname('MPRTSpec').Value;
      FieldByName('MPRTCF').Value:=ClientDataSet2.fieldbyname('MPRTCF').Value;
      FieldByName('MPRTMF').Value:=ClientDataSet2.fieldbyname('MPRTMF').Value;
      FieldByName('MPRTKZ').Value:=ClientDataSet2.fieldbyname('MPRTKZ').Value;
      FieldByName('HSCode').Value:=ClientDataSet2.fieldbyname('HSCode').Value;
      FieldByName('KPName').Value:=ClientDataSet2.fieldbyname('KPName').Value;
      FieldByName('BoxType').Value:=ClientDataSet2.fieldbyname('BoxType').Value;
      FieldByName('ORDQty').Value:=ClientDataSet2.fieldbyname('ORDQty').Value;
      FieldByName('QtyUnit').Value:=ClientDataSet2.fieldbyname('QtyUnit').Value;
      FieldByName('Status').Value:='生产中';
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('ORderNo').Value:=Trim(Edit1.Text);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('LiDanPerson').Value:=Trim(DName);
      FieldByName('MPRTCode').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCode').AsString);
      FieldByName('MPRTCodeName').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCodeName').AsString);
      FieldByName('Status').Value:='生产中';
      Post;
    end;
    with ClientDataSet1 do
    begin
      Edit;
      FieldByName('MPRTCode').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCode').AsString);
      FieldByName('MPRTCodeName').Value:=Trim(ClientDataSet2.fieldbyname('MPRTCodeName').AsString);
      Post;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('新增订单异常!','提示',0);
  end;
end;

procedure TfrmOrderJDList.Tv3DblClick(Sender: TObject);
begin
  FInitNote('理单');
end;
procedure TfrmOrderJDList.FInitNote(FFAttyp:string);
begin
  try
    frmOrderJDNote:=TfrmOrderJDNote.Create(Application);
    with frmOrderJDNote do
    begin
      FMainId:=Trim(ClientDataSet1.fieldbyname('MainId').AsString);
      FTTYpe:=FFAttyp;
      frmOrderJDNote.Caption:=Trim(FTTYpe)+Trim(frmOrderJDNote.Caption);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderJDNote.Free;
  end;
end;

procedure TfrmOrderJDList.Tv4DblClick(Sender: TObject);
begin
  FInitNote('原材料');
end;

procedure TfrmOrderJDList.Tv5DblClick(Sender: TObject);
begin
  FInitNote('加工');
end;

procedure TfrmOrderJDList.Tv6DblClick(Sender: TObject);
begin
  FInitNote('仓库');
end;

procedure TfrmOrderJDList.Tv7DblClick(Sender: TObject);
begin
  FInitNote('出货');
end;

procedure TfrmOrderJDList.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  TcxGridToExcel('订单进度表',cxGrid1);
end;

procedure TfrmOrderJDList.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrderJD_Main where MainId='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  SCSHDataNew(ADOQueryTemp,Panel5,2);
  SCSHDataNew(ADOQueryTemp,Panel7,2);
  SCSHDataNew(ADOQueryTemp,Panel9,2);
  SCSHDataNew(ADOQueryTemp,Panel11,2);
  DlyNote.Text:=Trim(ClientDataSet1.fieldbyname('DlyNote').AsString);
  YCLNote.Text:=Trim(ClientDataSet1.fieldbyname('YCLNote').AsString);
  JGNote.Text:=Trim(ClientDataSet1.fieldbyname('JGNote').AsString);
  CKNote.Text:=Trim(ClientDataSet1.fieldbyname('CKNote').AsString);
  OutNote.Text:=Trim(ClientDataSet1.fieldbyname('OutNote').AsString);


  InitATGrid(ClientDataSet3,'理单');
  InitATGrid(ClientDataSet4,'原材料');
  InitATGrid(ClientDataSet5,'加工');
  InitATGrid(ClientDataSet6,'仓库');
  InitATGrid(ClientDataSet7,'出货');
end;

procedure TfrmOrderJDList.ToolButton5Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='订单进度表';
      if ShowModal=1 then
      begin

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

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

procedure TfrmOrderJDList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters2);
  
end;

end.
