Adding CR/DR to accounting transactions - sql-server

I have 7 tables of COA as show in the picture attached. what I need is whenever opening balance goto - (negative) , the type must automatically show CR sign and whenever that account go to positive, it must change to DR automatically. The account will change to DR or CR as per entries. Suppose I am doing entry in cash payment and if cash in hand account is Debit and utilities are CR in Chart of account, and if I pass an entry then and make utilities debit then in chart of accounts it must change from CR to DR in type column.
Also my accountant do not want to see (-) sign with negative he need type should change as per entries.
Let me explain more. Suppose I have 2 accounts (cash in hand) and (Utility Bills). The opening balance of both accounts in Chart of Accounts is:
Cash in Hand = 5000 DR
Utility Bills = 2000 CR
Now if I do an entry in cash payment so it will be
utility bills (DR) = 8000
cash in hand (CR) = 8000
Now after I do this entry the Chart of accounts must look like
Cash in hand = 3000 CR (because it is in negative due to I had only 5000) (now here the balance must show like -3000) but instead of (-) it showing 3000 and DR change to CR.
This is what I need to do:
Error after code

I guess you should keep the value as it is in database else you need to maintain another column to maintain account status anyway you have CR/DR and I am sure you kept it as boolean.
To convert value to absolute without any sign while displaying you can use below trick. To add CR/DR you have iterate through cells
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "YourAmountColumnName")) <0 Then
'Below line will remove (-) sign'
amt = amt * -1
e.Row.Cells(n).Text = "CR"
Else
e.Row.Cells(n).Text = "DR"
End If
End If
End Sub
It will return amount without any sign, If it is less than 0 obviously it is credit else it is debit.
In MSSQL you can use case for same, You have to check if amount is going negative
Type = CASE WHEN OpeningBalance <0 THEN Type="CR" ELSE Type="DR"
OpeningBalance = CASE WHEN OpeningBalance <0 THEN OpeningBalance= OpeningBalance*-1

Related

VBA - Validate quantity including the field being edited

Setup: MS Access + SQL Server 2019.
I have Purchase Orders which have to be Delivered. For example:
PurchaseOrderDetailID
QtyOrdered
1
10
DeliveryID
DateShipped
DateReceived
1
1.1.2022
2.1.2022
2
1.2.2022
2.2.2022
DeliveryDetailID
DeliveryID
PurchaseOrderDetailID
QtyDelivered
1
1
1
5
2
2
1
3
This means that there are 10 items ordered, 5 items have been delivered in the first Delivery, 3 items in the second one, therefore 8 delivered in total.
Meaning that this Purchase Order is partially delivered at the moment.
I have a Delivery Data Entry Form in MS Access (back end is SQL Server). I want to validate the quantity so that TotalQtyDelivered cannot be more than TotalQtyOrdered.
Private Sub txtQuantity_BeforeUpdate(Cancel As Integer)
Dim QtyOrdered, QtyDelivered, LeftToDeliver As Double
QtyOrdered = DLookup("QtyOrdered", "dbo_v_PurchaseOrderItemStatus", "PurchaseOrderDetailID=" & Me.PurchaseOrderDetailID)
QtyDelivered = DLookup("QtyDelivered", "dbo_v_PurchaseOrderItemStatus", "PurchaseOrderDetailID=" & Me.PurchaseOrderDetailID)
LeftToDeliver = QtyOrdered - QtyDelivered
If Me.txtQuantity > LeftToDeliver Then
MsgBox "TOO MUCH"
Me.Undo
Cancel = True
End If
End Sub
This works fine if I'm entering a new Delivery. But if I am editing an existing delivery, the algorithm accounts also for the previous value in the Qty field.
With the example above:
QtyOrdered = 10
QtyDelivered = 8
(this is calculated by SQL View)
Let's say that I made a mistake and there were actually 4 items delivered in the second delivery, istead of 3. When I try to change the value, this happens:
Me.txtQuantity = 4
LeftToDeliver = 2
(because it still accounts for the "3" previously inserted in Me.txtQuantity
Result: Operation failed, too many items. However in reality there should only be 9 items.
Can you please advise how I should do this correctly? Thanks.

Average of a player

I have a record that contains stat for a certain cricket player.
It has columns having dates, oppositions, Runs, Balls, Dismissals, Match_Number.
I want to do a query (SQL SERVER) to find out the batting average where every runs (Sum) is to be added; innings having a count of all innings except DNB but dismissal should not have a count of "Not Out", "Retired Hurt", "DNB" grouped by the opposition.
Note : DNB means Did not Bat.
The query doesn't have the required number of innings to calculate the average
So the problem is can't gather information for a single entity (count of no. of innings) having two set of parameters.
Without DNB
Without DNB, Not Out, Retired Hurt.
Please suggest.
You can put a case expression within an aggregate to exclude certain rows from a count/sum/average etc. So you could use something like this:
SELECT a.Opposition,
Matches = COUNT(*),
Innings = COUNT(CASE WHEN a.Dismissal <> 'DNB' THEN 1 END),
Runs = SUM(a.Runs),
Average = SUM(a.Runs) / NULLIF(COUNT(CASE WHEN a.Dismissal NOT IN ('DNB', 'Not Out', 'Retired not out') THEN 1 END), 0)
FROM dbo.SRTundlkarODI AS a
GROUP BY a.Opposition;
N.B. I have wrapped the COUNT for the average in NULLIF(<exp>, 0) so that should the batsmen have never got out you avoid a divide by zero error.

How to make running total for datagridview with multiple conditions?

I go this problem on how to make a running total in a datagridview with a multiple conditions.
I had this table compose of columns 'Bal' that makes the running total and the columns 'Balance' that copies the amount of fees if the payment is enough, if you would mind to look at the image below for your reference:
Here is the case: if the amount of payment is enough to pay the values in the values under column Amount, then it will copy the values of column Amount and paste it into column Balance.
But if not enough to pay the amount of fee to the next row, It will show the difference under columns Balance.
I have tried this code but, it does not meet the required condition, although it works fine.
Dim Bal As Decimal = 0
Dim i As Integer
With dgvSTSub
.Rows(i).Cells("Bal").Value = CurrencyTextBoxAmount.Text
End With
For Each r As DataGridViewRow In dgvSTSub.Rows
Bal = Bal + r.Cells(3).Value - r.Cells(1).Value
r.Cells(3).Value = Bal
If r.Cells(1).Value > r.Cells(2).Value Then
r.Cells(2).Value = r.Cells(1).Value
ElseIf Bal < 0 Then
r.Cells(2).Value = Bal - bal - 1
End If
Next
Please help me with this, I'm really stuck here.

Sum of cost amount calculation

I have attached the screenshot. Most probably that the image itself will explain my logic. Let me paste the code I have used in the fields. Product Info1 field which hold Y & N.
RemainingCosu2=If(Invoice Line Items::Product Info1 = GetAsText ("N"); Sum(Cost Total) - Invoice Line Items::VendPaid_total;0)
RemainingCosu1=If(Vendor Status="Partly Paid"; RemainingCosu2; 0)
What should I do to fix this issue?. Please check the screenshot:
Filemaker has no SumIf() function. You need to create a calculation field in the LineItems table, along the lines of:
If ( Paid = "N" ; Cost )
then sum this field at the invoice level (and/or summarize it at the LineItems table itself), instead of the Cost field.
--
BTW, it is much more convenient to define Boolean (yes/no) fields as Number and use the values of 1 for True, 0 (or empty) for False. Then the calculation can be simply:
If ( not Paid ; Cost )

MATLAB receipt print random values issue

I have a project where I must make the following;
You have a small business and you sell 6 different products. Choose your products
and their prices within the range of 20p to £25.00 (these could be completely fictitious). Your
shop has 4 employees, one of whom will be at the till at the time of purchase.
Your task is to write MATLAB code to prepare a receipt for a fictitious transaction as explained
below.
There is a customer at the till. They want to purchase 3 random products with specific
quantities for each. For example, the customer wants 2 cappuccinos, 1 croissant and 6 raspberry
muffins.
(1) Select randomly 3 products from your list. For each product choose a random quantity
between 1 and 9.
(2) Calculate the total cost.
(3) Choose randomly the staff member to complete the transaction.
(4) Suppose that the price includes 20% VAT. Calculate the amount of VAT included in the price.
(6) Prepare the receipt as text in the MATLAB command window. Use the current date and time
(check datestr(now,0)).
Your code should output the receipt in the format shown in the picture. There should be
60 symbols across. Choose our own shop name.
My code so far is the following:
clear all
clc
close all
items = {'apples ','carrots ','tomatoes','lemons ','potatoes','kiwis '};% products
price = {3.10, 1.70, 4.00, 1.65, 9.32, 5.28};% item prices. I set spaces for each entry in order to maintain the border format.
employee = {'James','Karina','George','Stacey'};%the employees array
disp(sprintf('+-----------------------------------------------+'));
disp(sprintf('|\t%s \t\t\tAlex''s Shop |\n|\t\t\t\t\t\t\t\t\t\t\t\t|', datestr(now,0)));
totalPrice = 0;
for i = 1:3
randItems = items {ceil(rand*6)};
randprice = price {ceil(rand*6)};
randQuantity = ceil(rand*9);% random quantity from 1 to 9 pieces
randEmployee = employee{ceil(rand*4)};
itemTotal = randprice * randQuantity;%total price of individual item
totalPrice = totalPrice + itemTotal;
disp(sprintf('|\t%s\t (%d) x %.2f = £ %.2f \t\t\t|', randItems, randQuantity, randprice, itemTotal))
end
disp(sprintf('|\t\t\t\t-----------------------------\t|'));
disp(sprintf('|\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t Total to pay \t £ %.2f\t\t\t\t|',totalPrice));
disp(sprintf('|\t VAT \t\t\t\t £ %.2f\t\t\t\t| \n|\t\t\t\t\t\t\t\t\t\t\t\t|', totalPrice*0.2));
disp(sprintf('|\tThank you! You have been served by %s\t|\t', randEmployee));
disp(sprintf('+-----------------------------------------------+'));
My issue of course is the following. Upon choosing a random item from the items list, I then choose a random price to assign as well. I don't want this though. I would like to find a way to assign a preset price to each item to be printed automatically when generating a random item to be added to the basket. I hope this explanation is sufficient for you, if you have any questions feel free to ask. Thank you in advance.
When you write
randItems = items {ceil(rand*6)};
randprice = price {ceil(rand*6)};
you calculate a random index into the array items, and then you calculate a random index into the array price. If you instead assign the index you calculate via ceil(rand*6) to a separate variable, called e.g. index, you can re-use it to pick, say, item #3 from both items and price. Thus, the ith item will always show up with the ith price.

Resources