unit U_ClothPurchasePlanPut;

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
  TfrmClothPurchasePlanPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    cxgrdpmn1: TcxGridPopupMenu;
    pm_sub: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    cxPageControl3: TcxPageControl;
    cxTabSheet3: TcxTabSheet;
    ScrollBox1: TScrollBox;
    lbl26: TLabel;
    lbl27: TLabel;
    lbl28: TLabel;
    lbl30: TLabel;
    lbl31: TLabel;
    lbl32: TLabel;
    SellRepresentative: TcxTextEdit;
    SellTel: TcxTextEdit;
    SellAddress: TcxTextEdit;
    BuyRepresentative: TcxTextEdit;
    BuyTel: TcxTextEdit;
    BuyAddress: TcxTextEdit;
    cxTabSheet1: TcxTabSheet;
    pnl1: TPanel;
    lbl5: TLabel;
    lbl6: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    PurNo: TcxTextEdit;
    Sellname: TcxButtonEdit;
    BuyName: TcxButtonEdit;
    PurDate: TcxDateEdit;
    lbl9: TLabel;
    lbl10: TLabel;
    lbl1: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    ShippMent: TcxTextEdit;
    Payment: TcxButtonEdit;
    ShipMent: TcxButtonEdit;
    IsTax: TcxComboBox;
    DlyDate: TcxDateEdit;
    lbl11: TLabel;
    Note: TcxMemo;
    Label1: TLabel;
    LightColorRatio: TcxMemo;
    lbl23: TLabel;
    PurReason: TcxComboBox;
    ToolButton8: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    TV1Column22: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    TV1Column25: TcxGridDBColumn;
    TV1Column23: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    TV1Column24: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    TV1Column21: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV1Column18: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    TV1Column20: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton1: TToolButton;
    ToolButton7: TToolButton;
    ToolButton2: TToolButton;
    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 SellNameBtnUpClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ShippMentKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure BuyNameDblClick(Sender: TObject);
    procedure BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SellnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ShipMentPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TV1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton8Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;
    procedure JSMoney();
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function TestData(): Boolean;
    function SaveData(): Boolean;
    procedure SetXH();
    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    fFlileFlag: string;
    FPurMId: string;
    FAuthority, FGSTT: string;
    { Public declarations }
  end;

var
  frmClothPurchasePlanPut: TfrmClothPurchasePlanPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel,
  U_TradeSalesContractFCSel, U_TradeSalesContractGCSel,
  U_TradeSalesContractBCSel;

{$R *.dfm}

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

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

procedure TfrmClothPurchasePlanPut.ShipMentPropertiesButtonClick(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 := 'Pur';
      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 TfrmClothPurchasePlanPut.ShippMentKeyPress(Sender: TObject; var Key: Char);
begin

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

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

function TfrmClothPurchasePlanPut.TestData(): Boolean;
begin
  try
//    if Trim(PurNo.Text) = '' then
//      raise Exception.Create('采购单号号不能为空！');
    if Trim(SellName.Text) = '' then
      raise Exception.Create('供应商不能为空！');
    if Trim(BuyName.Text) = '' then
      raise Exception.Create('我方单位不能为空！');
    if Trim(IsTax.Text) = '' then
      raise Exception.Create('是否含税不能为空！');

    if CDS_1.IsEmpty then
      raise Exception.Create('明细不能为空！');
//    if CDS_1.Locate('C_Name', null, []) then
//      raise Exception.Create('品名不能为空！');
    if CDS_1.Locate('C_Name', '', []) then
      raise Exception.Create('品名不能为空！');
    if CDS_1.Locate('Price', null, []) then
      raise Exception.Create('单价不能为空！');
    if CDS_1.Locate('Qty', null, []) then
      raise Exception.Create('数量不能为空！');
    if CDS_1.Locate('Qty', 0, []) then
      raise Exception.Create('数量不能为0！');
    if CDS_1.Locate('QtyUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if CDS_1.Locate('QtyUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmClothPurchasePlanPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmClothPurchasePlanPut.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Pur_ClothPlan_Sub ');
    sql.Add('where PurMId=''' + Trim(FPurMId) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);
   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Pur_ClothPlan_Main where PurMId=''' + Trim(FPurMId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  SCSHData(ADOTemp, pnl1, 2);
  BuyName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('BuyNo').AsString);
  Sellname.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('SellNo').AsString);
  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

  if PState = '新增' then
  begin
    PurDate.Date := SGetServerDateTime(ADOTemp);
    DlyDate.Date := PurDate.Date;
    PurNo.Text := '';
  end;

  if PState = '复制' then
  begin
    PState := '新增';
    FPurMId := '';
    PurNo.Text := '';
    PurDate.Date := SGetServerDateTime(ADOTemp);
    DlyDate.Date := PurDate.Date;
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOCmd, maxno, 'SCS', 'Pur_ClothPlan_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('PurMId').Value := '';
        FieldByName('PurSId').Value := maxno;
        Post;
        Next;
      end;
    end;
  end;

end;

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

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

procedure TfrmClothPurchasePlanPut.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 := CDS_1.FieldByName('XHNo').ASInteger;
  MC_Code := CDS_1.fieldbyname('C_Code').AsString;
  MC_Craft := CDS_1.fieldbyname('C_Craft').AsString;
  MC_Name := CDS_1.fieldbyname('C_Name').AsString;
  MC_Spec := CDS_1.fieldbyname('C_Spec').AsString;
  MC_CF := CDS_1.fieldbyname('C_CF').AsString;
  MC_Color := CDS_1.fieldbyname('C_Color').AsString;
  MC_MF := CDS_1.fieldbyname('C_MF').AsString;
  MC_KZ := CDS_1.fieldbyname('C_KZ').AsString;
  MC_Note := CDS_1.fieldbyname('C_Note').AsString;
  MC_Unit := CDS_1.fieldbyname('C_Unit').AsString;
  MSuoLv := CDS_1.fieldbyname('SuoLv').AsString;
  MGcHuaNo := CDS_1.fieldbyname('GcHuaNo').AsString;
  MC_EName := CDS_1.fieldbyname('C_EName').AsString;

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

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

procedure TfrmClothPurchasePlanPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

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

procedure TfrmClothPurchasePlanPut.BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      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.BuyRepresentative.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CompanyContact').AsString);
//        self.BuyTel.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CompanyTelephone').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);
        self.BuyAddress.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmClothPurchasePlanPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmClothPurchasePlanPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmClothPurchasePlanPut := nil;
end;

procedure TfrmClothPurchasePlanPut.FormResize(Sender: TObject);
begin
  inherited;
  if self.WindowState = wsMinimized then
    self.Hide;
end;

procedure TfrmClothPurchasePlanPut.FormShow(Sender: TObject);
begin
  inherited;
  readCxGrid(trim(self.Caption), Tv1, '销售合同管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

end;

function TfrmClothPurchasePlanPut.SaveData(): Boolean;
var
  maxno, maxsno: string;
  MPurNo, fDate: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FPurMId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'PM', 'Pur_ClothPlan_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');

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

      if trim(PurNo.Text) = '' then
      begin
        if GetLSNo(ADOTemp, MPurNo, fDate, 'Pur_ClothPlan_Main', 4, 0) = False then
          raise Exception.Create('取最新采购单号失败！');
        PurNo.Text := MPurNo;
      end;
    end
    else
    begin
      maxno := Trim(FPurMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Pur_ClothPlan_Main where PurMId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FPurMId) = '' 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('PurMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'Pur_ClothPlan_Main', ScrollBox1, 2);
      RTSetsavedata(ADOCmd, 'Pur_ClothPlan_Main', pnl1, 2);
      FieldByName('BuyNo').Value := Trim(BuyName.Properties.LookupItems.Text);
      FieldByName('SellNo').Value := Trim(Sellname.Properties.LookupItems.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Pur_ClothPlan_Main where PurNo=''' + Trim(PurNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('采购单号重复！');


////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin

        maxsno := Trim(CDS_1.fieldbyname('PurSId').AsString);

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Pur_ClothPlan_Sub    ');
          sql.Add('where   PurSId=''' + Trim(maxsno) + '''');
          SQL.Add('and PurMId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Pur_ClothPlan_Sub', 0);
          FieldByName('PurMId').Value := Trim(maxno);
          FieldByName('PurSId').Value := Trim(maxsno);

          Post;
        end;

        Next;
      end;
    end;
////////////////       保存子表          //////////////////////

    ADOCmd.Connection.CommitTrans;
    FPurMId := Trim(maxno);
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from Pur_ClothPlan_Sub ');
      sql.Add('where PurMId=''' + Trim(FPurMId) + '''');
      sql.Add(' order by SerialNo ');
      Open;
    end;
    SCreateCDS(ADOTemp, CDS_1);
    SInitCDSData(ADOTemp, CDS_1);

    Result := True;
  except

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

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

  if TestData() = false then
    exit;
  if Trim(PurNo.Text) = '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('exec P_Trade_Plan_Get_OrdNo @ConNo=''' + Trim(CDS_1.FieldByName('ConNo').AsString) + '''');
      SQL.Add(',@OrdType=''大货单'' ');
      Open;
    end;
    PurNo.Text := ADOTemp.FieldByName('OrderNo').AsString;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmClothPurchasePlanPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_ClothPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := CDS_1.RecordCount + 1;

  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.CDS_1 do
              begin
                Append;
                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('PurSId').Value := maxno + INTTOSTR(i);
                FieldByName('C_Code').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Code').value;
                FieldByName('C_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Name').value;
                FieldByName('C_Spec').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').value;
                FieldByName('C_Composition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').value;
                FieldByName('C_Color').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Color').value;
                FieldByName('FtyNo').Value := frmClothInfoSel.CDS_1.fieldbyname('FtyNo').value;

                FieldByName('Qty').Value := 0;
                FieldByName('QtyUnit').Value := 'Kg';
                FieldByName('GC_ColorType').Value := '白胚';
              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
  SetXH();
end;

procedure TfrmClothPurchasePlanPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  while CDS_1.Locate('SSel', True, []) do
  begin
//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from  Trade_Plan_Sub where PurSId=''' + Trim(CDS_1.fieldbyname('PurSId').AsString) + '''');
//      open;
//    end;
//    if ADOTemp.IsEmpty = false then
//    begin
//      exit;
//      application.MessageBox('该明细已经在生产！', '提示信息');
//    end;
    if Trim(CDS_1.fieldbyname('PurSId').AsString) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update Pur_ClothPlan_Sub set DelId=' + QuotedStr(Trim(DCode)) + ',Deler=' + QuotedStr(Trim(dname)) + ',Deltime=GETDATE()    ');
        sql.Add('where PurSId=' + QuotedStr(Trim(CDS_1.fieldbyname('PurSId').AsString)));

        sql.Add('insert into Pur_ClothPlan_Sub_Del select * from Pur_ClothPlan_Sub where PurSId=' + QuotedStr(Trim(CDS_1.fieldbyname('PurSId').AsString)));

        sql.Add('delete Pur_ClothPlan_Sub where PurSId=' + QuotedStr(Trim(CDS_1.fieldbyname('PurSId').AsString)));
        ExecSQL;
      end;
    end;
    CDS_1.Delete;
  end;
  SetXH();
end;

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

procedure TfrmClothPurchasePlanPut.ToolButton4Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
  i: Integer;
  maxno: string;
begin
  if not CDS_1.IsEmpty then
  begin
    if CDS_1.Fieldbyname('PurType').AsString = '' then
      raise Exception.Create('采购类型不能为空！');
  end;

  i := CDS_1.RecordCount + 1;
  try
    frmTradeSalesContractFCSel := TfrmTradeSalesContractFCSel.Create(Application);
    with frmTradeSalesContractFCSel do
    begin
//      fconNo := self.ConNo.Text;
//      fC_Code := self.C_Code.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('GC_Name;ConSId', VarArrayOf([trim(CDS_1.Fieldbyname('PurchaseType').AsString), trim(CDS_1.Fieldbyname('ConSId').AsString)]), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;
          if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_ClothPlan_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
//          self.BuyName.Properties.LookupItems.Text := Trim(frmTradeSalesContractFCSel.CDS_1.fieldbyname('Sellno').AsString);
//          self.BuyName.Text := Trim(frmTradeSalesContractFCSel.CDS_1.fieldbyname('Sellname').AsString);
          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSEL').Value := False;
            FieldByName('SerialNo').Value := i;
            FieldByName('PurSId').Value := maxno + INTTOSTR(i);
            self.BuyName.Properties.LookupItems.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('Sellno').AsString;
            self.BuyName.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('Sellname').AsString;
            self.BuyRepresentative.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('SellRepresentative').AsString;
            self.BuyTel.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('SellTel').AsString;
            self.BuyAddress.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('SellAddress').AsString;
            self.LightColorRatio.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('LCRatio').AsString;
            Fieldbyname('C_Degree').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('PurchaseType').Value;
            Fieldbyname('SerialNo').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('ConNo').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConNo').Value;
            Fieldbyname('C_Name').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Name').Value;
            Fieldbyname('C_Spec').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Spec').Value;
            Fieldbyname('C_Composition').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Composition').Value;
            Fieldbyname('C_Width').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Width').Value;
            Fieldbyname('C_GramWeight').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_GramWeight').Value;
            Fieldbyname('GC_Name').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('GC_Name').Value;
            Fieldbyname('CraftCode').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('CraftCode').Value;
            Fieldbyname('CraftName').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('CraftName').Value;
            Fieldbyname('C_Code').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Code').Value;
            Fieldbyname('ConNo').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConSId').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_Pattern').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_ColorConfirm').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('C_ColorConfirm').AsString);
            Fieldbyname('OrgQty').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConQty').AsFloat;
            Fieldbyname('QtyUnit').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('ConUnit').Value;

            Fieldbyname('BuyName').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('BuyName').AsString);
            Fieldbyname('TallyId').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('TallyId').AsString);
            Fieldbyname('Tallyer').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('Tallyer').AsString);
            Fieldbyname('ConSalesId').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('SalesId').AsString);
            Fieldbyname('ConSaleser').value := trim(frmTradeSalesContractFCSel.CDS_1.Fieldbyname('Saleser').AsString);

            Fieldbyname('MoreQty').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('MoreQty').Value;
            Fieldbyname('LessQty').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('LessQty').Value;
            Fieldbyname('MoreLessUnit').value := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('MoreLessUnit').Value;
            FieldByName('FtyNo').Value := frmTradeSalesContractFCSel.CDS_1.fieldbyname('FtyNo').value;
            FieldByName('GC_ColorType').Value := '白胚';
            Post;
          end;
          i := i + 1;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

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

procedure TfrmClothPurchasePlanPut.ToolButton5Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
  i: Integer;
  maxno: string;
begin
  if not CDS_1.IsEmpty then
  begin
    if CDS_1.Fieldbyname('PurType').AsString = '' then
      raise Exception.Create('采购类型不能为空！');
  end;

  i := CDS_1.RecordCount + 1;
  try
    frmTradeSalesContractGCSel := TfrmTradeSalesContractGCSel.Create(Application);
    with frmTradeSalesContractGCSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('GC_Name;ConSId', VarArrayOf([trim(CDS_1.Fieldbyname('PurchaseType').AsString), trim(CDS_1.Fieldbyname('ConSId').AsString)]), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;
          if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_ClothPlan_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
//          self.BuyName.Properties.LookupItems.Text := Trim(frmTradeSalesContractGCSel.CDS_1.fieldbyname('Sellno').AsString);
//          self.BuyName.Text := Trim(frmTradeSalesContractGCSel.CDS_1.fieldbyname('Sellname').AsString);
          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSEL').Value := False;
            FieldByName('SerialNo').Value := i;
            FieldByName('PurSId').Value := maxno + INTTOSTR(i);

            self.BuyName.Properties.LookupItems.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('Sellno').AsString;
            self.BuyName.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('Sellname').AsString;
            self.BuyRepresentative.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('SellRepresentative').AsString;
            self.BuyTel.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('SellTel').AsString;
            self.BuyAddress.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('SellAddress').AsString;
            self.LightColorRatio.Text := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('LCRatio').AsString;

            Fieldbyname('C_Degree').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('PurchaseType').Value;
            Fieldbyname('SerialNo').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('ConNo').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConNo').Value;
            Fieldbyname('C_Name').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Name').Value;
            Fieldbyname('C_Spec').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Spec').Value;
            Fieldbyname('C_Composition').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Composition').Value;
//            Fieldbyname('C_Width').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Width').Value;
//            Fieldbyname('C_GramWeight').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_GramWeight').Value;
            Fieldbyname('GC_Name').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('GC_Name').Value;
            Fieldbyname('CraftCode').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('CraftCode').Value;
            Fieldbyname('CraftName').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('CraftName').Value;
            Fieldbyname('C_Code').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Code').Value;
            Fieldbyname('ConNo').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConSId').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_Pattern').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_ColorConfirm').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('C_ColorConfirm').AsString);
            Fieldbyname('OrgQty').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConQty').AsFloat;
            Fieldbyname('QtyUnit').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('ConUnit').Value;

            Fieldbyname('BuyName').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('BuyName').AsString);
            Fieldbyname('TallyId').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('TallyId').AsString);
            Fieldbyname('Tallyer').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('Tallyer').AsString);
            Fieldbyname('ConSalesId').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('SalesId').AsString);
            Fieldbyname('ConSaleser').value := trim(frmTradeSalesContractGCSel.CDS_1.Fieldbyname('Saleser').AsString);

            Fieldbyname('MoreQty').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('MoreQty').Value;
            Fieldbyname('LessQty').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('LessQty').Value;
            Fieldbyname('MoreLessUnit').value := frmTradeSalesContractGCSel.CDS_1.Fieldbyname('MoreLessUnit').Value;
            FieldByName('FtyNo').Value := frmTradeSalesContractGCSel.CDS_1.fieldbyname('FtyNo').value;
            FieldByName('GC_ColorType').Value := '白胚';

            Post;
          end;
          i := i + 1;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

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

procedure TfrmClothPurchasePlanPut.ToolButton6Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
  i: Integer;
  maxno: string;
begin
  if not CDS_1.IsEmpty then
  begin
    if CDS_1.Fieldbyname('PurType').AsString = '' then
      raise Exception.Create('采购类型不能为空！');
  end;

  i := CDS_1.RecordCount + 1;
  try
    frmTradeSalesContractBCSel := TfrmTradeSalesContractBCSel.Create(Application);
    with frmTradeSalesContractBCSel do
    begin
//      fconNo := self.ConNo.Text;
//      fC_Code := self.C_Code.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          if self.CDS_1.locate('GC_Name;ConSId', VarArrayOf([trim(CDS_1.Fieldbyname('PurchaseType').AsString), trim(CDS_1.Fieldbyname('ConSId').AsString)]), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
            continue;
          end;
          if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_ClothPlan_Sub', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
//          self.BuyName.Properties.LookupItems.Text := Trim(frmTradeSalesContractBCSel.CDS_1.fieldbyname('Sellno').AsString);
//          self.BuyName.Text := Trim(frmTradeSalesContractBCSel.CDS_1.fieldbyname('Sellname').AsString);
          with self.CDS_1 do
          begin
            Append;
            FieldByName('SSEL').Value := False;
            FieldByName('SerialNo').Value := i;
            FieldByName('PurSId').Value := maxno + INTTOSTR(i);
            self.BuyName.Properties.LookupItems.Text := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('Sellno').AsString;
            self.BuyName.Text := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('Sellname').AsString;
            self.BuyRepresentative.Text := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('SellRepresentative').AsString;
            self.BuyTel.Text := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('SellTel').AsString;
            self.BuyAddress.Text := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('SellAddress').AsString;
            self.LightColorRatio.Text := frmTradeSalesContractFCSel.CDS_1.Fieldbyname('LCRatio').AsString;
            Fieldbyname('C_Degree').value := '底布';
            Fieldbyname('SerialNo').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('SerialNo').AsString);
            Fieldbyname('ConNo').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConNo').Value;
            Fieldbyname('C_Name').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Name').Value;
            Fieldbyname('C_Spec').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Spec').Value;
            Fieldbyname('C_Composition').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Composition').Value;
            Fieldbyname('C_Width').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Width').Value;
            Fieldbyname('C_GramWeight').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_GramWeight').Value;
            Fieldbyname('GC_Name').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('GC_Name').Value;
            Fieldbyname('CraftCode').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('CraftCode').Value;
            Fieldbyname('CraftName').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('CraftName').Value;
            Fieldbyname('C_Code').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Code').Value;
            Fieldbyname('ConNo').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConNo').AsString);
            Fieldbyname('ConMId').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConMId').AsString);
            Fieldbyname('ConSId').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConSId').AsString);
            Fieldbyname('C_ColorNo').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
            Fieldbyname('C_Color').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Color').AsString);
            Fieldbyname('C_Pattern').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_Pattern').AsString);
            Fieldbyname('C_ColorConfirm').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('C_ColorConfirm').AsString);
            Fieldbyname('OrgQty').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConQty').AsFloat;
            Fieldbyname('QtyUnit').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('ConUnit').Value;

            Fieldbyname('BuyName').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('BuyName').AsString);
            Fieldbyname('TallyId').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('TallyId').AsString);
            Fieldbyname('Tallyer').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('Tallyer').AsString);
            Fieldbyname('ConSalesId').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('SalesId').AsString);
            Fieldbyname('ConSaleser').value := trim(frmTradeSalesContractBCSel.CDS_1.Fieldbyname('Saleser').AsString);

            Fieldbyname('MoreQty').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('MoreQty').Value;
            Fieldbyname('LessQty').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('LessQty').Value;
            Fieldbyname('MoreLessUnit').value := frmTradeSalesContractBCSel.CDS_1.Fieldbyname('MoreLessUnit').Value;
            FieldByName('FtyNo').Value := frmTradeSalesContractBCSel.CDS_1.fieldbyname('FtyNo').value;
            FieldByName('GC_ColorType').Value := '白胚';
            Post;
          end;
          i := i + 1;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;

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

procedure TfrmClothPurchasePlanPut.ToolButton7Click(Sender: TObject);
var
  i: Integer;
  maxno, FPurType: string;
begin
  if not CDS_1.IsEmpty then
  begin
    if CDS_1.Fieldbyname('PurType').AsString = '' then
      raise Exception.Create('采购类型不能为空！');
  end;

  FPurType := CDS_1.Fieldbyname('PurType').AsString;

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_ClothPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := CDS_1.RecordCount + 1;

  if CDS_1.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_1);
    with CDS_1 do
    begin
      Edit;
      FieldByName('SSEL').Value := False;
      FieldByName('SerialNo').Value := i;
      FieldByName('PurSId').Value := maxno + INTTOSTR(i);
      FieldByName('PurType').Value := '';
      if FPurType = '计划采购' then
      begin
        FieldByName('ConNo').Value := '';
      end;
      FieldByName('GC_ColorType').Value := '白胚';
      post;
    end;
  end
  else
  begin
    with CDS_1 do
    begin
      Append;
      FieldByName('SSEL').Value := False;
      FieldByName('SerialNo').Value := i;
      FieldByName('PurSId').Value := maxno + INTTOSTR(i);
      FieldByName('Qty').Value := 0;
      FieldByName('QtyUnit').Value := 'Kg';
      FieldByName('GC_ColorType').Value := '白胚';
      Post;
    end;
  end;
  SetXH();
end;

procedure TfrmClothPurchasePlanPut.ToolButton8Click(Sender: TObject);
begin

  Note.Text := '二、质量要求及标准：以上面料各项指标要求,' + #13;
  Note.Text := Note.Text + '三、交货地点及方式：甲方指定的地点交货；' + #13;
  Note.Text := Note.Text + '四、运输及风险承担：乙方应负责把全部面料运送到甲方指定的地点，并承担运输费用及运输过程中的风险；' + #13;
  Note.Text := Note.Text + '五、结算方式及期限：' + #13;
  Note.Text := Note.Text + '六、以上单价含13%增值税发票；' + #13;
  Note.Text := Note.Text + '七、违约责任：' + #13;
  Note.Text := Note.Text + '   1、乙方应该按照合同约定的时间按时提供合格品质的货品,否则因交期延误，品质问题造成的损失由乙方全额承担；' + #13;
  Note.Text := Note.Text + '   2、甲方应该按照约定的方式进行付款,否则乙方有权利停止发货及追赔未发货造成的损失；' + #13;
  Note.Text := Note.Text + '八、质量索赔：' + #13;
  Note.Text := Note.Text + '   甲方收到货物后，经检测，如有不符合本合同质量标准的，甲方有权退货，并可要求乙方10天内补交质量合格的货物；如果乙方10天内不能补足货物，甲方有权拒收货物，乙方还应按本合同总价款10%向甲方支付违约金。若该违约金不足以弥补甲方因此所受损失的，甲方仍可向乙方追偿；' + #13;
  Note.Text := Note.Text + '九、解决合同纠纷方式：本合同未尽事宜由双方协商解决，协商后达成的书面协议作为本合同的组成部分，与本合同具有同等的法律效力。本合同第二条所提到的质量要求附件与本同同具有同等的法律效力。本合同履行过程中产生的纠纷由合同签订地人民法院管辖；' + #13;
  Note.Text := Note.Text + '十、本合同一式二份，甲乙双方各执一份，自双方加盖公司公章或合同专用章后生效；' + #13;
  Note.Text := Note.Text + '十一、合同签订地:福建省厦门市集美区。' + #13;

end;

procedure TfrmClothPurchasePlanPut.TV1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FOrgQty, FShrinkageRate, FQty, FPrice, fCaddyAmount, FAmount: Double;        //个数，单价，金额
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FOrgQty := Fieldbyname('OrgQty').AsFloat;
    fCaddyAmount := Fieldbyname('CaddyAmount').AsFloat;
    FShrinkageRate := Fieldbyname('ShrinkageRate').AsFloat;
    FPrice := Fieldbyname('Price').AsFloat;
  end;
  FQty := RoundFloat(FOrgQty / ((100 - FShrinkageRate) / 100), 0);
  FAmount := RoundFloat(FQty * FPrice, 2) + fCaddyAmount;
  with CDS_1 do
  begin
    Edit;
    FieldByName('Qty').Value := FQty;
    FieldByName('Amount').Value := FAmount;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmClothPurchasePlanPut.BuyNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';

      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);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;

end;

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

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

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

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

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

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

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

end;

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

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

procedure TfrmClothPurchasePlanPut.SellNameBtnUpClick(Sender: TObject);
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);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmClothPurchasePlanPut.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.SellTel.Text := Trim(frmCompanySel.CDS_1.fieldbyname('Telephone').AsString);
        self.SellAddress.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAddress').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

end.

