unit U_ClaimsPut;

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,
  dxScrollbarAnnotations;

type
  TfrmClaimsPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    Order_Sub: TClientDataSet;
    cxgrdpmn1: TcxGridPopupMenu;
    pm_sub: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label5: TLabel;
    Label13: TLabel;
    Label8: TLabel;
    Label15: TLabel;
    ClaDate: TDateTimePicker;
    ClaNo: TEdit;
    OurCoName: TBtnEditC;
    OppCoName: TBtnEditC;
    FinType: TBtnEditC;
    Currency: TComboBox;
    KKNote: TBtnEditC;
    QKNote: TBtnEditC;
    ClaAmount: TEdit;
    ClaQty: TEdit;
    Label1: TLabel;
    CLNote: TBtnEditC;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton4: TToolButton;
    QtyUnit: TComboBox;
    ClaType: TComboBox;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnDnClick(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 OppCoNameBtnUpClick(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure ClaTypeBtnUpClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ClaAmountKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;
    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;
    FClaMid: string;
    FAuthority, FGSTT: string;
    { Public declarations }
  end;

var
  frmClaimsPut: TfrmClaimsPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel, U_YarnInfoSel,
  U_PurTradePlanSel, U_TradeDefectKCListSel;

{$R *.dfm}

procedure TfrmClaimsPut.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 TfrmClaimsPut.ClaAmountKeyPress(Sender: TObject; var Key: Char);
begin

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

function TfrmClaimsPut.TestData(): Boolean;
begin
  try
    if Trim(ClaNo.Text) = '' then
      raise Exception.Create('申请单号号不能为空！');
    if Trim(OppCoName.Text) = '' then
      raise Exception.Create('对方单位不能为空！');
    if Trim(OurCoName.Text) = '' then
      raise Exception.Create('我方单位不能为空！');
    if Trim(QtyUnit.Text) = '' then
      raise Exception.Create('数量单位不能为空！');
    if Trim(ClaType.Text) = '' then
      raise Exception.Create('索赔类型不能为空！');

    if Order_Sub.IsEmpty 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('Price', null, []) then
//      raise Exception.Create('单价不能为空！');
//    if Order_Sub.Locate('Qty', null, []) then
//      raise Exception.Create('数量不能为空！');
//    if Order_Sub.Locate('Qty', 0, []) then
//      raise Exception.Create('数量不能为0！');
//    if Order_Sub.Locate('QtyUnit', null, []) then
//      raise Exception.Create('数量单位不能为空！');
//    if Order_Sub.Locate('QtyUnit', '', []) then
//      raise Exception.Create('数量单位不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmClaimsPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmClaimsPut.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Claims_Sub ');
    sql.Add('where ClaMId=''' + Trim(FClaMId) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Claims_Main where CLaMId=''' + Trim(FClaMId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  OurCoName.TxtCode := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  OppCoName.TxtCode := Trim(ADOTemp.FieldByName('OppCoNo').AsString);
  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

  if PState = '新增' then
  begin
    ClaDate.DateTime := SGetServerDateTime(ADOTemp);
//    DlyDate.DateTime := PurDate.DateTime;
    ClaNo.Text := '自动生成';
  end;

  if PState = '复制' then
  begin
    PState := '新增';
    FClaMId := '';
    ClaNo.Text := '自动生成';

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

end;

procedure TfrmClaimsPut.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 := Tv2.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 TfrmClaimsPut.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 TfrmClaimsPut.ClaTypeBtnUpClick(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 TfrmClaimsPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmClaimsPut.FormShow(Sender: TObject);
begin
  inherited;
  readCxGrid(trim(self.Caption), Tv2, '索赔管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

end;

function TfrmClaimsPut.SaveData(): Boolean;
var
  maxno, SMaxNo: string;
  MClaNo, fDate: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FClaMId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'CM', 'Claims_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');

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

      if trim(ClaNo.Text) = '自动生成' then
      begin
        if GetLSNo(ADOTemp, MClaNo, fDate, 'Claims_Main', 4, 0) = False then
          raise Exception.Create('取最新采购单号失败！');
        ClaNo.Text := MClaNo;
      end;
    end
    else
    begin
      maxno := Trim(FClaMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Claims_Main where ClaMId=''' + Trim(FClaMId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FClaMId) = '' 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('ClaMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'Claims_Main', ScrollBox1, 2);
      FieldByName('OurCoNo').Value := Trim(OurCoName.TxtCode);
      FieldByName('OppCoNo').Value := OppCoName.TxtCode;
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Claims_Main where ClaNo=''' + Trim(ClaNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('申请单号重复！');

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

        SMaxNo := Trim(Order_Sub.fieldbyname('ClaSId').AsString);

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Claims_Sub    ');
          sql.Add('where   ClaSId=''' + Trim(SMaxNo) + '''');
          SQL.Add('and ClaMId=''' + Trim(FClaMId) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv2, Order_Sub, 'Claims_Sub', 0);
          FieldByName('ClaMId').Value := Trim(FClaMId);
          FieldByName('ClaSId').Value := Trim(SMaxNo);

          Post;
        end;
        with Order_Sub do
        begin
          Edit;
          FieldByName('ClaSId').Value := Trim(SMaxNo);
          Post;
        end;
        Next;
      end;
    end;
////////////////       保存子表          //////////////////////

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

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

procedure TfrmClaimsPut.TBSaveClick(Sender: TObject);
begin
  ClaDate.SetFocus;

  if TestData() = false then
    exit;

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

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

  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQty := Fieldbyname('Qty').AsFloat;
    FPrice := Fieldbyname('Price').AsFloat;
  end;
  FAmount := RoundFloat(FQty * FPrice, 2);

  with Order_Sub do
  begin
    Edit;
    FieldByName('Qty').Value := FQty;
    FieldByName('Price').Value := FPrice;
    FieldByName('Amount').Value := FAmount;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

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

  if GetLSNo(ADOCmd, maxno, 'CS', 'Claims_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录

  try
    frmPurTradePlanSel := TfrmPurTradePlanSel.Create(Application);
    with frmPurTradePlanSel do
    begin
//      FMainID := MainID.Text;
      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.Order_Sub do
              begin
                Append;
//                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('ClaSId').Value := maxno + INTTOSTR(i);
                FieldByName('C_Code').Value := CDS_1.fieldbyname('C_Code').value;
                FieldByName('OrderNo').Value := CDS_1.fieldbyname('OrderNo').value;
                FieldByName('C_Name').Value := CDS_1.fieldbyname('C_Name').value;
                FieldByName('BuyConNo').Value := CDS_1.fieldbyname('BuyConNo').value;
                FieldByName('C_Pattern').Value := CDS_1.fieldbyname('C_Pattern').value;
                FieldByName('C_ColorNo').Value := CDS_1.fieldbyname('C_ColorNo').value;
                FieldByName('C_Width').Value := CDS_1.fieldbyname('C_Width').value;
                FieldByName('C_GramWeight').Value := CDS_1.fieldbyname('C_GramWeight').value;
                FieldByName('C_Color').Value := CDS_1.fieldbyname('C_Color').value;

                FieldByName('SubId').Value := CDS_1.fieldbyname('SubId').value;
                FieldByName('MainId').Value := CDS_1.fieldbyname('MainId').value;

              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmPurTradePlanSel.Free;
  end;

  SetXH();
end;

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

  SetXH();
end;

procedure TfrmClaimsPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv2, '索赔管理');
end;

procedure TfrmClaimsPut.ToolButton4Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin

  if GetLSNo(ADOCmd, maxno, 'CS', 'Claims_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录

  try
    frmTradeDefectKCListSel := TfrmTradeDefectKCListSel.Create(Application);
    with frmTradeDefectKCListSel do
    begin
//      FMainID := MainID.Text;
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;
//                FieldByName('SSEL').Value := False;
            FieldByName('SerialNo').Value := i;
            FieldByName('ClaSId').Value := maxno + INTTOSTR(i);
            FieldByName('OrderNo').Value := CDS_1.fieldbyname('OrderNo').value;
            FieldByName('BuyConNO').Value := CDS_1.fieldbyname('BuyConNO').value;
            FieldByName('C_Code').Value := CDS_1.fieldbyname('C_Code').value;
            FieldByName('C_Name').Value := CDS_1.fieldbyname('C_Name').value;
            FieldByName('C_Width').Value := CDS_1.fieldbyname('C_Width').value;
            FieldByName('C_GramWeight').Value := CDS_1.fieldbyname('C_GramWeight').value;
            FieldByName('C_StyleNo').Value := CDS_1.fieldbyname('C_StyleNo').value;
            FieldByName('C_Pattern').Value := CDS_1.fieldbyname('C_Pattern').value;
            FieldByName('C_ColorNo').Value := CDS_1.fieldbyname('C_ColorNo').value;
            FieldByName('C_EColor').Value := CDS_1.fieldbyname('C_EColor').value;
            FieldByName('C_Color').Value := CDS_1.fieldbyname('C_Color').value;

            FieldByName('SubId').Value := CDS_1.fieldbyname('SubId').value;
            FieldByName('MainId').Value := CDS_1.fieldbyname('MainId').value;

//                FieldByName('KKZY').Value := CDS_1.fieldbyname('KKZY').value;
//                FieldByName('KouFenQty').Value := CDS_1.fieldbyname('KouFenQty').value;
//                FieldByName('FlawList1').Value := CDS_1.fieldbyname('FlawList1').value;
//                FieldByName('FwNote').Value := CDS_1.fieldbyname('FwNote').value;
            FieldByName('FtyPCID').Value := CDS_1.fieldbyname('FtyPCID').value;

            FieldByName('GrossWeight').Value := CDS_1.fieldbyname('GrossWeight').value;
            FieldByName('Tare').Value := CDS_1.fieldbyname('Tare').value;
            FieldByName('NetWeight').Value := CDS_1.fieldbyname('NetWeight').value;
            FieldByName('Meter').Value := CDS_1.fieldbyname('Meter').value;
            FieldByName('Yardage').Value := CDS_1.fieldbyname('Yardage').value;
            FieldByName('CIID').Value := CDS_1.fieldbyname('CIID').value;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
          i := i + 1;
        end;
      end;

    end;
  finally
    frmTradeDefectKCListSel.Free;
  end;

  SetXH();
end;

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

      if ShowModal = 1 then
      begin
        self.OurCoName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OurCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmClaimsPut.OurCoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmClaimsPut.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;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmClaimsPut.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;
  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmClaimsPut.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;
  tv2.Controller.EditingController.ShowEdit();
end;

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

      Order_Sub.fieldbyname('C_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;
      Order_Sub.fieldbyname('C_EName').Value := frmClothInfoSel.CDS_1.fieldbyname('C_EName').asstring;
      Order_Sub.fieldbyname('C_Spec').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      Order_Sub.fieldbyname('C_Composition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;
      Order_Sub.fieldbyname('C_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;

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

procedure TfrmClaimsPut.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 TfrmClaimsPut.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

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

procedure TfrmClaimsPut.OppCoNameBtnUpClick(Sender: TObject);
begin
  if ClaType.text='' then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    Exit;
  end;

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := Trim(ClaType.text);
      if ShowModal = 1 then
      begin
        self.OppCoName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OppCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

end.

