unit U_CPCKNewList;

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
  TfrmCPCKNewList = 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;
    CKNo: TEdit;
    SPColor: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: 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;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Label6: TLabel;
    SPHX: TEdit;
    v1Column8: TcxGridDBColumn;
    v1Column15: 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 CKNoChange(Sender: TObject);
    procedure SPColorChange(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);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPCKNewList: TfrmCPCKNewList;

implementation
uses
  U_DataLink,U_Fun,U_RTFun;

{$R *.dfm}
function TfrmCPCKNewList.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName,FFactoryNo:String;
begin
  Result:=False;
  FFactoryName:=Trim(CDS_Main.fieldbyname('factoryName').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Main where KHNameJC='''+Trim(FFactoryName)+''' and KHFlag=''KH'' ');
    Open;
  end;
  FFactoryNo:=Trim(ADOQueryTemp.fieldbyname('KHNo').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YS_Money_KC where FactoryName='+quotedstr(Trim(FFactoryName)));
    sql.Add(' and KCType=''KH'' ');
    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 YS_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YS_Money_CRID select 0 ');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end;

    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(FFactoryName);
      FieldByName('FactoryNo').Value:=Trim(FFactoryNo);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;

  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_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,'YS','YS_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 YS_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('FactoryNo').Value:=Trim(FFactoryNo);
    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('CKNO').Value:=CDS_Main.fieldbyname('CKNO').Value;
    FieldByName('ShuiDian').Value:=CDS_Main.fieldbyname('ShuiDian').Value;
    FieldByName('SJQty').Value:=CDS_Main.fieldbyname('SJQty').Value;
    FieldByName('HZFee').Value:=CDS_Main.fieldbyname('HZFee').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_MF').Value:=Trim(CDS_Main.fieldbyname('SPMF').AsString);
    FieldByName('P_KZ').Value:=Trim(CDS_Main.fieldbyname('SPKZ').AsString);
    FieldByName('P_Color').Value:=Trim(CDS_Main.fieldbyname('SPColor').AsString);
    FieldByName('P_HX').Value:=Trim(CDS_Main.fieldbyname('SPHX').AsString);
    FieldByName('P_Code').Value:=Trim(CDS_Main.fieldbyname('SPCode').AsString);
    FieldByName('P_Spec').Value:=Trim(CDS_Main.fieldbyname('SPSpec').AsString);
    FieldByName('BZType').Value:=Trim(CDS_Main.fieldbyname('BZType').AsString);
    FieldByName('status').Value:='0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_CR Set HSPrice=Price*(1+isnull(ShuiDian,0)*0.01)');
    sql.Add(',HSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty+isnull(HZFee,0)');
    sql.Add(',JSMoney=Price*(1+isnull(ShuiDian,0)*0.01)*Qty+isnull(HZFee,0)');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(KPType,'''')=''开票'' ');
    sql.Add(' and isnull(Price,0)<>0 and isnull(Qty,0)<>0 ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_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=Money*(1+isnull(ShuiDian,0)*0.01)');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(KPType,'''')=''开票'' ');
    sql.Add(' and (isnull(Price,0)=0 Or isnull(Qty,0)=0) ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_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 YS_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YS_Money_CR A where A.CRID=YS_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;
procedure TfrmCPCKNewList.FormDestroy(Sender: TObject);
begin
  frmCPCKNewList:=nil;
end;

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

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

procedure TfrmCPCKNewList.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.HSQty*A.CRQtyFlag*-1');
      sql.Add(' from CK_YCL_CR_Main A inner join CK_YCL_CR B on A.SMID=B.SMID');
      sql.Add(' left join YS_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 YS_Money_CR X where X.YFTypeID=B.SPID ) ')
      else
        SQL.Add(' and  exists(select maiNID from YS_Money_CR X where X.YFTypeID=B.SPID )');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmCPCKNewList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption+'YX',Tv1,'应收款管理');
  Close;
end;

procedure TfrmCPCKNewList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+'YX',Tv1,'应收款管理');
end;

procedure TfrmCPCKNewList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('成品销售退货列表',cxGrid2);
end;

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

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

procedure TfrmCPCKNewList.CKNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPCKNewList.SPColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmCPCKNewList.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 TfrmCPCKNewList.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 TfrmCPCKNewList.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if button=mbright then
end;

procedure TfrmCPCKNewList.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 TfrmCPCKNewList.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 YS_Money_CR where YFTypeId='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
          sql.Add(' and FactoryName='''+Trim(CDS_Main.fieldbyname('FactoryName').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销异常！','提示信息',0);

  end;
end;

end.
