unit U_JTSJY220318;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, DB, ADODB, Buttons,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  DBClient, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmJTSJY220318 = class(TForm)
    CPInfo: TRichEdit;
    ADOQueryTemp: TADOQuery;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel1: TPanel;
    Label2: TLabel;
    Panel2: TPanel;
    Button2: TButton;
    SQty: TEdit;
    Label4: TLabel;
    Button3: TButton;
    DQty: TEdit;
    Panel8: TPanel;
    Panel9: TPanel;
    Label1: TLabel;
    ZGH: TEdit;
    Label3: TLabel;
    ZGL: TEdit;
    Label5: TLabel;
    Label7: TLabel;
    ZGQty: TEdit;
    Label9: TLabel;
    Panel5: TPanel;
    BZX: TEdit;
    BZB: TEdit;
    Label6: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    Panel6: TPanel;
    btnSampleSel: TButton;
    Button1: TButton;
    Panel10: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel7: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    v3Column4: TcxGridDBColumn;
    Panel11: TPanel;
    Label11: TLabel;
    Label12: TLabel;
    SJZL: TEdit;
    ADOCmd: TADOQuery;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    Button4: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure btnSampleSelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure ZGHClick(Sender: TObject);
    procedure ZGLClick(Sender: TObject);
    procedure BZXClick(Sender: TObject);
    procedure BZBClick(Sender: TObject);
    procedure ZGQtyClick(Sender: TObject);
    procedure SQtyClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    ZGKS,SJKS,ZGZL,ZXZL,FactoryName,Y_CodeName ,Y_Spec,BatchNo,FFFMJID:String;
    function SaveData(DataType:String): Boolean;
    procedure InitGrid();
    procedure InitJP(FName:string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmJTSJY220318: TfrmJTSJY220318;

implementation
uses
  U_DataLink, U_RTFun,U_CPSel,U_iniParam,U_SCPerson;

{$R *.dfm}

procedure TfrmJTSJY220318.FormDestroy(Sender: TObject);
begin
  frmJTSJY220318:=nil;
end;

procedure TfrmJTSJY220318.btnSampleSelClick(Sender: TObject);
begin
  try
    frmCPSel:=TfrmCPSel.Create(Self);
    with frmCPSel do
    begin
      if ShowModal=1 then
      begin
        CPInfo.Hint:=FHMainId;
      end;
    end;
  finally
    frmCPSel.Close;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YT_Plan_Main where Mainid='''+Trim(CPInfo.Hint)+'''');
    Open;
  end;
  with ADOQueryTemp do
  begin
    FactoryName:=Trim(fieldbyname('FactoryName').AsString);
    Y_CodeName:=Trim(fieldbyname('Y_CodeName').AsString);
    Y_Spec:=Trim(fieldbyname('Y_Spec').AsString);
    BatchNo:=Trim(fieldbyname('BatchNo').AsString);
    ZGKS:=Trim(fieldbyname('ZGKS').AsString);
    SJKS:=Trim(fieldbyname('SJKS').AsString);
    ZGZL:=Trim(fieldbyname('ZGZL').AsString);
    ZXZL:=Trim(fieldbyname('ZXZL').AsString);
  end;
  ADOQueryTemp.Close;
  CPInfo.Text:= '  '+ #13
                                         +'工厂：'+Trim(FactoryName) + #13+ #13
                                         +'品名：'+Trim(Y_CodeName) + #13 + #13
                                         +'规格：'+Trim(Y_Spec) + #13 + #13
                                         +'批号：'+Trim(BatchNo)+ #13 + #13
                                         +'纸管重量：'+Trim(ZGZL)+ #13 + #13
                                         +'纸箱重量：'+Trim(ZXZL)+ #13 + #13
                                         +'纸管颗数：'+Trim(ZGKS)+ #13 + #13
                                         +'板颗数：'+Trim(SJKS);

  if Trim(BZX.Text)<>'' then
  begin
    ZGQty.Text:=ZGKS;
  end;
  if Trim(BZB.Text)<>'' then
  begin
    ZGQty.Text:=SJKS;
  end;
end;

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

procedure TfrmJTSJY220318.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmJTSJY220318.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmJTSJY220318.ZGHClick(Sender: TObject);
begin
  if Trim(ZGH.Text)='' then
  begin
    ZGH.Text:='√';
    ZGL.Text:='';
  end else
  begin
    ZGL.Text:='√';
    ZGH.Text:='';
  end;
end;

procedure TfrmJTSJY220318.ZGLClick(Sender: TObject);
begin
  if Trim(ZGL.Text)='' then
  begin
    ZGL.Text:='√';
    ZGH.Text:='';
  end else
  begin
    ZGH.Text:='√';
    ZGL.Text:='';
  end;
end;

procedure TfrmJTSJY220318.BZXClick(Sender: TObject);
begin
  if Trim(BZX.Text)='' then
  begin
    BZX.Text:='√';
    BZB.Text:='';
    DQty.Text:='35';
    ZGQty.Text:=ZGKS;
    SJZL.Text:='';
    SJZL.ReadOnly:=True;
  end else
  begin
    BZB.Text:='√';
    DQty.Text:='985';
    BZX.Text:='';
    ZGQty.Text:=SJKS;
    SJZL.ReadOnly:=False;
  end;
end;

procedure TfrmJTSJY220318.BZBClick(Sender: TObject);
begin
  if Trim(BZB.Text)='' then
  begin
    BZB.Text:='√';
    BZX.Text:='';
    DQty.Text:='985';
    ZGQty.Text:=SJKS;
    SJZL.ReadOnly:=False;
  end else
  begin
    BZX.Text:='√';
    DQty.Text:='35';
    BZB.Text:='';
    ZGQty.Text:=ZGKS;
    SJZL.ReadOnly:=True;
    SJZL.Text:='';
  end;
end;

procedure TfrmJTSJY220318.ZGQtyClick(Sender: TObject);
var
  i:Integer;
begin
  {with Panel7 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end; }
end;

procedure TfrmJTSJY220318.SQtyClick(Sender: TObject);
var
  i:Integer;
begin
  with Panel7 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;
end;
procedure TfrmJTSJY220318.InitJP(FName:string);
var
  i:Integer;
begin
  with Panel7 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(FName);
      end;
    end;
  end;
end;

procedure TfrmJTSJY220318.SpeedButton1Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TSpeedButton(Sender).Hint);
  if Trim(fsj)='SJZL' then
  begin
    if BZX.Text<>'' then Exit;
  end;
  if Trim(fsj) = '' then
    Exit;
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := fsj + Trim(TSpeedButton(Sender).Caption);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;

end;

procedure TfrmJTSJY220318.SpeedButton12Click(Sender: TObject);
var
  fsj: string;
begin
  if Trim(TSpeedButton(Sender).Hint)='SJZL' then
  begin
    if BZX.Text<>'' then Exit;
  end;
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  if Trim(fsj) = '' then
    Exit;
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := Copy(fsj, 1, Length(fsj) - 1);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

function TfrmJTSJY220318.SaveData(DataType:String): Boolean;
var
  maxno,FMJID: string;
begin
  FMJID:='';
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if GetLSNo(ADOCmd, maxno, 'T' + Trim(SCXFlag), 'JY_YT', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('生成流水号异常！', '提示', 0);
      exit;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JY_YT where JYId=''' + Trim(FMJID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('JYID').Value := Trim(maxno);
      FieldByName('FactoryName').value :=FactoryName;
      FieldByName('Y_CodeName').value := Trim(Y_CodeName);
      FieldByName('Y_Spec').value := Trim(Y_Spec);
      FieldByName('BatchNo').value := Trim(BatchNo);
      if Trim(ZGH.Text)<>'' then
      begin
        FieldByName('ZGColor').value :='红色';
      end;
      if Trim(ZGL.Text)<>'' then
      begin
        FieldByName('ZGColor').value :='绿色';
      end;
      FieldByName('CRType').Value := '已入库';
      FieldByName('Valie').Value := 'Y';
      FieldByName('ZGZL').Value := StrToFloatdef(trim(ZGZL), 0);
      FieldByName('ZGKS').Value := StrToFloatdef(trim(ZGQty.Text), 0);
      if Trim(BZB.Text)<>'' then
      begin
        FieldByName('SJZL').Value := StrToFloatdef(trim(SJZL.Text), 0);
        FieldByName('BZType').Value :='板';
        FieldByName('SJNO').Value :=Trim(SJZL.Hint);
      end else
      begin
        FieldByName('SJZL').Value :=null;
        FieldByName('SJNO').Value :=Null;
      end;
      if Trim(BZX.Text)<>'' then
      begin
        FieldByName('ZXZL').Value := StrToFloatdef(trim(ZXZL), 0);
        FieldByName('BZType').Value :='箱';
      end else
      begin
        FieldByName('ZXZL').Value :=null;
      end;
      if Trim(DataType)='定重' then
      begin
        FieldByName('JYGW').Value := StrToFloatdef(trim(DQty.Text), 0);
      end;
      if Trim(DataType)='手输' then
      begin
        FieldByName('JYGW').Value := StrToFloatdef(trim(SQty.Text), 0);
      end;
      if Trim(FMJID) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('FillCode').Value := Trim(DCode);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('JTType').Value := Trim(SCXFlag);
      Post;
    end;
    FMJID := trim(maxno);
    FFFMJID := FMJID;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('JYID').Value:=Trim(maxno);
      FieldByName('ZGKS').Value:=trim(ZGQty.Text);
      if Trim(ZGH.Text)<>'' then
      begin
        FieldByName('ZGColor').value :='红色';
      end;
      if Trim(ZGL.Text)<>'' then
      begin
        FieldByName('ZGColor').value :='绿色';
      end;
      if Trim(DataType)='定重' then
      begin
        FieldByName('JYGW').Value :=trim(DQty.Text);
      end;
      if Trim(DataType)='手输' then
      begin
        FieldByName('JYGW').Value := trim(SQty.Text);
      end;
      Post;
    end;

////////////////////////净重、出入库及库存/////////////////////////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_JY_Insert_JTS @JYID=''' + trim(FMJID) + '''');
      execsql;
    end;
////////////////////////净重、出入库及库存/////////////////////////////////////

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

procedure TfrmJTSJY220318.Button2Click(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(CPInfo.Text)='' then
  begin
    Application.MessageBox('没有选择品种!','提示',0);
    exit;
  end;
  if Trim(Trim(ZGH.Text)+Trim(ZGL.Text))='' then
  begin
    Application.MessageBox('没有选择纸管颜色!','提示',0);
    exit;
  end;
  if Trim(Trim(BZX.Text)+Trim(BZB.Text))='' then
  begin
    Application.MessageBox('没有选择包装方式!','提示',0);
    exit;
  end;
  if Trim(ZGQty.Text)='' then
  begin
    InitJP('ZGQty');
    ZGQty.SetFocus;
    Application.MessageBox('颗数不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(ZGQty.Text,FReal)=False then
  begin
    Application.MessageBox('颗数非法数字!','提示',0);
    Exit;
  end;
  if Trim(BZB.Text)<>'' then
  begin
    if Trim(SJZL.Text)='' then
    begin
      //InitJP('SJZL');
      //SJZL.SetFocus;
      Application.MessageBox('丝架重量不能为空!','提示',0);
      Exit;
    end;
    if TryStrToFloat(SJZL.Text,FReal)=False then
    begin
      Application.MessageBox('丝架重量非法数字!','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要保存数据吗？数据保存后不能修改!','',32+4)<>IDYES then Exit;
  if SaveData('定重') then
  begin
    SJZL.Text:='';
    SJZL.Hint:='';
  end;
end;

procedure TfrmJTSJY220318.Button3Click(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(CPInfo.Text)='' then
  begin
    Application.MessageBox('没有选择品种!','提示',0);
    exit;
  end;
  if Trim(Trim(ZGH.Text)+Trim(ZGL.Text))='' then
  begin
    Application.MessageBox('没有选择纸管颜色!','提示',0);
    exit;
  end;
  if Trim(Trim(BZX.Text)+Trim(BZB.Text))='' then
  begin
    Application.MessageBox('没有选择包装方式!','提示',0);
    exit;
  end;
  if Trim(ZGQty.Text)='' then
  begin
    InitJP('ZGQty');
    ZGQty.SetFocus;
    Application.MessageBox('颗数不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(ZGQty.Text,FReal)=False then
  begin
    Application.MessageBox('颗数非法数字!','提示',0);
    Exit;
  end;
  if Trim(BZB.Text)<>'' then
  begin
    if Trim(SJZL.Text)='' then
    begin
     // InitJP('SJZL');
      //SJZL.SetFocus;
      Application.MessageBox('丝架重量不能为空!','提示',0);
      Exit;
    end;
    if TryStrToFloat(SJZL.Text,FReal)=False then
    begin
      Application.MessageBox('丝架重量非法数字!','提示',0);
      Exit;
    end;
  end;
  if Trim(SQty.Text)='' then
  begin
    InitJP('SQty');
    SQty.SetFocus;
    Application.MessageBox('手输不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(SQty.Text,FReal)=False then
  begin
    Application.MessageBox('手输非法数字!','提示',0);
    Exit;
  end;

  if Application.MessageBox('确定要保存数据吗？数据保存后不能修改!','',32+4)<>IDYES then Exit;
  if SaveData('手输') then
  begin
    SJZL.Text:='';
    SJZL.Hint:='';
    SQty.Text:='';
    //ZGQty.Text:='';
  end;
end;
procedure TfrmJTSJY220318.InitGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JY_YT where Convert(varchar(10),FillTime,120)=Convert(varchar(10),Getdate(),120)');
    sql.Add(' and Filler='''+Trim(DName)+''' order by FillTime desc ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
end;

procedure TfrmJTSJY220318.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmJTSJY220318.Button4Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      if ShowModal = 1 then
      begin
        Self.SJZL.Text :=FSJZL;
        Self.SJZL.hint := FSJNO;
      end;
    end;
  finally
    frmSCPerson.close;
  end;
end;

end.
