unit U_frmCgBjPut;

interface

uses
  Windows, Messages, SysUtils, strutils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, ADODB, cxGridCustomPopupMenu,
  cxGridPopupMenu, DBClient, BtnEdit, cxBlobEdit, cxShellComboBox,
  cxDropDownEdit, cxFontNameComboBox, cxButtonEdit, cxCurrencyEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxScrollBox, cxTextEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmCgBjPut = class(TForm)
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TClose: TToolButton;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar2: TToolBar;
    TAdd: TToolButton;
    TDel: TToolButton;
    ADOQueryTmp: TADOQuery;
    ADOcmd: TADOQuery;
    DataSource1: TDataSource;
    Cds_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    V1Column6: TcxGridDBColumn;
    Label6: TLabel;
    Label7: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    ScrollBox1: TcxScrollBox;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    OrderDate: TDateTimePicker;
    OrderNum: TEdit;
    CPSH: TEdit;
    Label31: TLabel;
    NJZB: TEdit;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    Label39: TLabel;
    JGPrice: TEdit;
    PBNum: TEdit;
    NJPrice: TEdit;
    PBPrice: TEdit;
    NJSH: TEdit;
    Label43: TLabel;
    Note1: TEdit;
    JFdate: TDateTimePicker;
    YWZ: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    ToolBar3: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TV2: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    DataSource2: TDataSource;
    CDS_ZB: TClientDataSet;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQueryTemp: TADOQuery;
    TV2Column5: TcxGridDBColumn;
    CDS_Main: TClientDataSet;
    PBName: TBtnEditC;
    GYSName: TBtnEditA;
    Label4: TLabel;
    Note2: TEdit;
    Label5: TLabel;
    orderno: TBtnEditC;
    TV1Column7: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure TCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TVC_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
//    procedure NoteDblClick(Sender: TObject);
//    procedure SQNameBtnClick(Sender: TObject);
    procedure TVC_Unit2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TVC_UnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure V1JGafterPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure NJZBChange(Sender: TObject);
    procedure V1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TV2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PBNameBtnDnClick(Sender: TObject);
    procedure PBNameBtnUpClick(Sender: TObject);
    procedure GYSNameBtnClick(Sender: TObject);
    procedure ordernoBtnUpClick(Sender: TObject);
    procedure TV1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV1Column6PropertiesChange(Sender: TObject);
    procedure TV1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
//    procedure ConNoBtnClick(Sender: TObject);
//    procedure defstr1BtnClick(Sender: TObject);
//    procedure defstr5BtnClick(Sender: TObject);
//    procedure defstr6BtnClick(Sender: TObject);
//    procedure defstr7BtnClick(Sender: TObject);
//    procedure factoryNameBtnClick(Sender: TObject);
//    procedure FYNOBtnClick(Sender: TObject);
  private
    function savedata(): Boolean;
    procedure initdata();
    { Private declarations }
  public
    formid, PState, CopyInt: integer;
    FMainid, Fzbid: string;
    A: Double;
    { Public declarations }
  end;

var
  frmCgBjPut: TfrmCgBjPut;

implementation

uses
  U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_ContractList_CX, U_DataLink, U_ZdyAttachGYS,
  U_ProductOrderListSel, U_FanYangList_sel;

{$R *.dfm}

procedure TfrmCgBjPut.initdata();
begin
//  with ADOQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from contract_Main_Sh ');
//    sql.Add('where Mainid='''+trim(Fmainid)+'''');
//    open;
//    if not isempty then
//    begin
//      SCSHDataNew(adoqueryTmp,Panel1,0);
//    end;
//  end;
//  with ADOQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.add('select * from contract_Sub_Sh ');
//    sql.Add('where Mainid='''+trim(Fmainid)+'''');
//    open;
//    SCreateCDS20(ADOQueryTmp,Cds_Sub);
//    SInitCDSData20(ADOQueryTmp,Cds_Sub);
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from CGBJ_Sub where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
//    ShowMessage(SQL.text);
    Open;
  end;
  SCreateCDS20(ADOQueryTmp, Cds_Sub);
  SInitCDSData20(ADOQueryTmp, Cds_Sub);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from CGBJ_ZB where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_ZB);
  SInitCDSData20(ADOQueryTemp, CDS_ZB);
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CGBJ_Main where MainId=''' + Trim(FMainId) + '''');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTmp, ScrollBox1, 2);
  if PState = 0 then
  begin
    YWZ.Text := trim(dName);
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    FMainId := '';
    PState := 0;
    with Cds_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
      with CDS_ZB do
      begin
        First;
        while not Eof do
        begin
          Edit;
          FieldByName('MainId').Value := '';
          FieldByName('ZBId').Value := '';
          Post;
          Next;
        end;
      end;
    end;
  end;

end;

function TfrmCgBjPut.savedata(): Boolean;
var
  maxNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxNo, 'CM', 'CGBJ_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxNo := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CGBJ_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxNo);
      SSetsaveSqlNew(ADOCmd, 'CGBJ_Main', ScrollBox1, 2);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTmp);
        FieldByName('SQDate').Value := SGetServerDateTime(ADOQueryTmp);
        FieldByName('SQName').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
      end;
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CGBJ_Main where PBName=''' + Trim(PBName.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('坯布名称重复!', '提示', 0);
      Exit;
    end;
    FMainId := Trim(maxNo);

//    if PState = 0 then
//    begin
//      ffacction := '指示单新增';
//      OpEvent:=trim('指示单号：' + Trim(OrderNo.Text));
//    end
//    else
//    begin
//      ffacction := '指示单修改';
//      OpEvent:=OpEvent+trim('修改后指示单号：' + Trim(OrderNo.Text));
//    end;
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
//      sql.Add(' ' + quotedstr(trim(DName)));
//      sql.Add(',getdate() ');
//      sql.Add(',' + quotedstr(trim(self.Caption)));
//      sql.Add(',' + quotedstr(trim(ffacction)));
//      sql.Add(',' + quotedstr(OpEvent));
//      sql.Add(',' + quotedstr(trim('成功')));
//      sql.Add(')');
//      ExecSQL;
//    end;
      ///保存中间表
    with CDS_ZB do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_ZB.fieldbyname('ZBId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxNo, 'Czb', 'CGBJ_zb', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取占比子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxNo := Trim(Cds_zb.fieldbyname('zbId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CGBJ_zb  where MainId=''' + Trim(FMainId) + '''');
          sql.Add('and  zbId=''' + Trim(maxNo) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_ZB.fieldbyname('zbId').AsString) = '' then
          begin
            Append;
          end
          else
            Edit;
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('zbId').Value := Trim(maxNo);
          RTSetSaveDataCDS(ADOCmd, Tv2, CDS_ZB, 'CGBJ_ZB', 0);
          Post;
        end;
        CDS_ZB.Edit;
        CDS_ZB.FieldByName('zbId').Value := Trim(maxNo);
        CDS_ZB.FieldByName('Mainid').Value := Trim(FMainId);
        Next;
      end;
    end;
    FzbId := Trim(maxNo);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
    ///保存子表
  with Cds_Sub do
  begin
    with ADOQueryTmp do
    begin
      close;
      sql.clear;
      sql.Add('delete from CGBJ_Sub where ');
      if PState = 1 then
      begin
        sql.Add('  MainId=''' + Trim(FMainId) + '''');
      end;
      if PState = 0 then
      begin
        sql.Add('  1<>1');
      end;
//      ShowMessage(sql.text);
      execsql;
    end;

    First;
    while not Eof do
    begin
      if Trim(Cds_Sub.fieldbyname('SubId').AsString) = '' then
      begin
        if GetLSNo(ADOCmd, maxNo, 'CS', 'CGBJ_Sub', 4, 1) = False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
      end
      else
      begin
        maxNo := Trim(Cds_Sub.fieldbyname('SubId').AsString);
      end;
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from CGBJ_Sub where MainId=''' + Trim(FMainId) + '''');
//          sql.Add(' and ZBid=''' + Trim(Fzbid) + '''');
        sql.Add(' and SubId=''' + Trim(maxNo) + '''');
        Open;
      end;
      with ADOCmd do
      begin
        if Trim(Cds_Sub.fieldbyname('SubId').AsString) = '' then
        begin
          Append;
        end
        else
          Edit;
        FieldByName('MainId').Value := Trim(FMainId);
//          FieldByName('zbid').Value := Trim(Fzbid);
        FieldByName('SubId').Value := Trim(maxNo);
        RTSetSaveDataCDS(ADOCmd, Tv1, Cds_Sub, 'CGBJ_Sub', 0);
        if Trim(fieldbyname('PRTOrderQty').AsString) = '' then
        begin
          fieldbyname('PRTOrderQty').Value := 0
        end;
        if Trim(fieldbyname('PRTRoll').AsString) = '' then
        begin
          fieldbyname('PRTRoll').Value := 0
        end;
//          fieldbyname('SordQty1').Value := Order_Sub.fieldbyname('SordQty1').AsFloat;
//          fieldbyname('SordQty2').Value := Order_Sub.fieldbyname('SordQty2').AsFloat;
//          fieldbyname('SordQty3').Value := Order_Sub.fieldbyname('SordQty3').AsFloat;
//          FieldByName('Sorddefstr10').Value := Order_Sub.FieldByName('Sorddefstr10').AsString;
//          FieldByName('khOrderNO').Value := trim(Order_Sub.FieldByName('khOrderNO').AsString);
        Post;
      end;
      Cds_Sub.Edit;
      Cds_Sub.FieldByName('SubId').Value := Trim(maxNo);
      Cds_Sub.FieldByName('Mainid').Value := Trim(FMainId);
//         Cds_Sub.FieldByName('zbid').Value := Trim(Fzbid);
//        CJEWM();
      Next;
    end;
  end;

end;

procedure TfrmCgBjPut.TCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmCgBjPut.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + tv1.Name + '41', Tv1);
  ReadCxGrid(self.Caption + tv2.Name + '41', Tv2);
//  if formid=2 then
//  begin
//    TSave.Enabled:=False;
//    ToolBar2.Visible:=false;
//  end;
//  SQDate.datetime:=SGetServerDateTime(ADOQueryTmp);
//  SQName.Text:=trim(DName);
  OrderDate.datetime := SGetServerDateTime(ADOQueryTmp);
  JFDate.datetime := SGetServerDateTime(ADOQueryTmp);
  initdata();

end;

procedure TfrmCgBjPut.FormCreate(Sender: TObject);
begin
  cxGrid1.Align := alclient;
end;

procedure TfrmCgBjPut.TSaveClick(Sender: TObject);
begin
  if trim(PBName.Text) = '' then
  begin
    application.MessageBox('坯布品名不能为空', '提示', 0);
    exit;
  end;
  if Cds_Sub.IsEmpty then
  begin
    application.MessageBox('明细不能为空', '提示', 0);
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmCgBjPut.TAddClick(Sender: TObject);
begin
  with Cds_Sub do
  begin
    append;
    post;
  end;
end;

procedure TfrmCgBjPut.TDelClick(Sender: TObject);
begin
  if Cds_Sub.IsEmpty then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from contract_Sub ');
    sql.add('where Sdefstr7=''' + trim(Cds_Sub.fieldbyname('subid').asstring) + '''');
    open;
    if not isempty then
    begin
      application.MessageBox('已采购不能撤销', '提示');
      exit;
    end;
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('delete from contract_sub_Sh where subID=''' + trim(cds_sub.fieldbyname('subID').AsString + ''''));
    execsql;
  end;
  cds_sub.Delete;
end;

procedure TfrmCgBjPut.FormDestroy(Sender: TObject);
begin
  frmCgBjPut := nil;
end;

procedure TfrmCgBjPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + tv1.Name + '41', Tv1);
  WriteCxGrid(self.Caption + tv2.Name + '41', Tv2);
  Action := cafree;
end;

procedure TfrmCgBjPut.TVC_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if showmodal = 1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_CodeName').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmCgBjPut.TVC_Unit2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag := 'c_Unit2';
      flagname := '订单数量单位';
      if showmodal = 1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_unit2').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmCgBjPut.TVC_UnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag:='c_Unit2';
      flagname:='数量单位';
      if showmodal=1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_unit').AsString:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;}
end;

procedure TfrmCgBjPut.V1JGafterPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Factory';
      flagname := '后加工';
      MainType := 'PBFactory';
      if ShowModal = 1 then
      begin
        with Cds_Sub do
        begin
          Edit;
          FieldByName('JGafter').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCgBjPut.NJZBChange(Sender: TObject);
var
  sumPrice: double;
begin

  if CDS_ZB.IsEmpty then
    exit;
  try
    sumPrice := 0;

    with CDS_ZB do
    begin
      DisableControls;
      First;
      while not eof do
      begin

//        sumPrice := RoundFloat(sumPrice + CDS_ZB.fieldbyname('ZbPrice').AsFloat, 2);
        sumPrice := sumPrice + CDS_ZB.fieldbyname('ZbPrice').AsFloat;
        next;
      end;
      EnableControls;
    end;

  except

  end;

//  PBPrice.Text := FloatToStr(TV2.DataController.Summary.FooterSummaryValues[4] + StrToFloatDef(Trim(JGPrice.Text), 0));
  PBPrice.Text := FloatToStr((sumPrice) + StrToFloatDef(Trim(JGPrice.Text), 0));

end;

procedure TfrmCgBjPut.V1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    FFInt := 1;
    if showmodal = 1 then
    begin
//       ConMainId:=trim(Order_Main.fieldbyname('mainID').asstring);
      Cds_Sub.Edit;
      Cds_Sub.fieldbyname('orderno').Value := trim(Order_Main.fieldbyname('OrderNo').asstring);
      Cds_Sub.fieldbyname('PRTCodeName').Value := trim(Order_Main.fieldbyname('MPRTCodeName').asstring);
      Cds_Sub.fieldbyname('PRTColor').Value := trim(Order_Main.fieldbyname('PRTColor').asstring);
      Cds_Sub.fieldbyname('PRTOrderQty').Value := trim(Order_Main.fieldbyname('PRTOrderQty').asstring);
    end;
    free;
  end;
end;

//procedure TfrmCgBjPut.TV1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
//begin
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'CGPRTcolor';
//      flagname := '颜色';
//      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
// //     MainType:=Trim(DName);
//      fnote := True;
//      if ShowModal = 1 then
//      begin
//        Self.Cds_Sub.Edit;
//        Self.Cds_Sub.FieldByName('PRTColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
////        Self.Order_Sub.FieldByName('SOrddefstr4').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
//end;

procedure TfrmCgBjPut.ToolButton1Click(Sender: TObject);
// var
//  i:Integer;
//  a:Integer;
begin
//  i:=CDS_ZB.RecordCount;
//  i:=i+1;
//  a:=order_sub.fieldbyname('SOrddefstr1').Value;
//  if TryStrToInt(a) then
//  begin
//    a:=a+1
//  end;
//  CopyAddRow(Tv2,CDS_ZB);
//   with CDS_ZB do
//  begin
//    Edit;
//    FieldByName('zbid').Value:=IntToStr(i);
//    Post;
//  end;
  with CDS_ZB do
  begin
    append;
    post;
  end;
//  with Cds_Sub do
//  begin
//    append;
//    post;
//  end;
end;

procedure TfrmCgBjPut.ToolButton2Click(Sender: TObject);
begin
//if CDS_ZB.IsEmpty then
//    Exit;
//  with ADOQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from contract_Sub ');
//    sql.add('where Sdefstr7=''' + trim(Cds_Sub.fieldbyname('subid').asstring) + '''');
//    open;
//    if not isempty then
//    begin
//      application.MessageBox('已采购不能撤销', '提示');
//      exit;
//    end;
//  end;
  with ADOQueryTemp do
  begin
    close;
    sql.clear;
    sql.Add('delete from CGBJ_ZB where zbID=''' + trim(CDS_ZB.fieldbyname('zbID').AsString + ''''));
    execsql;
  end;
  CDS_ZB.Delete;
end;

procedure TfrmCgBjPut.TV2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  Fzb, Fsj, FzbPrice, Fsh: Double;
  sumPrice: double;
  i: Integer;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  FzbPrice := 0;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with CDS_ZB do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
//    FSXMC:=CDS_ZB.fieldbyname('SXMC').AsFloat;
    Fsj := CDS_ZB.fieldbyname('sj').AsFloat;
    Fzb := CDS_ZB.fieldbyname('zb').AsFloat / 100;
    Fsh := CDS_ZB.fieldbyname('sh').AsFloat / 100;
    FzbPrice := CDS_ZB.fieldbyname('zbPrice').AsFloat;
  end;

  if Trim(FFieldName) <> 'zbPrice' then
  begin
    FzbPrice := (Fzb * Fsj) * (1 + Fsh);
  end;
//    with CDS_ZB do
//    begin
////      CDSTM.Last;
////       CDSTM.Append;
//           A:=0;
//        for i:=0 to FieldCount-1 do
//        begin
//           CDSTM.fields[i].value:=Fields[i].Value ;
//            A:= A+FzbPrice;
//        end;
//        CDS_ZB.Post;
//    end;
//  end;

  with CDS_ZB do
  begin
    Edit;
    FieldByName('zbPrice').Value := FzbPrice;
    Post;
  end;
  if CDS_ZB.IsEmpty then
    exit;
  try
    sumPrice := 0;

    with CDS_ZB do
    begin
      DisableControls;
      First;
      while not eof do
      begin

//        sumPrice := RoundFloat(sumPrice + CDS_ZB.fieldbyname('ZbPrice').AsFloat, 2);
        sumPrice := sumPrice + CDS_ZB.fieldbyname('ZbPrice').AsFloat;
        next;
      end;
      EnableControls;
    end;
                           
  except
                                    
  end;

  PBPrice.Text := FloatToStr((sumPrice) + StrToFloatDef(Trim(JGPrice.Text), 0));
//  PBPrice.Text := FloatToStr(TV2.DataController.Summary.FooterSummaryValues[1] + StrToFloatDef(Trim(JGPrice.Text), 0));
// with ADOQueryCmd do
//  begin
//    Close;
//    SQL.Clear;
//    sql.Add('update CGBJ_ZB set  ' + FFieldName + '=' + quotedstr(Trim(mvalue)));
//    if Trim(FFieldName) <> 'ZBPrice' then
//      sql.Add(' ,ZBPrice=' + FloatToStr(FZBPrice));
//    sql.Add('where  ZBid=' + quotedstr(Trim(CDS_ZB.fieldbyname('ZBid').AsString)));
//    ExecSQL;
//  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmCgBjPut.TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdSXMC';
      flagname := '纱线名称';
      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
 //     MainType:=Trim(DName);
//      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_ZB.Edit;
        Self.CDS_ZB.FieldByName('SXMC').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.Order_ZB.FieldByName('SOrddefstr4').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

//procedure TfrmCgBjPut.PBNameBtnClick(Sender: TObject);
//begin
//    try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'CustomerNoName';
//      flagname := '客户名称';
//      if Trim(DParameters1) <> '高权限' then
//        MainType := Trim(DName);
//      if ShowModal = 1 then
//      begin
//        CustomerNoName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        CustomerNoName.TxtCode := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
//end;

procedure TfrmCgBjPut.PBNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmCgBjPut.PBNameBtnUpClick(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 Trim(flag) = 'OrdPBName' then
      begin
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag) = 'PBName' then
        begin
          with ADOcmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from CGBJ_Main where PBName=''' + Trim(ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
          PBName.Text := ADOcmd.fieldbyname('PBName').AsString;
//          MPRTMF.Text := ADOTemp.fieldbyname('MPRTMF').AsString;
//          MPRTKZ.Text := ADOTemp.fieldbyname('MPRTKZ').AsString;
//          MPRTSpec.Text := ADOTemp.fieldbyname('MPRTSpec').AsString;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmCgBjPut.GYSNameBtnClick(Sender: TObject);
begin
  frmZdyAttachGYS := TfrmZdyAttachGYS.create(self);
  with frmZdyAttachGYS do
  begin
//    fType := '';
    if showmodal = 1 then
    begin
      GYSName.Text := trim(cds_hz.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmCgBjPut.ordernoBtnUpClick(Sender: TObject);
begin
  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    FFInt := 1;
    if showmodal = 1 then
    begin
      orderno.Text := trim(Order_Main.fieldbyname('OrderNo').asstring);
//       ConMainId:=trim(Order_Main.fieldbyname('mainID').asstring);
//      Cds_Sub.Edit;
//      Cds_Sub.fieldbyname('orderno').Value := trim(Order_Main.fieldbyname('OrderNo').asstring);
//      Cds_Sub.fieldbyname('PRTCodeName').Value := trim(Order_Main.fieldbyname('MPRTCodeName').asstring);
//      Cds_Sub.fieldbyname('PRTColor').Value := trim(Order_Main.fieldbyname('PRTColor').asstring);
//      Cds_Sub.fieldbyname('PRTOrderQty').Value := trim(Order_Main.fieldbyname('PRTOrderQty').asstring);
    end;
    free;
  end;
end;

procedure TfrmCgBjPut.TV1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  Fzb, FSPbsl, FSYJsl, Fsh: Double;
  i: Integer;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  FSYJsl := 0;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with Cds_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FSPbsl := Cds_Sub.fieldbyname('SPbsl').AsFloat;
    Fzb := Cds_Sub.fieldbyname('Szb').AsFloat / 100;
    Fsh := Cds_Sub.fieldbyname('Ssh').AsFloat / 100;
    FSYJsl := Cds_Sub.fieldbyname('Syjsl').AsFloat;
  end;

  if Trim(FFieldName) <> 'Syjsl' then
  begin
    FSYJsl := (Fzb * FSPbsl) * (1 + Fsh);
  end;
  with Cds_Sub do
  begin
    Edit;
    FieldByName('Syjsl').Value := FSYJsl;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCgBjPut.ToolButton3Click(Sender: TObject);
var
  mvalue, FFieldName: string;
  Fzb, FSPbsl, FSYJsl, Fsh: Double;
  i: Integer;
begin
  if CDS_ZB.IsEmpty then
  begin
    Exit;

  end
  else
  begin
    with CDS_ZB do
    begin
      First;
      while not Eof do
      begin
        with Cds_Sub do
        begin
          append;
          Edit;
          FieldByName('Spbsl').Value := StrToFloatDef((PBNum.text), 0);
          FieldByName('SSxmc').Value := CDS_ZB.fieldbyname('SXmc').asstring;
          FieldByName('Szb').Value := CDS_ZB.fieldbyname('zb').asstring;
          FieldByName('Ssh').Value := CDS_ZB.fieldbyname('sh').asstring;
          Post;
        end;
        mvalue := TcxTextEdit(Sender).EditingText;
        FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
        FSYJsl := 0;
        if Trim(mvalue) = '' then
        begin
          mvalue := '0';
        end;
        if mvalue = '复制' then
        begin
          mvalue := CDS_ZB.fieldbyname('SXmc').asstring;
        end;
        with Cds_Sub do
        begin

          Edit;

          FieldByName(FFieldName).Value := mvalue;
          Post;
//          FSPbsl := Cds_Sub.fieldbyname('SPbsl').AsFloat;
//          Fzb := Cds_Sub.fieldbyname('Szb').AsFloat / 100;
//          Fsh := Cds_Sub.fieldbyname('Ssh').AsFloat / 100;
          FSPbsl := Cds_Sub.fieldbyname('SPbsl').AsFloat;
          Fzb := strtofloatdef((Cds_Sub.fieldbyname('Szb').Asstring), 0); //Cds_Sub.fieldbyname('Szb').AsFloat / 100;
          Fsh := strtofloatdef((Cds_Sub.fieldbyname('Ssh').Asstring), 0); // Cds_Sub.fieldbyname('Ssh').AsFloat / 100;
          if Trim(PBNum.text) <> '' then
          begin
            FSYJsl := Cds_Sub.fieldbyname('Syjsl').AsFloat;
          end
          else
          begin
            FSYJsl := 0;
          end;

        end;

        if Trim(FFieldName) <> 'Syjsl' then
        begin
          FSYJsl := (Fzb * FSPbsl) * (1 + Fsh);
        end;
        with Cds_Sub do
        begin
          Edit;
          FieldByName('Syjsl').Value := FSYJsl;
          Post;
        end;
        tv1.Controller.EditingController.ShowEdit();

        Next;

      end;

    end;
  end;
end;

procedure TfrmCgBjPut.TV1Column6PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
  Fzb, FSPbsl, FSYJsl, Fsh: Double;
  i: Integer;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  FSYJsl := 0;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with Cds_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FSPbsl := Cds_Sub.fieldbyname('SPbsl').AsFloat;
    Fzb := Cds_Sub.fieldbyname('Szb').AsFloat / 100;
    Fsh := Cds_Sub.fieldbyname('Ssh').AsFloat / 100;
    FSYJsl := Cds_Sub.fieldbyname('Syjsl').AsFloat;
  end;

  if Trim(FFieldName) <> 'Syjsl' then
  begin
    FSYJsl := (Fzb * FSPbsl) * (1 + Fsh);
  end;
  with Cds_Sub do
  begin
    Edit;
    FieldByName('Syjsl').Value := FSYJsl;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCgBjPut.TV1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PRTColor';
      flagname := '纱线颜色';
      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
 //     MainType:=Trim(DName);
//      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_ZB.Edit;
        Self.CDS_ZB.FieldByName('PRTColor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.Order_ZB.FieldByName('SOrddefstr4').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

