unit U_PieceSampleCK;

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, dxSkinsCore, 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, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmPieceSampleCK = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label8: TLabel;
    InoutNo: TEdit;
    Label9: TLabel;
    ToFactoryName: TBtnEditA;
    Panel3: TPanel;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJXH: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cv1Column6: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(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 ToolButton1Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure FHAddressBtnDnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    canshu2: string;
    { Public declarations }
  end;

var
  frmPieceSampleCK: TfrmPieceSampleCK;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_ZDYHelpSel, U_ZdyAttachGYS,
  U_PieceSampleKcList, U_JYSQlistsel;
{$R *.dfm}

function TfrmPieceSampleCK.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, 'SPC', '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('CRNO').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('CKName').Value := trim(fCKName);
        ADOQueryCmd.FieldByName('ToFactoryName').Value := trim(ToFactoryName.Text);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('Chkstatus').Value := '1';
        ADOQueryCmd.FieldByName('CRFlag').Value := '出库';

        ADOQueryCmd.FieldByName('CRQtyFlag').Value := -1;

         //保存明细
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_Sample_CR', 0);

         //保存隐藏字段
        ADOQueryCmd.FieldByName('mainID').Value := trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(fieldbyname('subID').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('select k1=ISNULL(SUM(X.CRQtyFlag*X.Qty),0),k2=ISNULL(SUM(X.CRQtyFlag*X.RollNum),0)  from CK_Sample_CR	X  where mjid= ' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
//          SQL.Add('and  Valid=''Y''');
////          ShowMessage(SQL.Text);
//          Open;
//        end;
//        if ADOQueryCmd.FieldByName('k1').asfloat < 0 then
//        begin
//          CDS_Main.EnableControls;
//          ADOQueryCmd.Connection.RollbackTrans;
//          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
//          Exit;
//        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;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('UPDATE YP_InOut2 SET  STATUS=''1'' WHERE CRID=' + quotedstr(trim(CDS_Main.fieldbyname('JYSQD').AsString)));
          ExecSQL;
        end;

        Edit;
        FieldByName('CRNO').Value := trim(MaxNo);
        post;
        next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmPieceSampleCK.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,SJKC=ISNULL(KCQTY,0) from CK_Sample_CR A');
      sql.Add('where A.inoutno=' + 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));

      InoutNO.Text := trim(CDS_Main.fieldbyname('InoutNO').AsString);
      ToFactoryName.Text := trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
    end;
  finally
  end;
end;

procedure TfrmPieceSampleCK.FormDestroy(Sender: TObject);
begin
  frmPieceSampleCK := nil;
end;

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

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

procedure TfrmPieceSampleCK.FormShow(Sender: TObject);
var
  fbhType, fInoutNo: string;
begin
  readCxGrid(trim(self.Caption), Tv1, '样品仓库77');
  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'YCK', 'CK_Sample_CR', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    inoutNo.Text := trim(fkeyNo);
  end;
end;

procedure TfrmPieceSampleCK.TBSaveClick(Sender: TObject);
var
  FBS: string;
begin
  if cds_main.IsEmpty then
    exit;
  CRTime.SetFocus;
//  if trim(CRType.Text) = '' then
//  begin
//    application.MessageBox('出库类型不能为空！', '提示信息', 0);
//    exit;
//  end;
//  if trim(ToFactoryName.Text) = '' then
//  begin
//    application.MessageBox('出库单位不能为空！', '提示信息', 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;
  with cds_main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.fieldbyname('Qty').Value = CDS_Main.fieldbyname('SJKC').Value then
      begin
        FBS := '1';
      end;
      Next;
    end;
  end;
  if FBS = '1' then
  begin
    if Application.MessageBox('出库数量等于库存数量，是否继续？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;

  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmPieceSampleCK.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 TfrmPieceSampleCK.ToolButton1Click(Sender: TObject);
var
  FMJID, fjysq, fcode: string;
  fqty: Double;
begin
  frmJYSQLISTSEL := TfrmJYSQLISTSEL.create(self);
  with frmJYSQLISTSEL do
  begin

    if showmodal = 1 then
    begin

      fjysq := trim(cds_main.fieldbyname('crid').AsString);
      fcode := trim(cds_main.fieldbyname('P_Code').AsString);
      fqty := cds_main.fieldbyname('qty').value;
    end;
    frmJYSQLISTSEL.free;
  end;

  if fjysq <> '' then
  begin
    frmPieceSampleKcList := TfrmPieceSampleKcList.create(self);
    with frmPieceSampleKcList do
    begin
      fType := 10;
      fCKName := trim(self.fCKName);
      P_Code.Text := fcode;
      if showmodal = 1 then
      begin

        cds_main.DisableControls;
//        cds_main.First;

//        while not cds_main.Eof do
//        begin

        if (not self.cds_main.Locate('MJID', trim(cds_main.fieldbyname('MJID').AsString), [])) then
        begin
          self.cds_main.Append;
          self.cds_main.FieldByName('JYSQD').AsString := Trim(fjysq);
          self.cds_main.FieldByName('OrderNo').Value := trim(cds_main.fieldbyname('OrderNo').AsString);
          self.cds_main.FieldByName('mainID').Value := trim(cds_main.fieldbyname('mainID').AsString);
          self.cds_main.FieldByName('subID').Value := trim(cds_main.fieldbyname('subID').AsString);
          self.cds_main.fieldbyname('fromFactoryName').Value := trim(cds_main.fieldbyname('fromFactoryName').AsString);
          self.cds_main.fieldbyname('FactoryName').Value := trim(cds_main.fieldbyname('FactoryName').AsString);
          self.cds_main.fieldbyname('JGId').Value := trim(cds_main.fieldbyname('JGId').AsString);
          self.cds_main.FieldByName('MJID').Value := trim(cds_main.fieldbyname('MJID').AsString);
          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 := fqty;    // cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('yptype').Value := trim(cds_main.fieldbyname('yptype').AsString);

          self.cds_main.FieldByName('SJKC').Value := cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').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;
  end;

end;

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

procedure TfrmPieceSampleCK.ToFactoryNameBtnClick(Sender: TObject);
begin
//  if (trim(CRType.Text) = '加工出库') or (trim(CRType.Text) = '生产出库') or (trim(CRType.Text) = '退货出库') or (trim(CRType.Text) = '损耗出库') then
//  begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      canshu2 := '生产出库';
      if ShowModal = 1 then
      begin
        ToFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
//  end
//  else
//  begin
//    try
//      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
//      with frmZdyAttachment do
//      begin
//        if ShowModal = 1 then
//        begin
//          ToFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
//        end;
//      end;
//    finally
//      frmZdyAttachment.Free;
//    end;
//  end;
end;

procedure TfrmPieceSampleCK.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 TfrmPieceSampleCK.FHAddressBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmPieceSampleCK.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmOrderListSel:=TfrmOrderListSel.Create(Application);
//    with frmOrderListSel do
//    begin
//      if ShowModal=1 then
//      begin
//        Self.CDS_Main.Edit;
//        Self.CDS_Main.FieldByName('OrderNo').Value:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
//        Self.CDS_Main.FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
//      end;
//    end;
//  finally
//    frmOrderListSel.Free;
//  end;
//  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleCK.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MLType';
      flagname := '面料类型';
      if ShowModal = 1 then
      begin
        CDS_Main.Edit;
        CDS_Main.FieldByName('P_Type').Value := Trim(ClientDataSet1.fieldbyname('zdyName').AsString);
        CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPieceSampleCK.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 TfrmPieceSampleCK.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmPieceSampleCK.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmJYSQLISTSEL := TfrmJYSQLISTSEL.create(self);
  with frmJYSQLISTSEL do
  begin

    if showmodal = 1 then
    begin

      self.cds_main.edit;
      self.cds_main.FieldByName('JYSQD').Value := trim(cds_main.fieldbyname('crid').AsString);

      self.cds_main.Post;

    end;
    frmJYSQLISTSEL.free;
  end;

end;

procedure TfrmPieceSampleCK.ToolButton3Click(Sender: TObject);
begin
  frmPieceSampleKcList := TfrmPieceSampleKcList.create(self);
  with frmPieceSampleKcList do
  begin
    fType := 10;
    fCKName := trim(self.fCKName);
    yptype.Text := self.canshu2;
//    P_Code.Text := fcode;
    if showmodal = 1 then
    begin

      cds_main.DisableControls;
      cds_main.First;

      while not cds_main.Eof do
      begin

        if (not self.cds_main.Locate('MJID', trim(cds_main.fieldbyname('MJID').AsString), [])) and (cds_main.FieldByName('SSel').AsBoolean = True) then
        begin
          self.cds_main.Append;
//        self.cds_main.FieldByName('JYSQD').Value := fjysq;
          self.cds_main.FieldByName('OrderNo').Value := trim(cds_main.fieldbyname('OrderNo').AsString);
          self.cds_main.FieldByName('mainID').Value := trim(cds_main.fieldbyname('mainID').AsString);
          self.cds_main.FieldByName('subID').Value := trim(cds_main.fieldbyname('subID').AsString);
          self.cds_main.fieldbyname('fromFactoryName').Value := trim(cds_main.fieldbyname('fromFactoryName').AsString);
          self.cds_main.fieldbyname('FactoryName').Value := trim(cds_main.fieldbyname('FactoryName').AsString);
          self.cds_main.fieldbyname('JGId').Value := trim(cds_main.fieldbyname('JGId').AsString);
          self.cds_main.FieldByName('MJID').Value := trim(cds_main.fieldbyname('MJID').AsString);
          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_ecolor').Value := trim(cds_main.fieldbyname('P_ecolor').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 := 0;    // cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('yptype').Value := trim(cds_main.fieldbyname('yptype').AsString);

          self.cds_main.FieldByName('SJKC').Value := cds_main.fieldbyname('kcQty').AsFloat;
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').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;
end;

end.

