unit U_BefChkHX;

interface

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

type
  TfrmBefChkHX = class(TForm)
    Panel1: TPanel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column2: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    V2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    orderno: TLabel;
    PRTColor: TLabel;
    FirstName: TLabel;
    PBFactory: TLabel;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ToolButton1: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    V2Column4: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    ToolButton5: TToolButton;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    CDS_PRT: TClientDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FLLID,HXUnit:String;
  end;

var
  frmBefChkHX: TfrmBefChkHX;

implementation
uses
U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmBefChkHX.FormDestroy(Sender: TObject);
begin
  frmBefChkHX:=nil;
end;

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

procedure TfrmBefChkHX.ToolButton2Click(Sender: TObject);
begin
  with ClientDataSet1 do
  begin
    Append;
    FieldByName('HXFactory').Value:=Trim(FirstName.Caption);
    FieldByName('HXDate').Value:=SGetServerDate(ADOQuery2);
    FieldByName('HXType').Value:='检前回修';
    FieldByName('HXUnit').Value:=Trim(HXUnit);
    Post;
  end;
end;

procedure TfrmBefChkHX.ToolButton3Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if Trim(ClientDataSet1.fieldbyname('HXType').AsString)<>'检前回修' then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQuery3 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('delete Contract_Cloth_BefChkHX  where HXID='''+Trim(ClientDataSet1.fieldbyname('HXID').AsString)+'''');
    sql.Add('Update Contract_Cloth_LL Set HXPS=(select isnull(sum(HXPS),0) from Contract_Cloth_BefChkHX A where A.LLID='''+Trim(FLLID)+''')');
    sql.Add(',HXQty=(select isnull(sum(HXQty),0) from Contract_Cloth_BefChkHX A where A.LLID='''+Trim(FLLID)+''')');
    sql.Add(',HXMQty=(select isnull(sum(HXMQty),0) from Contract_Cloth_BefChkHX A where A.LLID='''+Trim(FLLID)+''')');
    sql.Add(',HXUnit=(select Top 1 HXUnit from Contract_Cloth_BefChkHX A where A.LLID='''+Trim(FLLID)+''')');
    sql.Add(' where LLID='''+Trim(FLLID)+'''');
    ExecSQL;
  end;
  ClientDataSet1.Delete;

end;

procedure TfrmBefChkHX.ToolButton4Click(Sender: TObject);
var
  maxno:string;
  FSubId:String;
begin
  try
    ADOQuery3.Connection.BeginTrans;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet1.fieldbyname('HXType').AsString)='检前回修' then
        begin
          if Trim(ClientDataSet1.fieldbyname('HXID').AsString)='' then
          begin
            with ADOQuery1 do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from Contract_Cloth_LL where LLID='''+Trim(FLLID)+'''');
              Open;
            end;
            FSubId:=Trim(ADOQuery1.fieldbyname('OrdSubId').AsString);
            with ADOQuery1 do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from JYOrder_Sub_AnPai where Subid='''+Trim(FSubId)+'''');
             // sql.Add(' and GangNo='''+Trim(ClientDataSet1.fieldbyname('GangNo').AsString)+'''');
              Open;
            end;
            if ADOQuery1.IsEmpty then
            begin
              ADOQuery3.Connection.RollbackTrans;
              Application.MessageBox('未回仓不能保存回修数据!','提示',0);
              Exit;
            end;
          end;

          if Trim(ClientDataSet1.fieldbyname('HXID').AsString)='' then
          begin
            if GetLSNo(ADOQuery3,maxno,'HX','Contract_Cloth_BefChkHX',2,1)=False then
            begin
              ADOQuery3.Connection.RollbackTrans;
              Application.MessageBox('取回修最大号失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(ClientDataSet1.fieldbyname('HXID').AsString);
          end;
          with ADOQuery3 do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Contract_Cloth_BefChkHX where HXID='''+Trim(ClientDataSet1.fieldbyname('HXID').AsString)+'''');
            Open;
          end;
          with ADOQuery3 do
          begin
            if Trim(ClientDataSet1.fieldbyname('HXID').AsString)='' then
              Append
            else
              Edit;
            FieldByName('LLID').Value:=Trim(FLLID);
            FieldByName('HXID').Value:=Trim(maxno);
            SSetSaveDataCDSNew(ADOQuery3,TV2,ClientDataSet1,'Contract_Cloth_BefChkHX',2);
            Post;
          end;
        end;
        Next;
      end;
    end;
    with ADOQuery3 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('Update Contract_Cloth_BefChkHX Set HXMQty=HXQty*ZSXS where LLID='''+Trim(FLLID)+'''');
      sql.Add('Update Contract_Cloth_LL Set HXPS=(select sum(HXPS) from Contract_Cloth_BefChkHX A where A.LLID=Contract_Cloth_LL.LLID)');
      sql.Add(',HXQty=(select sum(HXQty) from Contract_Cloth_BefChkHX A where A.LLID=Contract_Cloth_LL.LLID)');
      sql.Add(',HXMQty=(select sum(HXMQty) from Contract_Cloth_BefChkHX A where A.LLID=Contract_Cloth_LL.LLID)');
      sql.Add(',HXUnit=(select Top 1 HXUnit from Contract_Cloth_BefChkHX A where A.LLID=Contract_Cloth_LL.LLID)');
      sql.Add(' where LLID='''+Trim(FLLID)+'''');
      ExecSQL;
    end;
    ADOQuery3.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQuery3.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmBefChkHX.ToolButton1Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('检前回修',TV2,'回仓管理');
end;

procedure TfrmBefChkHX.FormShow(Sender: TObject);
begin
  ReadCxGrid('检前回修',TV2,'回仓管理');
end;

procedure TfrmBefChkHX.ToolButton5Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\回修单.rmf' ;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,C.PRTColor,D.MPRTCodeName,D.MPRTSpec,D.OrderNo from Contract_Cloth_BefChkHX A');
    sql.Add(' inner join Contract_Cloth_LL B on A.LLID=B.LLID');
    sql.Add(' inner join JYOrder_Sub C on B.OrdSubId=C.SubID');
    sql.Add(' inner join JYOrder_Main D on C.MainId=D.MainId');
    sql.Add(' where A.HXID='''+Trim(ClientDataSet1.fieldbyname('HXID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_PRT);
  SInitCDSData20(ADOQuery1,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    RMVariables['DYFiller']:=Trim(DName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\回修单.rmf'),'提示',0);
  end;
end;

end.
