unit U_SampleLR_SO;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit, BtnEdit,
  cxContainer, cxCurrencyEdit, Menus, cxLookAndFeelPainters, cxButtons, cxImage,
  cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,
  ShellAPI;

type
  TfrmSampleLR_So = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    factoryName: TBtnEditA;
    DeliveryTime: TDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    defstr1: TEdit;
    Label7: TLabel;
    Label9: TLabel;
    defstr3: TEdit;
    Label10: TLabel;
    request: TMemo;
    Qty: TcxCurrencyEdit;
    P_CodeName: TBtnEditA;
    P_Code: TBtnEditA;
    note: TMemo;
    Label11: TLabel;
    defstr5: TEdit;
    Label13: TLabel;
    custName: TBtnEditA;
    Label14: TLabel;
    defflt1: TcxCurrencyEdit;
    Label8: TLabel;
    defflt2: TcxCurrencyEdit;
    Label12: TLabel;
    defflt3: TcxCurrencyEdit;
    Label15: TLabel;
    defflt4: TcxCurrencyEdit;
    Label16: TLabel;
    Label17: TLabel;
    defflt5: TcxCurrencyEdit;
    Label18: TLabel;
    defstr2: TEdit;
    defstr4: TEdit;
    Label19: TLabel;
    Label20: TLabel;
    KHconNo: TEdit;
    kuanNo: TEdit;
    Label21: TLabel;
    Picture4: TcxDBImage;
    FileName: TcxButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    Panel2: TPanel;
    Label22: TLabel;
    defflt6: TcxCurrencyEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure P_CodeNameBtnClick(Sender: TObject);
    procedure P_CodeBtnClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure FileNameClick(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitGrid();
    function savedate(): Boolean;
    procedure InitImage();
    procedure LookImage(FileName: string);
    function YFData_other(moneyFee: string; money: double): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FCRFlag: string;
    canshu1: string;

    { Public declarations }
  end;

var
  frmSampleLR_So: TfrmSampleLR_So;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachGYS, U_CPManageSel, U_ZdyAttachment,
  getpic;
{$R *.dfm}

function TfrmSampleLR_So.YFData_other(moneyFee: string; money: double): Boolean;
var
  CRID, YFID: string;
begin
  CRID := '0';
  Result := False;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR where FactoryName=''' + Trim(factoryName.Text) + '''');
    sql.Add(' and mainID=''' + Trim(fkeyNo) + '''');
    sql.Add(' and YFName=''' + trim(moneyFee) + ''' ');
    Open;
  end;
  if ADOQueryTmp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'YF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应付最大号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(YFID);
      FieldByName('YFTypeId').Value := Trim(fkeyNo);
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应付款登记';
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryName').Value := Trim(factoryName.Text);
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CRTime.Date));
      FieldByName('YFType').Value := '自动生成';
      FieldByName('Qty').Value := money;
      FieldByName('Price').Value := 0;
      FieldByName('money').Value := 0;
      FieldByName('BBmoney').Value := 0;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := 'RMB';
      FieldByName('QtyUnit').Value := '套';
      FieldByName('ComTaiTou').Value := Trim(factoryName.Text);
      FieldByName('YFName').Value := trim(moneyFee);
      FieldByName('MainId').Value := Trim(fkeyNo);
       //   FieldByName('subID').Value:=Trim(Ado.fieldbyname('subID').AsString);
      //    FieldByName('OrderNO').Value:=Trim(Ado.fieldbyname('conNo').AsString);
      FieldByName('P_CodeName').Value := Trim(P_CodeName.Text);
      //    FieldByName('P_Spec').Value:=Trim(P_CodeName.Text);
      FieldByName('P_color').Value := Trim(defstr5.Text);
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select (case when YF_Money_CR.YFName=''SO样费''then  isnull(A.defflt6,0) else isnull(A.defflt4,0) end) from JY_Sample A where A.JSNO=YF_Money_CR.MainID)  ');
    sql.Add(' where YFID=''' + Trim(YFID) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from YF_Money_CR ');
    sql.Add('where YFID=''' + Trim(YFID) + ''' and Qty=0 and YFName=''' + trim(moneyFee) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
    sql.Add(' where YFID=''' + Trim(YFID) + '''');
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmSampleLR_So.LookImage(FileName: string);
var
  sFieldName: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

    Panel2.Caption := '正在下载数据，请稍等...';
    Panel2.Visible := true;
    application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Panel2.Visible := false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Panel2.Visible := false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  Panel2.Visible := false;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmSampleLR_So.InitImage();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fkeyNO)));
    sql.Add('and TFType=' + quotedstr(trim('SO')));
    open;
  end;
end;

function TfrmSampleLR_So.savedate(): Boolean;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if trim(fkeyNo) = '' then
    begin
      if GetLSNo(ADOQueryTmp, fkeyNo, '', 'JY_Sample', 3, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取子流水号失败！', '提示', 0);
        Exit;
      end;
      fkeyNo := trim(DCode) + fkeyNo;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JY_Sample where JSNO=''' + trim(fkeyNo) + ''' and CRFlag=''' + trim(fCRFlag) + '''');
      open;
    end;
    if ADOQueryCmd.IsEmpty then
    begin
      ADOQueryCmd.Append;
      ADOQueryCmd.FieldByName('Filler').Value := DName;
    end
    else
    begin
      ADOQueryCmd.Edit;
      ADOQueryCmd.FieldByName('editer').Value := DName;
      ADOQueryCmd.FieldByName('editTime').Value := now();
    end;
    SSetsaveSqlNew(ADOQueryCmd, 'JY_Sample', Panel1, 0);

    ADOQueryCmd.fieldbyname('JSNO').Value := fkeyNo;
    ADOQueryCmd.fieldbyname('JSID').Value := 1;
    ADOQueryCmd.fieldbyname('CRFlag').Value := fCRFlag;
    ADOQueryCmd.fieldbyname('Qty').Value := strtofloatdef(trim(Qty.Text), 0);
    ADOQueryCmd.fieldbyname('defflt1').Value := strtofloatdef(trim(defflt1.Text), 0);
    ADOQueryCmd.fieldbyname('defflt2').Value := strtofloatdef(trim(defflt2.Text), 0);
    ADOQueryCmd.fieldbyname('defflt3').Value := strtofloatdef(trim(defflt3.Text), 0);
    ADOQueryCmd.fieldbyname('defflt4').Value := strtofloatdef(trim(defflt4.Text), 0);
    ADOQueryCmd.Post;

    if trim(factoryName.Text) <> '' then
    begin
      if not YFdata_other('SO样费', strtofloatdef(defflt6.Text, 0)) then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        exit;
      end;
      if not YFdata_other('制网费', strtofloatdef(defflt4.Text, 0)) then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        exit;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmSampleLR_So.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from JY_Sample A');
      sql.Add('where JSNO=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    if not adoqueryTmp.IsEmpty then
    begin
      SCSHDataNew(adoqueryTmp, Panel1, 0);
    end;
  finally
  end;
end;

procedure TfrmSampleLR_So.FormDestroy(Sender: TObject);
begin
  frmSampleLR_SO := nil;
end;

procedure TfrmSampleLR_So.FormCreate(Sender: TObject);
begin
  CRTime.Date := DServerDate;
  DeliveryTime.Date := CRTime.Date;
end;

procedure TfrmSampleLR_So.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmSampleLR_So.FormShow(Sender: TObject);
begin
  if trim(canshu1) = '高权限' then
  begin
    defstr2.Enabled := true;
    defstr4.Enabled := true;
  end;
  fFlileFlag := UserDataFlag + 'SO';
  InitGrid();
  InitImage();
end;

procedure TfrmSampleLR_So.TBSaveClick(Sender: TObject);
begin
  CRTime.SetFocus;
  if trim(P_Code.Text) = '' then
  begin
    application.MessageBox('样品编号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if trim(P_CodeName.Text) = '' then
  begin
    application.MessageBox('产品名称不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
  //  ModalResult:=1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmSampleLR_So.ToolButton1Click(Sender: TObject);
begin
 { frmCPManageSel:=TfrmCPManageSel.create(self);
  with frmCPManageSel do
  begin
    if showmodal=1 then
    begin
      self.cds_main.DisableControls;
      CDS_Main.DisableControls;
      CDS_Main.First;
      while not CDS_Main.Eof  do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean) and
          (not self.cds_main.Locate('P_Code',trim(cds_main.fieldbyname('CYNo').AsString),[])) then
        begin
        self.cds_main.Append;
      //  self.cds_main.FieldByName('kcid').Value:=trim(cds_main.fieldbyname('kcid').AsString);
      //  self.cds_main.FieldByName('mainID').Value:=trim(cds_main.fieldbyname('mainID').AsString);
      //  self.cds_main.FieldByName('subID').Value:=trim(cds_main.fieldbyname('subID').AsString);
     //   self.cds_main.FieldByName('MJID').Value:=trim(cds_main.fieldbyname('MJID').AsString);
     //   self.cds_main.FieldByName('MJXH').Value:=trim(cds_main.fieldbyname('MJXH').AsString);
    //    self.cds_main.FieldByName('CPTYPE').Value:=trim(cds_main.fieldbyname('MJType').AsString);
        self.cds_main.FieldByName('P_Code').Value:=trim(cds_main.fieldbyname('CYNo').AsString);
        self.cds_main.FieldByName('P_CodeName').Value:=trim(cds_main.fieldbyname('CYName').AsString);
        self.cds_main.FieldByName('defnote5').Value:=trim(cds_main.fieldbyname('defnote5').AsString);
        self.cds_main.FieldByName('P_spec').Value:=trim(cds_main.fieldbyname('CYSpec').AsString);
        self.cds_main.FieldByName('P_cf').Value:=trim(cds_main.fieldbyname('CYECF').AsString);
        self.cds_main.FieldByName('CYMF').Value:=trim(cds_main.fieldbyname('CYMF').AsString);
        self.cds_main.FieldByName('CYKZ').Value:=trim(cds_main.fieldbyname('CYKZ').AsString);
        self.cds_main.FieldByName('factoryName').Value:=trim(cds_main.fieldbyname('DefStr7').AsString);
        self.cds_main.FieldByName('Kuwei').Value:=trim(cds_main.fieldbyname('DefStr2').AsString);
    //    self.cds_main.FieldByName('P_Color').Value:=trim(cds_main.fieldbyname('P_Color').AsString);
    //    self.cds_main.FieldByName('P_MF').Value:=cds_main.fieldbyname('P_MF').AsFloat;
    //    self.cds_main.FieldByName('P_KZ').Value:=cds_main.fieldbyname('P_KZ').AsFloat;
    //    self.cds_main.FieldByName('RollNum').Value:=;
    //    self.cds_main.FieldByName('Qty').Value:=cds_main.fieldbyname('MJLen').AsFloat;
    //    self.cds_main.FieldByName('QtyUnit').Value:=trim(cds_main.fieldbyname('MJTypeother').AsString);
        self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
      self.cds_main.EnableControls;
    end;
    free;
  end;
//  cds_main.Append;
//  cds_Main.fieldbyname('qtyUnit').Value:='M';
//  cds_main.post;   }
end;

procedure TfrmSampleLR_So.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmSampleLR_So.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPCode';
      flagname := '样品编号';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSampleLR_So.factoryNameBtnClick(Sender: TObject);
begin
  frmZdyAttachGYS := TfrmZdyAttachGYS.create(self);
  with frmZdyAttachGYS do
  begin
    DEFstr5.ItemIndex := DEFstr5.Items.IndexOf('加工厂');
    DEFstr5.Enabled := false;
    if showmodal = 1 then
    begin
      factoryName.Text := trim(CDS_HZ.fieldbyname('zdyName').AsString)
    end;
    free;
  end;
end;

procedure TfrmSampleLR_So.P_CodeNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        P_CodeName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSampleLR_So.P_CodeBtnClick(Sender: TObject);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      P_Code.Text := CDS_Main.fieldbyname('CYNo').asstring;
      P_CodeName.Text := CDS_Main.fieldbyname('CYName').asstring;
    end;
    free;
  end;
end;

procedure TfrmSampleLR_So.custNameBtnClick(Sender: TObject);
begin
  frmZdyAttachment := TfrmZdyAttachment.create(self);
  with frmZdyAttachment do
  begin
    if trim(canshu1) <> '高权限' then
      canshu1 := '业务员';
    if showmodal = 1 then
    begin
      custName.Text := trim(CDS_HZ.fieldbyname('CoName').AsString)
    end;
    free;
  end;
end;

procedure TfrmSampleLR_So.FileNameClick(Sender: TObject);
begin
  if trim(fkeyNO) = '' then
  begin
    application.MessageBox('SO样信息未保存，请先保存SO样信息！', '提示信息', MB_ICONERROR);
    exit;
  end;

  try
    FormGetPic := TFormGetPic.Create(Application);
    with FormGetPic do
    begin
      fkeyNO := trim(self.fkeyNO);
      pat1 := TCXbutton(Sender).Name;
      pic1 := TCXbutton(Sender).Hint;
      FTFType := 'SO';
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
     // self.Timage(Tbutton(Sender).Hint).Picture.Assign(FormGetPic.Image2.Picture.Bitmap);
     //   zdycode.Enabled:=false;
      end;

      Release;
    end;
    InitImage();
  except
    if FormGetPic <> nil then
      FormGetPic.Release;
  end;
end;

procedure TfrmSampleLR_So.Picture4DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);

end;

end.

