unit U_DJBDDRK_Edit;

interface

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

type
  TfrmDJBDDRK_Edit = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Main: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Panel1: TPanel;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    CRTime: TDateTimePicker;
    Label1: TLabel;
    Label8: TLabel;
    InoutNo: TEdit;
    Label2: TLabel;
    CRType: TComboBox;
    Label4: TLabel;
    FactoryName: TBtnEditA;
    ToolBar2: TToolBar;
    TBADD: TToolButton;
    TBdel: TToolButton;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    v2Column3: TcxGridDBColumn;
    ADOMX: TADOQuery;
    CDS_MX: TClientDataSet;
    DSMX: TDataSource;
    Panel3: TPanel;
    Label3: TLabel;
    QtyMX: TEdit;
    TBPrint: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    VC_MJqty1: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    v2Column5: TcxGridDBColumn;
    edtMJXH: TEdit;
    chkMJJH: TCheckBox;
    v1Column16: TcxGridDBColumn;
    KgQtyMX: TEdit;
    Label7: TLabel;
    Label9: TLabel;
    HSXS: TEdit;
    VC_MJMaoZ: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Label6: TLabel;
    edtMJQTY3: TEdit;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ZNote: TMemo;
    Label5: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure TBPrintClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBdelClick(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure edtMJXHKeyPress(Sender: TObject; var Key: Char);
    procedure chkMJJHClick(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N1Click(Sender: TObject);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveData(): Boolean;
    function YSData(Order_Main10: TClientDataSet): Boolean;
    procedure InitGrid();
    procedure initMJID();
    procedure PrintData(fMJID: string);
  public
    { Public declarations }
    fkeyNo, fckName: string;
    CopyFlag: Integer;
  end;

var
  frmDJBDDRK_Edit: TfrmDJBDDRK_Edit;

implementation

uses
  U_DataLink, U_RTFun, U_ClothContractListHZCX, U_ZDYHelp, U_ZdyAttachGYS,
  U_ProductOrderListSel, U_ZdyAttachCP, U_ZdyAttachment,
  U_ProductOrderNewList_New;

{$R *.dfm}
procedure TfrmDJBDDRK_Edit.PrintData(fMJID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(fMJID);
    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;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,C_CodeNameZW=(select Top 1 defnote3 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and Type=''CP'') ');
    sql.Add(',C_colorName=(select Top 1 DEFstr6 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and X.DEFstr3=A.C_Color and Type=''CP'') ');
    sql.Add(',factoryNameJZ=(select Top 1 zdyNameZ from KH_Zdy_Attachment X where X.ZdyName=A.factoryName and Type=''GYS'') ');
    sql.Add(' from MD_MJJY A');
    SQL.Add(' where A.MJID=''' + Trim(fMJID) + '''');
    Open;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用标签.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    RM2.DefaultCopies := 1;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示', 0);
  end;
end;

procedure TfrmDJBDDRK_Edit.initMJID();
begin
  try
    with ADOMX do
    begin
      close;
      sql.Clear;
      sql.Add('select * from MD_MJJY A');
      sql.Add('where APID=' + quotedstr(trim(CDS_Main.fieldByName('SPID').AsString)));
      open;
    end;
    SCreateCDS20(ADOMX, CDS_MX);
    SInitCDSData20(ADOMX, CDS_MX);
  except

  end;

//  if CopyFlag = 2 then
//  begin
//    with CDS_MX do
//    begin
//      Edit;
//      FieldByName('SPID').Value := '';
//      Post;
//    end;
//  end;

  if chkMJJH.Checked then
  begin
    if CDS_MX.IsEmpty then
      edtMJXH.Text := '1'
    else
      edtMJXH.Text := IntToStr(1 + CDS_MX.RecordCount);
  end
  else
  begin
    edtMJXH.Text := '';
  end;

end;

procedure TfrmDJBDDRK_Edit.InitGrid();
var
  fSPID: string;
begin

  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select *  from CK_XNMD_CR A');
      sql.Add('where SPID=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);

    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date := CDS_Main.fieldbyname('CRTime').AsDateTime;
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      factoryName.Text := trim(CDS_Main.fieldbyname('factoryName').AsString);
      ZNote.text := trim(CDS_Main.fieldbyname('ZNote').AsString);
    end;
  finally
  end;
  if CopyFlag = 1 then
  begin
    if GetLSNo(ADOQueryCmd, fSPID, 'DR', 'CK_XNMD_CR', 3, 1) = False then
    begin
      Application.MessageBox('取待检布入库编号失败!', '提示', 0);
      Exit;
    end;
    CRTime.Date := now();

    with CDS_Main do
    begin
      Edit;
      FieldByName('SPID').Value := fSPID;
      FieldByName('FZSPID').Value := '';
      FieldByName('FromSPID').Value := '';
      Post;
    end;
    fkeyNo := '';
  end;
end;

function TfrmDJBDDRK_Edit.YSData(Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, PriceUnit, OrderUnit, FComTaiTou, FromSubID, KHName, KHNo: string;
  fprice, huilv: double;
begin
  Result := False;
  fprice := 0;
  huilv := 0;
  PriceUnit := 'RMB';
  KHName := '';
  KHNo := '';
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub SET Huilv=isnull((select Huilv from JYOrderCon_Sub X where X.SubId=JYOrder_Sub.SOrddefstr10),1) where Huilv=0');
    execsql;
  end;
  with Order_Main10 do
  begin
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select A.*,fPrice=dbo.F_Get_Order_Price(A.MainID,A.subID),B.customerNoName,B.customerNo');
        sql.Add(' from JYOrder_Sub A  ');
        sql.Add(' inner join  JYOrder_main B on B.mainID=A.mainID  ');
        sql.Add(' Where subID=''' + Trim(Order_Main10.fieldbyname('ORDSubIdRK').AsString) + '''');
        Open;
      end;
      if not ADOQueryTemp.IsEmpty then
      begin
        PriceUnit := Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
        OrderUnit := Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
        fprice := ADOQueryTemp.fieldbyname('fPrice').AsFloat;
        huilv := ADOQueryTemp.fieldbyname('huilv').AsFloat;
        KHName := ADOQueryTemp.fieldbyname('customerNoName').AsString;
        KHNo := ADOQueryTemp.fieldbyname('CustomerNo').AsString;
      end;
      if huilv = 0 then
        huilv := 1;
      CRID := '0';
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR ');
        sql.Add(' where YFTypeId=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
        sql.Add(' and YFName=''客户退货'' ');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
        begin
          Application.MessageBox('取成品应收最大号失败!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value := Trim(YFID);
          FieldByName('YFTypeId').Value := Trim(Order_Main10.fieldbyname('SPID').AsString);
          FieldByName('CRID').Value := StrToInt(CRID);
          FieldByName('Filler').Value := Trim(DName);
          FieldByName('CRType').Value := '应收款登记';
          FieldByName('CRFlag').Value := '应收收';
          FieldByName('QtyFlag').Value := 1;
          FieldByName('FactoryNo').Value := Trim(KHNo);
          FieldByName('FactoryName').Value := Trim(KHName);
          FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CDS_Main.fieldbyname('CRTime').AsDateTime));
          FieldByName('YFType').Value := '自动生成';
          FieldByName('ps').Value := -1 * Order_Main10.fieldbyname('PiQty').AsFloat;
          FieldByName('Qty').Value := -1 * Order_Main10.fieldbyname('Qty').AsFloat;
          FieldByName('Price').Value := fprice;
          FieldByName('HuiLv').Value := huilv;
          FieldByName('BZType').Value := Trim(PriceUnit);
         // FieldByName('Money').Value:=-1*format('%.2f',[Order_Main10.fieldbyname('Qty').AsFloat*fprice]);
         // FieldByName('BBMoney').Value:=-1*format('%.2f',[Order_Main10.fieldbyname('Qty').AsFloat*fprice*huilv]);
          FieldByName('QtyUnit').Value := Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value := Trim(KHName);
          FieldByName('YFName').Value := '客户退货';
          FieldByName('MainId').Value := Trim(Order_Main10.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('subID').Value := Trim(Order_Main10.fieldbyname('ORDSubIdRK').AsString);
          FieldByName('OrderNO').Value := Trim(Order_Main10.fieldbyname('QCorderNo').AsString);
          FieldByName('P_CodeName').Value := Trim(Order_Main10.fieldbyname('SPName').AsString);
          FieldByName('P_Spec').Value := Trim(Order_Main10.fieldbyname('SPSpec').AsString);
          FieldByName('P_color').Value := Trim(Order_Main10.fieldbyname('SPColor').AsString);
          FieldByName('status').Value := '0';
          Post;
        end;
      end
      else
      begin
        YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID=''' + Trim(YFID) + '''');
        ExecSQL;
      end;

      //Next;
    end;
  end;
  Result := True;
end;

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

function TfrmDJBDDRK_Edit.SaveData(): Boolean;
var
  Maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not eof do
      begin
        Maxno := fieldbyName('SPID').AsString;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_XNMD_CR where SPID=''' + Trim(Maxno) + '''');
          Open;
          if IsEmpty then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(Maxno);
          FieldByName('FromSPID').Value := CDS_Main.fieldbyname('SPID').Value;
          FieldByName('ORDMainIdRK').Value := CDS_Main.fieldbyname('ORDMainIdRK').Value;
          FieldByName('ORDSubIdRK').Value := CDS_Main.fieldbyname('ORDSubIdRK').Value;
          FieldByName('QCorderNo').Value := CDS_Main.fieldbyname('QCorderNo').Value;
          FieldByName('CKName').Value := trim(fckName);
          FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTime.DateTime);
          FieldByName('CRType').Value := trim(CRType.Text);
          FieldByName('factoryName').Value := trim(factoryName.Text);
          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_XNMD_CR', 2);
          FieldByName('PiQty').Value := CDS_Main.fieldbyname('piQty').AsInteger;
          FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
          FieldByName('price').Value := CDS_Main.fieldbyname('price').AsFloat;
          FieldByName('money').Value := CDS_Main.fieldbyname('money').AsFloat;
          FieldByName('ZNote').Value := trim(ZNote.Text);
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_XNMD_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_XNMD_CR A where A.FZSPID=CK_XNMD_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_XNMD_CR A where A.FZSPID=CK_XNMD_CR.SPID)');
          sql.Add(' where SPID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;

//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' Update MD_MJJY SET CKName=B.CKName,factoryName=B.FactoryName,BatchNo=B.BatchNO,C_CodeName=B.SPName,C_Color=B.SPColor,');
//          sql.Add('  MJTypeOther=B.QtyUnit,MJStr1=B.QtyUnit,MJStr2=''未入库''');
//          sql.Add('  ,MJFK=B.SPMF,MJSJKZ=B.SPKZ,suolv=B.suolv');
//          sql.Add('  ,C_Code=B.SPCode,C_Spec=B.SPSpec,C_CF=B.SPCF');
//          sql.Add('  ,RCgangNo=B.RCGangNo,BCgangNo=B.RCGangNo,Filler=B.Filler,Filltime=B.Filltime ');
//          sql.Add(' FROM MD_MJJY A   ');
//          sql.Add(' inner join CK_XNMD_CR B on B.SPID=A.APID ');
//          sql.Add(' where A.APID=''' + Trim(Maxno) + '''');
//          ExecSQL;
//        end;
//        with ADOQueryTemp do
//        begin
//          close;
//          sql.Clear;
//          sql.Add('select A.SPID ');
//          sql.Add('from CK_XNMD_CR A');
//          sql.Add('inner join CK_XNMD_CR B on B.RCGangNo=A.RCgangNo and B.Qty=A.Qty and B.SPID<>A.SPID');
//          sql.Add('where A.SPID=''' + Trim(Maxno) + ''' and isnull(A.RCGangNo,'''')<>'''' ');
//          open;
//        end;
//        if not ADOQueryTemp.IsEmpty then
//        begin
//          ADOQueryCmd.Connection.RollbackTrans;
//          CDS_Main.EnableControls;
//          Application.MessageBox('相同缸号相同数量不能保存！', '提示', 0);
//          exit;
//        end;
//        if Trim(CDS_Main.fieldbyname('CRType').AsString) = '客户退货' then
//        begin
//          if not YSData(CDS_Main) then
//          begin
//            ADOQueryCmd.Connection.RollbackTrans;
//            CDS_Main.EnableControls;
//            Application.MessageBox('保存异常!', '提示', 0);
//            exit;
//          end;
//        end;

        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    TBPrint.Enabled := true;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmDJBDDRK_Edit.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + '1', Tv1, '待检布仓库');
  WriteCxGrid(Trim(self.Caption) + 'Tv22', Tv2, '待检布仓库');
  Close;
end;

procedure TfrmDJBDDRK_Edit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(self.Caption + '1', Tv1, '待检布仓库');
  ReadCxGrid(Trim(self.Caption) + 'Tv22', Tv2, '待检布仓库');
  InitGrid();
  initMJID();
end;

procedure TfrmDJBDDRK_Edit.TBSaveClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if trim(factoryName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  if trim(CRType.Text) = '' then
  begin
    Application.MessageBox('类型不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('SPName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Main.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('码单总数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('SPName', '', []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if not CDS_MX.IsEmpty then
  begin
    if tv2.DataController.Summary.FooterSummaryValues[0] <> CDS_Main.fieldByName('PiQty').AsInteger then
    begin
      Application.MessageBox('汇总和明细匹数不一致!', '提示', 0);
      Exit;
    end;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmDJBDDRK_Edit.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BPName';
      flagname := '布匹名称';
      fnote := True;
      V1Name.Caption := '中文品名';
      V1Note.Caption := '英文品名';
      if ShowModal = 1 then
      begin
        with Self.CDS_Main do
        begin
          Edit;
          FieldByName('SPName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//
//    try
//      frmYCLViewSel := TfrmYCLViewSel.Create(Application);
//      with frmYCLViewSel do
//      begin
//        if ShowModal = 1 then
//        begin
//
//          with Self.CDS_Main do
//          begin
//            Edit;
//            FieldByName('SPName').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPName').AsString);
//            FieldByName('SPSpec').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPSpec').AsString);
//            FieldByName('SPColor').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPColor').AsString);
//            FieldByName('SPCF').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPCF').AsString);
//            FieldByName('SPMF').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPMF').AsString);
//            FieldByName('SPKZ').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPKZ').AsString);
//            FieldByName('SPCode').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('SPSpec').AsString);
//            FieldByName('CPID').Value := Trim(frmYCLViewSel.CDS_HZ.fieldbyname('CPId').AsString);
//          end;
//
//        end;
//      end;
//    finally
//      frmYCLViewSel.Free;
//    end;

end;

procedure TfrmDJBDDRK_Edit.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  fieldname: string;
begin
  fieldname := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value := TcxTextEdit(Sender).EditingText;
    FieldByName('Money').Value := strtofloat(format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmDJBDDRK_Edit.FormDestroy(Sender: TObject);
begin
  frmDJBDDRK_Edit := nil;
end;

procedure TfrmDJBDDRK_Edit.FormCreate(Sender: TObject);
begin
  Panel2.Align := alclient;
  CRTime.DateTime := now();
end;

procedure TfrmDJBDDRK_Edit.FactoryNameBtnClick(Sender: TObject);
begin
  if Trim(CRType.text) = '销售出库' then
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        if ShowModal = 1 then
        begin
          FactoryName.Text := Trim(CDS_HZ.fieldbyname('KHNameJC').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end
  else
  begin
    frmZdyAttachGYS := TfrmZdyAttachGYS.create(self);
    with frmZdyAttachGYS do
    begin
      if showmodal = 1 then
      begin
        factoryName.Text := CDS_Hz.fieldbyname('KHNameJC').asstring;
      end;
      free;
    end;
  end;

end;

procedure TfrmDJBDDRK_Edit.TBADDClick(Sender: TObject);
var
  fSPID: string;
begin
  if not CDS_MX.IsEmpty then
  begin
    if tv2.DataController.Summary.FooterSummaryValues[0] <> CDS_Main.fieldByName('PiQty').AsInteger then
    begin
      Application.MessageBox('汇总和明细匹数不一致!', '提示', 0);
      Exit;
    end;
  end;
  CopyAddRowCDS(CDS_Main);
  if GetLSNo(ADOQueryCmd, fSPID, 'DR', 'CK_XNMD_CR', 3, 1) = False then
  begin
    Application.MessageBox('取待检布入库编号失败!', '提示', 0);
    Exit;
  end;
  CDS_Main.edit;
  CDS_Main.FieldByName('SPID').Value := fSPID;
  CDS_Main.FieldByName('xs').Value := 0;
  CDS_Main.FieldByName('PiQty').Value := 0;
  CDS_Main.FieldByName('Qty').Value := 0;
  CDS_Main.FieldByName('KgQty').Value := 0;
//  CDS_Main.FieldByName('QtyUnit').Value := 'M';
  CDS_Main.FieldByName('RCgangNo').Value := '';
  CDS_Main.Post;
end;

procedure TfrmDJBDDRK_Edit.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  fMJID: string;
  MMJXH: Integer;
begin
  if Key = #13 then
  begin

    if CDS_Main.IsEmpty then
    begin
      application.MessageBox('产品信息没有输入，不能输入明细信息！', '提示信息', MB_ICONERROR);
      exit;
    end;

    if Trim(edtMJXH.Text) = '' then
    begin
      application.MessageBox('请输入卷号！', '提示信息', MB_ICONERROR);
      exit;
    end;

    MMJXH := StrToIntDef(edtMJXH.Text, 1);

    if chkMJJH.Checked then
    begin
      edtMJXH.Text := IntToStr(MMJXH + 1);
    end
    else
    begin
      edtMJXH.Text := '';
    end;

    if GetLSNo(ADOQueryCmd, fMJID, '', 'APTM', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select *  from MD_MJJY A');
        sql.Add('where 1=2');
        open;
        append;
        Fieldbyname('mainID').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
        Fieldbyname('subID').Value := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
//        FieldByName('CloOrderNo').Value := Trim(CDS_Main.fieldbyname('QCorderNo').AsString);
        Fieldbyname('MJID').Value := trim(fMJID);
        Fieldbyname('MJXH').Value := MMJXH;

        if TEdit(Sender).Name = 'KgQtyMX' then
        begin
          Fieldbyname('MJMaoZ').Value := StrToFloatDef(KgQtyMX.Text, 0);
          Fieldbyname('MJQty3').Value := StrToFloatDef(edtMJQTY3.Text, 0);
          Fieldbyname('MJQty4').Value := StrToFloatDef(KgQtyMX.Text, 0) - StrToFloatDef(edtMJQTY3.Text, 0);
//          Fieldbyname('HSXS').Value := StrToFloatDef(HSXS.Text, 0);
          Fieldbyname('MJlen').Value := StrToFloatDef(QtyMX.Text, 0);
//          Fieldbyname('MJlen').Value := RoundFloat((StrToFloatDef(KgQtyMX.Text, 0)) * StrToFloatDef(HSXS.Text, 0), 2);
        end
        else
        begin
          Fieldbyname('MJlen').Value := strtofloatdef(QtyMX.Text, 0);
        end;
        Fieldbyname('APID').Value := trim(CDS_Main.fieldbyname('SPID').AsString);
        Fieldbyname('MJType').Value := '正品';
        Fieldbyname('MJStr2').Value := '未入库';
        post;
      end;

      with CDS_MX do
      begin
        append;
        Fieldbyname('MJID').Value := trim(fMJID);
        Fieldbyname('MJXH').Value := MMJXH;
        Fieldbyname('APID').Value := trim(CDS_Main.fieldbyname('SPID').AsString);

        if TEdit(Sender).Name = 'KgQtyMX' then
        begin
          Fieldbyname('MJMaoZ').Value := StrToFloatDef(KgQtyMX.Text, 0);
          Fieldbyname('MJQty3').Value := StrToFloatDef(edtMJQTY3.Text, 0);
          Fieldbyname('MJQty4').Value := StrToFloatDef(KgQtyMX.Text, 0) - StrToFloatDef(edtMJQTY3.Text, 0);
//          Fieldbyname('HSXS').Value := StrToFloatDef(HSXS.Text, 0);
          Fieldbyname('MJlen').Value := StrToFloatDef(QtyMX.Text, 0);
        //  Fieldbyname('MJlen').Value := RoundFloat((StrToFloatDef(KgQtyMX.Text, 0) - StrToFloatDef(edtMJQTY3.Text, 0)) * StrToFloatDef(HSXS.Text, 0), 2);
        end
        else
        begin
          Fieldbyname('MJlen').Value := strtofloatdef(QtyMX.Text, 0);
        end;
        post;
      end;

      with CDS_main do
      begin
        edit;
        if CDS_MX.IsEmpty then
        begin
          Fieldbyname('PiQty').Value := 0;
          Fieldbyname('Qty').Value := 0;
          Fieldbyname('KgQty').Value := 0;
        end
        else
        begin
          Fieldbyname('PiQty').Value := tv2.DataController.Summary.FooterSummaryValues[0];
          Fieldbyname('Qty').Value := tv2.DataController.Summary.FooterSummaryValues[1];
          Fieldbyname('KgQty').Value := tv2.DataController.Summary.FooterSummaryValues[2];
        end;

        post;
      end;
      KgQtyMX.Text := '';
      QtyMX.Text := '';
      if not chkMJJH.Checked then
        edtMJXH.SetFocus;
    except
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from MD_MJJY where MJID=''' + trim(fMJID) + '''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end;
end;

procedure TfrmDJBDDRK_Edit.TBPrintClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MJID from MD_MJJY');
    sql.Add('where APID=''' + trim(CDS_Main.fieldbyName('SPID').AsString) + ''' ');
    open;
  end;
  if ADOQueryTemp.IsEmpty then
    exit;
  while not ADOQueryTemp.Eof do
  begin
    sleep(200);
    Printdata(ADOQueryTemp.fieldbyName('MJID').AsString);
    ADOQueryTemp.next;
  end;
end;

procedure TfrmDJBDDRK_Edit.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
//  HSXS.Text := trim(CDS_Main.fieldByName('XS').AsString);
//  edtXSWS.Text := trim(CDS_Main.fieldByName('XSWS').AsString);
  initMJID();

end;

procedure TfrmDJBDDRK_Edit.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MX.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from MD_MJJY where MJID=''' + trim(CDS_MX.fieldbyName('MJID').AsString) + '''');
    open;
  end;
  if trim(ADOQueryTemp.fieldbyName('MJStr2').AsString) <> '未入库' then
  begin
    application.MessageBox('此条码已经入库，不能删除！', '提示信息', MB_ICONERROR);
    exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from MD_MJJY where MJID=''' + trim(CDS_MX.fieldbyName('MJID').AsString) + '''');
    execsql;
  end;
  CDS_MX.Delete;
  with CDS_main do
  begin
    edit;
    if CDS_MX.IsEmpty then
    begin
      Fieldbyname('PiQty').Value := 0;
      Fieldbyname('Qty').Value := 0;
      Fieldbyname('KgQty').Value := 0;
    end
    else
    begin
      Fieldbyname('PiQty').Value := tv2.DataController.Summary.FooterSummaryValues[0];
      Fieldbyname('Qty').Value := tv2.DataController.Summary.FooterSummaryValues[1];
      Fieldbyname('KgQty').Value := tv2.DataController.Summary.FooterSummaryValues[2];
    end;
    post;
  end;
end;

procedure TfrmDJBDDRK_Edit.TBdelClick(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select  * from MD_MJJY where isnull(APID,'''')=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('已检验不能删除数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_money_CR where YFTypeId=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有应付款记录，不能删除!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from CK_XNMD_CR where SPID=''' + trim(CDS_Main.fieldbyName('SPID').AsString) + '''');
    execsql;
  end;
  CDS_Main.Delete;
end;

procedure TfrmDJBDDRK_Edit.v1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if trim(mvalue) = '' then
    exit;

  with CDS_Main do
  begin
    Edit;
    FieldByName('batchNo').Value := Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
{
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select top 1 * from CK_XNMD_CR where batchNo='''+trim(CDS_Main.fieldbyName('batchNo').AsString)+'''   ');
    open;
    IF not IsEmpty then
    begin
      with CDS_Main do
      begin
        Edit;
        FieldByName('SPName').Value:=Trim(ADOQueryTemp.fieldbyName('SPName').AsString);
        Post;
      end;
    end;
  end; }
end;

procedure TfrmDJBDDRK_Edit.edtMJXHKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    QtyMX.SetFocus;
end;

procedure TfrmDJBDDRK_Edit.chkMJJHClick(Sender: TObject);
begin
  if chkMJJH.Checked then
  begin
    if CDS_MX.IsEmpty then
      edtMJXH.Text := '1'
    else
      edtMJXH.Text := IntToStr(1 + CDS_MX.RecordCount);
  end
  else
  begin
    edtMJXH.Text := '';
  end;
end;

procedure TfrmDJBDDRK_Edit.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' update MD_MJJY set ' + FFieldName + ' = ' + mvalue);
    sql.Add(' where  MJID= ' + quotedstr(Trim(CDS_MX.FieldByName('MJID').Asstring)));
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();

end;

procedure TfrmDJBDDRK_Edit.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from MD_MJJY where MJID=''' + trim(CDS_MX.fieldbyName('MJID').AsString) + '''');
    open;
  end;
  if trim(ADOQueryTemp.fieldbyName('MJStr2').AsString) <> '未入库' then
  begin
    VC_MJqty1.Options.Editing := False;
  end
  else
  begin
    VC_MJqty1.Options.Editing := True;
  end;
end;

procedure TfrmDJBDDRK_Edit.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' update MD_MJJY set ' + FFieldName + ' = ' + QuotedStr(mvalue));
    sql.Add(' where  MJID= ' + quotedstr(Trim(CDS_MX.FieldByName('MJID').Asstring)));
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();

end;

procedure TfrmDJBDDRK_Edit.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SPColor';
      flagname := '颜色';

      if ShowModal = 1 then
      begin
        with Self.CDS_Main do
        begin
          Edit;
          FieldByName('SPColor').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBDDRK_Edit.N1Click(Sender: TObject);
var
  str: string;
begin
  str := '单号';
  if InputQuery('转单', '输入单号', str) then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select SPID from CK_XNMD_CR where isnull(SPID,'''')=''' + Trim(str) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('单号不存在!', '提示', 0);
      Exit;
    end;

    while CDS_MX.Locate('SSel', True, []) do
    begin

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update MD_MJJY  set APID=' + Quotedstr(Trim(str)));
        sql.Add(' where MJID=''' + Trim(CDS_MX.fieldbyname('MJID').AsString) + '''');
        ExecSQL;
      end;
      CDS_MX.Delete;
    end;
  end;
end;

procedure TfrmDJBDDRK_Edit.v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderNewList_New := TfrmProductOrderNewList_New.Create(Application);
    with frmProductOrderNewList_New do
    begin
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('QCorderNo').Value := Trim(Order_Main.fieldbyname('MLOrderNo').AsString);
        Self.CDS_Main.FieldByName('MainID').Value := Trim(Order_Main.fieldbyname('NewMLID').AsString);
        Self.CDS_Main.FieldByName('SubID').Value := Trim(Order_Main.fieldbyname('NewSubid').AsString);
        Self.CDS_Main.FieldByName('SPName').Value := Trim(Order_Main.fieldbyname('MLCodeName').AsString);
        Self.CDS_Main.FieldByName('SPColor').Value := Trim(Order_Main.fieldbyname('MLColor').AsString);
        Self.CDS_Main.FieldByName('SPColorNo').Value := Trim(Order_Main.fieldbyname('MLColorNo').AsString);
        Self.CDS_Main.FieldByName('PiQty').Value := Order_Main.fieldbyname('PS').Value;
        Self.CDS_Main.FieldByName('Qty').Value := Order_Main.fieldbyname('Qty').Value;
      end;
    end;
  finally
    frmProductOrderNewList_New.Free;
  end;
end;

end.

