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, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges;

type
  TfrmTatPlanMachInput = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    PMID: TEdit;
    Label3: TLabel;
    Label5: TLabel;
    MCNO: TEdit;
    Label8: TLabel;
    PMType: TComboBox;
    Label4: TLabel;
    OrderNo: TcxButtonEdit;
    Label9: TLabel;
    MainID: TEdit;
    Label10: TLabel;
    SubID: TEdit;
    Label11: TLabel;
    C_Name: TEdit;
    C_Code: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure PMTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  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: string;
    CopyInt, PState: Integer;

    { Public declarations }
  end;

var
  frmTatPlanMachInput: TfrmTatPlanMachInput;

implementation

uses
  U_DataLink, U_RTFun, U_iniParam, U_ZDYHelp, U_TatPlanSel;

{$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);
    end;

  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmTatPlanMachInput.FormShow(Sender: TObject);
begin
  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 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);

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_Plan_Mach where PMID=''' + Trim(FPMID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin

      Append;
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTemp);
      FieldByName('PMID').Value := Trim(maxId);
      FieldByName('PMType').Value := Trim(PMType.text);
      FieldByName('MCNo').Value := Trim(MCNo.text);
      RTSetsavedata(ADOQueryCmd, 'Tat_Plan_Mach', ScrollBox1, 2);
      Post;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim(PMID.Text) + '''');
      sql.Add(',@Operate=''新增'' ');
      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryTemp.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);
begin

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

function TfrmTatPlanMachInput.CheckData(): Boolean;
var
  mvalue, FReal: Double;
  maxConNo: string;
begin
  try
    if Trim(orderno.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
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  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;
      end;
    end;

  finally
    frmTatPlanSel.Free;
  end;
end;

end.

