2010-03-02 17:56:21 +00:00
using System ;
2012-04-06 20:07:58 +00:00
using System.Collections ;
2010-03-02 17:56:21 +00:00
using System.Collections.Generic ;
using System.Drawing ;
using System.Globalization ;
using System.Linq ;
using System.Threading ;
using System.Windows.Forms ;
using Tanshu.Accounts.BI ;
using Tanshu.Accounts.Contracts ;
using Tanshu.Accounts.Helpers ;
using Tanshu.Common ;
namespace Tanshu.Accounts.PointOfSale
{
public partial class SalesForm : Form
{
2011-12-05 09:23:02 +00:00
private static readonly Logging . SqlLogger Log = new Logging . SqlLogger ( System . Reflection . MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2010-03-02 17:56:21 +00:00
#region Waiting
private List < Guid > selectedBills = new List < Guid > ( ) ;
private List < PendingBillsBO > pendingBills = new List < PendingBillsBO > ( ) ;
private PendingType pendingList = PendingType . Today ;
#endregion
2011-12-05 09:23:02 +00:00
private SaleFormState _formState = SaleFormState . Waiting ;
2010-03-02 17:56:21 +00:00
#region Billing
private Dictionary < BillItemKey , SalesBillItemBO > bill = new Dictionary < BillItemKey , SalesBillItemBO > ( ) ;
private CustomerBO customer = new CustomerBI ( ) . GetCustomer ( new Guid ( "F016CBAD-206C-42C0-BB1D-6006CE57BAB5" ) ) ;
2011-12-05 09:23:02 +00:00
private Guid ? _advanceID ;
2010-03-02 17:56:21 +00:00
#endregion
2011-12-05 09:23:02 +00:00
private SaleVoucherBO _billInfo ;
2010-03-02 17:56:21 +00:00
object lockObject = new object ( ) ;
2011-12-05 09:23:02 +00:00
Guid ? _newBillID ;
2010-03-02 17:56:21 +00:00
2011-12-05 09:23:02 +00:00
readonly int _floor ;
2010-03-02 17:56:21 +00:00
public SalesForm ( )
{
InitializeComponent ( ) ;
btnCustomer . Text = customer . Name ;
lblUser . Text = CurrentUser . user . Name ;
2011-12-05 09:23:02 +00:00
var floorString = System . Configuration . ConfigurationManager . AppSettings [ "floor" ] ;
_floor = ! string . IsNullOrEmpty ( floorString ) ? Convert . ToInt32 ( floorString . Trim ( ) ) : 0 ;
2010-03-02 17:56:21 +00:00
}
public SalesForm ( Guid voucherID )
: this ( )
{
2011-12-05 09:23:02 +00:00
_newBillID = voucherID ;
2010-03-02 17:56:21 +00:00
}
private void SalesForm_KeyDown ( object sender , KeyEventArgs e )
{
2011-12-05 09:23:02 +00:00
if ( _formState = = SaleFormState . Billing )
2010-03-02 17:56:21 +00:00
{
#region Billing KeyDown
switch ( e . KeyCode )
{
case Keys . F2 :
{
if ( dgvProducts . Rows . Count > 0 )
SetQuantity ( CurrentProduct , 0 , false , true ) ;
break ;
}
case Keys . F3 :
{
btnDiscount_Click ( sender , new EventArgs ( ) ) ;
break ;
}
case Keys . F4 :
{
if ( ! e . Alt )
ShowCustomerList ( false ) ;
break ;
}
case Keys . F5 :
{
btnWaiter_Click ( sender , new EventArgs ( ) ) ;
break ;
}
case Keys . F7 :
{
using ( SelectProduct selectProduct = new SelectProduct ( new ProductBI ( ) . GetFilteredProducts , true ) )
{
selectProduct . ShowDialog ( ) ;
if ( selectProduct . SelectedItem ! = null )
AddProductToGrid ( selectProduct . SelectedItem . ProductID ) ;
}
break ;
}
case Keys . F8 :
{
LoadBillFromTable ( ) ;
break ;
}
case Keys . F9 :
{
if ( dgvProducts . Rows . Count > 0 )
SetAmount ( CurrentProduct , - 1 ) ;
break ;
}
2011-12-05 09:23:02 +00:00
case Keys . F10 :
{
if ( dgvProducts . Rows . Count > 0 )
SetAmount ( CurrentProduct , - 1 ) ;
break ;
}
2010-03-02 17:56:21 +00:00
case Keys . F11 :
{
btnPrintBill_Click ( sender , e ) ;
break ;
}
case Keys . F12 :
{
btnPrintKot_Click ( sender , e ) ;
break ;
}
case Keys . Delete :
{
if ( dgvProducts . Rows . Count > 0 )
ProductRemove ( CurrentProduct ) ;
break ;
}
case Keys . Add :
{
if ( dgvProducts . Rows . Count > 0 )
SetQuantity ( CurrentProduct , 1 , false , false ) ;
break ;
}
case Keys . Subtract :
{
if ( dgvProducts . Rows . Count > 0 )
SetQuantity ( CurrentProduct , - 1 , false , false ) ;
break ;
}
case Keys . Up :
{
if ( ( bindingSource . Position > = 1 ) & & ( ! dgvProducts . Focused ) )
bindingSource . Position - = 1 ;
break ;
}
case Keys . Down :
{
if ( ( bindingSource . Position < bindingSource . Count - 1 ) & & ( ! dgvProducts . Focused ) )
bindingSource . Position + = 1 ;
break ;
}
case Keys . Escape :
{
if ( bill . Count ! = 0 )
if ( MessageBox . Show ( "Cancel current bill?" , "Cancel bill" , MessageBoxButtons . YesNo , MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) = = DialogResult . No )
return ;
ClearBill ( ) ;
break ;
}
}
#endregion
}
else
{
#region Waiting KeyDown
switch ( e . KeyCode )
{
case Keys . F6 :
{
ChangeFormState ( SaleFormState . Billing ) ;
break ;
}
case Keys . F8 :
{
LoadBillFromTable ( ) ;
break ;
}
}
#endregion
}
}
#region Helper Functions
private void ClearBill ( )
{
ShowCustomerList ( true ) ;
2011-12-05 09:23:02 +00:00
this . _billInfo = null ;
2010-03-02 17:56:21 +00:00
this . txtBillID . Text = "" ;
this . txtKotID . Text = "" ;
this . txtCreationDate . Text = "" ;
this . txtDate . Text = "" ;
this . txtLastEditDate . Text = "" ;
this . txtNarration . Text = "" ;
this . txtUserID . Text = "" ;
this . txtTableID . Text = "" ;
this . btnWaiter . Text = "Waiter - F5" ;
this . btnWaiter . Tag = null ;
txtGrossTax . Text = "0.00" ;
txtDiscount . Text = "0.00" ;
txtGrossAmount . Text = "0.00" ;
txtAmount . Text = "0.00" ;
bill . Clear ( ) ;
2011-12-05 09:23:02 +00:00
_advanceID = null ;
2010-03-02 17:56:21 +00:00
bindingSource . DataSource = bill . Values ;
ChangeFormState ( SaleFormState . Waiting ) ;
}
private void AddProductToGrid ( Guid productID )
{
BillHelperFunctions . AddProductToGrid ( productID , bindingSource , bill ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
}
private void SetQuantity ( SalesBillItemBO product , decimal quantity , bool absolute , bool prompt )
{
if ( product = = null )
return ;
BillHelperFunctions . SetQuantity ( product , quantity , absolute , prompt , bindingSource , bill ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
}
private void SetAmount ( SalesBillItemBO product , decimal amount )
{
if ( product = = null )
return ;
BillHelperFunctions . SetAmount ( product , amount , bindingSource , bill ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
}
private void SetDiscount ( SalesBillItemBO product , decimal discount )
{
if ( product = = null )
return ;
BillHelperFunctions . SetDiscount ( product . productID , discount , customer , bill ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
return ;
}
private bool ProductRemove ( SalesBillItemBO product )
{
if ( product = = null )
return false ;
if ( product . Printed > 0 )
{
if ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/EditPrintedProduct" ) )
{
MessageBox . Show ( "You are not allowed to delete already printed products" ) ;
return false ;
}
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 ) ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
return true ;
}
private void InputBox_Validating ( object sender , InputBoxValidatingArgs e )
{
}
private void button_Click ( object sender , EventArgs e )
{
2011-12-05 09:23:02 +00:00
var button = sender as Button ;
2010-03-02 17:56:21 +00:00
if ( button = = null )
return ;
2011-12-05 09:23:02 +00:00
var tag = ( Guid ) button . Tag ;
2010-03-02 17:56:21 +00:00
AddProductToGrid ( tag ) ;
}
2011-12-05 09:23:02 +00:00
private void CalculateAmount ( )
2010-03-02 17:56:21 +00:00
{
txtGrossTax . Text = string . Format ( "{0:#0.00}" , bill . Values . Sum ( b = > b . TaxAmount ) ) ;
txtDiscount . Text = string . Format ( "{0:#0.00}" , bill . Values . Sum ( b = > b . DiscountAmount ) ) ;
txtGrossAmount . Text = string . Format ( "{0:#0.00}" , bill . Values . Sum ( b = > b . GrossAmount ) ) ;
txtAmount . Text = string . Format ( "{0:#0.00}" , Math . Round ( bill . Values . Sum ( b = > b . Value ) ) ) ;
bindingSource . DataSource = bill . Values . ToList ( ) ;
dgvProducts . AutoResizeColumns ( DataGridViewAutoSizeColumnsMode . DisplayedCells ) ;
}
private void btnPrintBill_Click ( object sender , EventArgs e )
{
2012-04-06 20:07:58 +00:00
var val = Save ( true ) ;
if ( ! val . HasValue )
return ;
PrintBill ( val . Value ) ;
ClearBill ( ) ;
2010-03-02 17:56:21 +00:00
}
private void btnPrintKot_Click ( object sender , EventArgs e )
{
2012-04-06 20:07:58 +00:00
var val = Save ( false ) ;
if ( ! val . HasValue )
return ;
PrintKOT ( val . Value ) ;
ClearBill ( ) ;
2010-03-02 17:56:21 +00:00
}
private void btnCancel_Click ( object sender , EventArgs e )
{
if ( bill . Count ! = 0 )
if ( MessageBox . Show ( "Cancel current bill?" , "Cancel bill" , MessageBoxButtons . YesNo , MessageBoxIcon . Question , MessageBoxDefaultButton . Button2 ) = = DialogResult . No )
return ;
ClearBill ( ) ;
}
private SalesBillItemBO CurrentProduct
{
get
{
if ( dgvProducts . Rows . Count = = 0 )
return null ;
SalesBillItemBO product = bill . ElementAt ( dgvProducts . CurrentRow . Index ) . Value ;
return product ;
}
}
private void btnQuantity_Click ( object sender , EventArgs e )
{
if ( dgvProducts . Rows . Count > 0 )
SetQuantity ( CurrentProduct , 0 , false , true ) ;
}
private void btnDiscount_Click ( object sender , EventArgs e )
{
if ( dgvProducts . Rows . Count > 0 )
SetDiscount ( CurrentProduct , - 1 ) ;
}
#endregion
private void SalesForm_Load ( object sender , EventArgs e )
{
ChangeFormState ( SaleFormState . Waiting ) ;
2011-12-05 09:23:02 +00:00
if ( _newBillID . HasValue )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
LoadBill ( _newBillID . Value ) ;
2010-03-02 17:56:21 +00:00
ChangeFormState ( SaleFormState . Billing ) ;
}
ControlFactory . GenerateButtons ( ref pnlBilling , new Rectangle ( 489 , 94 , 481 , 385 ) , 6 , 6 , 2 , new ProductBI ( ) . GetUnFilteredProducts ( ) , new ButtonClickDelegate ( button_Click ) ) ;
}
private void ChangeFormState ( SaleFormState state )
{
2011-12-05 09:23:02 +00:00
_formState = state ;
2010-03-02 17:56:21 +00:00
if ( state = = SaleFormState . Billing )
{
pnlWaiting . Visible = false ;
pnlBilling . Visible = true ;
}
else
{
pnlWaiting . Visible = true ;
pnlBilling . Visible = false ;
ListUnpaidBills ( ) ;
}
}
#region Save Bill
2011-12-05 09:23:02 +00:00
private Guid ? Save ( bool finalBill )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
decimal? amount = null ;
if ( _billInfo ! = null & & new SaleVoucherBI ( ) . IsBillPrinted ( _billInfo . VoucherID ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
if ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/EditBill" ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
MessageBox . Show ( "You are not authorized to access" ) ;
return null ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
amount = new SaleVoucherBI ( ) . Amount ( _billInfo . VoucherID ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
if ( bill . Count = = 0 )
return null ;
2010-03-02 17:56:21 +00:00
2011-12-05 09:23:02 +00:00
//
if ( _advanceID . HasValue )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var billAmount = Math . Round ( bill . Values . Sum ( b = > b . Value ) ) ;
var advanceAmount = new AdvanceBI ( ) . Get ( _advanceID . Value ) . Amount ;
if ( advanceAmount > billAmount )
{
if ( MessageBox . Show ( "Advance adjusted amount more than bill amount, advance will not be adjusted.\n\rContinue will bill print?" , "Advace adjustment error" ,
MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . No )
return null ;
else
_advanceID = null ;
}
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
var saved = _billInfo = = null ? AddNewSale ( finalBill ) : UpdateSale ( finalBill ) ;
if ( saved . HasValue & & _advanceID . HasValue )
new AdvanceBI ( ) . Adjust ( _advanceID . Value , CurrentUser . user . UserID ) ;
if ( amount ! = null )
Log . Warn ( string . Format ( "{4} modified bill no {0}/{1} from (Rs. {2:#,00}) to (Rs. {3:#,00})" , _billInfo . BillID , _billInfo . TableID , amount . Value , new SaleVoucherBI ( ) . Amount ( saved . Value ) , Thread . CurrentPrincipal . Identity . Name ) ) ;
if ( _newBillID . HasValue )
this . Close ( ) ;
return saved ;
}
private void PrintBill ( Guid voucherID )
{
if ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/PrintBill" ) )
MessageBox . Show ( "You are not authorized to access" ) ;
2010-03-02 17:56:21 +00:00
else
{
2011-12-05 09:23:02 +00:00
Print . Thermal . PrintBill ( voucherID ) ;
Print . Thermal . PrintCustomerKot ( "KOT" , voucherID , bill . Values . ToList ( ) ) ;
2010-03-02 17:56:21 +00:00
}
}
2011-12-05 09:23:02 +00:00
private void PrintKOT ( Guid voucherID )
{
if ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/PrintKOT" ) )
MessageBox . Show ( "You are not authorized to access" ) ;
else
Print . Thermal . PrintWaiterKot ( "KOT" , voucherID , bill . Values . ToList ( ) ) ;
}
#region Save / Update
2010-03-02 17:56:21 +00:00
private Guid ? AddNewSale ( bool finalBill )
{
2011-12-05 09:23:02 +00:00
if ( _billInfo ! = null )
2010-03-02 17:56:21 +00:00
{
MessageBox . Show ( "Error in AddNewSale, there is a previous sale in memory" , "Error" ) ;
return null ;
}
if ( btnWaiter . Tag = = null )
btnWaiter . Tag = new WaiterBI ( ) . GetWaiters ( ) [ 0 ] . WaiterID ;
#region SaleVoucher
2011-12-05 09:23:02 +00:00
var user = CurrentUser . user ;
var saleVoucher = new SaleVoucherBO
2010-03-02 17:56:21 +00:00
{
CustomerID = customer . CustomerID ,
2011-12-05 09:23:02 +00:00
PaidStatus = PaidStatus . Pending ,
2010-03-02 17:56:21 +00:00
//Paid = finalBill,
TableID = txtTableID . Text ,
WaiterID = ( Guid ) btnWaiter . Tag ,
Printed = finalBill ,
Date = DateTime . Now ,
Narration = txtNarration . Text ,
Ref = "" ,
Type = 'S' ,
2011-12-05 09:23:02 +00:00
UserID = user . UserID ,
Floor = _floor ,
AdvanceID = _advanceID
2010-03-02 17:56:21 +00:00
} ;
#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 )
{
if ( btnWaiter . Tag = = null )
btnWaiter . Tag = new WaiterBI ( ) . GetWaiters ( ) [ 0 ] . WaiterID ;
2011-12-05 09:23:02 +00:00
var user = CurrentUser . user ;
2010-03-02 17:56:21 +00:00
#region Voucher and SaleVoucher
2011-12-05 09:23:02 +00:00
var saleVoucher = new SaleVoucherBO
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
VoucherID = _billInfo . VoucherID ,
timestamp = _billInfo . timestamp ,
UserID = _billInfo . UserID ,
Date = _billInfo . Date ,
2010-03-02 17:56:21 +00:00
CreationDate = DateTime . Now ,
LastEditDate = DateTime . Now ,
2011-12-05 09:23:02 +00:00
Narration = _billInfo . Narration ,
2010-03-02 17:56:21 +00:00
Ref = "" ,
Type = 'S' ,
2011-12-05 09:23:02 +00:00
Alarm = _billInfo . Alarm ,
BillID = _billInfo . BillID ,
2010-03-02 17:56:21 +00:00
CustomerID = customer . CustomerID ,
2011-12-05 09:23:02 +00:00
KotID = _billInfo . KotID ,
PaidStatus = _billInfo . PaidStatus ,
Printed = _billInfo . Printed | | finalBill ,
2010-03-02 17:56:21 +00:00
TableID = txtTableID . Text ,
2011-12-05 09:23:02 +00:00
VoidReason = _billInfo . VoidReason ,
2010-03-02 17:56:21 +00:00
WaiterID = ( Guid ) btnWaiter . Tag ,
2011-12-05 09:23:02 +00:00
SaleTimestamp = _billInfo . SaleTimestamp ,
Floor = _floor ,
AdvanceID = _advanceID
2010-03-02 17:56:21 +00:00
} ;
2011-12-05 09:23:02 +00:00
if ( ( ! _billInfo . Printed ) & & finalBill )
2010-03-02 17:56:21 +00:00
saleVoucher . Date = null ;
#endregion
#region Inventory
2011-12-05 09:23:02 +00:00
List < InventoryBO > iList = new SaleVoucherBI ( ) . SaleInventory ( bill . Values , _billInfo . VoucherID ) ;
2010-03-02 17:56:21 +00:00
#endregion
new SaleVoucherBI ( ) . Update ( saleVoucher , iList ) ;
return saleVoucher . VoucherID ;
}
#endregion
#endregion
private void LoadBillFromTable ( )
{
InputBoxResult result = InputBox . Show ( "Enter Table Number" , "Table" , "0" , InputBox_Validating ) ;
if ( result . OK )
{
txtTableID . Text = result . Text . Trim ( ) ;
if ( ( txtTableID . Text ! = "C" ) & & ( txtTableID . Text ! = "" ) & & ( ! txtTableID . Text . Contains ( "." ) ) )
{
2011-12-05 09:23:02 +00:00
Guid ? tID = new SaleVoucherBI ( ) . GetPendingVoucherID ( txtTableID . Text , _floor ) ;
2010-03-02 17:56:21 +00:00
if ( tID . HasValue )
{
LoadBill ( tID . Value ) ;
ChangeFormState ( SaleFormState . Billing ) ;
}
}
else
ClearBill ( ) ;
}
}
private void LoadBill ( Guid voucherID )
{
ClearBill ( ) ;
2011-12-05 09:23:02 +00:00
var iList = new List < InventoryDisplayBO > ( ) ;
new SaleVoucherBI ( ) . GetSaleVoucher ( voucherID , ref _billInfo , ref iList ) ;
this . txtBillID . Text = _billInfo . BillID ;
this . txtKotID . Text = _billInfo . KotID ;
this . txtCreationDate . Text = _billInfo . CreationDate . ToString ( "HH:mm dd-MMM-yyyy" ) ;
this . txtDate . Text = _billInfo . Date . Value . ToString ( "HH:mm dd-MMM-yyyy" ) ;
this . txtLastEditDate . Text = _billInfo . LastEditDate . ToString ( "HH:mm dd-MMM-yyyy" ) ;
this . txtNarration . Text = _billInfo . Narration ;
this . txtUserID . Text = new UserBI ( ) . GetUser ( _billInfo . UserID ) . Name ;
this . customer = new CustomerBI ( ) . GetCustomer ( _billInfo . CustomerID ) ;
2010-03-02 17:56:21 +00:00
this . btnCustomer . Text = this . customer . Name ;
2011-12-05 09:23:02 +00:00
this . txtTableID . Text = _billInfo . TableID ;
this . btnWaiter . Tag = _billInfo . WaiterID ;
this . _advanceID = _billInfo . AdvanceID ;
this . btnWaiter . Text = string . Format ( "{0} - F5" , new WaiterBI ( ) . GetWaiter ( _billInfo . WaiterID ) . Name ) ;
2010-03-02 17:56:21 +00:00
2011-12-05 09:23:02 +00:00
foreach ( var inventory in iList )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var key = new BillItemKey ( inventory . ProductID , inventory . Quantity = = 0 ) ;
bill . Add ( key , new SalesBillItemBO
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
productID = inventory . ProductID ,
Discount = inventory . Discount ,
Name = inventory . ProductName ,
Price = inventory . Rate ,
Printed = inventory . Quantity ,
Quantity = inventory . Quantity ,
Tax = inventory . Tax ,
} ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
}
#region Waiting
private void btnStartBill_Click ( object sender , EventArgs e )
{
ChangeFormState ( SaleFormState . Billing ) ;
}
private void btnSelectBill_Click ( object sender , EventArgs e )
{
if ( bsPending . Current ! = null )
{
LoadBill ( ( ( PendingBillsBO ) bsPending . Current ) . voucherID ) ;
ChangeFormState ( SaleFormState . Billing ) ;
}
}
private void tmrPending_Tick ( object sender , EventArgs e )
{
if ( chkRefresh . Checked )
ListUnpaidBills ( ) ;
}
private void ListUnpaidBills ( )
{
lock ( lockObject )
{
2011-12-05 09:23:02 +00:00
pendingBills = new SaleVoucherBI ( ) . GetPendingBills ( pendingList , _floor ) ;
2010-03-02 17:56:21 +00:00
bsPending . DataSource = pendingBills ;
2011-12-05 09:23:02 +00:00
var alarmBills = new SaleVoucherBI ( ) . GetPendingBills ( PendingType . Alarms , _floor ) . OrderBy ( b = > b . AlarmTime ) . ToList ( ) ;
2010-03-02 17:56:21 +00:00
if ( alarmBills . Count > 0 )
{
2011-12-05 09:23:02 +00:00
var al = alarmBills . First ( ) ;
var seconds = al . AlarmTime . Value . Subtract ( DateTime . Now ) . TotalSeconds ;
lblUser . Text = seconds < = 0 ? string . Format ( "Alarm {0:hh:MM}" , al . AlarmTime ) : CurrentUser . user . Name ;
2010-03-02 17:56:21 +00:00
}
else
{
lblUser . Text = CurrentUser . user . Name ;
}
}
}
private void dgvPending_CellFormatting ( object sender , DataGridViewCellFormattingEventArgs e )
{
try
{
DataGridView dgv = sender as DataGridView ;
PendingBillsBO data = dgv . Rows [ e . RowIndex ] . DataBoundItem as PendingBillsBO ;
if ( data . Printed )
e . CellStyle . BackColor = Color . LightSlateGray ;
}
catch
{
// Catch and swallow exception when DataGridView attemps to get values for removed rows.
}
}
private void tcPending_SelectedIndexChanged ( object sender , EventArgs e )
{
switch ( tcPending . SelectedTab . Text )
{
case "Today" :
pendingList = PendingType . Today ;
break ;
case "Week" :
pendingList = PendingType . Week ;
break ;
case "All" :
pendingList = PendingType . All ;
break ;
case "Alarms" :
pendingList = PendingType . Alarms ;
break ;
}
ListUnpaidBills ( ) ;
}
private void dgvPending_CellValuePushed ( object sender , DataGridViewCellValueEventArgs e )
{
if ( e . ColumnIndex ! = 0 ) return ;
if ( ( bool ) e . Value )
selectedBills . Add ( pendingBills [ e . RowIndex ] . voucherID ) ;
else
selectedBills . Remove ( pendingBills [ e . RowIndex ] . voucherID ) ;
}
private void dgvPending_CellValueNeeded ( object sender , DataGridViewCellValueEventArgs e )
{
if ( e . ColumnIndex ! = 0 ) return ;
if ( e . RowIndex > pendingBills . Count - 1 ) return ;
e . Value = selectedBills . Contains ( pendingBills . ElementAt ( e . RowIndex ) . voucherID ) ;
}
#endregion
#region Billing
private void btnCustomer_Click ( object sender , EventArgs e )
{
ShowCustomerList ( false ) ;
}
private 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 ;
btnCustomer . Text = customer . Name ;
}
else
{
customer = new CustomerBI ( ) . GetCustomer ( new Guid ( "F016CBAD-206C-42C0-BB1D-6006CE57BAB5" ) ) ;
}
}
}
2011-12-05 09:23:02 +00:00
else if ( ! reset )
{
using ( CustomersForm form = new CustomersForm ( customer . CustomerID , customer . Phone ) )
{
form . ShowDialog ( ) ;
}
}
2010-03-02 17:56:21 +00:00
else
{
customer = new CustomerBI ( ) . GetCustomer ( new Guid ( "F016CBAD-206C-42C0-BB1D-6006CE57BAB5" ) ) ;
btnCustomer . Text = customer . Name ;
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
}
}
CustomerBO selectCustomer_customerEvent ( object sender , CustomerEventArgs e )
{
using ( CustomersForm form = new CustomersForm ( e . CustomerID , e . Phone ) )
{
form . ShowDialog ( ) ;
return form . Customer ;
}
}
#endregion
private void dgvPending_CellDoubleClick ( object sender , DataGridViewCellEventArgs e )
{
if ( ( dgvProducts . Rows . Count > 0 ) & & ( dgvPending . CurrentRow ! = null ) )
{
LoadBill ( ( ( PendingBillsBO ) bsPending . Current ) . voucherID ) ;
ChangeFormState ( SaleFormState . Billing ) ;
}
}
private void btnRefresh_Click ( object sender , EventArgs e )
{
ListUnpaidBills ( ) ;
}
private void btnVoid_Click ( object sender , EventArgs e )
{
2011-12-05 09:23:02 +00:00
if ( _billInfo ! = null )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
if ( ( _billInfo . Printed ) & & ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/VoidPrintedBill" ) ) )
2010-03-02 17:56:21 +00:00
MessageBox . Show ( "Cannot void a paid bill" ) ;
else 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 )
{
2013-11-16 06:01:58 +00:00
new SaleVoucherBI ( ) . VoidBill ( _billInfo . VoucherID , voidReason . SelectedItem . Description , CurrentUser . user . UserID ) ;
2010-03-02 17:56:21 +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 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 ( ) ;
}
private void btnRate_Click ( object sender , EventArgs e )
{
if ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/ChangeRate" ) )
MessageBox . Show ( "You are not authorized to access" ) ;
else
{
if ( dgvProducts . Rows . Count > 0 )
{
SalesBillItemBO product = bill . ElementAt ( dgvProducts . CurrentRow . 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 ) ;
2011-12-05 09:23:02 +00:00
CalculateAmount ( ) ;
2010-03-02 17:56:21 +00:00
}
}
}
}
private void btnClear_Click ( object sender , EventArgs e )
{
ClearBill ( ) ;
}
private void btnPaidCash_Click ( object sender , EventArgs e )
{
2011-12-05 09:23:02 +00:00
var user = CurrentUser . user ;
new SaleVoucherBI ( ) . DeclareBillsPaid ( user . UserID , selectedBills , PaidStatus . Cash ) ;
2010-03-02 17:56:21 +00:00
ListUnpaidBills ( ) ;
}
private void btnPaidCC_Click ( object sender , EventArgs e )
{
2011-12-05 09:23:02 +00:00
var user = CurrentUser . user ;
new SaleVoucherBI ( ) . DeclareBillsPaid ( user . UserID , selectedBills , PaidStatus . CreditCard ) ;
2010-03-02 17:56:21 +00:00
ListUnpaidBills ( ) ;
}
private void btnAlarm_Click ( object sender , EventArgs e )
{
if ( bsPending . Current ! = null )
{
PendingBillsBO billAlarm = ( PendingBillsBO ) bsPending . Current ;
InputBoxResult result = InputBox . Show (
string . Format ( "Alarm for Bill {0} Rs. {1}" , billAlarm . BillNo , billAlarm . Amount ) ,
"Alarm" ,
string . Format ( "{0:dd-MMM-yyy HH:mm}" , billAlarm . LastEdited ) ,
InputBox_Validating ) ;
if ( result . OK )
{
DateTime alarmTime ;
if ( result . Text = = string . Empty )
{
new SaleVoucherBI ( ) . SetAlarm ( billAlarm . voucherID , null ) ;
MessageBox . Show ( "Alarm Cleared" , "Alarm Cleared" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else if ( DateTime . TryParseExact ( result . Text , "dd-MMM-yyyy HH:mm" , new CultureInfo ( "en-US" ) , DateTimeStyles . None , out alarmTime ) )
{
new SaleVoucherBI ( ) . SetAlarm ( billAlarm . voucherID , alarmTime ) ;
MessageBox . Show ( "Alarm set" , "Alarm Set" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
MessageBox . Show ( "Alarm NOT set, please try again" , "Alarm NOT Set" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
}
private void btnBillList_Click ( object sender , EventArgs e )
{
2012-04-06 20:07:58 +00:00
if ( ! Thread . CurrentPrincipal . IsInRole ( "Security/CreateUser" ) )
return ;
var result = InputBox . Show ( "Bill No" , "Bill No" , "" , InputBox_Validating ) ;
if ( result . OK )
{
var saleVoucher = new SaleVoucherBO ( ) ;
var list = new List < InventoryDisplayBO > ( ) ;
new SaleVoucherBI ( ) . GetSaleVoucher ( result . Text , ref saleVoucher , ref list ) ;
if ( saleVoucher = = null )
return ;
LoadBill ( saleVoucher . VoucherID ) ;
ChangeFormState ( SaleFormState . Billing ) ;
}
2010-03-02 17:56:21 +00:00
//#region Filters
//decimal? minValue = 0, maxValue = 0;
//decimal valTemp;
//DateTime? fromDate = DateTime.Now, toDate = DateTime.Now; // used in filters
//DateTime dateTemp;
//bool? showVoided;
//InputBoxResult result = InputBox.Show("Start Date", "Start Date", string.Format("{0:dd-MMM-yyy}", fromDate), InputBox_Validating);
//if ((result.OK) && (DateTime.TryParseExact(result.Text, "dd-MMM-yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out dateTemp)))
//{
// fromDate = dateTemp;
//}
//result = InputBox.Show("Finish Date", "Finish Date", string.Format("{0:dd-MMM-yyy}", toDate), InputBox_Validating);
//if ((result.OK) && (DateTime.TryParseExact(result.Text, "dd-MMM-yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out dateTemp)))
//{
// toDate = dateTemp;
//}
//result = InputBox.Show("Minimum Value", "Minimum Value", "0", InputBox_Validating);
//if ((result.OK) && (decimal.TryParse(result.Text, out valTemp)))
//{
// minValue = valTemp;
//}
//result = InputBox.Show("Maximum Value", "Maximum Value", "0", InputBox_Validating);
//if ((result.OK) && (decimal.TryParse(result.Text, out valTemp)))
//{
// maxValue = valTemp;
//}
//DialogResult dResult = MessageBox.Show("Show Un-Voided Bills only", "Un-Voided", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
//if (dResult == DialogResult.Yes)
// showVoided = false;
//else if (dResult == DialogResult.No)
// showVoided = true;
//else
// showVoided = null;
//#endregion
//List<PendingBillsBO> billList = ManagementBI.GetBillList(fromDate, toDate, minValue, maxValue, showVoided);
//using (SelectBill selectBill = new SelectBill(billList, true))
//{
// selectBill.ShowDialog();
// if (selectBill.SelectedItem != null)
// {
// LoadBill(selectBill.SelectedItem.voucherID);
// ChangeFormState(SaleFormState.Billing);
// }
//}
}
private void dgvProducts_CellFormatting ( object sender , DataGridViewCellFormattingEventArgs e )
{
DataGridView dgv = sender as DataGridView ;
SalesBillItemBO data = dgv . Rows [ e . RowIndex ] . DataBoundItem as SalesBillItemBO ;
if ( data . Printed > 0 )
{
e . CellStyle . SelectionBackColor = Color . HotPink ;
e . CellStyle . BackColor = Color . LightPink ;
}
else
{
e . CellStyle . SelectionBackColor = Color . Green ;
e . CellStyle . BackColor = Color . LightGreen ;
}
}
private void btnWaiter_Click ( object sender , EventArgs e )
{
using ( SelectWaiter selectWaiter = new SelectWaiter ( new WaiterBI ( ) . GetFilteredWaiters , true ) )
{
selectWaiter . waiterEvent + = new WaiterEventHandler ( selectWaiter_waiterEvent ) ;
selectWaiter . ShowDialog ( ) ;
if ( selectWaiter . SelectedItem ! = null )
{
btnWaiter . Text = string . Format ( "{0} - F5" , selectWaiter . SelectedItem . Name ) ;
btnWaiter . Tag = selectWaiter . SelectedItem . WaiterID ;
}
else
{
btnWaiter . Text = "Select Waiter - F5" ;
btnWaiter . Tag = new WaiterBI ( ) . GetWaiters ( ) [ 0 ] . WaiterID ;
}
}
}
WaiterBO selectWaiter_waiterEvent ( object sender , WaiterEventArgs e )
{
WaiterBO waiter = e . Waiter ;
if ( ! Thread . CurrentPrincipal . IsInRole ( "Waiter/Master" ) )
return waiter ;
switch ( e . Action )
{
case 1 : // Add
new WaiterBI ( ) . Insert ( waiter ) ;
e . Handled = true ;
return waiter ;
case 2 : // Edit
new WaiterBI ( ) . Update ( waiter ) ;
e . Handled = true ;
return waiter ;
case 3 : // Delete
e . Handled = new WaiterBI ( ) . Delete ( waiter . WaiterID ) ;
return new WaiterBI ( ) . GetWaiter ( 1 ) ;
default :
throw new ArgumentException ( ) ;
}
}
2011-12-05 09:23:02 +00:00
private void btnResetCustomer_Click ( object sender , EventArgs e )
{
ShowCustomerList ( true ) ;
}
private void btnPaidStaff_Click ( object sender , EventArgs e )
{
var user = CurrentUser . user ;
new SaleVoucherBI ( ) . DeclareBillsPaid ( user . UserID , selectedBills , PaidStatus . Staff ) ;
ListUnpaidBills ( ) ;
}
private void btnPaidCredit_Click ( object sender , EventArgs e )
{
var user = CurrentUser . user ;
new SaleVoucherBI ( ) . DeclareBillsPaid ( user . UserID , selectedBills , PaidStatus . Credit ) ;
ListUnpaidBills ( ) ;
}
private void btnAdvance_Click ( object sender , EventArgs e )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
if ( _billInfo ! = null & & _billInfo . AdvanceID . HasValue )
return ;
using ( var frm = new AdjustAdvanceForm ( true ) )
{
frm . ShowDialog ( ) ;
_advanceID = frm . AdvanceID ;
}
2010-03-02 17:56:21 +00:00
}
}
}