public ActionResult <Bill> Create([FromBody] BillCreatedModel billModel)
        {
            var creator = (HttpContext.Items["User"] as User).UserClaims;
            var res     = _billService.Create(creator, billModel);

            if (res.Item1 != 200)
            {
                return(StatusCode(res.Item1, res.Item2));
            }
            return(CreatedAtRoute("GetBill", new { id = res.Item3.Id.ToString() }, res.Item3));
        }
예제 #2
0
        public Tuple <int, string, Bill> Create(UserClaim creator, BillCreatedModel model)
        {
            if (isExisted(model.RepairedRequestId))
            {
                return(new Tuple <int, string, Bill>(400, "Bill is already existed", null));
            }

            double total = 0;

            var details = _rrService.GetQuotationDetails(model.RepairedRequestId);

            if (details != null)
            {
                foreach (var detail in details)
                {
                    total += detail.Quantity * detail.UnitPrice + detail.LaborCost;
                }
                if (model.Discount != 0)
                {
                    total = total * (1 - model.Discount);
                }
            }

            var customer = _customerService.Get(model.CustomerId);

            if (customer == null)
            {
                return(new Tuple <int, string, Bill>(400, "Customer Not Found", null));
            }

            Bill bill = new Bill()
            {
                TotalAmount = total,
                CreatedDate = System.DateTime.Now,
                Creator     = creator,
                Id          = model.RepairedRequestId,
                Details     = details,
                Customer    = customer,
                Discount    = model.Discount,
                VAT         = model.VAT
            };

            try
            {
                _bill.InsertOne(bill);
                return(new Tuple <int, string, Bill>(200, "", bill));
            }
            catch
            {
                return(new Tuple <int, string, Bill>(500, "", null));
            }
        }