unit U_ContractListSel;

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, cxButtonEdit,
  cxDropDownEdit, cxTextEdit, cxCurrencyEdit, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter,
  cxNavigator;

type
  TfrmContractListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CustomerNoName: TEdit;
    ConPerson1: TEdit;
    ShippMent: TEdit;
    ConNo: TEdit;
    QtyNote: TEdit;
    cxSplitter1: TcxSplitter;
    CDS_Chk: TClientDataSet;
    DS_Chk: TDataSource;
    Panel2: TPanel;
    cxGridPopupMenu3: TcxGridPopupMenu;
    CDS_Sub: TClientDataSet;
    DS_Sub: TDataSource;
    CDS_Chker: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1QianDPlace: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1KHConNO: TcxGridDBColumn;
    v1ConPerson1: TcxGridDBColumn;
    v1pichang: TcxGridDBColumn;
    v1QtyNote: TcxGridDBColumn;
    v1CustomerNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1ConType: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1ShippMent: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    v1ifFJ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1ConMoney: TcxGridDBColumn;
    v1SJFHMoney: TcxGridDBColumn;
    v1RLRMBMoney: TcxGridDBColumn;
    v1RLUSDMoney: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1PrtCode: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1PRTCodeNameEng: TcxGridDBColumn;
    v1PRTspec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v2PRTHX: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1SOrdDefStr4: TcxGridDBColumn;
    v1HXQty: TcxGridDBColumn;
    v1ColQty: TcxGridDBColumn;
    v1MSQty: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1HYPrice: TcxGridDBColumn;
    v1YJBLi: TcxGridDBColumn;
    v1YJPrice: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    v1ConMXMoney: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    v1TiaoQty: TcxGridDBColumn;
    v1ZXChiCun: TcxGridDBColumn;
    v1MXTQty: TcxGridDBColumn;
    v1MaoQty: TcxGridDBColumn;
    v1JQty: TcxGridDBColumn;
    v2XQty: TcxGridDBColumn;
    v2PKGFee: TcxGridDBColumn;
    v2MXNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Ssel: TcxGridDBColumn;
    v2StyleNo: 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 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 ToolButton3Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmContractListSel: TfrmContractListSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmContractListSel.FormDestroy(Sender: TObject);
begin
  frmContractListSel := nil;
end;

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

procedure TfrmContractListSel.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmContractListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单合同列表1', Tv1, '生产指示单管理');
  WriteCxGrid('订单合同列表选择2', Tv2, '生产指示单管理');
end;

procedure TfrmContractListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select distinct A.*,A.ConNo ConNoM ');
      sql.add(',SJFHMoney=(select Sum(B.Money) from YF_Money_CR B where B.CRType=''应收款登记'' and B.ConNO=A.ConNO) ');
      sql.add(',RLRMBMoney=(select Sum(RLMoney) from JYOrderYSK S where S.Mainid=A.Mainid and S.BZType=''￥'')');
      sql.add(',RLUSDMoney=(select Sum(RLMoney) from JYOrderYSK S where S.Mainid=A.Mainid and S.BZType=''$'')');
      SQL.Add(',PRTOrderQty=(select Sum(PRTOrderQty) from JYOrderCon_Sub B where B.MainId=A.MainId)');
      SQL.Add(',ConMoney=(select Sum(PRTOrderQty*PRTPrice) from JYOrderCon_Sub B where B.MainId=A.MainId)');
      SQL.Add(',OrderUnit=(select top 1 OrderUnit from JYOrderCon_Sub B where B.MainId=A.MainId)');
      SQL.Add(',PriceUnit=(select top 1 PriceUnit from JYOrderCon_Sub B where B.MainId=A.MainId)');
      SQL.Add(',PRTPrice=(select top 1 PRTPrice from JYOrderCon_Sub B where B.MainId=A.MainId)');
      SQL.Add(',OrderNo=(select top 1 OrderNo from JYOrder_Main B where B.ConNO=A.ConNO order by B.OrdDate desc)');
      SQL.Add(',ifFJ=(case when isnull((select Top 1 WBID from TP_File B where B.WBID=A.ConNo and B.TFType=''合同''),'''')='''' then ''否'' else ''是'' end)');
      sql.Add(' from JYOrderCon_Main A  ');
      SQL.Add('where OrdDate>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add(' and OrdDate<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      sql.Add(' and isnull(A.status,''0'')=''2''');
      if Trim(canshu1) <> '高权限' then
      begin
        sql.Add('and A.ConPerson1=''' + Trim(DName) + '''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmContractListSel.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 TfrmContractListSel.InitForm();
begin
  ReadCxGrid('订单合同列表1', Tv1, '生产指示单管理');
  ReadCxGrid('订单合同列表选择2', Tv2, '生产指示单管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 7;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmContractListSel.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 TfrmContractListSel.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) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Main 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 TfrmContractListSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

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

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

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

procedure TfrmContractListSel.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 TfrmContractListSel.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 TfrmContractListSel.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    exit;
  if CDS_Sub.Locate('Ssel', True, []) = false then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  ModalResult := 1;
end;

procedure TfrmContractListSel.CustomerNoNameChange(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 TfrmContractListSel.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(conno.Text) < 3 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add('select distinct A.*,A.ConNo ConNoM ');
        sql.add(',SJFHMoney=(select Sum(B.Money) from YF_Money_CR B where B.CRType=''应收款登记'' and B.ConNO=A.ConNO) ');
        sql.add(',RLRMBMoney=(select Sum(RLMoney) from JYOrderYSK S where S.Mainid=A.Mainid and S.BZType=''￥'')');
        sql.add(',RLUSDMoney=(select Sum(RLMoney) from JYOrderYSK S where S.Mainid=A.Mainid and S.BZType=''$'')');
        SQL.Add(',PRTOrderQty=(select Sum(PRTOrderQty) from JYOrderCon_Sub B where B.MainId=A.MainId)');
        SQL.Add(',ConMoney=(select Sum(PRTOrderQty*PRTPrice) from JYOrderCon_Sub B where B.MainId=A.MainId)');
        SQL.Add(',OrderUnit=(select top 1 OrderUnit from JYOrderCon_Sub B where B.MainId=A.MainId)');
        SQL.Add(',PriceUnit=(select top 1 PriceUnit from JYOrderCon_Sub B where B.MainId=A.MainId)');
        SQL.Add(',PRTPrice=(select top 1 PRTPrice from JYOrderCon_Sub B where B.MainId=A.MainId)');
        SQL.Add(',OrderNo=(select top 1 OrderNo from JYOrder_Main B where B.ConNO=A.ConNO)');
        SQL.Add(',ifFJ=(case when isnull((select Top 1 WBID from TP_File B where B.WBID=A.ConNo and B.TFType=''合同''),'''')='''' then ''否'' else ''是'' end)');
        sql.Add(' from JYOrderCon_Main A  ');
        SQL.Add(' where ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');
        sql.Add(' and isnull(status,'''')=''2'' ');
        if Trim(canshu1) <> '高权限' then
        begin
          sql.Add('and A.ConPerson1=''' + Trim(DName) + '''');
        end;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmContractListSel.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.FieldByName('ConFlag1').AsBoolean = False then
  begin
    if CDS_Chk.IsEmpty then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete OrdCon_Chk where CKID=''' + Trim(CDS_Chk.fieldbyname('CKID').AsString) + '''');
      ExecSQL;
    end;
    CDS_Chk.Delete;
  end;
end;

procedure TfrmContractListSel.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
var
  maxno: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrderCon_Sub ');
    sql.Add('where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

end.

