unit U_LaiLiaoJGInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGroupBox;

type
  TfrmLaiLiaoJGInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    Panel3: TPanel;
    Label33: TLabel;
    Label34: TLabel;
    Label5: TLabel;
    Label13: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    OrderNo: TEdit;
    MYCode: TEdit;
    KHName: TEdit;
    CRTime: TDateTimePicker;
    MYName: TEdit;
    PS: TEdit;
    KgQty: TEdit;
    Qty: TEdit;
    Label6: TLabel;
    Note: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column17: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGroupBox1: TcxGroupBox;
    QtyLR: TEdit;
    Label7: TLabel;
    UnitM: TEdit;
    Label9: TLabel;
    UnitKg: TEdit;
    Label10: TLabel;
    Label8: TLabel;
    Label11: TLabel;
    Edit1: TEdit;
    Label12: TLabel;
    Edit2: TEdit;
    Label14: TLabel;
    Edit3: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label17: TLabel;
    Label18: TLabel;
    Edit4: TEdit;
    Edit5: TEdit;
    Label19: TLabel;
    Edit6: TEdit;
    Button1: TButton;
    Label20: TLabel;
    Edit7: TEdit;
    Label21: TLabel;
    Edit8: TEdit;
    v1Column5: TcxGridDBColumn;
    Label22: TLabel;
    Edit9: TEdit;
    v1Column6: TcxGridDBColumn;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure OrderNoExit(Sender: TObject);
    procedure ShuiXiSuoLvClick(Sender: TObject);
    procedure ShuiXiSuoLvJGMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CaiYongBiaoZhunBtnClick(Sender: TObject);
    procedure UnitMClick(Sender: TObject);
    procedure UnitKgClick(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    FRead:String;
    { Public declarations }
  end;

var
  frmLaiLiaoJGInPut: TfrmLaiLiaoJGInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_GYSList;

{$R *.dfm}

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

procedure TfrmLaiLiaoJGInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * ');
    sql.Add(' from Test_Report   ');
    sql.Add(' where TRId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel3,2);
  SCSHDataNew(ADOQuery1,Panel1,2);
  if PState=0 then
  begin
    TRDate.Date:=SGetServerDate(ADOTemp);
  end;
end;


procedure TfrmLaiLiaoJGInPut.FormShow(Sender: TObject);
begin
  InitData();
  if TBSave.Visible=False then
  begin
    FactoryName.Enabled:=False;
    GDPerson.Enabled:=False;
    CaiYongBiaoZhun.Enabled:=False;
  end;
end;

function TfrmLaiLiaoJGInPut.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNoHZ(ADOCmd,maxno,'TR','Test_Report',3,1,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Test_Report where TRId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('TRId').Value:=Trim(maxno);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      if Trim(FRead)<>'' then
      begin
        FieldByName('CeShiPerson').Value:=Trim(DName);
        FieldByName('CeShitime').Value:=SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd,'Test_Report',Panel3,2);
      RTSetsavedata(ADOCmd,'Test_Report',Panel1,2);
      Post;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Test_Report where OrderNo='''+Trim(OrderNo.Text)+'''');
      sql.Add(' and FactoryName='''+Trim(FactoryName.Text)+'''');
      open;
    end;
    if ADOTemp.RecordCount>1 then
    begin

      if Application.MessageBox('此订单号此工厂已登记!确定要继续录入吗？','提示',32+4)<>IDYES then
      begin
        ADOCmd.Connection.RollbackTrans;
        Exit;
      end;

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

procedure TfrmLaiLiaoJGInPut.TBSaveClick(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(OrderNo.Text)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('此订单号不存在！','提示',0);
    Exit;
  end;
  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if Trim(CPCode.Text)='' then
  begin
    Application.MessageBox('编号不能为空!','提示',0);
    Exit;
  end;
  if Trim(FactoryName.Text)='' then
  begin
    Application.MessageBox('工厂不能为空!','提示',0);
    Exit;
  end;
  if Trim(GDPerson.Text)='' then
  begin
    Application.MessageBox('跟单不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmLaiLiaoJGInPut.OrderNoExit(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.MPRTCodeName,A.MPRTCode,A.MPRTMF,A.MPRTKZ,A.CustomerNoName,A.MPRTGY'  );
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where OrderNo='''+Trim(OrderNo.Text)+'''');
    Open;
  end;
  CPCode.Text:=Trim(ADOTemp.fieldbyname('MPRTCode').AsString)+Trim(ADOTemp.fieldbyname('MPRTGY').AsString);
  KHName.Text:=Trim(ADOTemp.fieldbyname('CustomerNoName').AsString);
end;

procedure TfrmLaiLiaoJGInPut.ShuiXiSuoLvClick(Sender: TObject);
begin
  if Trim(TEdit(Sender).Text)='' then
  begin
    TEdit(Sender).Text:='√';
  end else
  begin
    TEdit(Sender).Text:='';
  end;
end;

procedure TfrmLaiLiaoJGInPut.ShuiXiSuoLvJGMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Trim(FRead)='' then
  begin
    TEdit(Sender).ReadOnly:=True;
  end else
  begin
    TEdit(Sender).ReadOnly:=False;
  end;
end;

procedure TfrmLaiLiaoJGInPut.CaiYongBiaoZhunBtnClick(
  Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CaiYongBiaoZhun';
      flagname:='采用标准及方法';
      if ShowModal=1 then
      begin
        CaiYongBiaoZhun.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmLaiLiaoJGInPut.UnitMClick(Sender: TObject);
begin
  if Trim(UnitM.Text)='' then
  begin
    UnitM.Text:='√';
    UnitKg.Text:='';
  end else
  begin
    UnitM.Text:='';
    UnitKg.Text:='√';
  end;
end;

procedure TfrmLaiLiaoJGInPut.UnitKgClick(Sender: TObject);
begin
  if Trim(UnitKg.Text)='' then
  begin
    UnitKg.Text:='√';
    UnitM.Text:='';
  end else
  begin
    UnitKg.Text:='';
    UnitM.Text:='√';
  end;
end;

end.
