unit U_ShuttleLSSCInPut;

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,
  StrUtils, cxPC, ShellAPI, cxCalendar, Math, Menus, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmShuttleLSSCInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDS_Sanjiao: TClientDataSet;
    DS_Sanjiao: TDataSource;
    Label4: TLabel;
    BatchNo: TEdit;
    Label6: TLabel;
    Y_Spec: TEdit;
    FactoryName: TBtnEditC;
    Y_CodeName: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    Label3: TLabel;
    ZGZL: TEdit;
    Label11: TLabel;
    ZXZL: TEdit;
    Label1: TLabel;
    ZGKS: TEdit;
    Label5: TLabel;
    SJKS: TEdit;
    Label2: TLabel;
    Y_CodeNameCP: TEdit;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1Column1: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FactoryNameBtnUpClick(Sender: TObject);
  private
    FXS, Fint: Integer;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, FCopyInt, fpd: Integer;
    FMainId, FConNo, PType, FSubId: string;
    { Public declarations }
  end;

var
  frmShuttleLSSCInPut: TfrmShuttleLSSCInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_ZdyAttachGYS, U_ContractListNXSel,
  U_CompressionFun, U_FjList_RZ, U_CPManageFMSel, U_ZdyAttachment,
  U_DyeFinishScheduleListSel, U_YarnPCGLListSel, U_YarnListSel, U_JTSel;

{$R *.dfm}

procedure TfrmShuttleLSSCInPut.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmShuttleLSSCInPut.InitData();
var
  PBNO, PBFlag,fsj: string;
  i:Integer;
begin
  if fpd <> 1 then
  begin
     /////////////////刷新产品信息/////////////////////////
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from YT_Plan_Main A');
      if PState = 1 then
      begin
        sql.Add(' where A.MainId=' + Quotedstr(Trim(FMainId)));
      end;
      if PState = 0 then
      begin
        sql.Add('  where 1<>1 ');
      end;
      Open;
    end;
    SCSHDataNew(ADOQueryTemp, ScrollBox1, 0);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YT_Plan_Rule where Mainid='''+Trim(FMainId)+''' and RType=''颜色''  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  fsj:='select Name=ColorName,Code=ColorName from ZGColor  ';
  SInitCxGridComboBoxBySql(ADOQueryTemp,Tv1Column1,fsj,1,true,'');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YT_Plan_Rule where Mainid='''+Trim(FMainId)+''' and RType=''机台'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  if ClientDataSet1.IsEmpty then
  begin
    for i:=1 to 10 do
    begin
      with ClientDataSet1 do
      begin
        Append;
        Post;
      end;
    end;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    for i:=1 to 10 do
    begin
      with ClientDataSet2 do
      begin
        Append;
        Post;
      end;
    end;
  end;
  if PState = 0 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1 * from YT_Plan_Main order by FillTime desc');
      Open;
    end;
    ZGZL.Text:=Trim(ADOQueryTemp.fieldbyname('ZGZL').AsString);
    ZXZL.Text:=Trim(ADOQueryTemp.fieldbyname('ZXZL').AsString);
    ZGKS.Text:=Trim(ADOQueryTemp.fieldbyname('ZGKS').AsString);
    SJKS.Text:=Trim(ADOQueryTemp.fieldbyname('SJKS').AsString);
  end;

  if FCopyInt = 1 then
  begin
    FMainId := '';
  end;
end;

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

end;

procedure TfrmShuttleLSSCInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmShuttleLSSCInPut.SaveData(): Boolean;
var
  maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'LM', 'YT_Plan_Main', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('生成流水号异常！', '提示', 0);
        exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from YT_Plan_Main where MainId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('Status').Value := '0';
        FieldByName('Valid').Value := 'Y';
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('MainId').Value := Trim(maxno);
      RTSetsavedata(ADOQueryCmd, 'YT_Plan_Main', ScrollBox1, 0);

      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YT_Plan_Main where isnull(BatchNo,'''')='''+Trim(BatchNo.Text)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('批号不能重复!','提示',0);
      Result:=False;
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YT_Plan_Rule where MainId='''+Trim(maxno)+'''');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YT_Plan_Rule where 1=2');
      Open;
    end;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('MainId').Value:=Trim(maxno);
          ADOQueryCmd.FieldByName('RType').Value:=Trim('颜色');
          ADOQueryCmd.FieldByName('RName').Value:=Trim(ClientDataSet1.fieldbyname('RName').AsString);
          ADOQueryCmd.Post;
        end;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    ClientDataSet2.DisableControls;
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('MainId').Value:=Trim(maxno);
          ADOQueryCmd.FieldByName('RType').Value:=Trim('机台');
          ADOQueryCmd.FieldByName('RName').Value:=Trim(ClientDataSet2.fieldbyname('RName').AsString);
          ADOQueryCmd.Post;
        end;
        Next;
      end;
    end;
    ClientDataSet2.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;

    FMainId := Trim(maxno);
    Result := True;
  except
    Result := False;

    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmShuttleLSSCInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  maxConNo: string;
begin
  FactoryName.SetFocus;

  if trim(FactoryName.Text) = '' then
  begin
    Application.MessageBox('厂家不能为空!', '提示', 0);
    Exit;
  end;

  if trim(BatchNo.Text) = '' then
  begin
    Application.MessageBox('批号不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmShuttleLSSCInPut.FactoryNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin

      if ShowModal = 1 then
      begin

        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

end.

