unit U_QCRKInPut_KC;

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, Menus;

type
  TfrmQCRKInPut_KC = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    Panel3: TPanel;
    Label34: TLabel;
    Label13: TLabel;
    Label1: TLabel;
    MYCode: TEdit;
    CRTime: TDateTimePicker;
    MYName: TEdit;
    Label11: TLabel;
    MYColor: TEdit;
    Label12: TLabel;
    MYKZ: TEdit;
    Label14: TLabel;
    MYMF: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    MYColorNo: TEdit;
    Label3: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1MQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label4: TLabel;
    GangQty: TEdit;
    Label8: TLabel;
    MXHZPS: TEdit;
    Label17: TLabel;
    MXHZKGQty: TEdit;
    Label18: TLabel;
    MYType: TBtnEditA;
    v1Column4: TcxGridDBColumn;
    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;
    v1Laster: TcxGridDBColumn;
    RM1: TRMGridReport;
    CDS_Print: TClientDataSet;
    RMDB_Main: TRMDBDataSet;
    Label27: TLabel;
    Edit1: TEdit;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox2: TComboBox;
    Label29: TLabel;
    ComboBox1: TComboBox;
    BtnPrt: TButton;
    BtnQX: TButton;
    Label30: TLabel;
    Button3: TButton;
    Panel6: TPanel;
    Label6: TLabel;
    Note: TEdit;
    Label31: TLabel;
    GYLXName: TEdit;
    Button4: TButton;
    Button5: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Label33: TLabel;
    Note2: TEdit;
    v1MXNote: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ShuiXiSuoLvClick(Sender: TObject);
    procedure ShuiXiSuoLvJGMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BtnQXClick(Sender: TObject);
    procedure MYNameKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnPrtClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Panel6DblClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(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
  frmQCRKInPut_KC: TfrmQCRKInPut_KC;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_GYSList,U_SCPerson,U_BPZdy_LRGX_CX
  ,U_QCRKJLList,U_RSColorBig,U_MLMangeRS_GYCX,U_MLMangeRS_CX,U_RTPTColor,U_ZDYCPName;

{$R *.dfm}

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

procedure TfrmQCRKInPut_KC.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_MYSC_CR A ');
    sql.Add(' where A.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();
  MYCode.SetFocus;
  if Trim(FMainId)<>'' then
  begin
    InitColor();
  end;
end;

procedure TfrmQCRKInPut_KC.InitMXData();
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,Laster=isnull(Editer,Filler) ');
    sql.Add(',RCGangNo1=(case when isnull(RCGangNo,'''')='''' then GangNo else RCGangNo end) ');
    sql.Add('from CK_MYSC_CR_MX A where isnull(MYID,'''')='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet1);
  SInitCDSData20(ADOTemp,ClientDataSet1);
end;

procedure TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmQCRKInPut_KC.SaveData():Boolean;
var
  maxno,maxmxno,maxnoflag,fsj,FMXID,FCode: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,'SD','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('MXNote').Value:=ClientDataSet1.fieldbyname('MXNote').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 TfrmQCRKInPut_KC.ShuiXiSuoLvClick(Sender: TObject);
begin
  if Trim(TEdit(Sender).Text)='' then
  begin
    TEdit(Sender).Text:='√';
  end else
  begin
    TEdit(Sender).Text:='';
  end;
end;

procedure TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.BtnQXClick(Sender: TObject);
begin
  Panel5.Visible:=False;
end;

procedure TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;

procedure TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.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 TfrmQCRKInPut_KC.Button3Click(Sender: TObject);
begin
  Panel5.Visible:=True;
end;

procedure TfrmQCRKInPut_KC.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;

procedure TfrmQCRKInPut_KC.N1Click(Sender: TObject);
begin
  try
    frmRTPTColor:=TfrmRTPTColor.Create(Application);
    with frmRTPTColor do
    begin
      if ShowModal=1 then
      begin
        Self.PTID.Text:=Trim(frmRTPTColor.FPTID);
        Self.MYColor.Text:=Trim(frmRTPTColor.FColNameKH);
        Self.MYColorNo.Text:=Trim(frmRTPTColor.FColName);
      end;
    end;
  finally
    frmRTPTColor.Free;
  end;
  if Trim(PTID.Text)<>'' then
  begin
    InitColor();
  end;
end;

procedure TfrmQCRKInPut_KC.Button5Click(Sender: TObject);
begin
  frmZDYCPName:=TfrmZDYCPName.create(self);
  with frmZDYCPName do
  begin
    flag:='MYName';
    flagName:='品名';
    V1Code.Visible:=true;
    if showmodal=1 then
    begin
      MYName.text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      MYCode.text:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
    end;
    free;
  end;
end;

procedure TfrmQCRKInPut_KC.Button4Click(Sender: TObject);
begin
  frmBPZDY_LRGX_CX:=TfrmBPZDY_LRGX_CX.Create(self);
  with frmBPZDY_LRGX_CX do
  begin
    if ShowModal=1 then
    begin
      GYLXName.Text:=Trim(FZGY);
    end;
    free;
  end;
end;

end.
