unit U_TradePlanFtyInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  U_FormLayOutDesign, Winapi.ShellAPI, ComObj, 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, cxCurrencyEdit,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit, RM_Common, RM_Preview,
  RM_System, RM_Class, RM_GridReport, RM_Dataset, Menus, cxSplitter,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox, 
   dxDateRanges, dxBarBuiltInMenu, cxPC, System.ImageList,
  Vcl.ImgList, U_BaseInput,  dxCore, cxDateUtils,
  dxScrollbarAnnotations, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, cxImage, cxDBEdit,
  cxGroupBox,    
     
    
     
     
    
    
    
    
    
    
     
      
     
   
    
   cxCheckComboBox, cxImageList, cxButtons,  
  dxSkinWXI, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradePlanFtyInPut = class(TfrmBaseInput)
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    IdFTP1: TIdFTP;
    OpenDialog1: TOpenDialog;
    PM_1: TPopupMenu;
    N5: TMenuItem;
    N6: TMenuItem;
    GPM_2: TcxGridPopupMenu;
    ADO_1: TADOQuery;
    ADO_2: TADOQuery;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label52: TLabel;
    FtyName: TcxButtonEdit;
    FtyQty: TcxTextEdit;
    Label2: TLabel;
    cxButton1: TcxButton;
    TV2Column5: TcxGridDBColumn;
    SerialFtyNo: TcxComboBox;
    Label1: TLabel;
    Label3: TLabel;
    Follower: TcxButtonEdit;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    Panel2: TPanel;
    scnote: TMemo;
    Label4: TLabel;
    scnote1: TMemo;
    Panel3: TPanel;
    Panel4: TPanel;
    scnote2: TMemo;
    ToolButton1: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure FtyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure FtyNameDblClick(Sender: TObject);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    FSCXDH: string;
    procedure InitData();
    procedure InitGrid2();
    function SaveData(): Boolean;

    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTradePlanFtyInPut: TfrmTradePlanFtyInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_ZDYHelpSel, U_RTFun, U_CompanySel;


{$R *.dfm}

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

procedure TfrmTradePlanFtyInPut.InitGrid2();
begin
  with ADO_2 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*   ');
    sql.Add(',Technics=(select Technics  from Trade_Plan_FtyFlow X where X.MainId=A.MainId and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',FtyName1=(select FtyName  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=1 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',Follower1=(select Follower  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=1 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',FtyQty1=(select FtyQty  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=1 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',FtyName2=(select FtyName  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=2 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',Follower2=(select Follower  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=2 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',FtyQty2=(select FtyQty  from Trade_Plan_Fty X where X.subid=A.subid  and x.SerialFtyNo=2 and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' )   ');
    sql.Add(',FtyQtydfp=OrdQty-isnull((select sum(FtyQty)  from Trade_Plan_Fty X where X.subid=A.subid and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' ) ,0)  ');

    sql.Add(' from Trade_Plan_Sub A ');
    sql.Add(' where MainId=''' + Trim(FMainId) + '''');
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);

end;

procedure TfrmTradePlanFtyInPut.InitData();
begin

  with ADO_1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Plan_FtyFlow where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  if ADO_1.IsEmpty then
  begin
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('insert into Trade_Plan_FtyFlow( MainId,SerialNo) values');
      sql.Add(' (' + Quotedstr(Trim(FMainId)) + ',1  ) ');
      sql.Add(',(' + Quotedstr(Trim(FMainId)) + ',2  ) ');
      sql.Add(',(' + Quotedstr(Trim(FMainId)) + ',3  ) ');
      sql.Add(',(' + Quotedstr(Trim(FMainId)) + ',4  ) ');
      sql.Add(',(' + Quotedstr(Trim(FMainId)) + ',5  ) ');
      sql.Add(',(' + Quotedstr(Trim(FMainId)) + ',6  ) ');
      ExecSQL;
    end;
    with ADO_1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_Plan_FtyFlow where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;

  end;
  SCreateCDS(ADO_1, CDS_1);
  SInitCDSData(ADO_1, CDS_1);

end;

procedure TfrmTradePlanFtyInPut.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Follower';
      flagname := '跟单员';
      if ShowModal = 1 then
      begin
        Follower.text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradePlanFtyInPut.cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalue, FFieldName: string;
begin

  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Technics';
      flagname := '工艺';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('Technics').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_1.post;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('update Trade_Plan_FtyFlow set  Technics=' + quotedstr(Trim(Self.CDS_1.fieldbyname('Technics').AsString)));
          sql.Add('where  MainId=' + quotedstr(Trim(FMainId)));
          sql.Add('AND  SerialNo=' + quotedstr(Trim(Self.CDS_1.fieldbyname('SerialNo').AsString)));
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

  Tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanFtyInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;
end;

procedure TfrmTradePlanFtyInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradePlanFtyInPut := nil;
end;

procedure TfrmTradePlanFtyInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;

  readCxGrid(TRIM(SELF.Caption) + 'Tv1', Tv1, '生产指示单管理');
  readCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv2, '生产指示单管理');

  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  InitGrid2();
end;

procedure TfrmTradePlanFtyInPut.FtyNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradePlanFtyInPut.FtyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.FtyName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.FtyName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);

      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

function TfrmTradePlanFtyInPut.SaveData(): Boolean;
var
  MaxTPFId: string;
  MFtyQty: Double;
begin
  try
    ADOCmd.Connection.BeginTrans;
/////////////////////               保存加工厂            ///////////////////////////
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if CDS_2.fieldbyname('ssel').AsBoolean then
        begin

          MaxTPFId := Trim(CDS_2.fieldbyname('Subid').AsString) + Trim(CDS_1.FieldByName('SerialNo').AsString) + trim(SerialFtyNo.Text);

          if StrToFloatDef(FtyQty.Text, 0) = 0 then
          begin
            MFtyQty := CDS_2.fieldbyname('FtyQtydfp').AsFloat + CDS_2.fieldbyname('FtyQty' + trim(SerialFtyNo.Text)).AsFloat;
          end
          else
          begin
            MFtyQty := StrToFloatDef(FtyQty.Text, 0);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('delete Trade_Plan_Fty  where MainId=''' + Trim(FMainId) + '''');
            sql.Add(' and TPFId=''' + Trim(MaxTPFId) + '''');
            execsql;
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from Trade_Plan_Fty  where 1=2');
            Open;
          end;
          with ADOCmd do
          begin
            Append;
            FieldByName('MainId').Value := Trim(FMainId);
            FieldByName('TPFId').Value := Trim(MaxTPFId);
            FieldByName('SerialFtyNo').Value := Trim(SerialFtyNo.Text);
            FieldByName('SerialNo').Value := CDS_1.fieldbyname('SerialNo').Value;
            FieldByName('Technics').Value := CDS_1.fieldbyname('Technics').Value;
            FieldByName('FtyNo').Value := Trim(self.FtyName.Properties.LookupItems.Text);
            FieldByName('FtyName').Value := Trim(self.FtyName.Text);
            FieldByName('Followid').Value := Trim(self.Follower.Properties.LookupItems.Text);
            FieldByName('Follower').Value := Trim(self.Follower.Text);

            FieldByName('subid').Value := CDS_2.fieldbyname('subid').Value;

            FieldByName('FtyQty').Value := MFtyQty;
            FieldByName('SCNOTE').Value := scnote.Text;
            Post;

          end;
          with ADOTemp do
          begin
            Close;
            SQL.Clear;
            sql.Add(' select    ');

            sql.Add(' FtyQtydfp=OrdQty-isnull((select sum(FtyQty)  from Trade_Plan_Fty X where X.subid=A.subid and x.SerialNo=' + CDS_1.FieldByName('SerialNo').asstring + ' ) ,0)  ');

            sql.Add(' from Trade_Plan_Sub A ');
            sql.Add(' where SUBID=''' + Trim(CDS_2.fieldbyname('subid').Value) + '''');
//    ShowMessage(sql.Text);
            Open;
          end;
          if ADOTemp.FieldByName('FtyQtydfp').AsFloat < 0 then
          begin
            raise Exception.Create('分配数量超过总数!');
          end;

        end;
        Next;
      end;
    end;



/////////////////////               保存加工厂           ///////////////////////////

    ADOCmd.Connection.CommitTrans;

    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;
end;

procedure TfrmTradePlanFtyInPut.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.FieldByName('Technics').AsString = '' then
  begin
    Exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := CDS_1.FieldByName('Technics').AsString;
      flagname := CDS_1.FieldByName('Technics').AsString;
      if ShowModal = 1 then
      begin
//        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradePlanFtyInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(TRIM(SELF.Caption) + 'Tv1', Tv1, '生产指示单管理');
  WriteCxGrid(TRIM(SELF.Caption) + 'Tv2', Tv2, '生产指示单管理');
end;

procedure TfrmTradePlanFtyInPut.TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitGrid2();
end;

procedure TfrmTradePlanFtyInPut.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select scnote ');

    sql.Add(' from Trade_Plan_Fty A ');
    sql.Add(' where subid=''' + Trim(CDS_2.fieldbyname('SubId').AsString) + '''');
    sql.Add('and SerialNo=1');

    Open;
  end;
  scnote1.Text := ADOTemp.FieldByName('scnote').AsString;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select scnote ');

    sql.Add(' from Trade_Plan_Fty A ');
    sql.Add(' where subid=''' + Trim(CDS_2.fieldbyname('SubId').AsString) + '''');
    sql.Add('and SerialNo=2');

    Open;
  end;
  scnote2.Text := ADOTemp.FieldByName('scnote').AsString;

end;

procedure TfrmTradePlanFtyInPut.cxButton1Click(Sender: TObject);
begin

//  with ADOTemp do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from Trade_Plan_Fty_Technical where isnull(SubId,'''')<>'''' and SubId=''' + Trim(CDS_3.fieldbyname('SubId').AsString) + '''');
//        Open;
//      end;
//      if ADOTemp.IsEmpty = False then
//      begin
//        Application.MessageBox('已检验不能删除数据!', '提示', 0);
//        Exit;
//      end;
//      if Trim(CDS_3.fieldbyname('TPFTID').AsString) <> '' then
//      begin
//        with ADOCmd do
//        begin
//          Close;
//          sql.Clear;
//
////          sql.Add('insert into Trade_Plan_Sub_Del select * from Trade_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
//
//          sql.Add('delete Trade_Plan_Fty_Technical where TPFTID=''' + Trim(CDS_3.fieldbyname('TPFTID').AsString) + '''');
//
//          ExecSQL;
//        end;
//      end;


  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    InitGrid2();
  end;
end;

procedure TfrmTradePlanFtyInPut.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_2, True);
end;

procedure TfrmTradePlanFtyInPut.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_2, False);
end;

end.

