unit U_SCJDList;

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, cxDropDownEdit, 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
  TfrmSCJDList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton4: TToolButton;
    N1: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNo: TEdit;
    CustomerNoName: TEdit;
    ConNo: TEdit;
    MPRTSpec: TEdit;
    MPRTKZ: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    PRTColor: TComboBox;
    Label2: TLabel;
    DQGX: TComboBox;
    Label5: TLabel;
    MPRTCodeName: TComboBox;
    Label6: TLabel;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Label7: TLabel;
    carno: TEdit;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl2Change(Sender: TObject);
    procedure MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure SetStatus();
    procedure InitGrid();
 
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_FjList_RZ, U_FjList10, U_SysLogHelp;

{$R *.dfm}

procedure TfrmSCJDList.InitGrid();
var
  strwhere: string;
begin
  strwhere := SGetFilters(Panel1, 1, 2);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('select AA.*,WJYPS=RollNum-JYPS,CPKCPS=RKPS-CKPS from (');
      sql.add('select A.*,B.SubId,B.XHNo,B.PRTColor,B.PRTPs,B.PRTOrderQty,B.OrderUnit,B.SubNote');
      sql.add(',C.carno,C.RollNum,C.Qty,C.carxh,C.CRTime');
      sql.add(',PBRKDate= (select max(CRTime) PBRKDate from CK_PB_CR X where CRFlag=''入库'' and X.KCID IN (select Y.KCID  from CK_PB_CR Y where Y.CarNo=C.CarNo))      ');
      sql.add(',PBRKPS=(select sum(PS) PBRKPS from CK_PB_CR X where CRFlag=''入库'' and X.KCID IN (select Y.KCID  from CK_PB_CR Y where Y.CarNo=C.CarNo)) ');
      sql.add(',PBTCPS=(select sum(PS)  from CK_PB_CR X where CRFlag=''出库'' and CRType=''退货出库'' and X.KCID IN (select Y.KCID  from CK_PB_CR Y where Y.CarNo=C.CarNo)) ');
      sql.add(',PBSHPS=(select sum(PS)  from CK_PB_CR X where CRFlag=''出库'' and CRType=''损耗出库'' and X.KCID IN (select Y.KCID  from CK_PB_CR Y where Y.CarNo=C.CarNo)) ');
      sql.add(',JYPS=(select COUNT(MJID) from WFB_MJJY X where APID=C.CarNo)');
      sql.add(',RKPS=(select COUNT(MJID) from CK_BanCP_CR X where X.CRFlag=''入库'' and APID=C.CarNo)');
      sql.add(',CKPS=(select COUNT(MJID) from CK_BanCP_CR X where X.CRFlag=''出库'' and  APID=C.CarNo)');
      sql.add(',DQGX=(select top 1 cast(GlideIdx as varchar(4)) +GlideName as DQGX from  CL_Group X where X.CarNo=C.CarNo order by GlideIdx desc ) ');
      sql.Add(',GlideList=cast( (select '' ''+ GlideName from JYOrder_Glide X where X.CarNo=C.CarNo order by X.GlideIdx for xml path('''')) as varchar(100))  ');
      sql.add('from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId');
      sql.add('inner join JYOrder_car C on C.SubID=B.SubID');
      sql.Add('where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)));
      sql.Add('and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1)));
      SQL.Add(') AA   ');
      if strwhere <> '' then
        SQL.Add(' where  ' + strwhere);
      sql.Add(' order by Priority ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmSCJDList.SetStatus();
begin

end;

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

procedure TfrmSCJDList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmSCJDList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单进度查询FF', Tv1, '生产指示单管理');
end;

procedure TfrmSCJDList.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value := ' and FillTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''' + ' and FillTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSCJDList.InitForm();
begin
  ReadCxGrid('指示单进度查询FF', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  SInitComBoxBySql(ADOQueryCmd, MPRTCodeName, False, 'select distinct(MPRTCodeName) name from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)) + 'and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1)));
//  SInitComBoxBySql(ADOQueryCmd, PRTColor, False, 'select distinct(PRTColor) name from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)) + 'and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1)));
  SInitComBoxBySql(ADOQueryCmd, DQGX, False, 'select distinct(GlideName) name from CL_Group   ');

end;

function TfrmSCJDList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete JYOrder_Sub where MainId=''' + 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;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmSCJDList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '生产指示单列表');
end;

procedure TfrmSCJDList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();

end;

procedure TfrmSCJDList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmSCJDList.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

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

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

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

procedure TfrmSCJDList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, true);
end;

procedure TfrmSCJDList.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(conno.Text) < 3 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql');
        if Trim(canshu1) <> '高权限' then
        begin
          Parameters.ParamByName('WSql').Value := ' and A.Filler=''' + Trim(DName) + '''';
        end;
        begin
          Parameters.ParamByName('WSql').Value := ' and conno like ''' + '%' + Trim(conno.Text) + '%' + '''';
        end;
        Parameters.ParamByName('begdate').Value := '1899-01-01';
        Parameters.ParamByName('enddate').Value := '2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmSCJDList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
//      PState:=1;
      if Trim(canshu1) <> '高权限' then
        cxButton1.Visible := false;
      fkeyNO := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

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

procedure TfrmSCJDList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();

  end;

end;

procedure TfrmSCJDList.cxTabControl2Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmSCJDList.MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
var
  sql2: string;
begin
  if Key = #13 then
  begin
    sql2 := 'select distinct(PRTColor) name from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)) + 'and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1));
    if Trim(MPRTCodeName.Text) <> '' then
      sql2 := sql2 + ' and MPRTCodeName=' + Quotedstr(MPRTCodeName.Text);
    if trim(CustomerNoName.Text) <> '' then
      sql2 := sql2 + ' and CustomerNoName like ' + quotedstr('%' + trim(CustomerNoName.Text) + '%');
    SInitComBoxBySql(ADOQueryCmd, PRTColor, False, sql2);
    InitGrid();
  end;

end;

procedure TfrmSCJDList.MPRTCodeNameChange(Sender: TObject);
var
  sql2: string;
begin

  sql2 := 'select distinct(PRTColor) name from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)) + 'and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1));
  if Trim(MPRTCodeName.Text) <> '' then
    sql2 := sql2 + ' and MPRTCodeName=' + Quotedstr(MPRTCodeName.Text);
  if trim(CustomerNoName.Text) <> '' then
    sql2 := sql2 + ' and CustomerNoName like ' + quotedstr('%' + trim(CustomerNoName.Text) + '%');
  SInitComBoxBySql(ADOQueryCmd, PRTColor, False, sql2);
  InitGrid();
end;

procedure TfrmSCJDList.PRTColorChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSCJDList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
var
  sql1: string;
begin
  if Key = #13 then
  begin
    sql1 := 'select distinct(MPRTCodeName) name from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId where A.FILLTime>=' + Quotedstr(formatdateTime('yyyy-MM-dd', begdate.Date)) + 'and A.FILLTime<' + Quotedstr(formatdateTime('yyyy-MM-dd', enddate.Date + 1));
    if trim(CustomerNoName.Text) <> '' then
      sql1 := sql1 + ' and CustomerNoName like ' + quotedstr('%' + trim(CustomerNoName.Text) + '%');
    SInitComBoxBySql(ADOQueryCmd, MPRTCodeName, False, sql1);
    InitGrid();
  end;

end;

procedure TfrmSCJDList.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub set  ' + FFieldName + '=' + quotedstr(Trim(mvalue)));
    sql.Add('where subid=' + quotedstr(Trim(Order_Main.fieldbyname('subid').AsString)));
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

end.

