unit U_ProductOrderNewList_New;

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;

type
  TfrmProductOrderNewList_New = 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;
    MLOrderNo: TEdit;
    TBExport: TToolButton;
    v1MLOrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1MLConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1KHName: TcxGridDBColumn;
    Label4: TLabel;
    KHName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label9: TLabel;
    MLConNo: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    v1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    MLCodeName: TEdit;
    Label5: TLabel;
    N1: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    DataSource3: TDataSource;
    Panel2: TPanel;
    Order_Print: TClientDataSet;
    Label7: TLabel;
    status: TComboBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1MLColor: TcxGridDBColumn;
    v1MLColorNo: TcxGridDBColumn;
    v1MLHX: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1MLCodeName: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    DataSource4: TDataSource;
    Ord_GX: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Button2: TButton;
    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 MLOrderNoChange(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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2Click(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitSub();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1,canshu2,canshu3:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_New: TfrmProductOrderNewList_New;

implementation
uses
  U_DataLink,U_Fun, U_FjList_RZ,U_OrderInPut_New,U_KHList_CX;
{$R *.dfm}

procedure TfrmProductOrderNewList_New.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  tbedit.Visible:=false;
  tbdel.Visible:=false;
  TBPrint.Visible:=false;
  v1Ssel.Visible:=false;
  if ((Trim(canshu1)='高权限') and (trim(canshu2)=''))  then
  begin
    tbedit.Visible:=true;
    tbdel.Visible:=true;
    tchk.Visible:=true;
    TBPrint.Visible:=true;
  end
  else
  if (Trim(canshu1)='高权限') and (trim(canshu2)='查询')  then
  begin
    tbadd.Visible:=false;
  end
  else
  begin
    tbedit.Visible:=true;
    tbdel.Visible:=true;
    TBPrint.Visible:=true;
  end;
  if Trim(canshu1)='高权限' then
  begin
    v1Ssel.Visible:=true;
  end;
end;

procedure TfrmProductOrderNewList_New.InitSub();
begin
  IF Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from ML_OrderSubidNew ');
    sql.Add('where NewMLID ='+quotedstr((Order_Main.fieldbyname('NewMLID').AsString)));
    open;
  end;
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from ML_GX ');
    sql.Add('where NewMLID ='+quotedstr((Order_Main.fieldbyname('NewMLID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQuery1,Ord_GX);
  SInitCDSData20(ADOQuery1,Ord_GX);
end;

procedure TfrmProductOrderNewList_New.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_New:=nil;
end;

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

procedure TfrmProductOrderNewList_New.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=trim(DParameters1);
  canshu2:=trim(DdataBase);
end;

procedure TfrmProductOrderNewList_New.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('生产指示单新',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_New.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('select A.* ');
      SQL.Add(',ZQty=(select Sum(B.Qty) from ML_OrderSubidNew B where B.NewMLID=A.NewMLID) ');
      sql.Add('from ML_OrderMainNew A ');
      sql.Add('where A.OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
      sql.Add('and A.OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      if canshu1<>'高权限' then
      begin
        SQL.Add(' and filler='''+trim(DName)+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmProductOrderNewList_New.InitForm();
begin
  ReadCxGrid('生产指示单新',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-15;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  IF trim(DdataBase)='查询' then
  begin
    TBadd.Visible:=false;
    tbedit.Visible:=false;
    ToolButton2.Visible:=false;
    tbdel.Visible:=false;
  end;
  //SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderNewList_New.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_New.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if ((Trim(DParameters1)<>'高权限') and (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString))<>UpperCase(Trim(DName)))) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  try
    frmOrderInPut_New:=TfrmOrderInPut_New.Create(Application);
    with frmOrderInPut_New do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('NewMLID').AsString);
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_New.Free;
  end;
end;

procedure TfrmProductOrderNewList_New.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if (UpperCase(Trim(Order_Main.fieldbyname('Filler').AsString))<>UpperCase(Trim(DName))) and (Trim(canshu1)<>'高权限')  then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmProductOrderNewList_New.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_OrderMainNew where NewMLID='''+Trim(Order_Main.fieldbyname('NewMLID').AsString)+'''');
      sql.Add('delete ML_OrderSubidNew where NewMLID='''+Trim(Order_Main.fieldbyname('NewMLID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderNewList_New.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'面料染色计划单');
end;

procedure TfrmProductOrderNewList_New.TBPrintClick(Sender: TObject);
begin
  //Panel4.Left:=(self.Width - Panel4.Width) div 2;
  //Panel4.Visible:=true;
  Button1.Click;
end;

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

procedure TfrmProductOrderNewList_New.TBAddClick(Sender: TObject);
var
  maxno:string;
begin              
  try
    frmOrderInPut_New:=TfrmOrderInPut_New.Create(Application);
    with frmOrderInPut_New do
    begin
      PState:=0;
      FMainId:='';
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmOrderInPut_New.Free;
  end;
end;

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

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

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

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

procedure TfrmProductOrderNewList_New.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,false);
end;

procedure TfrmProductOrderNewList_New.N2Click(Sender: TObject);
begin
 SelOKNo(Order_Main,True);
end;

procedure TfrmProductOrderNewList_New.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut_New:=TfrmOrderInPut_New.Create(Application);
    with frmOrderInPut_New do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('NewMLID').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_New.Free;
  end;
end;

procedure TfrmProductOrderNewList_New.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut_New:=TfrmOrderInPut_New.Create(Application);
    with frmOrderInPut_New do
    begin
      PState:=1;
      CopyInt:=99;
      FMainId:=Trim(Self.Order_Main.fieldbyname('NewMLID').AsString);
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_New.Free;
  end;
end;

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

procedure TfrmProductOrderNewList_New.Button1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno,LBName,SYRName:string;
   i,j:Integer;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('Ssel',True,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select A.*,B.*,EWM=cast('''' as varchar(1000)) from ML_OrderMainNew A ');
    sql.Add('inner Join ML_OrderSubidNew B on A.NewMLID=B.NewMLID ');
    sql.Add('where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,Order_Print);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.add('select A.*,B.*,EWM=cast('''' as varchar(1000)) from ML_OrderMainNew A ');
          sql.Add('inner Join ML_OrderSubidNew B on A.NewMLID=B.NewMLID ');
          sql.Add('where A.NewMLID='''+Trim(Order_Main.fieldbyname('NewMLID').AsString)+'''');
          Open;
        end;
        SInitCDSData20(ADOQueryPrint,Order_Print);
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;
  with Order_Print do
  begin
    First;
    i:=0;
    while not eof do
    begin
      try
        Moudle:=LoadLibrary('MakeQRBarcode.dll');
        @Makebar:=GetProcAddress(Moudle,'Make');
        @Mixtext:=GetProcAddress(Moudle,'MixText');
        Txt:=Trim(Order_Print.fieldbyname('NewSubid').AsString);
        fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp'+inttostr(i)+'.bmp' ;
        if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
            CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
        if FileExists(fImagePath) then DeleteFile(fImagePath);
        Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
      except
        application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
        exit;
      end;
      Edit;
      FieldByName('EWM').Value:=Trim(fImagePath);
      i:=i+1;
      next;
    end;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) +'Report\生产指示单新.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(LBName)+'.rmf'),'提示',0);
  end;
end;

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

procedure TfrmProductOrderNewList_New.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=false then
   begin
     application.MessageBox('没有选择数据','提示');
     exit;
   end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  ML_OrderMainNew SET status=''已审核'',chker='''+trim(DName)+''',chkTime=getdate() ');
            sql.Add('where NewMLID='+quotedstr(trim(Order_Main.fieldbyname('NewMLID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
        next;
      end;
    end;
    application.MessageBox('订单审核成功！','提示信息');
    //TBRafresh.Click;
  except
   application.MessageBox('订单审核失败！','提示信息',0);
  end;
end;

procedure TfrmProductOrderNewList_New.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update ML_OrderMainNew SET status=''未审核'',chker='''',chkTime=null ');
            sql.Add('where NewMLID='+quotedstr(trim(Order_Main.fieldbyname('NewMLID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
        next;
      end;
    end;
    application.MessageBox('订单撤销审核成功！','提示信息');
    //TBRafresh.Click;
  except
    application.MessageBox('订单撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmProductOrderNewList_New.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitSub();
end;

procedure TfrmProductOrderNewList_New.Button2Click(Sender: TObject);
begin
  try
    frmKHList_CX:=TfrmKHList_CX.Create(Application);
    with frmKHList_CX do
    begin
      IF self.canshu1<>'高权限' then canshu1:='业务员';
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(CDS_HZ.fieldbyname('KHName').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmKHList_CX.Free;
  end;
end;

end.
