unit U_SXCKNewList;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  cxContainer, cxImage, cxDBEdit, 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;

type
  TfrmSXCKNewList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    P_CodeName: TEdit;
    FactoryName: TEdit;
    crno: TEdit;
    P_Spec: TEdit;
    Label6: TLabel;
    P_CF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label9: TLabel;
    SSType: TComboBox;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CRType: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure crnoKeyPress(Sender: TObject; var Key: Char);
    procedure crnoChange(Sender: TObject);
    procedure P_SpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    procedure SetStatus();
    function YFData(): Boolean;
    procedure InitImage(P_code: string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXCKNewList: TfrmSXCKNewList;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}
function TfrmSXCKNewList.YFData(): Boolean;
var
  CRID, YFID: string;
begin
  Result := False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName=''' + Trim(CDS_Main.fieldbyname('factoryName').AsString) + ''' ');
//    sql.Add('select * from YF_Money_KC where FactoryName='''+ quotedstr(Trim(CDS_Main.fieldbyname('factoryName').AsString))+''' ');
//    showmessage(SQL.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(CDS_Main.fieldbyname('factoryName').AsString);
      FieldByName('ZdyStr1').Value := '应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName=''' + Trim(CDS_Main.fieldbyname('factoryName').AsString) + ''' ');
//    + quotedstr(Trim(CDS_Main.fieldbyname('factoryName').AsString)));
    sql.Add(' and mainID=''' + Trim(CDS_Main.fieldbyname('crno').AsString) + '''  and yfname not in(''坯布其他费'',''外加工其他费'',''待检布其他费'',''成品费'')');
    sql.Add(' and P_color=''' + Trim(CDS_Main.fieldbyname('P_color').AsString) + '''');
    sql.Add(' and P_code=''' + Trim(CDS_Main.fieldbyname('P_code').AsString) + '''');
//     ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'YF', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应付流水号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID=''' + Trim(YFID) + '''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(CDS_Main.fieldbyname('crno').AsString);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应付款登记';
    FieldByName('CRFlag').Value := '应付付';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(CDS_Main.fieldbyname('factoryName').AsString);
//    FieldByName('ToFactoryName').Value := Trim(CDS_Main.fieldbyname('TofactoryName').AsString);
    FieldByName('CRTime').Value := CDS_Main.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
    FieldByName('CJQty').Value := CDS_Main.fieldbyname('Qty').AsFloat;
//       FieldByName('Qty').Value := CDS_Main.fieldbyname('Qty').AsFloat * CDS_Main.fieldbyname('CRQtyFlag').AsFloat;
//    FieldByName('MDQty').Value := CDS_Main.fieldbyname('Qty').AsFloat * CDS_Main.fieldbyname('CRQtyFlag').AsFloat;

    FieldByName('PS').Value := CDS_Main.fieldbyname('piqty').AsFloat;
//        FieldByName('PS').Value := CDS_Main.fieldbyname('rollnum').AsFloat * CDS_Main.fieldbyname('CRQtyFlag').AsFloat;
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := CDS_Main.fieldbyname('Price').AsFloat;
    FieldByName('CJPrice').Value := CDS_Main.fieldbyname('CBCJf').AsFloat;
    FieldByName('FRPrice').Value := CDS_Main.fieldbyname('CBfrf').AsFloat;
    FieldByName('money').Value := CDS_Main.fieldbyname('Money').AsFloat;
    FieldByName('BBmoney').Value := CDS_Main.fieldbyname('Money').AsFloat;
    FieldByName('HuiLv').Value := 1;
//    if Trim(CDS_Main.FieldByName('BZType').AsString) = '$' then
//      FieldByName('BZType').Value := 'USD'
//    else
//      FieldByName('BZType').Value := 'RMB';
    FieldByName('ComTaiTou').Value := '';
    FieldByName('QtyUnit').Value := Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
//    FieldByName('orderNo').Value := Trim(CDS_Main.fieldbyname('OrderNo').AsString);
    FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('P_codeName').AsString);
    FieldByName('P_Color').Value := Trim(CDS_Main.fieldbyname('P_Color').AsString);
    FieldByName('YFName').Value := Trim(CDS_Main.fieldbyname('FeeName').AsString);
    FieldByName('Price').Value := Trim(CDS_Main.fieldbyname('Price').AsString);
    FieldByName('Money').Value := Trim(CDS_Main.fieldbyname('Money').AsString);

    fieldbyname('P_Code').Value := Trim(CDS_Main.fieldbyname('p_code').AsString);
    fieldbyname('Note').Value := Trim(CDS_Main.fieldbyname('Note').AsString);
    FieldByName('MainId').Value := Trim(CDS_Main.fieldbyname('crno').AsString);
//    FieldByName('Subid').Value := Trim(CDS_Main.fieldbyname('crid').AsString);
//    FieldByName('FPNo').Value:=Trim(CDS_Main.fieldbyname('FPNo').AsString);
    FieldByName('status').Value := '0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmSXCKNewList.FormDestroy(Sender: TObject);
begin
  frmSXCKNewList := nil;
end;

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

procedure TfrmSXCKNewList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 10;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
end;

procedure TfrmSXCKNewList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_Get_YFYSList_c2');
      SQL.Add('@begdate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
      SQL.Add(',@enddate=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + ''' ');
      if cxTabControl1.TabIndex = 0 then
        SQL.Add(',@Flag=0')
      else
        SQL.Add(',@Flag=1');
      Open;
 //     ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmSXCKNewList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '坯布仓库');
  Close;
end;

procedure TfrmSXCKNewList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '坯布仓库');
  SetStatus();
end;

procedure TfrmSXCKNewList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('坯布入库列表', cxGrid2);
end;

procedure TfrmSXCKNewList.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 TfrmSXCKNewList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmSXCKNewList.crnoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmSXCKNewList.crnoChange(Sender: TObject);
begin
  if Length(Trim(crno.Text)) < 4 then
  begin
    if Trim(crno.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

procedure TfrmSXCKNewList.P_SpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSXCKNewList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSXCKNewList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if not CDS_Main.Locate('ssel', true, []) then
  begin
    application.MessageBox('没有选择数据！', '提示信息', 0);
    exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if fieldbyname('ssel').AsBoolean then
        begin
          if not YFData() then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('生成应付款失败！', '提示信息', 0);
          end;
        end;
        next;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;

    application.MessageBox('生成应付款成功！', '提示信息');
    initGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成应付款失败！', '提示信息', 0);
  end;
end;

procedure TfrmSXCKNewList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
  SetStatus();
end;

procedure TfrmSXCKNewList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbright then

end;

procedure TfrmSXCKNewList.SetStatus();
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    ToolButton1.Visible := True;
  end
  else
  begin
    ToolButton1.Visible := False;
  end;
end;

procedure TfrmSXCKNewList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initImage(CDS_Main.fieldbyname('P_code').AsString);
end;

procedure TfrmSXCKNewList.InitImage(P_code: string);
begin
  ADOQueryImage.close;
  if P_code = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where TFID=' + quotedstr(trim(P_code)));
//    ShowMessage(sql.Text);
    open;
  end;
end;

end.

