unit U_OrderInPut_CY;

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, cxCurrencyEdit, cxPC, Menus,
  cxCheckBox, cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmOrderInPut_CY = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxTabControl2: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    v1Column1: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N3: TMenuItem;
    CDS_CG: TClientDataSet;
    DataSource2: TDataSource;
    PopupMenu2: TPopupMenu;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label39: TLabel;
    Label14: TLabel;
    Label19: TLabel;
    Label4: TLabel;
    CYNO: TEdit;
    SQDate: TDateTimePicker;
    CustomerNoName: TBtnEditC;
    CyNote: TMemo;
    v1Column13: TcxGridDBColumn;
    SQPerson: TBtnEditC;
    Address: TEdit;
    Label13: TLabel;
    Label9: TLabel;
    LXPerson: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    FHType: TEdit;
    Label16: TLabel;
    Label18: TLabel;
    FHDate: TDateTimePicker;
    Label22: TLabel;
    PhoneNo: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    SQType: TComboBox;
    Label23: TLabel;
    v1Column3: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure CyNoteDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure conNoChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure SQPersonBtnUpClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmOrderInPut_CY: TfrmOrderInPut_CY;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ProductOrderListSel,
  U_ZdyAttachment,U_ProductOrderListMxSel, U_ZdyAttachGYS;

{$R *.dfm}


procedure TfrmOrderInPut_CY.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.caption),Tv1,'生产指示单管理');
end;

procedure TfrmOrderInPut_CY.InitData();
begin


  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrderCY_Sub where 1=1 ');
    if PState=1 then
    begin
      sql.Add(' and MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrderCY_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel1,2);
  if PState=0 then
  begin
     FHDate.Checked:=False;
  end
  else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    CYNO.Text:='';

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value:='';
        FieldByName('SubId').Value:='';
        Post;
        Next;
      end;
    end;

  end;
end;

procedure TfrmOrderInPut_CY.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
end;

procedure TfrmOrderInPut_CY.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.caption),Tv1,'生产指示单管理');

  InitData();
end;

function TfrmOrderInPut_CY.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'CY','JYOrderCY_Main',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 JYOrderCY_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      SSetsaveSqlNew(ADOCmd,'JYOrderCY_Main',Panel1,2);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('status').Value:='0';
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrderCY_Main where CYNO='''+Trim(CYNO.Text)+'''');
      Open;
    end;
    if ADOCmd.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('出运单号重复!','提示',0);
      Exit;
    end;
    FMainId:=Trim(maxno);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'CS','JYOrderCY_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrderCY_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          FieldByName('OrdMainId').Value:=Trim(Order_Sub.fieldbyname('MainId').AsString);
          FieldByName('OrdSubId').Value:=Trim(Order_Sub.fieldbyname('SubId').AsString);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrderCY_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;



procedure TfrmOrderInPut_CY.TBSaveClick(Sender: TObject);
begin
  SQDate.SetFocus;
   if  TRIM(CYNO.Text)='' then
  begin
    Application.MessageBox('发货交期不能为空','提示',0);
    Exit;
  end;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;

  if  FHDate.Checked=False then
  begin
    Application.MessageBox('发货交期不能为空','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmOrderInPut_CY.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
 //     MainType:=Trim(DName);
      fnote:=True;
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_CY.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;


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

end;

procedure TfrmOrderInPut_CY.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  if trim(SQType.Text)='供应商退货' then
  begin
    try
      frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal=1 then
        begin
          CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        fkhType:='';
        if ShowModal=1 then
        begin
          CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end;
end;

procedure TfrmOrderInPut_CY.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

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

procedure TfrmOrderInPut_CY.v1PriceUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='价格单位';
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBEdit.Visible:=False;
        TBDel.Visible:=False;
      end;

      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_CY.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      MainType:=TEdit(Sender).Name;
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPut_CY.MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;


procedure TfrmOrderInPut_CY.CyNoteDblClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
  i:integer;
begin
  fsj:=Trim(TMemo(Sender).Hint);
  FWZ:=Pos('/',fsj);
  i:=0;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        CyNote.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean=True then
            begin
              i:=i+1;
              CyNote.Lines.Add(inttostr(i)+'.'+FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPut_CY.FormCreate(Sender: TObject);
begin
  FHDate.DateTime:=SGetServerDateTime(ADOTemp);
  SQDate.DateTime:=FHDate.Date;
end;

procedure TfrmOrderInPut_CY.N1Click(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmOrderInPut_CY.N3Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmOrderInPut_CY.conNoChange(Sender: TObject);

var
  ConMainId:string;
begin

 
end;

procedure TfrmOrderInPut_CY.ToolButton4Click(Sender: TObject);
begin
  CDS_CG.Append;
  CDS_CG.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',date());
  CDS_CG.Post;
end;

procedure TfrmOrderInPut_CY.SQPersonBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if Trim(flag)='OrdDefStr2' then
      begin
        //flag:='OrdDefStr2';
        V1Name.Caption:='中文名称';
        V1Note.Caption:='英文名称';
        fnote:=True;
      end;
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag)='MPRTCode' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmOrderInPut_CY.ToolButton1Click(Sender: TObject);
begin
   frmProductOrderListMxSel:=TfrmProductOrderListMxSel.create(self);
   with frmProductOrderListMxSel do
   begin
     if showmodal=1 then
     begin
        with frmProductOrderListMxSel.Order_Main do
        begin
          while Order_Main.Locate('SSel',True,[]) do
          begin
            with Self.Order_Sub do
            begin
              if  Self.Order_Sub.Locate('OrdSubId',Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('SubId').AsString),[])=False then
              begin
                Append;
                FieldByName('PRTCode').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MPRTCode').AsString);
                FieldByName('PRTCodeName').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
                FieldByName('PRTspec').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MPRTspec').AsString);
                FieldByName('PRTKuanNO').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('PRTKuanNO').AsString);
                FieldByName('PRTMF').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MPRTMF').AsString);
                FieldByName('PRTKZ').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MPRTKZ').AsString);
                FieldByName('PRTColor').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('PRTColor').AsString);
                FieldByName('QtyUnit').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('OrderUnit').AsString);
                FieldByName('Qty').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('OrderQty').AsString);
                FieldByName('OrderNo').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('OrderNo').AsString);
                FieldByName('OrdMainId').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('MainId').AsString);
                FieldByName('OrdSubId').Value:=Trim(frmProductOrderListMxSel.Order_Main.fieldbyname('SubId').AsString);
                FieldByName('ps').Value:=   0;
                FieldByName('PrtPrice').Value:=   0;
                Post;
              end;
            end;
            Delete;
          end;
        end;
     end;
     free;
   end;
end;

end.
