unit U_ProductOrderNewList;

interface

uses
  Windows, Messages, SysUtils, strUtils, 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, cxContainer,
  cxImage, cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, shellAPI, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmProductOrderNewList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: 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;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label8: TLabel;
    PBFactory: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    RanFactory: TEdit;
    Label11: TLabel;
    OrdPerson2: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1OrdPerson2: TcxGridDBColumn;
    v1ZQty: TcxGridDBColumn;
    v1OrderUnit3: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1SOrddefstr2: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxSOrdDefNote1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1SYRName: TcxGridDBColumn;
    v1MPRTTeBieNote: TcxGridDBColumn;
    v1MPRTBZNote: TcxGridDBColumn;
    v1MPRTSCTeBieNote: TcxGridDBColumn;
    V2KHorderNo: TcxGridDBColumn;
    V2PRTCodeName: TcxGridDBColumn;
    V2PRTCode: TcxGridDBColumn;
    V2PRTMF: TcxGridDBColumn;
    V2PRTKZ: TcxGridDBColumn;
    TFJ: TToolButton;
    v1filler: TcxGridDBColumn;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel2: TPanel;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    Orddefstr5: TEdit;
    Label2: TLabel;
    PRTCodeName: TEdit;
    Label5: TLabel;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    V2Column10: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    V2Column11: TcxGridDBColumn;
    V2Column12: TcxGridDBColumn;
    V2Column13: TcxGridDBColumn;
    V2Column14: TcxGridDBColumn;
    Label6: TLabel;
    cxTabControl1: TcxTabControl;
    TWC: TToolButton;
    TNoWC: TToolButton;
    ToolButton4: TToolButton;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    ToolButton6: TToolButton;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: 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 TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    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 ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TFJClick(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4DblClick(Sender: TObject);
    procedure TWCClick(Sender: TObject);
    procedure TNoWCClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FMainId: string;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitSub();
    procedure InitImage(fwbid: string);
    procedure SetStatus();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmProductOrderNewList: TfrmProductOrderNewList;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_FjList, U_MJSJFX, U_ZDYHelp,
  U_SysLogOrderzsd;

{$R *.dfm}
procedure TfrmProductOrderNewList.SetStatus();
begin
  tbedit.Visible := false;
  tbdel.Visible := false;
  twc.Visible := false;
  tNOwc.Visible := false;

  case cxTabControl1.TabIndex of
    0:
      begin
        tbedit.Visible := true;
        tbdel.Visible := true;
        twc.Visible := true;
      end;
    1:
      begin
        tNOwc.Visible := true;
      end;
  end;
end;

procedure TfrmProductOrderNewList.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmProductOrderNewList.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select *,DJStatus=case when JYRoll=MaxRoll then 0 else 1 end from (');
    sql.Add('select A.*,  ');
    sql.Add('JYRoll=(select count(MJID) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID),');
    sql.Add('JYQty=(select sum(MJLen) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID),');
    sql.Add('JYJZ=(select sum(MJQty4) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID),');
    sql.Add('JYMZ=(select sum(MJMaoZ) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID),');
    sql.Add('JYCRoll=(select count(MJID) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and X.MJType=''次品''),');
    sql.Add('JYCQty=(select sum(MJLen) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and X.MJType=''次品''),');
    sql.Add('JYCJZ=(select sum(MJQty4) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and X.MJType=''次品''),');
    sql.Add('JYCMZ=(select sum(MJMaoZ) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and X.MJType=''次品''),');
    sql.Add('CKRoll=(select count(MJID) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and ckflag=''已出库'' ), ');
    sql.Add('CKQty=(select sum(MJLen) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and ckflag=''已出库''), ');
    sql.Add('CKJZ=(select sum(MJQty4) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and ckflag=''已出库''),');
    sql.Add('CKMZ=(select sum(MJMaoZ) from WFB_MJJY X where X.MainID=A.mainID and X.subID=A.subID and ckflag=''已出库''),');
    SQL.Add('MaxRoll=(select isnull(sum(maxroll),0) from (select Subid,mjstr4,max(mjxh) as maxroll from wfb_mjjy X group by mjstr4,Subid) XX where XX.subid=A.subid)');
    sql.Add('from JYOrder_sub A ');
    sql.Add('where A.mainID =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    sql.Add(')AA');
    open;
  end;
end;

procedure TfrmProductOrderNewList.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList := nil;
end;

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

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

procedure TfrmProductOrderNewList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('指示单子表列表', Tv2, '生产指示单管理1');
  WriteCxGrid('指示单列表FF', Tv1, '生产指示单管理1');
  Close;
end;

procedure TfrmProductOrderNewList.InitGrid();
var
  strwhere: string;
begin
  strwhere := '';
  if Trim(DParameters1) <> '高权限' then
    strwhere := strwhere + ' and Filler=''' + Trim(DName) + '''';

  if cxTabControl1.TabIndex < 2 then
  begin
    strwhere := strwhere + ' and  isnull(A.status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + '''';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin

      Close;
      Filtered := False;
      sql.Clear;
      sql.add('exec P_View_Order :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 := strwhere;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  tbfind.Click;
end;

procedure TfrmProductOrderNewList.InitForm();
begin

  ReadCxGrid('指示单列表FF', Tv1, '生产指示单管理1');
  ReadCxGrid('指示单子表列表', Tv2, '生产指示单管理1');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 15;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderNewList.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 TfrmProductOrderNewList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
{ if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end; }
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.clear;
//    sql.Add('select * from WFB_MJJY where Mainid=''' + trim(Order_Main.fieldbyname('MainId').AsString) + ''' ');
//    sql.add('and isnull(MJStr2,'''')=''已入库''');
//    open;
//    if not ISEmpty then
//    begin
//      application.MessageBox('已入库不能修改', '提示');
//      exit;
//    end;
//  end;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNoM').AsString);
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderNewList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
{  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;   }
  with ADOQueryTemp do
  begin
    close;
    sql.clear;
    sql.Add('select * from WFB_MJJY where Mainid=''' + trim(Order_Main.fieldbyname('MainId').AsString) + ''' ');
  //  sql.add('and isnull(MJStr2,'''')=''已入库''');
    open;
    if not ISEmpty then
    begin
      application.MessageBox('已入库不能删除', '提示');
      exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
  InitGrid();
end;

function TfrmProductOrderNewList.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) + '''');
      sql.Add('delete TP_File where WBID=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + ''' and TFType=''EWM'' ');
      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('OrderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      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.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '生产指示单列表');
end;

procedure TfrmProductOrderNewList.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
  Porderno: string;
  i, j: Integer;
begin
  Panel4.Left := (self.Width - Panel4.Width) div 2;
  Panel4.Visible := True;
end;

procedure TfrmProductOrderNewList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();

end;

procedure TfrmProductOrderNewList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 0;
      FMainId := '';
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderNewList.OrderNoChange(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 TfrmProductOrderNewList.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'HX';
  InitForm();
end;

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

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

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

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

procedure TfrmProductOrderNewList.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.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.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.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.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;
      Tv1.OptionsSelection.CellSelect := False;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderNewList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      CopyInt := 99;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderNewList.ToolButton3Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmProductOrderNewList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(OrderNo.Text) < 3 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql');
        if Trim(DParameters1) <> '高权限' then
        begin
          Parameters.ParamByName('WSql').Value := ' and Filler=''' + Trim(DName) + '''';
        end;
        begin
          Parameters.ParamByName('WSql').Value := ' and orderno like ''' + '%' + Trim(OrderNo.Text) + '%' + '''';
        end;
        Parameters.ParamByName('begdate').Value := '2014-01-01';
        Parameters.ParamByName('enddate').Value := '2064-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList.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('exec P_View_Order :begdate,:enddate,:WSql');
        if Trim(DParameters1) <> '高权限' then
        begin
          Parameters.ParamByName('WSql').Value := ' and A.Filler=''' + Trim(DName) + '''';
        end;
        begin
          Parameters.ParamByName('WSql').Value := ' and conno like ''' + '%' + Trim(conno.Text) + '%' + '''';
        end;
        Parameters.ParamByName('begdate').Value := '1899-01-01';
        Parameters.ParamByName('enddate').Value := '2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmProductOrderNewList.Button1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;
  RMDBMain.DataSet := CDS_Print;
  LBName := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.Filesother,ColorCount=(select isnull(Count(subID),0) from JYOrder_Sub where MainId=A.MainId),  ');
    sql.add('ZQty=(select sum(PRTOrderQty) from JYOrder_Sub where MainId=A.MainId),');
    sql.add('FQty=(select sum(PRTOrderQty) from JYOrder_Sub X where X.MainId=B.MainId and X.PrtCodeName=B.PrtCodeName and X.KHorderNo=B.KHorderNo),');
    sql.add('maxkhOrderNO=(select max(khOrderNO) from JYOrder_Sub X where X.MainId=A.MainId ),');
    sql.Add('HXFileOther=(select top 1 Filesother from TP_File X where X.WBID=B.HXFile and X.TFType=''HX'' )');
    sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
    sql.Add(' left join TP_File C on C.TFID=B.Subid and C.WBID=B.Mainid ');
   // sql.Add(' left join TP_File D on D.TFID=A.orderNO  ');
    sql.Add(' Where A.MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    sql.Add(' order by B.PRTCode,B.PRTCodeName,B.subID ');

    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
    RMDBMain.DataSet := nil;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + trim(fPrintFile)), '提示信息', 0);
    exit;
  end;
end;

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

procedure TfrmProductOrderNewList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmProductOrderNewList.TFJClick(Sender: TObject);
begin
  if order_Main.IsEmpty then
    exit;
  if order_Main.FieldByName('filler').asstring <> trim(DName) then
  begin
    application.MessageBox('不能操作其他人单据', '提示');
    exit;
  end;
  frmFjList := TfrmFjlist.create(self);
  with frmFjlist do
  begin
    fkeyNo := trim(order_Main.fieldbyname('Mainid').AsString);
    ftype := 'ZSDFJ';
    if showmodal = 1 then
    begin

    end;
    ;
  end;
  frmFjlist.Free;
end;

procedure TfrmProductOrderNewList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initImage(ADOQuerySub.fieldbyname('HXFile').AsString);
end;

procedure TfrmProductOrderNewList.Picture4DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
  if Picture4.Picture.Height = 0 then
    exit;
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
  fileName := ADOQuerySub.fieldbyname('hxFile').AsString;
  sFieldName := sFieldName + '\' + trim(fileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

    application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(fileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);

end;

procedure TfrmProductOrderNewList.TWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''1'' ');
      sql.Add('where mainID=' + quotedstr(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('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewList.TNoWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_Main SET status=''0'' ');
      sql.Add('where mainID=' + quotedstr(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('orderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('订单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销完成失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderNewList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try

    frmMJSJFX := TfrmMJSJFX.Create(Application);
    with frmMJSJFX do
    begin
      frmMJSJFX.OrderNo.Text := Self.Order_Main.fieldbyname('orderno').AsString;
      frmMJSJFX.OrderNo.Hint := Self.Order_Main.fieldbyname('MainId').AsString;
      if ShowModal = 1 then
      begin

      end;

    end;
  finally
    frmMJSJFX.Free;
  end;
end;

procedure TfrmProductOrderNewList.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('DJstatus').Index] = '1' then
    ACanvas.Brush.Color := clRed;
end;

procedure TfrmProductOrderNewList.ToolButton5Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCD';
      flagname := '疵点';
      fnote := True;
      V1Note.Caption := '英文名称';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderNewList.ToolButton6Click(Sender: TObject);
begin
  frmSysLogOrderzsd := TfrmSysLogOrderzsd.create(self);
  with frmSysLogOrderzsd do
  begin
    fModel := self.caption;
    frmSysLogOrderzsd.FMainId := Trim(Self.FMainId);
    showmodal;
    free;
  end;
end;

procedure TfrmProductOrderNewList.ToolButton7Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SLBName';
      flagname := '内销标签';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderNewList.ToolButton8Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'NLBName';
      flagname := '内销包标签';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

