unit U_PieceSampleRk;

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, cxTextEdit, BtnEdit,
  cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmPieceSampleRk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1P_Code: TcxGridDBColumn;
    InoutNo: TEdit;
    Label8: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    FactoryName: TBtnEditA;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label3: TLabel;
    fromFactoryName: TBtnEditA;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    DS_YPXZ: TDataSource;
    CDS_YPXZ: TClientDataSet;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    V4Column3: TcxGridDBColumn;
    V4Column2: TcxGridDBColumn;
    V4Column1: TcxGridDBColumn;
    V4Column5: TcxGridDBColumn;
    V4Column4: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    Panel4: TPanel;
    Panel5: TPanel;
    Label15: TLabel;
    CYNO: TEdit;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    v1Column5: TcxGridDBColumn;
    lbl1: TLabel;
    btn1: TToolButton;
    cv1Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    yptype: TBtnEditA;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_ColorPropertiesEditValueChanged(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure fromFactoryNameBtnClick(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure TV4DblClick(Sender: TObject);
    procedure TV4KeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure cv1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure yptypeBtnClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure GetYP();
    function savedate(): Boolean;
    { Private declarations }
  public
    fkeyNO, fCKName: string;
    FType: integer;
    canshu1: string;

    { Public declarations }
  end;

var
  frmPieceSampleRk: TfrmPieceSampleRk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_ZdyAttachGYS,
  U_ClothGMListSel, U_CPManageSel, U_PieceSampleCKlistsel, U_ClothSYarnCListSel;
{$R *.dfm}

procedure TfrmPieceSampleRk.GetYP();
var
  FMJID: string;
begin
  FMJID := '';
  if GetLSNo(self.ADOQueryCmd, FMJID, 'MJ', 'CK_Sample_CR', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('P_Code;P_COLOR', VarArrayOf([trim(CDS_YPXZ.fieldbyname('CYNo').AsString), trim(CDS_YPXZ.fieldbyname('CYColor').AsString)]), []) then
  begin
    if Application.MessageBox('已存在该样品，是否继续添加？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;

  CDS_Main.append;
  CDS_Main.FieldByName('P_Code').Value := trim(CDS_YPXZ.fieldbyname('CYNo').AsString);
  CDS_Main.FieldByName('P_CodeName').Value := trim(CDS_YPXZ.fieldbyname('CYName').AsString);
  CDS_Main.FieldByName('P_spec').Value := trim(CDS_YPXZ.fieldbyname('CYSpec').AsString);
  CDS_Main.FieldByName('P_cf').Value := trim(CDS_YPXZ.fieldbyname('CYCF').AsString);
  CDS_Main.FieldByName('P_MF').Value := trim(CDS_YPXZ.fieldbyname('CYMF').AsString);
  CDS_Main.FieldByName('P_KZ').Value := trim(CDS_YPXZ.fieldbyname('CYKZ').AsString);
  CDS_Main.FieldByName('P_color').Value := trim(CDS_YPXZ.fieldbyname('CYColor').AsString);

  CDS_Main.FieldbyName('CPType').Value := '正品';
  CDS_Main.FieldbyName('QtyUnit').Value := 'M';
  CDS_Main.FieldbyName('Rollnum').Value := 0;
  CDS_Main.FieldbyName('Price').Value := 0;
  CDS_Main.FieldbyName('qty').Value := 0;
  CDS_Main.FieldbyName('Money').Value := 0;
  CDS_Main.FieldbyName('MJID').Value := FMJID;
  CDS_Main.Post;

  cxGrid4.Visible := False;
end;

function TfrmPieceSampleRk.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 * from CK_Sample_CR');
          sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'SPR', 'CK_Sample_CR', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
          ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(SGetServerDateTime(adoqueryTmp));
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('CRNO').AsString);
          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;

        //保存主表
        ADOQueryCmd.FieldByName('InOutNo').Value := trim(InOutNo.Text);
        ADOQueryCmd.FieldByName('FactoryName').Value := trim(FactoryName.Text);
        ADOQueryCmd.FieldByName('fromFactoryName').Value := trim(fromFactoryName.Text);
        ADOQueryCmd.FieldByName('CRNO').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CKName').Value := trim(fCKName);
//        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('yptype').Value := yptype.Text;
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('CRQtyFlag').Value := 1;

        //保存明细
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_Sample_CR', 0);
        if trim(CRType.Text) <> '客户退样' then
        begin
          ADOQueryCmd.FieldByName('fromFactoryName').Value := trim(fromFactoryName.Text);
        end;

        //保存隐藏ID
        ADOQueryCmd.FieldByName('mainID').Value := trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(fieldbyname('subID').AsString);

//        ADOQueryCmd.FieldByName('ORDMainIdRK').Value := trim(fieldbyname('ORDMainIdRK').AsString);
//        ADOQueryCmd.FieldByName('ORDSubIdRK').Value := trim(fieldbyname('ORDSubIdRK').AsString);

        ADOQueryCmd.fieldbyname('JGId').Value := trim(fieldbyname('JGId').AsString);
        ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('Rollnum').Value := fieldbyname('Rollnum').AsInteger;
        ADOQueryCmd.Post;

        //插入库存
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_SampleKc ' + quotedstr(trim(CDS_Main.Fieldbyname('MJID').AsString)));
          Open;
        end;

        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
          Exit;
        end;

        CDS_Main.Edit;
        CDS_Main.FieldByName('CRNO').Value := Trim(MaxNo);
        CDS_Main.Post;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmPieceSampleRk.InitGrid();
var
  i: Integer;
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* ');
      SQL.Add(' from CK_Sample_CR A');
      sql.Add('where  A.inoutno=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, 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));
      inoutNo.Text := CDS_Main.fieldbyname('inoutNo').asstring;
      FactoryName.Text := trim(CDS_Main.fieldbyname('FactoryName').AsString);
      fromFactoryName.Text := trim(CDS_Main.fieldbyname('fromFactoryName').AsString);
      yptype.Text := trim(CDS_Main.fieldbyname('yptype').AsString);
//      yptype.ItemIndex := yptype.Items.IndexOf(trim(CDS_Main.fieldbyname('yptype').AsString));
    end;
  finally
  end;
end;

procedure TfrmPieceSampleRk.FormDestroy(Sender: TObject);
begin
  frmPieceSampleRk := nil;
  writeCxGrid(trim(self.Caption), Tv1, '样布入库');
end;

procedure TfrmPieceSampleRk.FormCreate(Sender: TObject);
begin
  CRTime.Date := SGetServerDate10(ADOQueryTmp);
end;

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

procedure TfrmPieceSampleRk.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption), Tv1, '样布入库');

  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'YRK', 'CK_Sample_CR', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    inoutNo.Text := Trim(fkeyNo);
    if yptype.Text = '' then
    begin
      yptype.Text := '染色米样';
    end;

  end;
  if fType = 1 then
  begin
    if CRType.Text = '客户退样' then
    begin
      fromFactoryName.Text := '';
    end;
  end;
end;

procedure TfrmPieceSampleRk.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  CRTime.SetFocus;
//  if trim(CRType.Text) = '' then
//  begin
//    application.MessageBox('入库类型不能为空！', '提示信息', 0);
//    exit;
//  end;
//  if trim(yptype.Text) = '' then
//  begin
//    application.MessageBox('类型不能为空！', '提示信息', 0);
//    exit;
//  end;
  if cds_main.Locate('P_COLOR', '', []) then
  begin
    application.MessageBox('颜色不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('P_COLOR', NULL, []) then
  begin
    application.MessageBox('颜色不能为空！', '提示信息', 0);
    exit;
  end;

  if cds_main.Locate('kuwei', '', []) then
  begin
    application.MessageBox('库位不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('kuwei', NULL, []) then
  begin
    application.MessageBox('库位不能为空！', '提示信息', 0);
    exit;
  end;

//  if cds_main.Locate('orderNo', '', []) then
//  begin
//    application.MessageBox('生产通知单号不能为空！', '提示信息', 0);
//    exit;
//  end;
//  if cds_main.Locate('orderNo', NULL, []) then
//  begin
//    application.MessageBox('生产通知单号不能为空！', '提示信息', 0);
//    exit;
//  end;

  if cds_main.Locate('P_Code', NULL, []) then
  begin
    application.MessageBox('产品编号不能为空！', '提示信息', 0);
    exit;
  end;
//  if cds_main.Locate('P_CodeName', '', []) then
//  begin
//    application.MessageBox('产品名称不能为空！', '提示信息', 0);
//    exit;
//  end;
//  if cds_main.Locate('P_CodeName', NULL, []) then
//  begin
//    application.MessageBox('产品名称不能为空！', '提示信息', 0);
//    exit;
//  end;
  if cds_main.Locate('qty', NULL, []) then
  begin
    application.MessageBox('数量不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('qty', 0, []) then
  begin
    application.MessageBox('数量不能为0！', '提示信息', 0);
    exit;
  end;

  if cds_main.Locate('QtyUnit', '', []) then
  begin
    application.MessageBox('数量单位不能为空！', '提示信息', 0);
    exit;
  end;
  if cds_main.Locate('QtyUnit', NULL, []) then
  begin
    application.MessageBox('数量单位不能为空！', '提示信息', 0);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmPieceSampleRk.ToolButton1Click(Sender: TObject);
var
  FMJID: string;
begin
  FMJID := '';
  frmClothSYarnCListSel := TfrmClothSYarnCListSel.Create(self);
  with frmClothSYarnCListSel do
  begin
//    ftype := GSTaiTou.Text;
//    OrderNo.Text := self.FOrderno;
    if showmodal = 1 then
    begin
      while Order_Main.Locate('SSel', true, []) do
      begin
        if GetLSNo(self.ADOQueryCmd, FMJID, 'MJ', 'CK_Sample_CR', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        Self.CDS_Main.Append;
        Self.CDS_Main.fieldbyname('Orderno').Value := Order_Main.fieldbyname('Orderno').asstring;
        Self.CDS_Main.fieldbyname('mainid').Value := Order_Main.fieldbyname('MainId').asstring;
        Self.CDS_Main.fieldbyname('subid').Value := Order_Main.fieldbyname('SubId').asstring;

        Self.CDS_Main.fieldbyname('P_Code').Value := Order_Main.fieldbyname('MPRTCode').asstring;
        Self.CDS_Main.fieldbyname('P_CodeName').Value := Order_Main.fieldbyname('MPRTCodeName').asstring;
        Self.CDS_Main.fieldbyname('P_SPEC').Value := Order_Main.fieldbyname('MPRTSPEC').asstring;
        Self.CDS_Main.fieldbyname('P_KZ').Value := Order_Main.fieldbyname('MPRTKZ').asstring;
        Self.CDS_Main.fieldbyname('P_MF').Value := Order_Main.fieldbyname('MPRTMF').asstring;
        Self.CDS_Main.fieldbyname('P_Color').Value := Order_Main.fieldbyname('PRTColor').asstring;
        Self.CDS_Main.fieldbyname('P_eColor').Value := Order_Main.fieldbyname('PRTeColor').asstring;

        Self.CDS_Main.fieldbyname('P_CF').Value := Order_Main.fieldbyname('MPRTCF').asstring;
        Self.CDS_Main.fieldbyname('P_HX').Value := Order_Main.fieldbyname('PRTHX').asstring;
        Self.CDS_Main.FieldbyName('CPType').Value := '正品';
        Self.CDS_Main.FieldbyName('QtyUnit').Value := 'M';
        Self.CDS_Main.FieldbyName('Rollnum').Value := 0;
        Self.CDS_Main.FieldbyName('Price').Value := 0;
        Self.CDS_Main.FieldbyName('qty').Value := 0;
        Self.CDS_Main.FieldbyName('XSKCqty').Value := 0;
        Self.CDS_Main.FieldbyName('Money').Value := 0;
        Self.CDS_Main.FieldbyName('MJID').Value := FMJID;
        Self.CDS_Main.Post;
        Order_Main.Delete;
      end;
      Next;
    end;
    free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmPieceSampleRk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Trim(CDS_Main.FieldByName('CRNO').AsString) = '' then
  begin
    CDS_Main.Delete;
  end
  else
  begin
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update CK_Sample_CR set Valid=''N'' where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Up_SampleKc ' + quotedstr(Trim(CDS_Main.FieldByName('MJID').AsString)));
      Open;
    end;
    if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
    begin
      CDS_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库数量超出库存数量!', '提示', 0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  end;
end;

procedure TfrmPieceSampleRk.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(trim(self.Caption), Tv1, '样品仓库');
  close;
end;

procedure TfrmPieceSampleRk.v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      MainType := Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  FKuWei: string;
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      self.CDS_Main.Edit;
      self.CDS_Main.fieldbyname('P_Code').Value := CDS_Main.fieldbyname('CYNo').asstring;
      self.CDS_Main.fieldbyname('P_CodeName').Value := CDS_Main.fieldbyname('CYName').asstring;
      self.CDS_Main.fieldbyname('P_SPEC').Value := CDS_Main.fieldbyname('CYSpec').asstring;
      self.CDS_Main.fieldbyname('P_CF').Value := CDS_Main.fieldbyname('CYCF').asstring;
      if Trim(self.CDS_Main.fieldbyname('P_Color').AsString) = '' then
        self.CDS_Main.fieldbyname('P_Color').Value := CDS_Main.fieldbyname('CYColor').asstring;
      self.CDS_Main.fieldbyname('P_HX').Value := CDS_Main.fieldbyname('CYHX').asstring;
      self.CDS_Main.fieldbyname('P_MF').Value := CDS_Main.fieldbyname('CYMF').asstring;
      self.CDS_Main.fieldbyname('P_KZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;
      self.CDS_Main.Post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.v1P_ColorPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
 { mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;



  with CDS_Main do
  begin
    Edit;
    FieldByName('P_Color').Value:=Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();  }
end;

procedure TfrmPieceSampleRk.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmClothGMListSel := TfrmClothGMListSel.Create(Application);
  with frmClothGMListSel do
  begin
    if showmodal = 1 then
    begin
      CDS_Main.Edit;
      CDS_Main.fieldbyname('OrderNo').Value := trim(Order_Main.fieldbyname('OrderNo').AsString);
      CDS_Main.fieldbyname('MainID').Value := trim(Order_Main.fieldbyname('MainID').AsString);
      CDS_Main.fieldbyname('subID').Value := trim(Order_Main.fieldbyname('subID').AsString);
      CDS_Main.fieldbyname('P_Code').Value := trim(Order_Main.fieldbyname('MPRTCode').AsString);
      CDS_Main.fieldbyname('P_CodeName').Value := trim(Order_Main.fieldbyname('MPRTCodeName').AsString);
      CDS_Main.fieldbyname('P_SPEC').Value := trim(Order_Main.fieldbyname('MPRTSpec').AsString);
      CDS_Main.fieldbyname('P_Color').Value := trim(Order_Main.fieldbyname('PRTColor').AsString);
      CDS_Main.fieldbyname('P_MF').Value := trim(Order_Main.fieldbyname('MPRTMF').AsString);
      CDS_Main.fieldbyname('P_KZ').Value := trim(Order_Main.fieldbyname('MPRTKZ').AsString);
      CDS_Main.Post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CPProperty';
      flagname := '面料属性';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('CPProperty').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPieceSampleRk.FactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

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

procedure TfrmPieceSampleRk.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'mykuwei';
      flagname := '库位';

      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('kuwei').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.fromFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        fromFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmPieceSampleRk.CYNOKeyPress(Sender: TObject; var Key: Char);
var
  FMJID: string;
begin

  if Key = #13 then
  begin
    if Length(Trim(CYNO.Text)) < 4 then
      Exit;
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select  * from CP_YDang where CYID=' + quotedstr(trim(CYNO.Text)));
      sql.Add('or CYNO like' + quotedstr('%' + trim(CYNO.Text) + '%'));
      open;
    end;
    if ADOQueryTmp.IsEmpty then
    begin
      application.MessageBox('此编号错误！', '提示信息', MB_ICONERROR);
      CYNO.Text := '';
    end
    else
    begin
      if ADOQueryTmp.RecordCount = 1 then
      begin

        if CDS_Main.Locate('P_Code;P_COLOR', VarArrayOf([trim(ADOQueryTmp.fieldbyname('CYNo').AsString), trim(ADOQueryTmp.fieldbyname('CYColor').AsString)]), []) then
        begin
          if Application.MessageBox('已存在该样品，是否继续添加？', '提示', 32 + 4) <> IDYES then
            Exit;
        end;
        FMJID := '';
        if GetLSNo(self.ADOQueryCmd, FMJID, 'MJ', 'CK_Sample_CR', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        CDS_Main.append;
        CDS_Main.FieldByName('P_Code').Value := trim(ADOQueryTmp.fieldbyname('CYNo').AsString);
        CDS_Main.FieldByName('P_CodeName').Value := trim(ADOQueryTmp.fieldbyname('CYName').AsString);
        CDS_Main.FieldByName('P_spec').Value := trim(ADOQueryTmp.fieldbyname('CYSpec').AsString);
        CDS_Main.FieldByName('P_cf').Value := trim(ADOQueryTmp.fieldbyname('CYCF').AsString);
        CDS_Main.FieldByName('P_MF').Value := trim(ADOQueryTmp.fieldbyname('CYMF').AsString);
        CDS_Main.FieldByName('P_KZ').Value := trim(ADOQueryTmp.fieldbyname('CYKZ').AsString);
        CDS_Main.FieldByName('P_color').Value := trim(ADOQueryTmp.fieldbyname('CYColor').AsString);
        CDS_Main.FieldbyName('CPType').Value := '正品';
        CDS_Main.FieldbyName('QtyUnit').Value := 'M';
        CDS_Main.FieldbyName('Rollnum').Value := 0;
        CDS_Main.FieldbyName('Price').Value := 0;
        CDS_Main.FieldbyName('qty').Value := 0;
        CDS_Main.FieldbyName('Money').Value := 0;
        CDS_Main.FieldbyName('MJID').Value := FMJID;
        CDS_Main.Post;

        CYNO.Text := '';
      end;
      if ADOQueryTmp.RecordCount > 1 then
      begin
        SCreateCDS20(ADOQueryTmp, CDS_YPXZ);
        SInitCDSData20(ADOQueryTmp, CDS_YPXZ);
        cxGrid4.Visible := True;
        cxGrid4.SetFocus;
        CYNO.Text := '';
      end;

    end;
  end;
end;

procedure TfrmPieceSampleRk.TV4DblClick(Sender: TObject);
begin
  GetYP();
end;

procedure TfrmPieceSampleRk.TV4KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    GetYP();
  end;
end;

procedure TfrmPieceSampleRk.ToolButton3Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
begin
  ToolBar1.SetFocus;
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要一键替换吗', '提示', 1) = 2 then
    exit;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_Main.fieldbyname(FColumn).AsString);
  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_Main.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Main do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue = '' then
      begin
        CDS_Main.FieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_Main.FieldByName(FColumn).Value := FValue;
      end;
      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_Main.FieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_Main.FieldByName(FFColumn).Value := FFValue;
        end;
      end;
      Post;
      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmPieceSampleRk.cv1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  fly, fqty: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with CDS_Main do
  begin
    Edit;
    fly := FieldByName('lyqty').AsFloat;

    Post;
  end;

  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('qty').Value := fly;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.btn1Click(Sender: TObject);
var
  F1, FMJID: string;
begin
  CRType.ItemIndex := 3;
  frmPieceSampleCKlistsel := TfrmPieceSampleCKlistsel.create(self);
  with frmPieceSampleCKlistsel do
  begin
    fType := 10;
    fCKName := trim(self.fCKName);
    if showmodal = 1 then
    begin

      cds_main.DisableControls;
      cds_main.First;
      F1 := CDS_Main.FieldByName('ToFactoryName').AsString;
//      fromFactoryName.Text := F1;

      while not cds_main.Eof do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean) and (not self.cds_main.Locate('MJID', trim(cds_main.fieldbyname('MJID').AsString), [])) then
        begin
          self.cds_main.Append;
          FMJID := '';
          if GetLSNo(self.ADOQueryCmd, FMJID, 'MJ', 'CK_Sample_CR', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          self.cds_main.FieldByName('MJID').Value := FMJID;
          self.cds_main.FieldByName('MJXH').Value := trim(cds_main.fieldbyname('MJXH').AsString);
          self.cds_main.FieldByName('CPTYPE').Value := trim(cds_main.fieldbyname('CPTYPE').AsString);
          self.cds_main.FieldByName('P_Code').Value := trim(cds_main.fieldbyname('P_Code').AsString);
          self.cds_main.FieldByName('P_CodeName').Value := trim(cds_main.fieldbyname('P_CodeName').AsString);
          self.cds_main.FieldByName('kuwei').Value := trim(cds_main.fieldbyname('kuwei').AsString);
          self.cds_main.FieldByName('P_spec').Value := trim(cds_main.fieldbyname('P_spec').AsString);
          self.cds_main.FieldByName('P_Color').Value := trim(cds_main.fieldbyname('P_Color').AsString);
          self.cds_main.FieldByName('P_HX').Value := trim(cds_main.fieldbyname('P_HX').AsString);
          self.cds_main.FieldByName('P_CF').Value := trim(cds_main.fieldbyname('P_CF').AsString);
          self.cds_main.FieldByName('P_MF').Value := trim(cds_main.fieldbyname('P_MF').AsString);
          self.cds_main.FieldByName('P_KZ').Value := trim(cds_main.fieldbyname('P_KZ').AsString);
          self.cds_main.FieldByName('RollNum').Value := cds_main.fieldbyname('RollNum').AsFloat;
          self.cds_main.FieldByName('Qty').Value := cds_main.fieldbyname('Qty').AsFloat;
          self.cds_main.FieldByName('yptype').Value := trim(cds_main.fieldbyname('yptype').AsString);
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').AsString);
          self.cds_main.FieldByName('fromFactoryName').Value := trim(cds_main.fieldbyname('TOFactoryName').AsString);
          self.cds_main.FieldByName('Price').Value := 0;
          self.cds_main.FieldByName('Money').Value := 0;
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
  CRType.Text := '客户退样';
end;

procedure TfrmPieceSampleRk.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmClothSYarnCListSel := TfrmClothSYarnCListSel.Create(self);
  with frmClothSYarnCListSel do
  begin
//    ftype := GSTaiTou.Text;
//    OrderNo.Text := self.FOrderno;
    if showmodal = 1 then
    begin
      Self.CDS_Main.Edit;
      Self.CDS_Main.fieldbyname('Orderno').Value := Order_Main.fieldbyname('Orderno').asstring;
      Self.CDS_Main.fieldbyname('mainid').Value := Order_Main.fieldbyname('MainId').asstring;
      Self.CDS_Main.fieldbyname('subid').Value := Order_Main.fieldbyname('SubId').asstring;

      Self.CDS_Main.fieldbyname('P_Code').Value := Order_Main.fieldbyname('MPRTCode').asstring;
      Self.CDS_Main.fieldbyname('P_CodeName').Value := Order_Main.fieldbyname('MPRTCodeName').asstring;
      Self.CDS_Main.fieldbyname('P_SPEC').Value := Order_Main.fieldbyname('MPRTSPEC').asstring;
      Self.CDS_Main.fieldbyname('P_KZ').Value := Order_Main.fieldbyname('MPRTKZ').asstring;
      Self.CDS_Main.fieldbyname('P_MF').Value := Order_Main.fieldbyname('MPRTMF').asstring;
      Self.CDS_Main.fieldbyname('P_Color').Value := Order_Main.fieldbyname('PRTColor').asstring;

      Self.CDS_Main.fieldbyname('P_CF').Value := Order_Main.fieldbyname('MPRTCF').asstring;
      Self.CDS_Main.fieldbyname('P_HX').Value := Order_Main.fieldbyname('PRTHX').asstring;

      Self.CDS_Main.Post;
    end;
    free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleRk.ToolButton4Click(Sender: TObject);
var
  fmjid: string;
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      while frmCPManageSel.CDS_Main.Locate('SSel', true, []) do
      begin
        if GetLSNo(self.ADOQueryCmd, fmjid, 'MJ', 'CK_Sample_CR', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        self.CDS_Main.Append;
        self.CDS_Main.fieldbyname('P_Code').Value := CDS_Main.fieldbyname('CYNo').asstring;
        self.CDS_Main.fieldbyname('P_CodeName').Value := CDS_Main.fieldbyname('CYName').asstring;
        self.CDS_Main.fieldbyname('P_SPEC').Value := CDS_Main.fieldbyname('CYSpec').asstring;
        self.CDS_Main.fieldbyname('P_CF').Value := CDS_Main.fieldbyname('CYCF').asstring;
        if Trim(self.CDS_Main.fieldbyname('P_Color').AsString) = '' then
          self.CDS_Main.fieldbyname('P_Color').Value := CDS_Main.fieldbyname('CYColor').asstring;

        self.CDS_Main.fieldbyname('P_HX').Value := CDS_Main.fieldbyname('CYHX').asstring;
        self.CDS_Main.fieldbyname('P_MF').Value := CDS_Main.fieldbyname('CYMF').asstring;
        self.CDS_Main.fieldbyname('P_KZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;

        Self.CDS_Main.FieldbyName('CPType').Value := '正品';
        Self.CDS_Main.FieldbyName('QtyUnit').Value := 'M';
        Self.CDS_Main.FieldbyName('Rollnum').Value := 0;
        Self.CDS_Main.FieldbyName('Price').Value := 0;
        Self.CDS_Main.FieldbyName('qty').Value := 0;
        Self.CDS_Main.FieldbyName('Money').Value := 0;
        Self.CDS_Main.FieldbyName('XSKCqty').Value := 0;
        Self.CDS_Main.FieldbyName('MJID').Value := fmjid;

        self.CDS_Main.Post;
        frmCPManageSel.CDS_Main.Delete;
      end;
      Next;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmPieceSampleRk.yptypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin

      flag := 'YPTYPE';
      flagname := '样品类型';

      if ShowModal = 1 then
      begin
        yptype.Text := ClientDataSet1.fieldbyname('zdyname').AsString;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

end.

