unit U_frmFabSQ;

interface

uses
  Windows, Messages, SysUtils, strutils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, ADODB, cxGridCustomPopupMenu,
  cxGridPopupMenu, DBClient, BtnEdit, cxBlobEdit, cxShellComboBox,
  cxDropDownEdit, cxFontNameComboBox, cxButtonEdit, cxCurrencyEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter, cxContainer, cxTextEdit, cxMaskEdit;

type
  TfrmFabSQ = class(TForm)
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    SQNo: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ToolBar2: TToolBar;
    TAdd: TToolButton;
    TDel: TToolButton;
    TVC_Note: TcxGridDBColumn;
    SQDate: TDateTimePicker;
    ADOQueryTmp: TADOQuery;
    ADOQueryCMD: TADOQuery;
    DataSource1: TDataSource;
    TVC_Qty: TcxGridDBColumn;
    TVC_Unit: TcxGridDBColumn;
    Note: TMemo;
    Cds_Sub: TClientDataSet;
    SQName: TBtnEditA;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label5: TLabel;
    Label6: TLabel;
    PONo: TEdit;
    Label7: TLabel;
    defstr1: TBtnEditA;
    ConNo: TBtnEditA;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Label12: TLabel;
    defstr5: TBtnEditA;
    defstr6: TBtnEditA;
    defstr7: TBtnEditA;
    defstr2: TEdit;
    defstr3: TEdit;
    defstr4: TEdit;
    Label14: TLabel;
    customerNoName: TEdit;
    V1Column6: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    factoryName: TBtnEditA;
    P_CodeName: TEdit;
    P_Spec: TEdit;
    P_CF: TEdit;
    P_MF: TEdit;
    P_KZ: TEdit;
    Label21: TLabel;
    FYNO: TBtnEditA;
    Label22: TLabel;
    defstr8: TEdit;
    Label23: TLabel;
    CompanyName: TcxButtonEdit;
    procedure TCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TVC_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure NoteDblClick(Sender: TObject);
    procedure SQNameBtnClick(Sender: TObject);
    procedure TVC_Unit2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TVC_UnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure V1JGafterPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNoBtnClick(Sender: TObject);
    procedure defstr1BtnClick(Sender: TObject);
    procedure defstr5BtnClick(Sender: TObject);
    procedure defstr6BtnClick(Sender: TObject);
    procedure defstr7BtnClick(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure FYNOBtnClick(Sender: TObject);
    procedure CompanyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    function savedata(): Boolean;
    procedure initdata();
    { Private declarations }
  public
    formid: integer;
    FMainid: string;
    { Public declarations }
  end;

var
  frmFabSQ: TfrmFabSQ;

implementation

uses
  U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_ContractList_CX, U_DataLink, U_ZdyAttachGYS,
  U_FanYangList_sel;

{$R *.dfm}

procedure TfrmFabSQ.initdata();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from contract_Main_Sh ');
    sql.Add('where Mainid=''' + trim(Fmainid) + '''');
    open;
    if not isempty then
    begin
      SCSHDataNew(adoqueryTmp, Panel1, 0);
    end;
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.add('select * from contract_Sub_Sh ');
    sql.Add('where Mainid=''' + trim(Fmainid) + '''');
    open;
    SCreateCDS20(ADOQueryTmp, Cds_Sub);
    SInitCDSData20(ADOQueryTmp, Cds_Sub);
  end;
end;

function TfrmFabSQ.savedata(): Boolean;
var
  maxNo: string;
  fSQNo: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if trim(Fmainid) = '' then
    begin
      if getLSNo(ADOQueryTmp, fSQNo, trim(conNO.text), 'contract_Main_Sh', 3, 0) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('产生流水号失败', '提示', 0);
        exit;
      end;
      sQNO.text := 'SQ-' + trim(conNO.text) + '-' + rightBstr(fSQNo, 3);
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.add('select * from contract_main_Sh ');
        sql.Add('where SQNo=' + quotedstr(trim(SQNo.Text)));
        open;
        if not isempty then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox('申请单号已存在', '提示');
          exit;
        end;
      end;
      if getLSNo(ADOQueryTmp, Fmainid, 'SM', 'contract_Main_Sh', 3, 1) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('产生流水号失败', '提示', 0);
        exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('select * from contract_main_Sh ');
      sql.Add('where Mainid=''' + trim(Fmainid) + '''');
      open;
      if ADOQueryCmd.IsEmpty then
      begin
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('Filler').Value := DName;
      end
      else
      begin
        ADOQueryCmd.Edit;
        ADOQueryCmd.FieldByName('editer').Value := DName;
        ADOQueryCmd.FieldByName('editTime').Value := now();
      end;
      SSetsaveSqlNew(ADOQueryCmd, 'contract_main_Sh', Panel1, 0);
      fieldbyname('Mainid').Value := Fmainid;
      fieldbyname('SQDate').Value := formatdatetime('yyyy-MM-dd', SQDate.DateTime);
      fieldbyname('chkstatus').Value := '0';
      post;
    end;
    with cds_sub do
    begin
      first;
      while not Eof do
      begin
        if trim(cds_Sub.fieldbyname('subID').asstring) = '' then
        begin
          if GetLSNo(ADOQueryTmp, maxNo, 'Sh', 'contract_sub_Sh', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('产生子流水号失败', '提示', 0);
            exit;
          end;
        end
        else
        begin
          maxNo := trim(cds_Sub.fieldbyname('subID').asstring);
        end;
        with ADOQueryTmp do
        begin
          close;
          sql.Clear;
          sql.Add('select * from contract_sub_Sh ');
          sql.Add('where Mainid=''' + trim(Fmainid) + '''');
          sql.Add('and subID=''' + trim(maxNo) + '''');
          open;
          if isempty then
            append
          else
            edit;
          fieldbyname('Mainid').Value := trim(FMainid);
          fieldbyname('subid').Value := trim(maxNo);
          RTSetSaveDataCDS(ADOQueryTmp, Tv1, Cds_Sub, 'contract_sub_Sh', 0);
          fieldbyname('C_Qty').Value := cds_Sub.fieldbyname('C_Qty').asfloat;
          post;
        end;
        cds_Sub.Edit;
        cds_Sub.FieldByName('SubId').Value := Trim(maxNo);
        Next;
      end;
    end;
    ADOQuerycmd.Connection.CommitTrans;
    application.messagebox('保存成功', '提示', 0);
    ModalResult := 1;
  except
    ADOQueryCmd.connection.RollbackTrans;
    application.messagebox('保存失败', '提示', 0);
  end;
end;

procedure TfrmFabSQ.TCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmFabSQ.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + tv1.Name + '1', Tv1);
  if formid = 2 then
  begin
    TSave.Enabled := False;
    ToolBar2.Visible := false;
  end;
  SQDate.datetime := SGetServerDateTime(ADOQueryTmp);
  SQName.Text := trim(DName);
  initdata();

  if formid = 3 then
  begin
    SQNo.Text := '自动生成';
    FMainId := '';
    formid := 0;
    with Cds_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        Post;
        Next;
      end;
    end;
  end;
end;

procedure TfrmFabSQ.FormCreate(Sender: TObject);
begin
  cxGrid1.Align := alclient;
end;

procedure TfrmFabSQ.TSaveClick(Sender: TObject);
begin
  if trim(SQNo.Text) = '' then
  begin
    application.MessageBox('申请单号不能为空', '提示', 0);
    exit;
  end;
  if trim(ConNo.Text) = '' then
  begin
    application.MessageBox('合同号不能为空', '提示', 0);
    exit;
  end;
  if trim(P_CodeName.Text) = '' then
  begin
    application.MessageBox('产品名称不能为空', '提示', 0);
    exit;
  end;
  if Cds_Sub.IsEmpty then
  begin
    application.MessageBox('明细不能为空', '提示', 0);
    exit;
  end;
  savedata();
end;

procedure TfrmFabSQ.TAddClick(Sender: TObject);
begin
  with Cds_Sub do
  begin
    append;
    post;
  end;
end;

procedure TfrmFabSQ.TDelClick(Sender: TObject);
begin
  if Cds_Sub.IsEmpty then
    Exit;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from contract_Sub ');
    sql.add('where Sdefstr7=''' + trim(Cds_Sub.fieldbyname('subid').asstring) + '''');
    open;
    if not isempty then
    begin
      application.MessageBox('已采购不能撤销', '提示');
      exit;
    end;
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.Add('delete from contract_sub_Sh where subID=''' + trim(cds_sub.fieldbyname('subID').AsString + ''''));
    execsql;
  end;
  cds_sub.Delete;
end;

procedure TfrmFabSQ.FormDestroy(Sender: TObject);
begin
  FrmFabSQ := nil;
end;

procedure TfrmFabSQ.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + tv1.Name + '1', Tv1);
  Action := cafree;
end;

procedure TfrmFabSQ.TVC_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if showmodal = 1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_CodeName').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmFabSQ.NoteDblClick(Sender: TObject);
var
  i: integer;
begin
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := 'note';
      flagname := '备注';
      if ShowModal = 1 then
      begin
        note.Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              note.Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmFabSQ.SQNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag := 'SQName';
      flagname := '申请人';
      if showmodal = 1 then
      begin
        SQName.Text := trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.TVC_Unit2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag := 'c_Unit2';
      flagname := '订单数量单位';
      if showmodal = 1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_unit2').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmFabSQ.TVC_UnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.create(self);
    with frmZDYHelp do
    begin
      flag:='c_Unit2';
      flagname:='数量单位';
      if showmodal=1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('C_unit').AsString:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.free;
  end;}
end;

procedure TfrmFabSQ.V1JGafterPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Factory';
      flagname := '后加工';
      MainType := 'PBFactory';
      if ShowModal = 1 then
      begin
        with Cds_Sub do
        begin
          Edit;
          FieldByName('JGafter').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.ConNoBtnClick(Sender: TObject);
begin
  frmContractList_CX := TfrmContractList_CX.create(self);
  with frmContractList_CX do
  begin
    if showModal = 1 then
    begin
      Self.ConNo.Text := Order_Main.fieldbyname('ConNo').AsString;
   //   Self.SQNo.Text:=Order_Main.fieldbyname('ConNo').AsString;
      Self.PONo.Text := Order_Main.fieldbyname('KHConNO').AsString;
      Self.customerNoName.Text := Order_Main.fieldbyname('customerNoName').AsString;
      Self.P_CodeName.Text := ADOQuerySub.fieldbyname('PrtCodeName').AsString;
      Self.P_Spec.Text := ADOQuerySub.fieldbyname('PRTspec').AsString;
      Self.P_CF.Text := ADOQuerySub.fieldbyname('PRTcf').AsString;
      Self.P_MF.Text := ADOQuerySub.fieldbyname('PRTMF').AsString;
      Self.P_KZ.Text := ADOQuerySub.fieldbyname('PRTKZ').AsString;
      Self.CompanyName.Text := Order_Main.fieldbyname('SYRName').AsString;
      with ADOQuerySub do
      begin                          
        first;
        while not eof do
        begin
          with Cds_Sub do
          begin
            append;
            fieldbyname('C_Color').Value := ADOQuerySub.fieldbyname('PRTColor').AsString;
            fieldbyname('C_Qty').Value := ADOQuerySub.fieldbyname('PRTOrderQty').AsString;
            fieldbyname('C_Unit').Value := ADOQuerySub.fieldbyname('OrderUnit').AsString;
            fieldbyname('Sdefstr2').Value := Order_Main.fieldbyname('DlyDate').AsString;
            post;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmFabSQ.defstr1BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag := 'SQBS';
      flagname := '备纱';
      if showmodal = 1 then
      begin
        defstr1.Text := trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.defstr5BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag := 'SQHJG';
      flagname := '后加工';
      if showmodal = 1 then
      begin
        defstr5.Text := trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.defstr6BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag := 'SQXS';
      flagname := '销售方式';
      if showmodal = 1 then
      begin
        defstr6.Text := trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.defstr7BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag := 'SQYS';
      flagname := '验收方式';
      if showmodal = 1 then
      begin
        defstr7.Text := trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFabSQ.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        factoryName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmFabSQ.FYNOBtnClick(Sender: TObject);
begin
  FrmFanYangList_sel := TFrmFanYangList_sel.create(self);
  with FrmFanYangList_sel do
  begin
    if showmodal = 1 then
    begin
      self.FYNO.text := Order_Main.fieldbyname('orderNo').asstring;
    end;
    free;
  end;
end;

procedure TfrmFabSQ.CompanyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdDefStr2';
      flagname := '需方';
      if ShowModal = 1 then             
      begin
//        FXS := 99;
        CompanyName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

