unit U_ClothGongYiList;

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, Menus,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmClothGongYiList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label5: TLabel;
    C_CodeName: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column9: TcxGridDBColumn;
    RMDBPRT: TRMDBDataSet;
    TBView: TToolButton;
    v1Column21: TcxGridDBColumn;
    Label10: TLabel;
    C_Code: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label1: TLabel;
    GongYiCode: TEdit;
    tbADD: TToolButton;
    tbEDIT: TToolButton;
    tbDEL: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    v1Column7: 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 TBViewClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tbADDClick(Sender: TObject);
    procedure tbEDITClick(Sender: TObject);
    procedure tbDELClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitButton();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

//var
  //frmClothSCList: TfrmClothSCList;

implementation

uses
  U_DataLink, U_ClothSCInPut, U_Fun, U_ZDYHelp, U_SCPerson, U_ClothSCInPutSC,
  U_ClothGongYi;

{$R *.dfm}

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

procedure TfrmClothGongYiList.InitButton();
begin
  if Self.canshu1 = '低权限' then
  begin
    tbADD.Visible := False;
    tbEDIT.Visible := False;
    tbDEL.Visible := False;
    N1.Visible := False;
  end;
  if Self.canshu1 = '单价修改' then
  begin
    tbADD.Visible := False;
    tbEDIT.Visible := False;
    tbDEL.Visible := False;
  end;
end;

procedure TfrmClothGongYiList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption, Tv1, '坯布生产管理');
end;

procedure TfrmClothGongYiList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      sql.Add('  ,YZConNo=cast((select distinct '' ''+ X.ConNo from Cloth_Main X inner join Cloth_Sub Y on X.MainId=Y.MainId ');
      sql.Add('  where DATEDIFF(MONTH,X.Filltime,getdate())<7 and Y.GongYiCode=B.GongYiCode for xml path('''')) as varchar(600))');
      sql.Add(' from Cloth_Main A inner join CLoth_Sub B on A.Mainid=B.MainId');
      sql.Add(' where IsGongYi=1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothGongYiList.InitForm();
begin
  InitButton();
  ReadCxGrid(self.Caption, Tv1, '坯布生产管理');
  InitGrid();
end;

procedure TfrmClothGongYiList.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 TfrmClothGongYiList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_Sub where SubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Cloth_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 Cloth_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 Cloth_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 TfrmClothGongYiList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

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

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

procedure TfrmClothGongYiList.TBViewClick(Sender: TObject);
begin
  try
    frmClothGongYi := TfrmClothGongYi.Create(Application);
    with frmClothGongYi do
    begin
      TBSave.Visible := False;
      ToolBar3.Visible := False;
      if canshu1 <> '高权限' then
        cxPageControl1.Visible := False;
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.Order_Main.fieldbyname('SubId').AsString);
      FConNo := Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
      if Self.canshu1 = '低权限' then
      begin
        ToolBar1.Visible := False;
        cxTabSheet2.Visible := False;
        cxTabSheet2.Enabled := False;
      end;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmClothGongYi.Free;
  end;
end;

procedure TfrmClothGongYiList.cxTabControl1Change(Sender: TObject);
begin
  InitButton();
  InitGrid();
end;

procedure TfrmClothGongYiList.tbADDClick(Sender: TObject);
begin
  try
    frmClothGongYi := TfrmClothGongYi.Create(Application);
    with frmClothGongYi do
    begin
      PState := 0;
      FMainId := '';
      FSubId := '';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmClothGongYi.Free;
  end;
end;

procedure TfrmClothGongYiList.tbEDITClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.fieldbyname('status').AsString <> '-1' then
  begin
    Application.MessageBox('计划单数据不能修改!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from Cloth_Sub A  where IsGongYi=1');
    sql.Add(' and mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    sql.Add(' and exists(select * from Cloth_Main B where A.MainId=B.MainId and DATEDIFF(DD,B.Filltime,GETDATE()) >30)  ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('数据已锁定无法修改!', '提示', 0);
    Exit;
  end;
  with Self.ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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(Order_Main.fieldbyname('SubId').AsString)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
  try
    frmClothGongYi := TfrmClothGongYi.Create(Application);
    with frmClothGongYi do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.Order_Main.fieldbyname('SubId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('MainId', frmClothGongYi.FMainId, []);
      end;
    end;
  finally
    frmClothGongYi.Free;
  end;
end;

procedure TfrmClothGongYiList.tbDELClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from Cloth_Sub A  where IsGongYi=1');
    sql.Add(' and mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
    sql.Add(' and exists(select * from Cloth_Main B where A.MainId=B.MainId and DATEDIFF(DD,B.Filltime,GETDATE()) >30)  ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('数据已锁定无法修改!', '提示', 0);
    Exit;
  end;
  with Self.ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    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(Order_Main.fieldbyname('SubId').AsString)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
  if Order_Main.fieldbyname('status').AsString <> '-1' then
  begin
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Cloth_Sub SET IsGongYi=0 ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        execsql;
      end;
      application.MessageBox('删除工艺成功！', '提示信息');
      TBRafresh.Click;
    except
      application.MessageBox('删除工艺失败！', '提示信息', 0);
    end;
  end
  else
  begin
    if DelData() then
    begin
      Order_Main.Delete;
    end;
  end;

end;

procedure TfrmClothGongYiList.N1Click(Sender: TObject);
var
  str, FSubId, strsql: string;
begin
  str := '0';
  if InputQuery('单价', '请输入数字！！', str) then
  begin
    FSubId := Trim(Order_Main.fieldbyname('SubId').AsString);
    strsql := '工艺编号：' + Trim(Order_Main.fieldbyname('GongYiCode').AsString);
    strsql := strsql + ' SubId:' + Trim(Order_Main.fieldbyname('SubId').AsString);

    if Application.MessageBox(PChar('请确定：' + strsql), '提示', 32 + 4) <> IDYES then
      Exit;
    try
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' update Cloth_Sub set Price= ' + str);
        sql.Add(' where SubId=' + quotedstr(Trim(Order_Main.fieldbyname('SubId').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('单价修改'));
        sql.Add(',' + quotedstr(trim(strsql)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
    except
      application.MessageBox('保存异常！', '提示信息');
    end;
    Order_Main.edit;
    Order_Main.fieldbyname('Price').value := str;
  end;
end;

end.

