unit U_ProductOrderMoneyList_BPXCX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxDropDownEdit, cxTextEdit, cxButtonEdit,cxPC, jpeg,
  IdBaseComponent, IdComponent,IdTCPConnection, IdTCPClient, IdFTP,ShellAPI,IniFiles,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmProductOrderMoneyList_BPXCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label12: TLabel;
    PRTHX: TEdit;
    Label13: TLabel;
    PRTColor: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Chker: TClientDataSet;
    Label2: TLabel;
    YWY: TEdit;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ADOQuery1: TADOQuery;
    cxSplitter1: TcxSplitter;
    Label5: TLabel;
    StyleNo: TEdit;
    Panel2: TPanel;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ADOQuery2: TADOQuery;
    Label7: TLabel;
    MXCPSpec: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    CDS_Prt: TClientDataSet;
    RMDBDCG: TRMDBDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    GroupBox1: TGroupBox;
    Image1: TImage;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1YWY: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCode: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    vPRTColor: TcxGridDBColumn;
    v1PRTHX: TcxGridDBColumn;
    v1StyleNo: TcxGridDBColumn;
    v1MXCPSpec: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel4: TPanel;
    Panel6: TPanel;
    Button1: TButton;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource3: TDataSource;
    Order_BP: TClientDataSet;
    Order_X: TClientDataSet;
    DataSource4: TDataSource;
    Label9: TLabel;
    BPDYID: TEdit;
    V3Ssel: TcxGridDBColumn;
    V3ZT: TcxGridDBColumn;
    V3OrdJH: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button4Click(Sender: TObject);
    procedure ReadINIFile();
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure DYIDChange(Sender: TObject);
    procedure BPDYIDChange(Sender: TObject);
  private
    canshu1,canshu2,canshu3:String;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure InitImage();
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    FWFPMoney,FWSXMoney:Integer;
    { Public declarations }
  end;

var
  frmProductOrderMoneyList_BPXCX: TfrmProductOrderMoneyList_BPXCX;

implementation
uses
  U_DataLink,U_OrderInPut1,U_Fun, U_ZDYHelp, U_ZDYHelpSel,U_FjList_RZ;

{$R *.dfm}
procedure TfrmProductOrderMoneyList_BPXCX.setstatus();
begin
  
end;

procedure TfrmProductOrderMoneyList_BPXCX.ReadINIFile();
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 TfrmProductOrderMoneyList_BPXCX.InitImage();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  begin
    Image1.Picture.Assign(nil);
    try
      with ADOQuery2 do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from TP_File A where isnull(A.WBID,'''')='''+Trim(Order_Main.fieldbyname('HXTuGao').AsString)+'''');
        //sql.Add(' and TFType=''BJ'' ');
        open;
        if RecordCount>0 then
        begin
          myStream:=tadoblobstream.Create(tblobfield(ADOQuery2.fieldbyname('FilesOther')),bmread);
          if myStream=nil then exit;
          jpg:=TJPEGImage.Create;
          jpg.LoadFromStream(myStream);
          Image1.Picture.Assign(jpg);
        end;
      end;
    finally;
      jpg.Free;
      myStream.Free;
    end;
  end;
end;

procedure TfrmProductOrderMoneyList_BPXCX.FormDestroy(Sender: TObject);
begin
  frmProductOrderMoneyList_BPXCX:=nil;
end;

procedure TfrmProductOrderMoneyList_BPXCX.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmProductOrderMoneyList_BPXCX.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  canshu3:=Trim(DParameters3);
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF',Tv1,'生产指示单管理');
  WriteCxGrid('指示单明细FF1',Tv2,'生产指示单管理');
  Close;
end;

procedure TfrmProductOrderMoneyList_BPXCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* from JYOrder_Main A ');
      sql.Add(' inner join JYOrder_Sub B on B.Mainid=A.Mainid');
      sql.Add(' where A.OrdDate>='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+'''');
      sql.add(' and A.OrdDate<'''+trim(formatdatetime('yyyy-MM-dd',enddate.DateTime+1))+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderMoneyList_BPXCX.InitGridFH();
begin
  
end;

procedure TfrmProductOrderMoneyList_BPXCX.InitForm();
begin
  ReadCxGrid('指示单列表FF',Tv1,'生产指示单管理');
  ReadCxGrid('指示单明细FF1',Tv2,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  setstatus();
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmProductOrderMoneyList_BPXCX.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add('delete from TP_File where WBID in (select A.HXTuGao from JYOrder_Sub A where A.Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''') ');
      sql.Add('delete from XD_File where CYID in (select A.HXTuGao from JYOrder_Sub A where A.Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''') ');
      sql.Add('delete from JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'生产指示单列表');
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
   i,j:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.FieldByName('ChkStatus').AsString)<>'审核通过' then
  begin
    RM1.PreviewButtons:=[pbExit];
  end else
  begin
    RM1.PreviewButtons:=[pbZoom,pbLoad,pbSave,pbPrint,pbFind,pbPageSetup,pbExit,pbExport,pbNavigator];
  end;
  if Trim(Order_Main.fieldbyname('JYType').AsString)='' then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  end else
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\包装指示单V2.rmf' ;
  end;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,ColorCount=(select isnull(Count(*),0) from JYOrder_Sub where MainId=A.MainId),  ');
    sql.add('OrderUnit=(select Top 1 OrderUnit from JYOrder_Sub where MainId=A.MainId),');
    sql.add('ZQty=(select sum(PRTOrderQty) from JYOrder_Sub where MainId=A.MainId)');
    sql.Add(' from JYOrder_Main A  ');
    sql.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    if Trim(Order_Main.fieldbyname('JYType').AsString)='' then
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0)
    else
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\包装指示单V2.rmf'),'提示',0);
  end;
  //Panel4.Visible:=True;
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_BPXCX.OrderNoChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmProductOrderMoneyList_BPXCX.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderMoneyList_BPXCX.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

procedure TfrmProductOrderMoneyList_BPXCX.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_BPXCX.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderMoneyList_BPXCX.v1DeliveryDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrderMoneyList_BPXCX.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderMoneyList_BPXCX.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderMoneyList_BPXCX.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmProductOrderMoneyList_BPXCX.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<3 then Exit;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* from JYOrder_Main A ');
      sql.Add(' inner join JYOrder_Sub B on B.Mainid=A.Mainid');
      sql.add('where A.OrderNo like '''+'%'+trim(OrderNo.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  end;
end;

procedure TfrmProductOrderMoneyList_BPXCX.ConNoKeyPress(Sender: TObject;
  var Key: Char);
var
  strwhere:string;
begin
  
end;

procedure TfrmProductOrderMoneyList_BPXCX.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmProductOrderMoneyList_BPXCX.Button4Click(Sender: TObject);
var
  maxsubno:String;
  FRLMoney,FSXMoney:double;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('FPDate',Null,[]) then
  begin
    Application.MessageBox('认领日期不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPType',Null,[]) then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('FPMoney',Null,[]) then
  begin
    Application.MessageBox('认领金额不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  Panel1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select Sum(FPMoney) SumFPMoney,Sum(SXMoney) SumSXMoney,Sum(OtherMoney) SumOtherMoney from SK_Money_FP where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
            Open;
          end;
          FRLMoney:=ADOQueryCmd.fieldbyname('SumFPMoney').AsCurrency+ClientDataSet2.fieldbyname('FPMoney').AsCurrency+ADOQueryCmd.fieldbyname('SumOtherMoney').AsCurrency+ClientDataSet2.fieldbyname('OtherMoney').AsCurrency;
          FSXMoney:=ADOQueryCmd.fieldbyname('SumSXMoney').AsCurrency+ClientDataSet2.fieldbyname('SXMoney').AsCurrency;
          if FRLMoney>ClientDataSet2.FieldByName('RZMoney').asfloat then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('认领总金额超过入账金额','提示');
            exit;
          end;
          if FSXMoney>ClientDataSet2.FieldByName('SJSXMoney').asfloat then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('认领总手续费超过手续费','提示');
            exit;
          end;
          if GetLSNo(ADOQueryTemp,maxsubno,'FP','SK_Money_FP',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet2.fieldbyname('FPId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SK_Money_FP where FPId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('FPId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('FPId').Value:=Trim(maxsubno);
          FieldByName('YFID').Value:=Trim(ClientDataSet2.fieldbyname('YFID').AsString);
          FieldByName('FPTYpe').Value:=ClientDataSet2.fieldbyname('FPTYpe').Value;
          FieldByName('FPDate').Value:=ClientDataSet2.fieldbyname('FPDate').Value;
          FieldByName('WBID').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          if Trim(ClientDataSet2.fieldbyname('FPMoney').AsString)='' then
          begin
            fieldbyname('FPMoney').Value:=0
          end else
          begin
            fieldbyname('FPMoney').Value:=Trim(ClientDataSet2.fieldbyname('FPMoney').AsString);
          end;
          fieldbyname('SXMoney').Value:=CLientDataSet2.fieldbyname('SXMoney').AsFloat;
          fieldbyname('OtherMoney').Value:=CLientDataSet2.fieldbyname('OtherMoney').AsFloat;
          FieldByName('Note').Value:=Trim(ClientDataSet2.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Main Set DJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''定金'') ');
          sql.Add(',HKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID and A.FPType=''货款'') ');
          sql.Add(',SXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID) ');
          sql.add(',JYOtherMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
          sql.Add(' where A.WBID=JYOrder_Main.MainID)');
          sql.add(',SCHuiLv=(Case when (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
          sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)<>0 then ');
          sql.add(' (select Top 1 C.HuiLv from YF_Money_CR C inner join SK_Money_FP A on C.YFID=A.YFID ');
          sql.add(' where A.WBID=JYOrder_Main.MainID and C.CRType=''收款登记'' and C.BZType=''$'' Order by A.FPDate desc)*1.17-0.35 else NULL end)');
          sql.Add(' where MainID='''+Trim(Order_Main.fieldbyname('MainID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPDJMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''定金'' and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''')),');
          sql.Add('YSXMoney=(select Sum(SXMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''' or A.FPType=''货款'')), ');
          sql.Add('TKMoney=(select Sum(OtherMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and (isnull(A.WBID,'''')='''' or isnull(A.TTID,'''')='''' or A.FPType=''货款'')) ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR Set FPHKMoney=(select Sum(FPMoney) from SK_Money_FP A ');
          sql.Add(' where A.YFID=YF_Money_CR.YFID and A.FPType=''货款'') ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set WFPMoney=isnull(RZMoney,0)-isnull(FPDJMoney,0)-isnull(FPHKMoney,0)-isnull(TKMoney,0) ');
          sql.Add('where YFID='''+Trim(ClientDataSet2.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('FPId').Value:=Trim(maxsubno);
        //ClientDataSet2.Post;
        FRLMoney:=0;
        FSXMoney:=0;
        Next;
      end;
    end;
    //initGrid();
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;


procedure TfrmProductOrderMoneyList_BPXCX.TV2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitImage();
end;

procedure TfrmProductOrderMoneyList_BPXCX.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

procedure TfrmProductOrderMoneyList_BPXCX.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add(',ZT=(case when isnull((select Top 1 B.MJID from CK_YgCl B where B.MJID=A.BPDYID),'''')='''' then ''未生成'' else ''已生成'' end)');
    sql.Add(' from Order_BPDY A ');
    sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQuery1,Order_BP);
  SInitCDSData20(ADOQuery1,Order_BP);
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* from Order_XDY A ');
    sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQuery2,Order_X);
  SInitCDSData20(ADOQuery2,Order_X);
end;

procedure TfrmProductOrderMoneyList_BPXCX.Button1Click(Sender: TObject);
var
  fPrintFile:string;
  Porderno,LBName,maxno:string;
  i,j:Integer;
  Txt,fImagePath,FSubid:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if Order_BP.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\布票标签.rmf' ;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.* ');
    sql.Add(',QRBARCODE=cast('''' as varchar(100))');
    sql.add(',ZT=(case when isnull((select Top 1 D.Mainid from CK_YgCl D where D.MJID=A.BPDYID),'''')='''' then '''' else ''已生产'' end)');
    sql.Add(' from Order_BPDY A ');
    sql.Add(' inner join JYOrder_Main B on B.Mainid=A.Mainid ');
    sql.Add(' inner join JYOrder_Sub C on C.Subid=A.Subid');
    sql.Add(' where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Print);
  Order_BP.DisableControls;
  with Order_BP do
  begin
    first;
    i:=0;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*,B.*,C.* ');
          sql.Add(',QRBARCODE=cast('''' as varchar(100))');
          sql.add(',ZT=(case when isnull((select Top 1 D.Mainid from CK_YgCl D where D.MJID=A.BPDYID),'''')='''' then '''' else ''已生产'' end)');
          sql.Add(' from Order_BPDY A ');
          sql.Add(' inner join JYOrder_Main B on B.Mainid=A.Mainid ');
          sql.Add(' inner join JYOrder_Sub C on C.Subid=A.Subid');
          sql.Add(' where A.BPDYID='''+Trim(Order_BP.fieldbyname('BPDYID').AsString)+'''');
          Open;
        end;
        SInitCDSData20(ADOQueryTemp,CDS_Print);
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(Order_BP.fieldbyname('BPDYID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp'+trim(IntToStr(i))+'.bmp' ;
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
              CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
          exit;
        end;
        CDS_Print.Locate('BPDYID',Trim(Order_BP.fieldbyname('BPDYID').AsString),[]);
        with CDS_Print do
        begin
          edit;
          FieldByName('QRBARCODE').Value:=Trim(fImagePath);
        end;
        i:=i+1;
      end;
      next;
    end;
  end;
  Order_BP.EnableControls;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\指示单.rmf'),'提示',0);
  end;
end;

procedure TfrmProductOrderMoneyList_BPXCX.DYIDChange(Sender: TObject);
begin
  SDofilter(ADOQuery2,SGetFilters(Panel5,1,2));
  SCreateCDS20(ADOQuery2,Order_X);
  SInitCDSData20(ADOQuery2,Order_X);
end;

procedure TfrmProductOrderMoneyList_BPXCX.BPDYIDChange(Sender: TObject);
begin
  SDofilter(ADOQuery1,SGetFilters(Panel6,1,2));
  SCreateCDS20(ADOQuery1,Order_BP);
  SInitCDSData20(ADOQuery1,Order_BP);
end;

end.
