unit U_ProductOrderNewListJDmx;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxCheckBox, cxLookAndFeelPainters, cxButtons, cxLookAndFeels,
  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, cxGridBandedTableView, cxGridDBBandedTableView;

type
  TfrmProductOrderNewListJDmx = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: 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;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label2: TLabel;
    JGFactoryName: TEdit;
    Label8: TLabel;
    PRTCodeName: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    tNochk: TToolButton;
    v1ordderNote1: 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;
    PrtColor: TEdit;
    Label7: TLabel;
    orderType: TComboBox;
    Label24: TLabel;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1RK_Qty: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    ToolButton4: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton5: TToolButton;
    v1Column22: TcxGridDBColumn;
    Panel2: TPanel;
    Label12: TLabel;
    MNote: TMemo;
    FileName: TcxButton;
    cxButton1: TcxButton;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column23: TcxGridDBColumn;
    Panel3: TPanel;
    Label10: TLabel;
    cxButton2: TcxButton;
    cxButton3: TcxButton;
    fen1: TcxCurrencyEdit;
    fen2: TcxCurrencyEdit;
    Label14: TLabel;
    fen3: TcxCurrencyEdit;
    Label15: TLabel;
    ToolButton6: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column1: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    IdFTP1: TIdFTP;
    Panel5: TPanel;
    Label16: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    RMDB_Sub: TRMDBDataSet;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    v1Column39: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    DBTv1: TcxGridDBBandedTableView;
    DBTv1Column1: TcxGridDBBandedColumn;
    DBTv1Column2: TcxGridDBBandedColumn;
    DBTv1Column3: TcxGridDBBandedColumn;
    DBTv1Column4: TcxGridDBBandedColumn;
    DBTv1Column5: TcxGridDBBandedColumn;
    DBTv1Column6: TcxGridDBBandedColumn;
    DBTv1Column7: TcxGridDBBandedColumn;
    DBTv1Column8: TcxGridDBBandedColumn;
    DBTv1Column9: TcxGridDBBandedColumn;
    DBTv1Column10: TcxGridDBBandedColumn;
    DBTv1Column11: TcxGridDBBandedColumn;
    DBTv1Column12: TcxGridDBBandedColumn;
    DBTv1Column13: TcxGridDBBandedColumn;
    DBTv1Column14: TcxGridDBBandedColumn;
    DBTv1Column15: TcxGridDBBandedColumn;
    DBTv1Column16: TcxGridDBBandedColumn;
    DBTv1Column17: TcxGridDBBandedColumn;
    DBTv1Column18: TcxGridDBBandedColumn;
    DBTv1Column19: TcxGridDBBandedColumn;
    DBTv1Column20: TcxGridDBBandedColumn;
    DBTv1Column21: TcxGridDBBandedColumn;
    DBTv1Column22: TcxGridDBBandedColumn;
    DBTv1Column23: TcxGridDBBandedColumn;
    DBTv1Column24: TcxGridDBBandedColumn;
    DBTv1Column25: TcxGridDBBandedColumn;
    Label4: TLabel;
    QCOrderNo: TEdit;
    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 TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure tNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FileNameClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxButton3Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure PRTCodeNameChange(Sender: TObject);
    procedure QCOrderNoKeyPress(Sender: TObject; var Key: Char);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure SetStatus();
    procedure InitSub();
    procedure InitImage(fwbid: string);
        { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmProductOrderNewListJDmx: TfrmProductOrderNewListJDmx;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun; //, U_OrderJD

{$R *.dfm}

procedure TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(',hcQty=(select sum(Qty) from CK_SXPB_CR X where X.ORDSubIdrK=A.subID and X.CKname=''待检布'' and CRQtyFlag=1 and CRType=''加工完成'')');
    sql.Add(',JYQty=(select sum(MJLen) from WFB_MJJY X where X.subID=A.subID )');
    sql.Add(',rkQty=(select sum(Qty) from CK_SXPB_CR X where A.subID=X.ORDSubIdRK and X.CRflag=''入库'' and X.CKName=''成品'' and CRType in (''加工完成'',''采购入库''))');
    sql.Add(',ckQty=(select sum(Qty) from CK_SXPB_CR X where A.subID=X.ORDSubIdCK and X.CRflag=''出库'' and X.CRType=''销售出库'' and X.CKName=''成品'')');
    sql.Add(' from JYOrder_sub A');
    sql.Add('where mainID =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  ADOQuerySub.EnableControls;
end;

procedure TfrmProductOrderNewListJDmx.SetStatus();
begin
{  Tchk.Visible:=false;
  tNochk.Visible:=false;
  TBEdit.Enabled:=false;
  TBDel.Enabled:=false;
  if Trim(fDParameters1)='高权限' then
  begin

  case  cxTabControl1.TabIndex of
    0:begin
        Tchk.Visible:=true;
        tNochk.Visible:=false;
        TBEdit.Enabled:=true;
        TBDel.Enabled:=true;
      end;
    1:begin
        Tchk.Visible:=false;
        tNochk.Visible:=true;
      end;
    2:begin
        Tchk.Visible:=false;
        tNochk.Visible:=false;
      end;
  end;
  end
  else
  begin
  case  cxTabControl1.TabIndex of
    0:begin

        TBEdit.Enabled:=true;
        TBDel.Enabled:=true;
      end;
    1:begin

      end;
    2:begin

      end;
  end;
  end; }
end;

procedure TfrmProductOrderNewListJDmx.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewListJDmx := nil;
end;

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

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

procedure TfrmProductOrderNewListJDmx.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption + tv2.Name + '2', Tv2, '生产指示单管理11');
end;

procedure TfrmProductOrderNewListJDmx.InitGrid();
var
  strwhere: string;
begin                                                    
//  strwhere := '';
//  if trim(canshu1) <> '高权限' then
//  begin
//    strwhere := ' and A.OrdPerson2=' + quotedstr(trim(DName));
//  end;
//   strwhere := strwhere +' and PRTCodeName=' + quotedstr(trim(PRTCodeName.text));
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      if Trim(QCOrderNo.text) = '' then
      begin
        sql.add(' exec P_View_Order_JD_CS_CS_copy1  ');
      end
      else
      begin
        sql.add(' exec P_View_Order_JD_CS_CS  ');
      end;
      sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
      sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      sql.add(',@WSql=' + quotedstr(trim(PRTCodeName.text)));
      sql.add(',@RKorderno=' + quotedstr(trim(QCOrderNo.text)));
      sql.Add(',@flag=''''');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
//    TBFind.Click;
  end;
end;

procedure TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.InitForm();
begin
  readCxGrid(self.Caption + tv2.Name + '2', Tv2, '生产指示单管理11');
  if FCloth = 1 then
  begin
    v1Column4.Visible := True;
  end
  else
  begin
    v1Column4.Visible := False;
  end;
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);

//  if trim(canshu1) <> '高权限' then
//  begin
//    v1JH_SL.Visible := false;
//    v1JH_SL.Hidden := true;
//  end
//  else
//  begin
//    v1JH_SL.Visible := true;
//    v1JH_SL.Hidden := false;
//  end;
  InitGrid();
end;

procedure TfrmProductOrderNewListJDmx.TBFindClick(Sender: TObject);
var
  strwhere: string;
begin
//  if ADOQueryMain.Active = False then
//    Exit;
//  strwhere := SGetFilters(Panel1, 1, 2);
//
//  SDofilter(ADOQueryMain, strwhere);
//  SCreateCDS20(ADOQueryMain, Order_Main);
//  SInitCDSData20(ADOQueryMain, Order_Main);
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  end;
end;

procedure TfrmProductOrderNewListJDmx.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;  }
  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('OrderNo').AsString);
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderNewListJDmx.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 Contract_Cloth_LL A where exists (select * from JYOrder_Sub B where B.SubId=A.OrdSubId  ');
    SQL.Add(' and B.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已领料不能删除数据!','提示',0);
    Exit;
  end; }
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已回仓不能删除数据!','提示',0);
    Exit;
  end;              }
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    TBRafresh.Click;
  end;
end;

function TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, '生产指示单列表');
end;

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

//  Panel5.Visible:=true;
end;

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

procedure TfrmProductOrderNewListJDmx.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 0;
      FMainId := '';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;

end;

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

procedure TfrmProductOrderNewListJDmx.FormShow(Sender: TObject);
begin
  SetStatus();

  canshu1 := trim(DParameters1);
  InitForm();
end;

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

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

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

procedure TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.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 TfrmProductOrderNewListJDmx.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.Enabled := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderNewListJDmx.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);
      if ShowModal = 1 then
      begin

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

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

procedure TfrmProductOrderNewListJDmx.tchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_main ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      open;
      edit;
      fieldbyname('status').Value := '1';
      post;
    end;
    application.MessageBox('业务审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('业务审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewListJDmx.tNochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrder_sub ');
    sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    sql.Add('and  isnull(substatus,'''')>''0'' ');
    open;
    if not ADOQueryCmd.IsEmpty then
    begin
      application.MessageBox('此单已经配货， 不能撤销审核！', '提示信息', 0);
      exit;
    end;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_main ');
      sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      open;
      edit;
      fieldbyname('status').Value := '0';
      post;
    end;
    application.MessageBox('审核撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewListJDmx.cxTabControl1Change(Sender: TObject);
begin
  application.ProcessMessages;
  initGrid();
end;

procedure TfrmProductOrderNewListJDmx.OrderNoKeyPress(Sender: TObject; var Key: Char);
var
  strwhere: string;
begin

  if Key = #13 then
  begin
    if Length(Trim(OrderNo.Text)) < 4 then
      Exit;
    try
      strwhere := ' and A.OrderNo like ''' + '%' + Trim(OrderNo.Text) + '%' + '''';
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
//        sql.add(' exec P_View_order_Jd');
//        sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
//        sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
//        sql.add(',@WSql=' + quotedstr(strwhere));
//        sql.Add(',@flag=''7''');
        sql.add(' exec P_View_Order_JD_CS_CS_copy1  '); //P_View_order_Jd
        sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
        sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
        sql.add(',@WSql=' + quotedstr(trim(PRTCodeName.text)));
        sql.Add(',@RKorderno=''''');
        sql.Add(',@flag=' + quotedstr(strwhere));
//        Showmessage(sql.text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);

    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewListJDmx.v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('RK_KGQty').Index] > AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('HC_Qty').Index] * 1.05 then
  begin
    ACanvas.Brush.Color := Clred;
  end;
end;

procedure TfrmProductOrderNewListJDmx.ToolButton4Click(Sender: TObject);
begin
//  if Order_Main.IsEmpty then
//    Exit;
//  try
//    frmOrderJD := TfrmOrderJD.Create(Application);
//    with frmOrderJD do
//    begin
//
//      OrderNo.Text := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
//      if ShowModal = 1 then
//      begin
//
//      end;
//    end;
//  finally
//    frmOrderJD.Free;
//  end;
end;

procedure TfrmProductOrderNewListJDmx.cxButton1Click(Sender: TObject);
begin
  Panel2.Visible := false;
end;

procedure TfrmProductOrderNewListJDmx.ToolButton5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  MNote.Text := Order_Main.fieldbyname('orddefnote15').AsString;
  Panel2.Left := (self.Width - Panel2.Width) div 2;
  Panel2.Visible := true;
end;

procedure TfrmProductOrderNewListJDmx.FileNameClick(Sender: TObject);
begin
  try
    with adoqueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('update JYOrder_main SET orddefnote15=' + quotedstr(trim(MNote.Text)));
      sql.Add('where mainID=''' + trim(Order_Main.fieldbyname('mainID').asstring) + '''');
      execsql;
    end;
    Order_Main.Edit;
    Order_Main.FieldByName('orddefnote15').Value := trim(MNote.Text);
    Order_Main.Post;
    tv2.Controller.EditingController.ShowEdit();
    Application.MessageBox('保存成功！', '提示信息');
    Panel2.Visible := false;
  except
    Application.MessageBox('保存失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmProductOrderNewListJDmx.cxButton3Click(Sender: TObject);
begin
  Panel3.Visible := false;
end;

procedure TfrmProductOrderNewListJDmx.cxButton2Click(Sender: TObject);
begin
  if strtointdef(trim(fen1.Text), 0) > 100 then
  begin
    application.MessageBox('进度分不能大于100', '提示信息', MB_Iconerror);
    exit;
  end;
  if strtointdef(trim(fen2.Text), 0) > 100 then
  begin
    application.MessageBox('质量分不能大于100', '提示信息', MB_Iconerror);
    exit;
  end;
  if strtointdef(trim(fen3.Text), 0) > 100 then
  begin
    application.MessageBox('损耗分不能大于100', '提示信息', MB_Iconerror);
    exit;
  end;
  try
    with adoqueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('update JYOrder_main SET fen1=' + quotedstr(trim(fen1.Text)));
      sql.Add(',fen2=' + quotedstr(trim(fen2.Text)));
      sql.Add(',fen3=' + quotedstr(trim(fen3.Text)));
      sql.Add('where mainID=''' + trim(Order_Main.fieldbyname('mainID').asstring) + '''');
      execsql;
    end;
    tv2.Controller.EditingController.ShowEdit();
    Application.MessageBox('保存成功！', '提示信息');
    Panel3.Visible := false;
    initgrid();
  except
    Application.MessageBox('保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderNewListJDmx.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  fen1.Text := Order_Main.fieldbyname('fen1').AsString;
  fen2.Text := Order_Main.fieldbyname('fen2').AsString;
  fen3.Text := Order_Main.fieldbyname('fen3').AsString;
  Panel3.Left := (self.Width - Panel3.Width) div 2;
  Panel3.Visible := true;
end;

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

procedure TfrmProductOrderNewListJDmx.Picture4DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
  fFlileFlag: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
  if Picture4.Picture.Height = 0 then
    exit;
  fFlileFlag := UserDataFlag + 'HX';
  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 TfrmProductOrderNewListJDmx.Button1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;

  LBName := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Print_Order       ');
    sql.add('@mainID=' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmProductOrderNewListJDmx.Image2Click(Sender: TObject);
begin
  Panel5.Visible := false;
end;

procedure TfrmProductOrderNewListJDmx.PRTCodeNameChange(Sender: TObject);
var
  strwhere: string;
begin

  if Length(Trim(PRTCodeName.Text)) < 2 then
    Exit;
  try
    strwhere := ' and B.PRTCodeName like ''' + '%' + Trim(PRTCodeName.Text) + '%' + '''';
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
//      sql.add(' exec P_View_order_Jd');
////        sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
////        sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
//      sql.add(' @begdate=''2000-01-01'' ');
//      sql.add(',@enddate=''2050-01-01'' ');
//      sql.add(',@WSql=' + quotedstr(strwhere));
//      sql.Add(',@flag=''7''');
      if Trim(QCOrderNo.text) = '' then
      begin
        sql.add(' exec P_View_Order_JD_CS_CS_copy1  ');
      end
      else
      begin
        sql.add(' exec P_View_Order_JD_CS_CS  ');
      end;

      sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
      sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      sql.add(',@RKorderno=' + quotedstr(trim(QCOrderNo.text)));
      sql.add(',@WSql=' + quotedstr(trim(PRTCodeName.text)));
      sql.Add(',@flag=''''');

//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmProductOrderNewListJDmx.QCOrderNoKeyPress(Sender: TObject; var Key: Char);
var
  strwhere: string;
begin

  if Key = #13 then
  begin
    if Length(Trim(QCOrderNo.Text)) < 4 then
      Exit;
    try
//      strwhere := ' and A.QCOrderNo like ''' + '%' + Trim(QCOrderNo.Text) + '%' + '''';
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add(' exec P_View_Order_JD_CS_CS  '); //P_View_order_Jd
        sql.add(' @begdate=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + ''' ');
        sql.add(',@enddate=''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
        sql.add(',@WSql=' + quotedstr(trim(PRTCodeName.text)));
        sql.add(',@RKorderno=' + quotedstr(trim(QCOrderNo.text)));
        sql.Add(',@flag=''''');
//        ShowMessage(sql.text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

end.

