unit U_ProductOrderLBNameSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,StrUtils,
  Menus, cxTextEdit, cxDropDownEdit, cxButtonEdit;

type
  TfrmProductOrderLBNameSet = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1MPRTKZ: TcxGridDBColumn;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    MPRTMF: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column2: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    PiZhong: TEdit;
    ToolButton4: TToolButton;
    v1Column5: TcxGridDBColumn;
    ToolButton5: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1LengUnit: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1LBName: TcxGridDBColumn;
    v1NLBName: TcxGridDBColumn;
    TJBQ: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1LengUnitPropertiesEditValueChanged(Sender: TObject);
    procedure TJBQClick(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();

    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1:string;
    { Public declarations }
  end;

var
  frmProductOrderLBNameSet: TfrmProductOrderLBNameSet;
implementation
uses
  U_DataLink,U_Fun, U_ZDYHelp,U_CustModelLabel,U_OrderInPut;

{$R *.dfm}

procedure TfrmProductOrderLBNameSet.FormDestroy(Sender: TObject);
begin
  frmProductOrderLBNameSet:=nil;
end;

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

procedure TfrmProductOrderLBNameSet.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderLBNameSet.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('标签设置2',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderLBNameSet.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('exec P_View_OrderLabelSet :begdate,:enddate,:WSql');
      if trim(canshu1)<>'高权限' then
        Parameters.ParamByName('WSql').Value:=' and (Filler='''+Trim(DName)+''' or LiDanPerson='''+Trim(DName)+''' or YWY='''+Trim(DName)+''')'
      else
      Parameters.ParamByName('WSql').Value:='';
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmProductOrderLBNameSet.InitForm();
begin
  ReadCxGrid('标签设置2',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  canshu1:=trim(DParameters1);
end;

procedure TfrmProductOrderLBNameSet.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;
procedure TfrmProductOrderLBNameSet.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderLBNameSet.OrderNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmProductOrderLBNameSet.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderLBNameSet.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible=False then Exit;
  ToolButton1.Click;
end;

procedure TfrmProductOrderLBNameSet.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderLBNameSet.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderLBNameSet.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      ToolBar3.Visible:=False;
      ToolBar4.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      //Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderLBNameSet.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_OrderLabelSet :begdate,:enddate,:WSql') ;
        begin
          Parameters.ParamByName('WSql').Value:=' and orderno like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
        end;
        Parameters.ParamByName('begdate').Value:='';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderLBNameSet.ToolButton2Click(Sender: TObject);
var
  labname:string;
begin
  if Order_Main.IsEmpty then Exit;
  {if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;}
  try
    frmCustModelLabel:=TfrmCustModelLabel.Create(Application);
    with frmCustModelLabel do
    begin
      if ShowModal=1 then
      begin
        labname:=Trim(frmCustModelLabel.ClientDataSet1.fieldbyname('labelCaption').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set LBName='''+Trim(labname)+'''');
          sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;
        with Order_Main do
        begin
          Edit;
          FieldByName('LbName').Value:=labname;
          Post;
        end;
      end;
    end;
  finally
    frmCustModelLabel.Free;
  end;

end;

procedure TfrmProductOrderLBNameSet.ToolButton3Click(Sender: TObject);
var
  FPiZhong:string;
  FReal:Double;
begin
  
  if Trim(PiZhong.Text)='' then
  begin
    Application.MessageBox('皮重不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(PiZhong.Text,FReal)=False then
  begin
    Application.MessageBox('皮重非法数字!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set PiZhong='+(PiZhong.Text));
    sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    ExecSQL;
  end;
  with Order_Main do
  begin
    Edit;
    FieldByName('PiZhong').Value:=PiZhong.Text;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderLBNameSet.ToolButton4Click(Sender: TObject);
var
  labname:string;
begin
  if Order_Main.IsEmpty then Exit;
  {if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;}
  try
    frmCustModelLabel:=TfrmCustModelLabel.Create(Application);
    with frmCustModelLabel do
    begin
      if ShowModal=1 then
      begin
        labname:=Trim(frmCustModelLabel.ClientDataSet1.fieldbyname('labelCaption').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set NLBName='''+Trim(labname)+'''');
          sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;
        with Order_Main do
        begin
          Edit;
          FieldByName('NLBName').Value:=labname;
          Post;
        end;
      end;
    end;
  finally
    frmCustModelLabel.Free;
  end;
end;


procedure TfrmProductOrderLBNameSet.ToolButton5Click(Sender: TObject);
var
  FReal:Double;
begin
  
  if Trim(PiZhong.Text)='' then
  begin
    Application.MessageBox('换算系数不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(PiZhong.Text,FReal)=False then
  begin
    Application.MessageBox('换算系数非法数字!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set XS='''+trim(PiZhong.Text)+'''');
    sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    ExecSQL;
  end;
  with Order_Main do
  begin
    Edit;
    FieldByName('XS').Value:=PiZhong.Text;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderLBNameSet.v1Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalues:string;
begin
  mvalues:=TCXTextEdit(Sender).Text;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set XSType='''+trim(mvalues)+''' ');
    sql.Add('where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    ExecSQL;
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname('XSType').Value:=mvalues;
    post;
  end;


   tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderLBNameSet.v1Column6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
   try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='xsType';
      flagname:='换算系数类型';
      if ShowModal=1 then
      begin
        with Order_Main do
        begin
         edit;
         fieldbyname('xsType').AsString:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main set xsType='''+trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+''' ');
          sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderLBNameSet.v1LengUnitPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalues:string;
begin
  mvalues:=TCXTextEdit(Sender).Text;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main Set LengUnit='''+trim(mvalues)+''' ');
          sql.Add('where Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
       end;
  with Order_Main do
  begin
    edit;
    fieldbyname('LengUnit').Value:=mvalues;
    post;
  end;
end;

procedure TfrmProductOrderLBNameSet.TJBQClick(Sender: TObject);
var
  fPrintFile:string;
   Txt,fImagePath,FKHZ,FKHY,FTM:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 ConNoHZ=(select Top 1 Q.ConNo from JYOrderCon_Main Q ');
    sql.Add(' inner join JYOrder_ConORD W on Q.MainId=W.ConMainId where W.MainId=B.MainId)');
    sql.Add(',KHConNo=(select Top 1 Q.KHConNo from JYOrderCon_Main Q ');
    sql.Add(' inner join JYOrder_ConORD W on Q.MainId=W.ConMainId where W.MainId=B.MainId)');
    sql.Add(',QtyUnit=C.OrderUnit,B.orderNo,GangNo=cast(''0001'' as varchar(6)),mjxh=cast(''0001'' as varchar(6)),MZ=cast(''0001'' as varchar(6))');
    sql.Add(',B.MaiTouNote,B.MPRTCode,B.LBName,B.NLBName,MJQty4=cast(12 as decimal(18,2)),MJLen=cast(12 as decimal(18,2)) ');
    sql.Add(',MPRTCodeNameEng=ISNULL((select Top 1 F.note from KH_Zdy F where F.zdyname=B.MPRTCodeName and F.Type=''PRTCodeName''),B.MPRTCodeName)');
    sql.Add(',B.MPRTCF,B.MPRTMF,B.MPRTKZ,C.PRTColorEng OrdPRTColorEng,SOrddefstr4=cast('''' as varchar(50))');
    sql.Add(',B.MPRTCodeName,B.MPRTSpec,B.MPRTKuanNO,B.LengUnit,PiQty=Cast(0 as decimal(18,2)) ');
    sql.Add(',MQty=cast(1.58 as decimal(18,2)),HS=cast(''1803'' as varchar(6)),C.XHInt XHNo');
    SQL.ADD(',B.MPRTMF PRTMF,B.MPRTKZ PRTKZ,B.MPRTCodeName PRTCodeName,PRTCode=cast('''' as varchar(50)),B.CustomerNoName,C.*');
    sql.Add(',TM=cast('''' as varchar(30)),MJMaoZ=cast(12 as decimal(18,2))');
    sql.Add(' from JYOrder_Main B ');
    sql.Add(' inner join JYOrder_Sub C on B.Mainid=C.Mainid');
    sql.Add(' left join KH_Zdy E on C.PRTColor=E.ZdyName and E.Type=''OrdColor'' ');
    SQL.Add(' where B.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  FKHZ:=Copy(Trim(CDS_Print.fieldbyname('styleNo').AsString),1,3);
  FKHY:=Copy(Trim(CDS_Print.fieldbyname('styleNo').AsString),5,7);
  FTM:=trim(FKHZ)+trim(FKHY)+copy(Trim(CDS_Print.fieldbyname('PRTHX').AsString)+Trim(CDS_Print.fieldbyname('PRTColorEng').AsString),1,3)+'0120000010001';
  with CDS_Print do
  begin
    Edit;
    FieldByName('TM').AsString:=Trim(FTM);
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(CDS_Print.fieldbyname('TM').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;
  if Trim(CDS_Print.fieldbyname('LBName').AsString)<>'' then
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(CDS_Print.fieldbyname('LBName').AsString)+'.rmf'
  else
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RMVariables['ID']:=trim(CDS_Print.fieldbyname('HS').AsString)+Trim(CDS_Print.fieldbyname('XHNo').AsString);
    RMVariables['FMPRTMF']:=StrToFloatDef(CDS_Print.fieldbyname('MPRTMF').AsString,0)/100;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Order_Main.EnableControls;
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
    Exit;
  end;
end;

end.
