unit U_MDdrXx;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  ComObj, Dialogs, ComCtrls, ToolWin, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxStyles, 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, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit,
  cxNavigator, DB, cxDBData, cxCheckBox, cxTextEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, DBClient, cxGridCustomPopupMenu, cxGridPopupMenu,
  ADODB, StdCtrls, ExtCtrls;

type
  Tfrmmddrxx = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    ToolButton3: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Order_Main: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    ADOQueryTmp: TADOQuery;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOTmp: TADOQuery;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    PBFactory: TEdit;
    ConNo: TEdit;
    RanFactory: TEdit;
    Orddefstr5: TEdit;
    PRTCodeName: TEdit;
    Label1: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    procedure ToolButton3Click(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    Fmainid, Fsubid: string;
    { Public declarations }
  end;

var
  frmmddrxx: Tfrmmddrxx;

implementation

uses
  U_RTFun, U_dataLink, U_ClothHCList;
  
{$R *.dfm}
procedure Tfrmmddrxx.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from WFB_MJJY where 1<>1');
    open;
  end;

  SCreateCDS20(ADOQueryTmp, Order_Main);
  SInitCDSData20(ADOQueryTmp, Order_Main);
end;

procedure Tfrmmddrxx.ToolButton3Click(Sender: TObject);
begin
  frmClothHCList := TfrmClothHCList.create(self);
  with frmClothHCList do
  begin
    fType := '10';
    if showmodal = 1 then
    begin                              
//      Self.Order_Main.Edit;
      Fmainid := Trim(Order_Main.fieldbyname('mainid').AsString);
      Fsubid := trim(Order_Main.fieldbyname('Subid').asstring);
      Self.Label3.Caption := '订单号:' + Trim(Order_Main.fieldbyname('OrderNoM').AsString);
      Self.Label4.Caption := '品名中文:' + Trim(Order_Main.fieldbyname('PRTCodeName').AsString);
      Self.Label11.Caption := '颜色:' + Trim(Order_Main.fieldbyname('PRTColor').AsString);
      Self.Label1.Caption := '款号:' + Trim(Order_Main.fieldbyname('PRTkuanNo').AsString);
    end;
    free;
  end;
end;

procedure Tfrmmddrxx.TBAddClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin
  if Fmainid = '' then
  begin
    application.MessageBox('没有选择指示单,请选择指示单！', '错误信息', MB_ICONERROR);
    exit;
  end;
  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with Order_Main do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if (CDS_LM.FieldByName('LXH').AsInteger > 0) then
            Order_Main.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
//        CDS_Import.fieldbyname('OurCoNo').Value := '91330602MA2JTEXX4B';
//        CDS_Import.fieldbyname('OurCoName').Value := '圆中圆';
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

procedure Tfrmmddrxx.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption, Tv1, '码单导入');
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  initGrid();
end;

procedure Tfrmmddrxx.TBRafreshClick(Sender: TObject);
begin
  if Order_Main.Locate('MJXH', null, []) then
  begin
    application.MessageBox('卷号不能为空', '提示');
    exit;
  end;
  SaveDate();
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from WFB_MJJY where 1<>1');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, Order_Main);
  SInitCDSData20(ADOQueryTmp, Order_Main);
end;

procedure Tfrmmddrxx.SaveDate();
var
  MaxNo, FInvoiceID: string;
  XH: Integer;
begin
  if Order_Main.IsEmpty then
    exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with Order_Main do
    begin
      first;
//      XH := 1;
//      if (Order_Main.FieldByName('MJXH').AsInteger >= 1) then
//      begin
      while not eof do
      begin
        if (Order_Main.FieldByName('MJXH').AsInteger < 1) then
        begin
          Next;
        end                                   
        else        
        begin
          if GetLSNo(ADOTmp, MaxNo, '', 'WFB_MJJY', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;                                                   
          with ADOQueryCmd do
          begin
            close;                                          
            sql.Clear;
            sql.add('select * from WFB_MJJY where mainid =''' + fmainid + ''' and subid =''' + fsubid + ''' and  DRcolor =''' + trim(Order_Main.fieldbyname('DRcolor').AsString) + ''' and  DRkuanno =''' + trim(Order_Main.fieldbyname('DRkuanno').AsString) + ''' and   MJStr4=''' + trim(Order_Main.fieldbyname('MJStr4').AsString) + ''' and   MJXH=''' + trim(Order_Main.fieldbyname('MJXH').AsString) + ''' ');
            open;
            if isempty then
            begin
              append;
              ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
              ADOQueryCmd.FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTmp);
            end
            else
              edit;
            SSetSaveDataCDSNew(ADOQueryCmd, Tv1, Order_Main, 'WFB_MJJY', 0);
            fieldbyname('mainid').Value := Trim(Fmainid);
            fieldbyname('subid').Value := Trim(Fsubid);
            FieldByName('MJStr2').Value := '未入库';
            FieldByName('MJType').Value := '正品';
            FieldByName('JTType').Value := '导入';
            fieldbyname('mjid').AsString := Trim(MaxNo);
            post;
          end;
//        XH := XH + 1;
          next;
        end;
      end;

    end;

    ADOQueryCmd.Connection.CommitTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJlen=round(mjlen,1) ');
      sql.Add('where mainid=''' + Trim(Fmainid) + '''');
      ExecSQL;
    end;
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure Tfrmmddrxx.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  Order_Main.delete;
end;

procedure Tfrmmddrxx.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '码单导入');
  Close;
end;

procedure Tfrmmddrxx.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure Tfrmmddrxx.FormDestroy(Sender: TObject);
begin
  frmmddrxx := nil;
end;

end.

