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;
    Label17: TLabel;
    Label6: TLabel;
    C_FinishLatitudeDensity: TcxTextEdit;
    C_FinishLatitudeSpec: TcxTextEdit;
    C_FinishLatitudeBatchNo: TcxTextEdit;
    Label7: TLabel;
    Label12: TLabel;
    WB_Name: TcxTextEdit;
    action: TcxComboBox;
    PMType: TcxComboBox;
    WB_Code: TcxButtonEdit;
    ToolButton2: TToolButton;
    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);
  private
    canshu1, YCP, XCP: string;
    Fint, CXS: Integer;
    procedure InitData();
    function SaveData(): Boolean;
    function CheckData(): Boolean;
    { 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;

{$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.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption) + 'Tv1', Tv1, '任务新增');
  readCxGrid(trim(self.Caption) + 'Tv2', Tv2, '任务新增');
  InitData();
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;

    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.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;
      end;
    end;

  finally
    frmTatPlanSel.Free;
  end;
end;

end.

