unit U_PBOuGLtList;

interface

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

type
  TfrmPBOuGLtList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label7: TLabel;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label10: TLabel;
    CkOrdNo: TEdit;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    TBCKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Sel: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    ConNo: TEdit;
    MJID: TEdit;
    Label8: TLabel;
    CRType: TComboBox;
    CDS_DH: TClientDataSet;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    Label11: TLabel;
    ZZCarNo: TEdit;
    CPType: TEdit;
    v1Column4: TcxGridDBColumn;
    Label12: TLabel;
    C_Code: TEdit;
    v1Column14: TcxGridDBColumn;
    Label14: TLabel;
    CustomerNoName: TEdit;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    v1Column7: TcxGridDBColumn;
    Label15: TLabel;
    CheJian: TComboBox;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxgrdbclmnv1Column12: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    ToolButton1: TToolButton;
    edtCarNo: TEdit;
    Label9: TLabel;
    CarNo: TEdit;
    v1Column13: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    CKColor: TEdit;
    Tv1Column2: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure CheJianChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPBOuGLtList: TfrmPBOuGLtList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPBOuGLtList.FormDestroy(Sender: TObject);
begin
  frmPBOuGLtList := nil;
end;

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

procedure TfrmPBOuGLtList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmPBOuGLtList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('select A.*, D.carno,D.ZZCarNo,B.KHNAME AS KHNAME1,B.ConNo,B.khconno,C.C_CodeName,E.SHDanWei,E.ranchang,C.C_Color,C.gongyi,D.APXH,D.CheJian,C.BatchNoHZ,D.APBatchNo,E.KHCONNO,F.ZhiGuan,E.CustomerNoName');
      //sql.add(',KHName1=(select Y.CustomerNoName from FHSQ_Main X inner join JYOrderCon_Main_PB Y on X.ConMainId=Y.MainId where X.FSId=A.CKORDNO ) ');
      sql.add('from CK_PBCP_CR A ');
      Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
      Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
      Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
      SQL.ADD(' inner join FHSQ_Main E on A.CKOrdNo=E.FSId');
      sql.Add(' inner join FHSQ_Sub F on E.FSID = F.FSID');
      sql.add('where A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      SQL.Add(' and CRFlag in (''出库'') ');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBOuGLtList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmPBOuGLtList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmPBOuGLtList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmPBOuGLtList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select distinct Code=Null,Name=MCCJ from Machine order by MCCJ ';
  SInitComBoxBySql(ADOQueryTemp, CheJian, False, fsj);
  ReadCxGrid(Self.Caption, Tv1, '坯布仓库');
  ReadCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
end;

procedure TfrmPBOuGLtList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

procedure TfrmPBOuGLtList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmPBOuGLtList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOuGLtList.orderNoChange(Sender: TObject);
begin
  if Length(ConNo.Text) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmPBOuGLtList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmPBOuGLtList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmPBOuGLtList.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        if FCKOrderNo = '' then
        begin
          FCKOrderNo := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR_Sub where yfpzno=''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = False then
          begin
            CDS_Main.EnableControls;
            Application.MessageBox('已生成应收款，不能撤销出库!', '提示', 0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  if Application.MessageBox('确定要撤销数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_PBCK @CRFlag=''未出库'', @CKOrdNo='''', @MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').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(CDS_Main.FieldByName('CKOrdNo').AsString) + ' 条码：' + trim(CDS_Main.FieldByName('MJID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_FHSQ ''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
            ExecSQL;
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;

end;

procedure TfrmPBOuGLtList.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOuGLtList.ConNoKeyPress(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('select A.*,B.ConNo,C.C_CodeName,C.C_Code,C.C_Color,D.APXH ');
        sql.add('from CK_PBCP_CR A ');
        Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
        Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
        Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
        sql.add('where B.ConNo like :ConNo');
        SQL.Add(' and CRFlag=''出库'' ');
        Parameters.ParamByName('ConNo').Value := '%' + Trim(ConNo.Text) + '%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmPBOuGLtList.CheJianChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOuGLtList.ToolButton1Click(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Trim(edtCarNo.Text) = '' then
  begin
    Application.MessageBox('请输入机台号', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        if FCKOrderNo = '' then
        begin
          FCKOrderNo := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR_Sub where yfpzno=''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = False then
          begin
            CDS_Main.EnableControls;
            Application.MessageBox('已生成应收款，不能撤销出库!', '提示', 0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  if Application.MessageBox('确定要修改出库机台吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('update WFB_MJJY_PB set ZZCarNo=' + quotedstr(Trim(edtCarNo.Text)) + ' where MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').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(CDS_Main.FieldByName('CKOrdNo').AsString) + ' 条码：' + trim(CDS_Main.FieldByName('MJID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

          with CDS_Main do
          begin
            Edit;
            FieldByName('SSel').Value := False;
            FieldByName('ZZCarNo').Value := Trim(edtCarNo.Text);
            Post;
          end;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('修改异常！', '提示', 0);
  end;
end;

procedure TfrmPBOuGLtList.ToolButton2Click(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Trim(CKColor.Text) = '' then
  begin
    Application.MessageBox('请先输入颜色', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要设置颜色吗？', '提示', 32 + 4) <> IDYES then
    Exit;

//  MovePanel2.Visible := True;
//  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('update CK_PBCP_CR set CKColor=' + quotedstr(Trim(CKColor.Text)) + ' where MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').asstring)));
            ExecSQL;
          end;

          with CDS_Main do
          begin
            Edit;
            FieldByName('SSel').Value := False;
            FieldByName('CKColor').Value := Trim(CKColor.Text);
            Post;
          end;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
//    MovePanel2.Visible := False;
    Exit;
  except
//    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常！', '提示', 0);
  end;
end;

end.

