2010-03-02 17:56:21 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using Tanshu.Common ;
using Tanshu.Accounts.BI ;
using Tanshu.Accounts.Contracts ;
using System.Windows.Forms ;
using System.Threading ;
namespace Tanshu.Accounts.PointOfSale
{
2011-01-17 14:55:43 +00:00
public enum SettleOptions
{
Cash = 1 ,
CreditCard = 2 ,
NoCharge = 3 ,
Cancel = 4
}
2010-03-02 17:56:21 +00:00
public static class BillHelperFunctions
{
#region Discount
public static void SetDiscount ( Guid productID , decimal discount , CustomerBO customer , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
#region InputBox
if ( discount = = - 1 )
{
InputBoxResult result = InputBox . Show ( "Discount Rate" , "Discount" , "0" , InputBox_Validating ) ;
if ( result . OK )
{
if ( ! decimal . TryParse ( result . Text , out discount ) )
return ;
discount / = 100 ;
}
}
if ( discount = = - 1 )
return ;
#endregion
#region Max Discount
decimal maxDiscount = new SaleVoucherBI ( ) . GetProductDiscountLimit ( productID ) ;
if ( ( discount > maxDiscount ) & & customer . CustomerID ! = new Guid ( "F016CBAD-206C-42C0-BB1D-6006CE57BAB5" ) )
{
MessageBox . Show ( string . Format ( "Maximum discount for this product is {0:P}" , maxDiscount ) , "Excessive Discount" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
}
else if ( ( discount > maxDiscount ) & & customer . CustomerID = = new Guid ( "F016CBAD-206C-42C0-BB1D-6006CE57BAB5" ) )
{
MessageBox . Show ( string . Format ( "Maximum discount for this product is {0:P} Discount Disallowed" , maxDiscount ) , "Excessive Discount" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
return ;
}
#endregion
if ( bill . ContainsKey ( new BillItemKey ( productID , true ) ) )
SetDiscount ( bill [ new BillItemKey ( productID , true ) ] , discount ) ;
if ( bill . ContainsKey ( new BillItemKey ( productID , false ) ) )
SetDiscount ( bill [ new BillItemKey ( productID , false ) ] , discount ) ;
return ;
}
private static void SetDiscount ( SalesBillItemBO product , decimal discount )
{
product . Discount = discount ;
}
#endregion
#region Add Product
public static SalesBillItemBO AddProductToGrid ( Guid productID , BindingSource bindingSource , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
SalesBillItemBO product ;
if ( ( ! bill . ContainsKey ( new BillItemKey ( productID , true ) ) ) & & ( ! bill . ContainsKey ( new BillItemKey ( productID , false ) ) ) )
{
//No new or old
product = AddNewProduct ( productID , bindingSource , bill ) ;
}
else if ( bill . ContainsKey ( new BillItemKey ( productID , true ) ) )
{
//Has new or both
BillItemKey key = new BillItemKey ( productID , true ) ;
bindingSource . CurrencyManager . Position = ProductPosition ( key , bill ) ;
product = bill [ key ] ;
SetQuantity ( product , 1 , false ) ;
}
else
{
//Has only old
product = bill [ new BillItemKey ( productID , false ) ] ;
if ( product . Additional < = - 1 )
SetQuantity ( product , 1 , false ) ;
else if ( product . Additional < 0 )
{
decimal quantity = 1 + product . Additional ;
SetQuantity ( product , 1 , false ) ;
decimal rate = bill [ new BillItemKey ( productID , false ) ] . Discount ;
decimal discount = bill [ new BillItemKey ( productID , false ) ] . Price ;
product = AddNewProduct ( productID , bindingSource , bill ) ;
SetDiscount ( product , discount ) ;
SetRate ( productID , rate , bill ) ;
SetQuantity ( product , quantity , true ) ;
}
else
{
decimal discount = bill [ new BillItemKey ( productID , false ) ] . Discount ;
decimal rate = bill [ new BillItemKey ( productID , false ) ] . Price ;
product = AddNewProduct ( productID , bindingSource , bill ) ;
SetDiscount ( product , discount ) ;
SetRate ( productID , rate , bill ) ;
}
}
return product ;
}
private static SalesBillItemBO AddNewProduct ( Guid productID , BindingSource bindingSource , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
BillItemKey key = new BillItemKey ( productID , true ) ;
SalesBillItemBO product = new SaleVoucherBI ( ) . GetDefaultSaleBillItem ( productID ) ;
product . Quantity = 1 ;
bill . Add ( key , product ) ;
bindingSource . DataSource = bill . Values ;
bindingSource . CurrencyManager . Position = bindingSource . CurrencyManager . Count + 1 ;
return product ;
}
private static int ProductPosition ( BillItemKey key , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
for ( int i = 0 ; i < bill . Count ; i + + )
{
if ( bill . Keys . ElementAt ( i ) = = key )
return i ;
}
return 0 ;
}
#endregion
#region Quantity
public static void SetQuantity ( SalesBillItemBO product , decimal quantity , bool absolute , bool prompt , BindingSource bindingSource , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
#region Prompt
if ( prompt )
{
InputBoxResult result = InputBox . Show ( "Enter Quantity" , "Quantity" , ( product . Quantity + 1 ) . ToString ( ) , InputBox_Validating ) ;
if ( result . OK )
{
if ( ! decimal . TryParse ( result . Text , out quantity ) )
return ;
absolute = true ;
}
}
if ( quantity = = 0 )
return ;
#endregion
CheckQuantity ( product , quantity , absolute ) ;
if ( product . Printed = = 0 )
{
SetQuantity ( product , quantity , absolute ) ;
}
else if ( bill . ContainsKey ( new BillItemKey ( product . productID , true ) ) )
{
SalesBillItemBO otherProduct = bill [ new BillItemKey ( product . productID , true ) ] ;
if ( absolute )
SetQuantity ( otherProduct , quantity - product . Printed , absolute ) ;
else
SetQuantity ( otherProduct , quantity , absolute ) ;
}
else
{
if ( product . Additional < 0 )
{
if ( ! absolute )
quantity + = product . Additional ;
product . Quantity = product . Printed ;
}
if ( absolute )
quantity - = product . Quantity ;
if ( quantity > 0 )
{
SalesBillItemBO otherProduct = AddProductToGrid ( product . productID , bindingSource , bill ) ;
SetQuantity ( otherProduct , quantity , true ) ;
}
else if ( ( quantity < 0 ) & & ( Thread . CurrentPrincipal . IsInRole ( "Sales/EditPrintedProduct" ) ) )
{
SetQuantity ( product , quantity , false ) ;
}
}
}
private static bool CheckQuantity ( SalesBillItemBO product , decimal quantity , bool absolute )
{
if ( ! absolute )
quantity = product . Quantity + quantity ;
if ( quantity < 0 )
return false ;
else if ( ( quantity < product . Printed ) & & ( ! Thread . CurrentPrincipal . IsInRole ( "Sales/EditPrintedProduct" ) ) )
return false ;
else
return true ;
}
private static void SetQuantity ( SalesBillItemBO product , decimal quantity , bool absolute )
{
if ( ! absolute )
{
quantity = product . Quantity + quantity ;
}
if ( quantity < = 0 )
return ;
product . Quantity = quantity ;
}
#endregion
#region Amount
public static void SetAmount ( SalesBillItemBO product , decimal amount , BindingSource bindingSource , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
if ( amount = = - 1 )
{
InputBoxResult result = InputBox . Show ( "Enter Amount" , "Amount" , ( product . Value ) . ToString ( ) , InputBox_Validating ) ;
if ( result . OK )
{
amount = Convert . ToDecimal ( result . Text ) ;
}
}
if ( amount = = - 1 )
return ;
else
{
SetQuantity ( product , amount / ( product . Price * ( 1 + product . Tax ) * ( 1 - product . Discount ) ) , true , false , bindingSource , bill ) ;
}
}
#endregion
#region Rate
public static void SetRate ( Guid productID , decimal rate , Dictionary < BillItemKey , SalesBillItemBO > bill )
{
if ( bill . ContainsKey ( new BillItemKey ( productID , true ) ) )
bill [ new BillItemKey ( productID , true ) ] . Price = rate ;
if ( bill . ContainsKey ( new BillItemKey ( productID , false ) ) )
bill [ new BillItemKey ( productID , false ) ] . Price = rate ;
}
#endregion
private static void InputBox_Validating ( object sender , InputBoxValidatingArgs e )
{
}
}
}