unit U_PBTHNewList;

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
  TfrmPBTHNewList = 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;
    C_CodeName: TEdit;
    FactoryName10: TEdit;
    THID: TEdit;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v0Column2: TcxGridDBColumn;
    v0Column3: TcxGridDBColumn;
    v0Column4: TcxGridDBColumn;
    Label6: TLabel;
    ConNo: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label7: TLabel;
    C_Code: TEdit;
    Label8: TLabel;
    C_Spec: TEdit;
    ToolButton2: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label12: TLabel;
    Label13: TLabel;
    GCCode: TEdit;
    BatchNoHZ: TEdit;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: 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 THIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v0Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxGridDBColumn3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPBTHNewList: TfrmPBTHNewList;

implementation
uses
  U_DataLink,U_Fun,U_RTFun;

{$R *.dfm}
function TfrmPBTHNewList.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='+quotedstr(Trim(CDS_Main.fieldbyname('KHName').AsString)));
    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 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(CDS_Main.fieldbyname('KHName').AsString);
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='+quotedstr(Trim(CDS_Main.fieldbyname('KHName').AsString)));
    sql.Add(' and YFTypeId='''+Trim(CDS_Main.fieldbyname('THID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'YS','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('THQty').AsFloat;
      FieldByName('SCPS').Value:=CDS_Main.fieldbyname('THPS').AsFloat;
    end
    else
    Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(CDS_Main.fieldbyname('THID').AsString);
    FieldByName('OrderNo').Value:=Trim(CDS_Main.fieldbyname('ConNo').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('KHName').AsString);
    FieldByName('CRTime').Value:=CDS_Main.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value:=CDS_Main.fieldbyname('THQty').AsFloat*-1;
    FieldByName('PS').Value:=CDS_Main.fieldbyname('THPS').AsFloat*-1;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('Price').Value:=CDS_Main.fieldbyname('Price').Value;
    FieldByName('ShuiDian').Value:=CDS_Main.fieldbyname('ShuiDian').Value;
    FieldByName('money').Value:=CDS_Main.fieldbyname('money').Value*-1;
    FieldByName('QtyUnit').Value:='Kg';
    FieldByName('P_CodeName').Value:=Trim(CDS_Main.fieldbyname('prtCodeName').AsString);
    FieldByName('P_Code').Value:=Trim(CDS_Main.fieldbyname('prtCode').AsString);
    FieldByName('P_Spec').Value:=Trim(CDS_Main.fieldbyname('prtSpec').AsString);
   // FieldByName('ComTaiTou').Value:=Trim(CDS_Main.fieldbyname('FHTaiTou').AsString);
    FieldByName('YFName').Value:='退货金额';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set HSPrice=Price*(1+ShuiDian*0.01)');
    sql.Add(',HSMoney=Price*(1+ShuiDian*0.01)*Qty');
    sql.Add(',JSMoney=Price*(1+ShuiDian*0.01)*Qty');
    sql.Add(',HSType=''含税'' ');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(ShuiDian,0)<>0');
    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(',HSType=''不含税'' ');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    sql.Add(' and isnull(ShuiDian,0)=0');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate Cloth_TH Set YSID='''+Trim(YFID)+'''');
    sql.Add(',THMoney=(select JSMoney from YF_Money_CR A where A.YFTypeId=Cloth_TH.THID)');
    sql.Add(' where THID='''+Trim(CDS_Main.fieldbyname('THID').AsString)+'''');
    ExecSQL;
  end;
  Result:=True;
end;
procedure TfrmPBTHNewList.FormDestroy(Sender: TObject);
begin
  frmPBTHNewList:=nil;
end;

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

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

procedure TfrmPBTHNewList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ,E.HSTYpe,FactoryName10=Rtrim(dbo.getPinYin(A.THKHNameJC))+RTrim(A.THKHNameJC)');
      sql.Add(',C.ConNo,B.prtCodeName,B.PrtCode,B.PrtSpec,B.PrtPrice,FactoryName=A.THKHNameJC');
      sql.Add(',CRTime=A.THDate,KHName=A.THKHNameJC,KHNameQC=A.THKHName');
      sql.Add(',CanKaoPrice=(select Top 1 Price from YF_Money_CR YS where YS.P_Code=B.prtCode and YS.FactoryName=A.THKHNameJC order by CRTime desc)');
      sql.Add(',Case when isnull(E.Price,-999)=-999 then B.prtPrice else E.Price end as Price');
      sql.Add(',Case when isnull(E.Money,-999)=-999 then Cast(B.prtPrice*A.THQty as decimal(18,2)) else ');
      sql.Add(' E.Money end as Money');
      sql.Add(',E.ComTaiTou,E.ShuiDian,CM.GCCode,CS.BatchNoHZ');
      sql.Add('from Cloth_TH A ');
      sql.Add(' inner join Cloth_Sub CS on A.MainId=CS.MainId');
      sql.Add(' inner join Cloth_Main CM on CS.MainId=CM.MainId');
      Sql.add(' inner join JYOrderCon_Sub B on CM.ConSubId=B.SubId');
      Sql.add(' inner join JYOrderCon_Main C on B.MainId=C.MainId');
      sql.Add(' left join YF_Money_CR E on A.THID=E.YFTypeId');
      sql.add('where A.THDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
      sql.Add(' and A.THDate<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      IF cxTabControl1.TabIndex=0 then
      begin
        SQL.Add(' and not exists(select maiNID from YF_Money_CR X where RTrim(isnull(X.YFTypeId,''''))=RTrim(isnull(A.THID,''''))  )');
      end
      else
      begin
        SQL.Add(' and  exists(select maiNID from YF_Money_CR X where RTrim(isnull(X.YFTypeId,''''))=RTrim(isnull(A.THID,'''')) )');
      end;
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

procedure TfrmPBTHNewList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+'LJ1',Tv1,'坯布仓库');
 { if Trim(canshu2)='查看' then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
  end else
  begin
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    TBEdit.Visible:=True;
  end;}
  //InitGrid();
end;

procedure TfrmPBTHNewList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('坯布退货列表',cxGrid1);
end;

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

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

procedure TfrmPBTHNewList.THIDChange(Sender: TObject);
begin
  if Length(Trim(THID.Text))<4 then
  begin
    if Trim(THID.Text)<>'' then Exit;
  end;
  TBFind.Click;
end;

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

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

procedure TfrmPBTHNewList.ToolButton1Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  BegDate.SetFocus;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  if CDS_Main.Locate('SSel;PrtPrice', VarArrayOf([True, Null]), [loPartialKey])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if CDS_Main.Locate('SSel;KHName', VarArrayOf([True, Null]), [loPartialKey])=True then
  begin
    Application.MessageBox('客户简称为空，请到客户信息管理模块中完善资料!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要生成应收款吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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;
    //application.MessageBox('生成应收款成功！','提示信息');
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成应收款失败！','提示信息',0);

  end;
end;

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

procedure TfrmPBTHNewList.v0Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FPrice,FQty,FFYMoney: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('THQty').AsString;
  if Trim(FFYMoney)='' then FFYMoney:='0';
  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 Cloth_TH Set THPrice=Null,THMoney=Null ');
      sql.Add(',EditTime=getdate(),Editer='''+Trim(DName)+'''');
      sql.Add('where THID='''+Trim(CDS_Main.fieldbyname('THID').AsString)+'''');
      ExecSQL;
    end;
  end else
  begin
    with CDS_Main do
    begin
      Edit;
      FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(mvalue)+StrToFloat(FFYMoney);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Cloth_TH Set THPrice='+mvalue);
      sql.Add(',THMoney='+CDS_Main.fieldbyname('Money').AsString);
      sql.Add('where FSID='''+Trim(CDS_Main.fieldbyname('FSID').AsString)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmPBTHNewList.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('THID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate CLoth_TH Set YSID=Null,THPrice=Null,THMoney=Null where THID='''+Trim(CDS_Main.fieldbyname('THID').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销异常！','提示信息',0);
  end;
end;

procedure TfrmPBTHNewList.cxGridDBColumn3CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id:Integer;
begin
   Id:=TV1.GetColumnByFieldName('KHName').Index;//;-TV1.GroupedItemCount;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id]=Null then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
end;

end.
