2011-08-23 07:10:05 +00:00
using System.Collections.Generic ;
2011-01-10 19:49:11 +00:00
using System.Linq ;
using System.Windows.Forms ;
2011-03-11 18:49:48 +00:00
using Tanshu.Accounts.Contracts ;
2011-01-30 07:14:05 +00:00
using Tanshu.Accounts.Entities ;
2011-03-11 18:49:48 +00:00
using Tanshu.Accounts.Helpers ;
using Tanshu.Accounts.Print ;
using Tanshu.Accounts.Repository ;
using Tanshu.Common ;
2014-10-12 09:41:45 +00:00
using System ;
2011-01-10 19:49:11 +00:00
namespace Tanshu.Accounts.PointOfSale
{
public class BillController
{
2012-12-01 09:49:33 +00:00
private readonly BillDict _bill ;
2011-03-11 18:49:48 +00:00
private Voucher _billInfo ;
2014-10-12 09:41:45 +00:00
private Guid ? _editVoucherID ;
2011-06-23 12:47:48 +00:00
private readonly bool _print ;
2011-03-11 18:49:48 +00:00
private ISaleForm _saleForm ;
2011-01-10 19:49:11 +00:00
2011-12-05 09:41:02 +00:00
public Waiter Waiter
{
get { return _billInfo . Waiter ; }
set { _billInfo . Waiter = value ; }
}
2014-10-12 09:41:45 +00:00
public BillController ( Guid ? editVoucherID , bool print )
2011-02-09 12:03:22 +00:00
{
2011-03-11 18:49:48 +00:00
this . _editVoucherID = editVoucherID ;
2011-06-23 12:47:48 +00:00
_print = print ;
2012-12-01 09:49:33 +00:00
_bill = new BillDict ( ) ;
2011-12-05 09:41:02 +00:00
_billInfo = new Voucher ( Session . User ) ;
2014-10-12 09:41:45 +00:00
using ( var bi = new CustomerBI ( ) )
_billInfo . Customer = bi . Get ( x = > x . CustomerID = = Constants . CASH_CUSTOMER ) ;
using ( var bi = new WaiterBI ( ) )
_billInfo . Waiter = bi . Get ( x = > x . WaiterID = = Constants . WAITER ) ;
2011-02-09 12:03:22 +00:00
}
2011-06-23 12:47:48 +00:00
public BillItemValue CurrentProduct
2011-03-11 18:49:48 +00:00
{
get
{
if ( _saleForm . BindingSource . Position = = - 1 )
return null ;
2011-06-23 12:47:48 +00:00
var item = _bill . ElementAt ( _saleForm . BindingSource . Position ) ;
return item . Key . BillItemType = = BillItemType . Product ? item . Value : null ;
2011-03-11 18:49:48 +00:00
}
}
2011-06-23 12:47:48 +00:00
public BillItemKey CurrentKot
2011-01-10 19:49:11 +00:00
{
2011-03-11 18:49:48 +00:00
get
{
if ( _saleForm . BindingSource . Position = = - 1 )
return null ;
2011-06-23 12:47:48 +00:00
var item = _bill . ElementAt ( _saleForm . BindingSource . Position ) ;
return item . Key . BillItemType = = BillItemType . Kot ? item . Key : null ;
2011-03-11 18:49:48 +00:00
}
}
2011-01-10 19:49:11 +00:00
2011-03-11 18:49:48 +00:00
public void InitGui ( ISaleForm saleForm )
{
this . _saleForm = saleForm ;
2011-12-05 09:41:02 +00:00
this . _saleForm . SetCustomerDisplay ( _billInfo . Customer . Name ) ;
2011-03-11 18:49:48 +00:00
this . _saleForm . SetUserName ( Session . User . Name ) ;
2011-01-10 19:49:11 +00:00
}
2011-08-23 07:10:05 +00:00
public void AddProduct ( Product product )
2011-06-23 12:47:48 +00:00
{
2014-10-12 09:41:45 +00:00
var newKey = new BillItemKey ( product . ProductID , Guid . Empty ) ;
2011-06-23 12:47:48 +00:00
if ( _bill . ContainsKey ( newKey ) )
{
_saleForm . BindingSource . CurrencyManager . Position = _bill . IndexOfKey ( newKey ) ;
_bill [ newKey ] . Quantity + = 1 ;
}
else
{
2011-08-23 07:10:05 +00:00
var billItemValue = new BillItemValue ( product ) ;
2012-12-01 09:49:33 +00:00
var old = _bill . FirstOrDefault ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . ProductID = = newKey . ProductID ) ;
2011-06-23 12:47:48 +00:00
if ( old . Key ! = null )
{
billItemValue . Discount = old . Value . Discount ;
billItemValue . Price = old . Value . Price ;
}
_bill . Add ( newKey , billItemValue ) ;
_saleForm . BindingSource . DataSource = _bill . Values . ToList ( ) ;
_saleForm . BindingSource . CurrencyManager . Position = _saleForm . BindingSource . CurrencyManager . Count - 1 ;
2011-08-23 07:10:05 +00:00
2014-10-12 09:41:45 +00:00
using ( var bi = new ProductGroupModifierBI ( ) )
2011-08-23 07:10:05 +00:00
if ( bi . HasCompulsoryModifier ( product . ProductGroup . ProductGroupID ) )
{
var item = CurrentProduct ;
ShowModifiers ( product . ProductGroup . ProductGroupID , item ) ;
}
2011-06-23 12:47:48 +00:00
}
2011-08-23 07:10:05 +00:00
ShowAmount ( ) ;
2011-06-23 12:47:48 +00:00
}
2014-10-12 09:41:45 +00:00
public void ShowModifiers ( Guid productGroupID , BillItemValue item )
2011-01-30 07:14:05 +00:00
{
2011-02-18 16:54:48 +00:00
if ( item . Printed )
2011-01-31 20:33:22 +00:00
return ;
2014-10-12 09:41:45 +00:00
using ( var bi = new ProductGroupModifierBI ( ) )
2011-01-30 07:14:05 +00:00
{
2011-06-29 20:27:07 +00:00
using ( var frm = new ModifierForm ( bi . List ( productGroupID ) , item . Modifiers ) )
{
frm . ShowDialog ( ) ;
item . Modifiers = frm . Selection ;
}
2011-01-30 07:14:05 +00:00
}
2011-01-31 20:33:22 +00:00
ShowAmount ( ) ;
}
2011-12-05 09:41:02 +00:00
public void SetDiscount ( )
2011-01-31 20:33:22 +00:00
{
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Discount" ) )
2014-10-12 09:41:45 +00:00
throw new PermissionException ( "Not Allowed to give Discount" ) ;
2011-02-09 12:03:22 +00:00
2011-06-23 12:47:48 +00:00
using ( var bi = new ProductGroupBI ( ) )
2011-01-31 20:33:22 +00:00
{
2011-06-23 12:47:48 +00:00
using ( var frm = new DiscountForm ( bi . GetProductGroupTypes ( ) ) )
2011-01-31 20:33:22 +00:00
{
2011-06-23 12:47:48 +00:00
if ( frm . ShowDialog ( ) = = DialogResult . OK )
2011-01-31 20:33:22 +00:00
{
2011-06-23 12:47:48 +00:00
HashSet < string > outList ;
var discount = frm . Selection ( out outList ) ;
discount = discount / 100 ;
if ( discount > 1 | | discount < 0 )
2014-10-12 09:41:45 +00:00
throw new ValidationException ( "Invalid Discount Amount" ) ;
2011-06-23 12:47:48 +00:00
foreach ( var item in _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & outList . Contains ( x . Value . Product . ProductGroup . GroupType ) ) )
{
var product = item . Value . Product ;
var maxDiscount = product . ProductGroup . DiscountLimit ;
if ( discount > item . Value . Product . ProductGroup . DiscountLimit )
MessageBox . Show ( string . Format ( "Maximum discount for {0} is {1:P}" , product . Name , maxDiscount ) ,
"Excessive Discount" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
item . Value . Discount = discount ;
}
2011-01-31 20:33:22 +00:00
}
}
}
ShowAmount ( ) ;
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
public void SetQuantity ( decimal quantity , bool prompt )
{
var item = CurrentProduct ;
if ( ! Allowed ( item ) )
return ;
if ( item . Printed )
return ;
if ( prompt & & ! GetInput ( "Quantity" , ref quantity ) )
return ;
if ( ! prompt )
quantity + = item . Quantity ;
2014-10-16 11:11:55 +00:00
if ( quantity < 0 & & ! Session . IsAllowed ( "Edit Printed Product" ) )
2011-12-05 09:41:02 +00:00
return ;
var total = quantity + _bill . Where ( x = > x . Key . ProductID = = item . ProductID & & x . Key . BillItemType = = BillItemType . Product & & x . Value . Printed ) . Sum ( x = > x . Value . Quantity ) ;
if ( total < 0 )
quantity - = total ;
item . Quantity = quantity ;
ShowAmount ( ) ;
}
public void SetRate ( )
2011-01-10 19:49:11 +00:00
{
2011-12-05 09:41:02 +00:00
var item = CurrentProduct ;
2014-10-12 09:41:45 +00:00
if ( item = = null )
throw new ValidationException ( "No Product Selected" ) ;
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Change Rate" ) )
2014-10-12 09:41:45 +00:00
throw new PermissionException ( "Rate Change not Allowed" ) ;
2011-12-05 09:41:02 +00:00
var rate = item . Price ;
if ( ! GetInput ( "Price" , ref rate ) )
return ;
2014-10-16 11:11:55 +00:00
if ( rate = = 0 & & ! Session . IsAllowed ( "NC Product" ) )
throw new PermissionException ( "NC of Product is not Allowed" ) ;
2011-12-05 09:41:02 +00:00
foreach ( var sub in _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . ProductID = = item . ProductID ) )
sub . Value . Price = rate ;
ShowAmount ( ) ;
}
public void ShowCustomers ( bool reset )
{
2014-10-12 09:41:45 +00:00
if ( ! reset & & ( ( _billInfo . Customer = = null ) | | _billInfo . Customer . CustomerID = = Constants . CASH_CUSTOMER ) )
2011-01-10 19:49:11 +00:00
{
2014-10-12 09:41:45 +00:00
using ( var selectCustomer = new SelectCustomer ( CustomerBI . StaticList , true ) )
2011-01-10 19:49:11 +00:00
{
2011-12-05 09:41:02 +00:00
selectCustomer . CustomerEvent + = selectCustomer_customerEvent ;
2011-01-10 19:49:11 +00:00
selectCustomer . ShowDialog ( ) ;
if ( selectCustomer . SelectedItem ! = null )
{
2011-12-05 09:41:02 +00:00
_billInfo . Customer = selectCustomer . SelectedItem ;
2011-01-10 19:49:11 +00:00
}
else
{
2014-10-12 09:41:45 +00:00
using ( var bi = new CustomerBI ( ) )
_billInfo . Customer = bi . Get ( x = > x . CustomerID = = Constants . CASH_CUSTOMER ) ;
2011-01-10 19:49:11 +00:00
}
}
}
else
{
2014-10-12 09:41:45 +00:00
using ( var bi = new CustomerBI ( ) )
_billInfo . Customer = bi . Get ( x = > x . CustomerID = = Constants . CASH_CUSTOMER ) ;
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
_saleForm . SetCustomerDisplay ( _billInfo . Customer . Name ) ;
2011-01-10 19:49:11 +00:00
}
2011-08-23 07:10:05 +00:00
private Customer selectCustomer_customerEvent ( object sender , CustomerEventArgs e )
{
using ( var form = new CustomersForm ( e . CustomerID , e . Phone ) )
{
form . ShowDialog ( ) ;
return form . Customer ;
}
}
2011-12-05 09:41:02 +00:00
public void ShowWaiters ( bool reset )
2011-01-10 19:49:11 +00:00
{
2011-12-05 09:41:02 +00:00
if ( reset )
2011-01-10 19:49:11 +00:00
{
2014-10-12 09:41:45 +00:00
using ( var bi = new WaiterBI ( ) )
_billInfo . Waiter = bi . Get ( x = > x . WaiterID = = Constants . WAITER ) ;
2011-12-05 09:41:02 +00:00
}
else
{
2014-10-12 09:41:45 +00:00
using ( var selectWaiter = new SelectWaiter ( WaiterBI . StaticList , true ) )
2011-06-29 20:27:07 +00:00
{
2011-12-05 09:41:02 +00:00
selectWaiter . WaiterEvent + = selectWaiter_waiterEvent ;
selectWaiter . ShowDialog ( ) ;
if ( selectWaiter . SelectedItem ! = null )
2011-06-29 20:27:07 +00:00
{
2011-12-05 09:41:02 +00:00
_billInfo . Waiter = selectWaiter . SelectedItem ;
}
else
{
2014-10-12 09:41:45 +00:00
using ( var bi = new WaiterBI ( ) )
_billInfo . Waiter = bi . Get ( x = > x . WaiterID = = Constants . WAITER ) ;
2011-06-29 20:27:07 +00:00
}
}
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
_saleForm . SetWaiterDisplay ( _billInfo . Waiter . Name ) ;
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
private Waiter selectWaiter_waiterEvent ( object sender , SelectorEventArgs < Waiter > e )
2011-03-11 18:49:48 +00:00
{
2011-12-05 09:41:02 +00:00
var waiter = e . Item ;
//if (!Thread.CurrentPrincipal.IsInRole("Waiter/Master"))
// return waiter;
using ( var bi = new WaiterBI ( ) )
{
switch ( e . Action )
{
case SelectorAction . Insert : // Add
bi . Insert ( waiter ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
2011-12-05 09:41:02 +00:00
e . Handled = true ;
return waiter ;
case SelectorAction . Update : // Edit
bi . Update ( waiter ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
2011-12-05 09:41:02 +00:00
e . Handled = true ;
return waiter ;
case SelectorAction . Delete : // Delete
bi . Delete ( x = > x . WaiterID = = waiter . WaiterID ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
2011-12-05 09:41:02 +00:00
e . Handled = true ;
2014-10-12 09:41:45 +00:00
return bi . Get ( x = > x . WaiterID = = Constants . WAITER ) ;
2011-12-05 09:41:02 +00:00
default :
e . Handled = true ;
2014-10-12 09:41:45 +00:00
return bi . Get ( x = > x . WaiterID = = Constants . WAITER ) ;
2011-12-05 09:41:02 +00:00
}
}
2011-01-10 19:49:11 +00:00
}
private void ShowAmount ( )
{
2012-04-08 12:28:15 +00:00
var taxAmount = _bill . Values . Sum ( b = > b . ServiceTaxAmount + b . VatAmount ) ;
2011-06-23 12:47:48 +00:00
var discountAmount = _bill . Values . Sum ( b = > b . DiscountAmount ) ;
var grossAmount = _bill . Values . Sum ( b = > b . GrossAmount ) ;
var valueAmount = _bill . Values . Sum ( b = > b . Value ) ;
var serviceChargeAmount = _bill . Values . Sum ( b = > b . ServiceChargeAmount ) ;
2011-03-11 18:49:48 +00:00
_saleForm . ShowAmount ( discountAmount , grossAmount , serviceChargeAmount , taxAmount , valueAmount ,
2011-06-23 12:47:48 +00:00
_bill . Values . ToList ( ) ) ;
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
private static bool Allowed ( BillItemValue item )
2011-01-10 19:49:11 +00:00
{
2011-12-05 09:41:02 +00:00
return item ! = null ;
}
private static void IsPrintedOrVoid ( Voucher voucher , out bool isPrinted , out bool isVoid )
{
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-12-05 09:41:02 +00:00
{
var dbVoucher = bi . Get ( x = > x . VoucherID = = voucher . VoucherID ) ;
isPrinted = dbVoucher . Printed ;
isVoid = dbVoucher . Void ;
}
2011-01-10 19:49:11 +00:00
}
2011-12-05 09:41:02 +00:00
private static bool IsPrintedOrVoid ( Voucher voucher )
2011-02-09 12:03:22 +00:00
{
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-12-05 09:41:02 +00:00
{
var dbVoucher = bi . Get ( x = > x . VoucherID = = voucher . VoucherID ) ;
return dbVoucher . Printed | | dbVoucher . Void ;
}
2011-02-09 12:03:22 +00:00
}
2014-10-12 09:41:45 +00:00
private static bool IsPrintedOrVoid ( Guid voucherID )
2011-03-11 18:49:48 +00:00
{
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-12-05 09:41:02 +00:00
{
var dbVoucher = bi . Get ( x = > x . VoucherID = = voucherID ) ;
return dbVoucher . Printed | | dbVoucher . Void ;
}
}
private bool? IsReprint ( out decimal amount )
{
amount = 0 ;
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID = = Guid . Empty )
2011-12-05 09:41:02 +00:00
return false ;
bool isPrinted , isVoid ;
IsPrintedOrVoid ( _billInfo , out isPrinted , out isVoid ) ;
if ( isVoid )
{
MessageBox . Show ( string . Format ( "This Bill is already void.\nReason: {0}" , _billInfo . VoidReason ) , "Bill already Voided" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return null ;
}
2014-10-16 11:11:55 +00:00
if ( isPrinted & & ( ! Session . IsAllowed ( "Edit Printed Bill" ) ) )
2011-12-05 09:41:02 +00:00
return null ;
2014-10-12 09:41:45 +00:00
amount = _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID ! = Guid . Empty ) . Sum ( x = > x . Value . GrossAmount ) ;
2011-12-05 09:41:02 +00:00
return isPrinted ;
2011-03-11 18:49:48 +00:00
}
2011-01-10 19:49:11 +00:00
private void InputBox_Validating ( object sender , InputBoxValidatingArgs e )
{
}
2014-10-12 09:41:45 +00:00
private void LoadBill ( Guid voucherID )
2011-01-10 19:49:11 +00:00
{
ClearBill ( ) ;
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-08-23 07:10:05 +00:00
{
2011-12-05 09:41:02 +00:00
_billInfo = bi . Get ( x = > x . VoucherID = = voucherID ) ;
_bill . Clear ( ) ;
_saleForm . ShowInfo ( _billInfo ) ;
2014-10-12 09:41:45 +00:00
_bill . Load ( _billInfo ) ;
var newKotKey = new BillItemKey ( Guid . Empty ) ;
2011-08-23 07:10:05 +00:00
var newKotItem = new BillItemValue ( ) ;
_bill . Add ( newKotKey , newKotItem ) ;
ShowAmount ( ) ;
_saleForm . FormState = SaleFormState . Billing ;
2011-01-10 19:49:11 +00:00
}
}
2011-08-23 07:10:05 +00:00
public void LoadBill ( string tableName )
2011-01-10 19:49:11 +00:00
{
2014-10-12 09:41:45 +00:00
FoodTable table ;
using ( var bi = new FoodTableBI ( ) )
table = bi . Get ( x = > x . Name = = tableName ) ;
if ( table ! = null & & table . VoucherID . HasValue )
2011-01-10 19:49:11 +00:00
{
2014-10-12 09:41:45 +00:00
LoadBill ( table . VoucherID . Value ) ;
2011-01-13 20:21:02 +00:00
}
else
{
2014-10-12 09:41:45 +00:00
using ( var frm = new PaxForm ( ) )
{
frm . ShowDialog ( ) ;
2014-10-16 11:11:55 +00:00
_billInfo . Table = table ;
2014-10-12 09:41:45 +00:00
_billInfo . Pax = frm . Pax ;
_saleForm . ShowInfo ( _billInfo ) ;
}
2011-01-10 19:49:11 +00:00
}
}
public void CancelBillChanges ( )
{
2011-06-23 12:47:48 +00:00
if ( _bill . Values . Any ( i = > i . Printed = = false ) & &
MessageBox . Show ( "Abandon Changes?" , "Abandon Changes" , MessageBoxButtons . YesNo ,
MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) = = DialogResult . No )
return ;
2011-01-10 19:49:11 +00:00
ClearBill ( ) ;
}
public void ClearBill ( )
{
2011-12-05 09:41:02 +00:00
_billInfo = new Voucher ( Session . User ) ;
ShowCustomers ( true ) ;
ShowWaiters ( true ) ;
2011-06-23 12:47:48 +00:00
_bill . Clear ( ) ;
2014-10-12 09:41:45 +00:00
var newKotKey = new BillItemKey ( Guid . Empty ) ;
2011-08-23 07:10:05 +00:00
var newKotItem = new BillItemValue ( ) ;
2011-06-23 12:47:48 +00:00
_bill . Add ( newKotKey , newKotItem ) ;
2011-08-23 07:10:05 +00:00
_saleForm . ClearBill ( _bill . Values . ToList ( ) ) ;
2011-01-10 19:49:11 +00:00
}
2011-07-12 07:00:48 +00:00
public SaleFormState FormLoad ( )
2011-01-10 19:49:11 +00:00
{
2011-02-18 16:54:48 +00:00
ClearBill ( ) ;
2011-03-11 18:49:48 +00:00
if ( _editVoucherID . HasValue )
2011-01-10 19:49:11 +00:00
{
2011-03-11 18:49:48 +00:00
LoadBill ( _editVoucherID . Value ) ;
2011-07-12 07:00:48 +00:00
return SaleFormState . Billing ;
2011-01-10 19:49:11 +00:00
}
2011-07-12 07:00:48 +00:00
return SaleFormState . Waiting ;
2011-01-10 19:49:11 +00:00
}
2011-01-17 14:55:43 +00:00
internal void SettleBill ( )
{
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID = = Guid . Empty )
2011-01-17 14:55:43 +00:00
return ;
2011-03-11 18:49:48 +00:00
if ( ! _billInfo . Printed )
2011-01-31 20:33:22 +00:00
return ;
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Settle Bill" ) )
2011-02-09 12:03:22 +00:00
return ;
2011-03-11 18:49:48 +00:00
IDictionary < SettleOption , decimal > options ;
2014-10-12 09:41:45 +00:00
var amount = - 1 * _billInfo . Kots . Sum ( x = > x . Inventories . Sum ( y = > y . Amount ) ) ;
VoucherSettlementBI . UpdateSettlements ( _billInfo . Settlements , amount ) ;
using ( var frm = new SettleChoicesForm ( Math . Round ( amount ) * - 1 , _billInfo . VoucherType ) )
2011-01-17 14:55:43 +00:00
{
frm . ShowDialog ( ) ;
2011-03-11 18:49:48 +00:00
options = frm . OptionsChosen ;
2011-01-17 14:55:43 +00:00
}
2011-03-11 18:49:48 +00:00
if ( options . Count = = 0 )
return ;
2011-06-29 20:27:07 +00:00
using ( var bi = new VoucherSettlementBI ( ) )
2014-10-12 09:41:45 +00:00
{
2011-06-29 20:27:07 +00:00
bi . SettleVoucher ( Session . User , _billInfo . VoucherID , options ) ;
2014-10-16 11:11:55 +00:00
bi . SaveChanges ( ) ;
2014-10-12 09:41:45 +00:00
}
2011-03-11 18:49:48 +00:00
ClearBill ( ) ;
2011-01-17 14:55:43 +00:00
}
2011-02-18 16:54:48 +00:00
2011-06-23 12:47:48 +00:00
#region Move Table ( s ) / Kot ( s )
2011-03-11 18:49:48 +00:00
private static FoodTable GetTableForMove ( bool allowMerge )
2011-02-18 16:54:48 +00:00
{
2011-06-23 12:47:48 +00:00
using ( var bi = new FoodTableBI ( ) )
2011-02-18 16:54:48 +00:00
{
2014-10-16 11:11:55 +00:00
using ( var frm = new MoveTableForm ( bi . List ( x = > x . IsActive ) , allowMerge ) )
2011-06-23 12:47:48 +00:00
{
frm . ShowDialog ( ) ;
if ( frm . Selection ! = null )
return frm . Selection ;
}
return null ;
2011-02-18 16:54:48 +00:00
}
}
2011-12-05 09:41:02 +00:00
internal void MoveKot ( )
2011-02-18 16:54:48 +00:00
{
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID = = Guid . Empty )
2011-12-05 09:41:02 +00:00
return ;
if ( IsPrintedOrVoid ( _billInfo ) )
2011-02-18 16:54:48 +00:00
return ;
var kot = CurrentKot ;
if ( kot = = null )
return ;
var table = GetTableForMove ( true ) ;
if ( table = = null )
return ;
2014-10-16 11:11:55 +00:00
if ( table . FoodTableID = = _billInfo . Table . FoodTableID )
2011-08-23 07:10:05 +00:00
return ;
2014-10-12 09:41:45 +00:00
if ( table . VoucherID ! = null )
if ( IsPrintedOrVoid ( table . VoucherID . Value ) )
2011-12-05 09:41:02 +00:00
return ;
2014-10-12 09:41:45 +00:00
var kotCount = _bill . Keys . Count ( x = > x . BillItemType = = BillItemType . Kot & & x . KotID ! = Guid . Empty ) ;
var voucherID = Guid . Empty ;
if ( table . VoucherID = = null & & kotCount > 1 )
2011-02-18 16:54:48 +00:00
//Move Kot
voucherID = MoveKot ( kot , table ) ;
2014-10-12 09:41:45 +00:00
else if ( table . VoucherID ! = null & & kotCount > 1 )
2011-02-18 16:54:48 +00:00
//Merge Kot
voucherID = MergeKot ( kot , table ) ;
2014-10-12 09:41:45 +00:00
else if ( table . VoucherID = = null & & kotCount = = 1 )
2011-12-05 09:41:02 +00:00
//Move Table
2014-10-16 11:11:55 +00:00
voucherID = MoveTable ( table . FoodTableID ) ;
2014-10-12 09:41:45 +00:00
else if ( table . VoucherID ! = null & & kotCount = = 1 )
2011-02-18 16:54:48 +00:00
//Merge Table
voucherID = MergeTable ( table ) ;
2014-10-12 09:41:45 +00:00
if ( voucherID ! = Guid . Empty )
2011-02-18 16:54:48 +00:00
LoadBill ( voucherID ) ;
}
2011-06-23 12:47:48 +00:00
internal void MoveTable ( )
{
2014-10-16 11:11:55 +00:00
if ( _billInfo . VoucherID = = Guid . Empty )
2011-06-23 12:47:48 +00:00
return ;
2011-12-05 09:41:02 +00:00
var allowMerge = ! IsPrintedOrVoid ( _billInfo ) ;
var table = GetTableForMove ( allowMerge ) ;
2011-06-23 12:47:48 +00:00
if ( table = = null )
return ;
2014-10-16 11:11:55 +00:00
if ( table . FoodTableID = = _billInfo . Table . FoodTableID )
2011-12-05 09:41:02 +00:00
return ;
2014-10-12 09:41:45 +00:00
if ( table . VoucherID . HasValue )
if ( IsPrintedOrVoid ( table . VoucherID . Value ) )
2011-12-05 09:41:02 +00:00
return ;
2014-10-16 11:11:55 +00:00
LoadBill ( table . VoucherID . HasValue ? MergeTable ( table ) : MoveTable ( table . FoodTableID ) ) ;
2011-06-23 12:47:48 +00:00
}
2014-10-12 09:41:45 +00:00
private Guid MoveKot ( BillItemKey kot , FoodTable table )
2011-06-23 12:47:48 +00:00
{
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Move Kot to New Table" ) )
2014-10-12 09:41:45 +00:00
return Guid . Empty ;
using ( var bi = new VoucherBI ( ) )
2011-06-23 12:47:48 +00:00
{
2014-10-16 11:11:55 +00:00
var newVoucherID = bi . MoveKot ( kot . KotID , table . FoodTableID ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
return newVoucherID ;
2011-06-29 20:27:07 +00:00
}
2011-06-23 12:47:48 +00:00
}
2014-10-12 09:41:45 +00:00
private static Guid MergeKot ( BillItemKey kot , FoodTable table )
2011-06-23 12:47:48 +00:00
{
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Merge Kots" ) )
2014-10-12 09:41:45 +00:00
return Guid . Empty ;
2011-06-29 20:27:07 +00:00
using ( var bi = new VoucherBI ( ) )
2014-10-12 09:41:45 +00:00
{
var newVoucherID = bi . MergeKot ( kot . KotID , table . Name ) ;
bi . SaveChanges ( ) ;
return newVoucherID ;
}
2011-06-23 12:47:48 +00:00
}
2014-10-16 11:11:55 +00:00
private Guid MoveTable ( Guid tableID )
2011-06-23 12:47:48 +00:00
{
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Move Table" ) )
2014-10-12 09:41:45 +00:00
return Guid . Empty ;
using ( var bi = new VoucherBI ( ) )
{
2014-10-16 11:11:55 +00:00
var newVoucherID = bi . Move ( _billInfo . Table . FoodTableID , tableID ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
return newVoucherID ;
}
2011-06-23 12:47:48 +00:00
}
2014-10-12 09:41:45 +00:00
private Guid MergeTable ( FoodTable table )
2011-06-23 12:47:48 +00:00
{
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Merge Tables" ) )
2014-10-12 09:41:45 +00:00
return Guid . Empty ;
2011-12-05 09:41:02 +00:00
using ( var bi = new VoucherBI ( ) )
2014-10-12 09:41:45 +00:00
{
var newVoucherID = bi . MergeTables ( _billInfo . VoucherID , table . Name ) ;
bi . SaveChanges ( ) ;
return newVoucherID ;
}
2011-06-23 12:47:48 +00:00
}
#endregion
2014-10-12 09:41:45 +00:00
2011-03-11 18:49:48 +00:00
#region Save
2011-12-05 09:41:02 +00:00
public void SaveKot ( )
{
#region Check if Allowed
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Print Kot" ) )
2011-12-05 09:41:02 +00:00
return ;
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID ! = Guid . Empty & & IsPrintedOrVoid ( _billInfo ) )
2011-12-05 09:41:02 +00:00
return ;
if ( _bill . Count = = 1 ) //new kot only
return ;
#endregion
//Save
2014-10-12 09:41:45 +00:00
var saved = _billInfo . VoucherID = = Guid . Empty ? InsertVoucher ( false , ! _editVoucherID . HasValue ) : UpdateVoucher ( false , ! _editVoucherID . HasValue ) ;
2011-03-11 18:49:48 +00:00
2011-12-05 09:41:02 +00:00
//Print
if ( ( ! _editVoucherID . HasValue | | _print ) & & saved . HasValue )
Thermal . PrintKot ( _billInfo . VoucherID , saved . Value ) ;
//Cleanup
if ( _editVoucherID . HasValue )
_saleForm . CloseWindow ( ) ;
else
ClearBill ( ) ;
}
public void SaveBill ( )
2011-03-11 18:49:48 +00:00
{
2011-12-05 09:41:02 +00:00
#region Check if Allowed
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Print Bill" ) )
2014-10-12 09:41:45 +00:00
throw new PermissionException ( "Printing not allowed" ) ;
2011-06-23 12:47:48 +00:00
if ( _bill . Count = = 1 ) //new kot only
return ;
2011-12-05 09:41:02 +00:00
#endregion
decimal amount ;
var isReprint = IsReprint ( out amount ) ;
if ( ! isReprint . HasValue )
2011-08-23 07:10:05 +00:00
return ;
2011-12-05 09:41:02 +00:00
else if ( isReprint . Value )
{
2014-10-12 09:41:45 +00:00
try
{
SetDiscount ( ) ;
}
catch ( Exception ex )
{
if ( ! ( ex is ValidationException ) & & ! ( ex is PermissionException ) )
throw ;
}
2011-12-05 09:41:02 +00:00
SaveReprintOrDiscountBill ( amount ) ;
}
2011-08-23 07:10:05 +00:00
else
2011-12-05 09:41:02 +00:00
{
2014-10-12 09:41:45 +00:00
try
{
SetDiscount ( ) ;
}
catch ( Exception ex )
{
if ( ! ( ex is ValidationException ) & & ! ( ex is PermissionException ) )
throw ;
}
2011-12-05 09:41:02 +00:00
// Ask for VoucherType only for new bill, if none, then cancel
VoucherType ? voucherType ;
using ( var frm = new VoucherTypeForm ( ) )
{
frm . ShowDialog ( ) ;
voucherType = frm . Selection ;
}
if ( ! voucherType . HasValue )
return ;
_billInfo . VoucherType = voucherType . Value ;
2014-10-12 09:41:45 +00:00
var saved = _billInfo . VoucherID = = Guid . Empty ? InsertVoucher ( true , ! _editVoucherID . HasValue ) : UpdateVoucher ( true , ! _editVoucherID . HasValue ) ;
2011-12-05 09:41:02 +00:00
}
2011-06-23 12:47:48 +00:00
if ( ! _editVoucherID . HasValue | | _print )
Thermal . PrintBill ( _billInfo . VoucherID ) ;
if ( _editVoucherID . HasValue )
_saleForm . CloseWindow ( ) ;
ClearBill ( ) ;
}
2011-12-05 09:41:02 +00:00
public void SplitBill ( )
{
#region Permissions
bool isPrinted , isVoid ;
IsPrintedOrVoid ( _billInfo , out isPrinted , out isVoid ) ;
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID = = Guid . Empty | | isVoid )
2011-12-05 09:41:02 +00:00
return ; // must be existing non void bill
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Split Bill" ) )
2011-12-05 09:41:02 +00:00
return ;
#endregion
#region Get Move List
HashSet < string > splitList = null ;
using ( var bi = new ProductGroupBI ( ) )
{
using ( var frm = new DiscountForm ( bi . GetProductGroupTypes ( ) ) )
if ( frm . ShowDialog ( ) = = DialogResult . OK )
frm . Selection ( out splitList ) ;
}
if ( splitList = = null | | splitList . Count = = 0 )
return ;
2014-10-12 09:41:45 +00:00
var listFirst = _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID ! = Guid . Empty & & splitList . Contains ( x . Value . Product . ProductGroup . GroupType ) ) ;
var listSecond = _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID ! = Guid . Empty & & ! splitList . Contains ( x . Value . Product . ProductGroup . GroupType ) ) ;
2011-12-05 09:41:02 +00:00
if ( listFirst . Count ( ) = = 0 | | listSecond . Count ( ) = = 0 )
return ; // all or none items selected to be moved
#endregion
var table = GetTableForMove ( false ) ;
if ( table = = null )
return ;
#region new voucherFirst
var voucherFirst = new Voucher ( Session . User )
{
Customer = _billInfo . Customer ,
2014-10-16 11:11:55 +00:00
Table = table ,
2011-12-05 09:41:02 +00:00
Waiter = _billInfo . Waiter ,
Printed = isPrinted ,
Void = false ,
Narration = "" ,
VoucherType = _billInfo . VoucherType
} ;
var kotFirst = GetKot ( listFirst ) ;
if ( kotFirst ! = null )
voucherFirst . Kots . Add ( kotFirst ) ;
#endregion
2014-10-12 09:41:45 +00:00
#region new voucherSecond
2011-12-05 09:41:02 +00:00
var voucherSecond = new Voucher ( Session . User )
{
Customer = _billInfo . Customer ,
2014-10-16 11:11:55 +00:00
Table = _billInfo . Table ,
2011-12-05 09:41:02 +00:00
Waiter = _billInfo . Waiter ,
Printed = isPrinted ,
Void = false ,
Narration = "" ,
VoucherType = _billInfo . VoucherType
} ;
var kotSecond = GetKot ( listSecond ) ;
if ( kotSecond ! = null )
voucherSecond . Kots . Add ( kotSecond ) ;
#endregion
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
{
bi . SplitBill ( _billInfo . VoucherID , voucherFirst , voucherSecond ) ;
bi . SaveChanges ( ) ;
2011-12-05 09:41:02 +00:00
}
if ( isPrinted )
{
Thermal . PrintBill ( voucherFirst . VoucherID ) ;
Thermal . PrintBill ( voucherSecond . VoucherID ) ;
}
LoadBill ( voucherFirst . VoucherID ) ;
}
public void VoidBill ( )
2011-08-23 07:10:05 +00:00
{
2011-12-05 09:41:02 +00:00
#region Check conditions and Permissions
2014-10-12 09:41:45 +00:00
if ( _billInfo . VoucherID = = Guid . Empty )
2011-12-05 09:41:02 +00:00
return ;
//if (!_billInfo.Printed)
// return;
if ( _billInfo . Void )
return ;
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Void Bill" ) )
2011-12-05 09:41:02 +00:00
return ;
if ( MessageBox . Show ( "Are you sure you want to void this bill?" , "Void Bill" , MessageBoxButtons . YesNo , MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) ! = DialogResult . Yes )
return ;
#endregion
2011-08-23 07:10:05 +00:00
2011-12-05 09:41:02 +00:00
var voidReason = new SelectVoidReason ( GetVoidReason , true ) ;
voidReason . ShowDialog ( ) ;
if ( voidReason . SelectedItem ! = null )
{
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-12-05 09:41:02 +00:00
{
2014-10-12 09:41:45 +00:00
bi . VoidBill ( _billInfo . VoucherID , voidReason . SelectedItem . Description , ! _editVoucherID . HasValue ) ;
bi . SaveChanges ( ) ;
2011-12-05 09:41:02 +00:00
}
ClearBill ( ) ;
}
else
{
MessageBox . Show ( "Please Select a reason if you want to void the bill" , "Bill NOT Voided" , MessageBoxButtons . OK , MessageBoxIcon . Asterisk ) ;
}
}
private static List < StringType > GetVoidReason ( Dictionary < string , string > filter )
{
var list = new List < StringType >
{
new StringType ( "Discount" ) ,
new StringType ( "Printing Fault" ) ,
new StringType ( "Item Changed" ) ,
new StringType ( "Quantity Reduced" ) ,
new StringType ( "Costing Bill for Party" ) ,
new StringType ( "Cashier Mistake" ) ,
new StringType ( "Management Freesale" ) ,
new StringType ( "Other" )
} ;
return list . Where ( i = > i . Description . ToLower ( ) . Contains ( filter [ "Name" ] . ToLower ( ) . Trim ( ) ) ) . ToList ( ) ;
2011-08-23 07:10:05 +00:00
}
2011-12-05 09:41:02 +00:00
private void SaveReprintOrDiscountBill ( decimal oldAmount )
2011-08-23 07:10:05 +00:00
{
2014-10-12 09:41:45 +00:00
var amountChanged = oldAmount ! = _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID ! = Guid . Empty ) . Sum ( x = > x . Value . GrossAmount ) ;
var itemsChanged = _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID = = Guid . Empty ) . Count ( ) ! = 0 ;
2011-08-23 07:10:05 +00:00
if ( amountChanged | | itemsChanged ) // Discount or Products changed
{
#region new voucherFirst
2014-10-12 09:41:45 +00:00
var newVoucher = new Voucher ( Session . User )
2011-08-23 07:10:05 +00:00
{
Customer = _billInfo . Customer ,
2014-10-16 11:11:55 +00:00
Table = _billInfo . Table ,
2011-08-23 07:10:05 +00:00
Waiter = _billInfo . Waiter ,
Printed = true ,
Void = false ,
2011-12-05 09:41:02 +00:00
Narration = "" ,
VoucherType = _billInfo . VoucherType
2011-08-23 07:10:05 +00:00
} ;
var kotNew = GetKot ( _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product ) ) ;
if ( kotNew ! = null )
2014-10-12 09:41:45 +00:00
newVoucher . Kots . Add ( kotNew ) ;
2011-08-23 07:10:05 +00:00
#endregion
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-08-23 07:10:05 +00:00
{
2014-10-12 09:41:45 +00:00
bi . DiscountPrintedBill ( _billInfo . VoucherID , newVoucher ) ;
bi . SaveChanges ( ) ;
2011-08-23 07:10:05 +00:00
}
2014-10-12 09:41:45 +00:00
LoadBill ( newVoucher . VoucherID ) ;
2011-08-23 07:10:05 +00:00
}
else
{
using ( var bi = new ReprintBI ( ) )
2014-10-12 09:41:45 +00:00
{
2011-08-23 07:10:05 +00:00
bi . Insert ( new Reprint ( ) { Date = DbValues . Date , User = Session . User , Voucher = _billInfo } ) ;
2014-10-12 09:41:45 +00:00
bi . SaveChanges ( ) ;
}
2011-08-23 07:10:05 +00:00
}
}
2014-10-12 09:41:45 +00:00
private Guid ? InsertVoucher ( bool finalBill , bool updateTable )
2011-03-11 18:49:48 +00:00
{
2011-12-05 09:41:02 +00:00
_billInfo . Printed = finalBill ;
_billInfo . Void = false ;
_billInfo . Narration = "" ;
2014-10-12 09:41:45 +00:00
//UpdateKotDiscountAndPrice();
var kot = GetKot ( _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID = = Guid . Empty & & x . Value . Quantity ! = 0 ) ) ;
2011-12-05 09:41:02 +00:00
if ( kot = = null )
return null ;
_billInfo . Kots . Add ( kot ) ;
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-06-29 20:27:07 +00:00
{
2014-10-12 09:41:45 +00:00
var kotID = bi . Insert ( _billInfo , updateTable ) ;
bi . SaveChanges ( ) ;
return kotID ;
2011-06-29 20:27:07 +00:00
}
2011-03-11 18:49:48 +00:00
}
2014-10-12 09:41:45 +00:00
private Guid ? UpdateVoucher ( bool finalBill , bool updateTable )
2011-03-11 18:49:48 +00:00
{
2014-10-12 09:41:45 +00:00
using ( var bi = new VoucherBI ( ) )
2011-06-29 20:27:07 +00:00
{
2014-10-12 09:41:45 +00:00
var voucher = bi . Get ( x = > x . VoucherID = = _billInfo . VoucherID ) ;
voucher . User = Session . User ;
voucher . Customer = _billInfo . Customer ;
voucher . Waiter = _billInfo . Waiter ;
voucher . Printed = finalBill ;
foreach ( var item in _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID ! = Guid . Empty ) )
2011-06-29 20:27:07 +00:00
{
2014-10-12 09:41:45 +00:00
var i = voucher . Kots . Single ( x = > x . KotID = = item . Key . KotID ) . Inventories . Single ( x = > x . Product . ProductID = = item . Key . ProductID ) ;
i . Discount = item . Value . Discount ;
i . Price = item . Value . Price ;
2011-06-29 20:27:07 +00:00
}
2014-10-12 09:41:45 +00:00
if ( ! _billInfo . Printed )
{
var kot = GetKot ( _bill . Where ( x = > x . Key . BillItemType = = BillItemType . Product & & x . Key . KotID = = Guid . Empty & & x . Value . Quantity ! = 0 ) ) ;
if ( kot ! = null )
voucher . Kots . Add ( kot ) ;
}
var kotID = bi . Update ( voucher , updateTable ) ;
bi . SaveChanges ( ) ;
return kotID ;
2011-06-23 12:47:48 +00:00
}
2011-03-11 18:49:48 +00:00
}
2011-08-23 07:10:05 +00:00
private static Kot GetKot ( IEnumerable < KeyValuePair < BillItemKey , BillItemValue > > list )
2011-03-11 18:49:48 +00:00
{
var kot = new Kot ( ) ;
2011-08-23 07:10:05 +00:00
foreach ( var item in list )
{
var oldInv = kot . Inventories . SingleOrDefault ( x = > x . Product . ProductID = = item . Key . ProductID ) ;
if ( oldInv ! = null )
{
oldInv . Quantity + = item . Value . Quantity ;
if ( oldInv . Quantity = = 0 )
kot . Inventories . Remove ( oldInv ) ;
}
else
{
var inv = new Inventory
{
Product = item . Value . Product ,
Quantity = item . Value . Quantity ,
2011-08-28 12:17:15 +00:00
Price = item . Value . Price ,
FullPrice = item . Value . FullPrice ,
2011-08-23 07:10:05 +00:00
Discount = item . Value . Discount ,
ServiceCharge = item . Value . ServiceCharge ,
2012-04-08 12:28:15 +00:00
IsScTaxable = item . Value . IsScTaxable ,
2014-10-12 09:41:45 +00:00
ServiceTaxRate = item . Value . ServiceTaxRate ,
VatRate = item . Value . VatRate ,
2012-04-08 12:28:15 +00:00
ServiceTax = item . Value . ServiceTax ,
Vat = item . Value . Vat
2011-08-23 07:10:05 +00:00
} ;
foreach ( var mod in item . Value . Modifiers )
inv . InventoryModifier . Add ( new InventoryModifier { Modifier = mod } ) ;
kot . Inventories . Add ( inv ) ;
}
2011-03-11 18:49:48 +00:00
}
2011-08-23 07:10:05 +00:00
2011-06-23 12:47:48 +00:00
return kot . Inventories . Count = = 0 ? null : kot ;
}
#endregion
2011-12-05 09:41:02 +00:00
2011-06-23 12:47:48 +00:00
#region InputBox
private bool GetInput ( string prompt , ref decimal amount )
{
var result = InputBox . Show ( prompt , amount . ToString ( ) , InputBox_Validating ) ;
if ( ! result . OK )
return false ;
2011-12-05 09:41:02 +00:00
return decimal . TryParse ( result . Text , out amount ) ;
2011-06-23 12:47:48 +00:00
}
private bool GetInput ( string prompt , ref string info )
{
var result = InputBox . Show ( prompt , info , InputBox_Validating ) ;
if ( ! result . OK )
return false ;
info = result . Text . Trim ( ) ;
2011-12-05 09:41:02 +00:00
return ! string . IsNullOrEmpty ( info ) ;
2011-03-11 18:49:48 +00:00
}
#endregion
2011-01-10 19:49:11 +00:00
}
2011-03-11 18:49:48 +00:00
}