unit U_ClothGMInPut;

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, cxDropDownEdit,
  StrUtils, cxPC, RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,
  cxCheckBox;

type
  TfrmClothGMInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    Panel2: TPanel;
    ADOQueryCmd: TADOQuery;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridLevel4: TcxGridLevel;
    Panel1: TPanel;
    Label39: TLabel;
    Label40: TLabel;
    Label41: TLabel;
    Label42: TLabel;
    www: TLabel;
    Edit12: TEdit;
    Edit7: TEdit;
    Label43: TLabel;
    ClothType: TComboBox;
    BTPrint: TButton;
    MJMaoZ: TEdit;
    v6Column1: TcxGridDBColumn;
    v6Column2: TcxGridDBColumn;
    v6Column3: TcxGridDBColumn;
    v6Column4: TcxGridDBColumn;
    v6Column5: TcxGridDBColumn;
    DataSource1: TDataSource;
    CDS_MJID: TClientDataSet;
    Label44: TLabel;
    Label36: TLabel;
    JXPerson: TBtnEditA;
    Label37: TLabel;
    SCPerson: TEdit;
    Button3: TButton;
    Label38: TLabel;
    CarNo: TEdit;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    CDS_Print: TClientDataSet;
    Button1: TButton;
    v6Column6: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label9: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label22: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label26: TLabel;
    Label31: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label35: TLabel;
    Label45: TLabel;
    Label46: TLabel;
    ConNo: TEdit;
    QDTime: TDateTimePicker;
    C_CodeName: TcxButtonEdit;
    KZQty: TEdit;
    MFQty: TEdit;
    DeliveryDate: TDateTimePicker;
    C_Color: TEdit;
    PiKgQty: TEdit;
    PiQty: TEdit;
    C_Qty: TEdit;
    C_Code: TEdit;
    C_Spec: TEdit;
    PiKgQtyFD: TEdit;
    Label7: TLabel;
    Label11: TLabel;
    WFJHPS: TEdit;
    WFJHQty: TEdit;
    WFGYSName: TEdit;
    Label6: TLabel;
    Label10: TLabel;
    WFWCPSZP: TEdit;
    WFWCQtyZP: TEdit;
    Label14: TLabel;
    Label15: TLabel;
    WFWCPSCP: TEdit;
    WFWCQtyCP: TEdit;
    Label17: TLabel;
    Label16: TLabel;
    BatchNoHZ: TEdit;
    v6Column7: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure Edit12Click(Sender: TObject);
    procedure MJMaoZKeyPress(Sender: TObject; var Key: Char);
    procedure Tv6DblClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure JXPersonBtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FXS: Integer;
    procedure InitData();
    function SaveData(): Boolean;
    procedure PrintData(FFFMJID: string);
    procedure SavedataCK();
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FMainId, FConNo, PType, FSubId, PZFlag, FJGID, FAPID: string;
    { Public declarations }
  end;

var
  frmClothGMInPut: TfrmClothGMInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_SCPerson, U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmClothGMInPut.InitData();
var
  PBNO, PBFlag, fsj: string;
begin
  if PState = 1 then
  begin
    ConNo.ReadOnly := True;
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.ConNo,A.QDTime,A.DeliveryDate,A.MainId,B.C_CodeName,B.C_Code,B.C_Color ');
    sql.Add(',B.MFQty,B.KZQty,B.PiKgQty,B.PiQty,B.C_Qty,B.SubId,B.PiKgQtyFD,B.C_Spec,B.BatchNoHZ,C.JGID');
    sql.Add(',C.JHJiaoQi,C.WFGYSName,C.WFGYSNameJC,C.WFJHPS,C.WFJHQty,C.WFWCPS,C.WFWCQty,C.WFWCPSZP,C.WFWCQtyZP,C.WFWCPSCP,C.WFWCQtyCP');
    sql.Add(' from Cloth_Main A inner join CLoth_Sub B on A.Mainid=B.MainId');
    sql.Add(' inner join Cloth_WJG  C on C.MainId=A.MainId ');
    sql.Add(' where C.JGID=''' + Trim(FJGID) + '''');
    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 0);
  SCSHData(ADOQuery1, ScrollBox1, 2);
  fsj := 'select Name=ZdyName from KH_Zdy where Type=''ClothType'' ';
  SInitComBoxBySql(ADOQueryTemp, ClothType, True, fsj);
  ClothType.ItemIndex := -1;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY_PB where JGID=''' + Trim(FJGID) + ''' and JTType=''挂码'' ');
    open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MJID);
  SInitCDSData20(ADOQueryTemp, CDS_MJID);
end;

procedure TfrmClothGMInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

procedure TfrmClothGMInPut.BTPrintClick(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(ClothType.Text) = '' then
  begin
    Application.MessageBox('没有选择等级!', '提示', 0);
    Exit;
  end;
  if Trim(MJMaoZ.Text) <> '' then
  begin
    if TryStrToFloat(MJMaoZ.Text, mvalue) = False then
    begin
      Application.MessageBox('重量录入错误!', '提示', 0);
      Exit;
    end;
  end;
  {if StrToFloat(MJMaoZ.Text)>50 then
  begin
    Application.MessageBox('毛重录入错误!','提示',0);
    Exit;
  end;   }
  if StrToFloat(MJMaoZ.Text) < 10 then
  begin
    if Application.MessageBox('毛重小于10确定要保存数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;
  if SaveData() then
  begin
    MJMaoZ.SetFocus;
  end;
end;

procedure TfrmClothGMInPut.SavedataCK();
var
  CRID: Integer;
  MaxCkNo: string;
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    SQL.add('Update CK_PBCP_CRID Set CRID=CRID+1');
    sql.Add('select * from CK_PBCP_CRID');
    Open;
  end;
  CRID := ADOCmd.fieldbyname('CRID').Value;
  if GetLSNo(ADOCmd, MaxCkNo, 'JR', 'CK_PBCP_CR', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取坯布仓库最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_CR where 1<>1');
    Open;
  end;
  with ADOCmd do
  begin
    Append;
    FieldByName('MainId').Value := Trim(FMainId);
    FieldByName('SubId').Value := Trim(FSubId);
    FieldByName('APID').Value := Trim(FAPID);
    FieldByName('JGID').Value := Trim(FJGID);
    FieldByName('MJID').Value := Trim(FAPID);
    FieldByName('BCID').Value := Trim(MaxCkNo);
    FieldByName('CRTime').Value := SGetServerDate(ADOTemp);
    FieldByName('CRFlag').Value := '入库';
    FieldByName('CRType').Value := '检验入库';
    FieldByName('CRID').Value := CRID;
    FieldByName('KGQty').Value := StrToFloat(MJMaoZ.Text);
    FieldByName('CPType').Value := ClothType.Text;
    FieldByName('Filler').Value := Trim(DName);
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_KC where 1<>1');
    Open;
  end;
  with ADOCmd do
  begin
    Append;
    FieldByName('CRID').Value := CRID;
    FieldByName('BCID').Value := Trim(MaxCkNo);
    FieldByName('MJID').Value := Trim(FAPID);
    FieldByName('JGID').Value := Trim(FJGID);
    FieldByName('KCKGQty').Value := StrToFloat(MJMaoZ.Text);
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update WFB_MJJY_PB Set MJStr2=''已入库'' where MJID=''' + Trim(FAPID) + '''');
    ExecSQL;
  end;

end;

function TfrmClothGMInPut.SaveData(): Boolean;
var
  maxno: string;
begin
  Result := False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if GetLSNo(ADOCmd, maxno, '8', 'WFB_MJJY_PB', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取坯布流水号失败!', '提示', 0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY_PB where MJId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('MainId').value := Trim(FMainId);
      FieldByName('SubId').value := Trim(FSubId);
      FieldByName('JGId').value := Trim(FJGId);
      FieldByName('APId').value := Trim(maxno);
      FieldByName('MJID').Value := Trim(maxno);
      FieldByName('MJStr2').Value := '未入库';
      if Trim(MJMaoZ.Text) <> '' then
      begin
        FieldByName('MJMaoZ').Value := StrToFloat(MJMaoZ.Text);
        FieldByName('MJJingZ').Value := StrToFloat(MJMaoZ.Text);
        FieldByName('MJPiZ').Value := 0;
      end;
      if Trim(Edit12.Text) <> '' then
      begin
        FieldByName('MJStr3').Value := '深色';
      end;
      if Trim(Edit7.Text) <> '' then
      begin
        FieldByName('MJStr3').Value := '漂白';
      end;
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('ClothType').Value := Trim(clothType.Text);
      FieldByName('ChuLiFlag').Value := 1;
      FieldByName('JTType').Value := '挂码';
      FieldByName('JXPerson').Value := Trim(JXPerson.Text);
      FieldByName('SCPerson').Value := Trim(SCPerson.Text);
      FieldByName('CarNo').Value := Trim(CarNo.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate WFB_MJJY_PB Set MJXH=(select isnull(max(MJXH),0)+1 from  WFB_MJJY_PB A where A.MainID=WFB_MJJY_PB.MainID)');
      sql.Add(' where MJID=''' + Trim(maxno) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate WFB_MJJY_PB Set APXH=MJXH ');
      sql.Add(' where MJID=''' + Trim(maxno) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY_PB Set CheJian=(select Top 1 Note from KH_ZDY A ');
      sql.Add(' where A.ZDYName=WFB_MJJY_PB.CarNo and A.Type=''CarNo'')');
      sql.Add(' where MJID=''' + Trim(maxno) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Cloth_WJG_Qty :JGID');
      Parameters.ParamByName('JGID').Value := Trim(FJGID);
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Cloth_Sub_WJG_Qty :MainId');
      Parameters.ParamByName('MainId').Value := Trim(FMainId);
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(maxno) + '''');
      Open;
    end;
    with CDS_MJID do
    begin
      Append;
      FieldByName('MJID').Value := ADOQueryTemp.fieldbyname('MJID').Value;
      FieldByName('MJXH').Value := ADOQueryTemp.fieldbyname('MJXH').Value;
      FieldByName('MJMaoZ').Value := ADOQueryTemp.fieldbyname('MJMaoZ').Value;
      FieldByName('ClothType').Value := ADOQueryTemp.fieldbyname('ClothType').Value;
      FieldByName('MJStr3').Value := ADOQueryTemp.fieldbyname('MJStr3').Value;
      FieldByName('CarNo').Value := ADOQueryTemp.fieldbyname('CarNo').Value;
      Post;
    end;

    FAPID := Trim(maxno);
//    SavedataCK();
    //保存出库
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PBJYCRK  @MJID=' + quotedstr(trim(Trim(FAPID))));
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
    if CheckBox1.Checked = True then
    begin
      PrintData(Trim(maxno));
    end;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmClothGMInPut.PrintData(FFFMJID: string);
var
  fPrintFile: string;
  i: Integer;
  FDate: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  ExportFtErpFile('坯布标签BP.rmf', ADOQueryTemp);
  ExportFtErpFile('坯布标签BP.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布标签BP.rmf';
  if FileExists(fPrintFile) then
  begin
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(FFFMJID);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Print_PBLab :MJID');
      Parameters.ParamByName('MJID').Value := Trim(FFFMJID);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_Print);
    SInitCDSData20(ADOQueryPrint, CDS_Print);
    RM2.LoadFromFile(fPrintFile);
    RMVariables['QRBARCODE'] := fImagePath;
    //RM2.ShowReport;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\坯布标签BP.rmf'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmClothGMInPut.Edit7Click(Sender: TObject);
begin
  if Trim(Edit7.Text) = '' then
  begin
    //Edit10.Text:='';
    Edit12.Text := '';
    Edit7.Text := '√';
  end
  else
  begin
    //Edit10.Text:='';
    Edit12.Text := '';
    Edit7.Text := '';
  end;
end;

procedure TfrmClothGMInPut.Edit12Click(Sender: TObject);
begin
  if Trim(Edit12.Text) = '' then
  begin
    Edit12.Text := '√';
    Edit7.Text := '';
  end
  else
  begin
    Edit12.Text := '';
    Edit7.Text := '';
  end;
end;

procedure TfrmClothGMInPut.MJMaoZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Trim(MJMaoZ.Text) = '' then
    begin
      Application.MessageBox('重量不能为空!', '提示', 0);
      Exit;
    end;
    BTPrint.Click;
    MJMaoZ.Text := '';
  end;
end;

procedure TfrmClothGMInPut.Tv6DblClick(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_CR where CRFlag=''出库'' and MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已出库不能删除数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete WFB_MJJY_PB where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    sql.Add('delete CK_PBCP_CR where  MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    sql.Add('delete CK_PBCP_KC where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    ExecSQL;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Update_Cloth_WJG_Qty :JGID');
    Parameters.ParamByName('JGID').Value := Trim(FJGID);
    ExecSQL;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Update_Cloth_Sub_WJG_Qty :MainId');
    Parameters.ParamByName('MainId').Value := Trim(FMainId);
    ExecSQL;
  end;
  CDS_MJID.Delete;
end;

procedure TfrmClothGMInPut.Button3Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        SCPerson.Text := frmSCPerson.FSDPerson;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

procedure TfrmClothGMInPut.JXPersonBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JXPerson';
      flagname := '机修';
      if ShowModal = 1 then
      begin
        JXPerson.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothGMInPut.Button1Click(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then
    Exit;
  if CDS_MJID.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要重打标签吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  CDS_MJID.DisableControls;
  with CDS_MJID do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      PrintData(Trim(CDS_MJID.fieldbyname('MJID').AsString));
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_MJID.EnableControls;
end;

end.

