unit U_CKProductXSMHZList;

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, MovePanel, cxButtonEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, 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, cxTextEdit;

type
  TfrmCKProducTXSHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Label6: TLabel;
    MJID: TEdit;
    v1Column6: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label11: TLabel;
    tt: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    prtcolor2: TEdit;
    Tv1Column6: 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 MPRTCodeName2Change(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1Column4PropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProducTXSHZList: TfrmCKProducTXSHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmCKProducTXSHZList.FormDestroy(Sender: TObject);
begin
  frmCKProductXSHZList := nil;

end;

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

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

procedure TfrmCKProducTXSHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

//      sql.Add('select  b.CustomerNoName,SUM(round((a.qty-a.kongjia)*machi,2)) Qty,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty  ');
//      sql.Add('   ,B.MPRTCodeName, PRTColor=isnull(jycolor,PRTColor),B.PRICE,MONEY=SUM(B.PRICE*(a.qty-a.kongjia)*machi),A.cknote  ');
//
//      sql.Add('from  CK_BanCP_CR A  inner join JYOrder_Main B on A.MainId=B.MainId  ');
//      sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId    ');
////      sql.Add('inner join JYOrder_Car D on D.CarNo=A.APID ');
//      sql.Add('   where A.CRFlag=''出库''  ');
//
//      sql.Add(' and A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
//      sql.Add(' and A.CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
//
//      sql.Add(' group by  CustomerNoName,MPRTCodeName,isnull(jycolor,PRTColor),B.PRICE,A.cknote  ');

      sql.Add('EXEC P_EVERYDAY_XS ');
      sql.Add('@begdate=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(',@enddate=''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');


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

procedure TfrmCKProducTXSHZList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKProducTXSHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '三水成品仓库');
  Close;
end;

procedure TfrmCKProducTXSHZList.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv1, '三水成品仓库');
end;

procedure TfrmCKProducTXSHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmCKProducTXSHZList.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 TfrmCKProducTXSHZList.MPRTCodeName2Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProducTXSHZList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SOrdDefStr10';
      flagname := '库存存放地点';
      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          Edit;
          FieldByName('SOrdDefStr10').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Sub Set SOrdDefStr10=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(' where SubId=''' + Trim(Self.CDS_Main.fieldbyname('SubId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKProducTXSHZList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProducTXSHZList.Tv1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('UPDATE CK_BanCP_CR SET cknote=' + QuotedStr(mvalue));
    SQL.ADD(' where MJID IN (SELECT    MJID FROM ');
    SQL.Add(' CK_BanCP_CR A  inner join JYOrder_Main B on A.MainId=B.MainId ');
    SQL.Add('inner join JYOrder_Sub C on A.SubId=C.SubId  ');
    sql.Add('   where A.CRFlag=''出库''  ');

    sql.Add(' and A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
    sql.Add(' and A.CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
    SQL.Add(' AND b.CustomerNoName=' + QuotedStr(CDS_Main.FIELDBYNAME('CustomerNoName').AsString));
    SQL.Add(' AND B.MPRTCodeName=' + QuotedStr(CDS_Main.FIELDBYNAME('MPRTCodeName').AsString));
    SQL.Add(' AND isnull(jycolor,PRTColor)=' + QuotedStr(CDS_Main.FIELDBYNAME('PRTColor').AsString));
    SQL.Add(' AND ISNULL(B.PRICE,0)=' + (CDS_Main.FIELDBYNAME('PRICE').AsString));
    SQL.Add(' AND ISNULL(a.cknote,'''')=' + QuotedStr(CDS_Main.FIELDBYNAME('cknote').AsString));

    sql.Add(')');
//    ShowMessage(SQL.Text);
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.

