Exemplo n.º 1
0
 public PosSaleReportReturnModels()
 {
     PosSaleReportDTOs       = new List <PosSaleModels>();
     PosSaleDetailReportDTOs = new List <PosSaleDetailModels>();
     PaymentReportXeroDTOs   = new List <PaymentReportXeroDTO>();
     BusinessInfo            = new BusinessInfoXeroDTO();
 }
Exemplo n.º 2
0
        public void PushInvoiceToXexo(string storeId, List <PosSaleDetailModels> lstPosSaleDetail, List <PaymentReportXeroDTO> paids, BusinessInfoXeroDTO businessInfo)
        {
            if (businessInfo == null || string.IsNullOrEmpty(businessInfo.Url))
            {
                return;
            }
            NSLog.Logger.Info("PushInvoiceToXexo Start");
            var listItemIds = lstPosSaleDetail.Where(ww => ww.ItemTypeId != (int)Commons.EProductType.Misc &&
                                                     !string.IsNullOrEmpty(ww.GLAccountCode)).Select(ss => ss.ItemId).Distinct().ToList();

            if (listItemIds == null || listItemIds.Count == 0)
            {
                return;
            }
            //get tax rate
            string taxRate = businessInfo.TaxRate;//"TAX002";
            //string accountCode = string.Empty;
            string accountCodeForMisc = string.Empty;
            //check recipe
            var listItemInRecipe = new List <string>();

            //get tax
            //NuWebNCloud.Shared.Factory.Settings.TaxFactory _taxFactory = new NuWebNCloud.Shared.Factory.Settings.TaxFactory();
            //var lstTaxes = _taxFactory.GetListTaxV2(storeId);
            //var tax = lstTaxes.Where(w => w.IsActive && !string.IsNullOrEmpty(w.Rate)).FirstOrDefault();
            //if (tax != null)
            //    taxRate = tax.Rate;
            ////get currency

            using (var db = new NuWebContext())
            {
                listItemInRecipe = (from ri in db.I_Recipe_Item
                                    join i in db.I_Ingredient on ri.IngredientId equals i.Id
                                    where ri.StoreId == storeId && ri.Status != (int)Commons.EStatus.Deleted &&
                                    i.IsCheckStock
                                    group ri by ri.ItemId into g
                                    where g.Count() == 1
                                    select g.Key
                                    ).ToList();
                accountCodeForMisc = (from s in db.G_GeneralSetting
                                      join ss in db.G_SettingOnStore on s.Id equals ss.SettingId
                                      where s.Code == (int)Commons.EGeneralSetting.Miscellaneous
                                      select ss.Value).FirstOrDefault();
            }
            //list item dont recipe
            var listsaleFromPOSId = listItemIds.Except(listItemInRecipe);
            //list have recipe
            var lstItemHaveRecipeId = listItemIds.Intersect(listItemInRecipe).ToList();
            //misc
            var listItemMiscs = lstPosSaleDetail.Where(ww => ww.ItemTypeId == (int)Commons.EProductType.Misc
                                                       ).ToList();
            //list seriviceCharge
            var listSC = lstPosSaleDetail.Where(ww => ww.ServiceCharge > 0).ToList();
            //payment
            //todo
            //---------------------------------------------------------------------
            XeroInvoiceReportDTO xeroInvoice = new XeroInvoiceReportDTO();

            xeroInvoice.CurrencyCode      = businessInfo.Currency;
            xeroInvoice.Reference         = "NUPOS_XERO_DEMO";
            xeroInvoice.AppRegistrationId = businessInfo.AppRegistrationId;
            xeroInvoice.StoreId           = businessInfo.StoreId;
            xeroInvoice.BusinessDayId     = businessInfo.BusinessDayId;
            xeroInvoice.ClosingDatetime   = businessInfo.ClosingDate;

            List <XeroInvoiceItemReportDTO>    lstLineItems = new List <XeroInvoiceItemReportDTO>();
            List <XeroInvoicePaymentReportDTO> payments     = new List <XeroInvoicePaymentReportDTO>();

            var lstSaleFromPOS = lstPosSaleDetail.Where(ww => listsaleFromPOSId.Contains(ww.ItemId)).GroupBy(gg => gg.GLAccountCode)
                                 .ToList();

            var lineItemsForSaleFromPOS = new List <XeroInvoiceItemReportDTO>();
            var lineHaveRecipeFromPOS   = new List <XeroInvoiceItemReportDTO>();

            XeroInvoiceItemReportDTO    obj  = null;
            XeroInvoicePaymentReportDTO paid = null;

            foreach (var item in lstSaleFromPOS)
            {
                obj             = new XeroInvoiceItemReportDTO();
                obj.Description = "Sales from NuPOS";
                obj.Quantity    = 1;
                obj.LineAmount  = Math.Round(item.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                obj.UnitAmount  = Math.Round(item.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                obj.AccountCode = item.Key;
                obj.TaxType     = taxRate;

                lineItemsForSaleFromPOS.Add(obj);
            }
            lstLineItems.AddRange(lineItemsForSaleFromPOS);
            //have recipe
            if (lstItemHaveRecipeId != null && lstItemHaveRecipeId.Any())
            {
                using (var db = new NuWebContext())
                {
                    //list recipe
                    var ingredients = (from ri in db.I_Recipe_Item
                                       join i in db.I_Ingredient on ri.IngredientId equals i.Id
                                       where ri.StoreId == storeId && ri.Status != (int)Commons.EStatus.Deleted &&
                                       i.IsCheckStock &&
                                       lstItemHaveRecipeId.Contains(ri.ItemId)
                                       select new { Description = i.Name, Code = i.Code, ItemId = ri.ItemId }
                                       ).ToList();
                    for (int i = 0; i < lstItemHaveRecipeId.Count(); i++)
                    {
                        obj             = new XeroInvoiceItemReportDTO();
                        obj.Description = ingredients.Where(ww => ww.ItemId == lstItemHaveRecipeId[i]).Select(ss => ss.Description).FirstOrDefault();
                        obj.ItemCode    = ingredients.Where(ww => ww.ItemId == lstItemHaveRecipeId[i]).Select(ss => ss.Code).FirstOrDefault();
                        obj.Quantity    = 1;
                        obj.LineAmount  = Math.Round(lstPosSaleDetail.Where(ww => ww.ItemId == lstItemHaveRecipeId[i]).Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                        obj.UnitAmount  = Math.Round(lstPosSaleDetail.Where(ww => ww.ItemId == lstItemHaveRecipeId[i]).Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                        obj.AccountCode = lstPosSaleDetail.Where(ww => ww.ItemId == lstItemHaveRecipeId[i]).Select(ss => ss.GLAccountCode).FirstOrDefault();
                        obj.TaxType     = taxRate;
                        lineHaveRecipeFromPOS.Add(obj);
                    }
                }
                lstLineItems.AddRange(lineHaveRecipeFromPOS);
            }
            //service charge
            if (listSC != null && listSC.Any())
            {
                var listSCGroupGLCode = listSC.GroupBy(g => g.GLAccountCode).ToList();
                foreach (var item in listSCGroupGLCode)
                {
                    obj             = new XeroInvoiceItemReportDTO();
                    obj.Description = "Svc charge from NuPOS";
                    obj.Quantity    = 1;
                    obj.LineAmount  = Math.Round(item.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                    obj.UnitAmount  = Math.Round(item.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                    obj.AccountCode = item.Key;
                    obj.TaxType     = taxRate;

                    lstLineItems.Add(obj);
                }
            }

            //check misc
            if (listItemMiscs != null && listItemMiscs.Any())
            {
                obj             = new XeroInvoiceItemReportDTO();
                obj.Description = "Misc from NuPOS";
                obj.Quantity    = 1;
                obj.LineAmount  = Math.Round(listSC.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                obj.UnitAmount  = Math.Round(listSC.Sum(a => a.TotalAmount - a.PromotionAmount), 2);
                obj.AccountCode = accountCodeForMisc;
                obj.TaxType     = "NONE";

                lstLineItems.Add(obj);
            }

            NSLog.Logger.Info("lstLineItems: ", lstLineItems);
            //list payments
            #region payments
            foreach (var item in paids)
            {
                paid = new XeroInvoicePaymentReportDTO();
                paid.Account.Add(new XeroInvoiceAccountReportDTO()
                {
                    Code = item.AccountCode
                });
                paid.Date      = item.Date;
                paid.Amount    = item.Amount;
                paid.Reference = item.Reference;

                payments.Add(paid);
            }
            #endregion end payment

            //call api
            xeroInvoice.LineItems = lstLineItems;
            xeroInvoice.Payments  = payments;
            string msg    = string.Empty;
            var    result = _xeroFactory.SaleItems(businessInfo.Url, xeroInvoice, ref msg);
            NSLog.Logger.Info("PushInvoiceToXexo end", result);
        }