unit U_LLRKInPutMH_PBCG;

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, jpeg, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, cxPC, cxGridCustomPopupMenu, cxGridPopupMenu, cxGroupBox,
  cxCheckBox, RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,
  IniFiles, Menus, ImgList, ShellAPI, cxGridBandedTableView,
  cxGridDBBandedTableView, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
  dxSkinDarkRoom, dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type

  TfrmLLRKInPutMH_PBCG = class(TForm)
    ToolBar1: TToolBar;
    li: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label21: TLabel;
    Label26: TLabel;
    Label19: TLabel;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    Panel3: TPanel;
    labMYType: TLabel;
    Panel4: TPanel;
    Label13: TLabel;
    CRTime: TDateTimePicker;
    Panel5: TPanel;
    Label31: TLabel;
    Label32: TLabel;
    PS: TEdit;
    Qty: TEdit;
    KgQty: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    MXHZPS: TEdit;
    MXHZQty: TEdit;
    MXHZKgQty: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    PSChaE: TEdit;
    QtyChaE: TEdit;
    KgQtyChaE: TEdit;
    Panel7: TPanel;
    Panel1: TPanel;
    Label20: TLabel;
    PTID: TEdit;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    adoqueryPicture: TADOQuery;
    Label23: TLabel;
    MYPrice: TEdit;
    Label24: TLabel;
    MYMDMoney: TEdit;
    MYHDMoney: TEdit;
    Label28: TLabel;
    Label29: TLabel;
    PopupMenu1: TPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    ImageList1: TImageList;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    cxGroupBox2: TcxGroupBox;
    ListView1: TListView;
    N7: TMenuItem;
    ADOQuery2: TADOQuery;
    Panel12: TPanel;
    Label8: TLabel;
    Button2: TButton;
    Panel13: TPanel;
    Image2: TImage;
    WJName: TEdit;
    Button7: TButton;
    WJPach: TEdit;
    Button8: TButton;
    Label44: TLabel;
    Label45: TLabel;
    Label49: TLabel;
    Label50: TLabel;
    Panel15: TPanel;
    Panel2: TPanel;
    Label6: TLabel;
    MoneyChaE: TEdit;
    Label1: TLabel;
    Note: TEdit;
    ADOQueryTemp: TADOQuery;
    Label7: TLabel;
    RKNo: TEdit;
    Label53: TLabel;
    CGName: TComboBox;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label34: TLabel;
    JHOrdNo: TEdit;
    Label33: TLabel;
    JCDanWei: TBtnEditA;
    JCNo: TEdit;
    Label27: TLabel;
    KHName: TBtnEditA;
    KHNo: TEdit;
    Panel17: TPanel;
    Panel18: TPanel;
    Label39: TLabel;
    Label25: TLabel;
    PBBatchNo: TEdit;
    Label30: TLabel;
    YGSunHao: TEdit;
    Label35: TLabel;
    Panel6: TPanel;
    Label22: TLabel;
    MYCode: TEdit;
    MYName: TBtnEditA;
    Label12: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label11: TLabel;
    Label36: TLabel;
    MYKZ: TEdit;
    MYMF: TEdit;
    MYCF: TEdit;
    MYJWS: TEdit;
    MYMD: TEdit;
    GCCodeName: TEdit;
    Panel14: TPanel;
    Label51: TLabel;
    Label43: TLabel;
    MYMDPrice: TEdit;
    MDMFlag: TCheckBox;
    MDKGFlag: TCheckBox;
    HSFlag: TCheckBox;
    BHSFlag: TCheckBox;
    procedure liClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure KHNameBtnClick(Sender: TObject);
    procedure Image1DblClick(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure BtnDelClick(Sender: TObject);
    procedure BtnQXClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure QtyChange(Sender: TObject);
    procedure MDMFlagClick(Sender: TObject);
    procedure MDKGFlagClick(Sender: TObject);
    procedure MYNameBtnClick(Sender: TObject);
    procedure MXHZQtyChange(Sender: TObject);
    procedure Panel13Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure JCDanWeiBtnClick(Sender: TObject);
    procedure MXHZKgQtyChange(Sender: TObject);
    procedure MYMDPriceChange(Sender: TObject);
    procedure HSFlagClick(Sender: TObject);
    procedure BHSFlagClick(Sender: TObject);
    procedure PSChaEChange(Sender: TObject);
    procedure QtyChaEChange(Sender: TObject);
    procedure KgQtyChaEChange(Sender: TObject);
    procedure MoneyChaEChange(Sender: TObject);
    procedure PSChange(Sender: TObject);
  private
    lstPat: TStringList;
    FangXiang1, FSaveFlag: string;
    procedure InitData();
    procedure SaveImage(maxnoWB: string);
    procedure ReadINIFile10();
    function SaveData(): Boolean;
    procedure InitColor();
    procedure GetHZData();
    function SaveDataMX(): Boolean;
    procedure InitMXData();
    procedure Initimage();
    function YFData(): Boolean;
    procedure HSPS();
    { Private declarations }
  public
    canshu1: string;
    PState, CopyInt: Integer;
    FMainId: string;
    FRead: string;
    FMYType, FMYTypeFlag, FML, SaveFlag: string;
    { Public declarations }
  end;

var
  frmLLRKInPutMH_PBCG: TfrmLLRKInPutMH_PBCG;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_ZDYHelpSel, U_GYSList, U_SCPerson,
  U_QCRKJLList, U_RSColorBig, U_RTPTColor, U_KHListSelJJ, U_ZHCPBigTP,
  U_LLRKMXInPutMH, U_ZDYCPName, U_BPZdy_LRPB, U_Fun, U_BPZdy_LRPM,
  U_LLRKMXInPutMH_PBMX;

{$R *.dfm}
function TfrmLLRKInPutMH_PBCG.YFData(): Boolean;
var
  CRID, OrdMainId, YFID, FComTaiTou, FCRID, FFactoryName, FFactoryNo: string;
begin
  Result := False;
  FFactoryName := Trim(KHName.Text);
  FFactoryNo := Trim(KHNo.Text);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YS_Money_KC where FactoryName=' + quotedstr(Trim(FFactoryName)));
    sql.Add(' and KCType=''KH'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YS_Money_CRID select 0 ');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end;

    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(FFactoryName);
      FieldByName('FactoryNo').Value := Trim(FFactoryNo);
      FieldByName('ZdyStr1').Value := '应付付';
      FieldByName('KCType').Value := 'KH';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where FactoryNo=' + quotedstr(Trim(FFactoryNo)));
    sql.Add(' and YFTypeId=''' + Trim(FMainId) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'PB', 'YS_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应付最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID=''' + Trim(YFID) + '''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      Append;
      FieldByName('SCQty').Value := strtofloatdef(Qty.text, 0);
      FieldByName('SCPS').Value := strtofloatdef(PS.text, 0);
    end
    else
      Edit;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(FMainid);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('YFName').Value := '坯布采购';
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应付款登记';
    FieldByName('CRFlag').Value := '应付付';
    FieldByName('ChkStatusP').Value := '未审核';
    FieldByName('ChkStatusQ').Value := '未审核';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(FFactoryName);
    FieldByName('FactoryNo').Value := Trim(FFactoryNo);
    FieldByName('CRTime').Value := CRTime.datetime;
    if MDMFlag.Checked = True then
    begin
      FieldByName('Qty').Value := Strtofloatdef(Qty.text, 0);
      FieldByName('QtyUnit').Value := 'M';
    end
    else
    begin
      FieldByName('Qty').Value := Strtofloatdef(KGQty.text, 0);
      FieldByName('QtyUnit').Value := 'KG';
    end;
    FieldByName('PS').Value := Strtofloatdef(PS.text, 0);
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := Strtofloatdef(MYMDPrice.text, 0);
    if HSFlag.Checked = true then
    begin
      FieldByName('HanShui').Value := '含税';
    end
    else
    begin
      FieldByName('HanShui').Value := '不含税';
    end;
    FieldByName('CKNO').Value := trim(FMainid);
    FieldByName('SJQty').Value := Strtofloatdef(Qty.text, 0);
    FieldByName('MiQty').Value := Strtofloatdef(Qty.text, 0);
    FieldByName('KGQty').Value := Strtofloatdef(KGQty.text, 0);
    FieldByName('money').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('JSmoney').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('HSmoney').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('P_CodeName').Value := Trim(MYName.text);
    FieldByName('P_MF').Value := Trim(MYMF.text);
    FieldByName('P_KZ').Value := Trim(MYKZ.Text);
    FieldByName('P_Code').Value := Trim(MYCode.Text);
    FieldByName('P_Spec').Value := Trim(MYCF.Text);
    FieldByName('BZType').Value := '￥';
    FieldByName('status').Value := '0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_KC Set KCMoney=(select isnull(Sum(HSMoney*QtyFlag),0) from YS_Money_CR A where A.CRID=YS_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmLLRKInPutMH_PBCG.liClick(Sender: TObject);
begin
  if Trim(SaveFlag) = '99' then
  begin
    if Application.MessageBox('没有保存数据，确定要关闭吗？', '提示', 32 + 4) <> IDYES then
      exit;
  end;
  Close;
end;

procedure TfrmLLRKInPutMH_PBCG.Initimage();
var
  ListItem: TListItem;
  Flag: Cardinal;
  info: SHFILEINFOA;
  Icon: TIcon;
begin
  ListView1.Items.Clear;
  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select fileName from TP_File ');
    sql.Add('where WBID=''' + trim(FMainId) + ''' ');
    sql.Add('and TFType=''采购坯布''');
    //ShowMessage(sql.Text);
    open;
    if not IsEmpty then
    begin
      while not eof do
      begin
        with ListView1 do
        begin
          LargeImages := ImageList1;
          Icon := TIcon.Create;
          ListItem := Items.Add;
          ListItem.Caption := trim(fieldbyname('fileName').AsString);
          Flag := (SHGFI_LARGEICON or SHGFI_ICON or SHGFI_USEFILEATTRIBUTES);
          SHGetFileInfo(Pchar(trim(fieldbyname('fileName').AsString)), 0, info, Sizeof(info), Flag);
          Icon.Handle := info.hIcon;
          ImageList1.AddIcon(Icon);
          ListItem.ImageIndex := ImageList1.Count - 1;
        end;
        next;
      end;
    end;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * ');
    sql.Add(' from CK_MYSC_CR  ');
    sql.Add(' where MYID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, Panel4, 2);
  SCSHDataNew(ADOQuery1, Panel4, 0);
  SCSHDataNew(ADOQuery1, Panel5, 2);
  SCSHDataNew(ADOQuery1, Panel5, 0);
  SCSHDataNew(ADOQuery1, Panel6, 2);
  SCSHDataNew(ADOQuery1, Panel15, 2);
  MDMFlag.Checked := ADOQuery1.FieldByName('MDMflag').AsBoolean;
  MDKGFlag.Checked := ADOQuery1.FieldByName('MDKGFlag').AsBoolean;
  HSFlag.Checked := ADOQuery1.FieldByName('HSFlag').AsBoolean;
  BHSFlag.Checked := ADOQuery1.FieldByName('BHSFlag').AsBoolean;
  KHName.TxtCode := Trim(ADOQuery1.fieldbyname('KHCode').AsString);
  labMYType.Caption := Trim(ADOQuery1.fieldbyname('MYType').AsString);
  MYName.text := Trim(ADOQuery1.fieldbyname('MYName').AsString);
  MYCode.text := Trim(ADOQuery1.fieldbyname('MYCode').AsString);
  CGName.text := Trim(ADOQuery1.fieldbyname('CGName').AsString);
  MYMDPrice.text := Trim(floattostr(ADOQuery1.fieldbyname('MYMDPrice').asfloat));
  if PState = 0 then
  begin
    CRTime.Date := SGetServerDate(ADOTemp);
    labMYType.Caption := FMYType;
  end;
  if TBSave.Visible = False then
  begin
    Panel2.Visible := False;
    initBtnColor(Panel4, clMenu);
    initBtnColor(Panel5, clMenu);
  end;
  InitMXData();
  GetHZData();
  if Trim(FMainId) = '' then
    Exit;
  initImage();
end;

procedure TfrmLLRKInPutMH_PBCG.InitMXData();
var
  i: integer;
begin
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,RCGangNo1=(case when isnull(RCGangNo,'''')='''' then GangNo else RCGangNo end)');
    sql.Add(',Laster=isnull(Editer,Filler) from CK_MYSC_CR_MX A where isnull(MYID,'''')=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQuery2, ClientDataSet1);
  SInitCDSData20(ADOQuery2, ClientDataSet1);
end;

procedure TfrmLLRKInPutMH_PBCG.FormShow(Sender: TObject);
begin
  SaveFlag := '99';
  InitData();
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.add(' select UserName from SY_User where Udept like ''%采购部%'' ');
    open;
  end;
  with ADOQueryTemp do
  begin
    first;
    CGName.Items.Add('');
    while not EOF do
    begin
      CGName.Items.Add(trim(fieldByName('UserName').AsString));
      next;
    end;
  end;
end;

function TfrmLLRKInPutMH_PBCG.SaveData(): Boolean;
var
  maxno, maxmxno, maxnoflag, fsj, FMXID: string;
begin
  Result := False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'LL', 'CK_MYSC_CR', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CK_MYSC_CR where MYId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if isempty then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
      end
      else
      begin
        Edit;
      end;
      FieldByName('MYId').Value := Trim(maxno);
      FieldByName('CRType').Value := '采购入库';
      FieldByName('CRFlag').Value := '入库';
      FieldByName('CRQtyFlag').Value := 1;
      if Trim(FMainId) = '' then
      begin

      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditerCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel4, 2);
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel5, 2);
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel15, 2);
      FieldByName('MDMflag').Value := MDMFlag.Checked;
      FieldByName('MDKGflag').Value := MDKGflag.Checked;
      FieldByName('HSFlag').AsBoolean := HSFlag.Checked;
      FieldByName('BHSFlag').AsBoolean := BHSFlag.Checked;
      FieldByName('MYType').Value := Trim(FMYType);
      FieldByName('MYTypeFlag').Value := Trim(FMYTypeFlag);
      fieldbyname('KHCode').AsString := Trim(KHName.TxtCode);
      fieldbyname('MYName').AsString := Trim(MYName.Text);
      fieldbyname('MYCode').AsString := Trim(MYCode.Text);
      fieldbyname('MYMDPrice').Value := StrToFloatdef(MYMDPrice.Text, 0);
      fieldbyname('CGName').AsString := Trim(CGName.Text);

      if RKNo.Text = '' then
      begin
        FieldByName('RKNo').Value := Trim(maxno);
      end;
      Post;
    end;
    FMainId := Trim(maxno);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_MYSC_CR where PBBatchNo=''' + Trim(PBBatchNo.Text) + '''');
      sql.Add(' and MYType=''' + Trim(FMYType) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
    begin
      Result := False;
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('坯布联系号已存在!', '提示', 0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate CK_MYSC_CR Set KHDayCS=isnull((select isnull(max(KHDayCS),0) from CK_MYSC_CR A ');
      sql.Add(' where A.CRTime=CK_MYSC_CR.CRTime and A.MYType=CK_MYSC_CR.MYType and isnull(A.KHName,'''')=isnull(CK_MYSC_CR.KHName,'''')),0)+1');
      SQL.Add('where MYID=''' + Trim(FMainId) + ''' and isnull(KHName,'''')<>'''' ');
      sql.Add(' and isnull(KHDayCS,0)=0');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_LLRK_PBQty :MYID');
      Parameters.ParamByName('MYID').Value := Trim(maxno);
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
      Parameters.ParamByName('PBBatchNo').Value := Trim(PBBatchNo.Text);
      Parameters.ParamByName('JGCNO').Value := Trim(JCNo.Text);
      ExecSQL;
    end;

    YFData();
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ClientDataSet1.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  FInt: Integer;
begin
  ToolBar1.SetFocus;
  if Trim(KHName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;
  if (Trim(MYName.Text) = '') and (Trim(GCCodeName.Text) = '') then
  begin
    Application.MessageBox('品名和工厂品名不能都为空!', '提示', 0);
    Exit;
  end;
  if Trim(MYCode.Text) = '' then
  begin
    Application.MessageBox('产品编号不能为空!', '提示', 0);
    Exit;
  end;
//  if Trim(PBBatchNo.Text) = '' then
//  begin
//    Application.MessageBox('坯布联系号不能为空!', '提示', 0);
//    Exit;
//  end;
  {if Trim(RKNo.Text)='' then
  begin
    Application.MessageBox('入库单号不能为空!','提示',0);
    Exit;
  end;}
  if Trim(MYMF.Text) = '' then
  begin
    Application.MessageBox('门幅不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(CGName.Text) = '' then
  begin
    Application.MessageBox('采购人不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(MYKZ.Text) = '' then
  begin
    Application.MessageBox('克重不能为空!', '提示', 0);
    Exit;
  end;
  if (MDMFlag.Checked = False) and (MDKGFlag.Checked = False) then
  begin
    Application.MessageBox('请选择计价方式', '提示', 0);
    Exit;
  end;
  if (HSFlag.Checked = False) and (BHSFlag.Checked = False) then
  begin
    Application.MessageBox('请选择含税或不含税', '提示', 0);
    Exit;
  end;
  if Trim(PS.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToInt(PS.Text, FInt) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    exit;
  end;
  if (Trim(Qty.Text) = '') and (Trim(KgQty.text) = '') then
  begin
    Application.MessageBox('米数和公斤数不能同时为空!', '提示', 0);
    exit;
  end;
  if Trim(Qty.Text) <> '' then
  begin
    if TryStrToFloat(Qty.Text, FReal) = False then
    begin
      Application.MessageBox('米数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(KgQty.Text) <> '' then
  begin
    if TryStrToFloat(KgQty.Text, FReal) = False then
    begin
      Application.MessageBox('公斤数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(YGSunHao.Text) = '' then
  begin
    Application.MessageBox('预估损耗不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(YGSunHao.Text, FReal) = False then
  begin
    Application.MessageBox('预估损耗非法数字!', '提示', 0);
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    SaveFlag := '';
    ModalResult := 1;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.ReadINIFile10();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmLLRKInPutMH_PBCG.SaveImage(maxnoWB: string);
begin

end;

procedure TfrmLLRKInPutMH_PBCG.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ModalResult := 1;
end;

procedure TfrmLLRKInPutMH_PBCG.InitColor();
var
  fsj: string;
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from RT_PTColor where PTID=''' + Trim(PTID.Text) + '''');
    Open;
  end;
  fsj := Trim(ADOTemp.fieldbyname('ColDaiMa').AsString);
  if Trim(fsj) <> '' then
  begin
    //Panel6.Color:=StrToInt('$'+Trim(fsj));
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
end;

procedure TfrmLLRKInPutMH_PBCG.KHNameBtnClick(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '坯布厂';
      KHType.TxtCode := '坯布厂';
      if ShowModal = 1 then
      begin
        Self.KHName.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
        Self.KHNo.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Image1DblClick(Sender: TObject);
begin
  if Trim(FMainId) = '' then
  begin
    Application.MessageBox('没有保存数据，不能查看大图!', '提示', 0);
    Exit;
  end;
  try
    frmZHCPBigTP := TfrmZHCPBigTP.Create(Application);
    with frmZHCPBigTP do
    begin
      frmZHCPBigTP.FMainId := Self.FMainId;
      frmZHCPBigTP.FIMNO := '';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZHCPBigTP.Free;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Panel1Click(Sender: TObject);
begin
  if Trim(FMainId) = '' then
  begin
    Application.MessageBox('未保存数据！', '', 0);
    Exit;
  end;
  try
    frmLLRKMXInPutMH_PBMX := TfrmLLRKMXInPutMH_PBMX.Create(Application);
    with frmLLRKMXInPutMH_PBMX do
    begin
      PState := 1;
      frmLLRKMXInPutMH_PBMX.FMainId := Trim(Self.FMainId);
      if Self.TBSave.Visible = False then
      begin
        frmLLRKMXInPutMH_PBMX.TBSave.Visible := False;
      end;
      if MDMFlag.Checked = True then
      begin
        frmLLRKMXInPutMH_PBMX.UnitM.Text := '√';
      end
      else
      begin
        frmLLRKMXInPutMH_PBMX.UnitM.Text := '';
      end;
      if MDKGFlag.Checked = True then
      begin
        frmLLRKMXInPutMH_PBMX.UnitKG.Text := '√';
      end
      else
      begin
        frmLLRKMXInPutMH_PBMX.UnitKG.Text := '';
      end;
      if ShowModal = 1 then
      begin
        InitData();
        Self.MXHZPS.Text :=  frmLLRKMXInPutMH_PBMX.MXHZPS.Text;
        Self.MXHZKGQty.Text :=  frmLLRKMXInPutMH_PBMX.MXHZKGQty.Text;
        Self.MXHZQty.Text :=  frmLLRKMXInPutMH_PBMX.MXHZQty.Text;
      end;
    end;
  finally
    frmLLRKMXInPutMH_PBMX.Free;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.GetHZData();
var
  FKG, FM, FPS: double;
begin
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Avg_JS :KgQty,:MQty,:PS');
    if Trim(MXHZKGQty.Text) <> '0' then
    begin
      Parameters.ParamByName('KGQty').Value := FKG;
    end
    else
    begin
      Parameters.ParamByName('KGQty').Value := Null;
    end;

    if Trim(MXHZQty.Text) <> '0' then
    begin
      Parameters.ParamByName('MQty').Value := FM;
    end
    else
    begin
      Parameters.ParamByName('MQty').Value := Null;
    end;
    Parameters.ParamByName('PS').Value := NULL;
    Open;
  end;
  QtyChaE.Text := FloatToStr(strtofloatdef(MXHZQty.Text, 0) - strtofloatdef(Qty.Text, 0));
  PSChaE.Text := FloatToStr(strtofloatdef(MXHZPS.Text, 0) - strtofloatdef(PS.Text, 0));
  KgQtyChaE.Text := FloatToStr(strtofloatdef(MXHZKgQty.Text, 0) - strtofloatdef(KGQty.Text, 0));
  MoneyChaE.Text := FloatToStr(strtofloatdef(MYHDMoney.Text, 0) - strtofloatdef(MYMDMoney.Text, 0));
end;

procedure TfrmLLRKInPutMH_PBCG.HSPS();
begin

end;

procedure TfrmLLRKInPutMH_PBCG.BtnDelClick(Sender: TObject);
var
  FFMXID: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOCmd.Connection.BeginTrans;
    ClientDataSet1.Delete;
    GetHZData();
    ADOCmd.Connection.CommitTrans;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
    Exit;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.BtnQXClick(Sender: TObject);
begin
  Panel5.Visible := False;
end;

function TfrmLLRKInPutMH_PBCG.SaveDataMX(): Boolean;
var
  maxmxno, maxnoflag, fsj, FMXID: string;
begin
  Result := False;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from CK_MYSC_CR_MX where MYID=''' + Trim(FMainId) + '''');
    ExecSQL;
  end;
  try
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        if fieldbyname('HJPS').AsFloat <> 0 then
        begin
          if GetLSNo(ADOCmd, maxmxno, 'LM', 'CK_MYSC_CR_MX', 4, 1) = False then
          begin
            ClientDataSet1.EnableControls;
            Application.MessageBox('取来料入库数据最大号失败', '提示', 0);
            Exit;
          end;
          maxmxno := maxmxno + 'LML';
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_MYSC_CR_MX where MXID=''' + Trim(maxmxno) + '''');
            Open;
          end;
          with ADOCmd do
          begin
            Append;
            FieldByName('MYID').Value := Trim(FMainId);
            FieldByName('MXID').Value := Trim(maxmxno);
            FieldByName('RCGangNo').Value := ClientDataSet1.fieldbyname('RCGangNo').Value;
            FieldByName('MXPrice').Value := ClientDataSet1.fieldbyname('MXPrice').Value;
            FieldByName('MXMoney').Value := ClientDataSet1.fieldbyname('MXMoney').Value;
            FieldByName('KgQty').Value := ClientDataSet1.fieldbyname('KgQty').Value;
            FieldByName('KgQty2').Value := ClientDataSet1.fieldbyname('KgQty2').Value;
            FieldByName('KgQty3').Value := ClientDataSet1.fieldbyname('KgQty3').Value;
            FieldByName('KgQty4').Value := ClientDataSet1.fieldbyname('KgQty4').Value;
            FieldByName('KgQty5').Value := ClientDataSet1.fieldbyname('KgQty5').Value;
            FieldByName('KgQty6').Value := ClientDataSet1.fieldbyname('KgQty6').Value;
            FieldByName('KgQty7').Value := ClientDataSet1.fieldbyname('KgQty7').Value;
            FieldByName('KgQty8').Value := ClientDataSet1.fieldbyname('KgQty8').Value;
            FieldByName('KgQty9').Value := ClientDataSet1.fieldbyname('KgQty9').Value;
            FieldByName('KgQty10').Value := ClientDataSet1.fieldbyname('KgQty10').Value;
            FieldByName('MQty').Value := ClientDataSet1.fieldbyname('MQty').Value;
            FieldByName('MQty2').Value := ClientDataSet1.fieldbyname('MQty2').Value;
            FieldByName('MQty3').Value := ClientDataSet1.fieldbyname('MQty3').Value;
            FieldByName('MQty4').Value := ClientDataSet1.fieldbyname('MQty4').Value;
            FieldByName('MQty5').Value := ClientDataSet1.fieldbyname('MQty5').Value;
            FieldByName('MQty6').Value := ClientDataSet1.fieldbyname('MQty6').Value;
            FieldByName('MQty7').Value := ClientDataSet1.fieldbyname('MQty7').Value;
            FieldByName('MQty8').Value := ClientDataSet1.fieldbyname('MQty8').Value;
            FieldByName('MQty9').Value := ClientDataSet1.fieldbyname('MQty9').Value;
            FieldByName('MQty10').Value := ClientDataSet1.fieldbyname('MQty10').Value;
            FieldByName('HJPS').Value := ClientDataSet1.fieldbyname('HJPS').Value;
            FieldByName('HJQty').Value := ClientDataSet1.fieldbyname('HJQty').Value;
            FieldByName('HJKGQty').Value := ClientDataSet1.fieldbyname('HJKGQty').Value;
            FieldByName('JH').Value := ClientDataSet1.fieldbyname('JH').Value;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillerCode').Value := Trim(DCode);
            Post;
          end;
          if ClientDataSet1.fieldbyname('GangNo').AsString = '' then
          begin
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' exec P_Update_QCRK_GangNo :MYID');
              Parameters.ParamByName('MYID').Value := Trim(FMainId);
              ExecSQL;
            end;
          end;
          Edit;
          FieldByName('MYID').Value := Trim(FMainId);
          FieldByName('MXID').Value := Trim(maxmxno);
          Post;
        end;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_LLRK_PBQty :MYID');
      Parameters.ParamByName('MYID').Value := Trim(FMainId);
      ExecSQL;
    end;
    //InitData();
    Result := True;
  except
    Result := False;
    ClientDataSet1.EnableControls;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Button5Click(Sender: TObject);
var
  FReal: Double;
begin
  if SaveDataMX() then
  begin
    GetHZData();
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.N1Click(Sender: TObject);
begin
  HSPS();
end;

procedure TfrmLLRKInPutMH_PBCG.N2Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet1, False);
end;

procedure TfrmLLRKInPutMH_PBCG.QtyChange(Sender: TObject);
begin
  if MDMFlag.Checked = true then
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(Qty.text, 0));
  if MDKGFlag.Checked = true then
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(KgQty.text, 0));
  GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.MDMFlagClick(Sender: TObject);
begin

  if MDMFlag.Checked = true then
  begin
    if MDKGFlag.Checked = true then
      MDKGFlag.Checked := false;
  end
  else
  begin
    if MDKGFlag.Checked = false then
      MDKGFlag.Checked := true;
  end;
  if MDMFlag.Checked = true then
  begin
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZQty.text, 0));
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(Qty.text, 0));
  end;
  if MDKGFlag.Checked = true then
  begin
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(KgQty.text, 0));
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZKgQty.text, 0));
  end;
  if ClientDataSet1.IsEmpty = false then
  begin
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        edit;
        FieldByName('MXPrice').Value := strtofloatdef(MYMDPrice.text, 0);
        if MDMFlag.Checked = True then
          FieldByName('MXMoney').Value := FieldByName('MQty').AsFloat * strtofloatdef(MYMDPrice.text, 0)
        else
          FieldByName('MXMoney').Value := FieldByName('KgQty').AsFloat * strtofloatdef(MYMDPrice.text, 0);
        next;
      end;
    end;
    ClientDataSet1.EnableControls;
  end;
  GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.MDKGFlagClick(Sender: TObject);
begin

  if MDKGFlag.Checked = true then
  begin
    if MDMFlag.Checked = true then
      MDMFlag.Checked := false;
  end
  else
  begin
    if MDMFlag.Checked = false then
      MDMFlag.Checked := true;
  end;
  if MDMFlag.Checked = true then
  begin
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZQty.text, 0));
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(Qty.text, 0));
  end;
  if MDKGFlag.Checked = true then
  begin
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(KgQty.text, 0));
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZKgQty.text, 0));
  end;
  if ClientDataSet1.IsEmpty = false then
  begin
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        edit;
        FieldByName('MXPrice').Value := strtofloatdef(MYMDPrice.text, 0);
        if MDMFlag.Checked = True then
          FieldByName('MXMoney').Value := FieldByName('MQty').AsFloat * strtofloatdef(MYMDPrice.text, 0)
        else
          FieldByName('MXMoney').Value := FieldByName('KgQty').AsFloat * strtofloatdef(MYMDPrice.text, 0);
        next;
      end;
    end;
    ClientDataSet1.EnableControls;
  end;
  GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.MYNameBtnClick(Sender: TObject);
begin
  frmBPZDY_LRPB := TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    Flag := 'PBName';
    flagName := '坯布';
    ViewFlag := true;
    ToolBar3.Visible := false;
    Button1.Visible := False;
    Panel7.Visible := false;
    if ShowModal = 1 then
    begin
      MYName.text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      MYCode.text := Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      MYCF.text := Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
      MYMF.text := Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
      MYKZ.text := Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
      MYJWS.text := Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
      MYMD.text := Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
      //MYPrice.text:=Trim(ClientDataSet1.fieldbyname('BPPrice').AsString);
      //MYMDPrice.text:=Trim(ClientDataSet1.fieldbyname('BPPrice').AsString);
      Note.text := Trim(ClientDataSet1.fieldbyname('BPNote').AsString);
      if ClientDataSet1.fieldbyname('IFHS').AsString = '含税' then
      begin
        HSFlag.Checked := True;
        BHSFlag.Checked := false;
      end
      else
      begin
        HSFlag.Checked := false;
        BHSFlag.Checked := true;
      end;
    end;
    free;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.MXHZQtyChange(Sender: TObject);
begin
  if MDMFlag.Checked = true then
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZQty.text, 0));
  if MDKGFlag.Checked = true then
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZKgQty.text, 0));
  //GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.Panel13Click(Sender: TObject);
begin
  Panel12.Visible := False;
end;

procedure TfrmLLRKInPutMH_PBCG.Button2Click(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
  maxNo: string;
begin
  if FMainid = '' then
  begin
    if GetLSNo(ADOCmd, FMainid, 'LL', 'CK_MYSC_CR', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
  end;
  try
    adoqueryCmd.Connection.BeginTrans;
    begin
      fFilePath := WJPach.Text;
      fFileName := WJName.Text;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TFId from TP_File ');
        sql.Add('where WBID=''' + trim(FMainid) + ''' ');
        sql.Add('and TFType=''采购坯布'' ');
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        open;
      end;
      if ADOQueryCmd.IsEmpty = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！', '提示信息', MB_ICONERROR);
        exit;
      end;
      Panel12.Visible := False;
      Panel2.Caption := '正在上传数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;
      if GetLSNo(ADOQueryCmd, maxNo, 'MD', 'TP_File', 4, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('delete from TP_File ');
        sql.Add('where WBID=''' + trim(FMainid) + '''');
        sql.Add('and TFType=''采购坯布''');
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        execsql;
      end;
      with adoqueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from TP_File ');
        sql.Add('where WBID=''' + trim(FMainid) + '''');
        sql.Add('and TFType=''采购坯布''');
        sql.Add('and FileName=' + quotedstr(trim(fFileName)));
        open;
        append;
        fieldbyname('TFID').Value := trim(maxNo);
        fieldbyname('WBID').Value := trim(FMainid);
        fieldbyname('TFType').Value := '采购坯布';
        fieldbyname('FileName').Value := trim(fFileName);
        post;
      end;
      if fFilePath <> '' then
      begin
        try
          IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
          IdFTP1.Username := 'three';
          IdFTP1.Password := '641010';
          IdFTP1.Connect();
          IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
          IdFTP1.Quit;
        except
          IdFTP1.Quit;
          Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
        end;
      end;
      IdFTP1.Quit;
      Panel2.Visible := false;
      Initimage();
    end;
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Button8Click(Sender: TObject);
var
  fFileName: string;
begin
  fFileName := WJName.Hint;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('UPdate  TP_File Set FileName=''' + Trim(WJName.Text) + '''');
    sql.Add('where WBID=''' + trim(FMainid) + '''');
    sql.Add('and TFType=''采购坯布''');
    sql.Add('and FileName=' + quotedstr(trim(fFileName)));
    execsql;
  end;
  Panel12.Visible := False;
  Initimage();
end;

procedure TfrmLLRKInPutMH_PBCG.Button7Click(Sender: TObject);
var
  fFileName: string;
  fFilePath: string;
  maxNo: string;
begin
  try
    adoqueryCmd.Connection.BeginTrans;
    fFilePath := WJPach.Text;
    fFileName := WJName.Hint;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select TFId from TP_File ');
      sql.Add('where WBID=''' + trim(FMainid) + '''');
      sql.Add('and TFType=''采购坯布''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      open;
      if not adoqueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('此附件名称已存在，请修改文件名，继续上传！', '提示信息', MB_ICONERROR);
        exit;
      end;
    end;
    Panel12.Visible := False;
    Panel2.Caption := '正在上传数据，请稍等...';
    Panel2.Visible := true;
    application.ProcessMessages;

    if GetLSNo(ADOQueryCmd, maxNo, 'FJ', 'TP_File', 4, 1) = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from TP_File ');
      sql.Add('where WBID=''' + trim(FMainid) + '''');
      sql.Add('and TFType=''采购坯布''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      execsql;
    end;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from TP_File ');
      sql.Add('where WBID=''' + trim(FMainid) + '''');
      sql.Add('and TFType=''采购坯布''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      open;
      append;
      fieldbyname('TFID').Value := trim(maxNo);
      fieldbyname('WBID').Value := trim(FMainid);
      fieldbyname('TFType').Value := '采购坯布';
      fieldbyname('FileName').Value := trim(fFileName);
      post;
    end;
    if fFilePath <> '' then
    begin
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
        IdFTP1.Username := 'three';
        IdFTP1.Password := '641010';
        IdFTP1.Connect();
        IdFTP1.Put(fFilePath, 'FJ\' + Trim(fFileName));
        IdFTP1.Quit;
      except
        IdFTP1.Quit;
        Application.MessageBox('上传文件失败，请检查文件服务器！', '提示', MB_ICONWARNING);
      end;
    end;
    IdFTP1.Quit;
    Panel2.Visible := false;
    Initimage();
    adoqueryCmd.Connection.CommitTrans;
  except
    adoqueryCmd.Connection.RollbackTrans;
    application.MessageBox('文件保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Image2Click(Sender: TObject);
begin
  Panel12.Visible := false;
end;

procedure TfrmLLRKInPutMH_PBCG.MenuItem1Click(Sender: TObject);
var
  OpenDiaLog: TOpenDialog;
  fFileName, FWJName: string;
  fFilePath: string;
begin

  if GetLSNo(ADOCmd, FWJName, 'PB', 'CK_MYSC_CR_MX', 2, 1) = False then
  begin
    ClientDataSet1.EnableControls;
    Application.MessageBox('取来料入库数据最大号失败', '提示', 0);
    Exit;
  end;
  OpenDiaLog := TOpenDialog.Create(Self);
  if OpenDiaLog.Execute then
  begin
    fFilePath := OpenDiaLog.FileName;
    fFileName := ExtractFileName(OpenDiaLog.FileName);
    fFileName := FWJName + copy(fFileName, Pos('.', fFileName), Length(fFileName));
    Panel12.Visible := True;
    WJName.Text := Trim(fFileName);
    WJName.Hint := Trim(fFileName);
    WJPach.Text := fFilePath;
    Button8.Visible := False;
    Button2.Visible := True;
    Button7.Visible := True;
    Panel12.Refresh;
  end;
  SaveFlag := '99';
end;

procedure TfrmLLRKInPutMH_PBCG.N3Click(Sender: TObject);
var
  fFileName: string;
begin
  if listView1.SelCount < 1 then
    exit;
  try
    fFileName := ListView1.Selected.Caption;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from TP_File ');
      sql.Add('where WBID=''' + trim(FMainid) + '''');
      sql.Add('and TFType=''采购坯布''');
      sql.Add('and FileName=' + quotedstr(trim(fFileName)));
      execsql;
    end;
    Initimage();
  except
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.MenuItem2Click(Sender: TObject);
begin
  if listView1.SelCount < 1 then
    exit;
  Panel12.Visible := True;
  Button2.Visible := False;
  Button7.Visible := False;
  Button8.Visible := True;
  Panel12.Refresh;
  WJName.Text := Trim(ListView1.Selected.Caption);
  WJName.Hint := Trim(ListView1.Selected.Caption);
end;

procedure TfrmLLRKInPutMH_PBCG.N4Click(Sender: TObject);
var
  SaveDialog: TSaveDialog;
  fFileName: string;
  fFilePath: string;
begin
  if listView1.SelCount < 1 then
    exit;
  try
    fFileName := ListView1.Selected.Caption;
    SaveDialog := TSaveDialog.Create(Self);
    SaveDialog.FileName := fFileName;
    if SaveDialog.Execute then
    begin
      Panel2.Caption := '正在保存数据，请稍等...';
      Panel2.Visible := true;
      application.ProcessMessages;
      fFilePath := SaveDialog.FileName;
      try
        IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', 'FTP地址', '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('FJ\' + Trim(fFileName), fFilePath, false, true);
        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;
    end;
  except
    Panel2.Visible := false;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.Panel2Click(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmLLRKInPutMH_PBCG.ListView1DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
begin
  if ListView1.Items.Count < 1 then
    EXIT;

  if listView1.SelCount < 1 then
    exit;
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

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

  fileName := ListView1.Selected.Caption;

  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('FJ\' + 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 TfrmLLRKInPutMH_PBCG.JCDanWeiBtnClick(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '染厂';
      KHType.TxtCode := '染厂';
      if ShowModal = 1 then
      begin
        Self.JCDanWei.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
        Self.JCNo.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.MXHZKgQtyChange(Sender: TObject);
begin
  if MDMFlag.Checked = true then
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZQty.text, 0));
  if MDKGFlag.Checked = true then
    MYHDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(MXHZKgQty.text, 0));
  //GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.MYMDPriceChange(Sender: TObject);
begin
  if MDMFlag.Checked = true then
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(Qty.text, 0));
  if MDKGFlag.Checked = true then
    MYMDMoney.Text := FloatToStr(strtofloatdef(MYMDPrice.text, 0) * strtofloatdef(KgQty.text, 0));
  if ClientDataSet1.IsEmpty = false then
  begin
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        edit;
        FieldByName('MXPrice').Value := strtofloatdef(MYMDPrice.text, 0);
        if MDMFlag.Checked = True then
          FieldByName('MXMoney').Value := FieldByName('MQty').AsFloat * strtofloatdef(MYMDPrice.text, 0)
        else
          FieldByName('MXMoney').Value := FieldByName('KgQty').AsFloat * strtofloatdef(MYMDPrice.text, 0);
        next;
      end;
    end;
    ClientDataSet1.EnableControls;
  end;
  GetHZData();
end;

procedure TfrmLLRKInPutMH_PBCG.HSFlagClick(Sender: TObject);
begin
  if HSFlag.Checked = true then
  begin
    if BHSFlag.Checked = true then
      BHSFlag.Checked := false;
  end
  else
  begin
    if BHSFlag.Checked = false then
      BHSFlag.Checked := true;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.BHSFlagClick(Sender: TObject);
begin
  if BHSFlag.Checked = true then
  begin
    if HSFlag.Checked = true then
      HSFlag.Checked := false;
  end
  else
  begin
    if HSFlag.Checked = false then
      HSFlag.Checked := true;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.PSChaEChange(Sender: TObject);
begin
  if StrToFloatDef(PSChaE.Text, 0) > 0 then
  begin
    PSChaE.Font.Color := clGreen;
  end;
  if StrToFloatDef(PSChaE.Text, 0) < 0 then
  begin
    PSChaE.Font.Color := clRed;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.QtyChaEChange(Sender: TObject);
begin
  if StrToFloatDef(QtyChaE.Text, 0) > 0 then
  begin
    QtyChaE.Font.Color := clGreen;
  end;
  if StrToFloatDef(QtyChaE.Text, 0) < 0 then
  begin
    QtyChaE.Font.Color := clRed;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.KgQtyChaEChange(Sender: TObject);
begin
  if StrToFloatDef(KgQtyChaE.Text, 0) > 0 then
  begin
    KgQtyChaE.Font.Color := clGreen;
  end;
  if StrToFloatDef(KgQtyChaE.Text, 0) < 0 then
  begin
    KgQtyChaE.Font.Color := clRed;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.MoneyChaEChange(Sender: TObject);
begin
  if StrToFloatDef(MoneyChaE.Text, 0) > 0 then
  begin
    MoneyChaE.Font.Color := clGreen;
  end;
  if StrToFloatDef(MoneyChaE.Text, 0) < 0 then
  begin
    MoneyChaE.Font.Color := clRed;
  end;
end;

procedure TfrmLLRKInPutMH_PBCG.PSChange(Sender: TObject);
begin
  PSChaE.Text := FloatToStr(strtofloatdef(MXHZPS.Text, 0) - strtofloatdef(PS.Text, 0));
end;

end.

