public void DeletePo(int id)
        {
            // Set status to remove Txn_PoDetail
            IRepository <Txn_PODetail> repoPoDetail = _unitOfWork.GetRepository <Txn_PODetail>();
            Txn_PODetail poDetail = repoPoDetail.GetQueryable(o => o.PoDetailId == id).FirstOrDefault();

            if (poDetail != null)
            {
                poDetail.Status = 0;
                repoPoDetail.Update(poDetail);
                _unitOfWork.Execute();
            }

            // Check If all detail that belong to PoDetail is status remove then remove Po
            int countStatus = repoPoDetail.GetQueryable(o => o.PoId == poDetail.PoId && o.Status == 1).Count();

            if (countStatus == 0)
            {
                IRepository <Txn_PO> repoPo = _unitOfWork.GetRepository <Txn_PO>();
                Txn_PO po = repoPo.GetQueryable(o => o.PoId == poDetail.PoId).FirstOrDefault();
                if (poDetail != null)
                {
                    po.Status = 0;
                    repoPo.Update(po);
                    _unitOfWork.Execute();
                }
            }
        }
        public void CreatePo(PoDetailViewModel model)
        {
            // If Po number already exists then do not insert
            Txn_PO existPo = _unitOfWork.GetRepository <Txn_PO>().GetQueryable(o => o.PoNumber == model.PoNumber).FirstOrDefault();

            if (existPo != null)
            {
                existPo.CustomerName = model.CustomerName;
                existPo.PoDate       = model.PoDate;
                existPo.Status       = 1;
                _unitOfWork.GetRepository <Txn_PO>().Update(existPo);

                model.PoId = existPo.PoId;
            }
            else
            {
                // Insert Po
                Txn_PO po = MappingPoDetailViewModelToPo(model);
                po.Status = 1;
                _unitOfWork.GetRepository <Txn_PO>().Insert(po);
                _unitOfWork.Execute();

                model.PoId = po.PoId;
            }

            // Insert PoDetail
            Txn_PODetail poDetail = MappingPoDetailViewModelToPoDetail(model);

            poDetail.Status = 1;
            _unitOfWork.GetRepository <Txn_PODetail>().Insert(poDetail);
            _unitOfWork.Execute();

            model.PoDetailId = poDetail.PoDetailId;

            // Insert Commission
            Txn_Commission commission = MappingPoDetailViewModelToCommission(model);

            _unitOfWork.GetRepository <Txn_Commission>().Insert(commission);

            // Insert CommissionDetail
            List <Txn_CommissionDetail> commissionDetail = MappingPoDetailViewModelToCommissionDetail(model);

            _unitOfWork.GetRepository <Txn_CommissionDetail>().Insert(commissionDetail);

            // Execute commission
            _unitOfWork.Execute();
        }
        public void UpdatePo(PoDetailViewModel model)
        {
            // PO
            IRepository <Txn_PO> repoPo = _unitOfWork.GetRepository <Txn_PO>();
            Txn_PO po = repoPo.GetQueryable(o => o.PoId == model.PoId).FirstOrDefault();

            if (po != null)
            {
                po.PoNumber        = model.PoNumber;
                po.PoDate          = model.PoDate;
                po.PoFilePath      = model.PoFilePath;
                po.InvoiceFilePath = model.InvoiceFilePath;
                po.CustomerName    = model.CustomerName;
                repoPo.Update(po);
            }

            // PO Detail
            IRepository <Txn_PODetail> repoPoDetail = _unitOfWork.GetRepository <Txn_PODetail>();
            Txn_PODetail poDetail = repoPoDetail.GetQueryable(o => o.PoDetailId == model.PoDetailId).FirstOrDefault();

            if (poDetail != null)
            {
                //poDetail.PoId = model.PoId;
                poDetail.ProductName        = model.ProductName;
                poDetail.ProductBrand       = model.ProductBrand;
                poDetail.ProductType        = model.ProductType;
                poDetail.PricePerUnit       = model.PricePerUnit;
                poDetail.ActualPricePerUnit = model.ActualPricePerUnit;
                poDetail.Amount             = model.Amount;
                poDetail.TotalPrice         = model.TotalPrice;
                poDetail.ActualTotalPrice   = model.ActualTotalPrice;
                poDetail.Tax = model.Tax;
                poDetail.TransportLocation      = model.TransportLocation;
                poDetail.TransportFee           = model.TransportFee;
                poDetail.ParcelFee              = model.ParcelFee;
                poDetail.ServiceFee             = model.ServiceFee;
                poDetail.OtherFee               = model.OtherFee;
                poDetail.CustomerLead           = model.CustomerLead;
                poDetail.CustomerLeadPercentage = model.CustomerLeadPercentage;
                poDetail.TotalCost              = model.TotalCost;
                poDetail.Profit           = model.Profit;
                poDetail.ProfitPercentage = model.ProfitPercentage;
                poDetail.Remark           = model.Remark;
                repoPoDetail.Update(poDetail);
            }

            // Commission
            IRepository <Txn_Commission> repoCommission = _unitOfWork.GetRepository <Txn_Commission>();
            Txn_Commission commission = repoCommission.GetQueryable(o => o.PoDetailId == model.PoDetailId).FirstOrDefault();

            if (commission != null)
            {
                commission.TotalCommission      = model.Commission.TotalCommission;
                commission.CommissionSales      = model.Commission.CommissionSales;
                commission.CommissionPM         = model.Commission.CommissionPM;
                commission.CommissionApp        = model.Commission.CommissionApp;
                commission.CommissionInstall    = model.Commission.CommissionInstall;
                commission.CommissionAdmin      = model.Commission.CommissionAdmin;
                commission.CommissionOutside    = model.Commission.CommissionOutside;
                commission.ConclusionCommission = model.Commission.ConclusionCommission;
                commission.AuditCommission      = model.Commission.AuditCommission;
                repoCommission.Update(commission);
            }

            // CommissionDetail
            IRepository <Txn_CommissionDetail> repoCommissionDetail = _unitOfWork.GetRepository <Txn_CommissionDetail>();
            List <Txn_CommissionDetail>        commissionDetailList = repoCommissionDetail.GetQueryable(o => o.PoDetailId == model.PoDetailId).ToList();

            foreach (Txn_CommissionDetail record in commissionDetailList)
            {
                repoCommissionDetail.Delete(record);
            }
            List <Txn_CommissionDetail> commissionDetail = MappingPoDetailViewModelToCommissionDetail(model);

            _unitOfWork.GetRepository <Txn_CommissionDetail>().Insert(commissionDetail);


            // Commit transaction
            _unitOfWork.Execute();
        }