unit U_OrderInPut;

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, cxCalendar;

type
  TfrmOrderInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    OrderNo: TEdit;
    OrderDate: TDateTimePicker;
    Label4: TLabel;
    DeliveryDate: TDateTimePicker;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    ScrollBox2: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNO: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1OrderQty: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    Label9: TLabel;
    Chker: TEdit;
    Label10: TLabel;
    Person1: TEdit;
    Label11: TLabel;
    Person2: TEdit;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    PRTCodeName: TcxButtonEdit;
    FactoryNo1Name: TcxButtonEdit;
    FactoryNo2Name: TcxButtonEdit;
    FactoryNo3Name: TcxButtonEdit;
    ADOQuery1: TADOQuery;
    PanZDY: TPanel;
    CXGridZDY: TcxGrid;
    TVZDY: TcxGridDBTableView;
    VHelpZDYName: TcxGridDBColumn;
    CXGridZDYLevel1: TcxGridLevel;
    Button1: TButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label12: TLabel;
    CustomerNoName: TcxButtonEdit;
    Note: TRichEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure TVZDYCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure PRTCodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure PRTCodeNamePropertiesChange(Sender: TObject);
    procedure FactoryNo1NamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FactoryNo1NamePropertiesChange(Sender: TObject);
    procedure FactoryNo2NamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FactoryNo2NamePropertiesChange(Sender: TObject);
    procedure FactoryNo3NamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FactoryNo3NamePropertiesChange(Sender: TObject);
    procedure PRTCodeNameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TVZDYKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PRTUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomNoNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure CustomNoNamePropertiesChange(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState:Integer;
    FMainId:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmOrderInPut: TfrmOrderInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun;

{$R *.dfm}

procedure TfrmOrderInPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmOrderInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec Order_QryList :MainId,:WSql');
    if PState=1 then
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:=Trim(FMainId);
      ADOQuery1.Parameters.ParamByName('WSQl').Value:='';
    end;
    if PState=0 then
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:=Trim(FMainId);
      ADOQuery1.Parameters.ParamByName('WSql').Value:=' and 1<>1 ';
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  SCSHData(ADOQuery1,ScrollBox1,0);
  if PState=0 then
  begin
    OrderDate.DateTime:=SGetServerDateTime(ADOTemp);
    DeliveryDate.DateTime:=SGetServerDateTime(ADOTemp);
  end;
  PanZDY.Visible:=False;
end;

procedure TfrmOrderInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
  PanZDY.Visible:=True;
  PanZDY.Left:=FButn.Left;
  PanZDY.Top:=FButn.Top+FButn.Height;
  with ADOZDY do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    SQL.Add('select RTrim(ZDYNo) ZDYNo,RTrim(ZDYName) ZDYName from KH_ZDY where Type='''+Trim(LType)+'''');
    Open;
  end;
  FText:=Trim(FButn.Text);
  if FText<>'' then
    SDofilter(ADOZDY,' ZDYName like '+QuotedStr('%'+Trim(FText)+'%'))
  else
    SDofilter(ADOZDY,'');
  VHelpZDYName.Summary.GroupFormat:=Trim(FButn.Name);
end;

procedure TfrmOrderInPut.TVZDYCellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  if ADOZDY.IsEmpty then Exit;
  FName:=Trim(VHelpZDYName.Summary.GroupFormat);
  TcxButtonEdit(FindComponent(FName)).Text:=Trim(ADOZDY.fieldbyname('ZDYName').AsString);
  TcxButtonEdit(FindComponent(FName)).Hint:=Trim(ADOZDY.fieldbyname('ZDYNO').AsString);
  PanZDY.Visible:=False;
end;

procedure TfrmOrderInPut.Button1Click(Sender: TObject);
begin
  PanZDY.Visible:=False;
end;

procedure TfrmOrderInPut.PRTCodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim(PRTCodeName.Name);
      flagname:='产品名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        PRTCodeName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        PRTCodeName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.PRTCodeNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(PRTCodeName,Trim(PRTCodeName.Name));
end;

procedure TfrmOrderInPut.FactoryNo1NamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim(FactoryNo1Name.Name);
      flagname:='坯布厂名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        FactoryNo1Name.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        FactoryNo1Name.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
       // VHelpZDYName.Visible:=False;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.FactoryNo1NamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(FactoryNo1Name,Trim(FactoryNo1Name.Name));
end;

procedure TfrmOrderInPut.FactoryNo2NamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim(FactoryNo2Name.Name);
      flagname:='印染厂名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        FactoryNo2Name.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        FactoryNo2Name.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.FactoryNo2NamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(FactoryNo2Name,Trim(FactoryNo2Name.Name));
end;

procedure TfrmOrderInPut.FactoryNo3NamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim(FactoryNo3Name.Name);
      flagname:='加工厂名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        FactoryNo3Name.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        FactoryNo3Name.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.FactoryNo3NamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(FactoryNo3Name,Trim(FactoryNo3Name.Name));
end;

procedure TfrmOrderInPut.PRTCodeNameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key=vk_return) or (Key=vk_Down) then
  begin
    if ADOZDY.Active then
      CXGridZDY.SetFocus;
  end;
end;

procedure TfrmOrderInPut.TVZDYKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if ADOZDY.IsEmpty then Exit;
    TcxButtonEdit(FindComponent(VHelpZDYName.Summary.GroupFormat)).Text:=Trim(ADOZDY.fieldbyname('ZDYName').AsString);
    TcxButtonEdit(FindComponent(VHelpZDYName.Summary.GroupFormat)).Hint:=Trim(ADOZDY.fieldbyname('ZDYNO').AsString);
    PanZDY.Visible:=False;
    ADOZDY.Active:=False;
  end;
end;

procedure TfrmOrderInPut.FormShow(Sender: TObject);
begin
  if Trim(DParameters1)='1' then
  begin
    Label12.Visible:=False;
    CustomerNoName.Visible:=False;
  end else
  begin
    Label12.Visible:=True;
    CustomerNoName.Visible:=True;
  end;
  InitData();
end;

function TfrmOrderInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Order_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        if GetLSNo(ADOTemp,maxno,'OM','Order_Main',2,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          exit;
        end;
      end
      else begin
        maxno:=Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      SSetsaveSql(ADOCmd,'Order_Main',ScrollBox1,0);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId:=Trim(maxno);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'OS','Order_Sub',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Order_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd,Tv1,Order_Sub,'Order_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmOrderInPut.TBSaveClick(Sender: TObject);
begin
  OrderDate.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单编号不能为空！','提示',0);
    Exit;
  end;
  if Trim(PRTCodeName.Text)='' then
  begin
    Application.MessageBox('品名不能为空！','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmOrderInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdUnit';
      flagname:='单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdUnit';
      flagname:='单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.ToolButton1Click(Sender: TObject);
var
  i:Integer;
begin
  i:=Order_Sub.RecordCount;
  i:=i+1;
  with Order_Sub do
  begin
    Append;
    FieldByName('XHNO').Value:=IntToStr(i);
    Post;
  end;
end;

procedure TfrmOrderInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  end;
  Order_Sub.Delete;
end;

procedure TfrmOrderInPut.CustomNoNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('CustomNoName');
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        CustomerNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomerNoName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut.CustomNoNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(CustomerNoName,'CustomNoName');
end;

end.
