unit U_GDJinDuMXInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmGDJinDuMXInPut = class(TForm)
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TBSave: TToolButton;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1GDPerson: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1FromFactoryName: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v1OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure v1GDPersonPropertiesEditValueChanged(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType:String;

    { Public declarations }
  end;

var
  frmGDJinDuMXInPut: TfrmGDJinDuMXInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_SYDeptUserView;

{$R *.dfm}

procedure TfrmGDJinDuMXInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from JYOrder_GD_Info A  ');
    sql.Add(' where A.GDID='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
end;

procedure TfrmGDJinDuMXInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmGDJinDuMXInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('跟单信息录入',Tv1,'跟单进度管理');
  InitData();

end;
procedure TfrmGDJinDuMXInPut.GetMaxConNO();
begin

end;
function TfrmGDJinDuMXInPut.SaveData():Boolean;
var
  maxSubNo,FGDID:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存数据
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_GD_Info where GDID='''+Trim(Order_Sub.fieldbyname('GDID').AsString)+'''');
          Open;
        end;
        FGDID:=Trim(ADOTemp.fieldbyname('GDID').AsString);
        if Trim(FGDID)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'DD','JYOrder_GD_Info',2,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Order_Sub.EnableControls;
            Application.MessageBox('生成跟单进度明细表流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(FGDID);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_GD_Info where ');
          sql.Add('  GDID='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FGDID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditerCode').Value:=Trim(DCode);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          FieldByName('GDId').Value:=Trim(maxSubNo);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_GD_Info',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('GDId').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Order_Sub.EnableControls;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Order_Sub.EnableControls;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmGDJinDuMXInPut.TBSaveClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('GDDate',null,[]) then
  begin
    Application.MessageBox('日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('GDPerson',null,[]) then
  begin
    Application.MessageBox('跟单员不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('OrderNo',null,[]) then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('PSName',null,[]) then
  begin
    Application.MessageBox('工序不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('BPType',null,[]) then
  begin
    Application.MessageBox('布匹类型不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('CRType',null,[]) then
  begin
    Application.MessageBox('收发不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('FactoryName',null,[]) then
  begin
    Application.MessageBox('加工厂不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('PS',null,[]) then
  begin
    Application.MessageBox('匹数不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Qty',null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('QtyUnit',null,[]) then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('CRType;FromFactoryName',VarArrayOf(['收',Null]),[loPartialKey]) then
  begin
    Application.MessageBox('收发类型为收，来自不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('CRType;ToFactoryName',VarArrayOf(['发',Null]),[loPartialKey]) then
  begin
    Application.MessageBox('收发类型为发，去向不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    //ModalResult:=1;
  end;

end;

procedure TfrmGDJinDuMXInPut.ToolButton1Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    Append;
    FieldByName('GDDate').Value:=SGetServerDate(ADOTemp);
    Post;
  end;
end;

procedure TfrmGDJinDuMXInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('GDId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_GD_Info where GDId='''+Trim(Order_Sub.fieldbyname('GDId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmGDJinDuMXInPut.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  fsj,FName:String;
begin
  //FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Order_Sub.IsEmpty then Exit;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if FName='GDPerson' then
  begin
    fsj:='select distinct(C.PFGenDanPerson) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' ';
    SInitCxGridComboBoxBySql(ADOTemp,v1GDPerson,fsj,0,True,'');

  end else
  if FName='OrderNo' then
  begin
    if Trim(Order_Sub.fieldbyname('GDPerson').AsString)='' then
    begin
      Application.MessageBox('跟单员不能为空!','提示',0);
      Exit;
    end;
    fsj:='select distinct(A.OrderNo) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' '
        +' and isnull(C.PFGenDanPerson,'''')='''+Trim(Order_Sub.fieldbyname('GDPerson').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOTemp,v1OrderNo,fsj,0,True,'');

  end else
  if FName='PSName' then
  begin
    if Trim(Order_Sub.fieldbyname('OrderNo').AsString)='' then
    begin
      Application.MessageBox('订单号不能为空!','提示',0);
      Exit;
    end;
    fsj:='select distinct(B.PSName) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' '
        +' and isnull(A.OrderNo,'''')='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOTemp,v1PSName,fsj,0,True,'');

  end else
  if FName='FactoryName' then
  begin
    if Trim(Order_Sub.fieldbyname('OrderNo').AsString)='' then
    begin
      Application.MessageBox('订单号不能为空!','提示',0);
      Exit;
    end;
    fsj:='select distinct(C.FactoryName) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' '
        +' and isnull(A.OrderNo,'''')='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOTemp,v1FactoryName,fsj,0,True,'');
    if (v1FactoryName.Properties as TcxComboBoxProperties).Items.IndexOf('长阳仓库')>-1 then Exit;
    (v1FactoryName.Properties as TcxComboBoxProperties).Items.Add('长阳仓库');
  end  else
  if FName='FromFactoryName' then
  begin
    if Trim(Order_Sub.fieldbyname('CRType').AsString)<>'收' then
    begin
      v1FromFactoryName.Options.Focusing:=False;
      v1ToFactoryName.Options.Focusing:=True;
    end else
    begin
      v1FromFactoryName.Options.Focusing:=True;
      v1ToFactoryName.Options.Focusing:=False;
    end;
    if Trim(Order_Sub.fieldbyname('OrderNo').AsString)='' then
    begin
      Application.MessageBox('订单号不能为空!','提示',0);
      Exit;
    end;
    fsj:='select distinct(C.FactoryName) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' '
        +' and isnull(A.OrderNo,'''')='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOTemp,v1FromFactoryName,fsj,0,True,'');
    if (v1FromFactoryName.Properties as TcxComboBoxProperties).Items.IndexOf('长阳仓库')>-1 then Exit;
    (v1FromFactoryName.Properties as TcxComboBoxProperties).Items.Add('长阳仓库');
  end  else
  if FName='ToFactoryName' then
  begin
    if Trim(Order_Sub.fieldbyname('CRType').AsString)<>'发' then
    begin
      v1ToFactoryName.Options.Focusing:=False;
      v1FromFactoryName.Options.Focusing:=True;
    end else
    begin
      v1ToFactoryName.Options.Focusing:=True;
      v1FromFactoryName.Options.Focusing:=False;
    end;
    if Trim(Order_Sub.fieldbyname('OrderNo').AsString)='' then
    begin
      Application.MessageBox('订单号不能为空!','提示',0);
      Exit;
    end;
    fsj:='select distinct(C.FactoryName) Name,Code='''' from JYOrder_Main A'
        +' inner join JYOrder_Process B on A.MainId=B.Mainid'
        +' inner join JYOrder_PCS_Sub C on B.PSId=C.PSId'
        +' where GDStartFlag=1 and GDFinishFlag=0 '
        +' and isnull(B.PSName,'''') not in(''检验包装'',''出货'')'
        +' and isnull(B.PSName,'''') not like ''%回检%'' '
        +' and isnull(A.OrderNo,'''')='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOTemp,v1ToFactoryName,fsj,0,True,'');
    if (v1ToFactoryName.Properties as TcxComboBoxProperties).Items.IndexOf('长阳仓库')>-1 then Exit;
    (v1ToFactoryName.Properties as TcxComboBoxProperties).Items.Add('长阳仓库');
  end;
end;

procedure TfrmGDJinDuMXInPut.ToolButton4Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('跟单信息录入',Tv1,'跟单进度管理');
end;

procedure TfrmGDJinDuMXInPut.v1Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('CRType').Value:=mvalue;
    FieldByName('FactoryName').Value:=Null;
    FieldByName('FromFactoryName').Value:=Null;
    FieldByName('ToFactoryName').Value:=Null;
    Post;
  end;
end;

procedure TfrmGDJinDuMXInPut.v1OrderNoPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('OrderNo').Value:=mvalue;
    FieldByName('PSName').Value:=Null;
    FieldByName('FactoryName').Value:=Null;
    FieldByName('FromFactoryName').Value:=Null;
    FieldByName('ToFactoryName').Value:=Null;
    Post;
  end;
end;

procedure TfrmGDJinDuMXInPut.v1GDPersonPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('GDPerson').Value:=mvalue;
    FieldByName('OrderNo').Value:=Null;
    FieldByName('PSName').Value:=Null;
    FieldByName('FactoryName').Value:=Null;
    FieldByName('FromFactoryName').Value:=Null;
    FieldByName('ToFactoryName').Value:=Null;
    Post;
  end;
end;

end.
