unit U_SalesContract_SXSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  System.ImageList, U_BaseInput, cxButtonEdit, cxDropDownEdit, cxCheckBox,
  Vcl.Menus, dxSkinsCore, dxSkinsDefaultPainters, dxScrollbarAnnotations,
  Clipbrd, dxSkinBasic, 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, dxSkinOffice2016Colorful,
  dxSkinOffice2016Dark, dxSkinOffice2019Black, dxSkinOffice2019Colorful,
  dxSkinOffice2019DarkGray, dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinWXI, dxSkinXmas2008Blue;

type
  TfrmSalesContract_SXSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    ConNo: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    TV1Column7: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label2: TLabel;
    C_Name: TEdit;
    Label3: TLabel;
    BuyName: TEdit;
    TV1Column8: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    N3: TMenuItem;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV1Column18: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FCoType, FAuthority: string;
    { Public declarations }
  end;

var
  frmSalesContract_SXSel: TfrmSalesContract_SXSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmSalesContract_SXSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmSalesContract_SXSel.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);

end;

procedure TfrmSalesContract_SXSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ConNo.SetFocus;
  Action := cahide;
end;

procedure TfrmSalesContract_SXSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
//      sql.Clear;
//      SQL.Add('SELECT DISTINCT');
//      SQL.Add('    A.ConNo,');
//      SQL.Add('    A.BuyConNo,');
//      SQL.Add('    A.SellName,');
//      SQL.Add('    A.BuyName,');
//      SQL.Add('    A.MiddleName,');
//      SQL.Add('    B.C_Spec,');
//      SQL.Add('    A.DlyDate,');
//      SQL.Add('    B.C_Code,');
//      SQL.Add('    C.C_Name,');
//      SQL.Add('    C.C_EName,');
//      SQL.Add('    C.C_Composition,');
//      SQL.Add('    C.C_Color,');
//      SQL.Add('    C.C_ColorNo,');
//      SQL.Add('    C.C_StyleNo,');
//      SQL.Add('    C.C_Pattern,');
//      SQL.Add('    C.C_Width,');
//      SQL.Add('    C.C_GramWeight,');
//      SQL.Add('    D.*,');
//      SQL.Add('    KCQTy=(select Sum(StkQty) from Bs_Yarn_IO X where X.Y_Code = D.Y_Code),');
//      SQL.Add('    SXQty= case when B.ConUnit =''Y'' then round(ConQty*0.9144*D.MeterQty/1000,2)');
//      SQL.Add('      else round(ConQty*D.MeterQty/1000,2) end,');
//      SQL.Add('    GYS=STUFF((');
//      SQL.Add('        SELECT ''/'' + T.FromCoName');
//      SQL.Add('        FROM (');
//      SQL.Add('          SELECT DISTINCT FromCoName');
//      SQL.Add('          FROM Bs_Yarn_IO');
//      SQL.Add('          WHERE Y_Code = D.Y_Code AND FromCoName IS NOT NULL AND FromCoName <> ''''');
//      SQL.Add('        ) T');
//      SQL.Add('        FOR XML PATH('''')');
//      SQL.Add('    ), 1, 1, '''')');
//      SQL.Add('FROM Bs_Contract_Main A');
//      SQL.Add('INNER JOIN Bs_Contract_Sub B ON A.ConMId = B.ConMId');
//      SQL.Add('INNER JOIN Bs_Cloth_Info C ON B.C_Code = C.C_Code');
//      SQL.Add('INNER JOIN BS_Cloth_Yarn D ON C.BCIID = D.BCIID');
//      SQL.Add('WHERE A.Status = ''9'' and D.Y_Code <> ''''');
//      Open;
    sql.Clear;
    SQL.Add('SELECT DISTINCT');
    SQL.Add('    A.ConNo,');
    SQL.Add('    A.BuyConNo,');
    SQL.Add('    A.SellName,');
    SQL.Add('    A.BuyName,');
    SQL.Add('    A.MiddleName,');
    SQL.Add('    B.C_Spec,');
    SQL.Add('    A.DlyDate,');
    SQL.Add('    B.C_Code,');
    SQL.Add('    C.C_Name,');
    SQL.Add('    C.C_EName,');
    SQL.Add('    C.C_Composition,');
    SQL.Add('    C.C_Color,');
    SQL.Add('    C.C_ColorNo,');
    SQL.Add('    C.C_StyleNo,');
    SQL.Add('    C.C_Pattern,');
    SQL.Add('    C.C_Width,');
    SQL.Add('    C.C_GramWeight,');
    SQL.Add('    D.*,');
    SQL.Add('    KCQTy = (SELECT SUM(StkQty) FROM Bs_Yarn_IO X WHERE X.Y_Code = D.Y_Code),');
    SQL.Add('    SXQty = CASE WHEN B.ConUnit = ''Y'' THEN ROUND(ConQty*0.9144*D.MeterQty/1000,2)');
    SQL.Add('                ELSE ROUND(ConQty*D.MeterQty/1000,2) END,');
    SQL.Add('    GYS = STUFF((');
    SQL.Add('        SELECT ''/'' + T.FromCoName');
    SQL.Add('        FROM (');
    SQL.Add('            SELECT DISTINCT FromCoName');
    SQL.Add('            FROM Bs_Yarn_IO');
    SQL.Add('            WHERE Y_Code = D.Y_Code AND FromCoName IS NOT NULL AND FromCoName <> ''''');
    SQL.Add('        ) T');
    SQL.Add('        FOR XML PATH('''')');
    SQL.Add('    ), 1, 1, ''''),');
    SQL.Add('    SQQty = (SELECT SUM(Y.Qty) ');
    SQL.Add('             FROM Tat_Plan_Main T ');
    SQL.Add('             INNER JOIN Tat_Plan_Yarn TP ON T.Mainid = TP.MainId');
    SQL.Add('             INNER JOIN Pur_YarnPlan_Main X ON X.OrderNo = T.OrderNo');
    SQL.Add('             INNER JOIN Pur_YarnPlan_Sub Y ON X.PurMId = Y.PurMId');
    SQL.Add('             WHERE T.OrderNo = A.ConNo AND Y.Y_Code = D.Y_Code),');
    SQL.Add('    LQQty = (SELECT SUM(X.Qty) ');
    SQL.Add('             FROM Tat_Plan_Main T');
    SQL.Add('             INNER JOIN Tat_Plan_Yarn TP ON T.Mainid = TP.MainId');
    SQL.Add('             INNER JOIN Bs_Yarn_IO X ON X.ToOrdNo = T.OrderNo AND X.Y_Code = TP.Y_Code');
    SQL.Add('             WHERE T.OrderNo = A.ConNo AND X.IOFlag = ''出库'' AND X.Y_Code = D.Y_Code)');
    SQL.Add('FROM Bs_Contract_Main A');
    SQL.Add('INNER JOIN Bs_Contract_Sub B ON A.ConMId = B.ConMId');
    SQL.Add('INNER JOIN Bs_Cloth_Info C ON B.C_Code = C.C_Code');
    SQL.Add('INNER JOIN BS_Cloth_Yarn D ON C.BCIID = D.BCIID');
    SQL.Add('WHERE A.Status = ''9'' AND D.Y_Code <> ''''');
    Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSalesContract_SXSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);

end;

procedure TfrmSalesContract_SXSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);
end;

procedure TfrmSalesContract_SXSel.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmSalesContract_SXSel.FormShow(Sender: TObject);
begin
  inherited;

  InitGrid();

  ReadCxGrid(Trim(self.Caption) + '1', TV1, '自定义数据');

end;

procedure TfrmSalesContract_SXSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmSalesContract_SXSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption) + '1', TV1, '自定义数据');

end;

procedure TfrmSalesContract_SXSel.ToolButton1Click(Sender: TObject);
begin
  ConNo.SetFocus;
  ModalResult := 1;
end;

procedure TfrmSalesContract_SXSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmSalesContract_SXSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmSalesContract_SXSel := nil;
end;

end.

