unit U_DyeFinishScheduleListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxPC,
  cxDropDownEdit, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu;

type
  TfrmDyeFinishScheduleListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    P_CodeName: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    RMDBPRT: TRMDBDataSet;
    Label10: TLabel;
    P_Code: TEdit;
    KHNAME: TEdit;
    Label7: TLabel;
    ADOQueryPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure tnochkClick(Sender: TObject);
  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitButton();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

var
  frmDyeFinishScheduleListSel: TfrmDyeFinishScheduleListSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_LabelList;

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

end;

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

procedure TfrmDyeFinishScheduleListSel.InitButton();
begin

end;

procedure TfrmDyeFinishScheduleListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption, Tv1, '成品生产管理');
end;

procedure TfrmDyeFinishScheduleListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*  ');
      sql.Add(',KCQty=isnull((select sum(lslen+lslen2) from CK_LS_CR X where X.CRFlag=''待出库'' and X.Y_code=B.P_code),0)');
      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
      sql.Add(' where A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
      sql.Add(' and A.OrdType=''成品计划单''');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  isnull(A.status,''0'')=''0''');
          end;
        1:
          begin
            sql.Add(' and  isnull(A.status,''0'')=''1''');
          end;

      end;
      if Trim(canshu1) = '录入' then
        sql.Add(' and A.Filler=' + quotedstr(DName));
      sql.Add('and B.qty>isnull((select sum(lslen+lslen2) from CK_LS_CR X where X.CRFlag=''待出库'' and X.Y_code=B.P_code),0)');
      Open;
    end;

    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyeFinishScheduleListSel.InitForm();
begin
  InitButton();
  ReadCxGrid(self.Caption, Tv1, '成品生产管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  SetStatus();
  InitGrid();
end;

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

function TfrmDyeFinishScheduleListSel.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete PD_Plan_Sub where SubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from PD_Plan_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete PD_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString) = '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete PD_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_PaiZhen where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_YCL_PB where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmDyeFinishScheduleListSel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

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

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

procedure TfrmDyeFinishScheduleListSel.conPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        //sql.Add('exec Cloth_QryList :MainId,:WSql');
       // Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+''''
                                             // +' and ConType=''生产'' ';
        sql.Add('select * from PD_Plan_Main A inner join CLoth_Sub B on A.Mainid=B.MainId');
        sql.Add(' where A.ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');

        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmDyeFinishScheduleListSel.ToolButton1Click(Sender: TObject);
begin
  if order_Main.IsEmpty then
    exit;
  ModalResult := 1;
end;

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

procedure TfrmDyeFinishScheduleListSel.tchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  PD_Plan_Main SET status=''1'',Chktime=getdate(),ChkCode=' + quotedstr(trim(DCode)) + ',Chker=' + quotedstr(trim(DName)));
      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 TfrmDyeFinishScheduleListSel.tnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  PD_Plan_Main SET status=''0'',Chktime=null,Chker=null,ChkCode=null ');
      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;

end.

