unit U_CPAddPN;

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;

type
  TfrmCPAddPN = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label7: TLabel;
    CYNo: TEdit;
    CYNote: TMemo;
    Label5: TLabel;
    CYName: TEdit;
    Label11: TLabel;
    CYCF: TEdit;
    Label15: TLabel;
    CYMF: TEdit;
    Label22: TLabel;
    CYKZ: TEdit;
    Label23: TLabel;
    Label2: TLabel;
    CYColor: TEdit;
    Label10: TLabel;
    CYID: TEdit;
    Label3: TLabel;
    CYSpec: TMemo;
    CYPrice: TEdit;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label4: TLabel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label6: TLabel;
    Y_Note_PLJ: TMemo;
    Label8: TLabel;
    Y_Note_PLW: TMemo;
    Label9: TLabel;
    DS_YarnJX: TDataSource;
    CDS_YarnJX: TClientDataSet;
    CDS_YarnWX: TClientDataSet;
    DS_YarnWX: TDataSource;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYKZExit(Sender: TObject);
    procedure CYMFExit(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitSubGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FCYID, FCYCode, FCPID, FCPNO, FCPName: string;
    CopyInt,PState: Integer;

    { Public declarations }
  end;

var
  frmCPAddPN: TfrmCPAddPN;

implementation

uses
  U_DataLink, U_Fun, U_iniParam, U_ZDYHelp, U_ZdyAttachGYS, U_FileUp;

{$R *.dfm}

procedure TfrmCPAddPN.InitSubGrid();
var
  FXHInt: Integer;
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCSHDataNew(ADOQueryMain, ScrollBox1, 2);
    SCSHDataNew(ADOQueryMain, ScrollBox1, 0);

    /////////////////刷新经丝/////////////////////////
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_Plan_Yarn where PYType=''经丝'' and Y_CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_YarnJX);
    SInitCDSData20(ADOQueryTemp, CDS_YarnJX);
    Y_Note_PLJ.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
    if (CopyInt = 0) and (PState<>1) then
    begin
      with CDS_YarnJX do
      begin
        Append;
        FieldByName('PYXH').Value := '甲经';
        Post;
        Append;
        FieldByName('PYXH').Value := '乙经';
        Post;
        Append;
        FieldByName('PYXH').Value := '丙经';
        Post;
        Append;
        FieldByName('PYXH').Value := '丁经';
        Post;
        Append;
        FieldByName('PYXH').Value := '戊经';
        Post;
        Append;
        FieldByName('PYXH').Value := '己经';
        Post;
        Append;
        FieldByName('PYXH').Value := '庚经';
        Post;
      end;
    end;
   /////////////////刷新经丝/////////////////////////

   /////////////////刷新纬丝/////////////////////////

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_Plan_Yarn where PYType=''纬丝'' and Y_CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_YarnWX);
    SInitCDSData20(ADOQueryTemp, CDS_YarnWX);
    Y_Note_PLW.Text := Trim(ADOQueryTemp.FieldByName('Y_Note_PL').AsString);
    if (CopyInt = 0) and (PState<>1) then
    begin
      with CDS_YarnWX do
      begin
        Append;
        FieldByName('PYXH').Value := '甲纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '乙纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '丙纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '丁纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '戊纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '己纬';
        Post;
        Append;
        FieldByName('PYXH').Value := '庚纬';
        Post;
      end;
    end;
 /////////////////刷新纬丝/////////////////////////

  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmCPAddPN.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(self.Caption), Tv1, '产品录入经丝');
  ReadCxGrid(Trim(self.Caption), Tv2, '产品录入纬丝');
  InitSubGrid();
  if CopyInt = 1 then
  begin
    FCYID := '';
    CYID.text := '';
    CYNo.Text :='';
    /////////////////清除原料ID/////////////////////////
    CDS_YarnJX.DisableControls;
    with CDS_YarnJX do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('Y_CYID').Value := '';
        FieldByName('PYId').Value := '';
        Post;
        Next;
      end;
    end;
    CDS_YarnJX.EnableControls;

    CDS_YarnWX.DisableControls;
    with CDS_YarnWX do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('PYId').Value := '';
        FieldByName('Y_CYID').Value := '';
        Post;
        Next;
      end;
    end;
    CDS_YarnWX.EnableControls;
/////////////////清除原料ID/////////////////////////
  end;
end;

procedure TfrmCPAddPN.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption), Tv1, '产品录入经丝');
  WriteCxGrid(Trim(self.Caption), Tv2, '产品录入纬丝');
  Close;
end;

function TfrmCPAddPN.SaveData(): Boolean;
var
  maxId, FCYNo,maxpbno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'CP_YDang', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      CYID.Text := trim(maxId);
    end
    else
    begin
      maxId := Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID) = '' then
      begin
        Append;
        FieldByName('CYType').Value := Trim(FCPID);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillCode').Value := Trim(DCode);
        FieldByName('Filler').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditCode').Value := Trim(DCode);
        FieldByName('Editer').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('CYID').Value := Trim(maxId);
      if trim(CYNo.Text) = '' then
        CYNo.Text := Trim(maxId);
      SSetsaveSqlNew(ADOQueryCmd, 'CP_YDang', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where CYNo=''' + Trim(CYNo.Text) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    ///////////////////////////////////经丝组合//////////////////////////////
    with CDS_YarnJX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnJX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'CP_Plan_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('经丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnJX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_Plan_Yarn where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnJX.fieldbyname('PYId').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('PYType').Value := '经丝';
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_CYID').Value := Trim(maxId);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLJ.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_YarnJX, 'CP_Plan_Yarn', 0);
          Post;
        end;
        CDS_YarnJX.Edit;
        CDS_YarnJX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////经丝组合//////////////////////////////

///////////////////////////////////纬丝组合//////////////////////////////
    with CDS_YarnWX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnWX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'CP_Plan_Yarn', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('纬丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnWX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_Plan_Yarn where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnWX.fieldbyname('PYId').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('PYType').Value := '纬丝';
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_CYID').Value := Trim(maxId);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLW.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_YarnWX, 'CP_Plan_Yarn', 0);
          Post;
        end;
        CDS_YarnJX.Edit;
        CDS_YarnJX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////纬丝组合//////////////////////////////

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

procedure TfrmCPAddPN.ToolButton1Click(Sender: TObject);
begin
  if CYNo.Text = '' then
  begin
    Application.MessageBox('产品编号不能为空！', '提示', 0);
    Exit;
  end;
  if CYName.Text = '' then
  begin
//    Application.MessageBox('产品名称不能为空！', '提示', 0);
//    Exit;
    CYName.Text := CYNo.Text;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmCPAddPN.CYColorKeyPress(Sender: TObject; var Key: Char);
var
  c: Integer;
begin
  if Key = '.' then
  begin
    for c := 1 to length(TEdit(Sender).text) do
    begin
      if Tedit(Sender).text[c] = '.' then
        Key := #0;
    end;
  end
  else if Key = #13 then
    PerForm(WM_NEXTDLGCTL, 0, 0)
  else if Key = #8 then
    Key := #8
  else if (Key < '0') or (Key > '9') then
    Key := #0;
end;

procedure TfrmCPAddPN.CYKZExit(Sender: TObject);
begin
  if pos('GSM', trim(CYKZ.Text)) = 0 then
    CYKZ.Text := trim(CYKZ.Text) + 'GSM';
end;

procedure TfrmCPAddPN.CYMFExit(Sender: TObject);
begin
  if pos('CM', trim(CYMF.Text)) = 0 then
    CYMF.Text := trim(CYMF.Text) + 'CM';
end;

end.

