unit U_FHSQInPut;

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, cxCalendar, StrUtils,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmFHSQInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    Label14: TLabel;
    v1Column4: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    CRType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    SHDanWei: TBtnEditC;
    CustomerNoName: TBtnEditA;
    v1Column1: TcxGridDBColumn;
    cv1Column3: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    dazi1: TcxStyle;
    cv1Column8: TcxGridDBColumn;
    lbl2: TLabel;
    DLYDATE: TDateTimePicker;
    Label3: TLabel;
    GuiXing: TBtnEditC;
    v1Column2: TcxGridDBColumn;
    Label7: TLabel;
    XSType: TComboBox;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label2: TLabel;
    THDATE: TDateTimePicker;
    Label4: TLabel;
    FKNote: TBtnEditC;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label5: TLabel;
    FPNO: TEdit;
    Label6: TLabel;
    FKDanWei: TBtnEditC;
    Tv1Column1: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Label8: TLabel;
    DCNO: TBtnEditC;
    Tv1Column2: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure SHDanWeiBtnUpClick(Sender: TObject);
    procedure SHDanWeiBtnDnClick(Sender: TObject);
    procedure CustomerNoNameBtnClick(Sender: TObject);
    procedure GuiXingBtnUpClick(Sender: TObject);
    procedure GuiXingBtnDnClick(Sender: TObject);
    procedure FKNoteBtnUpClick(Sender: TObject);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure FKDanWeiBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure DCNOBtnUpClick(Sender: TObject);
    procedure DCNOBtnDnClick(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FMainId, FFMainId, ChkStr, ConPriceStr: string;
    FXS: Integer;
    { Public declarations }
  end;

var
  frmFHSQInPut: TfrmFHSQInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_SQKCHZSelList, U_ContractListNXSel,
  U_CPManageFMSel, U_ZdyAttachment, U_ClothSCYWYListSel, U_ProductOrderListSel,
  U_DCDList_Sel;

{$R *.dfm}

procedure TfrmFHSQInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货申请录入ZY', Tv1, '发货管理');
end;

procedure TfrmFHSQInPut.InitData();
var
  fsj: string;
begin
  if PState = 0 then
  begin
    DLYDATE.datetime := strtodatetime(Trim(FormatDateTime('yyyy-MM-dd', Now)));
    THDATE.DateTime := DLYDATE.datetime;
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from FHSQ_Sub A');
    if PState = 1 then
    begin
      sql.Add('where FSId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from FHSQ_Main A');
    sql.Add(' where FSId=''' + Trim(FMainId) + ''' ');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 1);

end;

procedure TfrmFHSQInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  readCxGrid('发货申请录入ZY', Tv1, '发货管理');

  InitData();
end;

function TfrmFHSQInPut.SaveData(): Boolean;
var
  maxno, maxmnno, FSSId: string;
  fconNO, fmxType: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'SM', 'FHSQ_Main', 4, 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 FHSQ_Main where FSID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('SQType').Value := '订单';
      FieldByName('FSID').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'FHSQ_Main', ScrollBox1, 1);

      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('SQDate').Value := SGetServerDate(ADOTemp);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;

    FMainId := Trim(maxno);
    ///保存子表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'SS', 'FHSQ_Sub', 4, 1) = False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from FHSQ_Sub ');
          sql.Add(' where SSId=''' + Trim(maxno) + '''');
          Open;
        end;
        if ADOCmd.IsEmpty then
        begin
          FSSId := '';
        end
        else
        begin
          FSSId := Trim(maxno);
        end;
        with ADOCmd do
        begin
          if Trim(FSSId) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('FSID').Value := Trim(FMainId);
          FieldByName('SSID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'FHSQ_Sub', 0);
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('SSConMainId').Value := Order_Sub.fieldbyname('SSConMainId').Value;
          FieldByName('SSConSubId').Value := Order_Sub.fieldbyname('SSConSubId').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SSID').Value := Trim(maxno);
        Next;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('update FHSQ_Main set DBConNo=cast((select '' ''+ SSConNo from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId and X.SSConNo<>FHSQ_Main.ConNo for xml Path('''')) as varchar(100)) ');
        SQL.Add(', SQPSHZ= (select sum(SQPS) from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId ) ');
        SQL.Add(', SQqtyHZ= (select sum(SQqty) from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId ) ');
        SQL.Add('where FSID=''' + Trim(FMainId) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.EnableControls;

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    Result := False;
  end;
end;

procedure TfrmFHSQInPut.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;
  if Trim(XSType.Text) = '' then
  begin
    Application.MessageBox('销售类型不能为空！', '提示', 0);
    Exit;
  end;
  if (Trim(XSType.Text) = '外销') and (Trim(DCNO.Text) = '') and (Trim(CRType.Text) = '销售出库') then
  begin
    Application.MessageBox('外销订舱单号不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(CRType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(CustomerNoName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空！', '提示', 0);
    Exit;
  end;
  if (Order_Sub.Locate('SQPS', null, [])) and (Order_Sub.Locate('SQqty', null, [])) = True then
  begin
    Application.MessageBox('细码申请匹数和数量不能全为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmFHSQInPut.ToolButton1Click(Sender: TObject);
var
  FQtyUnit, FFSubid: string;
begin
//  if Trim(CustomerNoName.Text) = '' then
//  begin
//    Application.MessageBox('客户不能为空!', '提示', 0);
//    Exit;
//  end;
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
//      frmProductOrderListSel.CustomerNoName.Text := Trim(Self.CustomerNoName.Text);
      if ShowModal = 1 then
      begin
        while Order_Main.locate('SSel', true, []) do
        begin
          Self.CustomerNoName.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
          FQtyUnit := Trim(frmProductOrderListSel.Order_Main.fieldbyname('orderUnit').AsString);
//          if Self.Order_Sub.Locate('Subid', Trim(frmProductOrderListSel.Order_Main.fieldbyname('Subid').AsString), []) = False then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('SCConNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('Orderno').AsString);
              FieldByName('SSKHName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
              FieldByName('MainId').Value := frmProductOrderListSel.Order_Main.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmProductOrderListSel.Order_Main.fieldbyname('SubId').Value;
              FieldByName('PRTColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);

              FieldByName('PRTEColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTEColor').AsString);

              FieldByName('SPrtCode').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
              FieldByName('SPrtCodeName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
              FieldByName('PRTHX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
              FieldByName('orderqty').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTOrderQty').AsString);
              FieldByName('orderUnit').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('orderUnit').AsString);
              if Trim(FQtyUnit) = 'Kg' then
              begin
                FieldByName('sqqty').Value := frmProductOrderListSel.Order_Main.fieldbyname('KCkg').Value;
              end
              else if Trim(FQtyUnit) = 'M' then
              begin
                FieldByName('sqqty').Value := frmProductOrderListSel.Order_Main.fieldbyname('KCM').Value;
              end
              else if Trim(FQtyUnit) = 'Y' then
              begin
                FieldByName('sqqty').Value := frmProductOrderListSel.Order_Main.fieldbyname('KCY').Value;
              end;

            end;
          end;
          Order_Main.Delete;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmFHSQInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SSId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      sql.add(' and isnull(FHPS,0)>0');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已发货不能删除数据!', '提示', 0);
      exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from FHSQ_Sub A where A.FSID=FHSQ_Main.FSID)');
      sql.Add('where FSID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SHDanWei';
      flagname := '收货单位';
      if ShowModal = 1 then
      begin
        Self.SHDanWei.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnDnClick(Sender: TObject);
begin
  SHDanWei.Text := '';
end;

procedure TfrmFHSQInPut.CustomerNoNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        Self.CustomerNoName.Text := Trim(frmZdyAttachment.CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmFHSQInPut.GuiXingBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GuiXing';
      flagname := '柜型';
      if ShowModal = 1 then
      begin
        Self.GuiXing.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut.GuiXingBtnDnClick(Sender: TObject);
begin
  GuiXing.Text := '';
end;

procedure TfrmFHSQInPut.FKNoteBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FKNote';
      flagname := '付款条件';
      if ShowModal = 1 then
      begin
        Self.FKNote.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut.v1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FSQPrice, Fsqqty, FSQMoney: Double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FSQPrice := FieldByName('SQPrice').AsFloat;
    Fsqqty := FieldByName('sqqty').AsFloat;
  end;
  FSQMoney := FSQPrice * Fsqqty;
  with Order_Sub do
  begin
    Edit;
    FieldByName('SQMoney').Value := FSQMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmFHSQInPut.FKDanWeiBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FKDanWei';
      flagname := '付款单位';
      if ShowModal = 1 then
      begin
        Self.FKDanWei.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut.ToolButton3Click(Sender: TObject);
begin
  CopyAddRow(Tv1, Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('SSId').Value := '';
    Post;
  end;
end;

procedure TfrmFHSQInPut.DCNOBtnUpClick(Sender: TObject);
begin
  try
    frmDCDList_Sel := TfrmDCDList_Sel.Create(Application);
    with frmDCDList_Sel do
    begin

      if ShowModal = 1 then
      begin
//        self.TOCOUNTRY.Text := Trim(Order_Main.fieldbyname('TOCOUNTRY').AsString);
        self.DCNO.Text := Trim(Order_Main.fieldbyname('DCNO').AsString);
      end;
    end;
  finally
    frmDCDList_Sel.Free;
  end;
end;

procedure TfrmFHSQInPut.DCNOBtnDnClick(Sender: TObject);
begin
  DCNO.Text := '';
end;

end.

