private void PopupModelControl_Load(object sender, EventArgs e)
        {
            if (_dealer != null)
            {
                CB_PMC_SelectDealer.Text = _dealer.Code;
            }

            if (_dealerList != null)
            {
                LPMC_Title.Text = "ADD New Collection";
                BPMC_Add.Text   = "Add";

                _collection = new SalesAndCollectionModel
                {
                    Id               = Guid.NewGuid().ToString("N"),
                    Sl               = (SalesAndCollectionManager.GetAllSalesAndCollectionsCount() + 1).ToString(),
                    DealerName       = "",
                    Address          = "",
                    Contact          = "",
                    DealerCode       = "",
                    Date             = DTP_PMC_Date.Value.Date,
                    IC_NO            = "",
                    MR_NO            = "",
                    OpeningBalance   = null,
                    SalesAmount      = 0,
                    CollectionAmount = 0,
                    ClosingBalance   = null,
                    Remarks          = "",
                    SyncType         = "Collection"
                };
            }

            BindObjectDataToInterface();
        }
        private void BSAC_DeleteSOC_Click(object sender, EventArgs e)
        {
            if (_tempedSalesAndCollections.Count > 0)
            {
                DataGridViewRow row = this.DGV_SAC_SalesAndCollectionList.SelectedRows[0];

                if (row != null)
                {
                    DialogResult dialogResult = MessageBox.Show(" Are you sure? ", "Confirm Delete?", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        SalesAndCollectionModel soc = row.DataBoundItem as SalesAndCollectionModel;

                        if (CKB_GroupBy.Checked)
                        {
                            if (CB_SAC_DealerCode.Text.ToLower().Equals("any"))
                            {
                                MessageBox.Show("Please select a Dealer code!");
                            }
                            else
                            {
                                if (SalesAndCollectionManager.DeleteSaleOrCollection(soc, true))
                                {
                                    MessageBox.Show("All sales and collections for Dealer :" + soc.DealerName + "(" + soc.DealerCode + ") on Date : " + soc.Date + " has been Deleated!");
                                    BindAllFilters();
                                }
                                else
                                {
                                    MessageBox.Show("Error! Something went wrong while Deleting!!");
                                }
                            }
                        }
                        else
                        {
                            if (SalesAndCollectionManager.DeleteSaleOrCollection(soc, false))
                            {
                                MessageBox.Show("Deleated Successfully!");
                                BindAllFilters();
                            }
                            else
                            {
                                MessageBox.Show("Error! Something went wrong while Deleting!!");
                            }
                        }
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do something else
                    }
                }
                else
                {
                    MessageBox.Show("Select a row to delete!");
                }
            }
            else
            {
                MessageBox.Show("List is Empty :(");
            }
        }
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool isSuccess = false;

            if (isPartyLedger)
            {
                isSuccess = SalesAndCollectionManager.ExportAsPartyLedgerExcell(_fullPath, _tempedSalesAndCollections);
            }
            else
            {
                isSuccess = SalesAndCollectionManager.ExportAsDailyExcell(_fullPath, _tempedSalesAndCollections);
            }

            if (isSuccess)
            {
                MessageBox.Show("File has been saved successfully");
            }
            else
            {
                MessageBox.Show("Error! Something went wrong while saving file!!");
            }
        }
        private void BindInterfaceDataToObject()
        {
            var selectedDealer = _dealerList.Where(x => x.Code.Equals(CB_PMC_SelectDealer.Text)).FirstOrDefault();

            _collection.DealerName = selectedDealer.DealerName;
            _collection.DealerCode = selectedDealer.Code;
            _collection.Address    = selectedDealer.Address;
            _collection.Contact    = selectedDealer.Contact;

            _collection.MR_NO            = TB_PMC_MR_No.Text;
            _collection.IC_NO            = TB_PMC_IC_No.Text;
            _collection.CollectionAmount = Convert.ToInt32(TB_PMC_CollectionAmount.Text);
            _collection.Remarks          = TB_PMC_Remark.Text;
            _collection.Date             = DTP_PMC_Date.Value.Date;

            if (_collection.CollectionAmount <= 0
                //|| _collection.MR_NO.Equals("")
                //|| _collection.IC_NO.Equals("")
                )
            {
                MessageBox.Show("Make sure Collection amount is not 0 ");
            }
            else
            {
                ConfirmChange();
            }

            void ConfirmChange()
            {
                DialogResult dialogResult = MessageBox.Show(BPMC_Add.Text + " Collection? ", "Confirm Change?", MessageBoxButtons.YesNo);

                if (dialogResult == DialogResult.Yes)
                {
                    if (_collection != null)
                    {
                        if (BPMC_Add.Text.Equals("Add"))
                        {
                            if (SalesAndCollectionManager.AddNewSale(_collection))
                            {
                                MessageBox.Show("Collection Added successfully!");

                                ReloadDealerControl();
                            }
                            else
                            {
                                MessageBox.Show("Error! Something went wrong!!");
                            }
                        }
                    }
                    else
                    {
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    //do something else
                }

                void ReloadDealerControl()
                {
                    _hPanel.Controls.Clear();
                    _salesAndCollectionControl = new SalesAndCollectionControl(_hPanel, _vPanel);
                    _hPanel.Controls.Add(_salesAndCollectionControl);
                    _salesAndCollectionControl.Dock = DockStyle.Fill;
                    _salesAndCollectionControl.Show();
                    _popUpForm.Close();
                }
            }
        }
        private void BindAllFilters()
        {
            var filteredSalesAndCollections = new List <SalesAndCollectionModel>();

            if (string.IsNullOrEmpty(CB_SAC_DealerCode.Text))
            {
                CB_SAC_DealerCode.Text = "Any";
                CB_SAC_DealerCode.Items.Add("Any");
                CB_SAC_DealerCode.Items.AddRange(_dealerList.Select(x => x.Code).ToArray());
            }
            else if (CB_SAC_DealerCode.Items.Count <= 1)
            {
                CB_SAC_DealerCode.Items.Add("Any");
                CB_SAC_DealerCode.Items.AddRange(_dealerList.Select(x => x.Code).ToArray());
            }

            if (CB_SAC_DealerCode.Text.ToLower().Equals("any"))
            {
                _dealer = null;
            }
            else
            {
                if (!string.IsNullOrEmpty(CB_SAC_DealerCode.Text))
                {
                    _dealer = DealerManager.FindDealer(CB_SAC_DealerCode.Text);
                }
            }

            if (_dealer != null)
            {
                CB_SAC_DealerCode.Text = _dealer.Code;

                LSAC_PartyName.Visible      = true;
                LSAC_PartyAddress.Visible   = true;
                LSAC_PartyContact.Visible   = true;
                TB_SAC_PartyName.Visible    = true;
                TB_SAC_PartyAddress.Visible = true;
                TB_SAC_PartyContact.Visible = true;

                TB_SAC_PartyName.Text    = _dealer.DealerName;
                TB_SAC_PartyAddress.Text = _dealer.Address;
                TB_SAC_PartyContact.Text = _dealer.Contact;
                //TB_SAC_PartyName.Enabled = false;
                //TB_SAC_PartyAddress.Enabled = false;
                //TB_SAC_PartyContact.Enabled = false;
            }
            else
            {
                LSAC_PartyName.Visible      = false;
                LSAC_PartyAddress.Visible   = false;
                LSAC_PartyContact.Visible   = false;
                TB_SAC_PartyName.Visible    = false;
                TB_SAC_PartyAddress.Visible = false;
                TB_SAC_PartyContact.Visible = false;

                TB_SAC_PartyName.Text    = "";
                TB_SAC_PartyAddress.Text = "";
                TB_SAC_PartyContact.Text = "";
            }

            if (CKB_GroupBy.Checked)
            {
                if (CB_SAC_DealerCode.Text.ToLower().Equals("any"))
                {
                    filteredSalesAndCollections = SalesAndCollectionManager.EverydaySalesAndCollections();
                }
                else
                {
                    filteredSalesAndCollections = SalesAndCollectionManager.EverydaySalesAndCollections(CB_SAC_DealerCode.Text);
                }
            }
            else
            {
                if (CB_SAC_DealerCode.Text.ToLower().Equals("any"))
                {
                    filteredSalesAndCollections = SalesAndCollectionManager.GetAllSalesAndCollections();
                }
                else
                {
                    filteredSalesAndCollections = SalesAndCollectionManager.GetSalesAndCollections(CB_SAC_DealerCode.Text);
                }
            }

            if (filteredSalesAndCollections.Count > 0)
            {
                if (!string.IsNullOrEmpty(TB_SAC_Search.Text))
                {
                    filteredSalesAndCollections = SearchResult(TB_SAC_Search.Text, filteredSalesAndCollections);
                }

                if (CB_SAC_TimePeriod.Text.Equals("Custom"))
                {
                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date >= DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Today"))
                {
                    DTP_SAC_FromDate.Value = DateTime.Today;
                    DTP_SAC_ToDate.Value   = DTP_SAC_FromDate.Value;

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date > DTP_SAC_FromDate.Value.AddDays(-1) && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Yesterday"))
                {
                    DTP_SAC_FromDate.Value = DateTime.Today.AddDays(-1);
                    DTP_SAC_ToDate.Value   = DTP_SAC_FromDate.Value;

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date > DTP_SAC_FromDate.Value.AddDays(-1) && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Weekly"))
                {
                    DTP_SAC_FromDate.Value = DateTime.Today.AddDays(-7);
                    DTP_SAC_ToDate.Value   = DateTime.Today;

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date > DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Monthly"))
                {
                    DTP_SAC_FromDate.Value = DateTime.Today.AddDays(-30);
                    DTP_SAC_ToDate.Value   = DateTime.Today;

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date > DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("This Month"))
                {
                    DateTime date = DateTime.Today;
                    DTP_SAC_FromDate.Value = new DateTime(date.Year, date.Month, 1);
                    DTP_SAC_ToDate.Value   = DTP_SAC_FromDate.Value.AddMonths(1).AddDays(-1);

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date >= DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Last Month"))
                {
                    DateTime date = DateTime.Today;
                    DTP_SAC_FromDate.Value = new DateTime(date.Year, date.AddMonths(-1).Month, 1);
                    DTP_SAC_ToDate.Value   = DTP_SAC_FromDate.Value.AddMonths(1).AddDays(-1);

                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date >= DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }
                else if (CB_SAC_TimePeriod.Text.Equals("Default"))
                {
                    DTP_SAC_FromDate.Value      = new DateTime(2020, 1, 1);
                    DTP_SAC_ToDate.Value        = DateTime.Today;
                    filteredSalesAndCollections = filteredSalesAndCollections.Where(x => x.Date >= DTP_SAC_FromDate.Value && x.Date <= DTP_SAC_ToDate.Value).ToList();
                }


                var allDates = filteredSalesAndCollections.Select(x => x.Date).Distinct().ToList();
                allDates.RemoveAll(item => item == null);

                if (CKB_Ascending.Checked)
                {
                    allDates = allDates.OrderBy(x => x.Value).ToList();
                }
                else
                {
                    allDates = allDates.OrderByDescending(x => x.Value).ToList();
                }

                var mapSalesAndCollections = new List <SalesAndCollectionModel>();

                foreach (var date in allDates)
                {
                    var thatDaysHistory = filteredSalesAndCollections.Where(x => x.Date == date).ToList();

                    var allDealerForThatDay = thatDaysHistory.Select(x => x.DealerCode).Distinct().ToList();

                    foreach (var dealerCode in allDealerForThatDay)
                    {
                        var dealerHistoryForThatDay = thatDaysHistory.Where(x => x.DealerCode.Equals(dealerCode)).ToList();
                        mapSalesAndCollections.AddRange(dealerHistoryForThatDay);
                    }
                }

                filteredSalesAndCollections = mapSalesAndCollections;

                int count = 1;
                foreach (var soc in filteredSalesAndCollections)
                {
                    soc.Sl = (count++).ToString();
                }
            }

            if (filteredSalesAndCollections != null)
            {
                BindDataTable(filteredSalesAndCollections);
                _tempedSalesAndCollections = filteredSalesAndCollections;
            }

            isTimePeriodChanged = false;
        }
예제 #6
0
        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IC_ProgressPanel.Visible   = false;
            InvoiceProgressBar.Visible = false;
            _vPanel.Enabled            = true;
            _hPanel.Enabled            = true;

            void UpdateProductStock()
            {
                foreach (var item in _invoicePage.AllProducts)
                {
                    var product = _productList.Where(x => x.ProductCode.Equals(item.ProductCode)).FirstOrDefault();

                    if (product != null)
                    {
                        if (_itemList != null)
                        {
                            var previousItem = _itemList.Where(x => x.ProductCode.Equals(item.ProductCode)).FirstOrDefault();
                            product.StockAvailable += previousItem.Quantity;
                        }

                        product.StockAvailable -= item.Quantity;
                    }
                }

                ProductManager.UpdateAllProducts(_productList);
            }

            void SaveInvoiceLogFile()
            {
                if (!string.IsNullOrEmpty(_invoicePage.CurrentLogFile))
                {
                    _invoicePage.PreviousLogFile = _invoicePage.CurrentLogFile;
                    FileSystemUtility.DeleteFile(_invoicePage.CurrentLogFile);
                }

                Files invoiceLog = new Files();

                invoiceLog.FileLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Invoiceasy\InvoiceLog";

                invoiceLog.FileName         = "InvoiceLog-" + _invoicePage.No + "_" + DTP_IC_Date.Value.ToString("dd MMMM yyyy") + " " + DateTime.Now.ToString("HH-mm-ss") + ".txt";
                _invoicePage.CurrentLogFile = invoiceLog.FileLocation + @"\" + invoiceLog.FileName;

                invoiceLog.FileText = JsonConvert.SerializeObject(_invoicePage);
                FileSystemUtility.DeleteFile(_invoicePage.CurrentLogFile);
                FileSystemUtility.Initialize(true, invoiceLog);
                FileSystemUtility.WriteFile();

                var logFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Invoiceasy" + @"\InvoiceItems\" + "Invoice-" + _invoicePage.No + "_" + "ItemList_" + DTP_IC_Date.Value.ToString("dd MMMM yyyy HH-mm-ss") + ".txt";

                FileSystemUtility.CreateFolder(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Invoiceasy" + @"\InvoiceItems");
                CsvHelperUtility.WriteDataToFile <ItemModel>(true, logFilePath, ",", _invoicePage.AllProducts);
            }

            void AddSale()
            {
                var sale = new SalesAndCollectionModel
                {
                    Id               = Guid.NewGuid().ToString("N"),
                    Sl               = (SalesAndCollectionManager.GetAllSalesAndCollectionsCount() + 1).ToString(),
                    DealerName       = _invoicePage.Dealer.DealerName,
                    Address          = _invoicePage.Dealer.Address,
                    Contact          = _invoicePage.Dealer.Contact,
                    DealerCode       = _invoicePage.Dealer.Code,
                    Date             = DTP_IC_Date.Value,
                    IC_NO            = _invoicePage.No,
                    MR_NO            = "",
                    OpeningBalance   = null,
                    SalesAmount      = _invoicePage.PayableAmount,
                    CollectionAmount = 0,
                    ClosingBalance   = null,
                    Remarks          = "",
                    SyncType         = "Sales"
                };

                if (_invoicePage.sale == null)
                {
                    _invoicePage.sale = sale;
                    SalesAndCollectionManager.AddNewSale(sale);
                }
                else
                {
                    SalesAndCollectionManager.UpdateSale(_invoicePage.sale, sale);
                }
            }

            BindInterfaceDataToObject();

            UpdateProductStock();

            AddSale();

            SaveInvoiceLogFile();

            if (_isSuccess)
            {
                MessageBox.Show("Invoice file has been saved successfully");
            }
            else
            {
                MessageBox.Show("Error! Something Went Wrong while saving the file");
            }

            LoadChallanForm();
        }