unit U_ProductOrderNewList_OrdJD;

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

type
  TfrmProductOrderNewList_OrdJD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label2: TLabel;
    JGFactoryName: TEdit;
    Label5: TLabel;
    OrdPerson1: TEdit;
    Label8: TLabel;
    PRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1Column8: TcxGridDBColumn;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1EditTime: TcxGridDBColumn;
    v1ordderNote1: TcxGridDBColumn;
    v1SordQtY1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    Label6: TLabel;
    ISPH: TComboBox;
    v1ISPH: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    PrtColor: TEdit;
    Label7: TLabel;
    orderType: TComboBox;
    Label24: TLabel;
    v1Column11: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    SYRName: TEdit;
    batchNo: TEdit;
    Label10: TLabel;
    Label12: TLabel;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    PRTKZ: TEdit;
    Label13: TLabel;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    defstr7: TEdit;
    Label14: TLabel;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    C_CodeNameZW: TEdit;
    Label15: TLabel;
    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 Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v1Column23PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column24PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column25PropertiesEditValueChanged(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure SetStatus();
        { Private declarations }
  public

    fDParameters1:string;
    forderType:string;
    fsyrName:string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_OrdJD: TfrmProductOrderNewList_OrdJD;

implementation
uses
  U_DataLink,U_Fun, U_OrderInPut;

{$R *.dfm}
procedure TfrmProductOrderNewList_OrdJD.SetStatus();
begin

end;

procedure TfrmProductOrderNewList_OrdJD.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_OrdJD:=nil;
end;

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

procedure TfrmProductOrderNewList_OrdJD.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderNewList_OrdJD.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_OrdJD.InitGrid();
var
  strwhere:string;
begin
  strwhere:='';

  strwhere:=strwhere+' and  B.orderType='''+Trim(forderType)+'''';

  If trim(fsyrName)<>'' then
  begin
     strwhere:=strwhere+' and  B.syrName='''+Trim(fsyrName)+'''';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('exec P_View_order_Jd :begdate,:enddate,:WSql') ;
      parameters.ParamByName('WSql').Value:=strwhere;
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmProductOrderNewList_OrdJD.InitForm();
begin

  readCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');

  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);

  //InitGrid();
end;

procedure TfrmProductOrderNewList_OrdJD.TBFindClick(Sender: TObject);
var
  strwhere :string;
begin
  if ADOQueryMain.Active=False then Exit;
  strwhere:=SGetFilters(Panel1,1,2);
  IF CheckBox1.Checked then
  begin
    IF trim(strwhere)='' then
    strwhere:=' CustomerNoName<>''广东门市部'' '
    else
    strwhere:=strwhere+' and CustomerNoName<>''广东门市部'' ';
  end;

  SDofilter(ADOQueryMain,strwhere);
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmProductOrderNewList_OrdJD.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').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 TfrmProductOrderNewList_OrdJD.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'生产指示单列表');
end;

procedure TfrmProductOrderNewList_OrdJD.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno,PRTCodeName,funit:string;
   i,j:Integer;
begin
  if Order_Main.IsEmpty then Exit;
   fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单进度表.rmf' ;

  if FileExists(fPrintFile) then
  begin
    RMVariables['begdate']:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+' - '+FormatDateTime('yyyy-MM-dd',enddate.DateTime);
    RMVariables['dtxz']:=formatdateTime('yyyy-MM-dd',Now);
    RMVariables['zdr']:=Trim(dName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report生产指示单进度表.rmf'),'提示',0);
  end;

end;

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

procedure TfrmProductOrderNewList_OrdJD.OrderNoChange(Sender: TObject);
begin
 TBFind.Click;
end;

procedure TfrmProductOrderNewList_OrdJD.FormShow(Sender: TObject);
begin
  IF forderType='剪样单' then
  begin
    v1Column6.Visible:=false;
    v1Column7.Visible:=false;
  end;
  SetStatus();
  InitForm();
end;

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

procedure TfrmProductOrderNewList_OrdJD.CheckBox1Click(Sender: TObject);
begin
   TBFind.Click;
end;

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

procedure TfrmProductOrderNewList_OrdJD.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 TfrmProductOrderNewList_OrdJD.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 TfrmProductOrderNewList_OrdJD.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 TfrmProductOrderNewList_OrdJD.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 TfrmProductOrderNewList_OrdJD.cxTabControl1Change(Sender: TObject);
begin
  application.ProcessMessages;
  initGrid();
end;

procedure TfrmProductOrderNewList_OrdJD.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  IF  AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('PH_RollNum').Index]<>AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('PRTRollNum').Index] then
  begin
    ACanvas.Brush.Color:=clSkyBlue;
  end;
end;

procedure TfrmProductOrderNewList_OrdJD.v1Column23PropertiesEditValueChanged(
  Sender: TObject);
VAR
  fieldname:string;
BEGIN
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TCXTextEdit(Sender).Text;
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update JYOrder_sub SET SOrddefstr1='''+trim(Order_Main.fieldbyName('SOrddefstr1').AsString)+''' ');
    sql.Add('where subID ='''+trim(Order_Main.fieldbyName('subID').AsString)+''' ');
    execsql;
  end;
end;

procedure TfrmProductOrderNewList_OrdJD.v1Column24PropertiesEditValueChanged(
  Sender: TObject);
VAR
  fieldname:string;
BEGIN
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TCXTextEdit(Sender).Text;
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update JYOrder_sub SET SOrddefstr2='''+trim(Order_Main.fieldbyName('SOrddefstr2').AsString)+''' ');
    sql.Add('where subID ='''+trim(Order_Main.fieldbyName('subID').AsString)+''' ');
    execsql;
  end;

end;

procedure TfrmProductOrderNewList_OrdJD.v1Column25PropertiesEditValueChanged(
  Sender: TObject);
VAR
  fieldname:string;
BEGIN
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TCXTextEdit(Sender).Text;
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update JYOrder_sub SET SOrddefstr3='''+trim(Order_Main.fieldbyName('SOrddefstr3').AsString)+''' ');
    sql.Add('where subID ='''+trim(Order_Main.fieldbyName('subID').AsString)+''' ');
    execsql;
  end;


end;

end.
