narsil/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs

443 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanshu.Accounts.BI;
using Tanshu.Accounts.Helpers;
using Tanshu.Accounts.Contracts;
using System.Windows.Forms;
using Tanshu.Common;
namespace Tanshu.Accounts.PointOfSale
{
public class BillController
{
private SaleVoucherBO billInfo;
private Dictionary<BillItemKey, SalesBillItemBO> bill = new Dictionary<BillItemKey, SalesBillItemBO>();
Guid? newBillID;
ISaleForm saleForm;
private CustomerBO customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5"));
public void InitGui(ISaleForm saleForm)
{
this.saleForm = saleForm;
this.saleForm.SetCustomerDisplay(customer.Name);
this.saleForm.SetUserName(Session.User.Name);
}
private void InitComponents()
{
InitModel();
InitView();
}
private void InitView()
{
throw new NotImplementedException();
}
private void InitModel()
{
throw new NotImplementedException();
}
public void AddProductToGrid(Guid productID, BindingSource bindingSource)
{
BillHelperFunctions.AddProductToGrid(productID, bindingSource, bill);
ShowAmount();
}
public void ShowCustomerList(bool reset)
{
if ((customer.CustomerID == new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5")) && (!reset))
{
using (SelectCustomer selectCustomer = new SelectCustomer(new CustomerBI().GetFilteredCustomers, true))
{
selectCustomer.customerEvent += new CustomerEventHandler(selectCustomer_customerEvent);
selectCustomer.ShowDialog();
if (selectCustomer.SelectedItem != null)
{
customer = selectCustomer.SelectedItem;
saleForm.SetCustomerDisplay(customer.Name);
}
else
{
customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5"));
}
}
}
else
{
customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5"));
saleForm.SetCustomerDisplay(customer.Name);
}
}
#region Save
// if (btnWaiter.Tag == null)
//btnWaiter.Tag = new WaiterBI().GetWaiters()[0].WaiterID;
public void Save(bool print, Guid waiterID, string tableID)
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_EDIT_BILL))
{
if ((billInfo != null) && (new SaleVoucherBI().IsBillPrinted(billInfo.VoucherID)) && (!roleBI.IsAllowed))
{
throw new PermissionException("You do not have the permission to reprint a bill.");
}
}
if (bill.Count != 0)
{
Guid? saved;
if (billInfo == null)
saved = AddNewSale(print, waiterID, tableID);
else
saved = UpdateSale(print, waiterID, tableID);
if (saved.HasValue)
{
if (newBillID.HasValue)
saleForm.CloseWindow();
else
PrintBill(print, saved.Value);
}
ClearBill();
}
}
private Guid? AddNewSale(bool finalBill, Guid waiterID, string tableID)
{
if (billInfo != null)
{
MessageBox.Show("Error in AddNewSale, there is a previous sale in memory", "Error");
return null;
}
#region SaleVoucher
UserBO user = Session.User;
SaleVoucherBO saleVoucher = new SaleVoucherBO
{
CustomerID = customer.CustomerID,
Paid = false,
//Paid = finalBill,
TableID = tableID,
WaiterID = waiterID,
Printed = finalBill,
Void = false,
Date = DateTime.Now,
Narration = "",
Ref = "",
Type = 'S',
UserID = user.UserID
};
#endregion
#region Inventories
List<InventoryBO> iList = new SaleVoucherBI().SaleInventory(bill.Values, null);
#endregion
new SaleVoucherBI().Insert(saleVoucher, iList);
return saleVoucher.VoucherID;
}
private Guid? UpdateSale(bool finalBill, Guid waiterID, string tableID)
{
UserBO user = Session.User;
#region Voucher and SaleVoucher
SaleVoucherBO saleVoucher = new SaleVoucherBO
{
VoucherID = billInfo.VoucherID,
timestamp = (byte[])billInfo.timestamp,
UserID = billInfo.UserID,
Date = billInfo.Date,
CreationDate = DateTime.Now,
LastEditDate = DateTime.Now,
Narration = billInfo.Narration,
Ref = "",
Type = 'S',
Alarm = billInfo.Alarm,
BillID = billInfo.BillID,
CustomerID = customer.CustomerID,
KotID = billInfo.KotID,
Paid = billInfo.Paid,
Printed = billInfo.Printed || finalBill,
TableID = tableID,
Void = billInfo.Void,
VoidReason = billInfo.VoidReason,
WaiterID = waiterID,
SaleTimestamp = (byte[])billInfo.SaleTimestamp
};
if ((!billInfo.Printed) && finalBill)
saleVoucher.Date = null;
#endregion
#region Inventory
List<InventoryBO> iList = new SaleVoucherBI().SaleInventory(bill.Values, billInfo.VoucherID);
#endregion
new SaleVoucherBI().Update(saleVoucher, iList);
return saleVoucher.VoucherID;
}
#endregion
public void VoidBill()
{
if (billInfo != null)
{
if (billInfo.Printed)
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_VOID_BILL))
{
if (roleBI.IsAllowed)
{
if (MessageBox.Show("Are you sure you want to void this bill?", "Void Bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
SelectVoidReason voidReason = new SelectVoidReason(GetVoidReason, true);
voidReason.ShowDialog();
if (voidReason.SelectedItem != null)
{
new SaleVoucherBI().VoidBill(billInfo.VoucherID, voidReason.SelectedItem.Description);
ClearBill();
}
else
{
MessageBox.Show("Please Select a reason if you want to void the bill", "Bill NOT Voided", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
else
throw new PermissionException("You do not have the permission to void a bill.");
}
}
}
}
CustomerBO selectCustomer_customerEvent(object sender, CustomerEventArgs e)
{
using (CustomersForm form = new CustomersForm(e.CustomerID, e.Phone))
{
form.ShowDialog();
return form.Customer;
}
}
private void PrintBill(bool finalBill, Guid voucherID)
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_PRINT_KOT))
if (!roleBI.IsAllowed)
throw new PermissionException("You are not allowed to print KOT");
if (!finalBill)
Accounts.Print.Thermal.PrintWaiterKot("KOT", voucherID, bill.Values.ToList());
else
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_PRINT_BILL))
{
if (roleBI.IsAllowed)
{
Accounts.Print.Thermal.PrintBill(Session.printer(), true, voucherID, bill.Values.ToList());
Accounts.Print.Thermal.PrintCustomerKot("KOT", voucherID, bill.Values.ToList());
}
else
throw new PermissionException("You are not allowed to print KOT");
}
}
}
private List<StringType> GetVoidReason(Dictionary<string, string> filter)
{
List<StringType> list = new List<StringType>();
list.Add(new StringType("Discount"));
list.Add(new StringType("Printing Fault"));
list.Add(new StringType("Item Changed"));
list.Add(new StringType("Quantity Reduced"));
list.Add(new StringType("Costing Bill for Party"));
list.Add(new StringType("Cashier Mistake"));
list.Add(new StringType("Management Freesale"));
list.Add(new StringType("Other"));
return list.Where(i => i.Description.ToLower().Contains(filter["Name"].ToLower().Trim())).ToList();
}
public void SetNewBillID(Guid voucherID)
{
this.newBillID = voucherID;
}
public SalesBillItemBO CurrentProduct(int index)
{
if ((index != -1) && (index < bill.Count))
return bill.ElementAt(index).Value;
else
return null;
//if (dgvProducts.Rows.Count == 0)
// return null;
//SalesBillItemBO product = bill.ElementAt(dgvProducts.CurrentRow.Index).Value;
//return product;
}
private void ShowAmount()
{
decimal grossTax = bill.Values.Sum(b => b.TaxAmount);
decimal discount = bill.Values.Sum(b => b.DiscountAmount);
decimal grossAmount = bill.Values.Sum(b => b.GrossAmount);
decimal amount = bill.Values.Sum(b => b.Value);
//bill.Values.ToList();
saleForm.ShowAmount(grossTax, discount, grossAmount, amount, bill.Values.ToList());
}
public void SetDiscount(SalesBillItemBO product, decimal discount)
{
if (product == null)
return;
BillHelperFunctions.SetDiscount(product.productID, discount, customer, bill);
ShowAmount();
}
public void SetAmount(SalesBillItemBO product, BindingSource bindingSource, decimal amount)
{
if (product == null)
return;
BillHelperFunctions.SetAmount(product, amount, bindingSource, bill);
ShowAmount();
}
public bool ProductRemove(SalesBillItemBO product)
{
if (product == null)
return false;
if (product.Printed > 0)
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_EDIT_PRINTED_PRODUCT))
{
if (roleBI.IsAllowed)
{
if (MessageBox.Show(string.Format("Already {0} items have been printed.\n\rAre you sure you want to delete this item?", product.Printed), "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return false;
//If BillList(Location).Printed <> 0 Then ItemsDeleted.Add("Deleted " & BillList(Location).Printed & " " & BillList(Location).Name & " from Bill No " & mBillNo)
bill.Remove(new BillItemKey(product.productID, product.Printed == 0));
ShowAmount();
}
else
throw new PermissionException("You are not allowed to delete already printed products");
return true;
}
}
return false;
}
public void SetQuantity(SalesBillItemBO product, BindingSource bindingSource, decimal quantity, bool absolute, bool prompt)
{
if (product == null)
return;
BillHelperFunctions.SetQuantity(product, quantity, absolute, prompt, bindingSource, bill);
ShowAmount();
}
public void ChangeRate(int index)
{
using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_CHANGE_RATE))
{
if (roleBI.IsAllowed)
{
SalesBillItemBO product = bill.ElementAt(index).Value;
decimal rate = 0;
InputBoxResult result = InputBox.Show("Enter Rate", "Rate", product.Price.ToString(), InputBox_Validating);
if (result.OK)
rate = Convert.ToDecimal(result.Text);
if (rate != 0)
{
BillHelperFunctions.SetRate(product.productID, rate, bill);
ShowAmount();
}
}
else
throw new PermissionException("You do not have permission to Change the Rate");
}
}
private void InputBox_Validating(object sender, InputBoxValidatingArgs e)
{
}
private void LoadBill(Guid voucherID)
{
ClearBill();
List<InventoryDisplayBO> iList = new List<InventoryDisplayBO>();
new SaleVoucherBI().GetSaleVoucher(voucherID, ref billInfo, ref iList);
this.customer = new CustomerBI().GetCustomer(billInfo.CustomerID);
saleForm.ShowInfo(billInfo.BillID, billInfo.KotID, billInfo.CreationDate, billInfo.Date.Value, billInfo.LastEditDate, customer.Name, billInfo.TableID, billInfo.WaiterID, new WaiterBI().GetWaiter(billInfo.WaiterID).Name);
foreach (InventoryDisplayBO inventory in iList)
{
if (inventory.ComplexProductID.HasValue)
{
BillItemKey key = new BillItemKey(inventory.ComplexProductID.Value, inventory.Quantity == 0);
if (!bill.ContainsKey(key))
{
decimal rate = 0, quantity = 0;
string name = "";
new SaleVoucherBI().GetComplexBillInformation(voucherID, inventory.ComplexProductID.Value, ref rate, ref quantity, ref name);
bill.Add(key, new SalesBillItemBO
{
productID = inventory.ComplexProductID.Value,
Discount = inventory.Discount,
Name = name,
Price = rate,
Printed = quantity,
Quantity = quantity,
Tax = inventory.Tax,
});
}
}
else
{
BillItemKey key = new BillItemKey(inventory.ProductID, inventory.Quantity == 0);
bill.Add(key, new SalesBillItemBO
{
productID = inventory.ProductID,
Discount = inventory.Discount,
Name = inventory.ProductName,
Price = inventory.Rate,
Printed = inventory.Quantity,
Quantity = inventory.Quantity,
Tax = inventory.Tax,
});
}
}
ShowAmount();
}
public void LoadBillFromTable()
{
InputBoxResult result = InputBox.Show("Enter Table Number", "Table", "0", InputBox_Validating);
if (result.OK)
{
string tableID = result.Text.Trim();
if ((tableID != "C") && (tableID != "") && (!tableID.Contains(".")))
{
Guid? tID = new SaleVoucherBI().GetPendingVoucherID(tableID);
if (tID.HasValue)
{
LoadBill(tID.Value);
}
}
else
ClearBill();
}
}
public void CancelBillChanges()
{
if (bill.Count != 0)
if (MessageBox.Show("Cancel current bill?", "Cancel bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
return;
ClearBill();
}
public void ClearBill()
{
billInfo = null;
ShowCustomerList(true);
bill.Clear();
saleForm.ClearBill(bill);
}
public void FormLoad()
{
if (newBillID.HasValue)
{
LoadBill(newBillID.Value);
}
}
}
}
// How to load a bill
//LoadBill(((PendingBillsBO)bsPending.Current).voucherID);
// ChangeFormState(SaleFormState.Billing);