Example #1
0
        /// <summary>
        /// Save Bottle return
        /// </summary>
        /// <param name="brPayment">Br payment</param>
        /// <param name="userCode">User code</param>
        /// <param name="error">Error message</param>
        /// <param name="bottleReport">Report</param>
        /// <param name="openDrawer">Open drawer or not</param>
        public Sale SaveBottleReturn(BR_Payment brPayment,
                                     out ErrorMessage error, out Report bottleReport, out bool openDrawer)
        {
            bottleReport = null;
            openDrawer   = false;
            var dateStart = DateTime.Now;

            Performancelog.Debug($"Start,BottleManager,SaveBottleReturn,{string.Empty},{DateTime.Now:hh.mm.ss.ffffff}");
            error = new ErrorMessage();
            var offSet       = _policyManager.LoadStoreInfo().OffSet;
            var userCode     = UserCode;
            var user         = _loginManager.GetExistingUser(userCode);
            var existingSale = _saleService.GetSaleByTillNumber(brPayment.TillNumber);

            if (existingSale != null && existingSale.Sale_Lines.Count > 0)
            {
                error.MessageStyle = new MessageStyle
                {
                    Message     = "Please finish current sale before bottle return.~Bottle Return",
                    MessageType = ExclamationOkMessageType
                };
                error.StatusCode = HttpStatusCode.Conflict;
                return(null);
            }
            if (!_policyManager.GetPol("U_BOTTLERTN", user))
            {
                error.MessageStyle = _resourceManager.CreateMessage(offSet, 38, 57, null, ExclamationOkMessageType);
                error.StatusCode   = HttpStatusCode.Forbidden;
                return(null);
            }

            if (brPayment.Amount != 0)
            {
                if (string.IsNullOrEmpty(Convert.ToString(_policyManager.GetPol("BASECURR", null))))
                {
                    error.MessageStyle = _resourceManager.CreateMessage(offSet, 18, 61, null, CriticalOkMessageType);
                    error.StatusCode   = HttpStatusCode.NotFound;
                    return(null);
                }

                if (brPayment.Amount > (decimal)Math.Abs(_policyManager.GetPol("U_BR_LIMIT", user)))
                {
                    // Exceed the bottle return limit, Please get an authorized user!
                    error.MessageStyle = _resourceManager.CreateMessage(offSet, 18, 62, null, CriticalOkMessageType);
                    error.StatusCode   = HttpStatusCode.Forbidden;
                    return(null);
                }

                if (_policyManager.OPEN_DRAWER == "Every Sale")
                {
                    openDrawer = true;
                }
                var sale = new Sale
                {
                    Sale_Totals = { Net = Convert.ToDecimal(brPayment.Amount) }
                };
                var saleTotals = sale.Sale_Totals;
                _saleManager.SetGross(ref saleTotals, sale.Sale_Totals.Net);
                sale.Sale_Totals.Gross        = saleTotals.Gross;
                sale.Sale_Totals.TotalLabel   = saleTotals.TotalLabel;
                sale.Sale_Totals.SummaryLabel = saleTotals.SummaryLabel;
                sale.Register    = brPayment.RegisterNumber;
                sale.Sale_Change = 0;

                sale.TillNumber  = Convert.ToByte(brPayment.TillNumber);
                sale.Sale_Date   = DateTime.Now;
                sale.Sale_Tender = 0;
                sale.Sale_Change = 0;
                sale.Sale_Amount = 0;
                if (_policyManager.GetPol("Penny_Adj", null) && brPayment.Amount != 0)
                {
                    sale.Sale_Totals.Penny_Adj = Helper.Calculate_Penny_Adj(Convert.ToDecimal(brPayment.Amount));
                }
                else
                {
                    sale.Sale_Totals.Penny_Adj = 0;
                }

                brPayment.Penny_Adj = sale.Sale_Totals.Penny_Adj;
                if (existingSale == null)
                {
                    brPayment.Sale_Num = _saleManager.GetCurrentSaleNo(brPayment.TillNumber, userCode, out error);
                }
                var tendBr = new Tenders();
                var tender = tendBr.Add(Convert.ToString(_policyManager.GetPol("BASECURR", null)),
                                        "Cash", 1, true, true, false, (short)1,
                                        Convert.ToString(_policyManager.GetPol("BASECURR", null)),
                                        false, 0, 0, 0.01, true, Convert.ToDouble(-brPayment.Amount),
                                        (short)1, true, false, "", "");
                _tenderManager.Set_Amount_Entered(ref tendBr, ref sale, tender, -brPayment.Amount);

                sale.Sale_Num  = Convert.ToInt32(brPayment.Sale_Num);
                sale.Sale_Type = "BTL RTN";
                var shiftNumber = _tillService.GetTill(brPayment.TillNumber).Shift;
                bottleReport = _receiptManager.Print_BottleReturn(brPayment, user.Name, DateTime.Today,
                                                                  DateTime.Now, sale.Register, (short)brPayment.TillNumber, shiftNumber);
                bottleReport.Copies = _policyManager.BottleReturnReceiptCopies;
                _bottleReturnService.SaveBottleReturnsToDbTrans(brPayment);
                _saleManager.SaveSale(sale, userCode, ref tendBr, null);
                CacheManager.DeleteCurrentSaleForTill(sale.TillNumber, sale.Sale_Num);
                //_saleManager.Clear_Sale(sale.Sale_Num,sale.TillNumber, userCode, "", null, true, false, false, out msg);
                sale = _saleManager.InitializeSale(brPayment.TillNumber, brPayment.RegisterNumber, userCode, out error);
                //Update Sale object in Cache
                CacheManager.AddCurrentSaleForTill(sale.TillNumber, sale.Sale_Num, sale);
                Performancelog.Debug($"End,BottleManager,SaveBottleReturn,{DateTime.Now.Subtract(dateStart).TotalMilliseconds},{DateTime.Now:hh.mm.ss.ffffff}");
                return(sale);
            }
            return(null);
        }
        /// <summary>
        /// Method to save vendor payout
        /// </summary>
        /// <param name="po">Payout</param>
        /// <param name="tillNumber">Till number</param>
        /// <param name="userCode">User code</param>
        /// <param name="registerNumber">Register number</param>
        /// <param name="taxes">Taxes</param>
        /// <param name="openDrawer">Open cash drawer</param>
        /// <param name="error">Error message</param>
        /// <returns>Report</returns>
        public Report SaveVendorPayout(Payout po, int tillNumber, string userCode, byte registerNumber,
                                       List <Tax> taxes, out bool openDrawer, out ErrorMessage error)
        {
            openDrawer = false;
            var sale   = _saleManager.GetCurrentSale(po.Sale_Num, tillNumber, 0, userCode, out error);
            var offSet = _policyManager.LoadStoreInfo().OffSet;

            if (!string.IsNullOrEmpty(error.MessageStyle.Message))
            {
                return(null);
            }
            if (!_policyManager.DO_PAYOUTS || (sale != null && sale.Sale_Lines.Count > 0))
            {
                MessageType temp_VbStyle8 = (int)MessageType.Exclamation + MessageType.OkOnly;
                error.MessageStyle = _resourceManager.CreateMessage(offSet, 38, 54, null, temp_VbStyle8);
                return(null);
            }
            if (po.Gross > (decimal)214748.3647)
            {
                error = new ErrorMessage
                {
                    MessageStyle = new MessageStyle
                    {
                        Message = "Maximum payout amount is 214748.3647"
                    }
                };
                return(null);
            }
            var salePo = new Sale();
            var tendPo = new Tenders();



            var curr = Convert.ToString(_policyManager.BASECURR);

            if (string.IsNullOrEmpty(curr))
            {
                MessageType temp_VbStyle = (int)MessageType.OkOnly + MessageType.Critical;
                error.MessageStyle = _resourceManager.CreateMessage(offSet, 23, 94, null, temp_VbStyle);
                return(null);
            }
            var   poTaxes  = _taxService.GetAllTaxes();
            float sumTaxes = 0;

            foreach (var tax in taxes)
            {
                if (poTaxes.Any(p => p.TaxName == tax.Code))
                {
                    po.Payout_Taxes.Add(tax.Code, "", tax.Amount, true, tax.Code);
                    salePo.Sale_Totals.Sale_Taxes.Add(tax.Code, "I", 0, 0, 0, tax.Amount, po.Gross, 0, 0, "");
                    sumTaxes = (float)(sumTaxes + Conversion.Val(tax.Amount));
                }
                else
                {
                    MessageType temp_VbStyle8 = (int)MessageType.Exclamation + MessageType.OkOnly;
                    error.MessageStyle = new MessageStyle
                    {
                        Message     = "Invalid tax entered",
                        MessageType = temp_VbStyle8
                    };
                    return(null);
                }
            }
            if (!string.IsNullOrEmpty(po.Vendor.Code))
            {
                po.Vendor = _stockService.GetVendorByCode(po.Vendor.Code);
                if (po.Vendor.Code == null)
                {
                    MessageType temp_VbStyle2 = (int)MessageType.Critical + MessageType.OkOnly;
                    error.MessageStyle = new MessageStyle
                    {
                        Message     = "Invalid vendor code",
                        MessageType = temp_VbStyle2
                    };
                    return(null);
                }
            }
            // Nicoolette changed next line on Nov 13,2007 to allow negative payouts, based on Mr. Gas requirement
            if (po.Gross == 0)
            {
                //    If PO.Gross <= 0 Then
                //You must specify the Payout Amount, vbCritical + vbOKOnly
                MessageType temp_VbStyle2 = (int)MessageType.Critical + MessageType.OkOnly;
                error.MessageStyle = _resourceManager.CreateMessage(offSet, 23, 91, null, temp_VbStyle2);
                return(null);
            }

            if (_policyManager.PO_REASON)
            {
                po.Return_Reason = _reasonService.GetReturnReason(po.Return_Reason.Reason, (char)ReasonType.Payouts);
                if (po.Return_Reason == null)
                {
                    MessageType temp_VbStyle2 = (int)MessageType.Critical + MessageType.OkOnly;
                    error.MessageStyle = _resourceManager.CreateMessage(offSet, 23, 92, null, temp_VbStyle2);
                    return(null);
                }
                salePo.Return_Reason = po.Return_Reason;
            }


            var till = _tillService.GetTill(tillNumber);

            till.Cash = till.Cash - po.Gross;
            _tillService.UpdateTill(till);
            if (_policyManager.OPEN_DRAWER == "Every Sale")
            {
                openDrawer = true;
            }



            tendPo.Add(curr, "Cash", 1, true, true, false, 1, curr, false, 0, 0, 0.01, true, Convert.ToDouble((object)-po.Gross), 1, true, false, "", "");
            _tenderManager.Set_Amount_Entered(ref tendPo, ref salePo, tendPo[1], -po.Gross, -1);
            // payout

            if (_policyManager.PENNY_ADJ && po.Gross != 0)
            {
                salePo.Sale_Totals.Penny_Adj = modGlobalFunctions.Calculate_Penny_Adj(po.Gross);
            }
            else
            {
                salePo.Sale_Totals.Penny_Adj = 0;
            }
            po.Penny_Adj = salePo.Sale_Totals.Penny_Adj;
            //   end

            salePo.Sale_Totals.Net = po.Gross; // - Sum_Taxes   Nicolette commented out,
                                               // once for payout all taxes are included, don't subtract the taxes,
                                               // this will afect Sale_Amt in SaleHead, March 07, 2003

            var saleTotals = salePo.Sale_Totals;

            _saleManager.SetGross(ref saleTotals, salePo.Sale_Totals.Net);
            salePo.Sale_Totals.Gross        = saleTotals.Gross;
            salePo.Sale_Totals.TotalLabel   = saleTotals.TotalLabel;
            salePo.Sale_Totals.SummaryLabel = saleTotals.SummaryLabel;

            salePo.Register    = Convert.ToByte(registerNumber);
            salePo.Sale_Change = 0;

            salePo.TillNumber         = Convert.ToByte(tillNumber);
            salePo.Sale_Date          = DateTime.Now;
            salePo.Sale_Tender        = 0;
            salePo.Sale_Totals.PayOut = po.Gross;
            salePo.Sale_Change        = 0;
            salePo.Sale_Amount        = 0;
            if (po.Sale_Num == 0)
            {
                salePo.Sale_Num = _saleManager.GetSaleNo(tillNumber, userCode, out error);
            }
            else
            {
                salePo.Sale_Num = po.Sale_Num;
            }

            po.Penny_Adj = salePo.Sale_Totals.Penny_Adj; //

            salePo.Sale_Type = "PAYOUT";
            salePo.Vendor    = po.Vendor.Code;
            var user   = _userService.GetUser(userCode);
            var stream = _receiptManager.Print_Payout(po, userCode, user.Name, DateTime.Today,
                                                      salePo.Sale_Date, registerNumber,
                                                      till);

            stream.Copies = _policyManager.PayoutReceiptCopies;
            _saleManager.SaveSale(salePo, userCode, ref tendPo, null);


            _saleManager.Clear_Sale(salePo, po.Sale_Num, salePo.TillNumber, userCode, "", null,
                                    false, false, false, out error);


            return(stream);
        }