Exemplo n.º 1
0
        public ActionResult CreateInvoice(InvoiceCreateDTO req)
        {
            Response res = new Response();

            if (ModelState.IsValid)
            {
                try
                {
                    var currentUser = OperatorProvider.Provider.GetCurrent();
                    req.CreateUser = currentUser.UserId;
                    req.CreateDate = DateTime.Now;
                    res.Data       = _orderService.CreateOrderInvoice(req);
                }
                catch (Exception ex)
                {
                    res.Data    = false;
                    res.Message = ex.Message;
                }
            }
            else
            {
                res.Message = string.Join(",", ModelState
                                          .SelectMany(ms => ms.Value.Errors)
                                          .Select(e => e.ErrorMessage));
                res.Data = false;
            }
            return(Json(res));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建订单发票
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool CreateOrderInvoice(InvoiceCreateDTO req)
        {
            bool res = false;

            try
            {
                var restaurant = _resRep.GetModel(req.RestaurantId);
                var dateItem   = _extendItemRepository.GetModelList(restaurant.R_Company_Id, 10003).FirstOrDefault();
                req.BillDate = Convert.ToDateTime(dateItem.ItemValue);
                res          = _orderRep.CreateOrderInvoice(req);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return(res);
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> PostInvoice(InvoiceCreateDTO invoiceData)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string reg   = User.Identity.GetUserId();
            Order  order = await db.Orders.FindAsync(invoiceData.orderid);

            Accountant accountant = db.Accountants.Where(b => b.user_id == reg).SingleOrDefault();

            /*int inv = await db.Invoices.CountAsync(b => b.invoiceNumber== invoiceData.invoiceNumber || b.orderid==order.id);
             * if (inv != 0)
             * {
             *  return BadRequest("Invoice number exists or invoice for this order was already made");
             * }**/
            if (accountant == null)
            {
                return(BadRequest("Accountant does not exist"));
            }

            TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
            DateTime     userTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeInfo);

            Invoice invoice = new Invoice
            {
                accountant_id = accountant.id,
                date          = userTime,
                // invoiceNumber= invoiceData.invoiceNumber,
                orderid = invoiceData.orderid,
                status  = "not_paid",
            };

            // critical section generate invoice number
            lock (Lock)
            {
                string year      = DateTime.Now.Year.ToString();
                string reference = "INV-" + year + "-";
                int    invo      = db.Invoices.Count();
                if (invo == 0)
                {
                    invo      += 1;
                    reference += String.Format("{0:00000}", invo);
                }
                else
                {
                    invo       = db.Invoices.Count(b => b.invoiceNumber.Substring(4, 4) == year);
                    invo      += 1;
                    reference += String.Format("{0:00000}", invo);
                }
                while (db.Invoices.Count(d => d.invoiceNumber == reference) != 0)
                {
                    reference = "INV-" + year + "-" + String.Format("{0:00000}", ++invo);
                }
                order.status            = "processing";
                order.warehouseLocation = invoiceData.warehouseLocation;
                order.invoiceNumber     = reference;
                db.Entry(order).State   = EntityState.Modified;
                invoice.invoiceNumber   = reference;
                db.Invoices.Add(invoice);
                db.SaveChanges();
            }

            InvoiceGenerator inv    = new InvoiceGenerator();
            Stream           invPdf = inv.CreateInvoicePDF(order);
            FileDTO          file;
            string           filename = Guid.NewGuid().ToString() + "invoice.pdf";

            string bucket = "supremebrands";

            StringWriter s = new StringWriter();

            if (S3Bucket.UploadFileToS3(filename, invPdf, bucket))
            {
                file = new FileDTO()
                {
                    url = "https://s3-us-west-2.amazonaws.com/supremebrands/" + filename
                };

                string body = string.Format(@"
                <!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01//EN""      ""http://www.w3.org/TR/html4/strict.dtd"">
                <html>
                <body>
                    <h2> Dear sir/madam</h2>
                    <br>
                    <p>This email contains the link of the Invoice pdf created by the system</p>
                    <p><a href='{0}'>Click here</a> to open the pdf </p>
                    <p>Or copy and paste this link in your browser </p>
                    <p>{1}</p>
                    <br>
                    <p>Kind Regards</p>
                    
                     <p>Supreme Brands Team<p>
    
                    
                </body>
                </html>
                ", file.url, file.url);

                Email.sendEmail("*****@*****.**", "invoice " + order.invoiceNumber, body);
                invoice.invoiceUrl      = file.url;
                db.Entry(invoice).State = EntityState.Modified;
                db.SaveChanges();
            }


            return(Ok(invoice.invoiceNumber));
        }