unit U_YSKInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, ExtCtrls, StdCtrls, BtnEdit, cxButtonEdit, cxTextEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmYSKInput = class(TForm)
    DataSource1: TDataSource;
    YFSub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    TADD: TToolButton;
    TDEl: TToolButton;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_Spec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label1: TLabel;
    YFID: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    CRTime: TDateTimePicker;
    Note: TMemo;
    PayMent: TBtnEditA;
    FactoryName: TBtnEditA;
    Label4: TLabel;
    Label6: TLabel;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label8: TLabel;
    FtySYRName: TBtnEditA;
    Label9: TLabel;
    Money: TEdit;
    Label10: TLabel;
    HuiLv: TEdit;
    BZType: TComboBox;
    Label11: TLabel;
    Label12: TLabel;
    BBMoney: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label7: TLabel;
    v1Column4: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column5: TcxGridDBColumn;
    Label5: TLabel;
    ishs: TComboBox;
    Label13: TLabel;
    SYRName: TBtnEditA;
    ToolButton2: TToolButton;
    YFName: TBtnEditA;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    Label15: TLabel;
    ranchang: TBtnEditA;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Tv1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure PayMentBtnClick(Sender: TObject);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TADDClick(Sender: TObject);
    procedure TDElClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure FtySYRNameBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure HuiLvKeyPress(Sender: TObject; var Key: Char);
    procedure Panel1Click(Sender: TObject);
    procedure HuiLvExit(Sender: TObject);
    procedure v1MoneyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure YFNameBtnClick(Sender: TObject);
    procedure SYRNameBtnClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ranchangBtnClick(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    procedure HJJE();
    { Private declarations }
  public
    FYFID: string;
    { Public declarations }
  end;

var
  frmYSKInput: TfrmYSKInput;

implementation

uses
  U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_dataLink, U_ZdyAttachGYS, U_CPManageSel,
  U_YSSCList, U_SXXSListSel, U_PBCKlistSel, U_ZZJWLCKListSel, U_Fun;
{$R *.dfm}

procedure TfrmYSKInput.HJJE();
var
  FMoney, FHuiLv, FBBMoney: double;
begin
  if TV1.DataController.Summary.FooterSummaryValues[1] <> NULL then
    FMoney := TV1.DataController.Summary.FooterSummaryValues[1]
  else
    FMoney := 0;

  FHuiLv := strtofloat(HuiLv.Text);
  if FHuiLv = 0 then
    FHuiLv := 1;
  FBBMoney := FHuiLv * FMoney;
  Money.Text := floattostr(FMoney);
  HuiLv.Text := floattostr(FHuiLv);
  BBMoney.Text := floattostr(FBBMoney);
end;

procedure TfrmYSKInput.SaveDate();
var
  MaxYFSID, MaxYFID: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    if trim(FYFID) = '' then
    begin
      if GetLSNo(ADOQueryTmp, MaxYFID, 'YS', 'YF_Money_CR_Sub', 4, 1) = False then
      begin
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
      YFID.Text := MaxYFID;
    end
    else
    begin
      MaxYFID := trim(FYFID);
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('select * from YF_Money_CR where YFID=' + quotedstr(FYFID));
      open;
    end;
    with ADOQueryCmd do
    begin
      if trim(FYFID) = '' then
      begin
        append;
        Fieldbyname('YFID').value := trim(MaxYFID);
        FieldByName('CRType').Value := '应收款登记';
        FieldByName('CRFlag').Value := '应收收';
        FieldByName('QtyFlag').Value := 1;
        FieldByName('status').Value := '0';
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
      end;

      SSetsaveSqlnew(ADOQueryCmd, 'YF_Money_CR', panel1, 0);
      post;
    end;

    with YFSub do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('YFSID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxYFSID, 'YSS', 'YF_Money_CR_Sub', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxYFSID := YFSub.fieldbyname('YFSID').AsString;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from YF_Money_CR_Sub where YFSID=' + Quotedstr(trim(MaxYFSID)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.Fieldbyname('YFSID').value := trim(MaxYFSID);
          ADOQueryCmd.Fieldbyname('YFID').value := trim(MaxYFID);
        end
        else
        begin
          ADOQueryCmd.edit;
        end;

        SSetSaveDataCDSNew(ADOQueryCmd, Tv1, YFSub, 'YF_Money_CR_Sub', 0);
        ADOQueryCmd.Post;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示');
//    Modalresult:=1;
    FYFID := '';
    initGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示', MB_ICONERROR);
  end;
end;

procedure TfrmYSKInput.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID=' + quotedstr(trim(FYFID)));
    open;
  end;
  if ADOQueryTmp.isempty = False then
  begin
    SCSHDataNew(ADOQueryTmp, Panel1, 0);
  end
  else
  begin
//    SYRName.Text := '';
    FactoryName.Text := '';
    PayMent.Text := '';
    Note.Text := '';
    YFID.Text := '';
    YFName.Text := '';
    BBMoney.Text := '0';
    Money.Text := '0';
    HuiLv.Text := '1';
    SYRName.text := '杭州乙对纺织有限公司';
    CRTime.DateTime := SGetServerDate(ADOQueryTmp);
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR_Sub where YFID=''' + trim(FYFID) + '''');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, YFSub);
  SInitCDSData20(ADOQueryTmp, YFSub);
end;

procedure TfrmYSKInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '财务管理');
  Close;
end;

procedure TfrmYSKInput.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv1, '财务管理');
  initGrid();

end;

procedure TfrmYSKInput.TSaveClick(Sender: TObject);
begin
  yfname.SetFocus;
  if YFSub.IsEmpty then
  begin
    application.MessageBox('明细不能为空', '提示');
    exit;
  end;

  if YFSub.Locate('KXType', null, []) then
  begin
    application.MessageBox('款项类型不能为空', '提示');
    exit;
  end;
  if YFSub.Locate('KXType', '', []) then
  begin
    application.MessageBox('款项类型不能为空', '提示');
    exit;
  end;
  if trim(FactoryName.Text) = '' then
  begin
    application.MessageBox('对方单位不能为空', '提示');
    exit;
  end;
  SaveDate();
end;

procedure TfrmYSKInput.FactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
//        FtySYRName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;

end;

procedure TfrmYSKInput.PayMentBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PayMent';
      flagname := '收款方式';
      if ShowModal = 1 then
      begin
        PayMent.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KXType3';
      flagname := '款项类型';
      if ShowModal = 1 then
      begin
        Self.YFSub.Edit;
        Self.YFSub.FieldByName('KXType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'QtyUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.YFSub.Edit;
        Self.YFSub.FieldByName('QtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.TADDClick(Sender: TObject);
begin
  with YFSub do
  begin
    Append;
    FieldByName('Qty').Value := 0;
    FieldByName('Price').Value := 0;
    FieldByName('MxMoney').Value := 0;
    FieldByName('QtyUnit').Value := 'M';
    Post;
  end;
end;

procedure TfrmYSKInput.TDElClick(Sender: TObject);
begin
  if YFSub.IsEmpty then
    exit;
  if application.MessageBox('确定要删除吗', '提示', 1) = 2 then
    exit;
  if trim(YFSub.fieldbyname('YFSID').asstring) = '' then
  begin
    YFSub.delete;
    HJJE();
  end
  else
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('delete from YF_Money_CR_Sub where YFSID=''' + trim(YFSub.fieldbyname('YFSID').asstring) + ''' ');
      execsql;
    end;
    YFSub.delete;
    HJJE();
    if YFSub.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.clear;
        sql.add('delete from YF_Money_CR where YFID=''' + trim(FYFID) + ''' ');
        execsql;
      end;
    end
    else
    begin
      TSave.Click;
    end;
  end;
end;

procedure TfrmYSKInput.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with YFSub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with YFSub do
  begin
    Edit;
    FQty := FieldByName('Qty').AsFloat;
    FPrice := FieldByName('Price').AsFloat;
    Post;
  end;

  FMxMoney := RoundFloat(FQty * FPrice, 2);
  with YFSub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('MxMoney').Value := FMxMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  HJJE();
end;

procedure TfrmYSKInput.FtySYRNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YSFtySYRName';
      flagname := '对方抬头';
      if ShowModal = 1 then
      begin
        FtySYRName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        with YFSub do
        begin
          Edit;
          FieldByName('CPName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
          FieldByName('CPCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  //self.tv1.Controller.EditingController.ShowEdit();
end;

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

procedure TfrmYSKInput.Panel1Click(Sender: TObject);
begin
  yfname.SetFocus;
end;

procedure TfrmYSKInput.HuiLvExit(Sender: TObject);
begin
  HJJE();
end;

procedure TfrmYSKInput.v1MoneyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with YFSub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  HJJE();
end;

procedure TfrmYSKInput.ToolButton1Click(Sender: TObject);
var
  CFactoryName: string;
begin
  try
    frmYSSCList := TfrmYSSCList.create(self);
    with frmYSSCList do
    begin
      with frmYSSCList do
      begin
        if showmodal = 1 then
        begin
          CFactoryName := Trim(CDS_Main.fieldbyname('SHDanWei').AsString);
//          FtySYRName.Text := CFactoryName;
          CRTime.DateTime := CDS_Main.fieldbyname('FHDate').AsDateTime;
          ishs.ItemIndex := ishs.Items.IndexOf(trim(CDS_Main.fieldbyname('ishs').AsString));
          while CDS_Main.Locate('SSel', true, []) do
          begin
            if trim(CFactoryName) <> Trim(CDS_Main.fieldbyname('SHDanWei').AsString) then
            begin
              Application.MessageBox('不能选择多个对方单位!', '提示', 0);
              Exit;
            end;

            self.YFSub.Append;
//            self.YFGSType.ItemIndex := self.YFGSType.Items.IndexOf('坯布类');
            self.YFSub.FieldByName('YFPZNO').Value := CDS_Main.FieldByName('FSId').Value;
            self.YFSub.FieldByName('KXType').Value := CDS_Main.FieldByName('YFName').Value;
            self.YFSub.FieldByName('CPCode').Value := CDS_Main.FieldByName('PrtCode').Value;
            self.YFSub.FieldByName('CPName').Value := CDS_Main.FieldByName('PrtCodeName').Value;
            self.YFSub.FieldByName('toconno').Value := CDS_Main.FieldByName('toconno').Value;
//            self.ranchang.Text := CDS_Main.FieldByName('ranchang').Value;
            self.YFSub.FieldByName('PS').Value := CDS_Main.FieldByName('HZPS').Value;
            self.YFSub.FieldByName('Qty').Value := CDS_Main.FieldByName('HZQty').Value;
            self.YFSub.FieldByName('QtyUnit').Value := CDS_Main.FieldByName('QtyUnit').Value;
            if CDS_Main.FieldByName('PRTPrice').AsString = '' then
            begin
              self.YFSub.FieldByName('Price').Value := 0;
            end
            else
            begin
              self.YFSub.FieldByName('Price').Value := CDS_Main.FieldByName('PRTPrice').Value;
            end;
            self.YFSub.FieldByName('MxMoney').Value := CDS_Main.FieldByName('money').Value;
            self.YFSub.post;
            if CDS_Main.FieldByName('DYMoney').AsFloat > 0 then
            begin
              self.YFSub.Append;
              self.YFSub.FieldByName('YFPZNO').Value := CDS_Main.FieldByName('FSId').Value;
              self.YFSub.FieldByName('KXType').Value := '打样费';
              self.YFSub.FieldByName('CPCode').Value := CDS_Main.FieldByName('PrtCode').Value;
              self.YFSub.FieldByName('CPName').Value := CDS_Main.FieldByName('PrtCodeName').Value;
              self.YFSub.FieldByName('PS').Value := 0;
              self.YFSub.FieldByName('Qty').Value := 0;
              self.YFSub.FieldByName('Price').Value := 0;
              self.YFSub.FieldByName('MxMoney').Value := CDS_Main.FieldByName('DYMoney').Value;
              self.YFSub.post;
            end;
            CDS_Main.Delete;
          end;
          self.FactoryName.Text := Trim(CFactoryName);
          HJJE();
        end;
      end;
    end;
  finally
    frmYSSCList.free;
  end;
end;

procedure TfrmYSKInput.ToolButton2Click(Sender: TObject);
var
  CFactoryName, CKXType: string;
  CQtyFlag: integer;
begin
  try
    frmSXXSListSel := TfrmSXXSListSel.create(self);
    with frmSXXSListSel do
    begin
      frmSXXSListSel.Caption := '纱线';
      FCKName := '纱线';
      if showmodal = 1 then
      begin
        CFactoryName := Trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
        CRTime.DateTime := CDS_Main.fieldbyname('CRTime').AsDateTime;
        FtySYRName.Text := CFactoryName;
        while CDS_Main.Locate('SSel', true, []) do
        begin
          if trim(CFactoryName) <> Trim(CDS_Main.fieldbyname('ToFactoryName').AsString) then
          begin
            Application.MessageBox('不能选择多个对方单位!', '提示', 0);
            Exit;
          end;

          if Trim(CDS_Main.fieldbyname('CRType').AsString) = '销售退回' then
          begin
            CQtyFlag := -1;
            CKXType := '纱线销售退货';
          end
          else
          begin
            CQtyFlag := 1;
            CKXType := '纱线销售';
          end;
          self.YFSub.Append;
//          self.YFGSType.ItemIndex := self.YFGSType.Items.IndexOf('纱线类');
          self.YFSub.FieldByName('YFPZNO').Value := CDS_Main.FieldByName('SPID').Value;
          self.YFSub.FieldByName('KXType').Value := TRIM(CKXType);
          self.YFSub.FieldByName('CPCode').Value := CDS_Main.FieldByName('SPCode').Value;
          self.YFSub.FieldByName('CPName').Value := CDS_Main.FieldByName('SPName').Value;
          self.YFSub.FieldByName('CPSpec').Value := CDS_Main.FieldByName('SPSpec').Value;
          self.YFSub.FieldByName('PS').Value := CQtyFlag * CDS_Main.FieldByName('PiQty').AsInteger;
          self.YFSub.FieldByName('Qty').Value := CQtyFlag * CDS_Main.FieldByName('Qty').AsFloat;
          self.YFSub.FieldByName('QtyUnit').Value := CDS_Main.FieldByName('QtyUnit').Value;
          self.YFSub.FieldByName('Price').Value := CDS_Main.FieldByName('Price').Value;
          self.YFSub.FieldByName('MxMoney').Value := CQtyFlag * CDS_Main.FieldByName('Money').AsFloat;
          self.YFSub.post;
          CDS_Main.Delete;
        end;
        self.FactoryName.Text := Trim(CFactoryName);
        HJJE();
      end;
    end;

  finally
    frmSXXSListSel.free;
  end;
end;

procedure TfrmYSKInput.YFNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YFName3';
      flagname := '项目及摘要';
      if ShowModal = 1 then
      begin
        YFName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.SYRNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GSTTPB';
      flagname := '供方';
      V1Name.Caption := '中文名称';
      if ShowModal = 1 then
      begin
        SYRName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSKInput.ToolButton3Click(Sender: TObject);
var
  CFactoryName, CKXType: string;
  CQtyFlag: integer;
begin
  try
    frmZZJWLCKListSel := TfrmZZJWLCKListSel.create(self);
    with frmZZJWLCKListSel do
    begin
      with frmZZJWLCKListSel do
      begin
        if showmodal = 1 then
        begin
          CFactoryName := Trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
          CRTime.DateTime := CDS_Main.fieldbyname('CRTime').AsDateTime;
          while CDS_Main.Locate('SSel', true, []) do
          begin
            if trim(CFactoryName) <> Trim(CDS_Main.fieldbyname('ToFactoryName').AsString) then
            begin
              Application.MessageBox('不能选择多个对方单位!', '提示', 0);
              Exit;
            end;
            self.YFSub.Append;
            self.YFSub.FieldByName('YFPZNO').Value := CDS_Main.FieldByName('SPID').Value;
            self.YFSub.FieldByName('CPCode').Value := CDS_Main.FieldByName('SPCode').Value;
            self.YFSub.FieldByName('CPName').Value := CDS_Main.FieldByName('SPName').Value;
            self.YFSub.FieldByName('CPSpec').Value := CDS_Main.FieldByName('SPSpec').Value;
            if Trim(CDS_Main.fieldbyname('CRType').AsString) = '销售退货' then
            begin
              CQtyFlag := -1;
              CKXType := '机物料退货';
            end
            else if Trim(CDS_Main.fieldbyname('CRType').AsString) = '销售出库' then
            begin
              CQtyFlag := 1;
              CKXType := '机物料采购';
            end;
//            self.YFGSType.ItemIndex := self.YFGSType.Items.IndexOf('机物料类');
            self.YFSub.FieldByName('KXType').Value := CKXType;
            self.YFSub.FieldByName('PS').Value := CQtyFlag * CDS_Main.FieldByName('PiQty').AsInteger;
            self.YFSub.FieldByName('Qty').Value := CQtyFlag * CDS_Main.FieldByName('Qty').AsFloat;
            self.YFSub.FieldByName('QtyUnit').Value := CDS_Main.FieldByName('QtyUnit').Value;
            self.YFSub.FieldByName('Price').Value := CDS_Main.FieldByName('Price').AsFloat;
            self.YFSub.FieldByName('MxMoney').Value := CQtyFlag * CDS_Main.FieldByName('Qty').AsFloat * CDS_Main.FieldByName('Price').AsFloat;
            self.YFSub.post;
            CDS_Main.Delete;
          end;
          self.FactoryName.Text := Trim(CFactoryName);
          HJJE();
        end;
      end;
    end;
  finally
    frmZZJWLCKListSel.free;
  end;
end;

procedure TfrmYSKInput.ToolButton5Click(Sender: TObject);
var
  CFactoryName, CKXType: string;
  CQtyFlag: integer;
begin
  try
    frmPBCKlistSel := TfrmPBCKlistSel.create(self);
    with frmPBCKlistSel do
    begin
      with frmPBCKlistSel do
      begin
        if showmodal = 1 then
        begin
          CFactoryName := Trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
          CRTime.DateTime := CDS_Main.fieldbyname('CRTime').AsDateTime;
          FtySYRName.Text := CFactoryName;
//         ishs.ItemIndex:=ishs.Items.IndexOf(trim(CDS_Main.fieldbyname('ishs').AsString));
          while CDS_Main.Locate('SSel', true, []) do
          begin
            if trim(CFactoryName) <> Trim(CDS_Main.fieldbyname('ToFactoryName').AsString) then
            begin
              Application.MessageBox('不能选择多个对方单位!', '提示', 0);
              Exit;
            end;
            if Trim(CDS_Main.fieldbyname('CRType').AsString) = '销售退回' then
            begin
              CQtyFlag := -1;
              CKXType := '汇总坯布销售退回';
            end
            else
            begin
              CQtyFlag := 1;
              CKXType := '汇总坯布销售';
            end;
            self.YFSub.Append;
//            self.YFGSType.ItemIndex := self.YFGSType.Items.IndexOf('坯布类');
            self.YFSub.FieldByName('YFPZNO').Value := CDS_Main.FieldByName('CRID').Value;
            self.YFSub.FieldByName('KXType').Value := CKXType;
            self.YFSub.FieldByName('CPCode').Value := CDS_Main.FieldByName('Code').Value;
            self.YFSub.FieldByName('CPName').Value := CDS_Main.FieldByName('CodeName').Value;
//            self.YFSub.FieldByName('CPSpec').Value:=CDS_Main.FieldByName('SPSpec').Value;
            self.YFSub.FieldByName('PS').Value := CQtyFlag * CDS_Main.FieldByName('PS').AsInteger;
            self.YFSub.FieldByName('Qty').Value := CQtyFlag * CDS_Main.FieldByName('Qty').AsFloat;
            self.YFSub.FieldByName('QtyUnit').Value := CDS_Main.FieldByName('QtyUnit').Value;
            self.YFSub.FieldByName('Price').Value := CDS_Main.FieldByName('Price').Value;
            self.YFSub.FieldByName('MxMoney').Value := CQtyFlag * CDS_Main.FieldByName('money').AsFloat;
            self.YFSub.post;

            CDS_Main.Delete;
          end;
          self.FactoryName.Text := Trim(CFactoryName);
          HJJE();
        end;
      end;
    end;
  finally
    frmPBCKlistSel.free;
  end;
end;

procedure TfrmYSKInput.ranchangBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ranchang';
      flagname := '染厂';
      if ShowModal = 1 then
      begin
        Self.ranchang.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

