unit U_KDInPut;

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, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmKDInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column18PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType,canshu1:String;

    { Public declarations }
  end;

var
  frmKDInPut: TfrmKDInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_OrderSunSelList,
  U_ZHKHListNewCX;

{$R *.dfm}

procedure TfrmKDInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('寄件信息录入',Tv1,'寄件管理');
end;

procedure TfrmKDInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*');
    SQL.Add(' from KuaiDi_Money A');
    sql.Add(' where A.KDID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  

end;

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

procedure TfrmKDInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('寄件信息录入',Tv1,'寄件管理');
  InitData();
end;
procedure TfrmKDInPut.GetMaxConNO();
begin

end;
function TfrmKDInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('KDID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'KD','KuaiDi_Money',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('KDID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from KuaiDi_Money where KDID='''+Trim(Order_Sub.fieldbyname('KDID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('KDID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('KDID').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'KuaiDi_Money',0) ;
          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;
        Edit;
        FieldByName('KDID').Value:=Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmKDInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('KDDate',null,[]) then
  begin
    Application.MessageBox('寄件日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDComName',null,[]) then
  begin
    Application.MessageBox('快递公司不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDNO',null,[]) then
  begin
    Application.MessageBox('面单号不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KHName',null,[]) then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Country',null,[]) then
  begin
    Application.MessageBox('国家不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDType',null,[]) then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KDFangShi',null,[]) then
  begin
    Application.MessageBox('方式不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('KgQty',null,[]) then
  begin
    Application.MessageBox('重量不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmKDInPut.ToolButton3Click(Sender: TObject);
var
  FSQDate:string;
begin
  FSQDate:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOTemp)));
  with Order_Sub do
  begin
    Append;
    FieldByName('KDDate').Value:=FSQDate;
    Post;
  end;
end;

procedure TfrmKDInPut.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('KDId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KuaiDi_Money where KDId='''+Trim(Order_Sub.fieldbyname('KDId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmKDInPut.v1Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('MDID').Value:=Trim(mvalue);
    Post;
  end;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select B.OrderNo,A.PS,A.MDDate from JYOrder_Main_MD A');
    sql.Add(' inner join JYorder_Main B on A.ORDMainId=B.MainId');
    sql.Add(' and A.MDID='''+Trim(mvalue)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('条码录入错误!','提示',0);
    Exit;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('OrderNo').Value:=ADOTemp.fieldbyname('OrderNo').Value;
    FieldByName('PS').Value:=ADOTemp.fieldbyname('PS').Value;
    Post;
  end;
end;

procedure TfrmKDInPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KDComName';
      flagname:='快递公司';
      if Trim(canshu1)<>'高权限' then
      begin
        frmZDYHelp.TBAdd.Visible:=False;
        frmZDYHelp.TBDel.Visible:=False;
        frmZDYHelp.TBEdit.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KDComName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.v1Column9PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Country';
      flagname:='国家';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('Country').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKDInPut.v1Column18PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZHKHListNewCX:=TfrmZHKHListNewCX.Create(Application);
    with frmZHKHListNewCX do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KHName').Value:=Trim(frmZHKHListNewCX.Order_Main.fieldbyname('KHNameJC').AsString);
        end;
      end;
    end;
  finally
    frmZHKHListNewCX.Free;
  end;
end;

end.
