unit U_CIInPut;

interface

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

type
  TfrmCIInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    CINO: TEdit;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    Label2: TLabel;
    CIDate: TDateTimePicker;
    Label11: TLabel;
    Label15: TLabel;
    OtherFeeName: TBtnEditA;
    CustomerNoName: TEdit;
    Label7: TLabel;
    OtherFee: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1ShipDate: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OtherFeeNameBtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType:String;

    { Public declarations }
  end;

var
  frmCIInPut: TfrmCIInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_ZDYHelpSel,U_FHOKSelList;

{$R *.dfm}

procedure TfrmCIInPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmCIInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.CIID,A.CINO,A.CIDate,A.OtherFeeName,A.OtherFee, ');
    sql.Add(' B.CSID,C.ConNo,C.CustomerNoName,C.CustomerNo,D.FHQty,D.FHPS,D.FHDate,D.FHType,D.QtyUnit,');
    sql.Add(' E.OrderNo,PRTCode=isnull(E.MPRTCode,'''')+isnull(E.MPRTGY,''''),');
    sql.Add(' PRTColor=isnull(F.PRTColorNo,'''')+'' ''+isnull(F.PRTColor,'''')+'' ''+isnull(F.PRTColorEng,''''), ');
    sql.Add(' F.PRTHX,G.PriceUnit,G.PRTPrice,FHMoney=G.PRTPrice*D.FHQty,PIMainId=G.MainId,PISubId=G.SubId,');
    sql.Add(' F.MainId,F.SubId,D.FHID,B.CSPrice,B.CSMoney,B.SKKMoney');
    sql.Add(' from JYOrder_CI A');
    sql.Add(' inner join JYOrder_CI_Sub B on A.CIId=B.CIId');
    sql.Add(' inner join JYOrder_FH D on B.FHID=D.FHID');
    sql.Add(' inner join JYOrder_Main E on D.MainId=E.MainId');
    sql.Add(' inner join JYOrder_Sub F on D.SubId=F.SubId');
    sql.Add(' inner join JYOrderCon_Sub G on F.PISubId=G.SubId');
    sql.Add(' inner join JYOrderCon_Main C on G.Mainid=C.MainId');
    sql.Add(' where A.CIID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  SCSHDataNew(ADOQuery1,ScrollBox1,0);
  if PState=0 then
  begin
    CIDate.DateTime:=Now;
  end;
end;

procedure TfrmCIInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmCIInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmCIInPut.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'CI','JYOrder_CI',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_CI where CIID='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('CIID').Value:=Trim(maxno);
      FieldByName('CINO').Value:=Trim(CINO.Text);
      FieldByName('CIDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',CIDate.Date));
      FieldByName('OtherFeeName').Value:=Trim(OtherFeeName.Text);
      if Trim(OtherFee.Text)<>'' then
        FieldByName('OtherFee').Value:=Trim(OtherFee.Text)
      else
        FieldByName('OtherFee').Value:=Null;
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_CI where CINO='''+Trim(CINO.Text)+'''');
      Open;
    end;
    if ADOTemp.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('销售发票号重复!','提示',0);
      Exit;
    end;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('CSId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'CS','JYOrder_CI_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('生成明细表流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(Order_Sub.fieldbyname('CSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_CI_Sub where ');
          sql.Add('  CSId='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('CSId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('CIID').Value:=Trim(maxno);
          FieldByName('CSId').Value:=Trim(maxSubNo);
          FieldByName('PIMainId').Value:=Order_Sub.fieldbyname('PIMainId').Value;
          FieldByName('PISubId').Value:=Order_Sub.fieldbyname('PISubId').Value;
          FieldByName('FHID').Value:=Order_Sub.fieldbyname('FHID').Value;
          FieldByName('CSBZType').Value:=Order_Sub.fieldbyname('PriceUnit').Value;
          FieldByName('CSQtyUnit').Value:=Order_Sub.fieldbyname('QtyUnit').Value;
          FieldByName('CSPrice').Value:=Order_Sub.fieldbyname('CSPrice').Value;
          FieldByName('CSPS').Value:=Order_Sub.fieldbyname('FHPS').Value;
          FieldByName('CSQty').Value:=Order_Sub.fieldbyname('FHQty').Value;
          FieldByName('CSMoney').Value:=Order_Sub.fieldbyname('CSMoney').Value;
          FieldByName('SKKMoney').Value:=Order_Sub.fieldbyname('SKKMoney').Value;
          Post;
        end;
        
        Order_Sub.Edit;
        Order_Sub.FieldByName('CSId').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set CustomerNoName='''+Trim(Order_Sub.fieldbyname('CustomerNoName').AsString)+''''); //
      sql.Add(',CustomerNo='''+Trim(Order_Sub.fieldbyname('CustomerNo').AsString)+''''); //
      sql.Add(',FHMoney=(select Sum(CSMoney) from JYOrder_CI_Sub CS where CS.CIID=JYOrder_CI.CIID)');
      sql.Add(',KKMoney=(select Sum(SKKMoney) from JYOrder_CI_Sub CS where CS.CIID=JYOrder_CI.CIID)');
      sql.Add(',BZType='''+Trim(Order_Sub.fieldbyname('PriceUnit').AsString)+'''');
      sql.Add(' where CIID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)+isnull(KKMoney,0)-isnull(DJMoney,0)');
      sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)');
      sql.Add(' where CIID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmCIInPut.TBSaveClick(Sender: TObject);
var
  PIMainId:String;
begin
  CINO.SetFocus;
  if Trim(CINO.Text)='' then
  begin
    Application.MessageBox('销售发票号不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  PIMainId:='';
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not eof do
    begin
      if PIMainId='' then
      begin
        PIMainId:=Trim(Order_Sub.fieldbyname('CustomerNoName').AsString);
      end else
      begin
        if Trim(PIMainId)<>Trim(Order_Sub.fieldbyname('CustomerNoName').AsString) then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('客户不同不能保存!','提示',0);
          Exit;
        end;
      end;
      if Trim(Order_Sub.fieldbyname('SKKMoney').AsString)<>'' then
      begin
        if Order_Sub.fieldbyname('SKKMoney').Value>0 then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('扣款金额不能为正数！','提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmCIInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('CSId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_CI_Sub where CSId='''+Trim(Order_Sub.fieldbyname('CSId').AsString)+'''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set ');
      sql.Add(' FHMoney=(select Sum(CSMoney) from JYOrder_CI_Sub CS where CS.CIID=JYOrder_CI.CIID)');
      sql.Add(' where CIID='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_CI Set YSMoney=isnull(FHMoney,0)+isnull(OtherFee,0)-isnull(DJMoney,0)');
      sql.Add('-isnull(YKMoney,0)-isnull(QKMoney,0)-isnull(YJMoney,0)+isnull(KKMoney,0)');
      sql.Add(' where CIID='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmCIInPut.OtherFeeNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CIOtherFeeName';
      flagname:='其他费用名称';
      if ShowModal=1 then
      begin
        OtherFeeName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCIInPut.ToolButton1Click(Sender: TObject);
begin
  try
    frmFHOKSelList:=TfrmFHOKSelList.Create(Application);
    with frmFHOKSelList do
    begin
      if ShowModal=1 then
      begin
        with frmFHOKSelList.Order_Main do
        begin
          First;
          while Locate('SSel',True,[]) do
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('PIMainId').Value:=frmFHOKSelList.Order_Main.fieldbyname('PIMainId').Value;
              FieldByName('PISubId').Value:=frmFHOKSelList.Order_Main.fieldbyname('PISubId').Value;
              FieldByName('MainId').Value:=frmFHOKSelList.Order_Main.fieldbyname('MainId').Value;
              FieldByName('SubId').Value:=frmFHOKSelList.Order_Main.fieldbyname('SubId').Value;
              FieldByName('FHID').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHID').Value;
              FieldByName('OrderNo').Value:=frmFHOKSelList.Order_Main.fieldbyname('OrderNo').Value;
              FieldByName('PRTCode').Value:=frmFHOKSelList.Order_Main.fieldbyname('PRTCode').Value;
              FieldByName('PRTColor').Value:=frmFHOKSelList.Order_Main.fieldbyname('PRTColor').Value;
              FieldByName('PRTHX').Value:=frmFHOKSelList.Order_Main.fieldbyname('PRTHX').Value;
              FieldByName('QtyUnit').Value:=frmFHOKSelList.Order_Main.fieldbyname('QtyUnit').Value;
              FieldByName('FHType').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHType').Value;
              FieldByName('FHDate').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHDate').Value;
              FieldByName('FHPS').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHPS').Value;
              FieldByName('FHQty').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHQty').Value;
              FieldByName('PriceUnit').Value:=frmFHOKSelList.Order_Main.fieldbyname('PriceUnit').Value;
              FieldByName('CSPrice').Value:=frmFHOKSelList.Order_Main.fieldbyname('PRTPrice').Value;
              FieldByName('CSMoney').Value:=frmFHOKSelList.Order_Main.fieldbyname('FHMoney').Value;
              FieldByName('ConNO').Value:=frmFHOKSelList.Order_Main.fieldbyname('ConNO').Value;
              FieldByName('CustomerNoName').Value:=frmFHOKSelList.Order_Main.fieldbyname('CustomerNoName').Value;
              FieldByName('CustomerNo').Value:=frmFHOKSelList.Order_Main.fieldbyname('CustomerNo').Value;
              //FieldByName('OrderNo').Value:=frmFHOKSelList.Order_Main.fieldbyname('OrderNo').Value;
              //FieldByName('OrderNo').Value:=frmFHOKSelList.Order_Main.fieldbyname('OrderNo').Value;
              //FieldByName('OrderNo').Value:=frmFHOKSelList.Order_Main.fieldbyname('OrderNo').Value;
              Post;
            end;
            frmFHOKSelList.Order_Main.Delete;
          end;
        end;

      end;
    end;
  finally
    frmFHOKSelList.Free;
  end;
end;

procedure TfrmCIInPut.v2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then mvalue:='0';
  with Order_Sub do
  begin
    Edit;
    FieldByName('CSPrice').Value:=mvalue;
    Post;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('CSMoney').Value:=FieldByName('CSPrice').Value*FieldByName('FHQty').Value;
  end;
end;

procedure TfrmCIInPut.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Trim(DParameters1)='高权限' then
  begin
    v2Column1.Options.Editing:=True;
  end else
  begin
    v2Column1.Options.Editing:=False;
  end;
end;

end.
