unit U_YarnWeighing;

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, RM_Dataset,
  RM_System, RM_Common, RM_Class, RM_GridReport, IniFiles, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, cxContainer, cxTextEdit, cxMaskEdit,
  cxButtonEdit,   dxDateRanges, U_BaseList, dxSkinsCore, dxSkinsDefaultPainters,
  dxScrollbarAnnotations, cxProgressBar;

type
  TfrmYarnWeighing = class(TfrmBaseList)
    CPInfo: TRichEdit;
    ADOQueryTemp: TADOQuery;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel2: TPanel;
    SQty: TEdit;
    Label4: TLabel;
    Button3: TButton;
    Panel10: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v3Column4: TcxGridDBColumn;
    ADOCmd: TADOQuery;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    ClientDataSetYS: TClientDataSet;
    Tv3Column3: TcxGridDBColumn;
    Button2: TButton;
    btnSampleSel: TButton;
    Button1: TButton;
    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;
    procedure FormDestroy(Sender: TObject);
    procedure btnSampleSelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FMainid, FY_Code, FY_Name, FY_Spec, FY_Composition, FY_Color, FNote: string;
    FFFMJID: string;
    IsCommopen: boolean;
    procedure InitJP(FName: string);   //刷新数字键盘
    function SaveData(DataType: string): Boolean;    //保存数据
    procedure InitGrid();  //刷新检验数据
    function TSInfo(MOKNOFlag, MTSStr: string): Boolean;  //提示用子窗体
    procedure PrtData(MYIID: string);   //打印标签
    procedure OpenCom(DllName: string);
    procedure CloseCom(DllName: string);
    procedure On1201(var Message: Tmessage); message 1201;   //电子称
  public
    { Public declarations }
  end;

var
  frmYarnWeighing: TfrmYarnWeighing;
  newh, newh1: hwnd;
  IniFile: TIniFile;

implementation

uses
  U_DataLink, U_iniParam, U_RTFun, U_TSOKNO, U_ZDYHelp, U_CPSelBig;

{$R *.dfm}

procedure TfrmYarnWeighing.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnWeighing := nil;
end;

procedure TfrmYarnWeighing.On1201(var Message: Tmessage);
var
  i1, i2: integer;
  FMJMaoZ: Double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;
  FMJMaoZ := RoundFloat(i1 * i2 / 100000, 2);
  SQty.Text := floattostr(FMJMaoZ);
end;

procedure TfrmYarnWeighing.btnSampleSelClick(Sender: TObject);  //选择品种
begin
  try
    frmCPSelBig := TfrmCPSelBig.Create(Self);
    with frmCPSelBig do
    begin
      if ShowModal = 1 then
      begin
        CPInfo.Hint := FHMainId;
      end;
    end;
  finally
    frmCPSelBig.Close;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Yarn_Info where BYIID=''' + Trim(CPInfo.Hint) + '''');
    Open;
  end;
  with ADOQueryTemp do
  begin
    FMainid := Trim(fieldbyname('BYIID').AsString);
    FY_Code := Trim(fieldbyname('Y_Code').AsString);
    FY_Name := Trim(fieldbyname('Y_Name').AsString);
    FY_Spec := Trim(fieldbyname('Y_Spec').AsString);
    FY_Composition := Trim(fieldbyname('Y_Composition').AsString);
    FY_Color := Trim(fieldbyname('Y_Color').AsString);
    FNote := Trim(fieldbyname('Note').AsString);
  end;
  ADOQueryTemp.Close;

  CPInfo.Text := '  ' + #13 + '编号：' + Trim(FY_Code) + #13 + #13;
  CPInfo.Text := CPInfo.Text + '品名：' + Trim(FY_Name) + #13 + #13;
  CPInfo.Text := CPInfo.Text + '规格：' + Trim(FY_Spec) + #13 + #13;
  CPInfo.Text := CPInfo.Text + '成分：' + Trim(FY_Composition) + #13 + #13;
  CPInfo.Text := CPInfo.Text + '颜色：' + Trim(FY_Color) + #13 + #13;
  CPInfo.Text := CPInfo.Text + '备注：' + Trim(FNote) + #13 + #13;

end;

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

procedure TfrmYarnWeighing.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  CloseCom(DZCDYDllName);
  Action := caFree;
end;

procedure TfrmYarnWeighing.Button1Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '1', Tv3, '纱线称重');
  Close;
end;

procedure TfrmYarnWeighing.Button2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if TSInfo('选择', '确定要重打标签吗？') then
  begin
    PrtData(Trim(CDS_1.fieldbyname('YIID').AsString));
  end;
end;

procedure TfrmYarnWeighing.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;
  TEdit(FindComponent(FName)).SetFocus;
end;

procedure TfrmYarnWeighing.SpeedButton1Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TSpeedButton(Sender).Hint);
  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 TfrmYarnWeighing.SpeedButton12Click(Sender: TObject);
var
  fsj: string;
begin
  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 TfrmYarnWeighing.SaveData(DataType: string): Boolean;
var
  Maxno, MBYIOID: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    CDS_1.DisableControls;

    if not GetLSNo(ADOCmd, Maxno, 'PR', 'BS_Yarn_IO', 3, 1) then
      raise Exception.Create('取入库编号失败！');

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(Maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin

      Append;
      FieldByName('Fillid').Value := Trim(Dcode);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('IOTime').Value := SGetServerDate(ADOQueryTemp);
      FieldByName('Y_Code').Value := FY_Code;
      FieldByName('Y_Name').Value := FY_Name;
      FieldByName('Y_Spec').Value := FY_Spec;
      FieldByName('Y_Composition').Value := FY_Composition;
      FieldByName('Y_Color').Value := FY_Color;
      FieldByName('STKName').Value := '合股丝';
      FieldByName('BYIOID').Value := Trim(Maxno);
      FieldByName('STKID').Value := Trim(Maxno);
      FFFMJID := Maxno;
      FieldByName('IOFlag').Value := '入库';
      FieldByName('IOQtyFlag').Value := 1;
      FieldByName('IOType').Value := '合股完成';
      FieldByName('Qty').Value := trim(SQty.Text);
      FieldByName('QtyUnit').Value := 'Kg';
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update BS_Yarn_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID)');
      sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID )');
      sql.Add('where BYIOID=''' + Trim(Maxno) + '''');
      ExecSQL;
    end;

    CDS_1.EnableControls;
    ADOCmd.Connection.CommitTrans;

    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnWeighing.PrtData(MYIID: string);
var
  fPrintFile: string;
begin
  ExportFtErpFile('合股纱线标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\合股纱线标签.rmf';

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_Yarn_Prt_Lab  ''' + Trim(MYIID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies := 1;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmYarnWeighing.Button3Click(Sender: TObject);
begin
  if TSInfo('选择', '确定要打印数据吗？数据打印后数据将不能修改！') then
  begin
    if SaveData('手输') then
    begin
      PrtData(Trim(FFFMJID));
      SQty.Text := '';
      InitGrid();
    end;
  end;
end;

function TfrmYarnWeighing.TSInfo(MOKNOFlag, MTSStr: string): Boolean;
begin
  try
    frmTSOKNO := TfrmTSOKNO.Create(Application);
    with frmTSOKNO do
    begin
      frmTSOKNO.TSStr := MTSStr;
      frmTSOKNO.OKNOFlag := MOKNOFlag;
      if ShowModal = 1 then
      begin
        Result := retbol;
      end;
    end;
  finally
    frmTSOKNO.Free;
  end;
end;

procedure TfrmYarnWeighing.InitGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Yarn_IO where Convert(varchar(10),FillTime,120)=Convert(varchar(10),Getdate(),120)');
    sql.Add(' and Filler=''' + Trim(DName) + ''' order by FillTime desc ');
//    showmessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
end;

procedure TfrmYarnWeighing.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Self.Caption, Tv3, '纱线称重');
  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'FileKPZ.INI');
    DZCDYDllName := IniFile.ReadString('生产车间配置', '电子秤调用Dll文件', '');
  finally
    IniFile.Free;
  end;
  InitGrid();
  if DZCDYDllName <> '' then
  begin
    OpenCom(DZCDYDllName);
  end;
end;

procedure TfrmYarnWeighing.OpenCom(DllName: string);
type
  TMyFunc = function(fhandle: hwnd; sCommName: PAnsiChar; IntTime: integer; IsMessage: integer): HWND; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
begin
 //  closeCom(FComFile);
  Th := LoadLibrary(Pchar(trim(DllName)));
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'CommOpen');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh := Tf(self.Handle, 'Comm1', 500, 1);
        if newh < 1 then
        begin
          application.MessageBox(Pchar('打开串口失败！'), '提示');
        end
        else
          IsCommOpen := true;
      end
      else
      begin
        IsCommOpen := false;
      end;
    finally
    // FreeLibrary(Th);
    end;
  end
  else
  begin
    IsCommOpen := false;
    application.MessageBox(Pchar('找不到 ' + trim(DllName) + ' 文件！'), '提示');
  end;
end;

procedure TfrmYarnWeighing.CloseCom(DllName: string);
type
  TMyFunc = function(sCommName: PAnsiChar): HWND; stdcall;
var
  Tf1: TMyFunc;
  Tp1: TFarProc;
  Th1: Thandle;
begin
  Th1 := LoadLibrary(Pchar(trim(DllName)));
  if Th1 > 0 then
  begin
    try
      Tp1 := GetProcAddress(Th1, 'CommClose');
      if Tp1 <> nil then
      begin
        Tf1 := TMyFunc(Tp1);
        newh1 := Tf1('Comm1');
      end
      else
      begin

      end;
    finally
    // FreeLibrary(Th1);
    end;
  end
  else
  begin
    application.MessageBox(Pchar('找不到 ' + trim(DllName) + ' 文件！'), '提示');
  end;
end;

end.

