unit U_JHFXLIST;

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, cxImage, cxBlobEdit,
  cxImageComboBox, ImgList, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  Tfrmjhfxlist = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    CDS_Type: TClientDataSet;
    Label34: TLabel;
    ConNo: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label1: TLabel;
    GroupBox1: TGroupBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    GroupBox2: TGroupBox;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    order_Sz: TClientDataSet;
    DataSource2: TDataSource;
    Label2: TLabel;
    P_Name: TEdit;
    GroupBox3: TGroupBox;
    Label3: TLabel;
    RKPS: TEdit;
    Label4: TLabel;
    CKPS: TEdit;
    Label5: TLabel;
    KCPS: TEdit;
    Label6: TLabel;
    Label8: TLabel;
    Label7: TLabel;
    RKQTY: TEdit;
    CKQTY: TEdit;
    KCQTY: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    GroupBox4: TGroupBox;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    WFRKPS: TEdit;
    WFCKPS: TEdit;
    WFKCPS: TEdit;
    WFRKQTY: TEdit;
    WFCKQTY: TEdit;
    WFKCQTY: TEdit;
    TV2Column1: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TV2Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNameBtnDnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure DlyNoteBtnUpClick(Sender: TObject);
    procedure ZLNoteDblClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1PRTPricePropertiesEditValueChanged(Sender: TObject);
    procedure v1Column10PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    fuserName: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);

   

    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    FMainId, FFMainId: string;
    canshu1, FGSTT: string;
    { Public declarations }
  end;

var
  frmjhfxlist: Tfrmjhfxlist;
  newh: hwnd;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_CPManageFMSel, U_ZdyAttachment,
  U_CPManage, U_CPSel, getpic;

{$R *.dfm}

procedure Tfrmjhfxlist.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(trim(self.Caption), Tv1, '销售合同管理');
  WriteCxGrid(trim(self.Caption) + 'TV2', Tv2, '销售合同管理');
  ModalResult := 1;
end;

procedure Tfrmjhfxlist.InitData();
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_ht_jhlist2 @consubid=''' + Trim(FMainId) + '''');

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

  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_ht_jhlist @consubid=''' + Trim(FMainId) + '''');

  //  ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOTemp, order_Sz);
  SInitCDSData20(ADOTemp, order_Sz);

   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
//    sql.Add('select P_Name,CONNO from Contract_Main A INNER JOIN Contract_SUB B ON A.CONMAINID=B.CONMAINID where CONSUBID=''' + Trim(FMainId) + '''');
    sql.Add('exec P_ht_jhlist_hz @consubid=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOTemp, ScrollBox1, 2);
  SCSHDataNew(ADOTemp, groupBOX3, 2);
  SCSHDataNew(ADOTemp, groupBOX4, 2);
end;

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

procedure Tfrmjhfxlist.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption), Tv1, '销售合同管理');
  readCxGrid(trim(self.Caption) + 'TV2', Tv2, '销售合同管理');
  InitData();
end;

procedure Tfrmjhfxlist.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmZDYHelp:=TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag:='OrdColor';
//      flagname:='颜色';
//      V1Name.Caption:='中文名称';
//      V1Note.Caption:='英文名称';
//      fnote:=True;
//      MainType:=Trim(DName);
//      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 Tfrmjhfxlist.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from  PD_Plan_Sub B INNER JOIN PD_PLAN_MAIN A ON A.MAINID=B.MAINID where conSubId=''' + Trim(Order_Sub.fieldbyname('conSubId').AsString) + '''');
    SQL.Add('AND A.VALID=''Y''');
    open;
  end;
  if ADOTemp.IsEmpty = false then
  begin
    application.MessageBox('该明细已经在生产！', '提示信息');
    exit;
  end;
  if Trim(Order_Sub.fieldbyname('conSubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where conSubId=''' + Trim(Order_Sub.fieldbyname('conSubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure Tfrmjhfxlist.BuyNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure Tfrmjhfxlist.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
type
  TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; Language: integer; WinStyle: integer; GCode: Pchar; GName: Pchar; DataBase: Pchar; Title: PChar; Parameters1: PChar; Parameters2: PChar; Parameters3: PChar; Parameters4: PChar; Parameters5: PChar; Parameters6: PChar; Parameters7: PChar; Parameters8: PChar; Parameters9: PChar; Parameters10: PChar; DataBaseStr: PChar): hwnd; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
  LabInt, labname: string;
begin
  //if PPInt=2 then Exit;
  Ddatabase := StringOfChar(' ', 32);
  Th := LoadLibrary('LabelSet.dll');
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'GetDllForm');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh := Tf(Application, 0, 2, 0, 0, PChar(DCode), PChar(DName), PChar(Ddatabase), PChar('标签模版设置'), PChar(''), PChar(''), '', '', '', '', '', '', '', '', PChar(DConString));
        if Trim(PChar(Ddatabase)) <> '' then
        begin
          Ddatabase := Trim(PChar(Ddatabase));
          LabInt := Trim(LeftBStr(Ddatabase, Pos('|', Ddatabase) - 1));
          labname := Trim(RightBStr(Ddatabase, Length(Ddatabase) - Pos('|', Ddatabase)));
          with Order_Sub do
          begin
            Edit;
            FieldByName('SLbName').Value := labname;
            FieldByName('SLbInt').Value := LabInt;
          end;
        end;
      end
      else
      begin
        ShowMessage('打印执行错误');
      end;
    finally
    // FreeLibrary();
    end;
  end
  else
  begin
    ShowMessage('找不到' + Trim('LabelSet.dll'));
  end;

end;

procedure Tfrmjhfxlist.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 Tfrmjhfxlist.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('PRTCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure Tfrmjhfxlist.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTMF';
      flagname := '门幅信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTMF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure Tfrmjhfxlist.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTKZ';
      flagname := '克重信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTKZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure Tfrmjhfxlist.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin
  frmCPSel := TfrmCPSel.create(self);
  with frmCPSel do
  begin
    if showmodal = 1 then
    begin
      cds_main.DisableControls;
      cds_main.first;
      while not cds_main.Eof do
      begin
        if cds_main.FieldByName('ssel').AsBoolean then
        begin
          Order_Sub.Edit;
          MCYNo := CDS_Main.fieldbyname('CYNo').asstring;
          Order_Sub.fieldbyname('P_Code').Value := MCYNo;
          Order_Sub.fieldbyname('P_Name').Value := CDS_Main.fieldbyname('CYName').asstring;
          Order_Sub.fieldbyname('P_Spec').Value := CDS_Main.fieldbyname('CYSpec').asstring;
          Order_Sub.fieldbyname('P_CF').Value := CDS_Main.fieldbyname('CYCF').asstring;
          Order_Sub.fieldbyname('P_Color').Value := CDS_Main.fieldbyname('CYColor').asstring;
          Order_Sub.fieldbyname('P_HX').Value := CDS_Main.fieldbyname('CYHX').asstring;
          Order_Sub.fieldbyname('P_MF').Value := CDS_Main.fieldbyname('CYMF').asstring;
          Order_Sub.fieldbyname('P_KZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;
          Order_Sub.Post;
        end;
        cds_main.next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure Tfrmjhfxlist.DlyNoteBtnUpClick(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 ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure Tfrmjhfxlist.ZLNoteDblClick(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
        TMemo(Sender).Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              TMemo(Sender).Lines.Add(FieldByName('ZDYName').AsString)      //inttostr(i)+'.'+
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure Tfrmjhfxlist.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('YongJinPrice').Value := mvalue;
    FieldByName('YongJin').Value := FieldByName('YongJinPrice').AsFloat * FieldByName('PRTOrderQty').AsFloat;
    Post;
  end;
end;

procedure Tfrmjhfxlist.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQuantity, FUnitPrice, FCourierCharge, FPiqty, Fmxmoney, Fpilen: double;
begin
  with Order_Sub do
  begin
    if TcxTextEdit(Sender).EditingText = '' then
    begin
      mvalue := '0';
    end
    else
      mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQuantity := FieldByName('Qty').asfloat;
    FUnitPrice := FieldByName('Price').asfloat;
    FPiqty := FieldByName('piQty').asfloat;
    Fpilen := FieldByName('pilen').asfloat;

  end;
  if (FFieldName = 'Qty') or (FFieldName = 'Price') then
  begin
    Fmxmoney := FQuantity * FUnitPrice;
  end
  else
  begin
    FQuantity := FPiqty * Fpilen;
    Fmxmoney := FQuantity * FUnitPrice;
  end;
  with Order_Sub do
  begin
    Edit;
    //if (FFieldName = 'PiQty') or (FFieldName = 'piLen') then

    FieldByName('Qty').Value := FQuantity;

    FieldByName('MxMoney').Value := Fmxmoney;
    Post;
  end;

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

procedure Tfrmjhfxlist.v1PRTPricePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQuantity, FUnitPrice, FCourierCharge: double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;

    FQuantity := FieldByName('Qty').asfloat;
    FUnitPrice := FieldByName('Price').asfloat;
  end;

  with Order_Sub do
  begin
    Edit;
    FieldByName('MxMoney').Value := (FQuantity * FUnitPrice);
    Post;
  end;

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

procedure Tfrmjhfxlist.v1Column10PropertiesEditValueChanged(Sender: TObject);
var
  mavlue, FFieldName: string;
begin
  mavlue := TcxTextEdit(Sender).EditingText;
  FFieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Sub do
  begin
    edit;
    FieldByName(FFieldName).Value := UpperCase(mavlue);
    post;
  end;

end;

function UpperCase(const S: string): string;
var
  Ch: Char;
  L: Integer;
  Source, Dest: PChar;
begin
  L := Length(S);
  SetLength(Result, L);
  Source := Pointer(S);
  Dest := Pointer(Result);
  while L <> 0 do
  begin
    Ch := Source^;
    if (Ch >= 'a') and (Ch <= 'z') then
      Dec(Ch, 32);
    Dest^ := Ch;
    Inc(Source);
    Inc(Dest);
    Dec(L);
  end;
end;

procedure Tfrmjhfxlist.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[Tv1Column1.Index] = '已上轴') or (AViewInfo.GridRecord.Values[Tv1Column1.Index] = '已外发') then
    ACanvas.Brush.Color := $0080FF80;
end;

procedure Tfrmjhfxlist.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TV2Column2.Index] = '完成' then
    ACanvas.Brush.Color := $0080FF80;
end;

end.

