unit U_ProductOrderCost;

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, cxTextEdit;

type
  TfrmProductOrderCost = 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;
    OrderNoM: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    CustomerNoName: TEdit;
    ToolButton1: TToolButton;
    Label5: TLabel;
    Filler: TEdit;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    MPRTMF: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Panel2: TPanel;
    cxSplitter1: TcxSplitter;
    DataSource2: TDataSource;
    CDS_FK: TClientDataSet;
    DataSource3: TDataSource;
    CDS_SK: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    v1Column10: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    djrq: TcxGridDBColumn;
    tv46: TcxGridDBColumn;
    Tv47: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DataSource4: TDataSource;
    CDS_YFK: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    v4Column1: TcxGridDBColumn;
    v4Column2: TcxGridDBColumn;
    v4Column3: TcxGridDBColumn;
    v4Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: 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 OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure OrderNoMKeyPress(Sender: TObject; var Key: Char);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmProductOrderCost: TfrmProductOrderCost;

implementation
uses
  U_DataLink,U_Fun;  //U_OrderInPut,

{$R *.dfm}

procedure TfrmProductOrderCost.FormDestroy(Sender: TObject);
begin
  frmProductOrderCost:=nil;
end;

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

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

procedure TfrmProductOrderCost.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('成本核算表1',Tv1,'生产指示单管理');
  WriteCxGrid('付款登记11',Tv2,'生产指示单管理');
  WriteCxGrid('收款登记11',Tv3,'生产指示单管理');
  WriteCxGrid('应付款登记11',Tv4,'生产指示单管理');
end;

procedure TfrmProductOrderCost.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_Cost :begdate,:enddate,:WSql');
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      Parameters.ParamByName('WSql').Value:='';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmProductOrderCost.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 TfrmProductOrderCost.InitForm();
begin
  ReadCxGrid('成本核算表1',Tv1,'生产指示单管理');
  ReadCxGrid('付款登记11',Tv2,'生产指示单管理');
  ReadCxGrid('收款登记11',Tv3,'生产指示单管理');
  ReadCxGrid('应付款登记11',Tv4,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-30;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmProductOrderCost.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 TfrmProductOrderCost.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

procedure TfrmProductOrderCost.OrderNoMChange(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 TfrmProductOrderCost.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderCost.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible=False then Exit;
  ToolButton1.Click;
end;

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

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

procedure TfrmProductOrderCost.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderCost.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 TfrmProductOrderCost.ToolButton1Click(Sender: TObject);
begin
 { if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      ScrollBox2.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;  }
end;

procedure TfrmProductOrderCost.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryPrint.DisableControls;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add('  from YF_Money_CR2 A');
      sql.Add(' inner join YF_Money_KC B on A.CRID=B.CRID');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and A.CRType=''付款登记'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryPrint,CDS_FK);
    SInitCDSData20(ADOQueryPrint,CDS_FK);
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add('  from YF_Money_CR2 A');
      sql.Add(' inner join YF_Money_KC B on A.CRID=B.CRID');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and A.CRType=''收款登记'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryPrint,CDS_SK);
    SInitCDSData20(ADOQueryPrint,CDS_SK);
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add('  from YF_Money_CR2 A');
      sql.Add(' inner join YF_Money_KC B on A.CRID=B.CRID');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and A.CRType=''应付款登记'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryPrint,CDS_YFK);
    SInitCDSData20(ADOQueryPrint,CDS_YFK);
  finally
    ADOQueryPrint.EnableControls;
  end;
end;

procedure TfrmProductOrderCost.OrderNoMKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Length(Trim(OrderNoM.Text))<4 then Exit;
    fsj:='%'+Trim(OrderNoM.Text)+'%';
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' exec P_View_Cost :begdate,:enddate,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('WSql').Value:=' and A.OrderNo like '''+Trim(fsj)+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

end.
