one to many relationship based on date - c#

I wrote an windows form app in c# that makes a presence/delay log for employees and now I'm trying to add a feature to that app which does the following :
Takes the log of that day (who is present and who came late to work) from it's database and assign it to the date of that day, so i'd be able to view one each separate day who was present and who came late to work.
To make it more clear :
Workers table : where 1 means the employee is present or delayed
ID--name----------presence-----delay
1--sam--------------1----------0---
2--jack-------------0----------0---
3--ted--------------1----------1---
Date table :
Day---------------present----delay-----absent
14/10/2012---------sam--------ted-------jack
------------------------ted-----------------------
and so on, i hope i made my idea clear.
how will the second table look like and how will the relation be ?
how will i view the result like the one in the date table ?

--Person-- --Presence-- --Delay--
ID ID ID
Name Date Date
// other info PersonID PersonID
IsPresent
First of all, you should hold Person table separately and use it's ID in other tables. I strongly recommend reading about data redundancy and database normalization. PersonID in the other tables refer to ID in Person table.
I think Presence should be logged for each day. Delay should be logged if only the Person shown up late that day.

you may want to do something like this, may not be 100% but should get you going in the right direction. Run into any other questions let me know will help as i can
table: Employees { Id, Name }
table: Logs {EmployeeId, DateCreated, OnTime (bit false if late)}
select Name
Status = CASE WHEN OnTime = 1 THEN "On Time"
WHEN OnTime is null THEN "Absent"
ELSE "Late"
from Employees as e
left join Logs as l
on e.Id = l.EmployeeId
Where l.DateCreated = #aDate
or l.DateCreated is null
to show the data the way you are looking to would then become a task for the report engine or a pivot table depending on the requirements

Related

After saving a record in a table, how to loop through all records with same ID and update them in ASP.NET MVC C#

Ok so I am stumped on how to achieve this. I have a database entity framework table called "ProductOrders" that saves a new record when a new order is placed. What I want to do though is after it saves the new record, I want to get the value of the "Qty Available" field that was just created/saved and then loop through all the records in the table and if there are any records where the "ProductID" field is the same as the one that was just created, then I want to change the value of the "Qty Available" field to the new "Qty Available" value from the newest entry. An example would be if someone ordered product # ABC on order # 100 and the new Qty Available value was 10 for that product. After that record is created, I want it to check every record in that table to see if there are other orders containing product # ABC. If there is, then change the Qty Available value to 10 for each one. I tried this and it didn't work:
productOrder = db.ProductOrders.Find(id);
foreach (var item in db.ProductOrders.Where(x => x.ProductID == productOrder.ProductID))
{
item.Qty_Available = productOrder.Qty_Available;
db.Entry(item).State = EntityState.Modified;
db.SaveChanges();
}
I got the error message: "New transaction not allowed because there are other threads running in the session". I just can't seem to get this to work. I'm not sure if I need to do a foreach statement or if I'm on the wrong track, right track...I would greatly appreciate any help as I have been stuck on this for days.
Oh wow, I feel really stupid now. I simply added .ToList() at the end of the for each statement and it worked lol.
It looks like you're better off creating another table called productAvailableQuantity that has a productID, Description and Quantity. That way when you're saving a new record to the productOrder table you can just update the productAvailableQuantity table without having to loop through all the records in productOrder table.

get due for each employee paid amount

I am newbie to asp.net mvc. I have searched a lot for the following situation but couldn't find anything in c#. Please don't block me. Kindly just provide a solution because I want to learn.
Employee_table (which shows active employees with following attributes)
1.1 id , name , total_due_amount , Status
transaction_table (which contain all transaction for all employees with following attributes)
2.1 id , parent_id , date , labor_cost_of_week , paid , due , details
Employee controller
Employee_transaction controller
where parent id is foreign key from Employee_table I need to show the due amount of last transaction in employee table for all employees.
Feel free to ask anything in case I didn't explain.
First of all you need to figure out the exact formula which is used for the calculation. Afterwards, collect necessary data from all database and make those calculations, then return your answer.
There is probably a more sophisticated way to do this, but for you the simplest one is the best.
Basically, you send request to the controller, the method in controller accesses some other class containing all the methods for calculations and then you return a string or JSON result with the controller.

Entity Framework / Database - Archiving Foreign Key Records

I've developed an application for a client with the following relationship:
A SalesOrder has a foreign key relationship to a Customer (one-to-one).
The client now wants to be able to delete Customers so that they won't be available for any future (new) SalesOrders, but obviously I would like to retain the historical records for reporting, etc. Likewise, the record may need to be update-able in the future in rare cases, so in "Edit" mode the customer would need to be there (but ALL other deleted Customers would not)
I am looking for input on a pattern to model this, or better recommendations. My thought was to have an "Archived" bit on the Customer, but my problem is that if someone loads up an old SalesOrder I would also need to load that archived record for the DropDownList that Customers populate.
Since I'm using Entity Framework and an EntityDataSource, my guess is that I might get a runtime exception on the SelectedValue bind of the DropDownList also, but I haven't verified this.
Any thoughts or recommendations on where to start?
Thank you.
I believe I have a solution. I've been racking my brain on it all day but I think it's fairly easy. In a quick prototype it appears to do what I need it to:
I added an "Archived" bit to the Customers table (actually to the BusinessEntity table they inherit from). Then for the "Edit Sales Order" page, when querying Customers, I have a where clause which includes the customer by ID.
For example (pseudocode):
SELECT CustomerID, Name FROM Customers WHERE Archived = 0 OR CustomerID = 52
That pulls all the active customers and the one customer for the record that I need. This allows the customer to still be linked to the record for editing AND doesn't generate a runtime exception with the DropDownList binding.
For reporting, I assume I'll just not filter based on the Archived bit since it's all read-only anyway.

Maker-Checker functionality for asp.net

I'm creating a maker-checker functionality where, maker creates a record and it is reviewed by the checker. The record creation will not happen until the checker approves it. If the checker rejects it then it should be rolled back.
The idea which i know is create a temporary table of records which will hold the records to be created until the checker approves it. But this method will have 2X number of tables to be created to cover all functionalities.
What are the best practices to achieve this?
Is it something which can be done using Windows Work Flow.? (WWF)
Just add a few more columns to your db.
Status : Current status of record ( Waiting for approval, Approved,
Declined . etc)
Maker : Username or id
Checker : Username or id
Checker Approval/Rejection Time
Alternatively if you have lots of tables like this needs to maker/checker workflow you can add another table and reference all other record to this table.
Windows workflow foundation also could work for you but i persnoally find it difficult to use
If you want revisions for the recored you also need more columns. Such as Revision Number and IsLastRevision.
I dont know what you are using for accessing db and modifiying records. If you are using OR/M you might override Update and do revisions on all saves . For Example
void Update(Entity e )
{
Entity n = new Entity();
// Create a copy of e ( With AutoMapper for example or manually )
e.Current = true;
e.RevisionNumber += 1;
Update(e);
Insert(n);
}
In this case you will have two options:
Create two identical tables and use one table for approved data and one for requested data.
OR
You can create two rows for a user with TypeID(requested/approved). in this case user will create a request with typeID = requested when approver approved that request you simply override current approved record with new one otherwise simply mark requested row with rejected status.

Database design: Custom fee/billing charge rates

A user can have custom billing rates, which may differ from one user to another.
You would most likely have a default billing fee table, that would look something like this
FEE
Id
FeeTypeId
FixedCharge
VariableCharge
When you charge the customer you might send them an invoice etc and then you have a table that stores the billed/charge amount that might look like this!
USER_TRANSACTION_CHARGE
Id
UserId
FeeId
ChargeName
ChargeAmount
DateTime
How do you design it for custom billing rates per specific user?
It doesn't look as if your FEE table has any history. It is a common legal requirement to be able to produce invoices up to 7 or 10 years ago. A FEE record can change, so what does that do to the link (feeid) in the invoice table?
FEE: Id, FeeTypeId, FixedCharge, VariableCharge
USER_FEE: Id, UserId, FeeTypeId, FixedCharge, VariableCharge
You could create a USER_FEE table that "overlays" the FEE table for a specific User. So for each given FeeTypeId, a user may have an overlay in USER_FEE or could just fall back to FEE.
As for USER_TRANSACTION_CHARGE, I seriously challenge the link column FeeId in there, unless you maintain history against FEE, which FeeId + DateTime would link to. I would drop that table to just
Id, UserId, ChargeName, ChargeAmount, DateTime
Where I assume ChargeName is somehow linked to FeeTypeId?
And ChargeAmount is whatever has been worked out from Fixed/VariableCharge
So in essence, the (USER_)FEE tables are used to look up the values, but beyond that, they are stored as values without a backlink to the (USER_)FEE table, since this appears to be a simple system without versioning.
Keep the table of default fees and add a cross-reference the lists user, feeType, and the override. At billing time determine the fee with a resolution (http://database-programmer.blogspot.com/2008/04/advanced-table-design-resolutions.html) That blog entry actually uses as its example a time-billing system, close to what you are doing.
Beware of false normalization -- by which I mean you should materialize the actual fee and save it permanently on the invoice. This is because at the time an invoice is created the actual fee charged becomes a historical fact about the transaction, and so it is not denormalizing to save it as such. The foreign key is there only to identify the item/activity/feeType, not for the purposes of obtaining the fee, the fee is saved onto the invoice and kept forever.
The default fee should be independant of what was actually charged, so the User_Transaction_Charge has to lose the FeeID foreign key.
If you need to store the components used to generate the User_Transaction_Charge, store them within the Charge or as a separate entity.
DEFAULT_FEE
Id
UserId
FeeTypeId
FixedCharge
VariableCharge
and
USER_TRANSACTION_CHARGE
Id
UserId
ChargeName
ChargeAmount
DateTime
USER_TRANSACTION_CHARGE_FEE
Id
UserTransactionChargeId
FixedAmount
VariableRate
or
USER_TRANSACTION_CHARGE
Id
UserId
ChargeName
ChargeAmount
FixedAmount
VariableRate
DateTime

Categories

Resources