unit U_ContractCBYG;

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, cxDropDownEdit, cxGridBandedTableView, cxGridDBBandedTableView;

type
  TfrmContractCBYG = 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;
    v1YGNo: TcxGridDBColumn;
    v1FLMoney: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_Ord: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: 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;
    YGNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    v1OtherMoney: TcxGridDBColumn;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    RMJPEGExport1: TRMJPEGExport;
    Label6: TLabel;
    CustomerNoName: TEdit;
    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;
    Label2: TLabel;
    PRTCodeName: TEdit;
    v1Filltime: TcxGridDBColumn;
    v1JGMoney: TcxGridDBColumn;
    v1TMoney: 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;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    V1PrtCode: TcxGridDBBandedColumn;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    V1PRTOrderQty: TcxGridDBBandedColumn;
    V1PRTPrice: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    V1Huilv: TcxGridDBBandedColumn;
    V1RMBMoney: TcxGridDBBandedColumn;
    V1PriceUnit: TcxGridDBBandedColumn;
    V1OrderUnit: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    v1TRMBMoney: TcxGridDBColumn;
    v1Column1: 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 TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerNoChange(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TEditClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1,Canshu2:string;
    { Public declarations }
  end;

var
  frmContractCBYG: TfrmContractCBYG;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FjList_RZ,U_ConInPut_CBYG;  // ,U_ConInPutXF,U_SKCR_CX

{$R *.dfm}
procedure TfrmContractCBYG.FormDestroy(Sender: TObject);
begin
  frmContractCBYG:=nil;
end;

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

procedure TfrmContractCBYG.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 TfrmContractCBYG.TBCloseClick(Sender: TObject);
begin
  if FCloth<>1 then
    WriteCxGrid('订单合同列表',Tv1,'生产指示单管理')
  else
    WriteCxGrid('订单合同列表选择',Tv1,'生产指示单管理');
  Close;
end;

procedure TfrmContractCBYG.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      SQL.Add(',TMoney=(select SUM(X.ConMXMoney) from JYOrderCon_Sub_JG X where X.MainId=A.MainId)');
      SQL.Add(',TRMBMoney=(select SUM(X.ConMXMoney*X.Huilv) from JYOrderCon_Sub_JG X where X.MainId=A.MainId)');
      sql.Add(' from JYOrderCon_Main_JG A');
      SQL.Add(' where  A.FillTime>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
      SQL.Add(' and A.FillTime<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      if canshu1<>'高权限' then
      sql.Add(' and  A.Filler='''+Trim(DName)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmContractCBYG.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;

procedure TfrmContractCBYG.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'成本预估登记表');
end;

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

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

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

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

procedure TfrmContractCBYG.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 TfrmContractCBYG.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 TfrmContractCBYG.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 TfrmContractCBYG.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmContractCBYG.TBPrintClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  Button1.Click;
end;

procedure TfrmContractCBYG.Button1Click(Sender: TObject);
var
   fPrintFile: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.*');
    sql.Add(' from JYOrderCon_Main_JG A');
    sql.Add(' inner join JYOrderCon_Sub_JG B on B.Mainid=A.Mainid');
    SQL.Add(' where A.MainId='''+trim(Order_Main.fieldbyname('MainId').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 A.* ');
    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.MainId='''+Trim(Order_Main.fieldbyname('MainId').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 A.* ');
    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 A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.add(' and A.CBType=''加工''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_MLJG);
  SInitCDSData20(ADOQuery1,Order_MLJG);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.* ');
    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.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.add(' and A.CBType=''辅料''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_FL);
  SInitCDSData20(ADOQuery1,Order_FL);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.* ');
    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 A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.add(' and A.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 TfrmContractCBYG.TEditClick(Sender: TObject);
begin
  try
    frmConInPut_CBYG:=TfrmConInPut_CBYG.Create(Application);
    with frmConInPut_CBYG do
    begin
      PState:=0;
      FMainid:='';
      canshu1:=self.canshu1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut_CBYG.Free;
  end;
end;

procedure TfrmContractCBYG.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmConInPut_CBYG:=TfrmConInPut_CBYG.Create(Application);
    with frmConInPut_CBYG do
    begin
      PState:=1;
      FMainid:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      YGNo.Enabled:=False;
      FZT:='1';
      canshu1:=self.canshu1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmConInPut_CBYG.Free;
  end;
end;

procedure TfrmContractCBYG.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from JYOrdercon_Main_JG where MainId='''+trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add('delete from JYOrdercon_Sub_JG where MainId='''+trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    sql.Add('delete from JYOrderCon_YGCB where MainId='''+trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Order_Main.Delete;
end;

procedure TfrmContractCBYG.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
//  if Order_Main.IsEmpty then Exit;
  with ADOQuerySub do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select A.* ');
    SQL.Add(',RMBMoney=A.ConMXMoney*A.Huilv');
    SQL.Add('from JYOrderCon_Sub_JG A where A.MainId='''+trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuerySub,order_Sub);
  SInitCDSData20(ADOQuerySub,order_Sub);
end;

end.
