unit U_FDINVOICE_Sub_S;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, ExtCtrls, StdCtrls, BtnEdit, cxButtonEdit, cxTextEdit,
  cxDropDownEdit;

type
  TFrmInvoice_Sub_S = class(TForm)
    DataSource1: TDataSource;
    FP_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    TADD: TToolButton;
    TDEl: TToolButton;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_Spec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label1: TLabel;
    InvoiceNo: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    InvoiceDate: TDateTimePicker;
    Note: TMemo;
    InvoiceType: TBtnEditA;
    ToFactoryName: TBtnEditA;
    FactoryName: TBtnEditA;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Filler: TEdit;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label8: TLabel;
    SYRName: TBtnEditA;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label9: TLabel;
    ywy: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure InvoiceTypeBtnClick(Sender: TObject);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TADDClick(Sender: TObject);
    procedure TDElClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1MoneyPropertiesEditValueChanged(Sender: TObject);
    procedure SYRNameBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
  private

    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FInvoiceNO: string;
    fInvoiceFlag:string;
    canshu1:string;
    { Public declarations }
  end;

var
  FrmInvoice_Sub_S: TFrmInvoice_Sub_S;

implementation

uses U_Fun10,U_ZDYHelp, U_ZdyAttachment,U_dataLink, U_ZdyAttachGYS,
  U_CPManageSel,U_ProductOrderNewList;
{$R *.dfm}

procedure TFrmInvoice_Sub_S.SaveDate();
  var FInvoiceID: string;
begin
  if trim(FInvoiceNO)='' then
  begin
    if GetLSNo(ADOQueryTmp,FInvoiceNO,'PS','FD_InvoiceSub',4,1)=False then
    begin
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    InvoiceNO.Text:=trim(FInvoiceNO);
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('select * from FD_Invoice where InvoiceNo='''+trim(InvoiceNo.Text)+'''');
      open;
      if isempty then
      begin
        append;
        fieldbyname('filltime').AsDateTime:=SGetServerDate(ADOQueryTmp);
      end
      else
      edit;
      SSetsaveSqlnew(ADOQueryCmd,'FD_Invoice',panel1,0);
      fieldbyname('Invoiceflag').Value:=strtoint(FInvoiceflag);
      fieldbyname('chkstatus').AsString:='0';
      post;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from FD_InvoiceSub where InvoiceNo='''+trim(InvoiceNo.Text)+''' ');
      execsql;
    end;
    with FP_Sub do
    begin
    first;
    while not eof do
    begin
      if fieldbyname('invoiceID').AsString='' then
      begin
        if GetLSNo(ADOQueryTmp,FInvoiceID,'FP','FD_InvoiceSub',4,1)=False then
        begin
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end
      else
      FInvoiceID:=FP_Sub.fieldbyname('invoiceID').AsString;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.add('select * from FD_InvoiceSub where InvoiceNo='''+trim(InvoiceNo.Text)+''' ');
        sql.add('and InvoiceID='''+trim(FInvoiceID)+'''');
        open;
      end;
      if ADOQueryCmd.IsEmpty then
      begin
        ADOQueryCmd.append;
      end
      else
      begin
        ADOQueryCmd.edit;
      end;
      ADOQueryCmd.fieldbyname('InvoiceNo').Value:=trim(InvoiceNo.Text);
      ADOQueryCmd.fieldbyname('InvoiceID').Value:=FInvoiceID;
      SSetSaveDataCDSNew(ADOQueryCmd,Tv1,FP_Sub,'FD_InvoiceSub',0);
       ADOQueryCmd.fieldbyname('MainID').Value:=FP_Sub.fieldbyname('MainID').asstring;
      ADOQueryCmd.fieldbyname('Qty').Value:=FP_Sub.fieldbyname('Qty').AsFloat;
      ADOQueryCmd.fieldbyname('Price').Value:=FP_Sub.fieldbyname('Price').AsFloat;
      ADOQueryCmd.fieldbyname('Money').Value:=FP_Sub.fieldbyname('Money').AsFloat;
      ADOQueryCmd.Post;
      next;
    end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！','提示');
    Modalresult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！','提示',MB_ICONERROR);
  end;
end;

procedure TFrmInvoice_Sub_S.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add('from FD_Invoice A');
    sql.Add('where A.InvoiceNo='''+trim(FInvoiceNo)+'''');
    open;
  end;
  if ADOQueryTmp.isempty=False then
  begin
    SCSHDataNew(ADOQueryTmp,Panel1,0);
  end
  else
  begin
    Filler.Text:=trim(DName);
    InvoiceDate.DateTime:=SGetServerDate(ADOQueryCmd);
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.OrderNo,');
    sql.Add('KHConNO=(select Top 1 KHConNO  from JYOrdercon_Main X where X.conNO=B.conNO)');
    sql.Add('from FD_InvoiceSub A ');
    sql.Add('left join JYorder_main B on B.MainID=A.mainID') ;
    sql.Add('where A.InvoiceNo='''+trim(FInvoiceNo)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,FP_Sub);
  SInitCDSData20(ADOQueryTmp,FP_Sub);
end;

procedure TFrmInvoice_Sub_S.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'财务管理');
  Close;
end;

procedure TFrmInvoice_Sub_S.FormShow(Sender: TObject);
begin
  if canshu1<>'高权限'then
  ywy.Text:=trim(DName);
  ReadCxGrid(self.Caption,Tv1,'财务管理');
  initGrid();
end;

procedure TFrmInvoice_Sub_S.ToFactoryNameBtnClick(Sender: TObject);
begin
  IF fInvoiceflag='0' then
  begin
    try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        TOFactoryName.Text:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
      end;
    end;
    finally
    frmZdyAttachGYS.Free;
    end;
  end
  else
  begin
    try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if canshu1<>'高权限' then
      begin
//        DEFstr5.Text:=trim(DName);
//        DEFstr5.Enabled:=false;
      end;
      if ShowModal=1 then
      begin
        TOFactoryName.Text:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
    finally
    frmZdyAttachment.Free;
    end;
  end;
end;

procedure TFrmInvoice_Sub_S.TSaveClick(Sender: TObject);
begin
  if trim(FactoryName.Text)='' then
  begin
    application.MessageBox('开票单位不能为空','提示');
    exit;
  end;
  if trim(FInvoiceNo)='' then
  begin
    with ADOQueryTmp do
    begin
      close;
      sql.clear;
      sql.add('select * from FD_Invoice where InvoiceNo='''+trim(InvoiceNo.Text)+''' ');
      open;
    end;
    if ADOQueryTmp.IsEmpty=False then
    begin
      application.MessageBox('发票号不能重复','提示');
      exit;
    end;
  end;
  SaveDate();
end;

procedure TFrmInvoice_Sub_S.FactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if canshu1<>'高权限' then
      begin
//        DEFstr5.Text:=trim(DName);
//        DEFstr5.Enabled:=false;
      end;
      if ShowModal=1 then
      begin
        FactoryName.Text:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.v1P_CodeNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeName';
      flagname:='产品名称';
      if ShowModal=1 then
      begin
        Self.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('P_CodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.FP_Sub.FieldByName('P_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  //self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TFrmInvoice_Sub_S.InvoiceTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='InvoiceType';
      flagname:='发票类型';
      if ShowModal=1 then
      begin
        InvoiceType.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.v1P_SpecPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='P_Spec';
      flagname:='成品规格';
      if ShowModal=1 then
      begin
        Self.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('P_Spec').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.v1QtyUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='QtyUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.v1PriceUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='币种';
      if ShowModal=1 then
      begin
        Self.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('PriceUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.TADDClick(Sender: TObject);
begin
{  frmCPManageSel:=TfrmCPManageSel.create(self);
  with frmCPManageSel do
  begin
    if showmodal=1 then
    begin
      FP_Sub.DisableControls;
      CDS_Main.DisableControls;
      CDS_Main.First;
      while not CDS_Main.Eof  do
      begin
        if (cds_main.fieldbyname('ssel').AsBoolean) and
          (not FP_Sub.Locate('P_Code',trim(cds_main.fieldbyname('CYNo').AsString),[])) then
        begin
          FP_Sub.Append;
          FP_Sub.FieldByName('P_Code').Value:=trim(cds_main.fieldbyname('CYNo').AsString);
          FP_Sub.FieldByName('P_CodeName').Value:=trim(cds_main.fieldbyname('CYName').AsString);
   //     self.cds_main.FieldByName('defnote5').Value:=trim(cds_main.fieldbyname('defnote5').AsString);
          FP_Sub.FieldByName('P_spec').Value:=trim(cds_main.fieldbyname('CYSpec').AsString);
          FP_Sub.FieldByName('QtyUnit').Value:='M';
          FP_Sub.FieldByName('priceUnit').Value:='RMB';

          FP_Sub.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
      FP_Sub.EnableControls;
    end;
    free;
  end; }
  FP_Sub.Append;
  FP_Sub.FieldByName('QtyUnit').Value:='M';
  FP_Sub.FieldByName('priceUnit').Value:='RMB';
  FP_Sub.Post;

end;

procedure TFrmInvoice_Sub_S.TDElClick(Sender: TObject);
begin
  if FP_Sub.IsEmpty then exit;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.add('delete from FD_Invoicesub where InvoiceID='''+trim(FP_Sub.fieldbyname('InvoiceID').asstring)+''' ');
    execsql;
  end;
  FP_Sub.delete;
end;

procedure TFrmInvoice_Sub_S.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:string;
begin
 mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with FP_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    FieldbyName('Money').Value:=format('%.2f',[FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TFrmInvoice_Sub_S.v1MoneyPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with FP_Sub do
  begin
    Edit;
    FieldByName('Money').Value:=Trim(mvalue);
    IF fieldbyname('Qty').AsFloat>0 then
    FieldbyName('price').Value:=format('%.4f',[FieldByName('money').AsFloat / FieldByName('Qty').AsFloat])
    else
    FieldbyName('price').Value:=0;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TFrmInvoice_Sub_S.SYRNameBtnClick(Sender: TObject);
begin
 try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SYRName';
      flagname:='公司抬头';
      if Trim(flag)='SYRName' then
      begin
        flag:='OrdDefStr2';
        V1Name.Caption:='中文名称';
        V1Note.Caption:='英文名称';
        fnote:=True;
      end;
      if ShowModal=1 then
      begin
        SYRName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_S.v1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
    with frmProductOrderNewList do
    begin
      if canshu1<>'高权限' then fywy:=trim(DName);
      if ShowModal=1 then
      begin
        with Self.FP_Sub do
        begin
          Edit;
          FieldByName('MainId').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('MainId').AsString);
      //    FieldByName('subID').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('subID').AsString);
          FieldByName('orderNo').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('orderNo').AsString);
          FieldByName('khconNO').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('khconNO').AsString);
          FieldByName('P_Code').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('MPRTCode').AsString);
          FieldByName('P_CodeName').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('MPRTCodeName').AsString);
          FieldByName('P_spec').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('Mprtspec').AsString);
          post;
        end;
      end;
    end;
  finally
    frmProductOrderNewList.Free;
    self.Tv1.Controller.EditingController.ShowEdit();
  end;
end;

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

procedure TFrmInvoice_Sub_S.FormDestroy(Sender: TObject);
begin
  frmInvoice_Sub_S:=nil;
end;

end.
