unit U_CPFHSQXZ;

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
  TfrmCPFHSQXZ = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    v1Column14: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    YWY: TBtnEditC;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    btnClose: TToolButton;
    TBSave: TToolButton;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ShouKuanKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnDnClick(Sender: TObject);
    procedure OrderNoBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure YWYBtnUpClick(Sender: TObject);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btnCloseClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    procedure GetKCSel(MKCStatus: string);
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FMainId, FFMainId, ChkStr, ConPriceStr: string;
    FXS: Integer;
    FClothType: string; 
    { Public declarations }
  end;

var
  frmCPFHSQXZ: TfrmCPFHSQXZ;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_SQKCHZSelList, U_ZdyAttachGYS,
  U_ZdyAttachment, U_SQKCHZMXSel;

{$R *.dfm}
procedure TfrmCPFHSQXZ.GetKCSel(MKCStatus: string);
var
  FSelType, FConNo, FMPRTCodeName, FPRTColor, FgangNo, FMJID: string;
  FEFalg, FPiQty, FMJXH: Integer;
begin

  try
    frmSQKCHZSelList := TfrmSQKCHZSelList.Create(Application);
    with frmSQKCHZSelList do
    begin
      FKCStatus := MKCStatus;
      FYWY := YWY.Text;
      if ShowModal = 1 then
      begin
        while frmSQKCHZSelList.CDS_Main.Locate('SSel', true, []) do
        begin
          FEFalg := 0;
          FSelType := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('SelType').AsString);
          FConNo := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('ConNo').AsString);
          FMPRTCodeName := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MPRTCodeName').AsString);
          FPRTColor := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('PRTColor').AsString);
          FgangNo := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('gangNo').AsString);
          FPiQty := frmSQKCHZSelList.CDS_Main.fieldbyname('PiQty').AsInteger;
          FMJID := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MJID').AsString);
          FMJXH := frmSQKCHZSelList.CDS_Main.fieldbyname('MJXH').AsInteger;
          if FSelType = '品名' then
          begin
            if Self.Order_Sub.Locate('PRTCodeName', FMPRTCodeName, []) then
              FEFalg := FEFalg - 1;
          end;
          if FSelType = '订单' then
          begin
            if Self.Order_Sub.Locate('SelType;PRTCodeName', VarArrayOf(['品名', FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SCConNo;PRTCodeName', VarArrayOf([FConNo, FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;
          end;
          if FSelType = '颜色' then
          begin
            if Self.Order_Sub.Locate('SelType;PRTCodeName', VarArrayOf(['品名', FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName', VarArrayOf(['订单', FConNo, FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SCConNo;PRTCodeName;PRTColor', VarArrayOf([FConNo, FMPRTCodeName, FPRTColor]), []) then
              FEFalg := FEFalg - 1;
          end;
          if FSelType = '缸号' then
          begin
            if Self.Order_Sub.Locate('SelType;PRTCodeName', VarArrayOf(['品名', FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName', VarArrayOf(['订单', FConNo, FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName;PRTColor', VarArrayOf(['颜色', FConNo, FMPRTCodeName, FPRTColor]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SCConNo;PRTCodeName;PRTColor;ganghao', VarArrayOf([FConNo, FMPRTCodeName, FPRTColor, FgangNo]), []) then
              FEFalg := FEFalg - 1;
          end;
          if FSelType = '明细' then
          begin
            if Self.Order_Sub.Locate('SelType;PRTCodeName', VarArrayOf(['品名', FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName', VarArrayOf(['订单', FConNo, FMPRTCodeName]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName;PRTColor', VarArrayOf(['颜色', FConNo, FMPRTCodeName, FPRTColor]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;SCConNo;PRTCodeName;PRTColor;ganghao', VarArrayOf(['缸号', FConNo, FMPRTCodeName, FPRTColor, FgangNo]), []) then
              FEFalg := FEFalg - 1;

            if Self.Order_Sub.Locate('SelType;MJID', VarArrayOf(['明细', FMJID]), []) then
              FEFalg := FEFalg - 1;
          end;
          if FEFalg = 0 then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('SelType').Value := FSelType;
              FieldByName('SCConNo').Value := FConNo;
              FieldByName('PrtCodeName').Value := FMPRTCodeName;
              FieldByName('PRTColor').Value := FPRTColor;
              FieldByName('ganghao').Value := FgangNo;
              FieldByName('SQPS').Value := FPiQty;
              FieldByName('MJXH').Value := FMJXH;
              FieldByName('MJID').Value := FMJID;
              Post;
            end;
          end;

          frmSQKCHZSelList.CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmSQKCHZSelList.Free;
  end;
end;

procedure TfrmCPFHSQXZ.InitData();
var
  fsj: string;
begin

  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from FHSQ_Sub_CP A');
    sql.Add('where FSId=''' + Trim(FMainId) + '''');

    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

end;

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

procedure TfrmCPFHSQXZ.FormShow(Sender: TObject);
begin
  readCxGrid('发货申请录入', Tv1, '发货管理');
  InitData();
end;

function TfrmCPFHSQXZ.SaveData(): Boolean;
var
  maxno, maxmnno, FSSId: string;
  fconNO, fmxType: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    FMainId := Trim('限制池');
    ///保存子表
    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_CP', 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_CP ');
          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_CP', 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;
    end;
    Order_Sub.EnableControls;

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

procedure TfrmCPFHSQXZ.ToolButton1Click(Sender: TObject);
begin
  GetKCSel('已开卡');
end;

procedure TfrmCPFHSQXZ.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SSId').AsString) <> '' then
  begin

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

  end;
  Order_Sub.Delete;
end;

procedure TfrmCPFHSQXZ.ShouKuanKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

procedure TfrmCPFHSQXZ.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmCPManageFMSel:=TfrmCPManageFMSel.Create(Application);
//    with frmCPManageFMSel do
//    begin
//      if ShowModal=1 then
//      begin
//        Self.PrtCodeName.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString);
//        Self.PrtCode.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYNo').AsString);
////        Self.C_Spec.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYSpec').AsString);
//      end;
//    end;
//  finally
//    frmCPManageFMSel.Free;
//  end;
end;

procedure TfrmCPFHSQXZ.SalesmanBtnDnClick(Sender: TObject);
begin
  YWY.Text := '';
end;

procedure TfrmCPFHSQXZ.OrderNoBtnUpClick(Sender: TObject);
begin
//  try
//    frmClothSCListSel := TfrmClothSCListSel.Create(Application);
//    with frmClothSCListSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        self.OrderNo.Text := trim(Order_Main.FieldByName('OrderNo').AsString);
//        self.SHDanWei.Text := trim(Order_Main.FieldByName('JGFactoryName').AsString);
//        self.Salesman.Text := trim(Order_Main.FieldByName('YWY').AsString);
//      end;
//    end;
//  finally
//    frmClothSCListSel.Free;
//  end;
end;

procedure TfrmCPFHSQXZ.ToolButton3Click(Sender: TObject);
begin

  GetKCSel('已打卷');

end;

procedure TfrmCPFHSQXZ.ToolButton4Click(Sender: TObject);
begin
  GetKCSel('未开卡');
end;

procedure TfrmCPFHSQXZ.YWYBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Salesman';
      flagname := '业务员';
      if ShowModal = 1 then
      begin
        Self.YWY.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPFHSQXZ.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          edit;
          FieldByName('PBFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmCPFHSQXZ.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          edit;
//          FieldByName('FactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COCode').AsString);
          FieldByName('RSFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COName').AsString);

        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

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

procedure TfrmCPFHSQXZ.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;

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

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

end.

