unit U_TatPlanMachInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, cxContainer, cxMaskEdit, dxDateRanges,
  dxScrollbarAnnotations, cxDropDownEdit, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI;

type
  TfrmTatPlanMachInput = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    PMID: TcxTextEdit;
    Label3: TLabel;
    Label5: TLabel;
    MCNO: TcxTextEdit;
    Label8: TLabel;
    Label4: TLabel;
    OrderNo: TcxButtonEdit;
    Label9: TLabel;
    MainID: TcxTextEdit;
    Label10: TLabel;
    SubID: TcxTextEdit;
    Label11: TLabel;
    C_Name: TcxTextEdit;
    C_Code: TcxTextEdit;
    Label2: TLabel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ADOQuery2: TADOQuery;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    Label13: TLabel;
    Label15: TLabel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label14: TLabel;
    Label6: TLabel;
    C_FinishLatitudeDensity: TcxTextEdit;
    C_FinishLatitudeBatchNo: TcxTextEdit;
    Label7: TLabel;
    Label12: TLabel;
    WB_Name: TcxTextEdit;
    action: TcxComboBox;
    PMType: TcxComboBox;
    WB_Code: TcxButtonEdit;
    ToolButton2: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    Tv2Column7: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    Tv2Y_Composition: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Tv2Z_Number: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Tv3BatchNO: TcxGridDBColumn;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure PMTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure PMTypePropertiesChange(Sender: TObject);
    procedure Tv3BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    canshu1, YCP, XCP: string;
    Fint, CXS: Integer;
    procedure InitData();
    function SaveData(): Boolean;
    function CheckData(): Boolean;
    procedure InitClothYarn();
    { Private declarations }
  public
    FPMID, FCYCode, FCPID, FCPNO, FCPName, FMCNo, FMCType: string;
    CopyInt, PState: Integer;

    { Public declarations }
  end;

var
  frmTatPlanMachInput: TfrmTatPlanMachInput;

implementation

uses
  U_DataLink, U_RTFun, U_iniParam, U_ZDYHelp, U_TatPlanSel, U_WBSpecSel,
  U_YarnStkSel,U_YarnInfoSel;

{$R *.dfm}

procedure TfrmTatPlanMachInput.InitData();
var
  FXHInt: Integer;
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from Tat_Plan_Mach where PMID=''' + Trim(FPMID) + '''');

      Open;
    end;
    if ADOQueryMain.IsEmpty then
    begin
      PMType.Text := '尽机安排';
      MCNo.text := FMCNo;
    end
    else
    begin
      SCSHData(ADOQueryMain, ScrollBox1, 2);
      PMType.ItemIndex := PMType.Properties.Items.IndexOf(trim(ADOQueryMain.fieldbyname('PMType').AsString));

      action.ItemIndex := action.Properties.Items.IndexOf(trim(ADOQueryMain.fieldbyname('action').AsString));

      if trim(PMType.text) = '下布改机' then
      begin
        action.text := '下布改机';
      end;
    end;





   /////////////////刷新前两次记录类型/////////////////////////
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select top 2 PMType,filltime from Tat_Plan_Mach where  MCNO =''' + Trim(MCNO.Text) + '''');
      sql.Add('ORDER BY FillTime desc ');
      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_2);
    SInitCDSData(ADOQueryTemp, CDS_2);
 /////////////////刷新前两次记录类型/////////////////////////

  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmTatPlanMachInput.InitClothYarn();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from Tat_Plan_Mach_Yarn A ');
    sql.Add(' where A.PMID=''' + Trim(FPMID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);
//  ClearCDSColumn(CDS_3, ['YRId', 'SubId', 'MainId']);

  if FPMID = '' then
  begin
    with CDS_3 do
    begin
      Append;
      FieldByName('RowNo').Value := 'A';
      Post;
      Append;
      FieldByName('RowNo').Value := 'B';
      Post;
      Append;
      FieldByName('RowNo').Value := 'C';
      Post;
      Append;
      FieldByName('RowNo').Value := 'D';
      Post;
      Append;
      FieldByName('RowNo').Value := 'E';
      Post;
      Append;
      FieldByName('RowNo').Value := 'F';
      Post;
    end;
  end;
end;

procedure TfrmTatPlanMachInput.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption) + 'Tv1', Tv1, '任务新增');
  readCxGrid(trim(self.Caption) + 'Tv2', Tv2, '任务新增');

  InitData();
  InitClothYarn();
end;

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

function TfrmTatPlanMachInput.SaveData(): Boolean;
var
  maxId, FCYNo, maxpbno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    if FPMID = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'RW', 'Tat_Plan_Mach', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      PMID.Text := trim(maxId);
    end
    else
    begin
      maxId := trim(FPMID);
      PMID.Text := trim(maxId);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_Plan_Mach where PMID=''' + Trim(FPMID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if FPMID = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;

      FieldByName('PMID').Value := Trim(maxId);
      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Mach', ScrollBox1, 2);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim(PMID.Text) + '''');
      sql.Add(',@Operate=''新增'' ');
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;

    //////////////////////////            保存纱线组合表              //////////////////////////
    with CDS_3 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_3.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'RM', 'Tat_Plan_Mach_Yarn', 4, 1) = False then
            raise Exception.Create('取配比子流水号失败！');

        end
        else
        begin
          maxpbno := Trim(CDS_3.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Plan_Mach_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_3.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('YRType').Value := '纬丝';
          FieldByName('MainId').Value := Trim(OrderNo.Text);
          FieldByName('OrderNo').Value := Trim(OrderNo.Text);
//          FieldByName('SubId').Value := Trim(maxsubno);
          FieldByName('YRId').Value := Trim(maxpbno);
          FieldByName('PMID').Value := Trim(maxId);
          RTSetSaveDataCDS(ADOQueryCmd, Tv3, CDS_3, 'Tat_Plan_Mach_Yarn', 0);
          Post;
        end;

        Next;
      end;
    end;
//////////////////////////            保存纱线组合表              //////////////////////////
               //////////////////////////



    ADOQueryCmd.Connection.CommitTrans;
    FPMID := maxId;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmTatPlanMachInput.ToolButton1Click(Sender: TObject);
var
  FWB_CODE: string;
begin

  if CheckData() = False then
  begin
    exit;
  end;
  if FPMID = '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM Tat_Plan_Mach');
      sql.Add('WHERE ISNULL(Status, ''0'') in (''0'') ');
      SQL.Add('AND MCNO=' + QuotedStr(FMCNo));
      Open;
    end;
    if ADOQueryTemp.IsEmpty = FALSE then
    begin
      Application.MessageBox('该机台已存在未完成任务，不允许保存！', '提示', 0);
      exit;
    end;

  end;

  if PMType.Text = '下布改机' then
  begin
    if FPMID = '' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('SELECT * FROM Tat_Plan_Mach');
        sql.Add('WHERE ISNULL(Status, ''0'') in (''1'') ');
        SQL.Add('AND MCNO=' + QuotedStr(FMCNo));
        Open;
      end;
      if ADOQueryTemp.IsEmpty = FALSE then
      begin
        if ADOQueryTemp.FieldByName('WB_CODE').AsString <> WB_CODE.Text then
        begin
          Application.MessageBox('工艺不同，不允许保存！', '提示', 0);
          exit;
        end;

      end;

    end;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmTatPlanMachInput.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '任务新增');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '任务新增');
end;

procedure TfrmTatPlanMachInput.Tv3BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
      FstkName := '纱线';
      if ShowModal = 1 then
      begin
        frmYarnStkSel.CDS_Main.DisableControls;

        with Self.CDS_3 do
        begin
          edit;
          FieldByName('BatchNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('BatchNo').Value;
        end;

        frmYarnStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;

end;

procedure TfrmTatPlanMachInput.v2Column10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_3 do
        begin
          Edit;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;

//          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').Value;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;

          FieldByName('FF').Value := frmYarnInfoSel.CDS_1.fieldbyname('FF').Value;
          FieldByName('ND').Value := frmYarnInfoSel.CDS_1.fieldbyname('ND').Value;

//          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Composition').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Composition').Value;
          FieldByName('TestDenier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;


          Post;
        end;


      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatPlanMachInput.WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBSpecSel := TfrmWBSpecSel.Create(Application);
    with frmWBSpecSel do
    begin
      if ShowModal = 1 then
      begin
        Self.WB_Code.Text := frmWBSpecSel.CDS_1.fieldbyname('WB_Code').AsString;
        Self.WB_Name.Text := frmWBSpecSel.CDS_1.fieldbyname('WB_Name').AsString;

      end;
    end;

  finally
    frmWBSpecSel.Free;
  end;
end;

function TfrmTatPlanMachInput.CheckData(): Boolean;
var
  mvalue, FReal: Double;
  maxConNo: string;
begin
  try
    if Trim(orderno.Text) = '' then
      raise Exception.Create('梭织单号不能为空!');

    if Trim(action.Text) = '' then
      raise Exception.Create('操作类型不能为空!');


//    if Trim(CYNo.Text) = '' then
//      raise Exception.Create('产品编号不能为空!');
//    if Trim(JZPM.Text) = '' then
//      raise Exception.Create('经轴品名不能为空!');
//    if Trim(weimi.Text) = '' then
//      raise Exception.Create('纬密不能为空!');
//    if Trim(wsspec.Text) = '' then
//      raise Exception.Create('纬纱规格不能为空!');
//    if Trim(pici.Text) = '' then
//      raise Exception.Create('批次不能为空!');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatPlanMachInput.PMTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);

      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTatPlanMachInput.PMTypePropertiesChange(Sender: TObject);
begin
  if Trim(PMType.text) = '下布改机' then
  begin
    action.Text := '下布改机';
    action.Enabled := False;
  end
  else
  begin
    action.Text := '';
    action.Enabled := true;
  end;

end;

procedure TfrmTatPlanMachInput.OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTatPlanSel := TfrmTatPlanSel.Create(Application);
    with frmTatPlanSel do
    begin

      if ShowModal = 1 then
      begin
        Self.OrderNo.Text := frmTatPlanSel.CDS_1.fieldbyname('OrderNo').AsString;
        Self.MainID.Text := frmTatPlanSel.CDS_1.fieldbyname('MainID').AsString;
        Self.SubID.Text := frmTatPlanSel.CDS_1.fieldbyname('SubID').AsString;
        Self.C_Code.Text := frmTatPlanSel.CDS_1.fieldbyname('C_Code').AsString;
        Self.C_Name.Text := frmTatPlanSel.CDS_1.fieldbyname('C_Name').AsString;
        Self.C_FinishLatitudeDensity.Text := frmTatPlanSel.CDS_1.fieldbyname('C_FinishLatitudeDensity').AsString;
        Self.WB_Code.Text := frmTatPlanSel.CDS_1.fieldbyname('WB_Code').AsString;
        Self.WB_Name.Text := frmTatPlanSel.CDS_1.fieldbyname('WB_Name').AsString;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select WB_Code,WB_Name from Bs_Cloth_Info  where C_Code =''' + Trim(Self.C_Code.Text) + '''');
          Open;
        end;
//        Self.WB_Code.Text := ADOQueryTemp.fieldbyname('WB_Code').AsString;
//        Self.WB_Name.Text := ADOQueryTemp.fieldbyname('WB_Name').AsString;


        with self.ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' select * from Tat_Plan_Yarn ');
          sql.Add(' where  YRType=''纬丝'' and MainId=''' + Trim(frmTatPlanSel.CDS_1.fieldbyname('MainId').AsString) + '''');
          sql.Add(' Order by RowNo ');
          Open;
        end;

        self.CDS_3.EmptyDataSet;
        self.ADOQueryCmd.First;

        while not self.ADOQueryCmd.eof do
        begin
          with self.CDS_3 do
          begin
            APpend;
            FieldByName('RowNo').Value := self.ADOQueryCmd.fieldbyname('RowNo').Value;

            FieldByName('Y_Code').Value := self.ADOQueryCmd.fieldbyname('Y_Code').Value;
            FieldByName('Y_Name').Value := self.ADOQueryCmd.fieldbyname('Y_Name').Value;

            FieldByName('Y_Color').Value := self.ADOQueryCmd.fieldbyname('Y_Color').Value;
            FieldByName('Y_Composition').Value := self.ADOQueryCmd.fieldbyname('Y_Composition').Value;

            FieldByName('BatchNO').Value := self.ADOQueryCmd.fieldbyname('BatchNO').Value;

            FieldByName('Z_Number').Value := self.ADOQueryCmd.fieldbyname('Z_Number').Value;

//            FieldByName('FF').Value := self.ADOQueryCmd.fieldbyname('FF').Value;
//            FieldByName('ND').Value := self.ADOQueryCmd.fieldbyname('ND').Value;
            FieldByName('note').Value := self.ADOQueryCmd.fieldbyname('note').Value;

            Post;
          end;

          self.ADOQueryCmd.next;
        end;

      end;
    end;

  finally
    frmTatPlanSel.Free;
  end;
end;

end.

