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
{
2016-03-31 06:57:39 +00:00
public readonly BillDict _bill ;
public Voucher _voucher ;
2016-03-31 08:43:26 +00:00
public Guid ? _editVoucherID ;
2011-01-10 19:49:11 +00:00
2014-11-20 08:12:20 +00:00
public BillController ( Guid ? editVoucherID )
2011-02-09 12:03:22 +00:00
{
2011-03-11 18:49:48 +00:00
this . _editVoucherID = editVoucherID ;
2012-12-01 09:49:33 +00:00
_bill = new BillDict ( ) ;
2018-08-24 10:41:33 +00:00
_voucher = new Voucher ( Session . User , CustomerBI . Get ( Constants . CASH_CUSTOMER ) ) ;
2011-03-11 18:49:48 +00:00
}
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
{
2016-04-11 07:01:52 +00:00
var newKey = new BillItemKey ( product . ProductID , Guid . Empty , product . HasHappyHour ) ;
2011-06-23 12:47:48 +00:00
if ( _bill . ContainsKey ( newKey ) )
{
2016-03-29 09:36:46 +00:00
_bill [ newKey ] . inventory . Quantity + = 1 ;
2011-06-23 12:47:48 +00:00
}
else
{
2016-04-11 07:01:52 +00:00
var billItemValue = new BillItemValue ( product , product . HasHappyHour ) ;
var old = _bill . FirstOrDefault ( x = > x . Key . BillItemType = = ( product . HasHappyHour ? BillItemType . HappyHourProduct : BillItemType . RegularProduct ) & & x . Key . ProductID = = newKey . ProductID ) ;
2011-06-23 12:47:48 +00:00
if ( old . Key ! = null )
{
2016-03-29 09:36:46 +00:00
billItemValue . inventory . Discount = old . Value . inventory . Discount ;
billItemValue . inventory . Price = old . Value . inventory . Price ;
2011-06-23 12:47:48 +00:00
}
_bill . Add ( newKey , billItemValue ) ;
}
}
2016-03-31 06:57:39 +00:00
public void ShowModifiers ( BillItemValue item )
2011-01-30 07:14:05 +00:00
{
2016-03-29 09:36:46 +00:00
using ( var frm = new ModifierForm ( Cache . ProductGroupModifiers ( item . inventory . Product . ProductGroup . ProductGroupID ) , item . inventory . InventoryModifier ) )
2011-01-30 07:14:05 +00:00
{
2014-11-10 11:06:49 +00:00
frm . ShowDialog ( ) ;
2011-01-30 07:14:05 +00:00
}
2011-01-31 20:33:22 +00:00
}
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" ) )
2016-03-31 06:57:39 +00:00
return ;
2011-02-09 12:03:22 +00:00
2018-08-24 10:41:33 +00:00
using ( var frm = new DiscountForm ( ProductGroupBI . GetProductGroupTypes ( ) ) )
2011-01-31 20:33:22 +00:00
{
2018-08-24 10:41:33 +00:00
if ( frm . ShowDialog ( ) = = DialogResult . OK )
2011-01-31 20:33:22 +00:00
{
2018-08-24 10:41:33 +00:00
HashSet < string > outList ;
var discount = frm . Selection ( out outList ) ;
discount = discount / 100 ;
if ( discount > 1 | | discount < 0 )
return ;
2011-06-23 12:47:48 +00:00
2018-08-24 10:41:33 +00:00
foreach ( var item in _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & outList . Contains ( x . Value . inventory . Product . ProductGroup . GroupType ) ) )
{
var product = item . Value . inventory . Product ;
var maxDiscount = product . ProductGroup . DiscountLimit ;
if ( discount > item . Value . inventory . 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 . inventory . Discount = discount ;
2011-01-31 20:33:22 +00:00
}
}
}
2011-01-10 19:49:11 +00:00
}
2016-03-31 06:57:39 +00:00
public void SetQuantity ( BillItemKey key , BillItemValue item , decimal quantity , bool prompt )
2011-12-05 09:41:02 +00:00
{
2016-03-29 09:36:46 +00:00
if ( item = = null | | key . KotID ! = Guid . Empty )
return ; // No Product or Old Product
2011-12-05 09:41:02 +00:00
if ( prompt & & ! GetInput ( "Quantity" , ref quantity ) )
return ;
if ( ! prompt )
2016-03-29 09:36:46 +00:00
quantity + = item . inventory . 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 ;
2016-04-11 13:27:07 +00:00
var old = _bill . Where ( x = > x . Key . ProductID = = key . ProductID & & x . Key . KotID ! = Guid . Empty & & x . Key . BillItemType = = key . BillItemType ) . Sum ( x = > x . Value . inventory . Quantity ) ;
quantity = Math . Max ( quantity , - 1 * old ) ;
2016-03-29 09:36:46 +00:00
item . inventory . Quantity = quantity ;
2011-12-05 09:41:02 +00:00
}
2016-03-31 06:57:39 +00:00
public void SetPrice ( BillItemValue item )
2011-01-10 19:49:11 +00:00
{
2016-04-11 07:01:52 +00:00
if ( item . inventory . IsHappyHour )
return ;
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Change Rate" ) )
2016-03-31 06:57:39 +00:00
return ;
2016-03-29 09:36:46 +00:00
var price = item . inventory . Price ;
if ( ! GetInput ( "Price" , ref price ) )
2011-12-05 09:41:02 +00:00
return ;
2016-03-29 09:36:46 +00:00
if ( price = = 0 & & ! Session . IsAllowed ( "NC Product" ) )
2014-10-16 11:11:55 +00:00
throw new PermissionException ( "NC of Product is not Allowed" ) ;
2016-04-11 07:01:52 +00:00
foreach ( var sub in _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . ProductID = = item . inventory . Product . ProductID ) )
2016-03-29 09:36:46 +00:00
sub . Value . inventory . Price = price ;
2011-01-10 19:49:11 +00:00
}
2016-03-31 06:57:39 +00:00
public void ShowCustomers ( )
2011-08-23 07:10:05 +00:00
{
2016-03-31 06:57:39 +00:00
using ( var frm = new CustomerListForm ( ) )
2011-08-23 07:10:05 +00:00
{
2016-03-31 06:57:39 +00:00
frm . ShowDialog ( ) ;
_voucher . Customer = frm . SelectedItem ;
2011-08-23 07:10:05 +00:00
}
}
2011-12-05 09:41:02 +00:00
private static bool IsPrintedOrVoid ( Voucher voucher )
2011-02-09 12:03:22 +00:00
{
2018-08-24 10:41:33 +00:00
var dbVoucher = VoucherBI . Get ( 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
{
2018-08-24 10:41:33 +00:00
var dbVoucher = VoucherBI . Get ( voucherID ) ;
return dbVoucher . Printed | | dbVoucher . Void ;
2011-12-05 09:41:02 +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
{
2018-08-24 10:41:33 +00:00
_voucher = VoucherBI . Get ( voucherID ) ;
_bill . Load ( _voucher ) ;
var newKotKey = new BillItemKey ( Guid . Empty ) ;
var newKotItem = new BillItemValue ( ) ;
_bill . Add ( newKotKey , newKotItem ) ;
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
{
2018-08-24 10:41:33 +00:00
FoodTable table = FoodTableBI . GetFromName ( tableName ) ;
2014-10-12 09:41:45 +00:00
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-11-20 08:12:20 +00:00
_voucher . Table = table ;
_voucher . Pax = frm . Pax ;
2014-10-12 09:41:45 +00:00
}
2011-01-10 19:49:11 +00:00
}
}
2016-03-31 06:57:39 +00:00
public bool CancelBillChanges ( )
2011-01-10 19:49:11 +00:00
{
2016-04-11 13:27:07 +00:00
if ( _bill . Any ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID = = Guid . Empty ) & &
2011-06-23 12:47:48 +00:00
MessageBox . Show ( "Abandon Changes?" , "Abandon Changes" , MessageBoxButtons . YesNo ,
MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) = = DialogResult . No )
2016-03-31 06:57:39 +00:00
return false ;
2011-01-10 19:49:11 +00:00
ClearBill ( ) ;
2016-03-31 06:57:39 +00:00
return true ;
2011-01-10 19:49:11 +00:00
}
2018-08-24 10:41:33 +00:00
public void ClearBill ( )
2011-01-10 19:49:11 +00:00
{
2018-08-24 10:41:33 +00:00
_voucher = new Voucher ( Session . User , CustomerBI . Get ( Constants . CASH_CUSTOMER ) ) ;
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-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
{
2016-03-31 08:43:26 +00:00
ClearBill ( ) ;
2011-03-11 18:49:48 +00:00
if ( _editVoucherID . HasValue )
2011-01-10 19:49:11 +00:00
{
2016-04-13 09:38:20 +00:00
FoodTable ft ;
2018-08-24 10:41:33 +00:00
ft = FoodTableBI . GetFromVoucher ( _editVoucherID . Value ) ;
2016-03-31 08:43:26 +00:00
LoadBill ( _editVoucherID . Value ) ;
2016-04-13 09:56:02 +00:00
if ( ft ! = null )
_editVoucherID = null ;
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
}
2016-03-31 06:57:39 +00:00
internal bool SettleBill ( )
2011-01-17 14:55:43 +00:00
{
2016-03-29 09:36:46 +00:00
if ( _voucher . VoucherID = = Guid . Empty | | ! _voucher . Printed | | ! Session . IsAllowed ( "Settle Bill" ) )
2016-03-31 06:57:39 +00:00
return false ;
2011-03-11 18:49:48 +00:00
IDictionary < SettleOption , decimal > options ;
2014-11-20 08:12:20 +00:00
var amount = - 1 * _voucher . Kots . Sum ( x = > x . Inventories . Sum ( y = > y . Amount ) ) ;
using ( var frm = new SettleChoicesForm ( Math . Round ( amount ) * - 1 , _voucher . 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 )
2016-03-31 06:57:39 +00:00
return false ;
2018-08-24 10:41:33 +00:00
VoucherBI . SettleVoucher ( Session . User , _voucher . VoucherID , options , ! _editVoucherID . HasValue ) ;
2011-03-11 18:49:48 +00:00
ClearBill ( ) ;
2016-03-31 06:57:39 +00:00
return true ;
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
{
2018-08-24 10:41:33 +00:00
using ( var frm = new MoveTableForm ( FoodTableBI . List ( true ) , allowMerge ) )
2011-02-18 16:54:48 +00:00
{
2018-08-24 10:41:33 +00:00
frm . ShowDialog ( ) ;
if ( frm . Selection ! = null )
return frm . Selection ;
2011-02-18 16:54:48 +00:00
}
2018-08-24 10:41:33 +00:00
return null ;
2011-02-18 16:54:48 +00:00
}
2016-03-31 06:57:39 +00:00
internal void MoveKot ( BillItemKey currentKot )
2011-02-18 16:54:48 +00:00
{
2016-03-29 09:36:46 +00:00
if ( _voucher . VoucherID = = Guid . Empty | | IsPrintedOrVoid ( _voucher ) )
2011-02-18 16:54:48 +00:00
return ;
2016-03-31 06:57:39 +00:00
var kot = currentKot ;
2011-02-18 16:54:48 +00:00
if ( kot = = null )
return ;
var table = GetTableForMove ( true ) ;
if ( table = = null )
return ;
2014-11-20 08:12:20 +00:00
if ( table . FoodTableID = = _voucher . 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 )
2018-08-24 10:41:33 +00:00
{
2011-02-18 16:54:48 +00:00
//Move Kot
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Move Kot" ) )
voucherID = VoucherBI . MoveKot ( kot . KotID , table . FoodTableID ) ;
}
else if ( table . VoucherID . HasValue & & kotCount > 1 )
{
2011-02-18 16:54:48 +00:00
//Merge Kot
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Merge Kots" ) )
voucherID = VoucherBI . MergeKot ( kot . KotID , table . VoucherID . Value ) ;
}
2014-10-12 09:41:45 +00:00
else if ( table . VoucherID = = null & & kotCount = = 1 )
2018-08-24 10:41:33 +00:00
{
2011-12-05 09:41:02 +00:00
//Move Table
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Move Table" ) )
{
VoucherBI . MoveTable ( _voucher . VoucherID , table . FoodTableID ) ;
voucherID = _voucher . VoucherID ;
}
}
else if ( table . VoucherID . HasValue & & kotCount = = 1 )
{
2011-02-18 16:54:48 +00:00
//Merge Table
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Merge Tables" ) )
voucherID = VoucherBI . MergeTables ( _voucher . VoucherID , table . VoucherID . Value ) ;
}
2014-10-12 09:41:45 +00:00
if ( voucherID ! = Guid . Empty )
2016-03-31 06:57:39 +00:00
{
2011-02-18 16:54:48 +00:00
LoadBill ( voucherID ) ;
2016-03-31 06:57:39 +00:00
}
2011-02-18 16:54:48 +00:00
}
2011-06-23 12:47:48 +00:00
internal void MoveTable ( )
{
2014-11-20 08:12:20 +00:00
if ( _voucher . VoucherID = = Guid . Empty )
2011-06-23 12:47:48 +00:00
return ;
2011-12-05 09:41:02 +00:00
2014-11-20 08:12:20 +00:00
var allowMerge = ! IsPrintedOrVoid ( _voucher ) ;
2011-12-05 09:41:02 +00:00
var table = GetTableForMove ( allowMerge ) ;
2011-06-23 12:47:48 +00:00
if ( table = = null )
return ;
2014-11-20 08:12:20 +00:00
if ( table . FoodTableID = = _voucher . 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 ;
2018-08-24 10:41:33 +00:00
if ( table . VoucherID . HasValue )
2014-10-12 09:41:45 +00:00
{
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Merge Tables" ) )
2016-03-31 06:57:39 +00:00
{
2018-08-24 10:41:33 +00:00
LoadBill ( VoucherBI . MergeTables ( _voucher . VoucherID , table . VoucherID . Value ) ) ;
2016-03-31 06:57:39 +00:00
}
2011-12-05 09:41:02 +00:00
}
2011-08-23 07:10:05 +00:00
else
2011-12-05 09:41:02 +00:00
{
2018-08-24 10:41:33 +00:00
if ( Session . IsAllowed ( "Move Table" ) )
2011-12-05 09:41:02 +00:00
{
2018-08-24 10:41:33 +00:00
LoadBill ( VoucherBI . MoveTable ( _voucher . VoucherID , table . FoodTableID ) ) ;
2011-12-05 09:41:02 +00:00
}
}
2011-06-23 12:47:48 +00:00
}
2018-08-24 10:41:33 +00:00
#endregion
#region Save
2011-12-05 09:41:02 +00:00
public void SplitBill ( )
{
2016-01-18 10:43:43 +00:00
bool isPrinted = false ;
2011-12-05 09:41:02 +00:00
#region Permissions
2014-10-16 11:11:55 +00:00
if ( ! Session . IsAllowed ( "Split Bill" ) )
2011-12-05 09:41:02 +00:00
return ;
2016-01-18 10:43:43 +00:00
if ( _voucher . VoucherID = = Guid . Empty )
return ; // must be existing non void bill
2018-08-24 10:41:33 +00:00
var dbVoucher = VoucherBI . Get ( _voucher . VoucherID ) ;
if ( dbVoucher . Void )
2016-01-18 10:43:43 +00:00
{
2018-08-24 10:41:33 +00:00
MessageBox . Show ( string . Format ( "This Bill is already void.\nReason: {0}" , _voucher . VoidReason ) , "Bill already Voided" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ; // must be a non void bill
}
if ( dbVoucher . Printed & & ! Session . IsAllowed ( "Edit Printed Bill" ) )
{
MessageBox . Show ( "This Bill is already Printed.\nYou do not have the authority to alter it" , "Bill already Printed" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ; // not allowed to edit printed bill
2016-01-18 10:43:43 +00:00
}
2018-08-24 10:41:33 +00:00
isPrinted = dbVoucher . Printed ;
2011-12-05 09:41:02 +00:00
#endregion
#region Get Move List
HashSet < string > splitList = null ;
2018-08-24 10:41:33 +00:00
using ( var frm = new DiscountForm ( ProductGroupBI . GetProductGroupTypes ( ) ) )
if ( frm . ShowDialog ( ) = = DialogResult . OK )
frm . Selection ( out splitList ) ;
2011-12-05 09:41:02 +00:00
if ( splitList = = null | | splitList . Count = = 0 )
return ;
2016-04-11 07:01:52 +00:00
var listFirst = _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID ! = Guid . Empty & & splitList . Contains ( x . Value . inventory . Product . ProductGroup . GroupType ) ) ;
var listSecond = _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID ! = Guid . Empty & & ! splitList . Contains ( x . Value . inventory . 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
2016-03-31 06:57:39 +00:00
var voucherFirst = new Voucher ( Session . User , _voucher . Customer )
2011-12-05 09:41:02 +00:00
{
2014-10-16 11:11:55 +00:00
Table = table ,
2011-12-05 09:41:02 +00:00
Printed = isPrinted ,
Void = false ,
Narration = "" ,
2014-11-20 08:12:20 +00:00
VoucherType = _voucher . VoucherType
2011-12-05 09:41:02 +00:00
} ;
var kotFirst = GetKot ( listFirst ) ;
if ( kotFirst ! = null )
voucherFirst . Kots . Add ( kotFirst ) ;
#endregion
2014-10-12 09:41:45 +00:00
#region new voucherSecond
2016-03-31 06:57:39 +00:00
var voucherSecond = new Voucher ( Session . User , _voucher . Customer )
2011-12-05 09:41:02 +00:00
{
2014-11-20 08:12:20 +00:00
Table = _voucher . Table ,
2011-12-05 09:41:02 +00:00
Printed = isPrinted ,
Void = false ,
Narration = "" ,
2014-11-20 08:12:20 +00:00
VoucherType = _voucher . VoucherType
2011-12-05 09:41:02 +00:00
} ;
var kotSecond = GetKot ( listSecond ) ;
if ( kotSecond ! = null )
voucherSecond . Kots . Add ( kotSecond ) ;
#endregion
2018-08-24 10:41:33 +00:00
VoucherBI . SplitBill ( _voucher . VoucherID , voucherFirst , voucherSecond ) ;
2011-12-05 09:41:02 +00:00
if ( isPrinted )
{
Thermal . PrintBill ( voucherFirst . VoucherID ) ;
Thermal . PrintBill ( voucherSecond . VoucherID ) ;
}
LoadBill ( voucherFirst . VoucherID ) ;
}
2016-03-31 06:57:39 +00:00
public bool VoidBill ( )
2011-08-23 07:10:05 +00:00
{
2011-12-05 09:41:02 +00:00
#region Check conditions and Permissions
2016-03-31 06:57:39 +00:00
if ( _voucher . VoucherID = = Guid . Empty | | _voucher . Void | | ! Session . IsAllowed ( "Void Bill" ) )
return false ;
2011-12-05 09:41:02 +00:00
if ( MessageBox . Show ( "Are you sure you want to void this bill?" , "Void Bill" , MessageBoxButtons . YesNo , MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) ! = DialogResult . Yes )
2016-03-31 06:57:39 +00:00
return false ;
2011-12-05 09:41:02 +00:00
#endregion
2018-08-24 10:41:33 +00:00
var reasons = new string [ ] {
"Discount" ,
"Printing Fault" ,
"Item Changed" ,
"Quantity Reduced" ,
"Costing Bill for Party" ,
"Cashier Mistake" ,
"Management Freesale" ,
"Other"
2016-04-11 07:01:52 +00:00
} ;
2011-08-23 07:10:05 +00:00
2016-04-11 07:01:52 +00:00
using ( var voidReason = new VoidReasonListForm ( reasons ) )
2011-12-05 09:41:02 +00:00
{
2016-04-11 07:01:52 +00:00
voidReason . ShowDialog ( ) ;
if ( voidReason . SelectedItem = = null )
{
MessageBox . Show ( "Please Select a reason if you want to void the bill" , "Bill NOT Voided" , MessageBoxButtons . OK , MessageBoxIcon . Asterisk ) ;
return false ;
}
2018-08-24 10:41:33 +00:00
VoucherBI . VoidBill ( _voucher . VoucherID , voidReason . SelectedItem , ! _editVoucherID . HasValue ) ;
}
ClearBill ( ) ;
return true ;
}
public bool SaveAndPrintKot ( )
{
#region Check if Allowed
if ( ! Session . IsAllowed ( "Print Kot" ) | | _bill . Count ( x = > x . Key . BillItemType ! = BillItemType . Kot ) = = 0 )
return false ;
bool isPrinted = false , isVoid = false ;
if ( _voucher . VoucherID ! = Guid . Empty )
{
var dbVoucher = VoucherBI . Get ( _voucher . VoucherID ) ;
isPrinted = dbVoucher . Printed ;
isVoid = dbVoucher . Void ;
}
if ( isVoid )
{
MessageBox . Show ( string . Format ( "This Bill is already void.\nReason: {0}" , _voucher . VoidReason ) , "Bill already Voided" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return false ;
}
if ( isPrinted )
{
MessageBox . Show ( string . Format ( "This Bill is already printed and a kot cannot be added" , _voucher . VoidReason ) , "Bill already Printed" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return false ;
}
if ( _voucher . VoucherID ! = Guid . Empty & & IsPrintedOrVoid ( _voucher ) )
return false ;
#endregion
//Save
var saved = _voucher . VoucherID = = Guid . Empty ? InsertVoucher ( false , ! _editVoucherID . HasValue ) : UpdateVoucher ( false , ! _editVoucherID . HasValue ) ;
//Print
if ( ! _editVoucherID . HasValue & & saved . HasValue )
Thermal . PrintKot ( _voucher . VoucherID , saved . Value ) ;
//Cleanup
ClearBill ( ) ;
return true ;
}
public bool CanSaveBill ( bool isPrinted , bool isVoid )
{
if ( ! Session . IsAllowed ( "Print Bill" ) | | _bill . Count ( x = > x . Key . BillItemType ! = BillItemType . Kot ) = = 0 )
return false ;
if ( isVoid )
{
MessageBox . Show ( string . Format ( "This Bill is already void.\nReason: {0}" , _voucher . VoidReason ) , "Bill already Voided" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return false ;
}
if ( isPrinted & & ! Session . IsAllowed ( "Edit Printed Bill" ) )
return false ;
return true ;
}
public bool SaveAndPrintBill ( bool isPrinted , decimal amount )
{
if ( isPrinted )
{
SaveReprintOrDiscountBill ( amount ) ;
}
else
{
// Ask for VoucherType only for new bill, if none, then cancel
using ( var frm = new VoucherTypeForm ( ) )
2016-04-11 07:01:52 +00:00
{
2018-08-24 10:41:33 +00:00
frm . ShowDialog ( ) ;
if ( ! frm . Selection . HasValue )
return false ;
_voucher . VoucherType = frm . Selection . Value ;
2016-04-11 07:01:52 +00:00
}
2018-08-24 10:41:33 +00:00
if ( _voucher . VoucherID = = Guid . Empty )
InsertVoucher ( true , ! _editVoucherID . HasValue ) ;
else
UpdateVoucher ( true , ! _editVoucherID . HasValue ) ;
2011-12-05 09:41:02 +00:00
}
2018-08-24 10:41:33 +00:00
Thermal . PrintBill ( _voucher . VoucherID ) ;
2016-03-31 06:57:39 +00:00
ClearBill ( ) ;
return true ;
2011-12-05 09:41:02 +00:00
}
private void SaveReprintOrDiscountBill ( decimal oldAmount )
2011-08-23 07:10:05 +00:00
{
2016-04-11 07:01:52 +00:00
var amountChanged = oldAmount ! = _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID ! = Guid . Empty ) . Sum ( x = > x . Value . inventory . Net ) ;
var itemsChanged = _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID = = Guid . Empty ) . Count ( ) ! = 0 ;
2016-03-29 09:36:46 +00:00
2011-08-23 07:10:05 +00:00
if ( amountChanged | | itemsChanged ) // Discount or Products changed
{
#region new voucherFirst
2016-03-31 06:57:39 +00:00
var newVoucher = new Voucher ( Session . User , _voucher . Customer )
2011-08-23 07:10:05 +00:00
{
2014-11-20 08:12:20 +00:00
Table = _voucher . Table ,
2011-08-23 07:10:05 +00:00
Printed = true ,
Void = false ,
2011-12-05 09:41:02 +00:00
Narration = "" ,
2014-11-20 08:12:20 +00:00
VoucherType = _voucher . VoucherType
2011-08-23 07:10:05 +00:00
} ;
2016-04-11 07:01:52 +00:00
var kotNew = GetKot ( _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot ) ) ;
2011-08-23 07:10:05 +00:00
if ( kotNew ! = null )
2014-10-12 09:41:45 +00:00
newVoucher . Kots . Add ( kotNew ) ;
2011-08-23 07:10:05 +00:00
#endregion
2018-08-24 10:41:33 +00:00
newVoucher = VoucherBI . DiscountPrintedBill ( _voucher . VoucherID , newVoucher ) ;
2014-11-20 08:12:20 +00:00
_editVoucherID = null ;
2014-10-12 09:41:45 +00:00
LoadBill ( newVoucher . VoucherID ) ;
2011-08-23 07:10:05 +00:00
}
else
{
2018-08-24 10:41:33 +00:00
ReprintBI . Insert ( new Reprint ( ) { User = Session . User , Voucher = _voucher } ) ;
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
{
2014-11-20 08:12:20 +00:00
_voucher . Printed = finalBill ;
_voucher . Void = false ;
_voucher . Narration = "" ;
2016-04-11 07:01:52 +00:00
var kot = GetKot ( _bill . Where ( x = > x . Key . KotID = = Guid . Empty ) ) ;
2011-12-05 09:41:02 +00:00
if ( kot = = null )
return null ;
2014-11-20 08:12:20 +00:00
_voucher . Kots . Add ( kot ) ;
2018-08-24 10:41:33 +00:00
_voucher = VoucherBI . Insert ( _voucher , updateTable ) ;
return _voucher . Kots [ 0 ] . KotID ;
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
{
2018-08-24 10:41:33 +00:00
var voucher = VoucherBI . Get ( _voucher . VoucherID ) ;
voucher . User = Session . User ;
voucher . Customer = _voucher . Customer ;
voucher . Printed = finalBill ;
voucher . VoucherType = _voucher . VoucherType ;
foreach ( var item in _bill . Where ( x = > x . Key . BillItemType ! = BillItemType . Kot & & x . Key . KotID ! = Guid . Empty ) )
{
var i = voucher . Kots . Single ( x = > x . KotID = = item . Key . KotID ) . Inventories . Single ( x = > x . Product . ProductID = = item . Key . ProductID & & x . IsHappyHour = = item . Value . inventory . IsHappyHour ) ;
i . Discount = item . Value . inventory . Discount ;
i . Price = item . Value . inventory . Price ;
}
if ( ! _voucher . Printed )
{
var kot = GetKot ( _bill . Where ( x = > x . Key . KotID = = Guid . Empty ) ) ;
if ( kot ! = null )
voucher . Kots . Add ( kot ) ;
}
try
{
var kotID = VoucherBI . Update ( voucher , updateTable ) ;
2014-10-12 09:41:45 +00:00
return kotID ;
2011-06-23 12:47:48 +00:00
}
2018-08-24 10:41:33 +00:00
catch ( ArgumentException ex )
{
MessageBox . Show ( ex . Message ) ;
return null ;
}
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 )
{
2016-04-11 07:01:52 +00:00
if ( item . Key . BillItemType = = BillItemType . Kot )
continue ;
if ( item . Value . inventory . Quantity = = 0 )
continue ;
var happyHour = item . Key . BillItemType = = BillItemType . HappyHourProduct ;
var productID = item . Key . ProductID ;
var oldInv = kot . Inventories . SingleOrDefault ( x = > x . Product . ProductID = = productID & & x . IsHappyHour = = happyHour ) ;
2011-08-23 07:10:05 +00:00
if ( oldInv ! = null )
{
2016-03-29 09:36:46 +00:00
oldInv . Quantity + = item . Value . inventory . Quantity ;
2011-08-23 07:10:05 +00:00
if ( oldInv . Quantity = = 0 )
kot . Inventories . Remove ( oldInv ) ;
}
else
{
var inv = new Inventory
2018-08-24 10:41:33 +00:00
{
Product = item . Value . inventory . Product ,
Quantity = item . Value . inventory . Quantity ,
Price = item . Value . inventory . Price ,
IsHappyHour = item . Value . inventory . IsHappyHour ,
Discount = item . Value . inventory . Discount ,
ServiceCharge = item . Value . inventory . ServiceCharge ,
IsScTaxable = item . Value . inventory . IsScTaxable ,
ServiceTaxRate = item . Value . inventory . ServiceTaxRate ,
VatRate = item . Value . inventory . VatRate ,
ServiceTax = item . Value . inventory . ServiceTax ,
Vat = item . Value . inventory . Vat
} ;
2016-03-29 09:36:46 +00:00
foreach ( var mod in item . Value . inventory . InventoryModifier )
inv . InventoryModifier . Add ( new InventoryModifier { Modifier = mod . Modifier } ) ;
2011-08-23 07:10:05 +00:00
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
}