unit U_YPGL_YGML;

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, cxPC, cxButtonEdit, cxTextEdit, cxCurrencyEdit, RM_e_Graphic,
  RM_e_Jpeg;

type
  TfrmYPGLYGML = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    v1FLMoney: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_Ord: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    v1BGYSFee: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    v1MLJGMoney: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    v1OtherMoney: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    RMJPEGExport1: TRMJPEGExport;
    Label6: TLabel;
    CustomerNoName: TEdit;
    v1DEFStr7: TcxGridDBColumn;
    v1YLYGMoney: TcxGridDBColumn;
    v1ZJYGMoney: TcxGridDBColumn;
    v1KPYGMoney: TcxGridDBColumn;
    v1GLMoney: TcxGridDBColumn;
    v1YGLiRun: TcxGridDBColumn;
    order_Sub: TClientDataSet;
    Label15: TLabel;
    DEFStr7: TEdit;
    TEdit: TToolButton;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1PRTCodeName: TcxGridDBColumn;
    Label2: TLabel;
    PRTCodeName: TEdit;
    v1PRTspec: TcxGridDBColumn;
    v1Filltime: TcxGridDBColumn;
    v1JGMoney: TcxGridDBColumn;
    v1ConMXMoney: TcxGridDBColumn;
    Order_ZL: TClientDataSet;
    Order_FL: TClientDataSet;
    Order_MLJG: TClientDataSet;
    Order_CBJG: TClientDataSet;
    RMDB_CBJG: TRMDBDataSet;
    RMDB_MLJG: TRMDBDataSet;
    RMDB_ZL: TRMDBDataSet;
    RMDB_FL: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    ToolButton1: TToolButton;
    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 TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TEditClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure SetStatus();
    procedure InitSub();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1,Canshu2:string;
    { Public declarations }
  end;

var
  frmYPGLYGML: TfrmYPGLYGML;
implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_YPGL_YGDJ;

{$R *.dfm}
procedure TfrmYPGLYGML.InitSub();
begin
  IF Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrderCon_sub ');
    sql.Add('where mainID ='+quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQuerySub,Order_Sub);
  SInitCDSData20(ADOQuerySub,Order_Sub);
end;

procedure TfrmYPGLYGML.SetStatus();
begin
 
end;

procedure TfrmYPGLYGML.FormDestroy(Sender: TObject);
begin
  frmYPGLYGML:=nil;
end;

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

procedure TfrmYPGLYGML.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=trim(DParameters1);
  Canshu2:=trim(DParameters2);
end;

procedure TfrmYPGLYGML.TBCloseClick(Sender: TObject);
begin
  Close;
  if FCloth<>1 then
    WriteCxGrid('订单合同列表',Tv1,'生产指示单管理')
  else
    WriteCxGrid('订单合同列表选择',Tv1,'生产指示单管理');
  SetDate(ADOQueryTemp,Self.Caption,DName,begdate.DateTime,enddate.DateTime);
end;

procedure TfrmYPGLYGML.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      sql.Add(',DEFStr7=(select Top 1 B.DEFStr7 from KH_Zdy_Attachment B where B.Type=''KHName'' and B.ZdyName=A.CustomerNoName)');
      sql.Add(' from JYOrderCon_Main A');
      sql.Add(' inner join JYOrderCon_Sub B on B.Mainid=A.Mainid');
      SQL.Add(' where  A.fILLtIME>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
      SQL.Add(' and A.fILLtIME<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      sql.Add(' and (A.ConPerson1='''+Trim(DName)+''' or A.Filler='''+Trim(DName)+''')');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYPGLYGML.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYPGLYGML.InitForm();
begin
  if FCloth<>1 then
    ReadCxGrid('订单合同列表',Tv1,'生产指示单管理')
  else
    ReadCxGrid('订单合同列表选择',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-GetBEDate(ADOQueryTemp,Self.Caption,Dname);
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmYPGLYGML.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmYPGLYGML.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JYOrderCon_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' '+quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(','+quotedstr(trim(self.Caption)));
      sql.Add(','+quotedstr(trim('外销合同删除')));
      sql.Add(','+quotedstr(trim('合同号：'+trim(Order_Main.FieldByName('conNo').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

procedure TfrmYPGLYGML.FormShow(Sender: TObject);
begin
  SetStatus();
  InitForm();

end;

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

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

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

procedure TfrmYPGLYGML.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 TfrmYPGLYGML.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 TfrmYPGLYGML.CustomerNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYPGLYGML.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmYPGLYGML.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmYPGLYGML.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   EngMoney:string;
begin
  if Order_Main.IsEmpty then Exit;
  Button1.Click;
end;

procedure TfrmYPGLYGML.Button1Click(Sender: TObject);
var
   fPrintFile:string;
   EngMoney:string;
   FimageFile: string;
   i:Integer;
begin
  FimageFile:='';
  if Order_Main.IsEmpty then Exit;
  if radiogroup1.ItemIndex=0 then
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\预估成本表.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,XHNo=cast(0 as int),RMBMoney=B.ConMXMoney*B.HuiLV ');
    sql.Add(',SumQty=(select Sum(C.PRTOrderQty) from JYOrderCon_Sub C where C.Subid=B.Subid)');
    sql.Add(',SumConMXMoney=(select Sum(C.ConMXMoney) from JYOrderCon_Sub C where C.Subid=B.Subid)');
    sql.Add(',SumRMBMoney=(select Sum(C.ConMXMoney*C.HuiLV) from JYOrderCon_Sub C where C.Subid=B.Subid)');
    sql.Add(',DEFStr7=(select Top 1 B.DEFStr7 from KH_Zdy_Attachment B where B.Type=''KHName'' and B.ZdyName=A.CustomerNoName)');
    sql.Add(' from JYOrderCon_Main A');
    sql.Add(' inner join JYOrderCon_Sub B on B.Mainid=A.Mainid');
    SQL.Add(' where B.Subid='''+trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  with CDS_Print do
  begin
    first;
    i:=1;
    while not eof do
    begin
      edit;
      FieldByName('XHNo').Value:=i;
      i:=i+1;
      next;
    end;
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * ');
    sql.add(',SumQty=(select Sum(B.Qty) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''主料'')');
    sql.add(',SumXLQty=(select Sum(B.XLQty) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''主料'')');
    sql.add(',SumMoney=(select Sum(B.Money) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''主料'')');
    sql.add('from JYOrderCon_YGCB A ');
    sql.Add('where A.Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    sql.add(' and A.CBType=''主料''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_ZL);
  SInitCDSData20(ADOQuery1,Order_ZL);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * ');
    sql.add(',SumQty=(select Sum(B.Qty) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''面料加工'')');
    sql.add(',SumMoney=(select Sum(B.Money) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''面料加工'')');
    sql.add('from JYOrderCon_YGCB A ');
    sql.Add('where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    sql.add(' and CBType=''面料加工''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_MLJG);
  SInitCDSData20(ADOQuery1,Order_MLJG);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * ');
    sql.add(',SumXLQty=(select Sum(B.XLQty) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''辅料'')');
    sql.add(',SumMoney=(select Sum(B.Money) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''辅料'')');
    sql.add('from JYOrderCon_YGCB A ');
    sql.Add('where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    sql.add(' and CBType=''辅料''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_FL);
  SInitCDSData20(ADOQuery1,Order_FL);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * ');
    sql.add(',SumQty=(select Sum(B.Qty) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''成品加工'')');
    sql.add(',SumMoney=(select Sum(B.Money) from JYOrderCon_YGCB B where B.Subid=A.Subid and B.CBType=''成品加工'')');
    sql.add('from JYOrderCon_YGCB A ');
    sql.Add('where Subid='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    sql.add(' and CBType=''成品加工''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_CBJG);
  SInitCDSData20(ADOQuery1,Order_CBJG);
  if FileExists(fPrintFile) then
  begin
    RMVariables['ImageFile']:=trim(FimageFile);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\预估成本表.rmf'),'提示',0);
  end;
end;

procedure TfrmYPGLYGML.TEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmYPGLYGDJ:=TfrmYPGLYGDJ.Create(Application);
    with frmYPGLYGDJ do
    begin
      PState:=1;
      FSubid:=Trim(Self.Order_Main.fieldbyname('Subid').AsString);
      FMainid:=Trim(Self.Order_Main.fieldbyname('Mainid').AsString);
      FZT:='1';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYPGLYGDJ.Free;
  end;
end;

procedure TfrmYPGLYGML.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TEdit.Click;
end;

procedure TfrmYPGLYGML.ToolButton1Click(Sender: TObject);
begin

  try
    frmYPGLYGDJ:=TfrmYPGLYGDJ.Create(Application);
    with frmYPGLYGDJ do
    begin
      PState:=0;
      FSubid:=Trim(Self.Order_Main.fieldbyname('Subid').AsString);
      FMainid:=Trim(Self.Order_Main.fieldbyname('Mainid').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYPGLYGDJ.Free;
  end;
end;

end.
