unit U_JYDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, ADODB, DBClient, cxGridCustomPopupMenu,
  cxGridPopupMenu, ExtCtrls, ComCtrls, ToolWin, cxTextEdit, Buttons,
  cxSplitter, cxCheckBox, MovePanel, RM_Preview;
{function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer):integer;stdcall;external 'JCYData.DLL';
function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';
  function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JCYData.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';}
  {function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JZCRS323C.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JZCRS323C.DLL';

  function GetCommData(fhandle:hwnd;var defnum1,defnum2:double;var fisjz:integer):integer;stdcall;external 'JZCRS323C.DLL'; }


type
  TfrmJYDJ = class(TForm)
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    ADOTmp: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource2: TDataSource;
    CDS_MJCD: TClientDataSet;
    DataSource3: TDataSource;
    CDS_MJID: TClientDataSet;
    Timer1: TTimer;
    ADOQueryPrint: TADOQuery;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle3: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle4: TcxStyle;
    cxStyleRepository4: TcxStyleRepository;
    cxStyle5: TcxStyle;
    cxStyleRepository5: TcxStyleRepository;
    cxStyle6: TcxStyle;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    Panel2: TPanel;
    Label4: TLabel;
    MJMaoZ: TEdit;
    Label11: TLabel;
    BTPrint: TButton;
    Button5: TButton;
    v3Column1: TcxGridDBColumn;
    MJZS: TEdit;
    Panel7: TPanel;
    ComboBox1: TComboBox;
    v3Column4: TcxGridDBColumn;
    Panel1: TPanel;
    RMPreview1: TRMPreview;
    ADOQueryLabel: TADOQuery;
    RMGridReport1: TRMGridReport;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Panel5: TPanel;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    Panel6: TPanel;
    Label16: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    BTEdit: TButton;
    BTAdd: TButton;
    OrderNo: TEdit;
    Color: TEdit;
    HX: TEdit;
    GangNo: TEdit;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure MJBanZuClick(Sender: TObject);
    procedure MJSJKZClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormClick(Sender: TObject);
    procedure BTAddClick(Sender: TObject);
    procedure BTEditClick(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure BTPrintKeyPress(Sender: TObject; var Key: Char);
    procedure MJZSKeyPress(Sender: TObject; var Key: Char);
    procedure MJMaoZKeyPress(Sender: TObject; var Key: Char);
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoDblClick(Sender: TObject);
    procedure ColorDblClick(Sender: TObject);
    procedure HXDblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    FInt,PState,PrintInt,SCInitGrid,FState,FCK:Integer;
    FColumn,FFFMJID,LabName:String;
    procedure InitCDGridID();

    function  SaveData():Boolean;

    procedure PrtData(FMJID:String);

  public
    { Public declarations }

  end;

var
  frmJYDJ: TfrmJYDJ;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_iniParam,U_OrderInPut, U_LabelList,U_PRTJYDJList;

{$R *.dfm}

procedure TfrmJYDJ.FormDestroy(Sender: TObject);
begin
  frmJYDJ:=nil;
end;


procedure TfrmJYDJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmJYDJ.InitCDGridID();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select ');
    sql.Add(' A.* from DJ_JY A ');
    sql.Add(' where isnull(OrderNo,'''')='''+Trim(OrderNo.Text)+'''');
    sql.Add(' and isnull(Color,'''')='''+Trim(Self.Color.Text)+'''');
    sql.Add(' and isnull(HX,'''')='''+Trim(Self.HX.Text)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_MJID);
  SInitCDSData20(ADOQueryMain,CDS_MJID);
end;

procedure TfrmJYDJ.FormShow(Sender: TObject);
begin
  
  if IsINIFile() then
    ReadINIFile()
end;
procedure TfrmJYDJ.MJBanZuClick(Sender: TObject);
begin
  FInt:=4;
end;

procedure TfrmJYDJ.MJSJKZClick(Sender: TObject);
begin
  FInt:=4;
end;

procedure TfrmJYDJ.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  FInt:=4;
  InitCDGridID();
end;

procedure TfrmJYDJ.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  FInt:=4;
  PState:=0;
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from DJ_JY where DJID='''+Trim(CDS_MJID.fieldbyname('DJID').AsString)+'''');
    Open;
  end;
  MJZS.Text:=Trim(ADOTmp.fieldbyname('MJZS').AsString);
  MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
  Label16.Caption:='';
end;


procedure TfrmJYDJ.FormClick(Sender: TObject);
begin
  FInt:=4;
end;

function  TfrmJYDJ.SaveData():Boolean;
var
  maxno,FMJID,BZID,strsql:String;
  FMJLen:Double;
  FOrder:Integer;
begin
  if PState=1 then
      FMJID:=''
  else if PState=2 then
     FMJID:=Trim(CDS_MJID.fieldbyname('DJID').AsString)
  else if PState<1 then Exit;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from DJ_JY where DJID='''+Trim(FMJID)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMJID)='' then
      begin
        Append;
        if GetLSNo(ADOTmp,maxno,'','DJ_JY',4,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          exit;
        end;
      end
      else begin
        maxno:=Trim(FMJID);
        Edit;
      end;
      FieldByName('DJID').Value:=Trim(maxno);
      FieldByName('OrderNo').Value:=Trim(OrderNo.Text);
      FieldByName('Color').Value:=Trim(Self.Color.Text);
      FieldByName('HX').Value:=Trim(HX.Text);
      FieldByName('LBName').Value:=Trim(LabName);
      FieldByName('GangNo').Value:=Trim(GangNo.Text);
      if Trim(MJZS.Text)<>'' then
      begin
        FieldByName('MJZS').Value:=StrToFloat(MJZS.Text);
      end else
      begin
        FieldByName('MJZS').Value:=Null;
      end;
      if Trim(MJMaoZ.Text)<>'' then
      begin
        FieldByName('MJMaoZ').Value:=StrToFloat(MJMaoZ.Text);
      end;
      Post;
    end;
    if PState=1 then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('UPdate DJ_JY Set MJXH=(select max(MJXH)+1 ');
        SQL.Add(' from  DJ_JY A  ');
        sql.Add(' where A.OrderNo=DJ_JY.OrderNo');
        sql.Add(' and isnull(A.Color,'''')=isnull(DJ_JY.Color,'''')');
        sql.Add(' and isnull(A.HX,'''')=isnull(DJ_JY.HX,'''')');
        sql.Add(' )where DJID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;

    FMJID:=Trim(maxno);
    FFFMJID:=Trim(maxno);
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from DJ_JY where DJID='''+Trim(maxno)+'''');
      Open;
    end;
    if PState=1 then
    begin
      with CDS_MJID do
      begin
        Append;
        FieldByName('DJID').Value:=Trim(maxno);
        FieldByName('MJXH').Value:=ADOTmp.fieldbyname('MJXH').Value;
        FieldByName('MJMaoZ').Value:=ADOTmp.fieldbyname('MJMaoZ').Value;
        FieldByName('MJZS').Value:=ADOTmp.fieldbyname('MJZS').Value;
        Post;
      end;
    end else
    begin
      CDS_MJID.Locate('DJID',maxno,[]);
      with CDS_MJID do
      begin
        Edit;
        FieldByName('DJID').Value:=Trim(maxno);
        FieldByName('MJXH').Value:=ADOTmp.fieldbyname('MJXH').Value;
        FieldByName('MJMaoZ').Value:=ADOTmp.fieldbyname('MJMaoZ').Value;
        FieldByName('MJZS').Value:=ADOTmp.fieldbyname('MJZS').Value;
        Post;
      end;
    end;

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

procedure TfrmJYDJ.BTAddClick(Sender: TObject);
begin
  PState:=1;
  InitCDGridID();
  Label16.Caption:='新增';
end;

procedure TfrmJYDJ.BTEditClick(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then Exit;
  FInt:=1;
  PState:=2;
  Label16.Caption:='修改';
end;

procedure TfrmJYDJ.BTPrintClick(Sender: TObject);
var
  fPrintFile:String;
  mvalue:Double;
  i:Integer;
begin
  if FInt=4  then
  begin
    if CDS_MJID.IsEmpty=False then
    begin
      Application.MessageBox('已打过条码？请尝试修改。','提示',0);
      Exit;
    end;
  end;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if (Trim(Self.Color.Text)='') and (Trim(Self.HX.Text)='') then
  begin
    Application.MessageBox('颜色花型不能同时为空!','提示',0);
    Exit;
  end;
  if  trim(MJMaoZ.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 Trim(MJZS.Text)<>'' then
  begin
    if TryStrToFloat(MJZS.Text,mvalue)=False then
    begin
      Application.MessageBox('赠送数量非法数字!','提示',0);
      Exit;
    end;
  end;

  if PState=1 then
  begin
    if Application.MessageBox('是否要打印条码？','提示',32+4)<>IDYES then Exit;
  end;
  if SaveData() then
  begin
    MJMaoZ.Text:='';
    MJZS.Text:='';
  end;
  for i:=1 to StrToInt(ComboBox1.Text) do
  begin
    PrtData(FFFMJID);
  end;
  Label16.Caption:='';
  BTAdd.Click;
  MJMaoZ.SetFocus;
end;
procedure TfrmJYDJ.PrtData(FMJID:String);
var
   fPrintFile:String;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,ROLLNO=MJXH ,');
    sql.Add('Qty=MJMaoZ-isnull(MJZS,0),');
    sql.Add('Case when isnull(MJZS,-99)=-99 then Cast('''' as varchar(20)) else ''+''+Cast(MJZS as varchar(20)) end as ZengSong ');
    sql.Add(' from DJ_JY A where DJID='''+Trim(FMJID)+'''');
    Open;
  end;
  if Trim(ADOQueryPrint.fieldbyname('LBName').AsString)<>'' then
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString+'.rmf')
  else begin
    Application.MessageBox('标签未设置!','提示',0);
    Exit;
  end;
  if FileExists(fPrintFile) then
  begin
    
    RM2.LoadFromFile(fPrintFile);
    //RM2.ShowReport;
    RM2.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString)),'提示',0);
  end;
end;


procedure TfrmJYDJ.ToolButton1Click(Sender: TObject);
begin

  MJMaoZ.Text:='';
  MJZS.Text:='';
  BTAdd.Click;
  FInt:=11;
  MJMaoZ.SetFocus;
end;

procedure TfrmJYDJ.Button5Click(Sender: TObject);
var
   fPrintFile:String;
   i:Integer;
begin
  {if CDS_MJID.IsEmpty then Exit;
  for i:=1 to StrToInt(ComboBox1.Text) do
  begin
    PrtData(Trim(CDS_MJID.fieldbyname('MJID').AsString));
  end;
  PState:=1;
  MJMaoZ.Text:='';
  MJZS.Text:='';
  BTAdd.Click;
  FInt:=11;
  MJMaoZ.SetFocus;  }
  BTEdit.Click;
end;

procedure TfrmJYDJ.BTPrintKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    BTPrint.Click;
  end;
end;

procedure TfrmJYDJ.MJZSKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
	begin
		Key:= #0;
    BTPrint.SetFocus;
	end;
end;

procedure TfrmJYDJ.MJMaoZKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
	begin
		Key:= #0;
    MJZS.SetFocus;
	end;
end;

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

procedure TfrmJYDJ.OrderNoDblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderNo';
      flagname:='订单号';
      fnote:=True;
      V1Note.Caption:='标签名称';
      if ShowModal=1 then
      begin
        OrderNo.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        LabName:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.Color.Text:='';
        Self.HX.Text:='';
        Self.BTAdd.Click;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  with Self.ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from P_Label where labelCaption='''+Trim(LabName)+'.rmf'+'''');
    Open;
  end;
  if ADOTmp.IsEmpty=False then
  begin
    with RMGridReport1 do
    begin
      LoadFromBlobField(tblobfield(ADOTmp.fieldbyname('labelFile')));
      //Preview :=RMPreview1;
      ShowReport ;
    end;
  end else
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Cast(''0x'' as image) labelFile ');
      open;
    end;
    with RMGridReport1 do
    begin
      LoadFromBlobField(tblobfield(ADOTmp.fieldbyname('labelFile')));
      ShowReport ;
    end;
  end;
end;

procedure TfrmJYDJ.ColorDblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DJColor';
      flagname:='颜色';
      MainType:=Trim(OrderNo.Text);
      if ShowModal=1 then
      begin
        Self.Color.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        Self.BTAdd.Click;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYDJ.HXDblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DJHX';
      flagname:='花型';
      MainType:=Trim(OrderNo.Text);
      if ShowModal=1 then
      begin
        HX.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        Self.BTAdd.Click;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYDJ.ToolButton2Click(Sender: TObject);
begin
  try
    frmLabelList:=TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      {if Self.JYInt=99 then
      begin
        frmLabelList.Tadd.Visible:=False;
        frmLabelList.Tupd.Visible:=False;
        frmLabelList.TOK.Visible:=False;
        frmLabelList.Tdel.Visible:=False;
      end; }
      if ShowModal=1 then
      begin
      end;
    end;
  finally
    frmLabelList.Free;
  end;
end;

procedure TfrmJYDJ.ToolButton3Click(Sender: TObject);
begin
  try
    frmPRTJYDJList:=TfrmPRTJYDJList.Create(Application);
    with frmPRTJYDJList do
    begin
      if ShowModal=1 then
      begin


      end;
    end;
  finally
    frmPRTJYDJList.Free;
  end;
end;

end.
