unit U_SalesConInPut;

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,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI, dxScrollbarAnnotations,
  dxCore, cxDateUtils, cxImageList, cxCurrencyEdit, dxCoreGraphics;

type
  TfrmSalesConInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTPrice: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    Order_Sub: TClientDataSet;
    v1PRTOrderQty: TcxGridDBColumn;
    Label9: TLabel;
    Label10: TLabel;
    ConNo: TcxTextEdit;
    Label14: TLabel;
    Label20: TLabel;
    v1XHNo: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    Label6: TLabel;
    Label34: TLabel;
    BuyConNo: TcxTextEdit;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    cxgrdpmn1: TcxGridPopupMenu;
    v1Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label26: TLabel;
    Label27: TLabel;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Label29: TLabel;
    Label30: TLabel;
    Label2: TLabel;
    ExchangeRate: TcxTextEdit;
    Label5: TLabel;
    Label7: TLabel;
    CommissionRatio: TcxTextEdit;
    Label11: TLabel;
    Label12: TLabel;
    Label4: TLabel;
    Label13: TLabel;
    pm_sub: TPopupMenu;
    Tv1Column1: TcxGridDBColumn;
    N1: TMenuItem;
    Tv1Column7: TcxGridDBColumn;
    DataSource1: TDataSource;
    ADOQueryImage: TADOQuery;
    cxDBImage1: TcxDBImage;
    DS_ImgHX: TDataSource;
    Qry_ImgHX: TADOQuery;
    IdFTP1: TIdFTP;
    Label8: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    PackSeal: TBtnEditC;
    Label17: TLabel;
    N2: TMenuItem;
    DlyNote: TcxTextEdit;
    ToolButton3: TToolButton;
    Label23: TLabel;
    Label24: TLabel;
    Saleser: TcxTextEdit;
    Tallyer: TcxTextEdit;
    lblSalesId: TLabel;
    Label25: TLabel;
    SalesId: TcxTextEdit;
    TallyId: TcxTextEdit;
    BuyName: TcxButtonEdit;
    PackNote: TcxButtonEdit;
    Sellname: TcxButtonEdit;
    MiddleName: TcxButtonEdit;
    ShipMent: TcxButtonEdit;
    BegPort: TcxButtonEdit;
    EndPort: TcxButtonEdit;
    Payment: TcxButtonEdit;
    QtyNote: TcxButtonEdit;
    PriceTerms: TcxButtonEdit;
    ShippingSample: TcxButtonEdit;
    Pic_Image1: TcxDBImage;
    ConType: TcxComboBox;
    ConDate: TcxDateEdit;
    Currency: TcxComboBox;
    DlyDate: TcxDateEdit;
    IsTax: TcxComboBox;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Label1: TLabel;
    BPType: TcxComboBox;
    ToolButton4: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Label18: TLabel;
    GFDZ: TcxTextEdit;
    Label19: TLabel;
    KHDZ: TcxTextEdit;
    Label21: TLabel;
    BankName: TcxButtonEdit;
    Label22: TLabel;
    BankDeposit: TcxTextEdit;
    Label28: TLabel;
    BankCardNo: TcxTextEdit;
    Label31: TLabel;
    YHDZ: TcxTextEdit;
    Label32: TLabel;
    YHDM: TcxTextEdit;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1C_FinishLongitudeDensity: TcxGridDBColumn;
    Tv1C_FinishLatitudeDensity: TcxGridDBColumn;
    Label33: TLabel;
    Label35: TLabel;
    InspScore: TcxTextEdit;
    InspStandard: TcxButtonEdit;
    Tv1BCIID: 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 v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure BankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure CurrencyPropertiesChange(Sender: TObject);
    procedure Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure InspStandardPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  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_PictureUpload11, U_TatClothInfoSel, U_BankSel, U_SalesContractCPSel;

{$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.Properties.LookupItems.Clear;
// 直接添加新项（不会自动添加多余换行符）
//        self.Sellname.Properties.LookupItems.Add(Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString));
//        ShowMessage(self.Sellname.Properties.LookupItems[0]+'hh');
        self.Sellname.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.GFDZ.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').AsString);
      end;
    end;

  finally
    frmCompanySel.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.InspStandardPropertiesButtonClick(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 := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      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.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;
begin
  try
    if Trim(ConNo.Text) = '' then
      raise Exception.Create('合同号不能为空！');
    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.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('C_MF', null, []) then
//      raise Exception.Create('门幅不能为空！');
//    if Order_Sub.Locate('C_MF', '', []) then
//      raise Exception.Create('门幅不能为空！');
//    if Order_Sub.Locate('C_KZ', null, []) then
//      raise Exception.Create('克重不能为空！');
//    if Order_Sub.Locate('C_KZ', '', []) 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('ConUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  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 A.ConMId,A.ConSId,A.FROMCPMID,A.FROMCPSID ');
    sql.Add(' ,A.SerialNo,A.BCIID,A.ConQty,A.ConUnit,A.ConPrice,A.ConTotalAmount ');
    SQL.Add(' ,A.TaxRate,A.Tax,A.TaxInclude,A.JHDate,A.YDZ,A.ConSNote,A.CP_CODE,A.CP_NAME ');
    sql.add(' ,A.CP_QTY,A.CP_TPXS ');
    SQL.Add(' ,B.C_Code,B.C_Name,B.C_Spec,B.C_Composition,B.C_Color,B.C_ColorNo,B.C_Pattern ');
    SQL.Add(' ,B.C_StyleNo,B.C_FinishLongitudeDensity,B.C_FinishLatitudeDensity');
    SQL.Add(' ,B.C_Width,B.C_GramWeight');
    SQL.Add(' ,AttName=(select top 1 FileName from TP_File X where X.WBID = A.BCIID ORDER BY FillTime )');
    SQL.Add(' from BS_Contract_Sub A');
    SQL.Add(' left join BS_Cloth_info B on A.BCIID = B.BCIID ');
    sql.Add(' where A.ConMId=''' + Trim(FConMId) + '''');
//    sql.Add(' and ConSID = ''CS251011001'' ');
    sql.Add(' order by A.SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
//  ShowMessage(Order_Sub.fieldByName('TaxInclude').AsString);

   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Contract_Main where ConMId=''' + Trim(FConMId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);

    //刷新产品信息
  with ADOCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.ConMId,A.ConSId,A.FROMCPMID,A.FROMCPSID ');
    sql.Add(' ,A.SerialNo,A.BCIID,A.ConQty,A.ConUnit,A.ConPrice,A.ConTotalAmount ');
    SQL.Add(' ,A.TaxRate,A.Tax,A.TaxInclude,A.JHDate,A.YDZ,A.ConSNote,A.CP_CODE,A.CP_NAME ');
    sql.add(' ,A.CP_QTY,A.CP_TPXS ');
    SQL.Add(' ,B.C_Code,B.C_Name,B.C_Spec,B.C_Composition,B.C_Color,B.C_ColorNo,B.C_Pattern ');
    SQL.Add(' ,B.C_StyleNo,B.C_FinishLongitudeDensity,B.C_FinishLatitudeDensity');
    SQL.Add(' ,B.C_Width,B.C_GramWeight');
    SQL.Add(' ,AttName=(select top 1 FileName from TP_File X where X.WBID = A.BCIID ORDER BY FillTime )');
    SQL.Add(' from BS_Contract_Sub A');
    SQL.Add(' left join BS_Cloth_info B on A.BCIID = B.BCIID ');
    sql.Add(' where A.ConMId=''' + Trim(FConMId) + '''');
//    sql.Add(' and ConSID = ''CS251011001'' ');
    sql.Add(' order by A.SerialNo ');
    Open;
  end;
  SCreateCDS(ADOCmd, Order_Sub);
  SInitCDSData(ADOCmd, Order_Sub);

  BuyName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('BuyNo').AsString);
  Sellname.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('SellNo').AsString);
  MiddleName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('MiddleNo').AsString);

  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

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

    Saleser.Text := trim(DName);
    SalesID.Text := trim(DCode);
  end;

  if PState = '复制' then
  begin

    PState := '新增';
    FConMId := '';
    ConNo.Text := '自动生成';

    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.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.FormShow(Sender: TObject);
begin
  inherited;
  readCxGrid(trim(self.Caption), Tv1, '销售合同管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  InitImage();
end;

function TfrmSalesConInPut.SaveData(): Boolean;
var
  maxno, ZiMu: string;
  fconNO, fmxType, fDate, YueRi: 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('取最大号失败！');

      fDate := FormatDateTime('yy', SGetServerDate(ADOTemp));
      YueRi := FormatDateTime('MMDD', SGetServerDate(ADOTemp));

      if trim(ConNo.Text) = '自动生成' then
      begin

        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select ZiMu = dbo.GetPinYin(''' + Trim(BuyName.text) + ''')');
          Open;
        end;
        ZiMu := Trim(ADOTemp.fieldbyname('ZiMu').AsString);

        if GetLSNo(ADOTemp, fconNO, fDate, 'BS_Contract_Main', 4, 0) = False then
          raise Exception.Create('取最合同编号失败！');
//
//        fconNO := Copy(fconNO, 1, 4) + YueRi + Copy(fconNO, 5, 4);
        if ConType.Text = '内销' then
          fconNO := 'N' + fconNO
        else
          fconNO := 'W' + fconNO;
        ConNo.Text := fconNO;
      end;
    end
    else
    begin
      maxno := Trim(FConMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BS_Contract_Main where ConMId=''' + Trim(FConMId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FConMId) = '' 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('ConMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'BS_Contract_Main', ScrollBox1, 2);
//      ShowMessage(self.BuyName.Properties.LookupItems[0]+'hh');
//      ShowMessage(self.Sellname.Properties.LookupItems[0]+'hh');
//      ShowMessage(self.MiddleName.Properties.LookupItems.Text+'hh');
      if Trim(BuyName.Properties.LookupItems.text) = '' then
      begin
        FieldByName('BuyNo').Value := Trim(BuyName.Properties.LookupItems[0]);
      end
      else
      begin
        FieldByName('BuyNo').Value := Trim(BuyName.Properties.LookupItems[0]);
      end;
      if Trim(Sellname.Properties.LookupItems.text) = '' then
      begin
        FieldByName('SellNo').Value := Trim(Sellname.Properties.LookupItems[0]);
      end
      else
      begin
        FieldByName('SellNo').Value := Trim(Sellname.Properties.LookupItems[0]);
      end;
//      if Trim(MiddleName.Properties.LookupItems.text) = '' then
//      begin
//        FieldByName('MiddleNo').Value := Trim(MiddleName.Properties.LookupItems[0]);
//      end
//      else
//      begin
//        FieldByName('MiddleNo').Value := Trim(MiddleName.Properties.LookupItems[0]);
//      end;
      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
      raise Exception.Create('合同号重复！');

    FConMId := Trim(maxno);
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        maxno := Trim(Order_Sub.fieldbyname('ConSId').AsString);
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BS_Contract_Sub where   ConSId=''' + Trim(maxno) + '''');
          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(FConMId);
          FieldByName('ConSId').Value := Trim(maxno);
          FieldByName('FROMCPSID').Value := Trim(Order_Sub.FieldByName('FROMCPSID').AsString);
          FieldByName('FROMCPMID').Value := Trim(Order_Sub.FieldByName('FROMCPMID').AsString);

          Post;
        end;
        with Order_Sub do
        begin
          Edit;
          FieldByName('ConSId').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(FConMId)));
//      execsql;
//    end;
////////////////       更新合同          //////////////////////

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

    ADOCmd.Connection.RollbackTrans;

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

    Result := False;
  end;
end;

procedure TfrmSalesConInPut.TBSaveClick(Sender: TObject);
begin
  ConDate.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: Double;        //数量，单价，金额
  mTaxRate, mTax, mTaxInclude: Double;        //税率，税额，含税金额

  mCP_QTY, mCP_TPXS: 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 := StrToFloat(Trim(mvalue));
    Post;
//    ShowMessage(Fieldbyname('TaxRate').AsString);
    FConQty := Fieldbyname('ConQty').AsFloat;
    FConPrice := Fieldbyname('ConPrice').AsFloat;

    mTaxRate := Fieldbyname('TaxRate').AsFloat / 100;
    mTax := Fieldbyname('Tax').AsFloat;
    mTaxInclude := Fieldbyname('TaxInclude').AsFloat;

    mCP_QTY := Fieldbyname('CP_QTY').AsFloat;
    mCP_TPXS := Fieldbyname('CP_TPXS').AsFloat;
  end;

  if mCP_QTY <> 0 then
  begin
    FConQty := RoundFloat(mCP_TPXS * mCP_QTY, 0);
  end;

  FC_Money := RoundFloat(FConQty * FConPrice, 2);
  mTax := RoundFloat(FC_Money * mTaxRate, 2);
  mTaxInclude := RoundFloat(FC_Money + mTax, 2);

  with Order_Sub do
  begin
    Edit;
    FieldByName('ConQty').Value := FConQty;
//    FieldByName('ConPrice').Value := FConPrice;
    FieldByName('ConTotalAmount').Value := FC_Money;

    FieldByName('TaxRate').Value := mTaxRate * 100;
    FieldByName('Tax').Value := mTax;
    FieldByName('TaxInclude').Value := mTaxInclude;
    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).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmSalesConInPut.CurrencyPropertiesChange(Sender: TObject);
begin
  inherited;
  if Currency.Text = 'VND' then
  begin
    (Tv1Column9.Properties as TcxCurrencyEditProperties).DisplayFormat := '';
    (v1PRTPrice.Properties as TcxCurrencyEditProperties).DisplayFormat := '';
    (Tv1Column14.Properties as TcxCurrencyEditProperties).DisplayFormat := '';
    (Tv1Column15.Properties as TcxCurrencyEditProperties).DisplayFormat := '';

    (Tv1Column9.Properties as TcxCurrencyEditProperties).DecimalPlaces := 0;
    (v1PRTPrice.Properties as TcxCurrencyEditProperties).DecimalPlaces := 0;
    (Tv1Column14.Properties as TcxCurrencyEditProperties).DecimalPlaces := 0;
    (Tv1Column15.Properties as TcxCurrencyEditProperties).DecimalPlaces := 0;

    with Order_Sub do
    begin
      Edit;
      Fieldbyname('ConPrice').Value := RoundFloat(Fieldbyname('ConPrice').AsFloat, 0);
      Fieldbyname('ConTotalAmount').value := RoundFloat(Fieldbyname('ConQty').AsFloat * Fieldbyname('ConPrice').Asfloat, 0);
      Fieldbyname('Tax').Value := RoundFloat(Fieldbyname('Tax').AsFloat, 0);
      Fieldbyname('TaxInclude').value := RoundFloat(Fieldbyname('TaxInclude').AsFloat, 0);
      Post;
    end;

  end
  else
  begin
    (Tv1Column9.Properties as TcxCurrencyEditProperties).DisplayFormat := '0.00';
    (v1PRTPrice.Properties as TcxCurrencyEditProperties).DisplayFormat := '0.00';
    (Tv1Column14.Properties as TcxCurrencyEditProperties).DisplayFormat := '0.00';
    (Tv1Column15.Properties as TcxCurrencyEditProperties).DisplayFormat := '0.00';
    (Tv1Column9.Properties as TcxCurrencyEditProperties).DecimalPlaces := 2;
    (v1PRTPrice.Properties as TcxCurrencyEditProperties).DecimalPlaces := 2;
    (Tv1Column14.Properties as TcxCurrencyEditProperties).DecimalPlaces := 2;
    (Tv1Column15.Properties as TcxCurrencyEditProperties).DecimalPlaces := 2;
  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.KHDZ.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').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('ConUnit').Value := 'M';
    Post;
  end;
  SetXH();
end;

procedure TfrmSalesConInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select top 1 1 from Tat_Plan_Sub where ConNo = ' + QuotedStr(Trim(ConNo.Text)));
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('该合同已生成织造单，不能删除明细！', '提示', 0);
      Exit;
    end;
  end;

  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) + '''');
      open;
    end;
    if ADOTemp.IsEmpty = false then
    begin
      exit;
      application.MessageBox('该明细已经在生产！', '提示信息');
    end;
    if Trim(Order_Sub.fieldbyname('ConSId').AsString) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        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
  i: Integer;
  maxno: string;
  MCYNo: string;
begin

  if Trim(ConNo.Text) = '' then
  begin
    Application.MessageBox('合同号不能为空!', '提示', 0);
    Exit;
  end;

  i := Order_Sub.RecordCount;      //当前的dataset有多少行记录


  frmTatClothInfoSel := TfrmTatClothInfoSel.Create(self);
  with frmTatClothInfoSel do
  begin
    if showmodal = 1 then
    begin

      with CDS_1 do
      begin
        DisableControls;
        First;
        while not eof do
        begin
          if CDS_1.fieldbyname('SSel').AsBoolean then
          begin

            if GetLSNo(ADOCmd, maxno, 'CS', 'BS_Contract_Sub', 4, 1) = False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取子流水号失败！', '提示', 0);
              Exit;
            end;

            with Self.Order_Sub do
            begin
              append;

              Order_Sub.fieldbyname('BCIID').Value := frmTatClothInfoSel.CDS_1.fieldbyname('BCIID').asstring;
              Order_Sub.fieldbyname('C_Code').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Code').Value;
              Order_Sub.fieldbyname('C_Name').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Code').Value;
              Order_Sub.fieldbyname('C_Spec').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Spec').Value;
              Order_Sub.fieldbyname('C_Composition').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Composition').Value;
              Order_Sub.fieldbyname('C_Color').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Color').Value;
              Order_Sub.fieldbyname('C_ColorNo').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_ColorNo').Value;
              Order_Sub.fieldbyname('C_Pattern').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Pattern').Value;
              Order_Sub.fieldbyname('C_StyleNo').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_StyleNo').Value;
              Order_Sub.fieldbyname('C_FinishLongitudeDensity').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_FinishLongitudeDensity').Value;
              Order_Sub.fieldbyname('C_FinishLatitudeDensity').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_FinishLatitudeDensity').Value;
              Order_Sub.fieldbyname('C_Width').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_Width').Value;
              Order_Sub.fieldbyname('C_GramWeight').Value := frmTatClothInfoSel.CDS_1.FieldByName('C_GramWeight').Value;

              Order_Sub.FieldByName('SSEL').Value := FALSE;
              Order_Sub.FieldByName('SerialNo').Value := i;
              Order_Sub.FieldByName('ConSId').Value := maxno;
              Order_Sub.FieldByName('ConQty').Value := null;
              Order_Sub.FieldByName('ConTotalAmount').Value := null;
              Order_Sub.FieldByName('ConUnit').Value := 'M';
            end;
          end;
          Next;
          i := i + 1;
        end;
        EnableControls;
      end;

    end;
    free;
  end;

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

procedure TfrmSalesConInPut.ToolButton5Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
  MCYNo: string;
begin
  try
    frmSalesContractCPSel := TfrmSalesContractCPSel.Create(Application);
    with frmSalesContractCPSel do
    begin

      if ShowModal = 1 then
      begin
        Self.BuyConNo.Text := frmSalesContractCPSel.CDS_1.fieldbyname('ConNO').asString;
        Self.InspStandard.Text := frmSalesContractCPSel.CDS_1.fieldbyname('InspStandard').asString;
        Self.InspScore.Text := frmSalesContractCPSel.CDS_1.fieldbyname('InspScore').asString;
//        Self.BuyName.Text := frmSalesContractCPSel.CDS_1.fieldbyname('BuyName').asString;
        with CDS_1 do
        begin
          DisableControls;
          First;
          while not eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              if GetLSNo(ADOCmd, maxno, 'CS', 'BS_Contract_Sub', 4, 1) = False then
              begin
                ADOCmd.Connection.RollbackTrans;
                Application.MessageBox('取子流水号失败！', '提示', 0);
                Exit;
              end;

              with Self.Order_Sub do
              begin
                append;

//                Order_Sub.fieldbyname('BCIID').Value := frmSalesContractCPSel.CDS_1.fieldbyname('BCIID').asstring;
//                Order_Sub.fieldbyname('ConQty').Value := frmSalesContractCPSel.CDS_1.fieldbyname('ConQty').asstring;
//                Order_Sub.fieldbyname('ConPrice').Value := frmSalesContractCPSel.CDS_1.fieldbyname('ConPrice').asstring;
//                Order_Sub.fieldbyname('ConTotalAmount').Value := frmSalesContractCPSel.CDS_1.fieldbyname('ConTotalAmount').asstring;
//                Order_Sub.fieldbyname('TaxRate').Value := frmSalesContractCPSel.CDS_1.fieldbyname('TaxRate').asstring;
//                Order_Sub.fieldbyname('Tax').Value := frmSalesContractCPSel.CDS_1.fieldbyname('Tax').asstring;
//                Order_Sub.fieldbyname('TaxInclude').Value := frmSalesContractCPSel.CDS_1.fieldbyname('TaxInclude').asstring;
//                Order_Sub.fieldbyname('JHDate').Value := frmSalesContractCPSel.CDS_1.fieldbyname('JHDate').asstring;
                Order_Sub.fieldbyname('YDZ').Value := frmSalesContractCPSel.CDS_1.fieldbyname('YDZ').asstring;
//                Order_Sub.fieldbyname('ConSNote').Value := frmSalesContractCPSel.CDS_1.fieldbyname('ConSNote').asstring;
                Order_Sub.fieldbyname('CP_Code').Value := frmSalesContractCPSel.CDS_1.fieldbyname('C_Code').asstring;
                Order_Sub.fieldbyname('CP_Name').Value := frmSalesContractCPSel.CDS_1.fieldbyname('C_Name').asstring;
//                Order_Sub.fieldbyname('FromCPConNo').Value := frmSalesContractCPSel.CDS_1.fieldbyname('ConNo').asstring;
                Order_Sub.fieldbyname('FROMCPSID').Value := frmSalesContractCPSel.CDS_1.fieldbyname('CONSID').asstring;
                Order_Sub.fieldbyname('FROMCPMID').Value := frmSalesContractCPSel.CDS_1.fieldbyname('CONMID').asstring;
//                Order_Sub.FieldByName('CP_QTY').Value := frmSalesContractCPSel.CDS_1.fieldbyname('CP_QTY').ASFLOAT;
//                Order_Sub.FieldByName('CP_TPXS').Value := frmSalesContractCPSel.CDS_1.fieldbyname('CP_TPXS').ASFLOAT;

                Order_Sub.fieldbyname('ConUnit').Value := 'M';

                Order_Sub.FieldByName('SSEL').Value := FALSE;
                Order_Sub.FieldByName('SerialNo').Value := i;
                Order_Sub.FieldByName('ConSId').Value := maxno;

              end;

              with ADOQueryCmd do
              begin
                close;
                SQL.Clear;
                sql.Add(' select top(1) A.ConMId,A.ConSId,A.FROMCPMID,A.FROMCPSID ');
                sql.Add(' ,A.SerialNo,A.BCIID,A.ConQty,A.ConUnit,A.ConPrice,A.ConTotalAmount ');
                SQL.Add(' ,A.TaxRate,A.Tax,A.TaxInclude,A.JHDate,A.YDZ,A.ConSNote,A.CP_CODE,A.CP_NAME ');
                sql.add(' ,A.CP_QTY,A.CP_TPXS ');
                SQL.Add(' ,B.C_Code,B.C_Name,B.C_Spec,B.C_Composition,B.C_Color,B.C_ColorNo,B.C_Pattern ');
                SQL.Add(' ,B.C_StyleNo,B.C_FinishLongitudeDensity,B.C_FinishLatitudeDensity');
                SQL.Add(' ,B.C_Width,B.C_GramWeight');
                SQL.Add(' ,AttName=(select top 1 FileName from TP_File X where X.WBID = A.BCIID ORDER BY FillTime )');
                SQL.Add(' from BS_Contract_Sub A');
                SQL.Add(' left join BS_Cloth_info B on A.BCIID = B.BCIID ');
                SQL.Add(' where CP_Code = ' + QuotedStr(Trim(frmSalesContractCPSel.CDS_1.fieldbyname('C_Code').asstring)));
                ShowMessage(sql.Text);
                Open;
              end;

              Order_Sub.fieldbyname('ConQty').Value := ADOQueryCmd.fieldbyname('ConQty').Value;
              Order_Sub.fieldbyname('ConPrice').Value := ADOQueryCmd.fieldbyname('ConPrice').Value;
              Order_Sub.fieldbyname('ConTotalAmount').Value := ADOQueryCmd.fieldbyname('ConTotalAmount').Value;
              Order_Sub.fieldbyname('TaxRate').Value := ADOQueryCmd.fieldbyname('TaxRate').Value;
              Order_Sub.fieldbyname('Tax').Value := ADOQueryCmd.fieldbyname('Tax').Value;
              Order_Sub.fieldbyname('TaxInclude').Value := ADOQueryCmd.fieldbyname('TaxInclude').Value;
              Order_Sub.fieldbyname('CP_QTY').Value := ADOQueryCmd.fieldbyname('CP_QTY').Value;
              Order_Sub.fieldbyname('CP_TPXS').Value := ADOQueryCmd.fieldbyname('CP_TPXS').Value;

              Order_Sub.fieldbyname('C_Code').Value := ADOQueryCmd.FieldByName('C_Code').Value;
              Order_Sub.fieldbyname('C_Name').Value := ADOQueryCmd.FieldByName('C_Name').Value;
              Order_Sub.fieldbyname('C_Spec').Value := ADOQueryCmd.FieldByName('C_Spec').Value;
              Order_Sub.fieldbyname('C_Composition').Value := ADOQueryCmd.FieldByName('C_Composition').Value;
              Order_Sub.fieldbyname('C_Color').Value := ADOQueryCmd.FieldByName('C_Color').Value;
              Order_Sub.fieldbyname('C_ColorNo').Value := ADOQueryCmd.FieldByName('C_ColorNo').Value;
              Order_Sub.fieldbyname('C_Pattern').Value := ADOQueryCmd.FieldByName('C_Pattern').Value;
              Order_Sub.fieldbyname('C_StyleNo').Value := ADOQueryCmd.FieldByName('C_StyleNo').Value;
              Order_Sub.fieldbyname('C_FinishLongitudeDensity').Value := ADOQueryCmd.FieldByName('C_FinishLongitudeDensity').Value;
              Order_Sub.fieldbyname('C_FinishLatitudeDensity').Value := ADOQueryCmd.FieldByName('C_FinishLatitudeDensity').Value;
              Order_Sub.fieldbyname('C_Width').Value := ADOQueryCmd.FieldByName('C_Width').Value;
              Order_Sub.fieldbyname('C_GramWeight').Value := ADOQueryCmd.FieldByName('C_GramWeight').Value;
              Order_Sub.fieldbyname('AttName').Value := ADOQueryCmd.FieldByName('AttName').Value;
            end;
            Next;
            i := i + 1;
          end;
          EnableControls;
        end;
      end;
    end;
  finally
    frmSalesContractCPSel.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
  SetXH();
end;

procedure TfrmSalesConInPut.Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'TaxRate';
      flagname := '税率';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('TaxRate').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.Order_Sub.Post;
      end;
    end;
    Tv1Column13.Properties.OnEditValueChanged(Sender);
  finally
    frmZDYHelp.Free;

  end;
end;

procedure TfrmSalesConInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPictureUpload11 := TfrmPictureUpload11.create(self);
    with frmPictureUpload11 do
    begin
      fFlileFlag := self.fFlileFlag;
      FDataId := Trim(self.Order_Sub.fieldbyname('AttName').AsString);
      FPictureName := Trim(self.Order_Sub.fieldbyname('AttName').AsString);
      FWidth := 1000;
      FHeight := 1000;
      FTFType := 'HX';
      if showmodal = 1 then
      begin
        with self.Order_Sub do
        begin
          Edit;
          Fieldbyname('AttName').Value := frmPictureUpload11.FPictureName;
          Post;
        end;
      end;
    end;
  finally
    frmPictureUpload11.free;
  end;

  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' and   FileName=' + quotedstr(Trim(self.Order_Sub.fieldbyname('AttName').AsString)));
    open;
  end;
  if ADOTemp.IsEmpty then
  begin
    with self.Order_Sub do
    begin
      Edit;
      Fieldbyname('AttName').Value := '';
      Post;
    end;
  end;
end;

procedure TfrmSalesConInPut.BankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmBankSel := TfrmBankSel.Create(Application);
    with frmBankSel do
    begin
      if ShowModal = 1 then
      begin
        self.BankName.Text := Trim(frmBankSel.CDS_1.fieldbyname('BankName').AsString);
        self.BankDeposit.Text := Trim(frmBankSel.CDS_1.fieldbyname('BankDeposit').AsString);

        self.BankCardNo.Text := Trim(frmBankSel.CDS_1.fieldbyname('BankCardNo').AsString);
        self.YHDZ.Text := Trim(frmBankSel.CDS_1.fieldbyname('YHDZ').AsString);

        self.YHDM.Text := Trim(frmBankSel.CDS_1.fieldbyname('YHDM').AsString);
        self.Currency.Text := Trim(frmBankSel.CDS_1.fieldbyname('Currency').AsString);
      end;
    end;

  finally
    frmBankSel.Free;
  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.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ConUnit';
      flagname := '单位';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('ConUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSalesConInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin

  frmTatClothInfoSel := TfrmTatClothInfoSel.Create(self);
  with frmTatClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      Order_Sub.Edit;
      Order_Sub.fieldbyname('BCIID').Value := frmTatClothInfoSel.CDS_1.fieldbyname('BCIID').asstring;
      Order_Sub.fieldbyname('C_Code').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Code').asstring;
      Order_Sub.fieldbyname('C_Name').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;

      Order_Sub.fieldbyname('C_Spec').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      Order_Sub.fieldbyname('C_Composition').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;

      Order_Sub.fieldbyname('C_Color').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Color').asstring;
      Order_Sub.fieldbyname('C_ColorNo').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_ColorNo').asstring;

      Order_Sub.fieldbyname('C_Pattern').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Pattern').asstring;
      Order_Sub.fieldbyname('C_StyleNo').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_StyleNo').asstring;

      Order_Sub.fieldbyname('C_Width').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Width').asstring;
      Order_Sub.fieldbyname('C_GramWeight').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_GramWeight').asstring;

      Order_Sub.fieldbyname('AttName').Value := frmTatClothInfoSel.CDS_1.fieldbyname('AttName').asstring;

      Order_Sub.fieldbyname('C_FinishLatitudeDensity').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_FinishLatitudeDensity').asstring;
      Order_Sub.fieldbyname('C_FinishLongitudeDensity').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_FinishLongitudeDensity').asstring;

      Order_Sub.Post;
    end;
    free;
  end;


//  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_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 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.

