Exemplo n.º 1
0
        /// <summary>
        /// Editing the transaction order by the transaction order id in the database
        /// </summary>
        /// <param name="transactionOrder">
        /// The modified transaction that would be replaced, can't change transaction order id
        /// </param>
        /// <returns>
        /// It will return <see cref="bool"/>, true if its successful and false otherwise.
        /// </returns>
        public static bool EditTransaction(TransactionOrder transactionOrder)
        {
            DeleteTransaction(transactionOrder.Id);
            string fileName = TransactionFolder() + transactionOrder.Id + @".json";

            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.WriteLine(JsonConvert.SerializeObject(transactionOrder));
                }

                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a transaction order and store it in a JSON file on Transaction Database Folder
        /// </summary>
        /// <param name="transactionOrder">
        /// This is the transaction order that will be created and stored in the database
        /// </param>
        /// <returns>
        /// The <see cref="TransactionOrder"/> that has been created and stored.
        /// </returns>
        public static TransactionOrder CreateTransaction(TransactionOrder transactionOrder)
        {
            var id = GenerateTransactionId() + " - "
                     + transactionOrder.TransactionType.Replace(".", string.Empty).Replace("/", " ") + " - "
                     + transactionOrder.DateTransaction.ToShortDateString().Replace(".", string.Empty).Replace("/", " ");
            var path = TransactionFolder() + id + @".json";

            transactionOrder.Id = id;
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(JsonConvert.SerializeObject(transactionOrder));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(transactionOrder);
        }
        /// <summary>
        /// The database backup.
        /// </summary>
        /// <param name="saveDialog">
        /// The save Dialog.
        /// </param>
        /// <param name="location">
        /// The location.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool DatabaseBackup(bool saveDialog = false, string location = "")
        {
            var backupName = "Backup - " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture)
                             + ".stockbook";

            var databaseBackup = new DatabaseBackupModel
            {
                Date                 = DateTime.Now,
                Products             = Product.GetAllProducts(),
                TransactionOrders    = TransactionOrder.GetAllTransactions(),
                ProductIdCounter     = Product.GetProductIdCounter(),
                TransactionIdCounter = TransactionOrder.GetTransactionIdCounter()
            };

            if (saveDialog)
            {
                var saveFileDialog = new SaveFileDialog
                {
                    FileName         = backupName,
                    Filter           = "Stock Book Database Backup | *.stockbook",
                    InitialDirectory = "Desktop"
                };
                if (saveFileDialog.ShowDialog() == true)
                {
                    File.WriteAllText(saveFileDialog.FileName, JsonConvert.SerializeObject(databaseBackup));
                }
            }
            else if (!string.IsNullOrWhiteSpace(location))
            {
                Directory.CreateDirectory(location);
                File.WriteAllText(location + backupName, JsonConvert.SerializeObject(databaseBackup));
            }
            else
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// The restore backup.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool RestoreBackup()
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter           = "Stock Book Database Backup | *.stockbook",
                InitialDirectory = "Desktop"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    var readAllText    = File.ReadAllText(openFileDialog.FileName, Encoding.Default);
                    var databaseBackup = JsonConvert.DeserializeObject <DatabaseBackupModel>(readAllText);

                    Product.EditProductIdCounter(databaseBackup.ProductIdCounter - databaseBackup.Products.Count);
                    TransactionOrder.EditTransactionIdCounter(databaseBackup.TransactionIdCounter - databaseBackup.TransactionOrders.Count);
                    foreach (var prod in databaseBackup.Products)
                    {
                        Product.CreateProduct(prod);
                    }

                    foreach (var transaction in databaseBackup.TransactionOrders)
                    {
                        TransactionOrder.CreateTransaction(transaction);
                    }

                    return(true);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
            /// <summary>
            /// The export transaction invoice.
            /// </summary>
            /// <param name="transOrder">
            /// The trans order.
            /// </param>
            public static void ExportTransactionInvoice(TransactionOrder transOrder)
            {
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = transOrder.DateTransaction.ToString("MMMMM dd yyyy") + " - " + transOrder.RefNo;
                // Default file name
                dlg.DefaultExt = ".xlsx";                         // Default file extension
                dlg.Filter     = "Excel Document (.xlsx)|*.xlsx"; // Filter files by extension
                Nullable <bool> result = dlg.ShowDialog();

                if (result == true)
                {
                    string   filename     = dlg.FileName;
                    FileInfo templateFile = new FileInfo(@"Invoice Template.xlsx");
                    FileInfo newFile      = new FileInfo(filename);
                    if (newFile.Exists)
                    {
                        try
                        {
                            newFile.Delete();
                            newFile = new FileInfo(filename);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("The file is currently being used, close the file or choose a different name.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                    }
                    var config = StockbookWindows.OpenConfig();
                    using (ExcelPackage package = new ExcelPackage(newFile, templateFile))
                    {
                        var currency = string.Empty;
                        switch (config.Currency)
                        {
                        case "PHP - ₱":
                            currency = "₱#,###,##0.00";
                            break;

                        case "USD - $":
                            currency = "$#,###,##0.00";
                            break;

                        case "YEN - ¥":
                            currency = "¥#,###,##0.00";
                            break;
                        }

                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                        worksheet.Cells["A1"].Value = config.CompanyName;
                        worksheet.Cells["A2"].Value = "SALES INVOICE - REFERENCE NUMBER: " + transOrder.RefNo;
                        worksheet.Cells["A3"].Value = "DATE: " + transOrder.DateTransaction.ToString("MMMM dd, yyyy");
                        worksheet.Cells["A4"].Value = "SOLD TO: " + transOrder.Particular;
                        worksheet.Cells["A5"].Value = "ADDRESS: " + transOrder.ParticularAddress;
                        worksheet.Cells["A6"].Value = "SALESMAN: " + transOrder.SalesmanName;
                        worksheet.Cells["F4"].Value = "TERMS: " + transOrder.Terms;
                        worksheet.Cells["F5"].Value = "DISCOUNT: " + transOrder.DiscountPercentage + "%";
                        int     i            = 9;
                        decimal unitTotal    = 0;
                        decimal billedTotal  = 0;
                        string  tempCategory = string.Empty;
                        foreach (var trans in transOrder.Transactions)
                        {
                            decimal tempTotal = 0;
                            if (trans.CaseTransact != 0)
                            {
                                tempTotal = trans.Product.CaseValue * trans.CaseTransact;
                                worksheet.Cells["A" + i].Value = trans.Product.ProdCode;
                                worksheet.Cells["B" + i].Value = trans.Product.Name;
                                worksheet.Cells["C" + i].Value = "Case";
                                worksheet.Cells["D" + i].Value = trans.CaseTransact;
                                worksheet.Cells["E" + i].Value = trans.Product.CaseValue;
                                unitTotal += tempTotal;
                                if (transOrder.DiscountPercentage > 0)
                                {
                                    tempTotal = ((transOrder.DiscountPercentage / 100) + 1) * tempTotal;
                                }
                                billedTotal += tempTotal;
                                worksheet.Cells["F" + i].Value = tempTotal;
                                i++;
                            }
                            if (trans.PackTransact != 0)
                            {
                                tempTotal = trans.Product.PackValue * trans.PackTransact;
                                worksheet.Cells["A" + i].Value = trans.Product.ProdCode;
                                worksheet.Cells["B" + i].Value = trans.Product.Name;
                                worksheet.Cells["C" + i].Value = "Pack";
                                worksheet.Cells["D" + i].Value = trans.PackTransact;
                                worksheet.Cells["E" + i].Value = trans.Product.PackValue;
                                unitTotal += tempTotal;
                                if (transOrder.DiscountPercentage > 0)
                                {
                                    tempTotal = ((transOrder.DiscountPercentage / 100) + 1) * tempTotal;
                                }
                                billedTotal += tempTotal;
                                worksheet.Cells["F" + i].Value = tempTotal;
                                i++;
                            }
                            if (trans.PieceTransact != 0)
                            {
                                tempTotal = trans.Product.PieceValue * trans.PieceTransact;
                                worksheet.Cells["A" + i].Value = trans.Product.ProdCode;
                                worksheet.Cells["B" + i].Value = trans.Product.Name;
                                worksheet.Cells["C" + i].Value = "Piece";
                                worksheet.Cells["D" + i].Value = trans.PieceTransact;
                                worksheet.Cells["E" + i].Value = trans.Product.PieceValue;
                                unitTotal += tempTotal;
                                if (transOrder.DiscountPercentage > 0)
                                {
                                    tempTotal = ((transOrder.DiscountPercentage / 100) + 1) * tempTotal;
                                }
                                billedTotal += tempTotal;
                                worksheet.Cells["F" + i].Value = tempTotal;
                                i++;
                            }
                        }

                        worksheet.Cells["A" + 9 + ":A" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        worksheet.Cells["b" + 9 + ":B" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        worksheet.Cells["C" + 9 + ":C" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        worksheet.Cells["D" + 9 + ":D" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        worksheet.Cells["E" + 9 + ":E" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        worksheet.Cells["F" + 9 + ":F" + i].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                        worksheet.Cells["A" + 9 + ":A" + i].Style.WrapText = true;
                        worksheet.Cells["b" + 9 + ":B" + i].Style.WrapText = true;
                        worksheet.Cells["C" + 9 + ":C" + i].Style.WrapText = true;
                        worksheet.Cells["D" + 9 + ":D" + i].Style.WrapText = true;
                        worksheet.Cells["E" + 9 + ":E" + i].Style.WrapText = true;
                        worksheet.Cells["F" + 9 + ":F" + i].Style.WrapText = true;

                        i++;
                        worksheet.Cells["C" + i + ":C" + (i + 2)].Style.Numberformat.Format = currency;
                        worksheet.Cells["F" + i + ":F" + (i + 2)].Style.Numberformat.Format = currency;

                        worksheet.Cells["C" + i + ":C" + (i + 2)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                        worksheet.Cells["F" + i + ":F" + (i + 2)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                        worksheet.Cells["E" + i + ":E" + (i + 2)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                        worksheet.Cells["B" + i + ":B" + (i + 2)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                        worksheet.Cells["B" + i].Value       = "Vatable Sales: ";
                        worksheet.Cells["C" + i].Value       = billedTotal * (decimal)0.88;
                        worksheet.Cells["B" + (i + 1)].Value = "12% VAT: ";
                        worksheet.Cells["C" + (i + 1)].Value = billedTotal * (decimal)0.12;
                        worksheet.Cells["B" + (i + 2)].Value = "Total w/o Discount: ";
                        worksheet.Cells["C" + (i + 2)].Value = billedTotal;

                        worksheet.Cells["E" + i].Value       = "Invoice Amt.: ";
                        worksheet.Cells["F" + i].Value       = unitTotal;
                        worksheet.Cells["E" + (i + 1)].Value = "Discount: " + transOrder.DiscountPercentage + "%";
                        worksheet.Cells["F" + (i + 1)].Value = unitTotal * (transOrder.DiscountPercentage / 100);
                        worksheet.Cells["E" + (i + 2)].Value = "Total w/ Discount: ";
                        worksheet.Cells["F" + (i + 2)].Value = billedTotal;

                        i += 6;
                        worksheet.Cells["A" + i].Value       = "PREPARED BY/DATE";
                        worksheet.Cells["A" + (i + 3)].Value = "PRINT NAME & SIGNATURE";
                        worksheet.Cells["D" + i].Value       = "DELIVERED BY/DATE";
                        worksheet.Cells["D" + (i + 3)].Value = "PRINT NAME & SIGNATURE";
                        i += 6;
                        worksheet.Cells["A" + i].Value       = "CHECKED BY/DATE";
                        worksheet.Cells["A" + (i + 3)].Value = "PRINT NAME & SIGNATURE";
                        worksheet.Cells["D" + i].Value       = "APPROVED BY";
                        worksheet.Cells["D" + (i + 3)].Value = "PRINT NAME & SIGNATURE";
                        worksheet.View.PageLayoutView        = false;

                        package.Save();
                    }
                }
            }