unit U_MLProductOrderNewList;

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, cxLookAndFeels, cxLookAndFeelPainters, 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,
  cxNavigator;

type
  TfrmMLProductOrderNewList = 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;
    v1YWY: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    JGType: TEdit;
    Label2: TLabel;
    v1GenDan: TcxGridDBColumn;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    GenDan: TEdit;
    Label5: TLabel;
    v1JGDanWei: TcxGridDBColumn;
    YWY: TEdit;
    Label6: TLabel;
    N1: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    DataSource3: TDataSource;
    Panel2: TPanel;
    Order_Print: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1MLCode: TcxGridDBColumn;
    v1MLCodeName: TcxGridDBColumn;
    v1MLColor: TcxGridDBColumn;
    v1MLColorNo: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1TrueSe: TcxGridDBColumn;
    v1MXNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1SHName: TcxGridDBColumn;
    v1status: TcxGridDBColumn;
    Label7: TLabel;
    status: TComboBox;
    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);
  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
  frmMLProductOrderNewList: TfrmMLProductOrderNewList;

implementation
uses
  U_DataLink,U_MLOrderInPut,U_Fun, U_FjList_RZ,U_MLOrderInPut_JHLR;
{$R *.dfm}

procedure TfrmMLProductOrderNewList.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 TfrmMLProductOrderNewList.InitSub();
begin
  IF Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from ML_OrderSubid ');
    sql.Add('where MLID ='+quotedstr((Order_Main.fieldbyname('MLID').AsString)));
    open;
  end;
end;

procedure TfrmMLProductOrderNewList.FormDestroy(Sender: TObject);
begin
  frmMLProductOrderNewList:=nil;
end;

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

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

procedure TfrmMLProductOrderNewList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('面料染色计划单',Tv1,'生产指示单管理');
end;

procedure TfrmMLProductOrderNewList.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_OrderSubid B where B.MLID=A.MLID) ');
      sql.Add('from ML_OrderMain 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 (A.YWY='''+trim(DName)+''' or filler='''+trim(DName)+''')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmMLProductOrderNewList.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 TfrmMLProductOrderNewList.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 TfrmMLProductOrderNewList.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
    frmMLOrderInPut_JHLR:=TfrmMLOrderInPut_JHLR.Create(Application);
    with frmMLOrderInPut_JHLR do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MLID').AsString);
      canshu1:=self.canshu1;
      canshu2:=self.canshu2;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmMLOrderInPut_JHLR.Free;
  end;
end;

procedure TfrmMLProductOrderNewList.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 TfrmMLProductOrderNewList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_OrderMain where MLID='''+Trim(Order_Main.fieldbyname('MLID').AsString)+'''');
      sql.Add('delete ML_OrderSubid where MLID='''+Trim(Order_Main.fieldbyname('MLID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

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

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

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

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

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

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

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

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

procedure TfrmMLProductOrderNewList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmMLOrderInPut_JHLR:=TfrmMLOrderInPut_JHLR.Create(Application);
    with frmMLOrderInPut_JHLR do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MLID').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
    frmMLOrderInPut_JHLR.Free;
  end;
end;

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

      end;
    end;
  finally
    frmMLOrderInPut_JHLR.Free;
  end;
end;

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

procedure TfrmMLProductOrderNewList.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.* from ML_OrderMain A ');
    sql.Add('inner Join ML_OrderSubid B on A.MLID=B.MLID ');
    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.* from ML_OrderMain A ');
          sql.Add('inner Join ML_OrderSubid B on A.MLID=B.MLID ');
          sql.Add('where A.MLID='''+Trim(Order_Main.fieldbyname('MLID').AsString)+'''');
          Open;
        end;
        SInitCDSData20(ADOQueryPrint,Order_Print);
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('MLID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.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;
  if RadioGroup1.ItemIndex=0 then
  fPrintFile:= ExtractFilePath(Application.ExeName) +'Report\加工合同.rmf'
  else
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) +'Report\染色指示单.rmf';
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    //RMVariables['SYRName']:=Trim(SYRName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(LBName)+'.rmf'),'提示',0);
  end;
end;

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

procedure TfrmMLProductOrderNewList.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_OrderMain SET status=''已审核'',chker='''+trim(DName)+''',chkTime=getdate() ');
              sql.Add('where MLID='+quotedstr(trim(Order_Main.fieldbyname('MLID').AsString)));
              execsql;
            end;
            Order_Main.Delete;
          end
          else
          next;
        end;
      end;
    application.MessageBox('订单审核成功！','提示信息');
    //TBRafresh.Click;
  except
   application.MessageBox('订单审核失败！','提示信息',0);
  end;
end;

procedure TfrmMLProductOrderNewList.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_OrderMain SET status=''未审核'',chker='''',chkTime=null ');
            sql.Add('where MLID='+quotedstr(trim(Order_Main.fieldbyname('MLID').AsString)));
            execsql;
          end;
          Order_Main.Delete;
        end
        else
        next;
      end;
    end;
    application.MessageBox('订单撤销审核成功！','提示信息');
    //TBRafresh.Click;
  except
    application.MessageBox('订单撤销审核失败！','提示信息',0);
  end;
end;

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

end.
