Exemplo n.º 1
0
        public static bool DeleteSalesInvoice(IOrderLabel order)
        {
            if (order == null)
            {
                return(false);
            }
            TStoreHouse sh = null;

            order.IsSHSent = false;
            try
            {
                logger.Debug($"DeleteSalesInvoice Oreder {order.Id}");
                sh = ConnectSH();
                if (sh == null)
                {
                    return(false);
                }



                var prefix    = Properties.Settings.Default.SHDocPrefix + DBProvider.TestStr + (order is OrderToGo ? "ToGo" : "");
                var prefixAnn = prefix + "_Annul";

                TExpenceDocument d = new TExpenceDocument()
                {
                    Prefix = prefixAnn,
                    DocNum = (int)order.Id,
                    Date   = order.DeliveryDate,
                    Type   = 1
                };


                logger.Debug($"sh.ExpenceDocumentDelete anul DocNum: {d.DocNum}; Prefix: {d.Prefix}; Date: {d.Date}");
                sh.ExpenceDocumentDelete(d, out int errCodet, out string errt);
                d.Prefix = prefix;


                logger.Debug($"sh.ExpenceDocumentDelete DocNum: {d.DocNum}; Prefix: {d.Prefix}; Date: {d.Date}");
                bool res = sh.ExpenceDocumentDelete(d, out int errCode, out string err);
                logger.Debug($"DeleteSalesInvoice {res}; err: {err}");
                if (errCode == -100)
                {
                    logger.Debug($"(errCode == -100) ok;");
                    res = true;
                }
                order.IsSHSent = res;
                return(res);
            }
            catch (Exception e)
            {
                logger.Error($"DeleteSalesInvoice error. Mess: {e.Message}");
                return(false);
            }
            finally
            {
                if (sh != null)
                {
                    try { sh.CloseConection(); } catch { }
                }
            }
        }
Exemplo n.º 2
0
        //public static bool CreateSalesInvoiceSync(OrderFlight order, out string ErrMesssage)
        //{

        //    ErrMesssage = "";
        //    if (order == null) return false;
        //    if (order.OrderStatus == OrderStatus.Cancelled || order.OrderStatus==OrderStatus.InWork)
        //    {
        //        return DeleteSalesInvoice(order);
        //    }



        //    TStoreHouse sh = null;
        //    order.IsSHSent = false;
        //    try
        //    {
        //        logger.Debug($"CreateSalesInvoice Order {order.Id}");
        //        sh = ConnectSH();
        //        if (sh == null) return false;

        //        if (order.AirCompany.SHId == 0)
        //        {
        //            var addAirRes = AddAirCompany(order.AirCompany, out ErrMesssage);
        //            if (!addAirRes)
        //            {
        //                return false;
        //            }
        //        }


        //        TExpenceDocument d = new TExpenceDocument()
        //        {
        //            Prefix = Properties.Settings.Default.SHDocPrefix,
        //            DocNum = (int)order.Id,
        //            RidPlace = Properties.Settings.Default.SHRidPlace,
        //            CatExpence = (int)order.AirCompany.SHId,
        //            Date = order.DeliveryDate,
        //            Coment = $"{order.DeliveryPlace?.Name} {order.FlightNumber} {order.DeliveryDate.ToString("HH:mm")} ",
        //            ListItemDocument = new List<TItemDocument>()
        //        };

        //        if (DBProvider.SharAirs.Contains(order.AirCompany.Id))
        //        {
        //            d.RidPlace = Properties.Settings.Default.SHRidPlaceSVO;
        //            d.Coment = "SVO" + d.Coment;
        //        }

        //        if (order.ContactPerson != null) { d.Coment += order.ContactPerson.FullSearchData; }
        //        logger.Debug($"TExpenceDocument create Prefix: {d.Prefix}; DocNum:{d.DocNum}; RidPlace:{d.RidPlace}; CatExpence:{d.CatExpence}; Date:{d.Date}; Coment:{d.Coment};");
        //        if (order.DishPackages != null)
        //        {
        //            decimal DPSumm = 0;
        //            foreach (var dp in order.DishPackages)
        //            {

        //                if (dp.Dish.SHIdNewBase == 0)
        //                {
        //                    logger.Debug($"dp.Dish.SHIdNewBase == 0");
        //                    int catId = 0;
        //                    if (!AddNewDishToFly(dp.Dish, out ErrMesssage, out catId))
        //                    {
        //                        logger.Debug($"TExpenceDocument create AddNewDishToFly error return false");
        //                        return false;
        //                    }
        //                }

        //                decimal discDownPecent = (order.OrderSumm == 0) ? 0 : order.OrderTotalSumm / order.OrderSumm;
        //                if (order.DiscountSumm == 0) { discDownPecent = 1; }
        //                decimal dSumm = dp.TotalPrice * (discDownPecent) * dp.Amount;
        //                if (dp == order.DishPackages.Last())
        //                {
        //                    dSumm = (order.OrderTotalSumm - DPSumm);
        //                }
        //                else
        //                {
        //                    DPSumm += dSumm;
        //                }
        //                TItemDocument itm = new TItemDocument()
        //                {
        //                    Quantity = (double)dp.Amount,
        //                    Rid = (int)dp.Dish.SHIdNewBase,
        //                    Price = (double)dSumm//* 1000
        //                };
        //                logger.Debug($"TExpenceDocument create itm {dp.DishName}; Price:  {itm.Price}; Quantity: {itm.Quantity}; Rid:{itm.Rid} ");
        //                d.ListItemDocument.Add(itm);
        //            }
        //        }
        //        d.Type = 1;
        //        string err = "";
        //        int errCode = 0;
        //        bool res = sh.ExpenceDocumentCreate(d, out errCode, out ErrMesssage);



        //        if (res)
        //        {
        //            logger.Debug($"CreateSalesToFlyInvoice Ok");

        //        }
        //        else
        //        {
        //            logger.Debug($"CreateSalesToFlyInvoice error Number {order.Id}  err: {ErrMesssage}");
        //        }

        //        order.IsSHSent = res;
        //        return res;

        //    }
        //    catch (Exception e)
        //    {
        //        ErrMesssage = $"Ошибка создания расходной накладной {Environment.NewLine + e.Message}";
        //        logger.Error($"CreateSalesInvoice Oreder {order.Id}. Mess: {e.Message}");
        //        return false;
        //    }
        //    finally
        //    {

        //        if (sh != null)
        //        {
        //            try
        //            {
        //                sh.CloseConection();

        //            }
        //            catch (Exception ee) { logger.Error($"CreateSalesInvoice error CloseConection Mess: {ee.Message}"); }

        //        }

        //    }
        //}

        /*
         *
         * public static bool ShSendCheck(IOrderLabel order, out string ErrMesssage)
         * {
         * ErrMesssage = "";
         * var prefix = Properties.Settings.Default.SHDocPrefix + DBProvider.TestStr + (order is OrderToGo ? "ToGo" : "");
         * var prefixAnn = prefix + "_Annul";
         * TExpenceDocument d = new TExpenceDocument()
         * {
         *  Prefix = prefix,
         *  DocNum = (int)order.Id,
         *  Date = order.DeliveryDate,
         *  Type = 1
         * };
         * TStoreHouse sh = null;
         * sh = ConnectSH();
         * if (sh == null) return false;
         * //sh.ExpenceDocumentCheck(d,)
         *  bool res = sh.ExpenceDocumentCheck(d, out int errCode, out string err);
         * logger.Debug($"DeleteSalesInvoice {res}; err: {err}");
         * if (errCode == -100)
         * {
         *  logger.Debug($"(errCode == -100) ok;");
         *  res = true;
         * }
         * }
         */

        public static bool CreateSalesInvoiceSync(IOrderLabel order, out string ErrMesssage)
        {
            ErrMesssage = "";

            if (order == null)
            {
                return(false);
            }

            try
            {
                DeleteSalesInvoice(order);
                if (order.OrderStatus == OrderStatus.Cancelled)
                {
                    order.IsSHSent = true;
                }
                if (order.OrderStatus == OrderStatus.Cancelled || order.OrderStatus == OrderStatus.InWork)
                {
                    return(true);
                }


                order.IsSHSent = false;

                TStoreHouse sh = null;
                sh = ConnectSH();
                if (sh == null)
                {
                    return(false);
                }



                foreach (var dp in order.DishPackagesForLab.Where(a => a.Dish.SHIdNewBase == 0))
                {
                    if (order is OrderFlight)
                    {
                        if (!AddNewDishToFly(dp.Dish, out ErrMesssage, out int catId))
                        {
                            logger.Debug($"TExpenceDocument create AddNewDishToFly error return false");
                            return(false);
                        }
                    }
                    else
                    {
                        if (!AddNewDishToGo(dp.Dish, out ErrMesssage, out int catId))
                        {
                            logger.Debug($"TExpenceDocument create AddNewDishToFly error return false");
                            return(false);
                        }
                    }
                }
                var prefix   = Properties.Settings.Default.SHDocPrefix + DBProvider.TestStr;
                var docNum   = (int)order.Id;
                var ridPlace = 0;
                var comment  = "";
                var catExp   = 0;
                if (order is OrderFlight orderFlight)
                {
                    //prefix += "ToFly";
                    ridPlace = Properties.Settings.Default.SHRidPlace;
                    comment  = $"{orderFlight.DeliveryPlace?.Name} {orderFlight.FlightNumber} {orderFlight.DeliveryDate.ToString("HH:mm")} ";
                    catExp   = (int)orderFlight.AirCompany.SHId;
                    if (DBProvider.SharAirs.Contains(orderFlight.AirCompany.Id))
                    {
                        ridPlace = Properties.Settings.Default.SHRidPlaceSVO;
                        comment  = "SVO " + comment;
                    }

                    if (orderFlight.AirCompany.SHId == 0)
                    {
                        var addAirRes = AddAirCompany(orderFlight.AirCompany, out ErrMesssage);
                        if (!addAirRes)
                        {
                            return(false);
                        }
                    }
                }
                else if (order is OrderToGo orderToGo)
                {
                    if (orderToGo.PaymentType != null && orderToGo.PaymentType.SHId == 0)
                    {
                        var addPRes = AddToGoPayment(orderToGo.PaymentType, out ErrMesssage);
                        if (!addPRes)
                        {
                            return(false);
                        }
                    }

                    prefix  += "ToGo";
                    ridPlace = Properties.Settings.Default.SHRidPlaceToGo;
                    catExp   = (int)(orderToGo.PaymentType?.SHId ?? Properties.Settings.Default.SHToGoNotPaymentCatId); //: (int)orderToGo.PaymentType.SHId
                    comment  = orderToGo.OrderComment == null ? "" : orderToGo.OrderComment + (orderToGo.CommentKitchen ?? "");
                }
                bool res = true;

                if (order.DishPackagesNoSpis.Any())
                {
                    res         &= CreateSalesInvoiceSyncSale(order, order.DishPackagesNoSpis.ToList(), docNum, catExp, ridPlace, prefix, comment, out string ErrMesssage2);
                    ErrMesssage += ErrMesssage2 + Environment.NewLine;
                }
                if (order.DishPackagesSpis.Where(a => a.DeletedStatus == 1).Any())
                {
                    foreach (var p in order.DishPackagesSpis.Where(a => a.DeletedStatus == 1).Select(x => x.SpisPaymentId).Distinct())
                    {
                        prefix += "_Annul";
                        catExp  = (int)DataExtension.DataCatalogsSingleton.Instance.GetPayment(p)?.SHId;

                        res         &= CreateSalesInvoiceSyncSale(order, order.DishPackagesSpis.Where(x => x.SpisPaymentId == p).ToList(), docNum, catExp, ridPlace, prefix, comment, out string ErrMesg);
                        ErrMesssage += ErrMesg + Environment.NewLine;
                    }
                }

                order.IsSHSent = res;
                return(res);
            }
            catch (Exception e)
            {
                ErrMesssage = $"Ошибка создания расходной накладной {Environment.NewLine + e.Message}";
                logger.Error($"CreateSalesInvoice Oreder {order.Id}. Mess: {e.Message}");
                return(false);
            }
            finally
            {
                if (sh != null)
                {
                    try
                    {
                        sh.CloseConection();
                    }
                    catch (Exception ee) { logger.Error($"CreateSalesInvoice error CloseConection Mess: {ee.Message}"); }
                }
            }
        }