unit U_TatDRtInPut;

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, cxNavigator, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, dxScrollbarAnnotations, DateUtils;

type
  TfrmTatDRtInPut = class(Tform)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    OutType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    ToCoName: TBtnEditC;
    Tv1C_width: TcxGridDBColumn;
    Tv1Qty: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    dazi1: TcxStyle;
    Tv1C_Name: TcxGridDBColumn;
    Label2: TLabel;
    DlyDate: TDateTimePicker;
    Tv1Piece: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label27: TLabel;
    QtyUnit: TComboBox;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Tv1C_code: TcxGridDBColumn;
    Label4: TLabel;
    Seal: TEdit;
    Label5: TLabel;
    tube: TEdit;
    Label6: TLabel;
    Packaging: TEdit;
    Tv1c_Color: TcxGridDBColumn;
    Tv1C_GramWeight: TcxGridDBColumn;
    Tv1PINGFANG: TcxGridDBColumn;
    Tv1Volume: TcxGridDBColumn;
    Tv1Applicant: TcxGridDBColumn;
    Tv1ApplicationReaon: TcxGridDBColumn;
    TbPHSave: TToolButton;
    Label7: TLabel;
    ToCoID: TEdit;
    Tv1issp: TcxGridDBColumn;
    Tv1PFPrice: TcxGridDBColumn;
    Tv1editStatus: TcxGridDBColumn;
    TbSavePFPrice: TToolButton;
    Tv1isIncludeTax: TcxGridDBColumn;
    Tv1note: TcxGridDBColumn;
    TbSaveNote: TToolButton;
    Label3: TLabel;
    CompanyName: TBtnEditC;
    Tv1Grade: TcxGridDBColumn;
    Label8: TLabel;
    KHaddress: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToCoNameBtnUpClick(Sender: TObject);
    procedure ToCoNameBtnDnClick(Sender: TObject);
    procedure JSQtyUnitChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure SHDANWEIBtnUpClick(Sender: TObject);
    procedure SHDANWEIBtnDnClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TbPHSaveClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TbSavePFPriceClick(Sender: TObject);
    procedure TbSaveNoteClick(Sender: TObject);
    procedure CompanyNameBtnUpClick(Sender: TObject);
    procedure CompanyNameBtnDnClick(Sender: TObject);
    procedure Tv1GradePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    fuserName: string;
    procedure InitData();
    function SaveData(): Boolean;
    function SavePHData(): Boolean;
    procedure C_NumHB();
    function CheckCusShip(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FDRMID, canshu1: string;
    isCheck: Integer; /////////判断修改是不是来自已审核状态
    { Public declarations }
  end;

var
  frmTatDRtInPut: TfrmTatDRtInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompanySel, U_TatPlanSel, U_TatClothInfoSel,
  U_TatClothStkHZListSEL,U_KHCompanySel;

{$R *.dfm}

procedure TfrmTatDRtInPut.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTatDRtInPut.TbPHSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;

  if Trim(OutType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ToCoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空！', '提示', 0);
    Exit;
  end;
  if (Order_Sub.Locate('Piece', null, [])) and (Order_Sub.Locate('Qty', null, [])) = True then
  begin
    Application.MessageBox('细码申请匹数和数量不能全为空！', '提示', 0);
    Exit;
  end;
  if SavePHData() then
  begin
    Application.MessageBox('配货保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmTatDRtInPut.InitData();
var
  fsj: string;
begin
  if PState = 0 then
  begin
    DlyDate.DateTime := strtodatetime(Trim(FormatDateTime('yyyy-MM-dd', Now)));

  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from Tat_Cloth_DR_Sub A');
    if (PState = 1) or (PState = 2) or (PState = 999) or (PState = 888) then   ////查看和修改 和修改单价和修改备注
    begin
      sql.Add('where DRMID=''' + Trim(FDRMID) + '''');
    end;
    if PState = 0 then  ////新增
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOQuery1, Order_Sub);
  SInitCDSData(ADOQuery1, Order_Sub);

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from Tat_Cloth_DR_Main A');
    sql.Add(' where DRMID=''' + Trim(FDRMID) + ''' ');
    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 1);

end;

procedure TfrmTatDRtInPut.JSQtyUnitChange(Sender: TObject);
begin
  if not Order_Sub.IsEmpty then
  begin

    exit;
  end;
end;

procedure TfrmTatDRtInPut.SHDANWEIBtnDnClick(Sender: TObject);
begin
//  SHDANWEI.Text := '';
end;

procedure TfrmTatDRtInPut.SHDANWEIBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin

      flag := 'SHDANWEI';
      flagname := '收货单位';
      if ShowModal = 1 then
      begin
//        SHDANWEI.text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatDRtInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin

  readCxGrid(trim(self.Caption), Tv1, '发货管理');

  if canshu1 = '查询' then
  begin
    Tv1PFPrice.Visible := false;
  end;

  InitData();
end;

function TfrmTatDRtInPut.SavePHData(): Boolean;
var
  MaxSid: string;
  CurrentYear: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
  /////////////////              保存子表                /////////////////
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxSid, 'TDS', 'Tat_Cloth_DR_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败！');
        end
        else
        begin
          MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
        end;

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Cloth_DR_Sub ');
          sql.Add(' where DRMID=''' + Trim(FDRMID) + '''');
          sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
          Open;
          if ADOCmd.IsEmpty then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('Volume').Value := Order_Sub.fieldbyname('Volume').Value;
          FieldByName('Applicant').Value := trim(DName);
          FieldByName('ApplicationReaon').Value := Order_Sub.fieldbyname('ApplicationReaon').Value;
          Post;
        end;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
   /////////////////              保存子表                /////////////////
    FDRMID := Trim(FDRMID);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmTatDRtInPut.CompanyNameBtnDnClick(Sender: TObject);
begin
  CompanyName.Text := '';
end;

procedure TfrmTatDRtInPut.CompanyNameBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('CompanyName/公司抬头', fsj); //先英文再中文
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatDRtInPut.C_NumHB();
var
  LocateValues: variant;
  TempTable: TClientDataSet;
begin
    // 创建临时表存储合并结果
  TempTable := TClientDataSet.Create(nil);
  try
    // 复制原表结构
    TempTable.CloneCursor(Order_Sub, False);
    TempTable.Close;
    TempTable.FieldDefs.Assign(Order_Sub.FieldDefs);
    TempTable.CreateDataSet;

    Order_Sub.First;

    while not Order_Sub.Eof do
    begin
      // 只处理 DRSID 为空的记录
//      if Order_Sub.FieldByName('DRSID').IsNull or (trim(Order_Sub.FieldByName('DRSID').AsString) = '') then
//      begin
      LocateValues := VarArrayOf([trim(Order_Sub.FieldByName('C_Name').AsString), trim(Order_Sub.FieldByName('C_width').AsString), trim(Order_Sub.FieldByName('C_GramWeight').AsString)]);

        // 在临时表中查找是否已存在相同规格的记录
      if TempTable.Locate('C_Name;C_width;C_GramWeight', LocateValues, []) then
      begin
          // 存在则累加 Piece 值
        TempTable.Edit;
        TempTable.FieldByName('Piece').AsFloat := TempTable.FieldByName('Piece').AsFloat + Order_Sub.FieldByName('Piece').AsFloat;
        TempTable.FieldByName('Qty').AsFloat := TempTable.FieldByName('Qty').AsFloat + Order_Sub.FieldByName('Qty').AsFloat;
        TempTable.Post;
        if (not Order_Sub.FieldByName('DRSID').IsNull) and (trim(Order_Sub.FieldByName('DRSID').AsString) <> '') then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete Tat_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
            ExecSQL;
          end;
        end;

      end
      else
      begin
          // 不存在则添加新记录
        TempTable.Append;
        TempTable.CopyFields(Order_Sub); // 复制所有字段值
        TempTable.Post;
      end;
//      end;

      Order_Sub.Next;
    end;

    // 用临时表替换原数据集
    Order_Sub.DisableControls;
    try
      Order_Sub.EmptyDataSet;
      TempTable.First;
      while not TempTable.Eof do
      begin
        Order_Sub.Append;
        Order_Sub.CopyFields(TempTable);
        Order_Sub.Post;
        TempTable.Next;
      end;
    finally
      Order_Sub.EnableControls;
    end;

  finally
    TempTable.Free;
  end;
end;

function TfrmTatDRtInPut.CheckCusShip(): Boolean;
begin
//  try
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('select * from Tat_Cloth_DR_Main where DRMID=''' + Trim(MaxMid) + '''');
//      Open;
//    end;
//    Result := True;
//  except
//    ADOCmd.Connection.RollbackTrans;
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    Result := False;
//  end;
end;

function TfrmTatDRtInPut.SaveData(): Boolean;
var
  MaxMid, MaxSid: string;
  CurrentYear: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    /////////////////              保存主表                /////////////////
    if Trim(FDRMID) = '' then
    begin
      CurrentYear := 'RSG' + FormatDateTime('yy', Now);
      if GetLSNo(ADOCmd, MaxMid, CurrentYear, 'Tat_Cloth_DR_Main', 5, 0) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      MaxMid := Trim(FDRMID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Tat_Cloth_DR_Main where DRMID=''' + Trim(MaxMid) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FDRMID) = '' then
      begin
        Append;
        FieldByName('FillID').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditID').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Tat_Cloth_DR_Main', ScrollBox1, 1);
      FieldByName('DRMID').Value := Trim(MaxMid);
      if isCheck <> 1 then
      begin
        FieldByName('Status').Value := '0';
      end;

      if isCheck = 1 then ///标记审核修改
      begin
        FieldByName('CheckEdit').Value := '已改单';
        FieldByName('CheckEditTime').Value := SGetServerDateTime(ADOTemp);

        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select sum(Piece) as sumPiece from Tat_Cloth_DR_Sub where DRMID=''' + Trim(FDRMID) + '''');
          Open;
        end;
        FieldByName('CheckEditPiece').Value := ADOTemp.FieldByName('sumPiece').Value;
      end;
      Post;
    end;
  /////////////////              保存主表                /////////////////
  ///
  /////////////////              保存子表                /////////////////
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxSid, 'TDS', 'Tat_Cloth_DR_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败！');
        end
        else
        begin
          MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
        end;

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Cloth_DR_Sub ');
          sql.Add(' where DRMID=''' + Trim(MaxMid) + '''');
          sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
          Open;
          if ADOCmd.IsEmpty then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Tat_Cloth_DR_Sub', 0);
          FieldByName('DRMID').Value := Trim(MaxMid);
          FieldByName('DRSID').Value := Trim(MaxSid);
          FieldByName('QtyUnit').Value := QtyUnit.Text;
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('ConMId').Value := Order_Sub.fieldbyname('ConMId').Value;
          FieldByName('ConSId').Value := Order_Sub.fieldbyname('ConSId').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('DRSID').Value := Trim(MaxSid);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
   /////////////////              保存子表                /////////////////
    FDRMID := Trim(MaxMid);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmTatDRtInPut.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
//ShowMessage(Order_Sub.FieldByName('Piece').AsString);
  ToolBar1.SetFocus;

  if Trim(OutType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ToCoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(CompanyName.Text) = '' then
  begin
    Application.MessageBox('公司抬头不能为空！', '提示', 0);
    Exit;
  end;

  if (Order_Sub.Locate('Piece', null, [])) and (Order_Sub.Locate('Qty', null, [])) then
  begin
    Application.MessageBox('细码申请匹数和数量不能全为空！', '提示', 0);
    Exit;
  end;

  if ((Order_Sub.Locate('PFPrice', null, [])) = True) then
  begin
    Application.MessageBox('平方单价不能为空！', '提示', 0);
    Exit;
  end;

  if ((Order_Sub.Locate('isIncludeTax', null, [])) = True) or ((Order_Sub.Locate('isIncludeTax', '', [])) = True) then
  begin
    Application.MessageBox('是否含税不能为空！', '提示', 0);
    Exit;
  end;

  if ((Order_Sub.Locate('Grade', null, [])) = True) or ((Order_Sub.Locate('Grade', '', [])) = True) then
  begin
    Application.MessageBox('等级不能为空！', '提示', 0);
    Exit;
  end;

//  ShowMessage(Order_Sub.FieldByName('Piece').AsString);
  C_NumHB();
///////////////////////////////////////配货申请原因////////////////////
  Order_Sub.First;
  while not Order_Sub.Eof do
  begin
    if (Order_Sub.FieldByName('Volume').AsString <> '') and (not Order_Sub.FieldByName('Volume').isnull) then
    begin
      if ((Order_Sub.FieldByName('ApplicationReaon').AsString = '') or (Order_Sub.FieldByName('ApplicationReaon').isnull)) then
      begin
        Application.MessageBox('请填写配货申请原因！', '提示信息', MB_OK);
        Exit;
      end;
    end;
    Order_Sub.Next;
  end;

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

procedure TfrmTatDRtInPut.TbSaveNoteClick(Sender: TObject);
var
  MaxMid, MaxSid: string;
  CurrentYear: string;
begin
  if (Order_Sub.Locate('note', null, [])) = True then
  begin
//    Application.MessageBox('平方单价不能为空！', '提示', 0);
//    Exit;
  end;
     /////////////////              保存子表                /////////////////
  Order_Sub.DisableControls;
  MaxMid := Trim(FDRMID);
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
//      if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
//      begin
//        if GetLSNo(ADOCmd, MaxSid, 'TDS', 'Tat_Cloth_DR_Sub', 4, 1) = False then
//          raise Exception.Create('取子流水号失败！');
//      end
//      else
//      begin
      MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
//      end;

      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from Tat_Cloth_DR_Sub ');
        sql.Add(' where DRMID=''' + Trim(MaxMid) + '''');
        sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
//        ShowMessage(sql.Text);
        Open;
        if ADOCmd.IsEmpty then
        begin
          Append;
        end
        else
        begin
          Edit;
        end;
        FieldByName('note').Value := Order_Sub.fieldbyname('note').Value;
        Post;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;

  Application.MessageBox('保存成功！', '提示', 0);
  ModalResult := 1;
end;

procedure TfrmTatDRtInPut.TbSavePFPriceClick(Sender: TObject);
var
  MaxMid, MaxSid: string;
  CurrentYear: string;
begin
  if (Order_Sub.Locate('PFPrice', null, [])) = True then
  begin
    Application.MessageBox('平方单价不能为空！', '提示', 0);
    Exit;
  end;

     /////////////////              保存子表                /////////////////
  Order_Sub.DisableControls;
  MaxMid := Trim(FDRMID);
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
//      if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
//      begin
//        if GetLSNo(ADOCmd, MaxSid, 'TDS', 'Tat_Cloth_DR_Sub', 4, 1) = False then
//          raise Exception.Create('取子流水号失败！');
//      end
//      else
//      begin
      MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
//      end;

      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from Tat_Cloth_DR_Sub ');
        sql.Add(' where DRMID=''' + Trim(MaxMid) + '''');
        sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
//        ShowMessage(sql.Text);
        Open;
        if ADOCmd.IsEmpty then
        begin
          Append;
        end
        else
        begin
          Edit;
        end;
        FieldByName('PFPrice').Value := Order_Sub.fieldbyname('PFPrice').Value;
        Post;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;

  Application.MessageBox('保存成功！', '提示', 0);
  ModalResult := 1;
end;

procedure TfrmTatDRtInPut.ToolButton1Click(Sender: TObject);
var
  FQtyUnit, FFSubid: string;
begin
 {
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
//          Self.CustomerNoName.Text := Trim(frmTradePlanSel.Order_Main.fieldbyname('CustomerNoName').AsString);
//          FQtyUnit := Trim(frmTradePlanSel.Order_Main.fieldbyname('orderUnit').AsString);
//          if Self.Order_Sub.Locate('Subid', Trim(frmTradePlanSel.Order_Main.fieldbyname('Subid').AsString), []) = False then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('SCConNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('Orderno').AsString);
              FieldByName('SSConMainId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConMainId').Value;
              FieldByName('SSConSubId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConSubId').Value;
              FieldByName('MainId').Value := frmTradePlanSel.CDS_1.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmTradePlanSel.CDS_1.fieldbyname('SubId').Value;
              FieldByName('PRTColor').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_Color').AsString);
              FieldByName('SPrtCode').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_Code').AsString);
              FieldByName('SPrtCodeName').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_CodeName').AsString);
              FieldByName('PRTHX').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_HX').AsString);
//              FieldByName('orderUnit').Value := JSQtyUnit.Text;
//              if Trim(JSQtyUnit.Text) = 'Kg' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYGW').Value;
//              end
//              else if Trim(JSQtyUnit.Text) = 'M' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYLen').Value;
//              end
//              else if Trim(JSQtyUnit.Text) = 'Y' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYYLen').Value;
//              end;

            end;
          end;
          CDS_1.Delete;
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;   }
end;

procedure TfrmTatDRtInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('DRSID').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
      sql.add(' and isnull(DlyPiece,0)>0');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已发货不能删除数据!', '提示', 0);
      exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Tat_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmTatDRtInPut.ToolButton3Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
  LocateValues: variant;
begin
//  try
//    frmTatPlanSel := TfrmTatPlanSel.Create(Application);
//    with frmTatPlanSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        while CDS_1.locate('SSel', true, []) do
//        begin
////          if not self.Order_Sub.locate('ConSId', trim(frmTatPlanSel.CDS_1.Fieldbyname('ConSId').AsString), []) then
////          begin
//          with Self.Order_Sub do
//          begin
//            Append;
//            FieldByName('ConNo').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConNo').AsString);
//            FieldByName('ConMId').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConMId').AsString);
//            FieldByName('ConSId').Value := frmTatPlanSel.CDS_1.fieldbyname('ConSId').Value;
//            FieldByName('BuyConNo').Value := frmTatPlanSel.CDS_1.fieldbyname('BuyConNo').Value;
//            FieldByName('C_Code').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Code').Value;
//            FieldByName('C_Name').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Name').Value;
//            FieldByName('C_Spec').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Spec').Value;
//            FieldByName('C_Width').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Width').Value;
//            FieldByName('C_GramWeight').Value := frmTatPlanSel.CDS_1.fieldbyname('C_GramWeight').Value;
//            FieldByName('C_Color').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Color').Value;
//            FieldByName('C_ColorNo').Value := frmTatPlanSel.CDS_1.fieldbyname('C_ColorNo').Value;
//            FieldByName('C_Pattern').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Pattern').Value;
////            FieldByName('Piece').Value := frmTatPlanSel.CDS_1.fieldbyname('SumPiece').Value;
////            FieldByName('Qty').Value := frmTatPlanSel.CDS_1.fieldbyname('SumStkGrossWeight').Value;
//          end;
////          end;
//          CDS_1.Edit;
//          CDS_1.fieldbyname('SSel').value := false;
//        end;
//      end;
//    end;
//  finally
//    frmTatPlanSel.Free;
//  end;

  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
//        while cds_sub.locate('SSel', true, []) do
        CDS_2.First;
        while not CDS_2.Eof do
        begin
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('C_Code').Value := frmTatClothInfoSel.cds_2.fieldbyname('C_Code').Value;
            FieldByName('C_Name').Value := frmTatClothInfoSel.cds_2.fieldbyname('C_Name').Value;
//            FieldByName('C_Spec').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Spec').Value;
            FieldByName('C_Width').Value := frmTatClothInfoSel.cds_2.fieldbyname('C_MXMF').Value;
            FieldByName('C_GramWeight').Value := frmTatClothInfoSel.cds_2.fieldbyname('C_GramWeight').Value;
            FieldByName('C_Color').Value := frmTatClothInfoSel.cds_2.fieldbyname('C_Color').Value;
//            FieldByName('C_ColorNo').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_ColorNo').Value;
//            FieldByName('C_Pattern').Value := frmTatClothInfoSel.CDS_1.fieldbyname('C_Pattern').Value;
          end;
          cds_2.Next;
        end;

      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;

end;

procedure TfrmTatDRtInPut.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '发货管理')
end;

procedure TfrmTatDRtInPut.ToolButton5Click(Sender: TObject);
begin
  try
    frmTatPlanSel := TfrmTatPlanSel.Create(Application);
    with frmTatPlanSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
//          if not self.Order_Sub.locate('ConSId', trim(frmTatPlanSel.CDS_1.Fieldbyname('ConSId').AsString), []) then
//          begin
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('ConNo').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConNo').AsString);
            FieldByName('ConMId').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConMId').AsString);
            FieldByName('ConSId').Value := frmTatPlanSel.CDS_1.fieldbyname('ConSId').Value;
            FieldByName('BuyConNo').Value := frmTatPlanSel.CDS_1.fieldbyname('BuyConNo').Value;
            FieldByName('C_Code').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Code').Value;
            FieldByName('C_Name').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Name').Value;
            FieldByName('C_Spec').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Spec').Value;
            FieldByName('C_Width').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Width').Value;
            FieldByName('C_GramWeight').Value := frmTatPlanSel.CDS_1.fieldbyname('C_GramWeight').Value;
            FieldByName('C_Color').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Color').Value;
            FieldByName('C_ColorNo').Value := frmTatPlanSel.CDS_1.fieldbyname('C_ColorNo').Value;
            FieldByName('C_Pattern').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Pattern').Value;

            FieldByName('orderno').Value := frmTatPlanSel.CDS_1.fieldbyname('orderno').Value;
//            FieldByName('Piece').Value := frmTatPlanSel.CDS_1.fieldbyname('SumPiece').Value;
//            FieldByName('Qty').Value := frmTatPlanSel.CDS_1.fieldbyname('SumStkGrossWeight').Value;
          end;
//          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmTatPlanSel.Free;
  end;
end;

procedure TfrmTatDRtInPut.ToolButton6Click(Sender: TObject);
var
  LocateValues: variant;
begin
  try
    frmTatClothStkHZListSEL := tfrmTatClothStkHZListSEL.Create(Application);
    with frmTatClothStkHZListSEL do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
//          if not self.Order_Sub.locate('C_name', trim(frmTatClothStkHZListSEL.CDS_1.Fieldbyname('C_Name').AsString), []) then
//          begin
//            with Self.Order_Sub do
//            begin
//              Append;
//              FieldByName('C_code').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_code').Value;
//              FieldByName('C_Name').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_Name').Value;
//              FieldByName('C_width').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_width').Value;
//
//              FieldByName('C_GramWeight').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_GramWeight').Value;
//              FieldByName('C_color').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_color').Value;
//              FieldByName('Piece').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('js').Value;
//              FieldByName('Qty').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('stkmeter').Value;
//
//            end;
//          end;

          LocateValues := VarArrayOf([trim(frmTatClothStkHZListSEL.CDS_1.Fieldbyname('C_Name').AsString), trim(frmTatClothStkHZListSEL.CDS_1.Fieldbyname('C_width').AsString), trim(frmTatClothStkHZListSEL.CDS_1.Fieldbyname('C_GramWeight').AsString)]);

          if self.Order_Sub.locate('C_name;C_width;C_GramWeight', LocateValues, []) then
          begin
    // 三个字段都匹配，执行编辑 - 这里已经定位到相同记录的那一条数据
            with Self.Order_Sub do
            begin
              Edit;
              FieldByName('Piece').AsFloat := FieldByName('Piece').AsFloat + frmTatClothStkHZListSEL.CDS_1.fieldbyname('js').AsFloat;
              FieldByName('Qty').AsFloat := FieldByName('Qty').AsFloat + frmTatClothStkHZListSEL.CDS_1.fieldbyname('stkmeter').AsFloat;
              Post; // 建议加上Post保存编辑
            end;
          end
          else
          begin
    // 三个字段不完全匹配，添加新记录
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('C_code').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_code').Value;
              FieldByName('C_Name').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_Name').Value;
              FieldByName('issp').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('issp').Value;

              FieldByName('C_width').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_width').Value;
              FieldByName('C_GramWeight').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_GramWeight').Value;
              FieldByName('C_color').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('C_color').Value;
              FieldByName('Piece').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('js').Value;
              FieldByName('Qty').Value := frmTatClothStkHZListSEL.CDS_1.fieldbyname('stkmeter').Value;
              Post; // 建议加上Post保存添加
            end;
          end;

          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmTatClothStkHZListSEL.Free;
  end;
end;

procedure TfrmTatDRtInPut.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if (Order_Sub.FieldByName('DlyQty').AsString = '') and (Order_Sub.FieldByName('Volume').AsString = '') then
  begin
    Tv1Piece.Options.Editing := True;
    Tv1Qty.Options.Editing := True;
    Tv1PINGFANG.Options.Editing := True;
  end
  else
  begin
    Tv1Piece.Options.Editing := False;
    Tv1Qty.Options.Editing := False;
    Tv1PINGFANG.Options.Editing := False;
  end;

end;

procedure TfrmTatDRtInPut.Tv1GradePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Grade';
      flagname := '等级';
      if ShowModal = 1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('Grade').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatDRtInPut.ToCoNameBtnUpClick(Sender: TObject);
begin

  try
    frmKHCompanySel := TfrmKHCompanySel.Create(Application);
    with frmKHCompanySel do
    begin
      FCoType := '客户';
      if canshu1 = '录入' then
      begin
        FAuthority := '理单业务';
      end;
      if ShowModal = 1 then
      begin
        self.ToCoName.TxtCode := Trim(frmKHCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.ToCoName.Text := Trim(frmKHCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.ToCoID.Text := Trim(frmKHCompanySel.CDS_1.fieldbyname('CoID').AsString);

         self.KHAddress.Text := Trim(frmKHCompanySel.CDS_2.fieldbyname('KHaddress').AsString);
      end;
    end;

  finally
    frmKHCompanySel.Free;
  end;

end;

procedure TfrmTatDRtInPut.ToCoNameBtnDnClick(Sender: TObject);
begin
  ToCoName.Text := '';
end;

end.

