protected void FillRecord(object rec, object[] fields)
        {
            ImportPOSData record = (ImportPOSData)rec;

            record.STKCODE   = (fields[0] is DBNull) ? string.Empty : fields[0].ToString();
            record.APPENDIX1 = (fields[1] is DBNull) ? string.Empty : fields[1].ToString();
            record.APPENDIX2 = (fields[2] is DBNull) ? string.Empty : fields[2].ToString();
            record.APPENDIX3 = (fields[3] is DBNull) ? string.Empty : fields[3].ToString();

            record.SeqNum     = (fields[4] is DBNull) ? 0 : Convert.ToInt32(fields[4].ToString());
            record.Shop       = (fields[5] is DBNull) ? string.Empty : fields[5].ToString();
            record.Terminal   = (fields[6] is DBNull) ? string.Empty : fields[6].ToString();
            record.Operator   = (fields[7] is DBNull) ? string.Empty : fields[7].ToString();
            record.Qty        = (fields[8] is DBNull) ? 0 : Convert.ToDecimal(fields[8].ToString());
            record.Shelf      = (fields[9] is DBNull) ? string.Empty : fields[9].ToString();
            record.Barcode    = (fields[10] is DBNull) ? string.Empty : fields[10].ToString();
            record.HHT        = (fields[11] is DBNull) ? string.Empty : fields[11].ToString();
            record.Keyboard   = (fields[12] is DBNull) ? string.Empty : fields[12].ToString();
            record.TxDate     = (fields[13] is DBNull) ? string.Empty : fields[13].ToString();
            record.Exported   = (fields[14] is DBNull) ? string.Empty : fields[14].ToString();
            record.ExportedOn = (fields[15] is DBNull) ? string.Empty : fields[15].ToString();
            record.ExportNum  = (fields[16] is DBNull) ? string.Empty : fields[16].ToString();
        }
        private void CreateStockTakeHHTDetails(Guid headerId, DateTime uploadOn, bool overwrite)
        {
            int     totalLine = PacketDataList.Length, missingLine = 0;
            decimal totalQty = 0, missingQty = 0;

            using (var ctx = new EF6.RT2020Entities())
            {
                using (var scope = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        for (int i = 0; i < PacketDataList.Length; i++)
                        {
                            EF6.StockTakeDetails_HHT hhtDetail;
                            ImportPOSData            posData = PacketDataList[i];
                            totalQty += posData.Qty;

                            // Calc missing line & qty
                            Guid productId = GetProductId(posData.STKCODE, posData.APPENDIX1, posData.APPENDIX2, posData.APPENDIX3);
                            if (productId == System.Guid.Empty)
                            {
                                missingLine++;
                                missingQty += posData.Qty;
                            }
                            else
                            {
                                var pb = ctx.ProductBarcode.Where(x => x.ProductId == productId).FirstOrDefault();
                                if (pb == null)
                                {
                                    missingLine++;
                                    missingQty += posData.Qty;
                                }

                                string hhtId = posData.HHT.Trim().Length == 0 ? "POS_ADV1" : posData.HHT.Trim();

                                if (overwrite)
                                {
                                    //string sql = "TxNumber = '" + posData.ExportNum + "' AND HHTId ='" + hhtId + "'";
                                    hhtDetail = ctx.StockTakeDetails_HHT
                                                .Where(x => x.TxNumber == posData.ExportNum && x.HHTId == hhtId)
                                                .FirstOrDefault();
                                }
                                else
                                {
                                    hhtDetail           = new EF6.StockTakeDetails_HHT();
                                    hhtDetail.DetailsId = Guid.NewGuid();

                                    ctx.StockTakeDetails_HHT.Add(hhtDetail);
                                }

                                hhtDetail.TxNumber   = posData.ExportNum;
                                hhtDetail.HHTId      = hhtId;
                                hhtDetail.UploadedOn = uploadOn;
                                hhtDetail.Barcode    = posData.Barcode;
                                hhtDetail.Qty        = posData.Qty;
                                hhtDetail.LineNumber = posData.SeqNum;
                                hhtDetail.ProductId  = productId;
                                hhtDetail.Remarks    = posData.Shelf;

                                ctx.SaveChanges();
                            }
                        }
                        scope.Commit();
                    }
                    catch (Exception ex)
                    {
                        scope.Rollback();
                    }
                }
            }

            UpdateHHtHeader(headerId, totalLine, missingLine, totalQty, missingQty);
        }