unit U_LLRKInPutMX;

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

type
  TfrmLLRKInPutMX = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    Panel3: TPanel;
    Label13: TLabel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    MYName: TEdit;
    Label6: TLabel;
    Note: TEdit;
    cxGroupBox1: TcxGroupBox;
    QtyLRM: TEdit;
    Label7: TLabel;
    UnitM: TEdit;
    Label9: TLabel;
    UnitKg: TEdit;
    Label10: TLabel;
    Label11: TLabel;
    MYColor: TEdit;
    Label12: TLabel;
    MYKZ: TEdit;
    Label14: TLabel;
    MYMF: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    Label22: TLabel;
    RCGangNo: TEdit;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    QtyLRKg: TEdit;
    Label2: TLabel;
    MYColorNo: TEdit;
    Label3: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label4: TLabel;
    GangQty: TEdit;
    Label8: TLabel;
    MXHZPS: TEdit;
    Label17: TLabel;
    MXHZKGQty: TEdit;
    Label18: TLabel;
    MYType: TBtnEditA;
    BtnDel: TButton;
    v1Column4: TcxGridDBColumn;
    Panel4: TPanel;
    Label23: TLabel;
    Label24: TLabel;
    KgQty1: TEdit;
    MQty1: TEdit;
    Button6: TButton;
    Label20: TLabel;
    RCGangNO1: TEdit;
    MLID: TEdit;
    BPID: TEdit;
    DYID: TEdit;
    PTID: TEdit;
    KgQtyAvg: TEdit;
    Label25: TLabel;
    QtyAvg: TEdit;
    Label5: TLabel;
    Label21: TLabel;
    Label26: TLabel;
    Label19: TLabel;
    labMYType: TLabel;
    MXHZQty: TEdit;
    v1Column7: TcxGridDBColumn;
    RM1: TRMGridReport;
    CDS_Print: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    Button1: TButton;
    BtnM: TButton;
    BtnKg: TButton;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox2: TComboBox;
    Label29: TLabel;
    ComboBox1: TComboBox;
    BtnPrt: TButton;
    BtnQX: TButton;
    Label30: TLabel;
    Button3: TButton;
    Panel6: TPanel;
    Label27: TLabel;
    KHName: TBtnEditA;
    Label31: TLabel;
    Edit1: TEdit;
    Label32: TLabel;
    Edit2: TEdit;
    Label33: TLabel;
    Edit3: TEdit;
    Label34: TLabel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ShuiXiSuoLvClick(Sender: TObject);
    procedure ShuiXiSuoLvJGMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure UnitMClick(Sender: TObject);
    procedure UnitKgClick(Sender: TObject);
    procedure QtyLRMKeyPress(Sender: TObject; var Key: Char);
    procedure QtyLRKgKeyPress(Sender: TObject; var Key: Char);
    procedure BtnQXClick(Sender: TObject);
    procedure MYNameKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button6Click(Sender: TObject);
    procedure BtnDelClick(Sender: TObject);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnPrtClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure BtnMClick(Sender: TObject);
    procedure BtnKgClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Panel6DblClick(Sender: TObject);
  private
    procedure InitData();
    procedure InitMXData();
    function  SaveData():Boolean;
    procedure GetHZData();
    procedure InitColor();
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    FRead:String;
    FSaveFlag:string;
    { Public declarations }
  end;

var
  frmLLRKInPutMX: TfrmLLRKInPutMX;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_GYSList,U_SCPerson,U_QCRKJLList,U_RSColorBig;

{$R *.dfm}

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

procedure TfrmLLRKInPutMX.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * ');
    sql.Add(' from CK_MYSC_CR   ');
    sql.Add(' where MYID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel3,2);
  SCSHDataNew(ADOQuery1,Panel2,2);

  labMYType.Caption:=Trim(ADOQuery1.fieldbyname('MYType').AsString);
  if Trim(labMYType.Caption)='' then
  begin
    labMYType.Caption:='        ';
  end;
  if PState=0 then
  begin
    CRTime.Date:=SGetServerDate(ADOTemp);
  end;
  InitMXData();
  GetHZData();
  Edit1.SetFocus;
  if Trim(FMainId)<>'' then
  begin
    InitColor();
  end;
end;
procedure TfrmLLRKInPutMX.InitMXData();
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,Laster=isnull(Editer,Filler) from CK_MYSC_CR_MX A where isnull(MYID,'''')='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet1);
  SInitCDSData20(ADOTemp,ClientDataSet1);
end;
procedure TfrmLLRKInPutMX.GetHZData();
var
  FKG,FM,FPS:String;
begin
  FKG:=Tv1.DataController.Summary.FooterSummaryTexts[0];
  FM:=Tv1.DataController.Summary.FooterSummaryTexts[1];
  FPS:=Tv1.DataController.Summary.FooterSummaryTexts[2];
  MXHZPS.Text:=FPS;
  MXHZKGQty.Text:=FKG;
  MXHZQty.Text:=FM;
  if (Trim(FPS)='') or (Trim(FPS)='0')  then
  begin
    SClearData(Panel2,2);
    SClearData(Panel2,99);
  end else
  begin
    with ADOTemp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Avg_JS :KgQty,:MQty,:PS');
      if Trim(FKG)<>'' then
      begin
        Parameters.ParamByName('KGQty').Value:=FKG;
      end else
      begin
        Parameters.ParamByName('KGQty').Value:=Null;
      end;
      if Trim(FM)<>'' then
      begin
        Parameters.ParamByName('MQty').Value:=FM;
      end else
      begin
        Parameters.ParamByName('MQty').Value:=Null;
      end;
      Parameters.ParamByName('PS').Value:=FPS;
      Open;
    end;
    KgQtyAvg.Text:=ADOTemp.fieldbyname('KgQtyAvg').Value;
    QtyAvg.Text:=ADOTemp.fieldbyname('QtyAvg').Value;
    
  end;
end;

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

function TfrmLLRKInPutMX.SaveData():Boolean;
var
  maxno,maxmxno,maxnoflag,fsj,FMXID:String;
begin
  Result:=False;
  if Trim(MYType.TxtCode)='' then
  begin
    Application.MessageBox('没有定义产品类型的编码标记!','提示',0);
    Exit;
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'QC','CK_MYSC_CR',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      maxno:=Trim(maxno)+Trim(MYType.TxtCode);
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CK_MYSC_CR where MYId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MYId').Value:=Trim(maxno);
      FieldByName('CRType').Value:='期初入库';
      FieldByName('CRFlag').Value:='入库';
      FieldByName('CRQtyFlag').Value:=1;
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditerCode').Value:=Trim(DCode);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd,'CK_MYSC_CR',Panel3,2);

      Post;
    end;
    FMainId:=Trim(maxno);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate CK_MYSC_CR Set KHDayCS=isnull((select isnull(max(KHDayCS),0) from CK_MYSC_CR A ');
      sql.Add(' where A.CRTime=CK_MYSC_CR.CRTime and isnull(A.KHName,'''')=isnull(CK_MYSC_CR.KHName,'''')),0)+1');
      SQL.Add('where MYID='''+Trim(maxno)+''' and isnull(KHName,'''')<>'''' ');
      ExecSQL;
    end;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
          Open;
        end;
        FMXID:=Trim(ADOCmd.fieldbyname('MXID').AsString);
        if Trim(FMXID)='' then
        begin
          if GetLSNo(ADOCmd,maxmxno,'QM','CK_MYSC_CR_MX',4,1)=False then
          begin
            ClientDataSet1.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取期初入库数据最大号失败','提示',0);
            Exit;
          end;
          maxmxno:=maxmxno+Trim(MYType.TxtCode);
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(maxmxno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            Append;
            FieldByName('MYID').Value:=Trim(maxno);
            FieldByName('MXID').Value:=Trim(maxmxno);
            FieldByName('RCGangNo').Value:=ClientDataSet1.fieldbyname('RCGangNo').Value;
            FieldByName('KgQty').Value:=ClientDataSet1.fieldbyname('KgQty').Value;
            FieldByName('MQty').Value:=ClientDataSet1.fieldbyname('MQty').Value;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
            Post;
          end;
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' exec P_Update_QCRK_GangNo :MYID');
            Parameters.ParamByName('MYID').Value:=Trim(maxno);
            ExecSQL;
          end;
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_MYSC_CR_MX Set JH=isnull((select isnull(max(JH),0) from CK_MYSC_CR_MX A ');
            sql.Add(' where A.MYID='''+Trim(maxno)+''' and isnull(A.RCGangNO,'''')=isnull(CK_MYSC_CR_MX.RCGangNO,'''')');
            sql.Add(' and isnull(A.GangNO,'''')=isnull(CK_MYSC_CR_MX.GangNO,'''')),0)+1 ');
            sql.Add(' where MXID='''+Trim(maxmxno)+'''');
            ExecSQL;
          end;
          Edit;
          FieldByName('MYID').Value:=Trim(maxno);
          FieldByName('MXID').Value:=Trim(maxmxno);
          Post;
        end;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_QCRK_Qty :MYID');
      Parameters.ParamByName('MYID').Value:=Trim(maxno);
      ExecSQL;
    end;

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

procedure TfrmLLRKInPutMX.TBSaveClick(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(MYName.Text)='' then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    GetHZData();
    if Trim(FSaveFlag)<>'99' then
    begin
      Application.MessageBox('保存成功！','提示',0);
    end;
  end;
end;

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

procedure TfrmLLRKInPutMX.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 TfrmLLRKInPutMX.UnitMClick(Sender: TObject);
begin
  if Trim(UnitM.Text)='' then
  begin
    UnitM.Text:='√';
    QtyLRM.Visible:=True;
    BtnM.Visible:=True;
    if Trim(UnitKg.Text)<>'' then
    begin
      BtnM.Visible:=False;
    end;
  end else
  begin
    UnitM.Text:='';
    QtyLRM.Visible:=False;
    BtnM.Visible:=False;
  end;
end;

procedure TfrmLLRKInPutMX.UnitKgClick(Sender: TObject);
begin
  if Trim(UnitKg.Text)='' then
  begin
    UnitKg.Text:='√';
    QtyLRKg.Visible:=True;
    BtnKg.Visible:=True;
    if Trim(UnitM.Text)<>'' then
    begin
      BtnM.Visible:=False;
    end;
  end else
  begin
    UnitKg.Text:='';
    QtyLRKg.Visible:=False;
    BtnKg.Visible:=False;
  end;
end;

procedure TfrmLLRKInPutMX.QtyLRMKeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
begin
  if Key=#13 then
  begin
    if Trim(QtyLRM.Text)='' then Exit;
    if TryStrToFloat(QtyLRM.Text,FReal)=False then
    begin
      Application.MessageBox('非法数字!','提示',0);
      Exit;
    end;
    if Trim(UnitKg.Text)<>'' then
    begin
      QtyLRKg.SetFocus;
      Exit;
    end;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('RCGangNo').Value:=Trim(RCGangNo.Text);
      FieldByName('MQty').Value:=Trim(QtyLRM.Text);
      Post;
    end;
    QtyLRM.Text:='';
    GetHZData();
  end;
end;

procedure TfrmLLRKInPutMX.QtyLRKgKeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
begin
  if Key=#13 then
  begin
    if Trim(QtyLRKg.Text)='' then Exit;
    if Trim(UnitM.Text)<>'' then
    begin
      if Trim(QtyLRM.Text)='' then
      begin
        Application.MessageBox('米数不能为空!','提示',0);
        Exit;
      end;
    end;
    if TryStrToFloat(QtyLRKg.Text,FReal)=False then
    begin
      Application.MessageBox('非法数字!','提示',0);
      Exit;
    end;
    with ClientDataSet1 do
    begin
      Append;
      FieldByName('RCGangNo').Value:=Trim(RCGangNo.Text);
      FieldByName('KgQty').Value:=Trim(QtyLRKg.Text);
      if Trim(UnitM.Text)<>'' then
      begin
        FieldByName('MQty').Value:=Trim(QtyLRM.Text);
      end;
      Post;
    end;
    QtyLRM.Text:='';
    QtyLRKg.Text:='';
    if Trim(UnitM.Text)<>'' then QtyLRM.SetFocus;
    GetHZData();
  end;
end;

procedure TfrmLLRKInPutMX.BtnQXClick(Sender: TObject);
begin
  Panel5.Visible:=False;
end;

procedure TfrmLLRKInPutMX.MYNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(MYType.Text)='' then
    begin
      Application.MessageBox('没有选择产品类型!','提示',0);
      Exit;
    end;
  end;
end;

procedure TfrmLLRKInPutMX.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if BtnDel.Visible=False then Exit;
  Panel4.Visible:=True;
  RCGangNO1.Text:=ClientDataSet1.fieldbyname('RCGangNO').Value;
  if Trim(ClientDataSet1.fieldbyname('KgQty').AsString)<>'' then
    KgQty1.Text:=ClientDataSet1.fieldbyname('KgQty').Value
  else
     KgQty1.Text:='';
  if Trim(ClientDataSet1.fieldbyname('MQty').AsString)<>'' then
    MQty1.Text:=ClientDataSet1.fieldbyname('MQty').Value
  else
     MQty1.Text:='';
end;

procedure TfrmLLRKInPutMX.Button6Click(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(KgQty1.Text)<>'' then
  begin
    if TryStrToFloat(KgQty1.Text,FReal)=False then
    begin
      Application.MessageBox('公斤数非法数字!','提示',0);
      Exit;
    end;
  end;
  if Trim(MQty1.Text)<>'' then
  begin
    if TryStrToFloat(MQty1.Text,FReal)=False then
    begin
      Application.MessageBox('米数非法数字!','提示',0);
      Exit;
    end;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('RCGangNo').Value:=Trim(RCGangNo1.Text);
    if Trim(MQty1.Text)<>'' then
      FieldByName('MQty').Value:=Trim(MQty1.Text)
    else
      FieldByName('MQty').Value:=Null;
    if Trim(KgQty1.Text)<>'' then
      FieldByName('KgQty').Value:=Trim(KgQty1.Text)
    else
      FieldByName('KgQty').Value:=Null;
    FieldByName('Laster').Value:=Trim(DName);
    Post;
  end;
  Panel4.Visible:=False;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then exit;
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      Edit;
      FieldByName('RCGangNo').Value:=ClientDataSet1.fieldbyname('RCGangNo').Value;
      FieldByName('KgQty').Value:=ClientDataSet1.fieldbyname('KgQty').Value;
      FieldByName('MQty').Value:=ClientDataSet1.fieldbyname('MQty').Value;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditerCode').Value:=Trim(DCode);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_QCRK_Qty :MYID');
      Parameters.ParamByName('MYID').Value:=Trim(ClientDataSet1.fieldbyname('MYID').AsString);
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_MYSC_CR where MYID='''+Trim(ClientDataSet1.fieldbyname('MYID').AsString)+'''');
      Open;
    end;
    SCSHDataNew(ADOTemp,Panel2,2);
    GetHZData();
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据更新失败!','提示',0);
    Exit;
  end;

end;

procedure TfrmLLRKInPutMX.BtnDelClick(Sender: TObject);
var
  FFMXID:string;
begin
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      first;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
          Open;
        end;
        FFMXID:=Trim(ADOTemp.fieldbyname('MXID').AsString);
        if Trim(FFMXID)<>'' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' insert into CK_MYSC_CR_MX_Log  ');
            sql.Add(' select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
            sql.Add(' UPdate CK_MYSC_CR_MX_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
            sql.Add(' where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
            sql.Add(' Delete CK_MYSC_CR_MX  where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
            ExecSQL;
          end;
        end;

        ClientDataSet1.Delete;
      end;
    end;
    ClientDataSet1.EnableControls;
    if Trim(FMainId)<>'' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Update_QCRK_Qty :MYID');
        Parameters.ParamByName('MYID').Value:=Trim(FMainId);
        ExecSQL;
      end;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CK_MYSC_CR where MYID='''+Trim(FMainId)+'''');
        Open;
      end;
      SCSHDataNew(ADOTemp,Panel2,2);
      GetHZData();
    end;
    ADOCmd.Connection.CommitTrans;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！','提示',0);
    Exit;
  end;
end;

procedure TfrmLLRKInPutMX.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmQCRKJLList:=TfrmQCRKJLList.Create(Application);
    with frmQCRKJLList do
    begin
      FMXID:=Trim(Self.ClientDataSet1.fieldbyname('MXID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmQCRKJLList.Free;
  end;
end;

procedure TfrmLLRKInPutMX.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;

procedure TfrmLLRKInPutMX.BtnPrtClick(Sender: TObject);
var
   fPrintFile,Txt,fImagePath:string;
   i,j:Integer;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if ClientDataSet1.Locate('GangNo',null,[])=true then
  begin
    Application.MessageBox('没有保存数据, 请先点击保存，再打印标签。','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel',True,[])=false then
  begin
    Application.MessageBox('没有选择数据, 请先选择数据，再打印标签。','提示',0);
    Exit;
  end;
  if ComboBox2.Text='' then
  begin
    Application.MessageBox('请选择张数!','提示',0);
    Exit;
  end;
  if ComboBox1.Text='' then
  begin
    Application.MessageBox('请选择尺寸!','提示',0);
    Exit;
  end;
  if ComboBox1.Text='9*7' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\期初入库染色面料标签97.rmf';
  end else
  if ComboBox1.Text='8*5' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\期初入库染色面料标签85.rmf';
  end;
  if FileExists(fPrintFile)=False then
  begin
    if ComboBox1.Text='9*7' then
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\期初入库染色面料标签97.rmf'),'提示',0);
    if ComboBox1.Text='8*5' then
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\期初入库染色面料标签85.rmf'),'提示',0);
    exit;
  end;
  ClientDataSet1.DisableControls;
  with ClientDataSet1 do
  begin
    First;
    while Locate('SSel',True,[]) do
    begin
      with ADOTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' Exec P_PRT_QCRK_RSML :MXID');
        Parameters.ParamByName('MXID').Value:=Trim(ClientDataSet1.fieldbyname('MXID').AsString);
        Open;
      end;
      SCreateCDS20(ADOTemp,CDS_Print);
      SInitCDSData20(ADOTemp,CDS_Print);
      try
        Moudle:=LoadLibrary('MakeQRBarcode.dll');
        @Makebar:=GetProcAddress(Moudle,'Make');
        @Mixtext:=GetProcAddress(Moudle,'MixText');
        Txt:=Trim(ClientDataSet1.fieldbyname('MXID').AsString);
        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;
      Edit;
      FieldByName('SSel').Value:=False;
      RMVariables['QRBARCODE']:=fImagePath;
      RM1.LoadFromFile(fPrintFile);
      //RM1.ShowReport;
      for i:=1 to StrToInt(ComboBox2.Text) do
      begin
        RM1.PrintReport;
      end;
    end;
  end;
  ClientDataSet1.EnableControls;
  ComboBox2.ItemIndex:=0;
  ComboBox1.ItemIndex:=-1;
  Panel5.Visible:=False;
end;
procedure TfrmLLRKInPutMX.InitColor();
var
  fsj:string;
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from RT_PTColor where PTID='''+Trim(PTID.Text)+'''');
    Open;
  end;
  fsj:=Trim(ADOTemp.fieldbyname('ColDaiMa').AsString);
  Panel6.Color:=StrToInt('$'+Trim(fsj));
end;
procedure TfrmLLRKInPutMX.Button1Click(Sender: TObject);
var
  FFMXID:string;
begin
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      first;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from CK_MYSC_CR_MX where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
          Open;
        end;
        FFMXID:=Trim(ADOTemp.fieldbyname('MXID').AsString);
        if Trim(FFMXID)<>'' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' UPdate CK_MYSC_CR_MX Set RCGangNo='''+Trim(RCGangNO1.Text)+'''');
            sql.Add(' where MXID='''+Trim(ClientDataSet1.fieldbyname('MXID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Edit;
        FieldByName('SSel').Value:=false;
        FieldByName('RCGangNo').Value:=Trim(RCGangNO1.Text);
      end;
    end;
    ClientDataSet1.EnableControls;
    ADOCmd.Connection.CommitTrans;
    FSaveFlag:='99';
    TBSave.Click;
    FSaveFlag:='';
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_QCRK_Qty :MYID');
      Parameters.ParamByName('MYID').Value:=Trim(FMainId);
      ExecSQL;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_MYSC_CR where MYID='''+Trim(FMainId)+'''');
      Open;
    end;
    SCSHDataNew(ADOTemp,Panel2,2);
    GetHZData();
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！','提示',0);
    Exit;
  end;
end;

procedure TfrmLLRKInPutMX.BtnMClick(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(QtyLRM.Text)='' then Exit;
  if TryStrToFloat(QtyLRM.Text,FReal)=False then
  begin
    Application.MessageBox('非法数字!','提示',0);
    Exit;
  end;
  if Trim(UnitKg.Text)<>'' then
  begin
    QtyLRKg.SetFocus;
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Append;
    FieldByName('RCGangNo').Value:=Trim(RCGangNo.Text);
    FieldByName('MQty').Value:=Trim(QtyLRM.Text);
    Post;
  end;
  QtyLRM.Text:='';
  GetHZData();
end;

procedure TfrmLLRKInPutMX.BtnKgClick(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(QtyLRKg.Text)='' then Exit;
  if Trim(UnitM.Text)<>'' then
  begin
    if Trim(QtyLRM.Text)='' then
    begin
      Application.MessageBox('米数不能为空!','提示',0);
      Exit;
    end;
  end;
  if TryStrToFloat(QtyLRKg.Text,FReal)=False then
  begin
    Application.MessageBox('非法数字!','提示',0);
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Append;
    FieldByName('RCGangNo').Value:=Trim(RCGangNo.Text);
    FieldByName('KgQty').Value:=Trim(QtyLRKg.Text);
    if Trim(UnitM.Text)<>'' then
    begin
      FieldByName('MQty').Value:=Trim(QtyLRM.Text);
    end;
    Post;
  end;
  QtyLRM.Text:='';
  QtyLRKg.Text:='';
  if Trim(UnitM.Text)<>'' then QtyLRM.SetFocus;
  GetHZData();
end;

procedure TfrmLLRKInPutMX.Button3Click(Sender: TObject);
begin
  Panel5.Visible:=True;
end;

procedure TfrmLLRKInPutMX.Panel6DblClick(Sender: TObject);
begin
  try
    frmRSColorBig:=TfrmRSColorBig.Create(Application);
    with frmRSColorBig do
    begin
      frmRSColorBig.Label1.Caption:=Self.MYColorNo.Text+'#';
      frmRSColorBig.Label2.Caption:=Self.MYColor.Text;
      frmRSColorBig.Color:=Self.Panel6.Color;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
  end;
end;

end.
