public async Task <Tbl_Customer> AddCustomerAsync(Tbl_Customer tbl_Customer)
        {
            await _context.Tbl_Customer.AddAsync(tbl_Customer);

            await _context.SaveChangesAsync();

            return(tbl_Customer);
        }
        public async Task <Tbl_Products> AddProductAsync(Tbl_Products tbl_Products)
        {
            await _context.Tbl_Products.AddAsync(tbl_Products);

            await _context.SaveChangesAsync();

            return(tbl_Products);
        }
Exemple #3
0
        public async Task <Tbl_Discounts> AddDiscountAsync(Tbl_Discounts tbl_Discounts)
        {
            await _context.AddAsync(tbl_Discounts);

            await _context.SaveChangesAsync();

            return(tbl_Discounts);
        }
        public async Task <Tbl_Invoices> CreateInvoiceAsync(CreateInvoiceDto requests)
        {
            var user = await _customerService.GetCustomerByIdAsync(requests.customerId);

            //Add products
            var invoice = await AddProductsToInvoice(requests.products);

            invoice.customer   = user;
            invoice.customerId = user.id;

            //Calculate Affiliate Discount 10 percent
            if (user.isAffiliate)
            {
                var percentage = Convert.ToDecimal(10F / 100);
                var total      = percentage * invoice.totalAmount;
                invoice.totalDiscountAmount = total;
            }

            //Calculate Employee Discount 5 percent
            if (user.isCustomerLoyal())
            {
                var percentage = Convert.ToDecimal(5F / 100);
                var total      = percentage * invoice.totalAmount;
                invoice.totalDiscountAmount = total;
            }

            //Customer can only get one percentage based discount
            //Calculate Employee Discount 30 percent
            if (user.isEmployee)
            {
                var percentage = Convert.ToDecimal(30F / 100);
                var total      = percentage * invoice.totalAmount;
                invoice.totalDiscountAmount = total;
            }

            //Calculate 5% discount on every $100
            var discountedAmount = 100;
            var x             = invoice.totalAmount % discountedAmount;
            var y             = invoice.totalAmount - x;
            var z             = y / discountedAmount;
            var totalDiscount = z * 5;

            invoice.billDiscountAmount     = totalDiscount;
            invoice.billDiscountPercentage = 5;

            invoice.totalDiscountAmount = invoice.totalDiscountAmount + totalDiscount;
            invoice.updateTotalWithDiscount();
            invoice.issueDate = DateTime.Now;

            //Save to Db
            try{
                await _context.Tbl_Invoices.AddAsync(invoice);

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                throw;
            }
            return(invoice);
        }