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;

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;
    SPName: TEdit;
    FactoryName10: TEdit;
    YunDanNo: TEdit;
    SPSpec: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Price: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1ShuiDian: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: 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 YunDanNoChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column19PropertiesEditValueChanged(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXCKNewList: TfrmSXCKNewList;

implementation
uses
  U_DataLink,U_Fun,U_RTFun;

{$R *.dfm}
function TfrmSXCKNewList.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  if Trim(CDS_Main.fieldbyname('CRType').AsString)<>'销售出库' then
  begin
    FFactoryName:=Trim(CDS_Main.fieldbyname('factoryName').AsString);
  end else
  begin
    FFactoryName:=Trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='+quotedstr(Trim(FFactoryName)));
    sql.Add(' and KCType=''GYS'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    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;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YF_Money_CRID select 0 ');
        sql.Add('select * from YF_Money_CRID ');
        Open;
      end;
    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(FFactoryName);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='+quotedstr(Trim(FFactoryName)));
    sql.Add(' and YFTypeId='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
    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
    begin
      Append;
      FieldByName('SCQty').Value:=CDS_Main.fieldbyname('Qty10').AsFloat;
      FieldByName('SCPS').Value:=CDS_Main.fieldbyname('JQty').AsFloat;
    end
    else
    Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(CDS_Main.fieldbyname('SPID').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(FFactoryName);
    FieldByName('CRTime').Value:=CDS_Main.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty10').AsFloat;
    FieldByName('PS').Value:=CDS_Main.fieldbyname('JQty').AsFloat;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('Price').Value:=CDS_Main.fieldbyname('Price').Value;
    FieldByName('KPType').Value:=CDS_Main.fieldbyname('KPType').Value;
    FieldByName('YunDanNo').Value:=CDS_Main.fieldbyname('YunDanNo').Value;
    FieldByName('ShuiDian').Value:=CDS_Main.fieldbyname('ShuiDian').Value;
    FieldByName('SJQty').Value:=CDS_Main.fieldbyname('SJQty').Value;
    FieldByName('money').Value:=CDS_Main.fieldbyname('money').Value;
    FieldByName('QtyUnit').Value:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
    FieldByName('P_CodeName').Value:=Trim(CDS_Main.fieldbyname('SPName').AsString);
    FieldByName('P_Spec').Value:=Trim(CDS_Main.fieldbyname('SPSpec').AsString);
    FieldByName('status').Value:='0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set HSPrice=Price*(1+isnull(ShuiDian,0)*0.01)');
    sql.Add(',HSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
    sql.Add(',JSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(KPType,'''')=''开票'' ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set HSPrice=Null');
    sql.Add(',HSMoney=Null');
    sql.Add(',JSMoney=Money');
    sql.Add(',ShuiDian=Null');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(KPType,'''')=''不开票'' ');
    ExecSQL;
  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(' 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:=SGetServerDateMBeg(ADOQueryTemp);
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmSXCKNewList.InitGrid();
begin
  ToolBar1.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*,C.KPType,C.ShuiDian ');
      sql.Add(',FactoryName10=RTrim(dbo.getPinYin(A.FactoryName))+RTrim(A.FactoryName)');
      sql.Add(',Qty10=B.Qty*A.CRQtyFlag');
      sql.Add(' from CK_YCL_CR_Main A inner join CK_YCL_CR B on A.SMID=B.SMID');
      sql.Add(' left join YF_Money_CR C on B.SPID=C.YFTypeId');
      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 A.CKName=''原料仓库'' ');
      sql.Add(' and A.CRType in(''销售出库'',''退货'',''采购入库'')');
      SQL.Add('  and B.ZFFlag=0 ');
      IF cxTabControl1.TabIndex=0 then
        SQL.Add(' and not exists(select maiNID from YF_Money_CR X where X.YFTypeID=B.SPID ) ')
      else
        SQL.Add(' and  exists(select maiNID from YF_Money_CR X where X.YFTypeID=B.SPID )');
      Open;
    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+'LJ',Tv1,'应付款管理');
  Close;
end;

procedure TfrmSXCKNewList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+'LJ',Tv1,'应付款管理');
end;

procedure TfrmSXCKNewList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('原料采购销售退货列表',cxGrid2);
end;

procedure TfrmSXCKNewList.TBFindClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  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.YunDanNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmSXCKNewList.ToolButton1Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  BegDate.SetFocus;
  if cxTabControl1.TabIndex<>0 then Exit;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  if CDS_Main.Locate('SSel;Price', VarArrayOf([True, Null]), [loPartialKey])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if CDS_Main.Locate('SSel;KPType', VarArrayOf([True, Null]), [loPartialKey])=True then
  begin
    Application.MessageBox('是否开票不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要生成应付款吗？','提示',32+4)<>IDYES then Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while Locate('SSel',True,[]) do
      begin
        IF not YFData() then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox('生成应付款失败！','提示信息',0);
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成应付款失败！','提示信息',0);

  end;
end;

procedure TfrmSXCKNewList.cxTabControl1Change(Sender: TObject);
begin
  INITGrid();
  if cxTabControl1.TabIndex=0 then
  begin
    v1Price.Options.Editing:=True;
    v1ShuiDian.Options.Editing:=True;
  end else
  if cxTabControl1.TabIndex=1 then
  begin
    v1Price.Options.Editing:=False;
    v1ShuiDian.Options.Editing:=False;
  end;
end;

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

procedure TfrmSXCKNewList.v1Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FPrice,FQty:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingValue;
  with CDS_Main do
  begin
    Edit;
    if Trim(mvalue)='' then
    begin
      FieldByName('Price').Value:=Null;
    end else
    begin
      FieldByName('Price').Value:=mvalue;
    end;
  end;
  FQty:=CDS_Main.fieldbyname('Qty10').AsString;
  if Trim(mvalue)='' then
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('Money').Value:=Null;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_YCL_CR Set Price=Null,Money=Null ');
      sql.Add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
      ExecSQL;
    end;
  end else
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(mvalue);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_YCL_CR Set Price='+mvalue);
      sql.Add(',Money='+CDS_Main.fieldbyname('Money').AsString);
      sql.Add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmSXCKNewList.ToolButton2Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  BegDate.SetFocus;
  if cxTabControl1.TabIndex<>1 then Exit;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  if Application.MessageBox('确定要撤销吗？','提示',32+4)<>IDYES then Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' delete YF_Money_CR where YFTypeId='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
          if Trim(CDS_Main.fieldbyname('CRType').AsString)<>'销售出库' then
          begin
            sql.Add(' and FactoryName='''+Trim(CDS_Main.fieldbyname('FactoryName').AsString)+'''');
          end else
          begin
            sql.Add(' and FactoryName='''+Trim(CDS_Main.fieldbyname('ToFactoryName').AsString)+'''');
          end;
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销异常！','提示信息',0);

  end;
end;

procedure TfrmSXCKNewList.v1Column19PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingValue;
  with CDS_Main do
  begin
    Edit;
    if Trim(mvalue)='' then
    begin
      FieldByName('HSType').Value:=Null;
    end else
    begin
      FieldByName('HSType').Value:=mvalue;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update CK_SXPB_CR Set HSType='''+mvalue+'''');
    sql.Add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
    ExecSQL;
  end;
end;

end.
