/*
         * Developer: Sajid Khan
         * Date: 7-5-19
         * Action: Insert purchase order discount data to database
         * Input: new Purchase Orders discount data
         * output: string of PurchaseOrders discount id
         */
        public async Task <string> Create(PurchaseOrderDiscounts NewPurchaseOrder)
        {
            using (IDbConnection conn = Connection)
            {
                var result = await conn.InsertAsync <PurchaseOrderDiscounts>(NewPurchaseOrder);

                return(result.ToString());
            }
        }
        /*
         * Developer: Sajid Khan
         * Date: 7-5-19
         * Action: update purchase order discount data by id from database
         * Input: purchase order discount data
         * output: Boolean
         */
        public async Task <Boolean> Update(PurchaseOrderDiscounts PurchaseOrdersToUpdate)
        {
            using (IDbConnection conn = Connection)
            {
                var result = await conn.UpdateAsync <PurchaseOrderDiscounts>(PurchaseOrdersToUpdate);

                return(result);
            }
        }
        public async Task <List <PurchaseOrderDiscounts> > GetAsync(int id)
        {
            List <PurchaseOrderDiscounts> ToReturn = new List <PurchaseOrderDiscounts>();

            try
            {
                PurchaseOrderDiscounts Res = await _PurchaseOrdersDiscountsRepo.GetById(id);

                ToReturn.Add(Res);
            }
            catch (Exception ex)
            {
                var logger = _loggerFactory.CreateLogger("internal_error_log");
                logger.LogInformation("Problem happened in selecting the data for purchaseordersdiscounts list by id with message" + ex.Message);
            }
            return(ToReturn);
        }
        public async Task <string> Update(int id, [FromBody] string value)
        {
            string UpdateResult        = "Success";
            bool   UpdateProcessOutput = false;

            try
            {
                PurchaseOrderDiscounts PurchaseOrdersToUpdate = JsonConvert.DeserializeObject <PurchaseOrderDiscounts>(value);
                PurchaseOrdersToUpdate.po_discount_id = id;
                UpdateProcessOutput = await _PurchaseOrdersDiscountsRepo.Update(PurchaseOrdersToUpdate);

                var eventModel = new EventModel(tableName)
                {
                    EntityId    = PurchaseOrdersToUpdate.po_id,
                    EventName   = discount_update,
                    RefrenceId  = id,
                    UserId      = PurchaseOrdersToUpdate.updated_by,
                    EventNoteId = id
                };
                await _eventRepo.AddEventAsync(eventModel);

                var userEvent = new EventModel(userEventTableName)
                {
                    EntityId    = PurchaseOrdersToUpdate.updated_by,
                    EventName   = discount_update,
                    RefrenceId  = id,
                    UserId      = PurchaseOrdersToUpdate.updated_by,
                    EventNoteId = id
                };
                await _eventRepo.AddEventAsync(userEvent);
            }
            catch (Exception ex)
            {
                var logger = _loggerFactory.CreateLogger("internal_error_log");
                logger.LogInformation("Problem happened in updating  purchaseordersdiscounts with message" + ex.Message);
                UpdateResult = "Failed";
            }
            if (!UpdateProcessOutput)
            {
                UpdateResult = "Creation failed due to reason: No specific reson";
            }
            return(UpdateResult);
        }
        public async Task <string> PostAsync([FromBody]   string value)
        {
            string NewInsertionID = "0";

            try
            {
                PurchaseOrderDiscounts newPurchaseOrder = JsonConvert.DeserializeObject <PurchaseOrderDiscounts>(value);
                NewInsertionID = await _PurchaseOrdersDiscountsRepo.Create(newPurchaseOrder);

                var eventModel = new EventModel(tableName)
                {
                    EntityId    = newPurchaseOrder.po_id,
                    EventName   = discount_create,
                    RefrenceId  = Int32.Parse(NewInsertionID),
                    UserId      = newPurchaseOrder.created_by,
                    EventNoteId = Int32.Parse(NewInsertionID)
                };
                await _eventRepo.AddEventAsync(eventModel);

                var userEvent = new EventModel(userEventTableName)
                {
                    EntityId    = newPurchaseOrder.created_by,
                    EventName   = discount_create,
                    RefrenceId  = Convert.ToInt32(NewInsertionID),
                    UserId      = newPurchaseOrder.created_by,
                    EventNoteId = Int32.Parse(NewInsertionID)
                };
                await _eventRepo.AddEventAsync(userEvent);
            }
            catch (Exception ex)
            {
                var logger = _loggerFactory.CreateLogger("internal_error_log");
                logger.LogInformation("Problem happened in making new purchaseordersdiscounts create with message" + ex.Message);
            }
            return(NewInsertionID);
        }
        public async Task <string> UpdateSpecific(int id, [FromBody] string value)
        {
            string UpdateResult = "Success";

            try
            {
                PurchaseOrderDiscounts PurchaseOrdersToUpdate = JsonConvert.DeserializeObject <PurchaseOrderDiscounts>(value);
                PurchaseOrdersToUpdate.po_discount_id = id;
                Dictionary <String, String> ValuesToUpdate = new Dictionary <string, string>();

                if (PurchaseOrdersToUpdate.po_id != 0)
                {
                    ValuesToUpdate.Add("po_id", PurchaseOrdersToUpdate.po_id.ToString());
                }
                if (PurchaseOrdersToUpdate.discount_percentage != 0)
                {
                    ValuesToUpdate.Add("discount_percentage", PurchaseOrdersToUpdate.discount_percentage.ToString());
                }
                if (PurchaseOrdersToUpdate.discount_note != null)
                {
                    ValuesToUpdate.Add("discount_note", PurchaseOrdersToUpdate.discount_note.ToString());
                }
                if (PurchaseOrdersToUpdate.completed_status != 0)
                {
                    ValuesToUpdate.Add("completed_status", PurchaseOrdersToUpdate.completed_status.ToString());
                }
                if (PurchaseOrdersToUpdate.created_by != 0)
                {
                    ValuesToUpdate.Add("created_by", PurchaseOrdersToUpdate.created_by.ToString());
                }
                if (PurchaseOrdersToUpdate.updated_by != 0)
                {
                    ValuesToUpdate.Add("updated_by", PurchaseOrdersToUpdate.updated_by.ToString());
                }
                if (PurchaseOrdersToUpdate.created_at != 0)
                {
                    ValuesToUpdate.Add("created_at", PurchaseOrdersToUpdate.created_at.ToString());
                }
                if (PurchaseOrdersToUpdate.updated_at != 0)
                {
                    ValuesToUpdate.Add("updated_at", PurchaseOrdersToUpdate.updated_at.ToString());
                }

                await _PurchaseOrdersDiscountsRepo.UpdateSpecific(ValuesToUpdate, "po_discount_id=" + id);

                var eventModel = new EventModel(tableName)
                {
                    EntityId    = PurchaseOrdersToUpdate.po_id,
                    EventName   = discount_specificupdate,
                    RefrenceId  = id,
                    UserId      = PurchaseOrdersToUpdate.updated_by,
                    EventNoteId = id
                };
                await _eventRepo.AddEventAsync(eventModel);

                var userEvent = new EventModel(userEventTableName)
                {
                    EntityId    = PurchaseOrdersToUpdate.updated_by,
                    EventName   = discount_specificupdate,
                    RefrenceId  = id,
                    UserId      = PurchaseOrdersToUpdate.updated_by,
                    EventNoteId = id
                };
                await _eventRepo.AddEventAsync(userEvent);
            }
            catch (Exception ex)
            {
                var logger = _loggerFactory.CreateLogger("internal_error_log");
                logger.LogInformation("Problem happened in updating updatespecific purchaseordersdiscounts with message" + ex.Message);
                UpdateResult = "Failed";
            }

            return(UpdateResult);
        }