unit U_SalesConInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  U_FormLayOutDesign, 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,
  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, dxScrollbarAnnotations, dxCore, cxDateUtils, cxPC,
  cxImageList, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmSalesConInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    Order_Sub: TClientDataSet;
    cxgrdpmn1: TcxGridPopupMenu;
    pm_sub: TPopupMenu;
    N1: TMenuItem;
    DataSource1: TDataSource;
    ADOQueryImage: TADOQuery;
    DS_ImgHX: TDataSource;
    Qry_ImgHX: TADOQuery;
    IdFTP1: TIdFTP;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    cxPageControl3: TcxPageControl;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet1: TcxTabSheet;
    ScrollBox1: TScrollBox;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    lbl5: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl20: TLabel;
    lbl21: TLabel;
    lbl22: TLabel;
    lbl23: TLabel;
    lbl24: TLabel;
    lblSalesId: TLabel;
    lbl25: TLabel;
    ConNo: TcxTextEdit;
    BuyConNo: TcxTextEdit;
    cxDBImage1: TcxDBImage;
    PackSeal: TBtnEditC;
    DlyNote: TcxTextEdit;
    SalesId: TcxTextEdit;
    TallyId: TcxTextEdit;
    BuyName: TcxButtonEdit;
    Sellname: TcxButtonEdit;
    MiddleName: TcxButtonEdit;
    Pic_Image1: TcxDBImage;
    ConType: TcxComboBox;
    ConDate: TcxDateEdit;
    DlyDate: TcxDateEdit;
    lbl14: TLabel;
    lbl26: TLabel;
    lbl27: TLabel;
    SellTel: TcxTextEdit;
    lbl28: TLabel;
    SellAddress: TcxTextEdit;
    SellFax: TcxTextEdit;
    lbl29: TLabel;
    lbl30: TLabel;
    lbl31: TLabel;
    BuyTel: TcxTextEdit;
    lbl32: TLabel;
    BuyAddress: TcxTextEdit;
    BuyFax: TcxTextEdit;
    lbl33: TLabel;
    pnl1: TPanel;
    lbl10: TLabel;
    lbl11: TLabel;
    lbl16: TLabel;
    ShipMent: TcxButtonEdit;
    BegPort: TcxButtonEdit;
    EndPort: TcxButtonEdit;
    lbl17: TLabel;
    lbl19: TLabel;
    PackNote: TcxButtonEdit;
    ShippingSample: TcxButtonEdit;
    lbl9: TLabel;
    IsTax: TcxComboBox;
    lbl12: TLabel;
    lbl13: TLabel;
    ExchangeRate: TcxTextEdit;
    Currency: TcxComboBox;
    lbl15: TLabel;
    lbl18: TLabel;
    Payment: TcxButtonEdit;
    PriceTerms: TcxButtonEdit;
    CommissionRatio: TcxComboBox;
    Label1: TLabel;
    LCRatio: TcxTextEdit;
    Saleser: TcxButtonEdit;
    Tallyer: TcxButtonEdit;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column7: TcxGridDBColumn;
    v1XHNo: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ConTerms: TcxMemo;
    Label2: TLabel;
    ToolButton6: TToolButton;
    Tv1Column21: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ADO_XF: TADOQuery;
    SellRepresentative: TcxButtonEdit;
    BuyRepresentative: TcxButtonEdit;
    N3: TMenuItem;
    N4: TMenuItem;
    ADO_Main: TADOQuery;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column23: 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 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 v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Pic_Image1DblClick(Sender: TObject);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure PackSealBtnUpClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ExchangeRateKeyPress(Sender: TObject; var Key: Char);
    procedure BuyNameDblClick(Sender: TObject);
    procedure ParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SellnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure MiddleNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_CompositionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure CurrencyPropertiesChange(Sender: TObject);
    procedure SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TallyerPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SaleserDblClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormResize(Sender: TObject);
    procedure TallyerDblClick(Sender: TObject);
    procedure SellRepresentativePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SellRepresentativeDblClick(Sender: TObject);
    procedure BuyRepresentativePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure BuyRepresentativeDblClick(Sender: TObject);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;
    procedure InitImage();
    procedure JSMoney();
    procedure InitData();
    function TestData(): Boolean;
    function SaveData(): Boolean;
    procedure SetXH();
    procedure LookImage(FileName: string);
    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    fFlileFlag: string;
    FConMId: string;
    FAuthority, FGSTT: string;
    { Public declarations }
  end;

var
  frmSalesConInPut: TfrmSalesConInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel, U_PictureUpload,
  U_MarketValueSel, U_PlanColorSel, U_UserSel, U_CompanyContactSel;

{$R *.dfm}

procedure TfrmSalesConInPut.SellnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.Sellname.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.Sellname.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);

        self.SellRepresentative.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Contacts').AsString);
        self.SellAddress.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').AsString);
        self.SellTel.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Telephone').AsString);

//        self.SellRepresentative.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CompanyContact').AsString);
//        self.SellTel.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CompanyTelephone').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmSalesConInPut.SellRepresentativeDblClick(Sender: TObject);
begin
  self.SellRepresentative.Text := '';
  self.SellTel.Text := '';
end;

procedure TfrmSalesConInPut.SellRepresentativePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanyContactSel := TfrmCompanyContactSel.Create(Application);
    with frmCompanyContactSel do
    begin
      FCoCode := Trim(self.Sellname.Properties.LookupItems.Text);
      if ShowModal = 1 then
      begin
        self.SellRepresentative.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('Contacts').AsString);
        self.SellTel.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('Telephone').AsString);
        self.SellAddress.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('CoAddress').AsString);

      end;
    end;

  finally
    frmCompanyContactSel.Free;
  end;
end;

procedure TfrmSalesConInPut.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 TfrmSalesConInPut.InitImage();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' and FileName=' + quotedstr(Trim(PackSeal.Text)));
//    showmessage(sql.text);        Self..Text
    open;
  end;
end;

procedure TfrmSalesConInPut.JSMoney();
var
  FConQty, FConPrice, FC_Money: Double;
begin
  with Order_Sub do
  begin
    FConQty := Fieldbyname('ConQty').AsFloat;
    FConPrice := Fieldbyname('ConPrice').AsFloat;
  end;
  //计算金额，保留2位小数
  FC_Money := RoundFloat(FConQty * FConPrice, 2);
  with Order_Sub do
  begin
    Edit;
    FieldByName('ConTotalAmount').Value := FC_Money;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

function TfrmSalesConInPut.TestData(): Boolean;
var
  fconNO, fmxType, fDate: string;
begin
  try
//    if ConType.Text = '内销' then
//    begin
//      if Trim(BegPort.Text) = '' then
//        raise Exception.Create('出货地不能为空！');
//      if Trim(EndPort.Text) = '' then
//        raise Exception.Create('到货地不能为空！');
//    end;
//    if ConType.Text = '外销' then
//    begin
//      if Trim(PriceTerms.Text) = '' then
//        raise Exception.Create('贸易术语不能为空！');
//    end;

    if Trim(SellName.Text) = '' then
      raise Exception.Create('经营单位不能为空！');
    if Trim(BuyName.Text) = '' then
      raise Exception.Create('对方单位不能为空！');
    if Trim(ConType.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('WidthUnit', null, []) then
      raise Exception.Create('门幅单位不能为空！');
    if Order_Sub.Locate('WidthUnit', '', []) then
      raise Exception.Create('门幅单位不能为空！');

    if Order_Sub.Locate('PurchaseType', null, []) then
      raise Exception.Create('采购类型不能为空！');
    if Order_Sub.Locate('BottomCloth', '', []) 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('C_Color', null, []) then
      raise Exception.Create('颜色不能为空！');
    if Order_Sub.Locate('C_Color', '', []) then
      raise Exception.Create('颜色不能为空！');
    if Order_Sub.Locate('ConQty', null, []) then
      raise Exception.Create('数量不能为空！');
    if Order_Sub.Locate('ConQty', 0, []) then
      raise Exception.Create('数量不能为0！');
    if Order_Sub.Locate('ConUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if Order_Sub.Locate('WidthUnit', '', []) then
      raise Exception.Create('门幅单位不能为空！');

//    if Order_Sub.Locate('MoreQty', null, []) then
//      raise Exception.Create('超交不能为空！');
//
//    if Order_Sub.Locate('LessQty', null, []) then
//      raise Exception.Create('短交不能为空！');
//
//    if Order_Sub.Locate('MoreLessUnit', null, []) then
//      raise Exception.Create('超交单位不能为空！');
//    if Order_Sub.Locate('MoreLessUnit', '', []) then
//      raise Exception.Create('超交单位不能为空！');

    if trim(ConNo.Text) = '自动生成' then
    begin
      fDate := 'UX' + FormatDateTime('yyMM', SGetServerDate(ADOTemp));
      if GetLSNo(ADOTemp, fconNO, fDate, 'BS_Contract_Main', 3, 0) = False then
        raise Exception.Create('取最合同编号失败！');
      ConNo.Text := fconNO;
    end;
    if Trim(ConNo.Text) = '' then
      raise Exception.Create('合同号不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmSalesConInPut.TallyerDblClick(Sender: TObject);
begin
  self.TallyId.Text := '';
  self.Tallyer.Text := '';
end;

procedure TfrmSalesConInPut.TallyerPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '理单';
      FRTUserID := Self.TallyId.Text;
      FMultiple := True;
      if ShowModal = 1 then
      begin
        self.TallyId.Text := FRTUserID;
        self.Tallyer.Text := FRTUserName;
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmSalesConInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmSalesConInPut.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BS_Contract_Sub ');
    sql.Add('where ConMId=''' + Trim(FConMId) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
   //刷新条款信息
  with ADO_Main do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Contract_Main where ConMId=''' + Trim(FConMId) + '''');
    Open;
  end;
  SCSHData(ADO_Main, ScrollBox1, 2);
  SCSHData(ADO_Main, pnl1, 2);
  //单位切换事件导致重新赋值
  ExchangeRate.Text := Trim(ADO_Main.FieldByName('ExchangeRate').AsString);
  BuyName.Properties.LookupItems.Text := Trim(ADO_Main.FieldByName('BuyNo').AsString);
  Sellname.Properties.LookupItems.Text := Trim(ADO_Main.FieldByName('SellNo').AsString);
  MiddleName.Properties.LookupItems.Text := Trim(ADO_Main.FieldByName('MiddleNo').AsString);
  Fstatus := Trim(ADO_Main.FieldByName('status').AsString);
  if Currency.text = 'CNY' then
    ExchangeRate.text := '1';

  if PState = '新增' then
  begin
    ConDate.Date := SGetServerDateTime(ADOTemp);
    DlyDate.Date := ConDate.Date;
    ConNo.Text := '自动生成';
  end;

  if PState = '复制' then
  begin

    PState := '新增';
    FConMId := '';
    ConNo.Text := '自动生成';
    ConDate.Date := SGetServerDateTime(ADOTemp);
    DlyDate.Date := ConDate.Date;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOCmd, maxno, 'SCS', 'BS_Contract_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('ConMId').Value := '';
        FieldByName('ConSId').Value := maxno;
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmSalesConInPut.N1Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn, MHXSID: string;
  MXHNO: Integer;
begin
  if Fstatus = '1' then
  begin
    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
    exit;
  end;
  MXHNO := Order_Sub.FieldByName('XHNo').ASInteger;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FValue := Trim(Order_Sub.fieldbyname(FColumn).AsString);
  MHXSID := Trim(Order_Sub.fieldbyname('HXSID').AsString);
  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean then
      begin
        Edit;

        if FValue = '' then
        begin
          Order_Sub.FieldByName(FColumn).Value := null;
        end
        else
        begin
          Order_Sub.FieldByName(FColumn).Value := FValue;
        end;
        if FColumn = 'C_HX' then
        begin
          Order_Sub.FieldByName('HXSID').Value := MHXSID;
        end;
        Post;
        JSMoney();
      end;
      Next;
    end;
    EnableControls;
  end;
  Order_Sub.Locate('XHNO', MXHNO, []);
end;

procedure TfrmSalesConInPut.N2Click(Sender: TObject);
var
  FValue, MC_Code, MC_Craft, MC_Name, MC_Spec, MC_CF, MC_Color, MC_MF, MC_KZ, MC_Note, MC_Unit, MGcHuaNo, MSuoLv, MC_EName: string;
  MXHNO: Integer;
begin
  if Fstatus = '1' then
  begin
    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
    exit;
  end;

  MXHNO := Order_Sub.FieldByName('XHNo').ASInteger;
  MC_Code := Order_Sub.fieldbyname('C_Code').AsString;
  MC_Craft := Order_Sub.fieldbyname('C_Craft').AsString;
  MC_Name := Order_Sub.fieldbyname('C_Name').AsString;
  MC_Spec := Order_Sub.fieldbyname('C_Spec').AsString;
  MC_CF := Order_Sub.fieldbyname('C_CF').AsString;
  MC_Color := Order_Sub.fieldbyname('C_Color').AsString;
  MC_MF := Order_Sub.fieldbyname('C_MF').AsString;
  MC_KZ := Order_Sub.fieldbyname('C_KZ').AsString;
  MC_Note := Order_Sub.fieldbyname('C_Note').AsString;
  MC_Unit := Order_Sub.fieldbyname('C_Unit').AsString;
  MSuoLv := Order_Sub.fieldbyname('SuoLv').AsString;
  MGcHuaNo := Order_Sub.fieldbyname('GcHuaNo').AsString;
  MC_EName := Order_Sub.fieldbyname('C_EName').AsString;

  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean then
      begin
        Edit;
        Order_Sub.fieldbyname('C_Code').Value := MC_Code;
        Order_Sub.fieldbyname('C_Craft').Value := MC_Craft;
        Order_Sub.fieldbyname('C_Name').Value := MC_Name;
        Order_Sub.fieldbyname('C_Spec').Value := MC_Spec;
        Order_Sub.fieldbyname('C_CF').Value := MC_CF;
        Order_Sub.fieldbyname('C_Color').Value := MC_Color;
        Order_Sub.fieldbyname('C_MF').Value := MC_MF;
        Order_Sub.fieldbyname('C_KZ').Value := MC_KZ;
        Order_Sub.fieldbyname('C_Note').Value := MC_Note;
        Order_Sub.fieldbyname('C_Unit').Value := MC_Unit;
        Order_Sub.fieldbyname('SuoLv').Value := MSuoLv;
        Order_Sub.fieldbyname('GcHuaNo').Value := MGcHuaNo;
        Order_Sub.fieldbyname('C_EName').Value := MC_EName;
        Post;

      end;
      Next;
    end;
    EnableControls;
  end;
  Order_Sub.Locate('XHNO', MXHNO, []);
end;

procedure TfrmSalesConInPut.N3Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, True);
end;

procedure TfrmSalesConInPut.N4Click(Sender: TObject);
begin
  SelOKNo(Order_Sub, false);
end;

procedure TfrmSalesConInPut.Pic_Image1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmSalesConInPut.LookImage(FileName: string);
var
  sFieldName: string;
begin
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';
  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
  sFieldName := sFieldName + '\' + trim(FileName);
  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmSalesConInPut.MiddleNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if Self.FAuthority = '录入' then
        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin
        self.MiddleName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.MiddleName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmSalesConInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;
end;

procedure TfrmSalesConInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmSalesConInPut := nil;
end;

procedure TfrmSalesConInPut.FormResize(Sender: TObject);
begin
  inherited;
  if self.WindowState = wsMinimized then
    SetShowModalHide(application.MainForm.Handle, self);
end;

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

  readCxGrid(trim(self.Caption), Tv1, '销售合同管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  InitImage();
end;

function TfrmSalesConInPut.SaveData(): Boolean;
var
  maxno, maxsno: string;
begin

  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FConMId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'CM', 'BS_Contract_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      maxno := Trim(FConMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BS_Contract_Main where ConMId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FConMId) = '' then
      begin
        Append;
        FieldByName('Fillid').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editid').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('status').Value := '0';
      FieldByName('ConMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'BS_Contract_Main', ScrollBox1, 2);
      RTSetsavedata(ADOCmd, 'BS_Contract_Main', pnl1, 2);
      FieldByName('BuyNo').Value := Trim(BuyName.Properties.LookupItems.Text);
      FieldByName('SellNo').Value := Trim(Sellname.Properties.LookupItems.Text);
      FieldByName('MiddleNo').Value := Trim(MiddleName.Properties.LookupItems.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Contract_Main where ConNo=''' + Trim(ConNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ConNo.Text := '自动生成';
      raise Exception.Create('合同号重复！');
    end;
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        maxsno := Trim(Order_Sub.fieldbyname('ConSId').AsString);
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Contract_Sub where   ConSId=''' + Trim(maxsno) + '''');
          sql.Add(' and ConMId=''' + Trim(FConMId) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'BS_Contract_Sub', 0);
          FieldByName('ConMId').Value := Trim(maxno);
          FieldByName('ConSId').Value := Trim(maxsno);
          Post;
        end;
        Next;
      end;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('update BS_Contract_Sub  set ActualUnitprice=ConPrice');
      SQL.Add(' where   ISNULL(ActualUnitprice,0)=0 and ConMId=''' + Trim(FConMId) + '''');
      ExecSQL;
    end;

////////////////       保存子表          //////////////////////
///
////////////////       更新合同          //////////////////////
//    with ADOCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('EXEC  P_Con_Up_PiQty   ');
//      sql.Add('@DataID=' + quotedstr(Trim(FConMId)));
//      execsql;
//    end;
////////////////       更新合同          //////////////////////
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('EXEC  P_Authority_Controls   ');
      sql.Add(' @DataType =''销售合同'' ');
      sql.Add(',@DataCode=' + quotedstr(Trim(FConMId)));
      execsql;
    end;

    ADOCmd.Connection.CommitTrans;

    FConMId := Trim(maxno);
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_Contract_Sub   where ConMId=''' + Trim(FConMId) + '''');
      sql.Add(' order by SerialNo ');
      Open;
    end;
    SCreateCDS(ADOTemp, Order_Sub);
    SInitCDSData(ADOTemp, Order_Sub);

    Result := True;
  except

    ADOCmd.Connection.RollbackTrans;

    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    Result := False;
  end;
end;

procedure TfrmSalesConInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;

  if TestData() = false then
    exit;

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

procedure TfrmSalesConInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FConQty, FConPrice, FC_Money, FConCost: Double;        //个数，单价，金额
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FConQty := Fieldbyname('ConQty').AsFloat;
    FConPrice := Fieldbyname('ConPrice').AsFloat;
    FConCost := Fieldbyname('ConCost').AsFloat;
  end;
  FC_Money := RoundFloat(FConQty * FConPrice + FConCost, 2);

  with Order_Sub do
  begin
    Edit;
    FieldByName('ConQty').Value := FConQty;
    FieldByName('ConPrice').Value := FConPrice;
    FieldByName('ConTotalAmount').Value := FC_Money;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmSalesConInPut.PackSealBtnUpClick(Sender: TObject);
begin
  if FConMId = '' then
  begin

  end;
  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := self.fFlileFlag;
      FDataId := trim(Self.FConMId);
      FPictureName := TRIM(PackSeal.Text);
      FWidth := 450;
      FHeight := 600;
//      FTFType := 'HX';
      FTFType := 'YP';
      if showmodal = 1 then
      begin
        Self.PackSeal.Text := frmPictureUpload.FPictureName;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
  InitImage();
end;

procedure TfrmSalesConInPut.BuyNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
  TcxButtonEdit(Sender).Text := '';
end;

procedure TfrmSalesConInPut.BuyRepresentativeDblClick(Sender: TObject);
begin
  self.BuyRepresentative.Text := '';
  self.BuyTel.Text := '';
end;

procedure TfrmSalesConInPut.BuyRepresentativePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanyContactSel := TfrmCompanyContactSel.Create(Application);
    with frmCompanyContactSel do
    begin
      FCoCode := Trim(self.BuyName.Properties.LookupItems.Text);
      if ShowModal = 1 then
      begin
        self.BuyAddress.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('CoAddress').AsString);
        self.BuyRepresentative.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('Contacts').AsString);
        self.BuyTel.Text := Trim(frmCompanyContactSel.CDS_1.fieldbyname('Telephone').AsString);
      end;
    end;

  finally
    frmCompanyContactSel.Free;
  end;
end;

procedure TfrmSalesConInPut.CurrencyPropertiesChange(Sender: TObject);
begin
  if Currency.text = 'CNY' then
  begin
    ExchangeRate.text := '1';
  end
  else
  begin
    ExchangeRate.text := '';
  end;

end;

procedure TfrmSalesConInPut.ParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
//      if Self.FAuthority = '录入' then
//        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin
        self.BuyName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.BuyName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);

        self.SalesId.Text := Trim(frmCompanySel.CDS_1.fieldbyname('SalesId').AsString);
        self.Saleser.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Saleser').AsString);
        self.TallyId.Text := Trim(frmCompanySel.CDS_1.fieldbyname('TallyId').AsString);
        self.Tallyer.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Tallyer').AsString);

        self.BuyAddress.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').AsString);
        self.BuyRepresentative.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Contacts').AsString);
        self.BuyTel.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Telephone').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmSalesConInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if GetLSNo(ADOCmd, maxno, 'CS', 'BS_Contract_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  if Trim(ConNo.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('ConSId').Value := maxno;
    FieldByName('C_Color').Value := '';
    FieldByName('ConQty').Value := null;
    FieldByName('C_StyleNo').Value := BuyConNo.Text;
    FieldByName('DlyDateSub').Value := DlyDate.Date;

    Post;
  end;
  SetXH();
end;

procedure TfrmSalesConInPut.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 ConSId=''' + Trim(Order_Sub.fieldbyname('ConSId').AsString) + '''');
//      showmessage(sql.Text);
      open;
    end;

    if Trim(Order_Sub.fieldbyname('ConSId').AsString) <> '' then
    begin
      if ADOTemp.IsEmpty = false then
      begin
        application.MessageBox('该明细已经在生产！', '提示信息');
        exit;
      end;
    end;

    if Trim(Order_Sub.fieldbyname('ConSId').AsString) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;

        sql.Add('update BS_Contract_Sub set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()    ');
        sql.Add('where ConSId=' + QuotedStr(Trim(Order_Sub.fieldbyname('ConSId').AsString)));

        sql.Add('insert into BS_Contract_Sub_Del select * from BS_Contract_Sub where ConSId=' + QuotedStr(Trim(Order_Sub.fieldbyname('ConSId').AsString)));

        sql.Add('delete BS_Contract_Sub where ConSId=''' + Trim(Order_Sub.fieldbyname('ConSId').AsString) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.Delete;
  end;

  SetXH();
end;

procedure TfrmSalesConInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '销售合同管理');
end;

procedure TfrmSalesConInPut.ToolButton4Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmMarketValueSel := TfrmMarketValueSel.Create(Application);
    with frmMarketValueSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if GetLSNo(ADOCmd, maxno, 'CS', 'BS_Contract_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
          self.Sellname.Properties.LookupItems.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SellNo').AsString);
          self.Sellname.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SellName').AsString);
          self.SellAddress.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SellAddress').AsString);
          self.SellRepresentative.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SellRepresentative').AsString);
          self.SellTel.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SellTel').AsString);

          self.BuyName.Properties.LookupItems.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('BuyNo').AsString);
          self.BuyName.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('BuyName').AsString);
          self.BuyAddress.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('BuyAddress').AsString);
          self.BuyRepresentative.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('BuyRepresentative').AsString);
          self.BuyTel.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('BuyTel').AsString);

          self.SalesId.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('SalesId').AsString);
          self.Saleser.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('Saleser').AsString);
          self.TallyId.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('TallyId').AsString);
          self.Tallyer.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('TallyId').AsString);

          self.MiddleName.Properties.LookupItems.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('MiddleNo').AsString);
          self.MiddleName.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('MiddleName').AsString);
          self.DlyNote.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('DlyNote').AsString);
          self.Payment.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('Payment').AsString);
          self.PriceTerms.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('JiaGeSY').AsString);
          self.ExchangeRate.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('ExchangeRate').AsString);
          self.ShipMent.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('ShipMent').AsString);
          self.BegPort.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('QiYun').AsString);
          self.EndPort.Text := Trim(frmMarketValueSel.CDS_1.fieldbyname('MuDi').AsString);
          self.Currency.ItemIndex := Currency.Properties.Items.IndexOf(trim(frmMarketValueSel.CDS_1.fieldbyname('Currency').AsString));
          self.IsTax.ItemIndex := IsTax.Properties.Items.IndexOf(trim(frmMarketValueSel.CDS_1.fieldbyname('IsTax').AsString));

          with self.Order_Sub do
          begin
            Append;
            FieldByName('SSel').Value := false;
            FieldByName('ConSId').Value := maxno;
            Fieldbyname('C_Code').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Code').AsString);
            Fieldbyname('SerialNo').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('C_Name').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Name').AsString);
            Fieldbyname('C_EName').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_EName').AsString);
            Fieldbyname('C_Spec').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Spec').AsString);
            Fieldbyname('C_Composition').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Composition').AsString);
            Fieldbyname('GC_Name').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('GC_Name').AsString);
            Fieldbyname('CraftCode').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('CraftCode').AsString);
            Fieldbyname('C_Color').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_EColor').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_EColor').AsString);
            Fieldbyname('C_EComposition').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_EComposition').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_StyleNo').value := BuyConNo.Text; // trim(frmMarketValueSel.CDS_1.Fieldbyname('C_StyleNo').AsString);
            Fieldbyname('C_Pattern').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_Width').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_Width').AsString);
            Fieldbyname('C_GramWeight').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            Fieldbyname('ConUnit').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('QtyUnit').AsString);
            Fieldbyname('ConSNote').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('MVCSNote').AsString);
            Fieldbyname('CraftEName').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('CraftEName').AsString);
            Fieldbyname('CraftName').value := trim(frmMarketValueSel.CDS_1.Fieldbyname('CraftName').AsString);
            FieldByName('DlyDateSub').Value := DlyDate.Date;
            Fieldbyname('ConPrice').value := frmMarketValueSel.CDS_1.Fieldbyname('Price').value;
            Post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmMarketValueSel.Free;
  end;
  SetXH();
end;

procedure TfrmSalesConInPut.ToolButton5Click(Sender: TObject);      //打色
var
  maxno: string;
begin
  try
    frmPlanColorSel := TfrmPlanColorSel.Create(Application);
    with frmPlanColorSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if GetLSNo(ADOCmd, maxno, 'CS', 'BS_Contract_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
          self.BuyConNo.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('BuyConNo').AsString);
          self.Sellname.Properties.LookupItems.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('OurCoNo').AsString);
          self.Sellname.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('OurCoName').AsString);
          self.BuyName.Properties.LookupItems.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('CustNo').AsString);
          self.BuyName.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('CustName').AsString);
          self.SalesId.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('SalesId').AsString);
          self.Saleser.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('Saleser').AsString);
          self.TallyId.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('TallyId').AsString);
          self.Tallyer.Text := Trim(frmPlanColorSel.CDS_1.fieldbyname('TallyId').AsString);

          with self.Order_Sub do
          begin
            Append;
            FieldByName('SSel').Value := false;
            FieldByName('ConSId').Value := maxno;
            Fieldbyname('C_Code').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Code').AsString);
            Fieldbyname('SerialNo').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('C_Name').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Name').AsString);
            Fieldbyname('C_EName').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_EName').AsString);
            Fieldbyname('C_Spec').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Spec').AsString);
            Fieldbyname('C_Composition').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Composition').AsString);
            Fieldbyname('GC_Name').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('GC_Name').AsString);
            Fieldbyname('CraftCode').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('CraftCode').AsString);
            Fieldbyname('C_Color').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_EColor').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_EColor').AsString);

            Fieldbyname('C_EComposition').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_EComposition').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_StyleNo').value := BuyConNo.Text; // trim(frmPlanColorSel.CDS_1.Fieldbyname('C_StyleNo').AsString);
            Fieldbyname('C_Pattern').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_Width').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Width').AsString);
            Fieldbyname('C_GramWeight').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
            Fieldbyname('ConSNote').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('OrdSNote').AsString);
            Fieldbyname('CraftEName').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('CraftEName').AsString);
            Fieldbyname('CraftName').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('CraftName').AsString);
            Fieldbyname('C_ColorConfirm').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_ColorConfirm').AsString);
            FieldByName('DlyDateSub').Value := DlyDate.Date;
            Post;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

      end;
    end;
  finally
    frmPlanColorSel.Free;
  end;
  SetXH();
end;

procedure TfrmSalesConInPut.ToolButton6Click(Sender: TObject);
begin
  if TRIM(ConType.Text) = '外销' then
  begin
    ConTerms.Text := '1.PI executed in Jimei District, Xiamen City, China. ' + #13;
    ConTerms.Text := ConTerms.Text + '2.Transhipment and partial shipment: To be negotiated.' + #13;
    ConTerms.Text := ConTerms.Text + '3.Payment:' + #13;
    ConTerms.Text := ConTerms.Text + '   Beneficiary :Xiamen Upward Textile Co., Ltd. ;' + #13;
    ConTerms.Text := ConTerms.Text + '   Beneficiary Bank: CITIBANK N.A.HONG KONG BRANCH' + #13;
    ConTerms.Text := ConTerms.Text + '   Bank Address: 20/F, TOWER ONE, TIMES SQUARE, 1 MATHESON STREET, CAUSEWAY BAY, HONG KONG' + #13;
    ConTerms.Text := ConTerms.Text + '   Beneficiary’s A/C No: 3974000048354; Swift Code: CITIHKHX or CITIHKHXXXX' + #13;
    ConTerms.Text := ConTerms.Text + '4.This agreement is made in duplicate and shall take effect upon due execution of the same by the parties hereto. ' + #13;
    ConTerms.Text := ConTerms.Text + '5.Mill’s fabrics inspection standard: ASTM D 5430 4 points system, ≤20 points per 100 square yard(28 points for lining  ' + #13;
    ConTerms.Text := ConTerms.Text + '6.In the event of quality dispute, Buyer must advise Seller defective details within two months, no claim can be entertained if cut without Seller’s approval.' + #13;
    ConTerms.Text := ConTerms.Text + '7.This contract should be counter-signed before within 3 days Any delay will result in another delivery date.' + #13;
    ConTerms.Text := ConTerms.Text + '8.Any amendment of this PI without the approval of Seller is considered invalid.' + #13;
    ConTerms.Text := ConTerms.Text + '9.Buyer must take delivery of cargo within one month after production completion.' + #13;
    ConTerms.Text := ConTerms.Text + '10.The jurisdiction of the contract shall be under the jurisdiction of the court by the plaintiff is located.' + #13;

  end
  else
  begin
    ConTerms.Text := '1.合同签订地：厦门 集美。' + #13;
    ConTerms.Text := ConTerms.Text + '2.运输方式：由供方代办运输,送到需方工厂，运费由供方支付。 ' + #13;
    ConTerms.Text := ConTerms.Text + '3.付款方式：         ' + #13;
    ConTerms.Text := ConTerms.Text + '4.此合同一式二份,供需双方各执一份,双方签字盖章生效。' + #13;
    ConTerms.Text := ConTerms.Text + '5.产品外观检验标准以ASTM D 5430四分制≤20分（里布28分）每100平方码为准（接匹不扣分）。 ' + #13;
    ConTerms.Text := ConTerms.Text + '6.当供方品质出现问题时,请需方不要裁剪,需方先书面通知供方,如需方先裁剪,供方不受理客诉，正常客诉期限为交货后二个月之内，逾期未提出视为合格。' + #13;
    ConTerms.Text := ConTerms.Text + '7.合同请在3日内签回，否则完工日期另议。' + #13;
    ConTerms.Text := ConTerms.Text + '8.合同如有涂改，未经卖方同意，则属无效。' + #13;
    ConTerms.Text := ConTerms.Text + '9.货物完成后一个月内，买方须提货。' + #13;
    ConTerms.Text := ConTerms.Text + '10.扫描件、传真件及照相回签的合同，均具有法律效力。' + #13;
    ConTerms.Text := ConTerms.Text + '11.合同管辖权由提出异议方所在地法院管辖' + #13;
  end;
end;

procedure TfrmSalesConInPut.ToolButton7Click(Sender: TObject);
var
  maxno, fDate, fconNO: string;
begin
  while true do
  begin
    try
      ADO_XF.Connection.BeginTrans;
      if GetLSNo(ADO_XF, maxno, 'CM', 'BS_Contract_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');
      with ADO_XF do
      begin
        close;
        sql.Clear;
        sql.Add('select * from BS_Contract_Main A');
        sql.Add('where ConMId=' + quotedstr(Trim(maxno)));
        open;
      end;
      if ADO_XF.IsEmpty then
      begin
        raise Exception.Create('修复完毕！');
      end;
      ADO_XF.Connection.CommitTrans;
    except
      ADO_XF.Connection.RollbackTrans;
      Break;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end;
  while true do
  begin
    fDate := 'UX' + FormatDateTime('yyMM', SGetServerDate(ADOTemp));
    try
      ADO_XF.Connection.BeginTrans;
      if GetLSNo(ADOTemp, fconNO, fDate, 'BS_Contract_Main', 3, 0) = False then
        raise Exception.Create('取最合同编号失败！');
      with ADO_XF do
      begin
        close;
        sql.Clear;
        sql.Add('select * from BS_Contract_Main A');
        sql.Add('where ConNo=' + quotedstr(Trim(fconNO)));
        open;
      end;
      if ADO_XF.IsEmpty then
      begin
        raise Exception.Create('修复完毕！');
      end
      else
      begin
        if FConMId = '' then
          ConNo.Text := '自动生成';
      end;
      ADO_XF.Connection.CommitTrans;
    except
      ADO_XF.Connection.RollbackTrans;
      Break;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end;

end;

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

  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmSalesConInPut.cxDBImage1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(Qry_ImgHX.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

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

procedure TfrmSalesConInPut.ExchangeRateKeyPress(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('.', TcxTextEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

procedure TfrmSalesConInPut.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 TfrmSalesConInPut.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 TfrmSalesConInPut.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 TfrmSalesConInPut.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPlanColorSel := TfrmPlanColorSel.Create(Application);
    with frmPlanColorSel do
    begin
      FC_Color := Trim(self.Order_Sub.Fieldbyname('C_Color').AsString);
      if ShowModal = 1 then
      begin

        with self.Order_Sub do
        begin
          Edit;
          Fieldbyname('C_ColorNo').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
          Fieldbyname('C_Color').value := trim(frmPlanColorSel.CDS_1.Fieldbyname('C_Color').AsString);
          Post;
        end;

      end;
    end;
  finally
    frmPlanColorSel.Free;
  end;
end;

procedure TfrmSalesConInPut.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('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_EComposition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_EComposition').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.fieldbyname('GC_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('GC_Name').asstring;
      Order_Sub.fieldbyname('CraftCode').Value := frmClothInfoSel.CDS_1.fieldbyname('CraftCode').asstring;
      Order_Sub.fieldbyname('CraftName').Value := frmClothInfoSel.CDS_1.fieldbyname('CraftName').asstring;
      Order_Sub.fieldbyname('CraftEName').Value := frmClothInfoSel.CDS_1.fieldbyname('CraftEName').asstring;
      Order_Sub.Post;
    end;
    free;
  end;

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

procedure TfrmSalesConInPut.SaleserDblClick(Sender: TObject);
begin
  self.SalesId.Text := '';
  self.Saleser.Text := '';
end;

procedure TfrmSalesConInPut.SaleserPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '业务';
      if ShowModal = 1 then
      begin

        self.SalesId.Text := Trim(CDS_1.fieldbyname('UserID').AsString);
        self.Saleser.Text := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmSalesConInPut.SalesmanBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(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
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmSalesConInPut.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;

end.

