Example #1
0
        public JsonResult Edit(int BillID, string BillNo, int NewAmount, string chequeBransh, string chequeNumber, DateTime chequeNumberDate, int methodIDToBill)
        {
            if (ModelState.IsValid)
            {
                //  db.Entry(pur_purshaseOrders).State = EntityState.Modified;

                var v         = db.pur_Bills.Where(I => I.BillID == BillID).FirstOrDefault();
                var OldAmount = v.BillPaidAmount;
                if (v.BillTotalAmount - (OldAmount + NewAmount) > 0)
                {
                    v.BillPaidAmount      = OldAmount + NewAmount;
                    v.BillRemainingAmount = v.BillTotalAmount - (OldAmount + NewAmount);
                    v.PaymentMethodID     = v.PaymentMethodID == methodIDToBill ? v.PaymentMethodID : 3;
                    v.BillPaymentStatus   = v.BillRemainingAmount == 0 ? 2 : 1;
                    db.SaveChanges();
                }
                else
                {
                    return(Json(0));
                }
                //var lastbill = ( from items in db.pur_BillDetails where (items.BillNo == BillNo) select items ).ToList();
                //var mid = methodIDToBill;
                //foreach(var x in lastbill)
                //{
                //    if (x.PaymentMethodID != methodIDToBill)
                //    {
                //        mid = 3;
                //        break;
                //    }
                //}
                string decription = " دفع مبلغ بقيمة : ";
                decription += " " + NewAmount + " ";
                decription += "الى المورد : ";
                decription += v.SuplierName + "";
                decription += " والمتبقى : ";
                decription += v.BillTotalAmount - (OldAmount + NewAmount) + "  ";

                var billhestory = new pur_BillDetails
                {
                    Amount                 = NewAmount,
                    PaymentMethodID        = methodIDToBill,
                    BillDetailsDate        = DateTime.Now,
                    BillDetailsDescription = decription,
                    BillNo                 = BillNo,
                    ChequeBank             = chequeBransh,
                    ChequeDate             = chequeNumberDate,
                    ChequeNo               = chequeNumber,
                    UpLoaded               = false,
                    ChequeBankBranch       = chequeBransh,
                };
                db.pur_BillDetails.Add(billhestory);
                db.SaveChanges();
                #region
                var     suplier = db.pay_SuplierAcounts.Where(s => s.SuplierID == v.SuplierID).Count();
                decimal?LastSuplierAcountAmount = 0;
                if (suplier > 0)
                {
                    LastSuplierAcountAmount = db.pay_SuplierAcounts.Where(s => s.SuplierID == v.SuplierID).OrderBy(c => 1 == 1).Skip(suplier - 1).FirstOrDefault().SuplierAcountAmount;
                }
                else
                {
                    LastSuplierAcountAmount = 0;
                }
                var supAcount = new pay_SuplierAcounts
                {
                    PaymentMethodID              = methodIDToBill,
                    SuplierAcountDepit           = NewAmount,
                    PaymentDescription           = decription,
                    SuplierAcountAmount          = 0,//LastSuplierAcountAmount - NewAmount,
                    SuplierAcountCredit          = 0,
                    SuplierID                    = v.SuplierID,
                    SuplierName                  = v.SuplierName,
                    SuplierAcountTransactionDate = DateTime.Now,
                    UpLoaded = false
                };
                db.pay_SuplierAcounts.Add(supAcount);
                db.SaveChanges();
                #endregion
            }


            return(Json(1));
        }
Example #2
0
        public JsonResult ComplatePurchaseOrder
            (DateTime chequeNumberDate, string OrderNumber, decimal OrderpaymentAmount, string CategoryIDArray, string ItemIDArray, string NamesArray, string ItempurchasePriceArray, string QuantitiesArray, decimal OrderAmount, int SuplierID, string SuplierName, int paymentMethodeID, string chequeNumber, string chequeBransh)
        {
            var Remaining = OrderAmount - OrderpaymentAmount;

            int[]    ItemIDs     = Array.ConvertAll(ItemIDArray.Split(','), int.Parse);
            string[] Names       = NamesArray.Split(',');
            int[]    CategoryIDs = Array.ConvertAll(CategoryIDArray.Split(','), int.Parse);

            int[] ItemspurchasePrice = Array.ConvertAll(ItempurchasePriceArray.Split(','), int.Parse);
            int[] Quantities         = Array.ConvertAll(QuantitiesArray.Split(','), int.Parse);

            string decription = " إضافة بضاعة بقيمة : ";

            decription += " " + OrderAmount + " ";
            decription += "من المورد : ";
            decription += SuplierName + "";
            // ################################################ //
            //        Phase 0: select purchaseNo         //
            try
            {
                #region
                var    NewbillSNo             = db.pur_Bills.Count();
                var    NewbillNo              = "";
                string DateFormateToInventory = "PUR" + DateTime.Now.ToString("yyyyMMdd");
                if (NewbillSNo > 0)
                {
                    NewbillNo = db.pur_Bills.OrderByDescending(o => o.BillID).FirstOrDefault().BillNo;
                    var LastNo = NewbillNo.Remove(0, DateFormateToInventory.Length);
                    NewbillNo = DateFormateToInventory + (int.Parse(LastNo) + 1).ToString(); //NewbillNo.Substring(0, DateFormateToInventory.) +Int32.Parse(NewbillNo.Substring(9, -1)+1);
                }
                else
                {
                    NewbillNo = DateFormateToInventory + "1";
                }
                Console.Write("dsdsa");
                #endregion

                // ################################################ //
                //             Phase 2: Insert in   pur_BillDetails         //
                // ################################################ //
                #region
                var billDetailes = new pur_BillDetails
                {
                    Amount                 = OrderAmount,
                    BillDetailsDate        = DateTime.Now,
                    BillDetailsDescription = decription,
                    BillNo                 = NewbillNo,
                    PaymentMethodID        = paymentMethodeID,
                    ChequeNo               = chequeNumber,
                    ChequeBank             = chequeBransh,
                    UpLoaded               = false,
                    ChequeDate             = chequeNumberDate,
                    ChequeBankBranch       = chequeBransh,
                };
                db.pur_BillDetails.Add(billDetailes);
                db.SaveChanges();
                #endregion
                // ################################################ //
                //        Phase 2: Insert Purshase Bill (order)     //
                // ################################################ //

                string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;

                // ################################################ //
                //             Phase 2: Insert Purshase History     //
                // ################################################ //

                // ################################################ //
                //             Phase 3: Empty Parshase Cart
                // ################################################ //
                #region
                using (SqlConnection con2 = new SqlConnection(constr))
                {
                    using (SqlCommand cmd2 = new SqlCommand("Delete_PurchaseCart"))
                    {
                        cmd2.Connection = con2;
                        con2.Open();
                        cmd2.ExecuteNonQuery();
                        con2.Close();
                    }
                }
                #endregion

                // ################################################ //
                //        Phase 2: Insert Purshase      //
                // ################################################ //
                #region
                for (int i = 0; i < Names.Length; i++)
                {
                    var name     = Names[i];
                    var quantity = Quantities[i];
                    //   var sellamount = ItemsSellPrice[i];
                    var itemID = ItemIDs[i];
                    // var ItemInventoryID = ItemInventoryIDs[i];

                    //var ItemCostPurchase = ItemsCostPurchase[i];
                    var ItempurchasePrice = ItemspurchasePrice[i];

                    var purchase = new pur_Purchase
                    {
                        BillNo                  = NewbillNo,
                        CategoryID              = CategoryIDs[i],
                        ItemCostPurchase        = 0,
                        ItemID                  = itemID,
                        ItemPurchaseCurrencyID  = 1,
                        ItemPurchasePrice       = ItempurchasePrice,
                        ItemSalePrice           = 0,
                        ItemSaleCurrencyID      = 1,
                        PurchaseItemID          = itemID,
                        ItemTotalPurchaseAmount = ItempurchasePrice * quantity,
                        PurchaseItemNameAr      = name,
                        PurchaseItemNameEn      = name,
                        PurchaseItemAddedDate   = DateTime.Now,
                        PurchaseItemQuantity    = quantity,
                        PurchaseStatusID        = 0,
                        IsReturn                = false,
                        SubCategoryID           = 1,
                        UpLoaded                = false,
                        BarCode                 = "",
                        SuplierID               = SuplierID,
                        SuplierName             = SuplierName
                    };
                    db.pur_Purchase.Add(purchase);
                    db.SaveChanges();
                }
                #endregion

                // ################################################ //
                //     (OPtional)  Phase 5: Insert Purshases as Requseted  Items    //
                // ################################################ //

                // ############################################## //
                //   Phase 6: Insert Suplier payment transaction  //
                // ############################################## //
                #region
                var     suplier = db.pay_SuplierAcounts.Where(s => s.SuplierID == SuplierID).Count();
                decimal?LastSuplierAcountAmount = 0;
                if (suplier > 0)
                {
                    LastSuplierAcountAmount = db.pay_SuplierAcounts.OrderBy(c => 1 == 1).Skip(suplier - 1).FirstOrDefault().SuplierAcountAmount;
                }
                else
                {
                    LastSuplierAcountAmount = 0;
                }
                var suplierPayment = new pay_SuplierAcounts();
                //suplierPayment.SuplierID = SuplierID;
                //suplierPayment.SuplierName = SuplierName;
                //suplierPayment.SuplierAcountCredit = OrderAmount;
                //suplierPayment.CustomeAcountDepit = 0;
                //suplierPayment.UpLoaded = false;
                //suplierPayment.PaymentDescription = decription;
                //suplierPayment.PaymentMethodID = paymentMethodeID;
                //suplierPayment.SuplierAcountTransactionDate = DateTime.Now;
                //suplierPayment.SuplierAcountAmount = LastSuplierAcountAmount + OrderAmount;
                //db.pay_SuplierAcounts.Add(suplierPayment);
                //db.SaveChanges();
                decription  = " دفع مبلغ بقيمة : ";
                decription += " " + OrderpaymentAmount + " ";
                decription += " والمتبقى : ";
                decription += Remaining + "  ";
                decription += "الى المورد : ";
                decription += SuplierName + "";
                suplierPayment.SuplierID                    = SuplierID;
                suplierPayment.SuplierName                  = SuplierName;
                suplierPayment.SuplierAcountCredit          = 0;
                suplierPayment.SuplierAcountDepit           = OrderpaymentAmount;
                suplierPayment.UpLoaded                     = false;
                suplierPayment.PaymentDescription           = decription;
                suplierPayment.PaymentMethodID              = paymentMethodeID;
                suplierPayment.SuplierAcountAmount          = 0;///(LastSuplierAcountAmount + OrderAmount) - OrderpaymentAmount;
                suplierPayment.SuplierAcountTransactionDate = DateTime.Now;
                db.pay_SuplierAcounts.Add(suplierPayment);
                db.SaveChanges();


                #endregion
                // ############################################## //
                //   Phase 6: Insert bill payment transaction  //
                // ############################################## //
                #region
                string Billdecription = " إضافة بضاعة بقيمة : ";
                Billdecription += " " + OrderAmount + " ";
                Billdecription += "من المورد : ";
                Billdecription += SuplierName + "";
                var     lastPayMent       = db.pur_Bills.Count();
                decimal?lastPayMentAmount = 0;
                if (lastPayMent > 0)
                {
                    lastPayMentAmount = db.pur_Bills.OrderBy(c => 1 == 1).Skip(lastPayMent - 1).FirstOrDefault().BillNetAmount;
                }
                else
                {
                    lastPayMentAmount = 0;
                }
                var bill = new pur_Bills
                {
                    TotalCostPurchase   = 0,
                    BillNo              = NewbillNo,
                    SuplierID           = SuplierID,
                    SuplierName         = SuplierName,
                    TotalAmount         = OrderAmount,
                    BillPaidAmount      = OrderpaymentAmount,
                    BillRemainingAmount = Remaining,
                    BillStatusID        = 0,
                    BillDescription     = decription,
                    BillPaymentStatus   = 1,
                    BillDate            = DateTime.Now,
                    BillTotalAmount     = OrderAmount,
                    BillIsReturned      = false,
                    BillNetAmount       = OrderAmount,
                    UpLoaded            = false,
                    PaymentMethodID     = paymentMethodeID,
                };
                db.pur_Bills.Add(bill);
                db.SaveChanges();
                #endregion
                if (Remaining == 0)
                {
                    Functions.Functions.InsertPaymentHistory(0, OrderpaymentAmount, decription, paymentMethodeID);
                }
                else
                {
                    Functions.Functions.InsertPaymentHistory(0, OrderpaymentAmount, decription, paymentMethodeID);
                    Functions.Functions.InsertPaymentHistory(Remaining, 0, decription, paymentMethodeID);
                }
            }
            catch (Exception ex)
            {
                return(Json(2));
            }

            return(Json(1));
        }