I still don't get whether the electrons are allowed to take the energy of the whole photon when they are in energy levels and can they absorb a fraction of the photon energy when they are free
Electrons may be free, or bound within an atom due to the relative potential between the nucleus and the electron, for a simple case, i.e. the hydrogen atom.
When bound in an atom a photon with an exact energy difference ( within the width) to the ionization level can be completely absorbed by the atomic system and an electron will go free and the atom will recoil. This kinematically is a two body situation. Before it is "photon + atom" after "electron + atom". There exists a physical center of mass system.
In two body interactions energy and momentum are conserved by the end particles, in this case an ionized atom and an electron.
When an electron is free , as in compton scattering, the system is "Photon1 + electron" as initial state, and the final state is "photon2+electron" There exists a consistent physical center of mass system in both cases.
Energy and momentum are conserved from initial to final state.
If photon2 is a zero vector, the initial center of mass will have the invariant mass of "photon1+electron" which will be larger than just the mass of the electron, and the final one will have just the invariant mass of the electron. This is inconsistent with energy conservation and cannot be physical.
And how energy and Momentum will not be conserved when the bound electron is hit by a photon of energy less than the energy difference between any two consecutive cells and then the electron gets out of its shell
In quantum mechanics, the atom is a bound system to which energy can be given in quanta, corresponding to the energy levels.
If the transition is "Photon + atom" going to "excited atom", i.e. the electron changed energy level, energy is conserved because now the atom has more energy, the one the photon gave up. In the center of mass before, momentum is zero, and after momentum is zero and the energy before is equal to the energy after because the atom is at an excited level carrying the photon energy.
The case of photon +electron ---> electron cannot conserve energy, because at the center of mass there is the energy of the photon +the energy of the mass of the electron, incoming, but only the energy of the mass of the electron outgoing, the electron does not have quantum mechanical excited states to increase its rest mass energy, as the excited atom does.