unit U_ClothQuotationInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, dxDateTimeWheelPicker, dxGDIPlusClasses,
  Vcl.Menus, MovePanel, cxCheckBox, cxImage, cxDBEdit, Vcl.DBCtrls, U_BaseInput,
  System.ImageList, Vcl.ImgList, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, ShellAPI, cxSplitter,
  dxSkinBasic, 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, dxSkinOffice2016Colorful,
  dxSkinOffice2016Dark, dxSkinOffice2019Black, dxSkinOffice2019Colorful,
  dxSkinOffice2019DarkGray, dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinWXI, dxSkinXmas2008Blue, dxScrollbarAnnotations;

type
  TfrmClothQuotationInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    MVCDate: TDateTimePicker;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    Order_Sub: TClientDataSet;
    Label9: TLabel;
    Label10: TLabel;
    MVCNo: TEdit;
    Label14: TLabel;
    Label20: TLabel;
    BuyName: TBtnEditC;
    GPM_1: TcxGridPopupMenu;
    Sellname: TBtnEditC;
    Label29: TLabel;
    Label30: TLabel;
    QiYun: TBtnEditC;
    MuDi: TBtnEditC;
    Label5: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    YunShu: TBtnEditC;
    Payment: TBtnEditC;
    Label4: TLabel;
    GSChuanYangNote: TBtnEditC;
    Label13: TLabel;
    JiaGeSY: TBtnEditC;
    Label1: TLabel;
    CKChuanYangNote: TBtnEditC;
    Label8: TLabel;
    Packing: TBtnEditC;
    Label15: TLabel;
    Label16: TLabel;
    DlyDate: TDateTimePicker;
    ToolButton3: TToolButton;
    lblSalesId: TLabel;
    Label25: TLabel;
    SalesId: TEdit;
    TallyId: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    GroupBox5: TGroupBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column7: TcxGridDBColumn;
    v1XHNo: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV2Column4: TcxGridDBColumn;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Code: TEdit;
    ADOQuery1: TADOQuery;
    Label2: TLabel;
    Note: TMemo;
    Saleser: TBtnEditC;
    ShippMent: TBtnEditC;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxSplitter1: TcxSplitter;
    Label27: TLabel;
    IsTax: TComboBox;
    TV2Column8: TcxGridDBColumn;
    Currency: TBtnEditC;
    Tv1Column2: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNameBtnUpClick(Sender: TObject);
    procedure BuyNameBtnDnClick(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure SellNameBtnUpClick(Sender: TObject);
    procedure QiYunBtnUpClick(Sender: TObject);
    procedure ShippMentKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure CodeKeyPress(Sender: TObject; var Key: Char);
    procedure MVCDateChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure YunShuBtnUpClick(Sender: TObject);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;
    procedure InitData();
    procedure InitHistoricalPrice();
    function TestData(): Boolean;
    function SaveData(): Boolean;
    procedure SetXH();
    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    fFlileFlag: string;
    FMVCMID: string;
    FAuthority, FGSTT: string;
    { Public declarations }
  end;

var
  frmClothQuotationInPut: TfrmClothQuotationInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel,
  U_PictureUpload, U_UserSel ,U_QuiryCompanySel;

{$R *.dfm}
procedure TfrmClothQuotationInPut.InitHistoricalPrice();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select TOP 30 *      ');
    sql.Add('from MV_Cloth_Main A inner join MV_Cloth_Sub B on A.MVCMId=B.MVCMId ');
    sql.Add('where B.C_Code=''' + Trim(Order_Sub.FieldByName('C_Code').AsString) + '''');
    sql.Add('and  isnull(A.status,''0'')<>''-1''');
    sql.Add('and  MVCType=''报价''');
    sql.Add('order by A.MVCDate Desc');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_2);
  SInitCDSData(ADOTemp, CDS_2);
end;

procedure TfrmClothQuotationInPut.MVCDateChange(Sender: TObject);
begin
  DlyDate.DateTime := MVCDate.DateTime + 30;

end;

procedure TfrmClothQuotationInPut.N1Click(Sender: TObject);
begin
  ToolButton2.Click;

end;

procedure TfrmClothQuotationInPut.SetXH();
var
  i: Integer;
begin
  with Order_Sub do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with Order_Sub do
  begin
    Order_Sub.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('SerialNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    Order_Sub.IndexFieldNames := 'SerialNo';
  end;
end;

procedure TfrmClothQuotationInPut.ShippMentKeyPress(Sender: TObject; var Key: Char);
begin

//  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
//  begin
//    Key := #0;
//  end
//  else if (pos('.', TEdit(Sender).text) <> 0) then
//  begin
//    if Key = '.' then
//    begin
//      Key := #0;
//    end;
//  end;
end;

function TfrmClothQuotationInPut.TestData(): Boolean;
begin
  try
    if Trim(MVCNo.Text) = '' then
      raise Exception.Create('报价号不能为空！');
//    if Trim(SellName.Text) = '' then
//      raise Exception.Create('我方单位不能为空！');
    if Trim(BuyName.Text) = '' then
      raise Exception.Create('客户不能为空！');

    if Order_Sub.IsEmpty then
      raise Exception.Create('明细不能为空！');
    if Order_Sub.Locate('C_Code', null, []) then
      raise Exception.Create('产品编号不能为空！');
    if Order_Sub.Locate('C_Code', '', []) then
      raise Exception.Create('产品编号不能为空！');
    if Order_Sub.Locate('C_Name', null, []) then
      raise Exception.Create('品名不能为空！');
    if Order_Sub.Locate('C_Name', '', []) then
      raise Exception.Create('品名不能为空！');
//    if Order_Sub.Locate('C_Color', null, []) then
//      raise Exception.Create('颜色不能为空！');
//    if Order_Sub.Locate('C_Color', '', []) then
//      raise Exception.Create('颜色不能为空！');

    if Order_Sub.Locate('QtyUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if Order_Sub.Locate('QtyUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmClothQuotationInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmClothQuotationInPut.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from MV_Cloth_Sub ');
    sql.Add('where MVCMID=''' + Trim(FMVCMID) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from MV_Cloth_Main where MVCMID=''' + Trim(FMVCMID) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  BuyName.TxtCode := Trim(ADOTemp.FieldByName('BuyNo').AsString);
  Sellname.TxtCode := Trim(ADOTemp.FieldByName('SellNo').AsString);
  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

  if PState = '新增' then
  begin
    MVCDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := MVCDate.DateTime + 30;
    MVCNo.Text := '自动生成';

  end;

  if PState = '复制' then
  begin

    PState := '新增';
    FMVCMID := '';
    MVCNo.Text := '自动生成';
    MVCDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := MVCDate.DateTime + 30;

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOCmd, maxno, 'MVS', 'MV_Cloth_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('MVCMID').Value := '';
        FieldByName('MVCSID').Value := maxno;
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmClothQuotationInPut.QiYunBtnUpClick(Sender: TObject);

begin
   try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
//      FCoType := '客户';
//      if Self.FAuthority = '录入' then
//        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin
//        self.BuyName.TxtCode := Trim(frmUserSel.CDS_1.fieldbyname('CoCode').AsString);
//        self.BuyName.Text := Trim(frmUserSel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.SalesId.Text := Trim(frmUserSel.CDS_1.fieldbyname('UserID').AsString);
        self.Saleser.Text := Trim(frmUserSel.CDS_1.fieldbyname('UserName').AsString);
      end;
    end;

  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmClothQuotationInPut.FormShow(Sender: TObject);
begin
  inherited;

  readCxGrid(trim(self.Caption) + 'Tv1', Tv1, '销售报价管理');
  readCxGrid(trim(self.Caption) + 'Tv2', Tv2, '销售报价管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
end;

function TfrmClothQuotationInPut.SaveData(): Boolean;
var
  maxno: string;
  fMVCNo, fmxType, fDate: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FMVCMID) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'MVM', 'MV_Cloth_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');

      fDate := 'BJ' + FormatDateTime('yy', SGetServerDate(ADOTemp));

      if trim(MVCNo.Text) = '自动生成' then
      begin
        if GetLSNo(ADOTemp, fMVCNo, fDate, 'MV_Cloth_Main', 4, 0) = False then
          raise Exception.Create('取最报价编号失败！');

        MVCNo.Text := fMVCNo;
      end;
    end
    else
    begin
      maxno := Trim(FMVCMID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from MV_Cloth_Main where MVCMID=''' + Trim(FMVCMID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMVCMID) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('status').Value := '0';
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('MVCMID').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'MV_Cloth_Main', ScrollBox1, 2);
      FieldByName('BuyNo').Value := Trim(BuyName.TxtCode);
      FieldByName('SellNo').Value := Sellname.TxtCode;
      FieldByName('MVCType').Value := '报价';
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from MV_Cloth_Main where MVCNo=''' + Trim(MVCNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('报价单号重复！');

    FMVCMID := Trim(maxno);
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin

        maxno := Trim(Order_Sub.fieldbyname('MVCSID').AsString);

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from MV_Cloth_Sub   ');
          sql.Add(' where MVCMID=''' + Trim(FMVCMID) + '''');
          sql.Add(' and MVCSID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'MV_Cloth_Sub', 0);
          FieldByName('MVCMID').Value := Trim(FMVCMID);
          FieldByName('MVCSID').Value := Trim(maxno);
          Post;
        end;
        with Order_Sub do
        begin
          Edit;
          FieldByName('MVCSID').Value := Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
////////////////       保存子表          //////////////////////
///
////////////////       更新报价          //////////////////////
//    with ADOCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('EXEC  P_Con_Up_PiQty   ');
//      sql.Add('@DataID=' + quotedstr(Trim(FMVCMID)));
//      execsql;
//    end;
////////////////       更新报价          //////////////////////

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except

    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmClothQuotationInPut.TBSaveClick(Sender: TObject);
begin
  MVCDate.SetFocus;

  if TestData() = false then
    exit;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmClothQuotationInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if GetLSNo(ADOCmd, maxno, 'MVS', 'MV_Cloth_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  if Trim(MVCNo.Text) = '' then
  begin
    Application.MessageBox('报价号不能为空!', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount;      //当前的dataset有多少行记录
  i := i + 1;
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('SSEL').Value := FALSE;
    FieldByName('SerialNo').Value := i;
    FieldByName('MVCSID').Value := maxno;
    FieldByName('C_Color').Value := '';
    FieldByName('QtyUnit').Value := 'M';
    FieldByName('Price').Value := null;
    Post;
  end;
  SetXH();
end;

procedure TfrmClothQuotationInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  while Order_Sub.Locate('SSel', True, []) do
  begin
//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from  Trade_Plan_Sub where MVCSID=''' + Trim(Order_Sub.fieldbyname('MVCSID').AsString) + '''');
//      open;
//    end;
//    if ADOTemp.IsEmpty = false then
//    begin
//      exit;
//      application.MessageBox('该明细已经在生产！', '提示信息');
//    end;
    if Trim(Order_Sub.fieldbyname('MVCSID').AsString) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete MV_Cloth_Sub where MVCSID=''' + Trim(Order_Sub.fieldbyname('MVCSID').AsString) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.Delete;
  end;

  SetXH();
end;

procedure TfrmClothQuotationInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '销售报价管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '销售报价管理');
end;

procedure TfrmClothQuotationInPut.ToolButton4Click(Sender: TObject);
begin
  GroupBox5.Visible := not GroupBox5.Visible;
end;

procedure TfrmClothQuotationInPut.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitHistoricalPrice();
end;

procedure TfrmClothQuotationInPut.BuyNameBtnUpClick(Sender: TObject);
begin
  try
    frmQuiryCompanySel := TfrmQuiryCompanySel.Create(Application);
    with frmQuiryCompanySel do
    begin
      FCoType := '客户';
      if Self.FAuthority = '录入' then
        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin
        self.BuyName.TxtCode := Trim(frmQuiryCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.BuyName.Text := Trim(frmQuiryCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.SalesId.Text := Trim(frmQuiryCompanySel.CDS_1.fieldbyname('SalesId').AsString);
        self.Saleser.Text := Trim(frmQuiryCompanySel.CDS_1.fieldbyname('Saleser').AsString);
      end;
    end;

  finally
    frmQuiryCompanySel.Free;
  end;

end;

procedure TfrmClothQuotationInPut.CodeKeyPress(Sender: TObject; var Key: Char);
var
  i: Integer;
  maxno, FBCIID: string;
begin
  if Key = #13 then
  begin

    FBCIID := TRIM(code.Text);
//    ShowMessage(FBCIID);
    if GetLSNo(ADOCmd, maxno, 'MVS', 'MV_Cloth_Sub', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取子流水号失败！', '提示', 0);
      Exit;
    end;
    if Trim(MVCNo.Text) = '' then
    begin
      Application.MessageBox('报价号不能为空!', '提示', 0);
      Exit;
    end;
    i := Order_Sub.RecordCount;      //当前的dataset有多少行记录
    i := i + 1;
//    CopyAddRow(Tv1, Order_Sub);
    with ADOQuery1 do
    begin
      Close;
//      Filtered := False;
      sql.Clear;
      sql.Add(' select * from V_BS_Cloth_Info ');
      sql.Add('where BCIID= ''' + Trim(FBCIID) + '''');
//      ShowMessage(sql.Text);
      Open;
    end;

    with Order_Sub do
    begin
      Append;
      FieldByName('SSEL').Value := FALSE;
      FieldByName('SerialNo').Value := i;
      FieldByName('MVCSID').Value := maxno;
      FieldByName('C_Color').Value := '';
      FieldByName('QtyUnit').Value := 'M';
      FieldByName('Price').Value := null;

      FieldByName('BCIID').Value := ADOQuery1.fieldbyname('BCIID').asstring;
      FieldByName('C_Code').Value := ADOQuery1.fieldbyname('C_Code').asstring;
      FieldByName('C_Name').Value := ADOQuery1.fieldbyname('C_Name').asstring;
      FieldByName('C_EName').Value := ADOQuery1.fieldbyname('C_EName').asstring;
      FieldByName('C_Spec').Value := ADOQuery1.fieldbyname('C_Spec').asstring;
      FieldByName('C_Composition').Value := ADOQuery1.fieldbyname('C_Composition').asstring;
      FieldByName('C_Color').Value := ADOQuery1.fieldbyname('C_Color').asstring;
      FieldByName('C_Width').Value := ADOQuery1.fieldbyname('C_Width').asstring;
      FieldByName('C_GramWeight').Value := ADOQuery1.fieldbyname('C_GramWeight').asstring;

      Post;
    end;
    Code.text := '';

  end;

end;

procedure TfrmClothQuotationInPut.BuyNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmClothQuotationInPut.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('PRTCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmClothQuotationInPut.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_MF';
      flagname := '门幅信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('C_MF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmClothQuotationInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_KZ';
      flagname := '克重信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('C_KZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmClothQuotationInPut.YunShuBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothQuotationInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin
  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      Order_Sub.Edit;
      Order_Sub.fieldbyname('BCIID').Value := frmClothInfoSel.CDS_1.fieldbyname('BCIID').asstring;
      Order_Sub.fieldbyname('C_Code').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Code').asstring;

      Order_Sub.fieldbyname('C_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;
      Order_Sub.fieldbyname('C_EName').Value := frmClothInfoSel.CDS_1.fieldbyname('C_EName').asstring;
      Order_Sub.fieldbyname('C_Spec').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      Order_Sub.fieldbyname('C_Composition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;
      Order_Sub.fieldbyname('C_Color').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Color').asstring;
      Order_Sub.fieldbyname('C_Width').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Width').asstring;
      Order_Sub.fieldbyname('C_GramWeight').Value := frmClothInfoSel.CDS_1.fieldbyname('C_GramWeight').asstring;
      Order_Sub.Post;
    end;
    free;
  end;

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

procedure TfrmClothQuotationInPut.SalesmanBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmClothQuotationInPut.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('YongJinPrice').Value := mvalue;
    FieldByName('YongJin').Value := FieldByName('YongJinPrice').AsFloat * FieldByName('PRTOrderQty').AsFloat;
    Post;
  end;
end;

procedure TfrmClothQuotationInPut.SellNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.Sellname.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.Sellname.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.Currency.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Currency').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

end.

