unit U_CPCKMXLR;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, MMSystem,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, BtnEdit, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, cxMaskEdit, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmCPCKMXLR = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel3: TPanel;
    ADOQueryMain: TADOQuery;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    vMXColumn1: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton3: TToolButton;
    vMXColumn2: TcxGridDBColumn;
    Panel1: TPanel;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    XS: TEdit;
    vMXColumn3: TcxGridDBColumn;
    vMXColumn4: TcxGridDBColumn;
    Label3: TLabel;
    Qty: TEdit;
    Label4: TLabel;
    ComboBox2: TComboBox;
    Label5: TLabel;
    gangno: TEdit;
    vMXColumn5: TcxGridDBColumn;
    Label6: TLabel;
    KGQTY: TEdit;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    CheckBox1: TCheckBox;
    MXNOTE: TEdit;
    Label7: TLabel;
    CheckBox2: TCheckBox;
    ComboBox3: TComboBox;
    ToolButton4: TToolButton;
    TvMXColumn1: TcxGridDBColumn;
    ToolButton5: TToolButton;
    LABNAME: TcxButtonEdit;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    TT: TEdit;
    Label11: TLabel;
    KH: TEdit;
    Label12: TLabel;
    KUANHAO: TEdit;
    Label13: TLabel;
    GUIGE: TEdit;
    TvMXColumn2: TcxGridDBColumn;
    TvMXColumn3: TcxGridDBColumn;
    TvMXColumn4: TcxGridDBColumn;
    TvMXColumn5: TcxGridDBColumn;
    Label14: TLabel;
    PIZHONG: TEdit;
    TvMXColumn6: TcxGridDBColumn;
    TvMXColumn7: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBCloseClick(Sender: TObject);
    procedure FHAddressBtnDnClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure TvMXKeyPress(Sender: TObject; var Key: Char);
    procedure TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure QtyKeyPress(Sender: TObject; var Key: Char);
    procedure KGQTYKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure LABNAMEPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitGrid();
    procedure PrtData(FJYID: string);
    function savedate(): Boolean;
    { Private declarations }
  public
    fkeyNO, FMXGangHao: string;
    FType: integer;
    fCKName: string;
    FCRTYPE: string;
    { Public declarations }
  end;

var
  frmCPCKMXLR: TfrmCPCKMXLR;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_ZDYHelpSel, U_FUN;

{$R *.dfm}
procedure TfrmCPCKMXLR.PrtData(FJYID: string);
var
  fPrintFile: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if LABNAME.Text = '' then
  begin
    application.MessageBox('未设置标签格式！', '提示信息', MB_ICONERROR);
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Print_JYLab ');
    SQL.Add(' @mjID=''' + Trim(FJYID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;

  ExportFtErpFile(Trim(labname.Text) + '.rmf', ADOQueryTmp);
  if Trim(labname.Text) <> '' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(labname.Text) + '.rmf'
  end
  else
  begin
    ExportFtErpFile('通用中文标签.rmf', ADOQueryTmp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用中文标签.rmf';
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['MXMJXH'] := CDS_MAIN.FIELDBYNAME('MXMJXH').ASSTRING;
    RMVariables['MXGangHao'] := CDS_MAIN.FIELDBYNAME('MXGangHao').ASSTRING;
    RMVariables['MXQty'] := CDS_MAIN.FIELDBYNAME('MXQty').ASSTRING;
    RMVariables['MXQTY2'] := CDS_MAIN.FIELDBYNAME('MXQTY2').ASSTRING;
    RMVariables['MXUnit'] := CDS_MAIN.FIELDBYNAME('MXUnit').ASSTRING;
    RMVariables['MXFM'] := CDS_MAIN.FIELDBYNAME('MXFM').ASSTRING;

    RMVariables['mxtt'] := CDS_MAIN.FIELDBYNAME('mxtt').ASSTRING;
    RMVariables['mxkh'] := CDS_MAIN.FIELDBYNAME('mxkh').ASSTRING;
    RMVariables['mxkuanno'] := CDS_MAIN.FIELDBYNAME('mxkuanno').ASSTRING;
    RMVariables['mxspec'] := CDS_MAIN.FIELDBYNAME('mxspec').ASSTRING;
    RMVariables['mjMAOZ'] := CDS_MAIN.FIELDBYNAME('MJMAOZ').ASSTRING;
    RMVariables['PIZHONG'] := CDS_MAIN.FIELDBYNAME('PIZHONG').ASSTRING;
    RM1.LoadFromFile(fPrintFile);

    RM1.DefaultCopies := strtointdef(trim(ComboBox3.Text), 1);

    RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

function TfrmCPCKMXLR.savedate(): Boolean;
var
  MaxNo: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * ');
          sql.Add(',MaxRollNo=(select max(isnull(MXMJXH,0)) from CK_SXPB_CRMX X where X.CRNO=''' + trim(fkeyNo) + ''' )');
          sql.Add(' from CK_SXPB_CRMX A');
          sql.Add('where MXID=' + quotedstr(Trim(CDS_Main.FieldByName('MXID').AsString)));
//          ShowMessage(sql.Text);
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'MCM', 'CK_SXPB_CRMX', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;

//          with ADOQueryCmd do
//          begin
//            close;
//            sql.Clear;
//            sql.Add('select * ');
//            sql.Add(',MaxRollNo=(select max(isnull(MXMJXH,0)) from CK_SXPB_CRMX X where X.CRNO=''' + trim(fkeyNo) + ''' )');
//            sql.Add(' from CK_SXPB_CRMX A');
//            sql.Add('where CRNO=' + quotedstr(Trim(fkeyNo)));
////          ShowMessage(sql.Text);
//            open;
//          end;
//
//          with CDS_Main do
//          begin
//            Edit;
//            if FieldByName('mxmjxh').AsString = '' then
//            begin
//              FieldByName('mxmjxh').Value := ADOQueryCmd.fieldbyname('MaxRollNo').AsInteger + 1;
//            end;
//
//            Post;
//          end;

          ADOQueryCmd.Append;
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('MXID').AsString);
          ADOQueryCmd.Edit;
        end;



        //保存明细
        RTSetSaveDataCDS(ADOQueryCmd, TvMX, CDS_Main, 'CK_SXPB_CRMX', 0);

        ADOQueryCmd.FieldByName('CRZT').Value := trim(FCRTYPE);

         //保存隐藏ID
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNo);

        ADOQueryCmd.FieldByName('MXNOTE').Value := trim(MXNOTE.Text);

        ADOQueryCmd.FieldByName('MXID').Value := trim(MaxNo);

        ADOQueryCmd.Post;

        CDS_Main.Edit;
        CDS_Main.FieldByName('MXID').Value := trim(MaxNo);
        CDS_Main.post;

//         更新库存
        if Trim(edit3.Text) = 'Kg' then
        begin

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
            sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
            sql.Add(' ) where SPID=''' + Trim(fkeyNO) + '''');
//            ShowMessage(sql.Text);
            ExecSQL;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
            sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
            sql.Add(' where SPID=''' + Trim(Edit2.Text) + '''');
            ExecSQL;
          end;
          with ADOQueryTmp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Edit2.Text) + '''');
            Open;
          end;
          if ADOQueryTmp.FieldByName('MXKCQty').Value < 0 then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
            Exit;
          end;

        end
        else
        begin

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set Qty=(select sum(isnull(mxqty2,0)) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
            sql.Add('),piqty=(select count(mxid) from CK_SXPB_CRMX A where A.crno=CK_SXPB_CR.spid');
            sql.Add(' ) where SPID=''' + Trim(fkeyNO) + '''');
//            ShowMessage(sql.Text);
            ExecSQL;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
            sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
            sql.Add(' where SPID=''' + Trim(Edit2.Text) + '''');
            ExecSQL;
          end;
          with ADOQueryTmp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Edit2.Text) + '''');
            Open;
          end;
          if ADOQueryTmp.FieldByName('MXKCQty').Value < 0 then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
            Exit;
          end;
        end;
//


        CDS_Main.next;
      end;
    end;

    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCPCKMXLR.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CRMX A where CRNO=' + quotedstr(trim(fkeyNO)));
//      SQL.Add('AND CRZT=' + QuotedStr(FCRTYPE));
      SQL.Add('AND NOT EXISTS(SELECT MXID FROM CK_SXPB_CRMX C WHERE C.FZMXID=A.MXID)');
      sql.Add(' order by MXID');
//      ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);
    MXNOTE.Text := adoqueryMain.FieldByName('MXNOTE').AsString;
  finally
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main A  inner join JYOrder_sub b on a.mainid=b.mainid ');
    SQL.Add('where subid=(select ordsubidrk from CK_SXPB_CR c where c.spid= ' + QuotedStr(Trim(fkeyNO)));
    sql.Add(')');

//      ShowMessage(SQL.Text);
    open;
  end;

  if ADOQueryTmp.IsEmpty = false then
  begin
    KUANHAO.TEXT := ADOQueryTmp.FieldByName('PRTKUANNO').AsString;
    KH.TEXT := ADOQueryTmp.FieldByName('BQKH').AsString;
    GUIGE.TEXT := ADOQueryTmp.FieldByName('MPRTCF').AsString;
    tt.TEXT := ADOQueryTmp.FieldByName('BJTT').AsString;
    xs.TEXT := ADOQueryTmp.FieldByName('prtbili').AsString;
    LABNAME.TEXT := ADOQueryTmp.FieldByName('LBNAME').AsString;
  end;
//  with ADOQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from CK_SXPB_CRMX A where CRNO=' + quotedstr(trim(fkeyNO)));
//
//    sql.Add(' order by MXID');
////      ShowMessage(SQL.Text);
//    open;
//  end;
//
//  if ADOQueryTmp.IsEmpty = false then
//  begin
//    KUANHAO.TEXT := ADOQueryTmp.FieldByName('mxkuanno').AsString;
//    KH.TEXT := ADOQueryTmp.FieldByName('mxkh').AsString;
//    GUIGE.TEXT := ADOQueryTmp.FieldByName('mxspec').AsString;
//    tt.TEXT := ADOQueryTmp.FieldByName('mxtt').AsString;
//  end
//  else
//  begin
//    with ADOQueryTmp do
//    begin
//      close;
//      sql.Clear;
//      SQL.Add('select top 1 a.* from CK_SXPB_CRMX a inner join CK_SXPB_CR b  on a.crno=b.spid  ');
//      SQL.Add('where b.QCorderNo=(select QCorderNo from CK_SXPB_CR b where spid=' + QuotedStr(Trim(fkeyNO)));
//      SQL.Add(')');
//      sql.Add('order by mxid desc');
//      Open;
//    end;
//    KUANHAO.TEXT := ADOQueryTmp.FieldByName('mxkuanno').AsString;
//    KH.TEXT := ADOQueryTmp.FieldByName('mxkh').AsString;
//    GUIGE.TEXT := ADOQueryTmp.FieldByName('mxspec').AsString;
//    tt.TEXT := ADOQueryTmp.FieldByName('mxtt').AsString;
//    if ADOQueryTmp.IsEmpty = True then
//    begin
//      with ADOQueryTmp do
//      begin
//        close;
//        sql.Clear;
//        SQL.Add('SELECT * FROM JYORDER_MAIN A INNER JOIN  JYORDER_SUB B ON A.MAINID=B.MAINID WHERE  ');
//        SQL.Add('B.MAINID=(SELECT ORDMAINIDRK FROM CK_SXPB_CR C WHERE C.SPID=' + QuotedStr(Trim(fkeyNO)));
//        SQL.Add(')');
////        ShowMessage(sql.Text);
//        Open;
//      end;
//      KUANHAO.TEXT := ADOQueryTmp.FieldByName('prtkuanno').AsString;
//      KH.TEXT := ADOQueryTmp.FieldByName('CustomerNoName').AsString;
//      GUIGE.TEXT := ADOQueryTmp.FieldByName('MPRTSpec').AsString;
//    end;
//  end;

end;

procedure TfrmCPCKMXLR.FormDestroy(Sender: TObject);
begin
  frmCPCKMXLR := nil;
end;

procedure TfrmCPCKMXLR.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmCPCKMXLR.FormShow(Sender: TObject);
var
  fbhType, fInoutNo: string;
begin
  readCxGrid(self.Caption, TvMX, '面料仓库1');
  InitGrid();
end;

procedure TfrmCPCKMXLR.TBSaveClick(Sender: TObject);
var
  FQTY, FKGQTY, FPIQTY: Double;
  FQTY1, FPIQTY1: Double;
begin
  if cds_main.IsEmpty then
    exit;
  ToolBar1.SetFocus;
//  FKGQTY := TvMX.DataController.Summary.FooterSummaryValues[1];
//  FQTY := TvMX.DataController.Summary.FooterSummaryValues[3];
//  FPIQTY := TvMX.DataController.Summary.FooterSummaryValues[2];
//
//  FKGQTY1 := StrToFloatDef(Edit1.Text, 0);
//  FQTY1 := StrToFloatDef(Edit3.Text, 0);
//  FPIQTY1 := StrToFloatDef(Edit2.Text, 0);
//
//  if (FKGQTY > FKGQTY1) or (FQTY > FQTY1) or (FPIQTY > FPIQTY1) then
//  begin
//    application.MessageBox('出库数大于库存数', '提示信息');
//    Exit;
//  end;
  if savedate() then
  begin

    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
    Exit;
  end
  else
  begin
//    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmCPCKMXLR.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPCKMXLR.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, TvMX, '面料仓库1');
//  ModalResult := 1;
  close;
end;

procedure TfrmCPCKMXLR.FHAddressBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmCPCKMXLR.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPCKMXLR.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPCKMXLR.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to 10 do
  begin
    with CDS_Main do
    begin
      Append;
      FieldByName('MXGangHao').Value := FMXGangHao;
      FieldByName('MXQty').Value := 0;
      FieldByName('MXQty2').Value := 0;
      Post;
    end;
  end;
end;

procedure TfrmCPCKMXLR.ToolButton2Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_SXPB_CRMX where MXID=' + quotedstr(Trim(CDS_Main.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  CDS_Main.Delete;
end;

procedure TfrmCPCKMXLR.TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmCPCKMXLR.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  OneKeyPost(TvMX, CDS_Main);
end;

procedure TfrmCPCKMXLR.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FZL, FCD, FXS: DOUBLE;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TvMX.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with cds_main do
  begin
    Edit;
    FieldByName(FFieldName).Value := (strtofloatdef(trim(mvalue), 0));

    Post;
  end;
  with cds_main do
  begin
    Edit;
    FZL := (strtofloatdef(trim(FieldByName('MXQTY').ASSTRING), 0));   //重量
    FCD := (strtofloatdef(trim(FieldByName('MXQTY2').ASSTRING), 0));   //长度

    FXS := (strtofloatdef(trim(xs.TEXT), 0));   //系数
    if FFieldName = 'mxqty' then
    begin
      FieldByName('mxqty2').Value := FZL * FXS;
    end;
    if FFieldName = 'mxqty2' then
    begin
      FieldByName('mxqty').Value := FCD * FXS;
    end;
    Post;
  end;

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

procedure TfrmCPCKMXLR.TvMXKeyPress(Sender: TObject; var Key: Char);
begin
  TvMX.OptionsData.Editing := False;
  if Key = #13 then
    Key := #40;
  TvMX.OptionsData.Editing := True;
end;

procedure TfrmCPCKMXLR.TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  TvMX.OptionsData.Editing := False;
  if Key = 13 then
    Key := 40;
  TvMX.OptionsData.Editing := True;
end;

procedure TfrmCPCKMXLR.QtyKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CDS_Main.Last;
    CopyAddRowCDS(CDS_Main);
    with CDS_Main do
    begin
      Edit;
      FieldByName('MXGangHao').Value := gangno.Text;

      FieldByName('mxtt').Value := TT.Text;
      FieldByName('mxkh').Value := KH.Text;
      FieldByName('mxkuanno').Value := KUANHAO.Text;
      FieldByName('mxspec').Value := guige.Text;
      FieldByName('pizhong').Value := StrToFloatDEF(pizhong.Text, 0);
      FieldByName('MXID').Value := '';
      FieldByName('CRNO').Value := '';
//      FieldByName('MXMJXH').Value := null;
      FieldByName('MXMJXH').Value := StrToFloatDef(FieldByName('MXMJXH').asstring, 0) + 1;
      if CDS_Main.FieldByName('MXUnit').AsString = '' then
        FieldByName('MXUnit').Value := 'Y';
      if XS.Text <> '' then
      begin
        if ComboBox2.Text = 'KG' then
        begin
          FieldByName('MJMAOZ').Value := strtofloatdef(KGQty.Text, 0) + strtofloatdef(PIZHONG.Text, 0);
          FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
          FieldByName('MXQty2').Value := Format('%.1f', [StrToFloatDef(KGQty.Text, 0) * StrToFloatDef(xs.Text, 0)]);

        end
        else
        begin

          FieldByName('MJMAOZ').Value := Format('%.1f', [StrToFloatDef(Qty.Text, 0) * StrToFloatDef(xs.Text, 0) + StrToFloatDef(PIZHONG.Text, 0)]);
//            FieldByName('MXUnit').Value := combobox2.Text;
          FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);
          FieldByName('MXQty').Value := Format('%.1f', [StrToFloatDef(Qty.Text, 0) * StrToFloatDef(xs.Text, 0)]);
//          FieldByName('MXUnit').Value := combobox2.Text;
        end;
      end
      else
      begin
        FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
        FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);
        FieldByName('MJMAOZ').Value := strtofloatdef(KGQty.Text, 0) + strtofloatdef(PIZHONG.Text, 0);
      end;

      Post;
    end;
    if CheckBox2.Checked = True then
    begin
      PrtData(Edit2.Text);
    end;
    Qty.Text := '';
    KGQTY.Text := '';
    KGQty.SetFocus;
  end;

end;

procedure TfrmCPCKMXLR.KGQTYKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    if CheckBox1.Checked = True then
    begin
      CDS_Main.Last;
      CopyAddRowCDS(CDS_Main);
      with CDS_Main do
      begin
        Edit;
        FieldByName('MXGangHao').Value := gangno.Text;
        FieldByName('mxtt').Value := TT.Text;
        FieldByName('mxkh').Value := KH.Text;
        FieldByName('mxkuanno').Value := KUANHAO.Text;
        FieldByName('pizhong').Value := StrToFloatDEF(pizhong.Text, 0);
        FieldByName('mxspec').Value := guige.Text;
        FieldByName('MXID').Value := '';
        FieldByName('CRNO').Value := '';
//        FieldByName('MXMJXH').Value := null;
        FieldByName('MXMJXH').Value := StrToFloatDef(FieldByName('MXMJXH').asstring, 0) + 1;
        if CDS_Main.FieldByName('MXUnit').AsString = '' then
          FieldByName('MXUnit').Value := 'Y';
        if XS.Text <> '' then
        begin
          if ComboBox2.Text = 'KG' then
          begin
            FieldByName('MJMAOZ').Value := strtofloatdef(KGQty.Text, 0) + strtofloatdef(PIZHONG.Text, 0);
            FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
            FieldByName('MXQty2').Value := Format('%.1f', [StrToFloatDef(KGQty.Text, 0) * StrToFloatDef(xs.Text, 0)]);
          end
          else
          begin

            FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);
            FieldByName('MXQty').Value := Format('%.1f', [StrToFloatDef(Qty.Text, 0) * StrToFloatDef(xs.Text, 0)]);
            FieldByName('MJMAOZ').Value := Format('%.1f', [StrToFloatDef(Qty.Text, 0) * StrToFloatDef(xs.Text, 0) + StrToFloatDef(PIZHONG.Text, 0)]);
//            FieldByName('MXUnit').Value := combobox2.Text;
          end;
        end
        else
        begin
          FieldByName('MJMAOZ').Value := strtofloatdef(KGQty.Text, 0) + strtofloatdef(PIZHONG.Text, 0);
          FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
          FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);

        end;

        Post;
      end;
      if CheckBox2.Checked = True then
      begin
        PrtData(Edit2.Text);
      end;
      Qty.Text := '';
      KGQTY.Text := '';
      KGQty.SetFocus;
    end
    else
    begin
      QTY.SetFocus;
    end;

  end;
//  if Key = #13 then
//  begin
//    CopyAddRowCDS(CDS_Main);
//    with CDS_Main do
//    begin
//      Edit;
//      FieldByName('MXGangHao').Value := gangno.Text;
//      FieldByName('MXID').Value := '';
//      FieldByName('CRNO').Value := '';
//      FieldByName('MXMJXH').Value := null;
//      if CDS_Main.FieldByName('MXUnit').AsString = '' then
//        FieldByName('MXUnit').Value := 'M';
//      if XS.Text <> '' then
//      begin
//        if ComboBox2.Text = 'KG' then
//        begin
//          FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
//          FieldByName('MXQty2').Value := Format('%.1f', [StrToFloatDef(KGQty.Text, 0) * StrToFloatDef(xs.Text, 0)]);
//        end
//        else
//        begin
//          FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);
//          FieldByName('MXQty').Value := Format('%.1f', [StrToFloatDef(Qty.Text, 0) * StrToFloatDef(xs.Text, 0)]);
//          FieldByName('MXUnit').Value := combobox2.Text;
//        end;
//      end
//      else
//      begin
//        FieldByName('MXQty').Value := strtofloatdef(KGQty.Text, 0);
//        FieldByName('MXQty2').Value := strtofloatdef(Qty.Text, 0);
//        if combobox2.Text <> 'KG' then
//        begin
//          FieldByName('MXUnit').Value := combobox2.Text;
//        end
//        else
//        begin
//          FieldByName('MXUnit').Value := 'M';
//        end;
//
//      end;
//
//      Post;
//    end;
//
//    Qty.Text := '';
//    KGQTY.Text := '';
//    Qty.SetFocus;
//  end;
end;

procedure TfrmCPCKMXLR.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('UPDATE JYOrder_Main     SET LABNAME=' + QuotedStr(LABNAME.Text));
    SQL.Add('where MAINID=(select ordMAINidrk from CK_SXPB_CR c where c.spid= ' + QuotedStr(Trim(fkeyNO)));
    sql.Add(')');

//      ShowMessage(SQL.Text);
    EXECSQL;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        PrtData(Edit2.Text);
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmCPCKMXLR.ToolButton5Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if LABNAME.Text = '' then
  begin
    application.MessageBox('未设置标签格式！', '提示信息', MB_ICONERROR);
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Print_JYLab ');
    SQL.Add(' @mjID=''' + Trim(Edit2.Text) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  ExportFtErpFile(Trim(labname.Text) + '.rmf', ADOQueryTmp);
  if Trim(labname.Text) <> '' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(labname.Text) + '.rmf'
  end
  else
  begin
    ExportFtErpFile('通用中文标签.rmf', ADOQueryTmp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用中文标签.rmf';
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['MXMJXH'] := CDS_MAIN.FIELDBYNAME('MXMJXH').ASSTRING;
    RMVariables['MXGangHao'] := CDS_MAIN.FIELDBYNAME('MXGangHao').ASSTRING;
    RMVariables['MXQty'] := CDS_MAIN.FIELDBYNAME('MXQty').ASSTRING;
    RMVariables['MXQTY2'] := CDS_MAIN.FIELDBYNAME('MXQTY2').ASSTRING;
    RMVariables['MXUnit'] := CDS_MAIN.FIELDBYNAME('MXUnit').ASSTRING;
    RMVariables['MXFM'] := CDS_MAIN.FIELDBYNAME('MXFM').ASSTRING;
    RMVariables['mxtt'] := CDS_MAIN.FIELDBYNAME('mxtt').ASSTRING;
    RMVariables['mxkh'] := CDS_MAIN.FIELDBYNAME('mxkh').ASSTRING;
    RMVariables['mxkuanno'] := CDS_MAIN.FIELDBYNAME('mxkuanno').ASSTRING;
    RMVariables['mxspec'] := CDS_MAIN.FIELDBYNAME('mxspec').ASSTRING;
    RMVariables['mjMAOZ'] := CDS_MAIN.FIELDBYNAME('MJMAOZ').ASSTRING;
    RMVariables['PIZHONG'] := CDS_MAIN.FIELDBYNAME('PIZHONG').ASSTRING;
    RM1.LoadFromFile(fPrintFile);

    RM1.DefaultCopies := strtointdef(trim(ComboBox3.Text), 1);

    RM1.showReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPCKMXLR.LABNAMEPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'LABNAME';
      flagname := '标签格式';
      if ShowModal = 1 then
      begin
        Self.LABNAME.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

