mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-10-31 05:31:10 -04:00 
			
		
		
		
	init crater
This commit is contained in:
		
							
								
								
									
										194
									
								
								app/Http/Controllers/Auth/AccessTokensController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								app/Http/Controllers/Auth/AccessTokensController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,194 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers\Auth; | ||||
|  | ||||
| use Laraspace\Proxy\HttpKernelProxy; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Http\Response; | ||||
| use Illuminate\Validation\ValidationException; | ||||
| use Illuminate\Foundation\Auth\ThrottlesLogins; | ||||
| use Validator; | ||||
| use Hash; | ||||
| use Laraspace\User; | ||||
| use Auth; | ||||
| use Laraspace\Http\Controllers\Controller; | ||||
|  | ||||
| class AccessTokensController extends Controller | ||||
| { | ||||
| 	use ThrottlesLogins; | ||||
|  | ||||
| 	/** | ||||
| 	 * A tool for proxying requests to the existing application. | ||||
| 	 * | ||||
| 	 * @var HttpKernelProxy | ||||
| 	 */ | ||||
| 	protected $proxy; | ||||
|  | ||||
| 	/** | ||||
| 	 * Create a new controller instance. | ||||
| 	 * | ||||
| 	 * @return void | ||||
| 	 */ | ||||
| 	public function __construct(HttpKernelProxy $proxy) | ||||
| 	{ | ||||
| 		$this->middleware('api')->except(['store', 'update']); | ||||
| 		$this->proxy = $proxy; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get the login username to be used by the controller. | ||||
| 	 * | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public function username() | ||||
| 	{ | ||||
| 		return 'email'; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Generate a new access token. | ||||
| 	 * | ||||
| 	 * @param  \Illuminate\Http\Request  $request | ||||
| 	 * @return \Illuminate\Http\Response | ||||
| 	 */ | ||||
| 	public function store(Request $request) | ||||
| 	{ | ||||
| 		$request->validate([ | ||||
| 			'username' => 'required|email', | ||||
| 			'password' => 'required|string', | ||||
| 		]); | ||||
|  | ||||
| 		if ($this->hasTooManyLoginAttempts($request)) { | ||||
| 			$this->fireLockoutEvent($request); | ||||
|  | ||||
| 			return $this->sendLockoutResponse($request); | ||||
| 		} | ||||
|  | ||||
| 		return $this->requestPasswordGrant($request); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Refresh an access token. | ||||
| 	 * | ||||
| 	 * @param  \Illuminate\Http\Request  $request | ||||
| 	 * @return \Illuminate\Http\Response | ||||
| 	 */ | ||||
| 	public function update(Request $request) | ||||
| 	{ | ||||
| 		$token = $request->cookie('refresh_token'); | ||||
|  | ||||
| 		if (!$token) { | ||||
| 			throw ValidationException::withMessages([ | ||||
| 				'refresh_token' => trans('oauth.missing_refresh_token') | ||||
| 			]); | ||||
| 		} | ||||
|  | ||||
| 		$response = $this->proxy->postJson('oauth/token', [ | ||||
| 			'client_id' => config('auth.proxy.client_id'), | ||||
| 			'client_secret' => config('auth.proxy.client_secret'), | ||||
| 			'grant_type' => 'refresh_token', | ||||
| 			'refresh_token' => $token, | ||||
| 			'scopes' => '[*]', | ||||
| 		]); | ||||
|  | ||||
| 		if ($response->isSuccessful()) { | ||||
| 			return $this->sendSuccessResponse($response); | ||||
| 		} | ||||
|  | ||||
| 		return response($response->getContent(), $response->getStatusCode()); | ||||
| 	} | ||||
|  | ||||
|    /** | ||||
|      * Get the guard to be used during authentication. | ||||
|      * | ||||
|      * @return \Illuminate\Contracts\Auth\StatefulGuard | ||||
|      */ | ||||
|     protected function guard() | ||||
|     { | ||||
|         return Auth::guard('api'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy(Request $request) | ||||
|     { | ||||
|         $accessToken = Auth::user()->token(); | ||||
|  | ||||
|         \DB::table('oauth_refresh_tokens') | ||||
|                 ->where('access_token_id', $accessToken->id) | ||||
|                 ->update([ | ||||
|                         'revoked' => true | ||||
|                 ]); | ||||
|  | ||||
|         $accessToken->revoke(); | ||||
|  | ||||
|         return response()->json(null, 200); | ||||
|     } | ||||
|  | ||||
| 	/** | ||||
| 	 * Create a new access token from a password grant client. | ||||
| 	 * | ||||
| 	 * @param \Illuminate\Http\Request  $request | ||||
| 	 * @return \Illuminate\Http\Response | ||||
| 	 */ | ||||
| 	public function requestPasswordGrant(Request $request) | ||||
| 	{ | ||||
| 		$response = $this->proxy->postJson('oauth/token', [ | ||||
| 			'client_id' => config('auth.proxy.client_id'), | ||||
| 			'client_secret' => config('auth.proxy.client_secret'), | ||||
| 			'grant_type' => config('auth.proxy.grant_type'), | ||||
| 			'username' => $request->username, | ||||
| 			'password' => $request->password, | ||||
| 			'scopes' => '[*]' | ||||
| 		]); | ||||
|  | ||||
|         $user = User::where('email', $request->username)->first(); | ||||
|  | ||||
|         if ($response->isSuccessful()) { | ||||
|             $this->clearLoginAttempts($request); | ||||
| 			return $this->sendSuccessResponse($response, $user); | ||||
| 		} | ||||
|  | ||||
| 		$this->incrementLoginAttempts($request); | ||||
|  | ||||
| 		return response($response->getContent(), $response->getStatusCode()); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Return a successful response for requesting an api token. | ||||
| 	 * | ||||
| 	 * @param \Illuminate\Http\Response $response | ||||
| 	 * @return \Illuminate\Http\Response | ||||
| 	 */ | ||||
| 	public function sendSuccessResponse(Response $response, $user) | ||||
| 	{ | ||||
| 		$data = json_decode($response->getContent()); | ||||
|  | ||||
|         $content = [ | ||||
| 			'access_token' => $data->access_token, | ||||
| 			'expires_in' => $data->expires_in, | ||||
| 		]; | ||||
|  | ||||
| 		return response($content, $response->getStatusCode())->cookie( | ||||
| 			'refresh_token', | ||||
| 			$data->refresh_token, | ||||
| 			10 * 24 * 60, | ||||
| 			"", | ||||
| 			"", | ||||
| 			true, | ||||
| 			true | ||||
| 		); | ||||
|     } | ||||
|  | ||||
|     public function isRegistered(Request $request) | ||||
|     { | ||||
|         if (User::whereEmail($request->email)->first()) { | ||||
|             return 'true'; | ||||
|         } else { | ||||
|             return 'false'; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										59
									
								
								app/Http/Controllers/Auth/ForgotPasswordController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								app/Http/Controllers/Auth/ForgotPasswordController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,59 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers\Auth; | ||||
|  | ||||
| use Laraspace\Http\Controllers\Controller; | ||||
| use Illuminate\Foundation\Auth\SendsPasswordResetEmails; | ||||
| use Illuminate\Http\Request; | ||||
|  | ||||
| class ForgotPasswordController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Password Reset Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | This controller is responsible for handling password reset emails and | ||||
|     | includes a trait which assists in sending these notifications from | ||||
|     | your application to your users. Feel free to explore this trait. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use SendsPasswordResetEmails; | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         // $this->middleware('guest'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the response for a successful password reset link. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  string  $response | ||||
|      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse | ||||
|      */ | ||||
|     protected function sendResetLinkResponse(Request $request, $response) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'message' => 'Password reset email sent.', | ||||
|             'data' => $response, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the response for a failed password reset link. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  string  $response | ||||
|      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse | ||||
|      */ | ||||
|     protected function sendResetLinkFailedResponse(Request $request, $response) | ||||
|     { | ||||
|         return response('Email could not be sent to this email address.', 403); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										85
									
								
								app/Http/Controllers/Auth/ResetPasswordController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								app/Http/Controllers/Auth/ResetPasswordController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,85 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers\Auth; | ||||
|  | ||||
| use Laraspace\Http\Controllers\Controller; | ||||
| use Illuminate\Foundation\Auth\ResetsPasswords; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Support\Str; | ||||
| use Illuminate\Auth\Events\PasswordReset; | ||||
|  | ||||
| class ResetPasswordController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Password Reset Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | This controller is responsible for handling password reset requests | ||||
|     | and uses a simple trait to include this behavior. You're free to | ||||
|     | explore this trait and override any methods you wish to tweak. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use ResetsPasswords; | ||||
|  | ||||
|     /** | ||||
|      * Where to redirect users after resetting their password. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $redirectTo = '/'; | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         // $this->middleware('guest'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the response for a successful password reset. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  string  $response | ||||
|      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse | ||||
|      */ | ||||
|     protected function sendResetResponse(Request $request, $response) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'message' => 'Password reset successfully.' | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Reset the given user's password. | ||||
|      * | ||||
|      * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user | ||||
|      * @param  string  $password | ||||
|      * @return void | ||||
|      */ | ||||
|     protected function resetPassword($user, $password) | ||||
|     { | ||||
|         $user->password = \Hash::make($password); | ||||
|  | ||||
|         $user->setRememberToken(Str::random(60)); | ||||
|  | ||||
|         $user->save(); | ||||
|  | ||||
|         event(new PasswordReset($user)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the response for a failed password reset. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  string  $response | ||||
|      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse | ||||
|      */ | ||||
|     protected function sendResetFailedResponse(Request $request, $response) | ||||
|     { | ||||
|         return response('Failed, Invalid Token.', 403); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										238
									
								
								app/Http/Controllers/CompanyController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										238
									
								
								app/Http/Controllers/CompanyController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,238 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Setting; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\Address; | ||||
| use Laraspace\Http\Requests\SettingRequest; | ||||
| use Laraspace\Http\Requests\SettingKeyRequest; | ||||
| use Laraspace\Http\Requests\ProfileRequest; | ||||
| use Laraspace\Http\Requests\CompanyRequest; | ||||
| use Laraspace\Http\Requests\CompanySettingRequest; | ||||
| use Laraspace\Http\Requests\NotificationSettingsRequest; | ||||
| use Laraspace\Space\CurrencyFormatter; | ||||
| use Laraspace\Space\DateFormatter; | ||||
| use Laraspace\Space\TimeZones; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\CompanySetting; | ||||
|  | ||||
| class CompanyController extends Controller | ||||
| { | ||||
|     public function getAdmin() | ||||
|     { | ||||
|         return User::find(1); | ||||
|     } | ||||
|  | ||||
|     public function updateAdminProfile(ProfileRequest $request) | ||||
|     { | ||||
|         $verifyEmail = User::where('email', $request->email)->first(); | ||||
|  | ||||
|         $user = auth()->user(); | ||||
|  | ||||
|         if ($verifyEmail) { | ||||
|             if ($verifyEmail->id !== $user->id) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'Email already in use' | ||||
|                 ]); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $user->name = $request->name; | ||||
|         $user->email = $request->email; | ||||
|  | ||||
|         if ($request->has('password')) { | ||||
|             $user->password = bcrypt($request->password); | ||||
|         } | ||||
|  | ||||
|         $user->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getAdminCompany() | ||||
|     { | ||||
|         $user = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city', 'company'])->find(1); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function updateAdminCompany(CompanyRequest $request) | ||||
|     { | ||||
|         $user = User::find(1); | ||||
|         $company = $user->company; | ||||
|         $company->name = $request->name; | ||||
|         $company->save(); | ||||
|  | ||||
|         if ($request->has('logo')) { | ||||
|             $company->clearMediaCollection('logo'); | ||||
|             $company->addMediaFromRequest('logo')->toMediaCollection('logo'); | ||||
|         } | ||||
|  | ||||
|         $fields = $request->only(['address_street_1', 'address_street_2', 'city_id', 'state_id', 'country_id', 'zip', 'phone']); | ||||
|         $address = Address::updateOrCreate(['user_id' => 1], $fields); | ||||
|         $user = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city', 'company'])->find(1); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getGeneralSettings(Request $request) | ||||
|     { | ||||
|         $date_formats = DateFormatter::get_list(); | ||||
|  | ||||
|         $time_zones = TimeZones::get_list(); | ||||
|         $fiscal_years = [ | ||||
|             ['key' => 'january-december' , 'value' => '1-12'], | ||||
|             ['key' => 'february-january' , 'value' => '2-1'], | ||||
|             ['key' => 'march-february'   , 'value' => '3-2'], | ||||
|             ['key' => 'april-march'      , 'value' => '4-3'], | ||||
|             ['key' => 'may-april'        , 'value' => '5-4'], | ||||
|             ['key' => 'june-may'         , 'value' => '6-5'], | ||||
|             ['key' => 'july-june'        , 'value' => '7-6'], | ||||
|             ['key' => 'august-july'      , 'value' => '8-7'], | ||||
|             ['key' => 'september-august' , 'value' => '9-8'], | ||||
|             ['key' => 'october-september', 'value' => '10-9'], | ||||
|             ['key' => 'november-october' , 'value' => '11-10'], | ||||
|             ['key' => 'december-november', 'value' => '12-11'], | ||||
|         ]; | ||||
|  | ||||
|         $languages = [ | ||||
|             "en" => "English", | ||||
|             "de" => "German", | ||||
|             "fr" => "French", | ||||
|             "es" => "Spanish" | ||||
|         ]; | ||||
|  | ||||
|         $language = CompanySetting::getSetting('language', $request->header('company')); | ||||
|         $carbon_date_format = CompanySetting::getSetting('carbon_date_format', $request->header('company')); | ||||
|         $moment_date_format = CompanySetting::getSetting('moment_date_format', $request->header('company')); | ||||
|         $time_zone = CompanySetting::getSetting('time_zone', $request->header('company')); | ||||
|         $currency = CompanySetting::getSetting('currency', $request->header('company')); | ||||
|         $fiscal_year = CompanySetting::getSetting('fiscal_year', $request->header('company')); | ||||
|  | ||||
|         $languages = [ | ||||
|             ["code"=>"en", "name" => "English"], | ||||
|             ["code"=>"de", "name" => "German"], | ||||
|             ["code"=>"fr", "name" => "French"], | ||||
|             ["code"=>"es", "name" => "Spanish"] | ||||
|         ]; | ||||
|  | ||||
|         return response()->json([ | ||||
|             'languages' => $languages, | ||||
|             'date_formats' => $date_formats, | ||||
|             'time_zones' => $time_zones, | ||||
|             'time_zone' => $time_zone, | ||||
|             'currencies' => Currency::all(), | ||||
|             'fiscal_years' => $fiscal_years, | ||||
|             'fiscal_year' => $fiscal_year, | ||||
|             'selectedLanguage' => $language, | ||||
|             'selectedCurrency' => $currency, | ||||
|             'carbon_date_format' => $carbon_date_format, | ||||
|             'moment_date_format' => $moment_date_format, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function updateGeneralSettings(CompanySettingRequest $request) | ||||
|     { | ||||
|         $sets = [ | ||||
|             'currency', | ||||
|             'time_zone', | ||||
|             'language', | ||||
|             'carbon_date_format', | ||||
|             'fiscal_year', | ||||
|             'moment_date_format' | ||||
|         ]; | ||||
|  | ||||
|         foreach ($sets as $key) { | ||||
|             CompanySetting::setSetting($key, $request->$key, $request->header('company')); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function updateSetting(SettingRequest $request) | ||||
|     { | ||||
|         CompanySetting::setSetting($request->key, $request->value, $request->header('company')); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getSetting(SettingKeyRequest $request) | ||||
|     { | ||||
|         $setting = CompanySetting::getSetting($request->key, $request->header('company')); | ||||
|  | ||||
|         return response()->json([ | ||||
|             $request->key => $setting | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getColors(Request $request) | ||||
|     { | ||||
|         $colors = [ | ||||
|             'invoice_primary_color', | ||||
|             'invoice_column_heading', | ||||
|             'invoice_field_label', | ||||
|             'invoice_field_value', | ||||
|             'invoice_body_text', | ||||
|             'invoice_description_text', | ||||
|             'invoice_border_color', | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|  | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'colorSettings' => $colorSettings | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Upload the company logo to storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function uploadCompanyLogo(Request $request) | ||||
|     { | ||||
|         $data = json_decode($request->company_logo); | ||||
|  | ||||
|         if($data) { | ||||
|             $company = Company::find($request->header('company')); | ||||
|  | ||||
|             if($company) { | ||||
|                 $company->clearMediaCollection('logo'); | ||||
|  | ||||
|                 $company->addMediaFromBase64($data->data) | ||||
|                     ->usingFileName($data->name) | ||||
|                     ->toMediaCollection('logo'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								app/Http/Controllers/Controller.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								app/Http/Controllers/Controller.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Foundation\Bus\DispatchesJobs; | ||||
| use Illuminate\Routing\Controller as BaseController; | ||||
| use Illuminate\Foundation\Validation\ValidatesRequests; | ||||
| use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||||
|  | ||||
| class Controller extends BaseController | ||||
| { | ||||
|     use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | ||||
| } | ||||
							
								
								
									
										233
									
								
								app/Http/Controllers/CustomersController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										233
									
								
								app/Http/Controllers/CustomersController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,233 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Support\Facades\Auth; | ||||
| use Laraspace\Conversation; | ||||
| use Laraspace\Group; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\Notifications\CustomerAdded; | ||||
| use Laraspace\User; | ||||
| use Illuminate\Support\Facades\Hash; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Address; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class CustomersController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $customers = User::customer() | ||||
|             ->applyFilters($request->only([ | ||||
|                 'search', | ||||
|                 'contact_name', | ||||
|                 'display_name', | ||||
|                 'phone', | ||||
|                 'orderByField', | ||||
|                 'orderBy' | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->select('users.*', | ||||
|                 DB::raw('sum(invoices.due_amount) as due_amount') | ||||
|             ) | ||||
|             ->groupBy('users.id') | ||||
|             ->leftJoin('invoices', 'users.id', '=', 'invoices.user_id') | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         $siteData = [ | ||||
|             'customers' => $customers | ||||
|         ]; | ||||
|  | ||||
|         return response()->json($siteData); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(Requests\CustomerRequest $request) | ||||
|     { | ||||
|         $verifyEmail = User::where('email', $request->email)->first(); | ||||
|  | ||||
|  | ||||
|         $customer = new User(); | ||||
|         $customer->name = $request->name; | ||||
|         $customer->currency_id = $request->currency_id; | ||||
|         $customer->company_id = $request->header('company'); | ||||
|         $customer->email = $request->email; | ||||
|         $customer->phone = $request->phone; | ||||
|         $customer->company_name = $request->company_name; | ||||
|         $customer->contact_name = $request->contact_name; | ||||
|         $customer->website = $request->website; | ||||
|         $customer->enable_portal = $request->enable_portal; | ||||
|         $customer->role = 'customer'; | ||||
|         $customer->password = Hash::make($request->password); | ||||
|         $customer->save(); | ||||
|  | ||||
|         if ($request->addresses) { | ||||
|             foreach ($request->addresses as $address) { | ||||
|                 $newAddress = new Address(); | ||||
|                 $newAddress->name = $address["name"]; | ||||
|                 $newAddress->address_street_1 = $address["address_street_1"]; | ||||
|                 $newAddress->address_street_2 = $address["address_street_2"]; | ||||
|                 $newAddress->city_id = $address["city_id"]; | ||||
|                 $newAddress->state_id = $address["state_id"]; | ||||
|                 $newAddress->country_id = $address["country_id"]; | ||||
|                 $newAddress->zip = $address["zip"]; | ||||
|                 $newAddress->phone = $address["phone"]; | ||||
|                 $newAddress->type = $address["type"]; | ||||
|                 $newAddress->user_id = $customer->id; | ||||
|                 $newAddress->save(); | ||||
|                 $customer->addresses()->save($newAddress); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $customer = User::with('billingAddress', 'shippingAddress')->find($customer->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customer' => $customer, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show($id) | ||||
|     { | ||||
|         $customer = User::with([ | ||||
|             'billingAddress', | ||||
|             'shippingAddress', | ||||
|             'billingAddress.country', | ||||
|             'billingAddress.state', | ||||
|             'billingAddress.city', | ||||
|             'shippingAddress.country', | ||||
|             'shippingAddress.state', | ||||
|             'shippingAddress.city', | ||||
|         ])->find($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customer' => $customer | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit($id) | ||||
|     { | ||||
|         $customer = User::with('billingAddress', 'shippingAddress')->findOrFail($id); | ||||
|         $currency = $customer->currency; | ||||
|         $currencies = Currency::all(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customer' => $customer, | ||||
|             'currencies' => $currencies, | ||||
|             'currency' => $currency | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update($id, Requests\CustomerRequest $request) | ||||
|     { | ||||
|         $customer = User::find($id); | ||||
|  | ||||
|         if ($request->email != null) { | ||||
|             $verifyEmail = User::where('email', $request->email)->first(); | ||||
|  | ||||
|             if ($verifyEmail) { | ||||
|                 if ($verifyEmail->id !== $customer->id) { | ||||
|                     return response()->json([ | ||||
|                         'error' => 'Email already in use' | ||||
|                     ]); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('password')) { | ||||
|             $customer->password = Hash::make($request->password); | ||||
|         } | ||||
|  | ||||
|         $customer->name = $request->name; | ||||
|         $customer->currency_id = $request->currency_id; | ||||
|         $customer->email = $request->email; | ||||
|         $customer->phone = $request->phone; | ||||
|         $customer->company_name = $request->company_name; | ||||
|         $customer->contact_name = $request->contact_name; | ||||
|         $customer->website = $request->website; | ||||
|         $customer->enable_portal = $request->enable_portal; | ||||
|         $customer->save(); | ||||
|  | ||||
|         if ($request->addresses) { | ||||
|             foreach ($request->addresses as $address) { | ||||
|                 $newAddress = $customer->addresses()->firstOrNew(['type' => $address["type"]]); | ||||
|                 $newAddress->name = $address["name"]; | ||||
|                 $newAddress->address_street_1 = $address["address_street_1"]; | ||||
|                 $newAddress->address_street_2 = $address["address_street_2"]; | ||||
|                 $newAddress->city_id = $address["city_id"]; | ||||
|                 $newAddress->state_id = $address["state_id"]; | ||||
|                 $newAddress->country_id = $address["country_id"]; | ||||
|                 $newAddress->zip = $address["zip"]; | ||||
|                 $newAddress->phone = $address["phone"]; | ||||
|                 $newAddress->type = $address["type"]; | ||||
|                 $newAddress->user_id = $customer->id; | ||||
|                 $newAddress->save(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $customer = User::with('billingAddress', 'shippingAddress')->find($customer->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customer' => $customer, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         User::deleteCustomer($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         foreach ($request->id as $id) { | ||||
|             User::deleteCustomer($id); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										155
									
								
								app/Http/Controllers/DashboardController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								app/Http/Controllers/DashboardController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,155 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
|  | ||||
| use Laraspace\Estimate; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Expense; | ||||
| use Laraspace\Payment; | ||||
| use Carbon\Carbon; | ||||
| use Laraspace\User; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class DashboardController extends Controller | ||||
| { | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $invoiceTotals = []; | ||||
|         $expenseTotals = []; | ||||
|         $receiptTotals = []; | ||||
|         $netProfits = []; | ||||
|         $i = 0; | ||||
|         $months = []; | ||||
|         $monthEnds = []; | ||||
|         $monthCounter = 0; | ||||
|         $fiscalYear = CompanySetting::getSetting('fiscal_year', $request->header('company')); | ||||
|         $startDate = Carbon::now(); | ||||
|         $start = Carbon::now(); | ||||
|         $end = Carbon::now(); | ||||
|         $terms = explode('-', $fiscalYear); | ||||
|         if ($terms[0] < $start->month) { | ||||
|             $startDate->month($terms[0])->startOfMonth(); | ||||
|             $start->month($terms[0])->startOfMonth(); | ||||
|             $end->month($terms[0])->endOfMonth(); | ||||
|         } else { | ||||
|             $startDate->subYear()->month($terms[0])->startOfMonth(); | ||||
|             $start->subYear()->month($terms[0])->startOfMonth(); | ||||
|             $end->subYear()->month($terms[0])->endOfMonth(); | ||||
|         } | ||||
|  | ||||
|         if ($request->has('previous_year')) { | ||||
|             $startDate->subYear()->startOfMonth(); | ||||
|             $start->subYear()->startOfMonth(); | ||||
|             $end->subYear()->endOfMonth(); | ||||
|         } | ||||
|  | ||||
|         while ($monthCounter < 12) { | ||||
|             array_push( | ||||
|                 $invoiceTotals, | ||||
|                 Invoice::whereBetween( | ||||
|                     'invoice_date', | ||||
|                     [$start->format('Y-m-d'), $end->format('Y-m-d')] | ||||
|                 ) | ||||
|                 ->whereCompany($request->header('company')) | ||||
|                 ->sum('total') | ||||
|             ); | ||||
|             array_push( | ||||
|                 $expenseTotals, | ||||
|                 Expense::whereBetween( | ||||
|                     'expense_date', | ||||
|                     [$start->format('Y-m-d'), $end->format('Y-m-d')] | ||||
|                 ) | ||||
|                 ->whereCompany($request->header('company')) | ||||
|                 ->sum('amount') | ||||
|             ); | ||||
|             array_push( | ||||
|                 $receiptTotals, | ||||
|                 Payment::whereBetween( | ||||
|                     'payment_date', | ||||
|                     [$start->format('Y-m-d'), $end->format('Y-m-d')] | ||||
|                 ) | ||||
|                 ->whereCompany($request->header('company')) | ||||
|                 ->sum('amount') | ||||
|             ); | ||||
|             array_push( | ||||
|                 $netProfits, | ||||
|                 ($receiptTotals[$i] - $expenseTotals[$i]) | ||||
|             ); | ||||
|             $i++; | ||||
|             array_push($months, $start->format('M')); | ||||
|             $monthCounter++; | ||||
|             $end->startOfMonth(); | ||||
|             $start->addMonth()->startOfMonth(); | ||||
|             $end->addMonth()->endOfMonth(); | ||||
|         } | ||||
|  | ||||
|         $start->subMonth()->endOfMonth(); | ||||
|         $salesTotal = Invoice::whereCompany($request->header('company')) | ||||
|             ->whereBetween( | ||||
|                 'invoice_date', | ||||
|                 [$startDate->format('Y-m-d'), $start->format('Y-m-d')] | ||||
|             ) | ||||
|             ->sum('total'); | ||||
|         $totalReceipts = Payment::whereCompany($request->header('company')) | ||||
|             ->whereBetween( | ||||
|                 'payment_date', | ||||
|                 [$startDate->format('Y-m-d'), $start->format('Y-m-d')] | ||||
|             ) | ||||
|             ->sum('amount'); | ||||
|         $totalExpenses = Expense::whereCompany($request->header('company')) | ||||
|             ->whereBetween( | ||||
|                 'expense_date', | ||||
|                 [$startDate->format('Y-m-d'), $start->format('Y-m-d')] | ||||
|             ) | ||||
|             ->sum('amount'); | ||||
|         $netProfit = (int)$totalReceipts - (int)$totalExpenses; | ||||
|  | ||||
|         $chartData = [ | ||||
|             'months'        => $months, | ||||
|             'invoiceTotals' => $invoiceTotals, | ||||
|             'expenseTotals' => $expenseTotals, | ||||
|             'receiptTotals' => $receiptTotals, | ||||
|             'netProfits'    => $netProfits | ||||
|         ]; | ||||
|  | ||||
|         $customersCount = User::customer()->whereCompany($request->header('company'))->get()->count(); | ||||
|         $invoicesCount = Invoice::whereCompany($request->header('company'))->get()->count(); | ||||
|         $estimatesCount = Estimate::whereCompany($request->header('company'))->get()->count(); | ||||
|         $totalDueAmount = Invoice::whereCompany($request->header('company'))->sum('due_amount'); | ||||
|         $dueInvoices = Invoice::with('user')->whereCompany($request->header('company'))->where('due_amount', '>', 0)->take(5)->latest()->get(); | ||||
|         $estimates = Estimate::with('user')->whereCompany($request->header('company'))->take(5)->latest()->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'dueInvoices' => $dueInvoices, | ||||
|             'estimates' => $estimates, | ||||
|             'estimatesCount' => $estimatesCount, | ||||
|             'totalDueAmount' => $totalDueAmount, | ||||
|             'invoicesCount' => $invoicesCount, | ||||
|             'customersCount' => $customersCount, | ||||
|             'chartData' => $chartData, | ||||
|             'salesTotal' => $salesTotal, | ||||
|             'totalReceipts' => $totalReceipts, | ||||
|             'totalExpenses' => $totalExpenses, | ||||
|             'netProfit' => $netProfit | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getExpenseChartData(Request $request) | ||||
|     { | ||||
|         $expensesCategories = Expense::with('category') | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->expensesAttributes() | ||||
|             ->get(); | ||||
|  | ||||
|         $amounts = $expensesCategories->pluck('total_amount'); | ||||
|         $names = $expensesCategories->pluck('category.name'); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'amounts' => $amounts, | ||||
|             'categories' => $names, | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										90
									
								
								app/Http/Controllers/EnvironmentController.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										90
									
								
								app/Http/Controllers/EnvironmentController.php
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,90 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Exception; | ||||
| use Validator; | ||||
| use Laraspace\Setting; | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Space\EnvironmentManager; | ||||
| use Laraspace\Http\Requests\DatabaseEnvironmentRequest; | ||||
| use Laraspace\Http\Requests\MailEnvironmentRequest; | ||||
| use Illuminate\Http\JsonResponse; | ||||
| use Illuminate\Support\Facades\Artisan; | ||||
|  | ||||
| class EnvironmentController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * @var EnvironmentManager | ||||
|      */ | ||||
|     protected $EnvironmentManager; | ||||
|  | ||||
|     /** | ||||
|      * @param EnvironmentManager $environmentManager | ||||
|      */ | ||||
|     public function __construct(EnvironmentManager $environmentManager) | ||||
|     { | ||||
|         $this->EnvironmentManager = $environmentManager; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * | ||||
|      * @param DatabaseEnvironmentRequest $request | ||||
|      */ | ||||
|     public function saveDatabaseEnvironment(DatabaseEnvironmentRequest $request) | ||||
|     { | ||||
|         $results = $this->EnvironmentManager->saveDatabaseVariables($request); | ||||
|  | ||||
|         try { | ||||
|  | ||||
|             if(array_key_exists("success", $results)) { | ||||
|                 Artisan::call('config:clear'); | ||||
|                 Artisan::call('migrate --seed'); | ||||
|                 Artisan::call('migrate', ['--path' => 'vendor/laravel/passport/database/migrations']); | ||||
|  | ||||
|                 \Storage::disk('local')->put('database_created', 'database_created'); | ||||
|  | ||||
|                 Setting::setSetting('profile_complete', 3); | ||||
|             } | ||||
|             return response()->json($results); | ||||
|         } catch (Exception $e) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'migrate_failed' | ||||
|             ]); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * | ||||
|      * @param DatabaseEnvironmentRequest $request | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function saveMailEnvironment(MailEnvironmentRequest $request) | ||||
|     { | ||||
|         $results = $this->EnvironmentManager->saveMailVariables($request); | ||||
|  | ||||
|         Setting::setSetting('profile_complete', 4); | ||||
|  | ||||
|         return response()->json($results); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function getMailDrivers() | ||||
|     { | ||||
|         $drivers = [ | ||||
|             'smtp', | ||||
|             'mail', | ||||
|             'sendmail', | ||||
|             'mailgun', | ||||
|             'mandrill', | ||||
|             'ses', | ||||
|             'sparkpost' | ||||
|         ]; | ||||
|  | ||||
|         return response()->json($drivers); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										463
									
								
								app/Http/Controllers/EstimatesController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										463
									
								
								app/Http/Controllers/EstimatesController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,463 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Estimate; | ||||
| use Laraspace\EstimateItem; | ||||
| use Laraspace\EstimateTemplate; | ||||
| use Carbon\Carbon; | ||||
| use Laraspace\Http\Requests\EstimatesRequest; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Item; | ||||
| use Validator; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Mail\EstimatePdf; | ||||
| use Laraspace\TaxType; | ||||
| use Laraspace\Tax; | ||||
|  | ||||
| class EstimatesController extends Controller | ||||
| { | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $estimates = Estimate::with([ | ||||
|                 'items', | ||||
|                 'user', | ||||
|                 'estimateTemplate', | ||||
|                 'taxes' | ||||
|             ]) | ||||
|             ->join('users', 'users.id', '=', 'estimates.user_id') | ||||
|             ->applyFilters($request->only([ | ||||
|                 'status', | ||||
|                 'customer_id', | ||||
|                 'estimate_number', | ||||
|                 'from_date', | ||||
|                 'to_date', | ||||
|                 'search', | ||||
|                 'orderByField', | ||||
|                 'orderBy' | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->select('estimates.*', 'users.name') | ||||
|             ->latest() | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         $siteData = [ | ||||
|             'estimates' => $estimates, | ||||
|             'estimateTotalCount' => Estimate::count() | ||||
|         ]; | ||||
|  | ||||
|         return response()->json($siteData); | ||||
|     } | ||||
|  | ||||
|     public function create(Request $request) | ||||
|     { | ||||
|         $nextEstimateNumber = 'EST-'.Estimate::getNextEstimateNumber(); | ||||
|         $tax_per_item = CompanySetting::getSetting('tax_per_item', $request->header('company')); | ||||
|         $discount_per_item = CompanySetting::getSetting('discount_per_item', $request->header('company')); | ||||
|         $customers = User::where('role', 'customer')->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customers' => $customers, | ||||
|             'nextEstimateNumber' => $nextEstimateNumber, | ||||
|             'taxes' => Tax::whereCompany($request->header('company'))->latest()->get(), | ||||
|             'items' => Item::whereCompany($request->header('company'))->get(), | ||||
|             'tax_per_item' => $tax_per_item, | ||||
|             'discount_per_item' => $discount_per_item, | ||||
|             'estimateTemplates' => EstimateTemplate::all(), | ||||
|             'shareable_link' => '' | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function store(EstimatesRequest $request) | ||||
|     { | ||||
|         $estimate_date = Carbon::createFromFormat('d/m/Y', $request->estimate_date); | ||||
|         $expiry_date = Carbon::createFromFormat('d/m/Y', $request->expiry_date); | ||||
|         $status = Estimate::STATUS_DRAFT; | ||||
|         $tax_per_item = CompanySetting::getSetting( | ||||
|             'tax_per_item', | ||||
|             $request->header('company') | ||||
|         ) ? CompanySetting::getSetting( | ||||
|             'tax_per_item', | ||||
|             $request->header('company') | ||||
|         ) : 'NO'; | ||||
|  | ||||
|         if ($request->has('estimateSend')) { | ||||
|             $status = Estimate::STATUS_SENT; | ||||
|         } | ||||
|  | ||||
|         $discount_per_item = CompanySetting::getSetting( | ||||
|             'discount_per_item', | ||||
|             $request->header('company') | ||||
|         ) ? CompanySetting::getSetting( | ||||
|             'discount_per_item', | ||||
|             $request->header('company') | ||||
|         ) : 'NO'; | ||||
|  | ||||
|         $estimate = Estimate::create([ | ||||
|             'estimate_date' => $estimate_date, | ||||
|             'expiry_date' => $expiry_date, | ||||
|             'estimate_number' => $request->estimate_number, | ||||
|             'reference_number' => $request->reference_number, | ||||
|             'user_id' => $request->user_id, | ||||
|             'company_id' => $request->header('company'), | ||||
|             'estimate_template_id' => $request->estimate_template_id, | ||||
|             'status' => $status, | ||||
|             'discount' => $request->discount, | ||||
|             'discount_type' => $request->discount_type, | ||||
|             'discount_val' => $request->discount_val, | ||||
|             'sub_total' => $request->sub_total, | ||||
|             'total' => $request->total, | ||||
|             'tax_per_item' => $tax_per_item, | ||||
|             'discount_per_item' => $discount_per_item, | ||||
|             'tax' => $request->tax, | ||||
|             'notes' => $request->notes, | ||||
|             'unique_hash' => str_random(60) | ||||
|         ]); | ||||
|  | ||||
|         $estimateItems = $request->items; | ||||
|  | ||||
|         foreach ($estimateItems as $estimateItem) { | ||||
|             $estimateItem['company_id'] = $request->header('company'); | ||||
|             $item = $estimate->items()->create($estimateItem); | ||||
|  | ||||
|             if (array_key_exists('taxes', $estimateItem) && $estimateItem['taxes']) { | ||||
|                 foreach ($estimateItem['taxes'] as $tax) { | ||||
|                     if ($tax['amount']) { | ||||
|                         $tax['company_id'] = $request->header('company'); | ||||
|                         $item->taxes()->create($tax); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 if ($tax['amount']) { | ||||
|                     $tax['company_id'] = $request->header('company'); | ||||
|                     $estimate->taxes()->create($tax); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('estimateSend')) { | ||||
|             $data['estimate'] = $estimate->toArray(); | ||||
|             $userId = $data['estimate']['user_id']; | ||||
|             $data['user'] = User::find($userId)->toArray(); | ||||
|             $email = $data['user']['email']; | ||||
|             $notificationEmail = CompanySetting::getSetting( | ||||
|                 'notification_email', | ||||
|                 $request->header('company') | ||||
|             ); | ||||
|  | ||||
|             if (!$email) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'user_email_does_not_exist' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             if (!$notificationEmail) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'notification_email_does_not_exist' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             \Mail::to($email)->send(new EstimatePdf($data, $notificationEmail)); | ||||
|         } | ||||
|  | ||||
|         $estimate = Estimate::with([ | ||||
|             'items', | ||||
|             'user', | ||||
|             'estimateTemplate', | ||||
|             'taxes' | ||||
|         ])->find($estimate->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'estimate' => $estimate, | ||||
|             'url' => url('/estimates/pdf/'.$estimate->unique_hash), | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function show(Request $request, $id) | ||||
|     { | ||||
|         $estimate = Estimate::with([ | ||||
|             'items', | ||||
|             'items.taxes', | ||||
|             'user', | ||||
|             'estimateTemplate', | ||||
|             'taxes', | ||||
|             'taxes.taxType' | ||||
|         ])->find($id); | ||||
|  | ||||
|         $siteData = [ | ||||
|             'estimate' => $estimate, | ||||
|             'shareable_link' => url('/estimates/pdf/'.$estimate->unique_hash) | ||||
|         ]; | ||||
|  | ||||
|         return response()->json($siteData); | ||||
|     } | ||||
|  | ||||
|     public function edit(Request $request,$id) | ||||
|     { | ||||
|         $estimate = Estimate::with([ | ||||
|             'items', | ||||
|             'items.taxes', | ||||
|             'user', | ||||
|             'estimateTemplate', | ||||
|             'taxes', | ||||
|             'taxes.taxType' | ||||
|         ])->find($id); | ||||
|         $customers = User::where('role', 'customer')->get(); | ||||
|  | ||||
|         return response()->json( [ | ||||
|             'customers' => $customers, | ||||
|             'nextEstimateNumber' => $estimate->estimate_number, | ||||
|             'taxes' => Tax::latest()->whereCompany($request->header('company'))->get(), | ||||
|             'estimate' => $estimate, | ||||
|             'items' => Item::whereCompany($request->header('company'))->latest()->get(), | ||||
|             'estimateTemplates' => EstimateTemplate::all(), | ||||
|             'tax_per_item' => $estimate->tax_per_item, | ||||
|             'discount_per_item' => $estimate->discount_per_item, | ||||
|             'shareable_link' => url('/estimates/pdf/'.$estimate->unique_hash) | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(EstimatesRequest $request, $id) | ||||
|     { | ||||
|         $estimate_date = Carbon::createFromFormat('d/m/Y', $request->estimate_date); | ||||
|         $expiry_date = Carbon::createFromFormat('d/m/Y', $request->expiry_date); | ||||
|  | ||||
|         $estimate = Estimate::find($id); | ||||
|         $estimate->estimate_date = $estimate_date; | ||||
|         $estimate->expiry_date = $expiry_date; | ||||
|         $estimate->estimate_number = $request->estimate_number; | ||||
|         $estimate->reference_number = $request->reference_number; | ||||
|         $estimate->user_id = $request->user_id; | ||||
|         $estimate->estimate_template_id = $request->estimate_template_id; | ||||
|         $estimate->discount = $request->discount; | ||||
|         $estimate->discount_type = $request->discount_type; | ||||
|         $estimate->discount_val = $request->discount_val; | ||||
|         $estimate->sub_total = $request->sub_total; | ||||
|         $estimate->total = $request->total; | ||||
|         $estimate->tax = $request->tax; | ||||
|         $estimate->notes = $request->notes; | ||||
|         $estimate->save(); | ||||
|  | ||||
|         $oldItems = $estimate->items->toArray(); | ||||
|         $oldTaxes = $estimate->taxes->toArray(); | ||||
|         $estimateItems = $request->items; | ||||
|  | ||||
|         foreach ($oldItems as $oldItem) { | ||||
|             EstimateItem::destroy($oldItem['id']); | ||||
|         } | ||||
|  | ||||
|         foreach ($oldTaxes as $oldTax) { | ||||
|             Tax::destroy($oldTax['id']); | ||||
|         } | ||||
|  | ||||
|         foreach ($estimateItems as $estimateItem) { | ||||
|             $estimateItem['company_id'] = $request->header('company'); | ||||
|             $item = $estimate->items()->create($estimateItem); | ||||
|  | ||||
|             if (array_key_exists('taxes', $estimateItem) && $estimateItem['taxes']) { | ||||
|                 foreach ($estimateItem['taxes'] as $tax) { | ||||
|                     if ($tax['amount']) { | ||||
|                         $tax['company_id'] = $request->header('company'); | ||||
|                         $item->taxes()->create($tax); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 if ($tax['amount']) { | ||||
|                     $tax['company_id'] = $request->header('company'); | ||||
|                     $estimate->taxes()->create($tax); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $estimate = Estimate::with([ | ||||
|             'items', | ||||
|             'user', | ||||
|             'estimateTemplate', | ||||
|             'taxes' | ||||
|         ])->find($estimate->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'estimate' => $estimate, | ||||
|             'url' => url('/estimates/pdf/'.$estimate->unique_hash), | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         Estimate::deleteEstimate($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function sendEstimate(Request $request) | ||||
|     { | ||||
|         $estimate = Estimate::findOrFail($request->id); | ||||
|         $estimate->status = Estimate::STATUS_SENT; | ||||
|         $estimate->save(); | ||||
|  | ||||
|         $data['estimate'] = $estimate->toArray(); | ||||
|         $userId = $data['estimate']['user_id']; | ||||
|         $data['user'] = User::find($userId)->toArray(); | ||||
|         $email = $data['user']['email']; | ||||
|         $notificationEmail = CompanySetting::getSetting( | ||||
|             'notification_email', | ||||
|             $request->header('company') | ||||
|         ); | ||||
|  | ||||
|         if (!$email) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'user_email_does_not_exist' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         if (!$notificationEmail) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'notification_email_does_not_exist' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         \Mail::to($email)->send(new EstimatePdf($data, $notificationEmail)); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function markEstimateAccepted(Request $request) | ||||
|     { | ||||
|         $estimate = Estimate::find($request->id); | ||||
|         $estimate->status = Estimate::STATUS_ACCEPTED; | ||||
|         $estimate->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function markEstimateRejected(Request $request) | ||||
|     { | ||||
|         $estimate = Estimate::find($request->id); | ||||
|         $estimate->status = Estimate::STATUS_REJECTED; | ||||
|         $estimate->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function markEstimateSent(Request $request) | ||||
|     { | ||||
|         $estimate = Estimate::find($request->id); | ||||
|         $estimate->status = Estimate::STATUS_SENT; | ||||
|         $estimate->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function estimateToInvoice(Request $request, $id) | ||||
|     { | ||||
|         $estimate = Estimate::with(['items', 'items.taxes', 'user', 'estimateTemplate', 'taxes'])->find($id); | ||||
|         $invoice_date = Carbon::parse($estimate->estimate_date); | ||||
|         $due_date = Carbon::parse($estimate->estimate_date)->addDays(7); | ||||
|         $tax_per_item = CompanySetting::getSetting( | ||||
|                 'tax_per_item', | ||||
|                 $request->header('company') | ||||
|             ) ? CompanySetting::getSetting( | ||||
|                 'tax_per_item', | ||||
|                 $request->header('company') | ||||
|             ) : 'NO'; | ||||
|         $discount_per_item = CompanySetting::getSetting( | ||||
|                 'discount_per_item', | ||||
|                 $request->header('company') | ||||
|             ) ? CompanySetting::getSetting( | ||||
|                 'discount_per_item', | ||||
|                 $request->header('company') | ||||
|             ) : 'NO'; | ||||
|  | ||||
|         $invoice = Invoice::create([ | ||||
|             'invoice_date' => $invoice_date, | ||||
|             'due_date' => $due_date, | ||||
|             'invoice_number' => "INV-".Invoice::getNextInvoiceNumber(), | ||||
|             'reference_number' => $estimate->reference_number, | ||||
|             'user_id' => $estimate->user_id, | ||||
|             'company_id' => $request->header('company'), | ||||
|             'invoice_template_id' => 1, | ||||
|             'status' => Invoice::STATUS_DRAFT, | ||||
|             'paid_status' => Invoice::STATUS_UNPAID, | ||||
|             'sub_total' => $estimate->sub_total, | ||||
|             'discount' => $estimate->discount, | ||||
|             'discount_type' => $estimate->discount_type, | ||||
|             'discount_val' => $estimate->discount_val, | ||||
|             'total' => $estimate->total, | ||||
|             'due_amount' => $estimate->total, | ||||
|             'tax_per_item' => $tax_per_item, | ||||
|             'discount_per_item' => $discount_per_item, | ||||
|             'tax' => $estimate->tax, | ||||
|             'notes' => $estimate->notes, | ||||
|             'unique_hash' => str_random(60) | ||||
|         ]); | ||||
|  | ||||
|         $invoiceItems = $estimate->items->toArray(); | ||||
|  | ||||
|         foreach ($invoiceItems as $invoiceItem) { | ||||
|             $invoiceItem['company_id'] = $request->header('company'); | ||||
|             $invoiceItem['name'] = $invoiceItem['name']; | ||||
|             $item = $invoice->items()->create($invoiceItem); | ||||
|  | ||||
|             if (array_key_exists('taxes', $invoiceItem) && $invoiceItem['taxes']) { | ||||
|                 foreach ($invoiceItem['taxes'] as $tax) { | ||||
|                     $tax['company_id'] = $request->header('company'); | ||||
|  | ||||
|                     if ($tax['amount']) { | ||||
|                         $item->taxes()->create($tax); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($estimate->taxes) { | ||||
|             foreach ($estimate->taxes->toArray() as $tax) { | ||||
|                 $tax['company_id'] = $request->header('company'); | ||||
|                 $invoice->taxes()->create($tax); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::with([ | ||||
|             'items', | ||||
|             'user', | ||||
|             'invoiceTemplate', | ||||
|             'taxes' | ||||
|         ])->find($invoice->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'invoice' => $invoice | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         foreach ($request->id as $id) { | ||||
|             Estimate::deleteEstimate($id); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										122
									
								
								app/Http/Controllers/ExpenseCategoryController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								app/Http/Controllers/ExpenseCategoryController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,122 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Laraspace\ExpenseCategory; | ||||
| use Laraspace\Expense; | ||||
| use Laraspace\User; | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Http\Requests\ExpenseCategoryRequest; | ||||
|  | ||||
| class ExpenseCategoryController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $categories = ExpenseCategory::whereCompany($request->header('company'))->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'categories' => $categories | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for creating a new resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function create() | ||||
|     { | ||||
|         // return view('app.categories.create'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(ExpenseCategoryRequest $request) | ||||
|     { | ||||
|         $category = new ExpenseCategory(); | ||||
|         $category->name = $request->name; | ||||
|         $category->description = $request->description; | ||||
|         $category->company_id = $request->header('company'); | ||||
|         $category->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'category' => $category, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  \Laraspace\ExpenseCategory $ExpenseCategory | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show(ExpenseCategory $ExpenseCategory) | ||||
|     { | ||||
|         // | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  \Laraspace\ExpensesCategory $ExpensesCategory | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit($id) | ||||
|     { | ||||
|         $category = ExpenseCategory::findOrFail($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'category' => $category | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @param  \Laraspace\ExpenseCategory $ExpenseCategory | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update(ExpenseCategoryRequest $request, $id) | ||||
|     { | ||||
|         $category = ExpenseCategory::findOrFail($id); | ||||
|         $category->name = $request->name; | ||||
|         $category->description = $request->description; | ||||
|         $category->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'category' => $category, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  \Laraspace\ExpensesCategory $expensesCategory | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         $category = ExpenseCategory::find($id); | ||||
|         if ($category->expenses() && $category->expenses()->count() > 0) { | ||||
|             return response()->json([ | ||||
|                 'success' => false | ||||
|             ]); | ||||
|         } | ||||
|         $category->delete(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										260
									
								
								app/Http/Controllers/ExpensesController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										260
									
								
								app/Http/Controllers/ExpensesController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,260 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Laraspace\Expense; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\CompanySetting; | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\ExpenseCategory; | ||||
| use Laraspace\Http\Requests\ExpenseRequest; | ||||
| use Carbon\Carbon; | ||||
| use Intervention\Image\Facades\Image; | ||||
|  | ||||
| class ExpensesController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $expenses = Expense::with('category') | ||||
|             ->join('expense_categories', 'expense_categories.id', '=', 'expenses.expense_category_id') | ||||
|             ->applyFilters($request->only([ | ||||
|                 'expense_category_id', | ||||
|                 'search', | ||||
|                 'from_date', | ||||
|                 'to_date', | ||||
|                 'orderByField', | ||||
|                 'orderBy' | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->select('expenses.*', 'expense_categories.name') | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'expenses' => $expenses, | ||||
|             'currency' => Currency::findOrFail( | ||||
|                 CompanySetting::getSetting('currency', $request->header('company')) | ||||
|             ) | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for creating a new resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function create(Request $request) | ||||
|     { | ||||
|         $categories = ExpenseCategory::whereCompany($request->header('company'))->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'categories' => $categories | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(ExpenseRequest $request) | ||||
|     { | ||||
|         $expense_date = Carbon::createFromFormat('d/m/Y', $request->expense_date); | ||||
|  | ||||
|         $expense = new Expense(); | ||||
|         $expense->notes = $request->notes; | ||||
|         $expense->expense_category_id = $request->expense_category_id; | ||||
|         $expense->amount = $request->amount; | ||||
|         $expense->company_id = $request->header('company'); | ||||
|         $expense->expense_date = $expense_date; | ||||
|         $expense->save(); | ||||
|  | ||||
|         if ($request->hasFile('attachment_receipt')) { | ||||
|             $expense->addMediaFromRequest('attachment_receipt')->toMediaCollection('receipts', 'local'); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'expense' => $expense, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  \Laraspace\Expense $expense | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show(Expense $expense) | ||||
|     { | ||||
|         // | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit(Request $request,$id) | ||||
|     { | ||||
|         $categories = ExpenseCategory::whereCompany($request->header('company'))->get(); | ||||
|         $customers = User::where('role', 'customer')->whereCompany($request->header('company'))->get(); | ||||
|         $expense = Expense::with('category')->where('id', $id)->first(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'categories' => $categories, | ||||
|             'customers' => $customers, | ||||
|             'expense' => $expense | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @param  \Laraspace\Expense $expense | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update(ExpenseRequest $request, Expense $expense) | ||||
|     { | ||||
|         $expense_date = Carbon::createFromFormat('d/m/Y', $request->expense_date); | ||||
|  | ||||
|         $expense = Expense::findOrFail($expense->id); | ||||
|         $expense->notes = $request->notes; | ||||
|         $expense->expense_category_id = $request->expense_category_id; | ||||
|         $expense->amount = $request->amount; | ||||
|         $expense->expense_date = $expense_date; | ||||
|         $expense->save(); | ||||
|  | ||||
|         if ($request->hasFile('attachment_receipt')) { | ||||
|             $expense->clearMediaCollection('receipts'); | ||||
|             $expense->addMediaFromRequest('attachment_receipt')->toMediaCollection('receipts', 'local'); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'expense' => $expense, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  \Laraspace\Expense $expense | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy(Expense $expense) | ||||
|     { | ||||
|         $expense->delete(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         Expense::destroy($request->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Upload the expense receipts to storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @param   $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function uploadReceipts(Request $request, $id) | ||||
|     { | ||||
|         $data = json_decode($request->attachment_receipt); | ||||
|  | ||||
|         if($data) { | ||||
|             $expense = Expense::find($id); | ||||
|  | ||||
|             if($expense) { | ||||
|                 if($request->type === 'edit') { | ||||
|                     $expense->clearMediaCollection('receipts'); | ||||
|                 } | ||||
|  | ||||
|                 $expense->addMediaFromBase64($data->data) | ||||
|                     ->usingFileName($data->name) | ||||
|                     ->toMediaCollection('receipts', 'local'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => 'Expense receipts uploaded successfully' | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function showReceipt($id) | ||||
|     { | ||||
|         $expense = Expense::find($id); | ||||
|         $imagePath  = null; | ||||
|  | ||||
|         if($expense) { | ||||
|             $media = $expense->getFirstMedia('receipts'); | ||||
|             if($media) { | ||||
|                 $imagePath = $media->getPath(); | ||||
|             } else { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'receipt_does_not_exist' | ||||
|                 ]); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $type = \File::mimeType($imagePath); | ||||
|  | ||||
|         $image = 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath)); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'image' => $image, | ||||
|             'type' => $type | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function downloadReceipt($id, $hash) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $expense = Expense::whereCompany($company->id) | ||||
|             ->where('id', $id) | ||||
|             ->first(); | ||||
|         $imagePath  = null; | ||||
|  | ||||
|         if($expense) { | ||||
|             $media = $expense->getFirstMedia('receipts'); | ||||
|             if($media) { | ||||
|                 $imagePath = $media->getPath(); | ||||
|                 $filename = $media->getPath(); | ||||
|                 $type = \File::mimeType($imagePath); | ||||
|  | ||||
|                 $headers = array( | ||||
|                     'Content-Type' => $type, | ||||
|                 ); | ||||
|  | ||||
|                 $response = \Response::download($imagePath, $media->file_name); | ||||
|                 ob_end_clean(); | ||||
|                 return $response; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'error' => 'receipt_not_found' | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										381
									
								
								app/Http/Controllers/FrontendController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										381
									
								
								app/Http/Controllers/FrontendController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,381 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Expense; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\Payment; | ||||
| use Laraspace\PdfSetting; | ||||
| use PDF; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Estimate; | ||||
| use Laraspace\Item; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\InvoiceTemplate; | ||||
| use Laraspace\EstimateTemplate; | ||||
| use Auth; | ||||
| use Laraspace\Mail\EstimateViewed; | ||||
| use Laraspace\Mail\InvoiceViewed; | ||||
|  | ||||
| class FrontendController extends Controller | ||||
| { | ||||
|     public function home() | ||||
|     { | ||||
|         return view('front.index'); | ||||
|     } | ||||
|  | ||||
|     public function getCustomerEstimatePdf($id) | ||||
|     { | ||||
|         $estimate = Estimate::with( | ||||
|                 'user', | ||||
|                 'items', | ||||
|                 'user.billingAddress', | ||||
|                 'user.shippingAddress' | ||||
|             ) | ||||
|             ->where('unique_hash', $id) | ||||
|             ->first(); | ||||
|  | ||||
|         $taxTypes = []; | ||||
|         $taxes = []; | ||||
|         $labels = []; | ||||
|  | ||||
|         if ($estimate->tax_per_item === 'YES') { | ||||
|             foreach ($estimate->items as $item) { | ||||
|                 foreach ($item->taxes as $tax) { | ||||
|                     if (!in_array($tax->name, $taxTypes)) { | ||||
|                         array_push($taxTypes, $tax->name); | ||||
|                         array_push($labels, $tax->name.' ('.$tax->percent.'%)'); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             foreach ($taxTypes as $taxType) { | ||||
|                 $total = 0; | ||||
|  | ||||
|                 foreach ($estimate->items as $item) { | ||||
|                     foreach ($item->taxes as $tax) { | ||||
|                         if($tax->name == $taxType) { | ||||
|                             $total += $tax->amount; | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 array_push($taxes, $total); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $estimateTemplate = EstimateTemplate::find($estimate->estimate_template_id); | ||||
|  | ||||
|         $company = Company::find($estimate->company_id); | ||||
|  | ||||
|         $logo = $company->getMedia('logo')->first(); | ||||
|  | ||||
|         if($logo) { | ||||
|             $logo = $logo->getFullUrl(); | ||||
|         } | ||||
|  | ||||
|         if ($estimate && ($estimate->status == Estimate::STATUS_SENT || $estimate->status == Estimate::STATUS_DRAFT)) { | ||||
|             $estimate->status = Estimate::STATUS_VIEWED; | ||||
|             $estimate->save(); | ||||
|             $notifyEstimateViewed = CompanySetting::getSetting( | ||||
|                 'notify_estimate_viewed', | ||||
|                 $estimate->company_id | ||||
|             ); | ||||
|  | ||||
|             if ($notifyEstimateViewed == 'YES') { | ||||
|                 $data['estimate'] = Estimate::findOrFail($estimate->id)->toArray(); | ||||
|                 $data['user'] = User::find($estimate->user_id)->toArray(); | ||||
|                 $notificationEmail = CompanySetting::getSetting( | ||||
|                     'notification_email', | ||||
|                     $estimate->company_id | ||||
|                 ); | ||||
|  | ||||
|                 \Mail::to($notificationEmail)->send(new EstimateViewed($data)); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $companyAddress = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city'])->find(1); | ||||
|  | ||||
|         $colors = [ | ||||
|             'invoice_primary_color', | ||||
|             'invoice_column_heading', | ||||
|             'invoice_field_label', | ||||
|             'invoice_field_value', | ||||
|             'invoice_body_text', | ||||
|             'invoice_description_text', | ||||
|             'invoice_border_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($estimate->company_id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'estimate' => $estimate, | ||||
|             'logo' => $logo ?? null, | ||||
|             'company_address' => $companyAddress, | ||||
|             'colors' => $colorSettings, | ||||
|             'labels' => $labels, | ||||
|             'taxes' => $taxes | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.estimate.'.$estimateTemplate->view); | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function getCustomerInvoicePdf($id) | ||||
|     { | ||||
|         $invoice = Invoice::with([ | ||||
|                 'items', | ||||
|                 'items.taxes', | ||||
|                 'user', | ||||
|                 'invoiceTemplate', | ||||
|                 'taxes' | ||||
|             ]) | ||||
|             ->where('unique_hash', $id) | ||||
|             ->first(); | ||||
|  | ||||
|         $taxTypes = []; | ||||
|         $taxes = []; | ||||
|         $labels = []; | ||||
|  | ||||
|         if ($invoice->tax_per_item === 'YES') { | ||||
|             foreach ($invoice->items as $item) { | ||||
|                 foreach ($item->taxes as $tax) { | ||||
|                     if (!in_array($tax->name, $labels)) { | ||||
|                         array_push($taxTypes, $tax->name); | ||||
|                         array_push($labels, $tax->name.' ('.$tax->percent.'%)'); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             foreach ($taxTypes as $taxType) { | ||||
|                 $total = 0; | ||||
|  | ||||
|                 foreach ($invoice->items as $item) { | ||||
|                     foreach ($item->taxes as $tax) { | ||||
|                         if($tax->name == $taxType) { | ||||
|                             $total += $tax->amount; | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 array_push($taxes, $total); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $invoiceTemplate = InvoiceTemplate::find($invoice->invoice_template_id); | ||||
|  | ||||
|         $company = Company::find($invoice->company_id); | ||||
|         $logo = $company->getMedia('logo')->first(); | ||||
|  | ||||
|         if($logo) { | ||||
|             $logo = $logo->getFullUrl(); | ||||
|         } | ||||
|  | ||||
|         if ($invoice && ($invoice->status == Invoice::STATUS_SENT || $invoice->status == Invoice::STATUS_DRAFT)) { | ||||
|             $invoice->status = Invoice::STATUS_VIEWED; | ||||
|             $invoice->viewed = true; | ||||
|             $invoice->save(); | ||||
|             $notifyInvoiceViewed = CompanySetting::getSetting( | ||||
|                 'notify_invoice_viewed', | ||||
|                 $invoice->company_id | ||||
|             ); | ||||
|  | ||||
|             if ($notifyInvoiceViewed == 'YES') { | ||||
|                 $data['invoice'] = Invoice::findOrFail($invoice->id)->toArray(); | ||||
|                 $data['user'] = User::find($invoice->user_id)->toArray(); | ||||
|                 $notificationEmail = CompanySetting::getSetting( | ||||
|                     'notification_email', | ||||
|                     $invoice->company_id | ||||
|                 ); | ||||
|  | ||||
|                 \Mail::to($notificationEmail)->send(new InvoiceViewed($data)); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $companyAddress = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city'])->find(1); | ||||
|  | ||||
|         $colors = [ | ||||
|             'invoice_primary_color', | ||||
|             'invoice_column_heading', | ||||
|             'invoice_field_label', | ||||
|             'invoice_field_value', | ||||
|             'invoice_body_text', | ||||
|             'invoice_description_text', | ||||
|             'invoice_border_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($invoice->company_id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'invoice' => $invoice, | ||||
|             'colors' => $colorSettings, | ||||
|             'company_address' => $companyAddress, | ||||
|             'logo' => $logo ?? null, | ||||
|             'labels' => $labels, | ||||
|             'taxes' => $taxes | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.invoice.'.$invoiceTemplate->view); | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function getEstimatePdf($id) | ||||
|     { | ||||
|         $estimate = Estimate::with([ | ||||
|                 'items', | ||||
|                 'items.taxes', | ||||
|                 'user', | ||||
|                 'estimateTemplate', | ||||
|                 'taxes', | ||||
|                 'taxes.taxType' | ||||
|             ]) | ||||
|             ->where('unique_hash', $id) | ||||
|             ->first(); | ||||
|  | ||||
|         $taxTypes = []; | ||||
|         $taxes = []; | ||||
|         $labels = []; | ||||
|  | ||||
|         if ($estimate->tax_per_item === 'YES') { | ||||
|             foreach ($estimate->items as $item) { | ||||
|                 foreach ($item->taxes as $tax) { | ||||
|                     if (!in_array($tax->name, $taxTypes)) { | ||||
|                         array_push($taxTypes, $tax->name); | ||||
|                         array_push($labels, $tax->name.' ('.$tax->percent.'%)'); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             foreach ($taxTypes as $taxType) { | ||||
|                 $total = 0; | ||||
|  | ||||
|                 foreach ($estimate->items as $item) { | ||||
|                     foreach ($item->taxes as $tax) { | ||||
|                         if($tax->name == $taxType) { | ||||
|                             $total += $tax->amount; | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 array_push($taxes, $total); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $estimateTemplate = EstimateTemplate::find($estimate->estimate_template_id); | ||||
|  | ||||
|         $company = Company::find($estimate->company_id); | ||||
|         $companyAddress = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city'])->find(1); | ||||
|         $logo = $company->getMedia('logo')->first(); | ||||
|  | ||||
|         if($logo) { | ||||
|             $logo = $logo->getFullUrl(); | ||||
|         } | ||||
|  | ||||
|         $colors = [ | ||||
|             'invoice_primary_color', | ||||
|             'invoice_column_heading', | ||||
|             'invoice_field_label', | ||||
|             'invoice_field_value', | ||||
|             'invoice_body_text', | ||||
|             'invoice_description_text', | ||||
|             'invoice_border_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($estimate->company_id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'estimate' => $estimate, | ||||
|             'logo' => $logo ?? null, | ||||
|             'company_address' => $companyAddress, | ||||
|             'colors' => $colorSettings, | ||||
|             'labels' => $labels, | ||||
|             'taxes' => $taxes | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.estimate.'.$estimateTemplate->view); | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function getInvoicePdf($id) | ||||
|     { | ||||
|         $invoice = Invoice::with([ | ||||
|                 'items', | ||||
|                 'items.taxes', | ||||
|                 'user', | ||||
|                 'invoiceTemplate', | ||||
|                 'taxes' | ||||
|             ]) | ||||
|             ->where('unique_hash', $id) | ||||
|             ->first(); | ||||
|  | ||||
|         $taxTypes = []; | ||||
|         $taxes = []; | ||||
|         $labels = []; | ||||
|  | ||||
|         if ($invoice->tax_per_item === 'YES') { | ||||
|             foreach ($invoice->items as $item) { | ||||
|                 foreach ($item->taxes as $tax) { | ||||
|                     if (!in_array($tax->name, $taxTypes)) { | ||||
|                         array_push($taxTypes, $tax->name); | ||||
|                         array_push($labels, $tax->name.' ('.$tax->percent.'%)'); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             foreach ($taxTypes as $taxType) { | ||||
|                 $total = 0; | ||||
|  | ||||
|                 foreach ($invoice->items as $item) { | ||||
|                     foreach ($item->taxes as $tax) { | ||||
|                         if($tax->name == $taxType) { | ||||
|                             $total += $tax->amount; | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 array_push($taxes, $total); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $invoiceTemplate = InvoiceTemplate::find($invoice->invoice_template_id); | ||||
|         $company = Company::find($invoice->company_id); | ||||
|         $companyAddress = User::with(['addresses', 'addresses.country', 'addresses.state', 'addresses.city'])->find(1); | ||||
|  | ||||
|         $logo = $company->getMedia('logo')->first(); | ||||
|  | ||||
|         if($logo) { | ||||
|             $logo = $logo->getFullUrl(); | ||||
|         } | ||||
|  | ||||
|         $colors = [ | ||||
|             'invoice_primary_color', | ||||
|             'invoice_column_heading', | ||||
|             'invoice_field_label', | ||||
|             'invoice_field_value', | ||||
|             'invoice_body_text', | ||||
|             'invoice_description_text', | ||||
|             'invoice_border_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($invoice->company_id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'invoice' => $invoice, | ||||
|             'company_address' => $companyAddress, | ||||
|             'logo' => $logo ?? null, | ||||
|             'colors' => $colorSettings, | ||||
|             'labels' => $labels, | ||||
|             'taxes' => $taxes | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.invoice.'.$invoiceTemplate->view); | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										443
									
								
								app/Http/Controllers/InvoicesController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										443
									
								
								app/Http/Controllers/InvoicesController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,443 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\CompanySetting; | ||||
| use Illuminate\Support\Collection; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\InvoiceTemplate; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\InvoiceItem; | ||||
| use Carbon\Carbon; | ||||
| use Laraspace\Item; | ||||
| use Laraspace\Mail\invoicePdf; | ||||
| use function MongoDB\BSON\toJSON; | ||||
| use Illuminate\Support\Facades\Log; | ||||
| use Laraspace\User; | ||||
| use Mailgun\Mailgun; | ||||
| use PDF; | ||||
| use Validator; | ||||
| use Laraspace\TaxType; | ||||
| use Laraspace\Tax; | ||||
|  | ||||
| class InvoicesController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $invoices = Invoice::with(['items', 'user', 'invoiceTemplate', 'taxes']) | ||||
|             ->join('users', 'users.id', '=', 'invoices.user_id') | ||||
|             ->applyFilters($request->only([ | ||||
|                 'status', | ||||
|                 'paid_status', | ||||
|                 'customer_id', | ||||
|                 'invoice_number', | ||||
|                 'from_date', | ||||
|                 'to_date', | ||||
|                 'orderByField', | ||||
|                 'orderBy', | ||||
|                 'search', | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->select('invoices.*', 'users.name') | ||||
|             ->latest() | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'invoices' => $invoices, | ||||
|             'invoiceTotalCount' => Invoice::count() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for creating a new resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function create(Request $request) | ||||
|     { | ||||
|         $tax_per_item = CompanySetting::getSetting('tax_per_item', $request->header('company')); | ||||
|         $discount_per_item = CompanySetting::getSetting('discount_per_item', $request->header('company')); | ||||
|         $nextInvoiceNumber = "INV-".Invoice::getNextInvoiceNumber(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'nextInvoiceNumber' => $nextInvoiceNumber, | ||||
|             'items' => Item::with('taxes')->whereCompany($request->header('company'))->get(), | ||||
|             'invoiceTemplates' => InvoiceTemplate::all(), | ||||
|             'tax_per_item' => $tax_per_item, | ||||
|             'discount_per_item' => $discount_per_item | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(Requests\InvoicesRequest $request) | ||||
|     { | ||||
|         $invoice_date = Carbon::createFromFormat('d/m/Y', $request->invoice_date); | ||||
|         $due_date = Carbon::createFromFormat('d/m/Y', $request->due_date); | ||||
|         $status = Invoice::STATUS_DRAFT; | ||||
|  | ||||
|         $tax_per_item = CompanySetting::getSetting('tax_per_item', $request->header('company')) ?? 'NO'; | ||||
|         $discount_per_item = CompanySetting::getSetting('discount_per_item', $request->header('company')) ?? 'NO'; | ||||
|  | ||||
|         if ($request->has('invoiceSend')) { | ||||
|             $status = Invoice::STATUS_SENT; | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::create([ | ||||
|             'invoice_date' => $invoice_date, | ||||
|             'due_date' => $due_date, | ||||
|             'invoice_number' => $request->invoice_number, | ||||
|             'reference_number' => $request->reference_number, | ||||
|             'user_id' => $request->user_id, | ||||
|             'company_id' => $request->header('company'), | ||||
|             'invoice_template_id' => $request->invoice_template_id, | ||||
|             'status' => $status, | ||||
|             'paid_status' => Invoice::STATUS_UNPAID, | ||||
|             'sub_total' => $request->sub_total, | ||||
|             'discount' => $request->discount, | ||||
|             'discount_type' => $request->discount_type, | ||||
|             'discount_val' => $request->discount_val, | ||||
|             'total' => $request->total, | ||||
|             'due_amount' => $request->total, | ||||
|             'tax_per_item' => $tax_per_item, | ||||
|             'discount_per_item' => $discount_per_item, | ||||
|             'tax' => $request->tax, | ||||
|             'notes' => $request->notes, | ||||
|             'unique_hash' => str_random(60) | ||||
|         ]); | ||||
|  | ||||
|         $invoiceItems = $request->items; | ||||
|  | ||||
|         foreach ($invoiceItems as $invoiceItem) { | ||||
|             $invoiceItem['company_id'] = $request->header('company'); | ||||
|             $item = $invoice->items()->create($invoiceItem); | ||||
|  | ||||
|             if (array_key_exists('taxes', $invoiceItem) && $invoiceItem['taxes']) { | ||||
|                 foreach ($invoiceItem['taxes'] as $tax) { | ||||
|                     $tax['company_id'] = $request->header('company'); | ||||
|  | ||||
|                     if ($tax['amount']) { | ||||
|                         $item->taxes()->create($tax); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 $tax['company_id'] = $request->header('company'); | ||||
|  | ||||
|                 if ($tax['amount']) { | ||||
|                     $invoice->taxes()->create($tax); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('invoiceSend')) { | ||||
|             $data['invoice'] = Invoice::findOrFail($invoice->id)->toArray(); | ||||
|             $data['user'] = User::find($request->user_id)->toArray(); | ||||
|  | ||||
|             $notificationEmail = CompanySetting::getSetting( | ||||
|                 'notification_email', | ||||
|                 $request->header('company') | ||||
|             ); | ||||
|  | ||||
|             $email = $data['user']['email']; | ||||
|  | ||||
|             if (!$email) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'user_email_does_not_exist' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             if (!$notificationEmail) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'notification_email_does_not_exist' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             \Mail::to($email)->send(new invoicePdf($data, $notificationEmail)); | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::with(['items', 'user', 'invoiceTemplate', 'taxes'])->find($invoice->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'url' => url('/invoices/pdf/'.$invoice->unique_hash), | ||||
|             'invoice' => $invoice | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  int $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show(Request $request, $id) | ||||
|     { | ||||
|         $invoice = Invoice::with([ | ||||
|             'items', | ||||
|             'items.taxes', | ||||
|             'user', | ||||
|             'invoiceTemplate', | ||||
|             'taxes.taxType' | ||||
|         ])->find($id); | ||||
|  | ||||
|         $siteData = [ | ||||
|             'invoice' => $invoice, | ||||
|             'shareable_link' => url('/invoices/pdf/' . $invoice->unique_hash) | ||||
|         ]; | ||||
|  | ||||
|         return response()->json($siteData); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  int $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit(Request $request,$id) | ||||
|     { | ||||
|         $invoice = Invoice::with([ | ||||
|             'items', | ||||
|             'items.taxes', | ||||
|             'user', | ||||
|             'invoiceTemplate', | ||||
|             'taxes.taxType' | ||||
|         ])->find($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'nextInvoiceNumber' => $invoice->invoice_number, | ||||
|             'invoice' => $invoice, | ||||
|             'invoiceTemplates' => InvoiceTemplate::all(), | ||||
|             'tax_per_item' => $invoice->tax_per_item, | ||||
|             'discount_per_item' => $invoice->discount_per_item, | ||||
|             'shareable_link' => url('/invoices/pdf/'.$invoice->unique_hash) | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * @param  int $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update(Requests\InvoicesRequest $request, $id) | ||||
|     { | ||||
|         $invoice_date = Carbon::createFromFormat('d/m/Y', $request->invoice_date); | ||||
|         $due_date = Carbon::createFromFormat('d/m/Y', $request->due_date); | ||||
|  | ||||
|         $invoice = Invoice::find($id); | ||||
|         $oldAmount = $invoice->total; | ||||
|  | ||||
|         if ($oldAmount != $request->total) { | ||||
|             $oldAmount = (int)round($request->total) - (int)$oldAmount; | ||||
|         } else { | ||||
|             $oldAmount = 0; | ||||
|         } | ||||
|  | ||||
|         $invoice->due_amount = ($invoice->due_amount + $oldAmount); | ||||
|  | ||||
|         if ($invoice->due_amount == 0 && $invoice->paid_status != Invoice::STATUS_PAID) { | ||||
|             $invoice->status = Invoice::STATUS_COMPLETED; | ||||
|             $invoice->paid_status = Invoice::STATUS_PAID; | ||||
|         } elseif ($invoice->due_amount < 0 && $invoice->paid_status != Invoice::STATUS_UNPAID) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'invalid_due_amount' | ||||
|             ]); | ||||
|         } elseif ($invoice->due_amount != 0 && $invoice->paid_status == Invoice::STATUS_PAID) { | ||||
|             $invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID; | ||||
|         } | ||||
|  | ||||
|         $invoice->invoice_date = $invoice_date; | ||||
|         $invoice->due_date = $due_date; | ||||
|         $invoice->invoice_number = $request->invoice_number; | ||||
|         $invoice->reference_number = $request->reference_number; | ||||
|         $invoice->user_id = $request->user_id; | ||||
|         $invoice->invoice_template_id = $request->invoice_template_id; | ||||
|         $invoice->sub_total = $request->sub_total; | ||||
|         $invoice->total = $request->total; | ||||
|         $invoice->discount = $request->discount; | ||||
|         $invoice->discount_type = $request->discount_type; | ||||
|         $invoice->discount_val = $request->discount_val; | ||||
|         $invoice->tax = $request->tax; | ||||
|         $invoice->notes = $request->notes; | ||||
|         $invoice->save(); | ||||
|  | ||||
|         $oldItems = $invoice->items->toArray(); | ||||
|         $oldTaxes = $invoice->taxes->toArray(); | ||||
|         $invoiceItems = $request->items; | ||||
|  | ||||
|         foreach ($oldItems as $oldItem) { | ||||
|             InvoiceItem::destroy($oldItem['id']); | ||||
|         } | ||||
|  | ||||
|         foreach ($oldTaxes as $oldTax) { | ||||
|             Tax::destroy($oldTax['id']); | ||||
|         } | ||||
|  | ||||
|         foreach ($invoiceItems as $invoiceItem) { | ||||
|             $invoiceItem['company_id'] = $request->header('company'); | ||||
|             $item = $invoice->items()->create($invoiceItem); | ||||
|  | ||||
|             if (array_key_exists('taxes', $invoiceItem) && $invoiceItem['taxes']) { | ||||
|                 foreach ($invoiceItem['taxes'] as $tax) { | ||||
|                     $tax['company_id'] = $request->header('company'); | ||||
|  | ||||
|                     if ($tax['amount']) { | ||||
|                         $item->taxes()->create($tax); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 $tax['company_id'] = $request->header('company'); | ||||
|  | ||||
|                 if ($tax['amount']) { | ||||
|                     $invoice->taxes()->create($tax); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::with(['items', 'user', 'invoiceTemplate', 'taxes'])->find($invoice->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'url' => url('/invoices/pdf/' . $invoice->unique_hash), | ||||
|             'invoice' => $invoice, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  int $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         $invoice = Invoice::find($id); | ||||
|  | ||||
|         if ($invoice->payments()->exists() && $invoice->payments()->count() > 0) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'payment_attached' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::destroy($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         foreach ($request->id as $id) { | ||||
|             $invoice = Invoice::find($id); | ||||
|  | ||||
|             if ($invoice->payments()->exists() && $invoice->payments()->count() > 0) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'payment_attached' | ||||
|                 ]); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $invoice = Invoice::destroy($request->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function sendInvoice(Request $request) | ||||
|     { | ||||
|         $invoice = Invoice::findOrFail($request->id); | ||||
|  | ||||
|         if ($invoice->status == Invoice::STATUS_DRAFT) { | ||||
|             $invoice->status = Invoice::STATUS_SENT; | ||||
|             $invoice->sent = true; | ||||
|             $invoice->save(); | ||||
|         } | ||||
|  | ||||
|         $data['invoice'] = $invoice->toArray(); | ||||
|         $userId = $data['invoice']['user_id']; | ||||
|         $data['user'] = User::find($userId)->toArray(); | ||||
|         $email = $data['user']['email']; | ||||
|         $notificationEmail = CompanySetting::getSetting( | ||||
|             'notification_email', | ||||
|             $request->header('company') | ||||
|         ); | ||||
|  | ||||
|         if (!$email) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'user_email_does_not_exist' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         if (!$notificationEmail) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'notification_email_does_not_exist' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         \Mail::to($email)->send(new invoicePdf($data, $notificationEmail)); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function markAsSent(Request $request) | ||||
|     { | ||||
|         $invoice = Invoice::findOrFail($request->id); | ||||
|         $invoice->status = Invoice::STATUS_SENT; | ||||
|         $invoice->sent = true; | ||||
|         $invoice->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function markAsPaid(Request $request) | ||||
|     { | ||||
|         $invoice = Invoice::findOrFail($request->id); | ||||
|         $invoice->status = Invoice::STATUS_COMPLETED; | ||||
|         $invoice->paid_status = Invoice::STATUS_PAID; | ||||
|         $invoice->due_amount = 0; | ||||
|         $invoice->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getCustomersUnpaidInvoices(Request $request, $id) | ||||
|     { | ||||
|         $invoices = Invoice::where('paid_status', '<>', Invoice::STATUS_PAID) | ||||
|             ->where('user_id', $id)->where('due_amount', '>', 0) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'invoices' => $invoices | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										129
									
								
								app/Http/Controllers/ItemsController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								app/Http/Controllers/ItemsController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,129 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\Item; | ||||
| use Laraspace\TaxType; | ||||
| use Laraspace\Tax; | ||||
| use Laraspace\User; | ||||
|  | ||||
| class ItemsController extends Controller | ||||
| { | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $items = Item::applyFilters($request->only([ | ||||
|                 'search', | ||||
|                 'price', | ||||
|                 'unit', | ||||
|                 'orderByField', | ||||
|                 'orderBy' | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->latest() | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'items' => $items, | ||||
|             'taxTypes' => TaxType::latest()->get() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function edit(Request $request, $id) | ||||
|     { | ||||
|         $item = Item::with('taxes')->find($id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'item' => $item, | ||||
|             'taxes' => Tax::whereCompany($request->header('company')) | ||||
|                 ->latest() | ||||
|                 ->get() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function store(Requests\ItemsRequest $request) | ||||
|     { | ||||
|         $item = new Item(); | ||||
|         $item->name = $request->name; | ||||
|         $item->unit = $request->unit; | ||||
|         $item->description = $request->description; | ||||
|         $item->company_id = $request->header('company'); | ||||
|         $item->price = $request->price; | ||||
|         $item->save(); | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 $item->taxes()->create($tax); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $item = Item::with('taxes')->find($item->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'item' => $item | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(Requests\ItemsRequest $request, $id) | ||||
|     { | ||||
|         $item = Item::find($id); | ||||
|         $item->name = $request->name; | ||||
|         $item->unit = $request->unit; | ||||
|         $item->description = $request->description; | ||||
|         $item->price = $request->price; | ||||
|         $item->save(); | ||||
|  | ||||
|         if ($request->has('taxes')) { | ||||
|             foreach ($request->taxes as $tax) { | ||||
|                 $item->taxes()->updateOrCreate( | ||||
|                     ['tax_type_id' => $tax['tax_type_id']], | ||||
|                     ['amount' => $tax['amount'], 'percent' => $tax['percent'], 'percent' => $tax['name']] | ||||
|                 ); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $item = Item::with('taxes')->find($item->id); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'item' => $item | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         $data = Item::deleteItem($id); | ||||
|  | ||||
|         if (!$data) { | ||||
|             return response()->json([ | ||||
|                 'error' => 'item_attached' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => $data | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         $items = []; | ||||
|         foreach ($request->id as $id) { | ||||
|             $item = Item::deleteItem($id); | ||||
|             if (!$item) { | ||||
|                 array_push($items, $id); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (empty($items)) { | ||||
|             return response()->json([ | ||||
|                 'success' => true | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'items' => $items | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										31
									
								
								app/Http/Controllers/LocationController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								app/Http/Controllers/LocationController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,31 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Country; | ||||
| use Laraspace\State; | ||||
| use Laraspace\City; | ||||
|  | ||||
| class LocationController extends Controller | ||||
| { | ||||
|     public function getCountries() | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'countries' => Country::all() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getStates($id) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'states' => Country::find($id)->states | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function getCities($id) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'cities' => State::find($id)->cities | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										224
									
								
								app/Http/Controllers/OnboardingController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										224
									
								
								app/Http/Controllers/OnboardingController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,224 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\Address; | ||||
| use Laraspace\Http\Requests\ProfileRequest; | ||||
| use Laraspace\Http\Requests\CompanyRequest; | ||||
| use Laraspace\Http\Requests\CompanySettingRequest; | ||||
| use Laraspace\Space\DateFormatter; | ||||
| use Laraspace\Space\TimeZones; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\Setting; | ||||
| use Laraspace\CompanySetting; | ||||
| use Illuminate\Support\Facades\DB; | ||||
| use Illuminate\Support\Facades\Artisan; | ||||
|  | ||||
| class OnboardingController extends Controller | ||||
| { | ||||
|     public function getOnboardingData(Request $request) | ||||
|     { | ||||
|         if (!\Storage::disk('local')->has('database_created')) { | ||||
|             return response()->json([ | ||||
|                 'profile_complete' => '0' | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         $setting = Setting::getSetting('profile_complete'); | ||||
|  | ||||
|         if ($setting !== 'COMPLETED' && $setting < 4){ | ||||
|             return response()->json([ | ||||
|                 'profile_complete' => $setting | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         $date_formats = DateFormatter::get_list(); | ||||
|         $time_zones = TimeZones::get_list(); | ||||
|         $languages = [ | ||||
|             ["code"=>"en", "name" => "English"], | ||||
|             ["code"=>"de", "name" => "German"], | ||||
|         ]; | ||||
|         $fiscal_years = [ | ||||
|             ['key' => 'january-december' , 'value' => '1-12'], | ||||
|             ['key' => 'february-january' , 'value' => '2-1'], | ||||
|             ['key' => 'march-february'   , 'value' => '3-2'], | ||||
|             ['key' => 'april-march'      , 'value' => '4-3'], | ||||
|             ['key' => 'may-april'        , 'value' => '5-4'], | ||||
|             ['key' => 'june-may'         , 'value' => '6-5'], | ||||
|             ['key' => 'july-june'        , 'value' => '7-6'], | ||||
|             ['key' => 'august-july'      , 'value' => '8-7'], | ||||
|             ['key' => 'september-august' , 'value' => '9-8'], | ||||
|             ['key' => 'october-september', 'value' => '10-9'], | ||||
|             ['key' => 'november-october' , 'value' => '11-10'], | ||||
|             ['key' => 'december-november', 'value' => '12-11'], | ||||
|         ]; | ||||
|         $user = User::with([ | ||||
|             'addresses', | ||||
|             'addresses.country', | ||||
|             'addresses.state', | ||||
|             'addresses.city', | ||||
|             'company' | ||||
|         ])->find(1); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user, | ||||
|             'profile_complete' => $setting, | ||||
|             'languages' => $languages, | ||||
|             'date_formats' => $date_formats, | ||||
|             'time_zones' => $time_zones, | ||||
|             'fiscal_years' => $fiscal_years, | ||||
|             'currencies' => Currency::all() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function adminProfile(ProfileRequest $request) | ||||
|     { | ||||
|         $setting = Setting::getSetting('profile_complete'); | ||||
|  | ||||
|         if ($setting == '1' || $setting == 'COMPLETED') { | ||||
|             return response()->json(['error' => 'Profile already created.']); | ||||
|         } else { | ||||
|             Setting::setSetting('profile_complete', 5); | ||||
|         } | ||||
|  | ||||
|         $user = User::find(1); | ||||
|         $user->name = $request->name; | ||||
|         $user->email = $request->email; | ||||
|  | ||||
|         if ($request->has('password')) { | ||||
|             $user->password = bcrypt($request->password); | ||||
|         } | ||||
|  | ||||
|         $user->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function adminCompany(CompanyRequest $request) | ||||
|     { | ||||
|         $setting = Setting::getSetting('profile_complete'); | ||||
|  | ||||
|         if ($setting == '6' || $setting == 'COMPLETED') { | ||||
|             return response()->json(['error' => 'Company already created.']); | ||||
|         } else { | ||||
|             Setting::setSetting('profile_complete', 6); | ||||
|         } | ||||
|  | ||||
|         $user = User::find(1); | ||||
|         $company = $user->company; | ||||
|  | ||||
|         if (!$company) { | ||||
|             $company = new Company(); | ||||
|         } | ||||
|  | ||||
|         $company->name = $request->name; | ||||
|         $company->unique_hash = str_random(60); | ||||
|         $company->save(); | ||||
|         $user->company()->associate($company); | ||||
|         $user->save(); | ||||
|  | ||||
|         if ($request->has('logo') && $request->logo !== null && $request->logo !== 'undefined' ) { | ||||
|             $company->addMediaFromRequest('logo')->toMediaCollection('logo'); | ||||
|  | ||||
|         } | ||||
|  | ||||
|         $fields = $request->only([ | ||||
|             'address_street_1', | ||||
|             'address_street_2', | ||||
|             'city_id', | ||||
|             'state_id', | ||||
|             'country_id', | ||||
|             'zip', | ||||
|             'phone' | ||||
|         ]); | ||||
|         $address = Address::updateOrCreate(['user_id' => 1], $fields); | ||||
|         $user = User::with('addresses', 'company')->find(1); | ||||
|  | ||||
|         CompanySetting::setSetting( | ||||
|             'notification_email', | ||||
|             $user->email, | ||||
|             $company->id | ||||
|         ); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function companySettings(CompanySettingRequest $request) | ||||
|     { | ||||
|         $setting = Setting::getSetting('profile_complete'); | ||||
|  | ||||
|         if($setting == 'COMPLETED') { | ||||
|             return response()->json(['error' => 'Settings already saved.']); | ||||
|         } else { | ||||
|             Setting::setSetting('profile_complete', 'COMPLETED'); | ||||
|         } | ||||
|  | ||||
|         $user = User::find(1); | ||||
|  | ||||
|         $sets = ['currency', | ||||
|             'time_zone', | ||||
|             'language', | ||||
|             'carbon_date_format', | ||||
|             'moment_date_format', | ||||
|             'fiscal_year' | ||||
|         ]; | ||||
|  | ||||
|         foreach ($sets as $key) { | ||||
|             CompanySetting::setSetting( | ||||
|                 $key, | ||||
|                 $request->$key, | ||||
|                 $user->company_id | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color' => '#5851D8', | ||||
|             'heading_text_color' => '#595959', | ||||
|             'section_heading_text_color' => '#040405', | ||||
|             'border_color' => '#EAF1FB', | ||||
|             'body_text_color' => '#595959', | ||||
|             'footer_text_color' => '#595959', | ||||
|             'footer_total_color' => '#5851D8', | ||||
|             'footer_bg_color' => '#F9FBFF', | ||||
|             'date_text_color' => '#A5ACC1', | ||||
|             'invoice_primary_color' => '#5851D8', | ||||
|             'invoice_column_heading' => '#55547A', | ||||
|             'invoice_field_label' => '#55547A', | ||||
|             'invoice_field_value' => '#040405', | ||||
|             'invoice_body_text' => '#040405', | ||||
|             'invoice_description_text' => '#595959', | ||||
|             'invoice_border_color' => '#EAF1FB' | ||||
|         ]; | ||||
|         foreach ($colors as $key => $value) { | ||||
|             CompanySetting::setSetting( | ||||
|                 $key, | ||||
|                 $value, | ||||
|                 $user->company_id | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|         Setting::setSetting('version','1.0.0'); | ||||
|  | ||||
|         Artisan::call('passport:install --force'); | ||||
|  | ||||
|         $client = DB::table('oauth_clients')->find(2); | ||||
|  | ||||
|         $path = base_path('.env'); | ||||
|  | ||||
|         if (file_exists($path)) { | ||||
|             file_put_contents($path, str_replace( | ||||
|                 'PROXY_OAUTH_CLIENT_SECRET='.config('auth.proxy.client_secret'), 'PROXY_OAUTH_CLIENT_SECRET='.$client->secret, file_get_contents($path) | ||||
|             )); | ||||
|         } | ||||
|  | ||||
|         $data['token'] = $user->createToken('password')->accessToken; | ||||
|  | ||||
|         return response()->json($data); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										269
									
								
								app/Http/Controllers/PaymentController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										269
									
								
								app/Http/Controllers/PaymentController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,269 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\Payment; | ||||
| use Carbon\Carbon; | ||||
| use function MongoDB\BSON\toJSON; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Http\Requests\PaymentRequest; | ||||
|  | ||||
| class PaymentController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $limit = $request->has('limit') ? $request->limit : 10; | ||||
|  | ||||
|         $payments = Payment::with('user', 'invoice') | ||||
|             ->join('users', 'users.id', '=', 'payments.user_id') | ||||
|             ->leftJoin('invoices', 'invoices.id', '=', 'payments.invoice_id') | ||||
|             ->applyFilters($request->only([ | ||||
|                 'search', | ||||
|                 'payment_number', | ||||
|                 'payment_mode', | ||||
|                 'customer_id', | ||||
|                 'orderByField', | ||||
|                 'orderBy' | ||||
|             ])) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->select('payments.*', 'users.name', 'invoices.invoice_number') | ||||
|             ->latest() | ||||
|             ->paginate($limit); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'payments' => $payments | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for creating a new resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function create(Request $request) | ||||
|     { | ||||
|         $nextPaymentNumber = 'PAY-'.Payment::getNextPaymentNumber(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customers' => User::where('role', 'customer') | ||||
|                 ->whereCompany($request->header('company')) | ||||
|                 ->get(), | ||||
|             'nextPaymentNumber' => $nextPaymentNumber | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(PaymentRequest $request) | ||||
|     { | ||||
|         $payment_date = Carbon::createFromFormat('d/m/Y', $request->payment_date); | ||||
|  | ||||
|         if ($request->has('invoice_id') && $request->invoice_id != null) { | ||||
|             $invoice = Invoice::find($request->invoice_id); | ||||
|             if ($invoice && $invoice->due_amount == $request->amount) { | ||||
|                 $invoice->status = Invoice::STATUS_COMPLETED; | ||||
|                 $invoice->paid_status = Invoice::STATUS_PAID; | ||||
|                 $invoice->due_amount = 0; | ||||
|             } elseif ($invoice && $invoice->due_amount != $request->amount) { | ||||
|                 $invoice->due_amount = (int)$invoice->due_amount - (int)$request->amount; | ||||
|                 if ($invoice->due_amount < 0) { | ||||
|                     return response()->json([ | ||||
|                         'error' => 'invalid_amount' | ||||
|                     ]); | ||||
|                 } | ||||
|                 $invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID; | ||||
|             } | ||||
|             $invoice->save(); | ||||
|         } | ||||
|  | ||||
|         $payment = Payment::create([ | ||||
|             'payment_date' => $payment_date, | ||||
|             'payment_number' => $request->payment_number, | ||||
|             'user_id' => $request->user_id, | ||||
|             'company_id' => $request->header('company'), | ||||
|             'invoice_id' => $request->invoice_id, | ||||
|             'payment_mode' => $request->payment_mode, | ||||
|             'amount' => $request->amount, | ||||
|             'notes' => $request->notes, | ||||
|         ]); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'payment' => $payment, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show($id) | ||||
|     { | ||||
|         // | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit(Request $request, $id) | ||||
|     { | ||||
|         $payment = Payment::with('user', 'invoice')->find($id); | ||||
|  | ||||
|         $invoices = Invoice::where('paid_status', '<>', Invoice::STATUS_PAID) | ||||
|             ->where('user_id', $payment->user_id)->where('due_amount', '>', 0) | ||||
|             ->whereCompany($request->header('company')) | ||||
|             ->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'customers' => User::where('role', 'customer') | ||||
|                 ->whereCompany($request->header('company')) | ||||
|                 ->get(), | ||||
|             'nextPaymentNumber' => $payment->payment_number, | ||||
|             'payment' => $payment, | ||||
|             'invoices' => $invoices | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update(PaymentRequest $request, $id) | ||||
|     { | ||||
|         $payment_date = Carbon::createFromFormat('d/m/Y', $request->payment_date); | ||||
|  | ||||
|         $payment = Payment::find($id); | ||||
|         $oldAmount = $payment->amount; | ||||
|  | ||||
|         if ($request->has('invoice_id') && $request->invoice_id && ($oldAmount != $request->amount)) { | ||||
|             $amount = (int)$request->amount - (int)$oldAmount; | ||||
|             $invoice = Invoice::find($request->invoice_id); | ||||
|             $invoice->due_amount = (int)$invoice->due_amount - (int)$amount; | ||||
|  | ||||
|             if ($invoice->due_amount < 0) { | ||||
|                 return response()->json([ | ||||
|                     'error' => 'invalid_amount' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             if ($invoice->due_amount == 0) { | ||||
|                 $invoice->status = Invoice::STATUS_COMPLETED; | ||||
|                 $invoice->paid_status = Invoice::STATUS_PAID; | ||||
|             } else { | ||||
|                 $invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID; | ||||
|             } | ||||
|  | ||||
|             $invoice->save(); | ||||
|         } | ||||
|  | ||||
|         $payment->payment_date = $payment_date; | ||||
|         $payment->payment_number = $request->payment_number; | ||||
|         $payment->user_id = $request->user_id; | ||||
|         $payment->invoice_id = $request->invoice_id; | ||||
|         $payment->payment_mode = $request->payment_mode; | ||||
|         $payment->amount = $request->amount; | ||||
|         $payment->notes = $request->notes; | ||||
|         $payment->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'payment' => $payment, | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  int  $id | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy($id) | ||||
|     { | ||||
|         $payment = Payment::find($id); | ||||
|  | ||||
|         if ($payment->invoice_id != null) { | ||||
|             $invoice = Invoice::find($payment->invoice_id); | ||||
|             $invoice->due_amount = ((int)$invoice->due_amount + (int)$payment->amount); | ||||
|  | ||||
|             if ($invoice->due_amount == $invoice->total) { | ||||
|                 $invoice->paid_status = Invoice::STATUS_UNPAID; | ||||
|             } else { | ||||
|                 $invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID; | ||||
|             } | ||||
|  | ||||
|             if ($invoice->due_date < Carbon::now()) { | ||||
|                 $invoice->status = Invoice::STATUS_OVERDUE; | ||||
|             } elseif ($invoice->viewed) { | ||||
|                 $invoice->status = Invoice::STATUS_VIEWED; | ||||
|             } elseif ($invoice->sent) { | ||||
|                 $invoice->status = Invoice::STATUS_SENT; | ||||
|             } else { | ||||
|                 $invoice->status = Invoice::STATUS_DRAFT; | ||||
|             } | ||||
|  | ||||
|             $invoice->save(); | ||||
|         } | ||||
|  | ||||
|         $payment->delete(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function delete(Request $request) | ||||
|     { | ||||
|         foreach ($request->id as $id) { | ||||
|             $payment = Payment::find($id); | ||||
|  | ||||
|             if ($payment->invoice_id != null) { | ||||
|                 $invoice = Invoice::find($payment->invoice_id); | ||||
|                 $invoice->due_amount = ((int)$invoice->due_amount + (int)$payment->amount); | ||||
|  | ||||
|                 if ($invoice->due_amount == $invoice->total) { | ||||
|                     $invoice->paid_status = Invoice::STATUS_UNPAID; | ||||
|                 } else { | ||||
|                     $invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID; | ||||
|                 } | ||||
|  | ||||
|                 if ($invoice->due_date < Carbon::now()) { | ||||
|                     $invoice->status = Invoice::STATUS_OVERDUE; | ||||
|                 } elseif ($invoice->sent) { | ||||
|                     $invoice->status = Invoice::STATUS_SENT; | ||||
|                 } elseif ($invoice->viewed) { | ||||
|                     $invoice->status = Invoice::STATUS_VIEWED; | ||||
|                 } else { | ||||
|                     $invoice->status = Invoice::STATUS_DRAFT; | ||||
|                 } | ||||
|  | ||||
|                 $invoice->save(); | ||||
|             } | ||||
|  | ||||
|             $payment->delete(); | ||||
|         } | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										38
									
								
								app/Http/Controllers/PermissionsController.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										38
									
								
								app/Http/Controllers/PermissionsController.php
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,38 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Laraspace\Space\PermissionsChecker; | ||||
| use Illuminate\Http\JsonResponse; | ||||
|  | ||||
| class PermissionsController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * @var PermissionsChecker | ||||
|      */ | ||||
|     protected $permissions; | ||||
|  | ||||
|     /** | ||||
|      * @param PermissionsChecker $checker | ||||
|      */ | ||||
|     public function __construct(PermissionsChecker $checker) | ||||
|     { | ||||
|         $this->permissions = $checker; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the permissions check page. | ||||
|      * | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function permissions() | ||||
|     { | ||||
|         $permissions = $this->permissions->check( | ||||
|             config('installer.permissions') | ||||
|         ); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'permissions' => $permissions | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										295
									
								
								app/Http/Controllers/ReportController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										295
									
								
								app/Http/Controllers/ReportController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,295 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Invoice; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\InvoiceItem; | ||||
| use Laraspace\Expense; | ||||
| use Laraspace\CompanySetting; | ||||
| use Laraspace\Tax; | ||||
| use PDF; | ||||
| use Carbon\Carbon; | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
|  | ||||
| class ReportController extends Controller | ||||
| { | ||||
|     public function customersSalesReport($hash, Request $request) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $start = Carbon::createFromFormat('d/m/Y', $request->from_date); | ||||
|         $end = Carbon::createFromFormat('d/m/Y', $request->to_date); | ||||
|  | ||||
|         $customers = User::with(['invoices' => function ($query) use ($start, $end) { | ||||
|                 $query->whereBetween( | ||||
|                     'invoice_date', | ||||
|                     [$start->format('Y-m-d'), $end->format('Y-m-d')] | ||||
|                 ) | ||||
|                 ->where('paid_status', Invoice::STATUS_PAID); | ||||
|             }]) | ||||
|             ->customer() | ||||
|             ->whereCompany($company->id) | ||||
|             ->applyInvoiceFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->get(); | ||||
|  | ||||
|         $totalAmount = 0; | ||||
|         foreach ($customers as $customer) { | ||||
|             $customerTotalAmount = 0; | ||||
|             foreach ($customer->invoices as $invoice) { | ||||
|                 $customerTotalAmount += $invoice->total; | ||||
|             } | ||||
|             $customer->totalAmount = $customerTotalAmount; | ||||
|             $totalAmount += $customerTotalAmount; | ||||
|         } | ||||
|  | ||||
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id); | ||||
|         $from_date = Carbon::createFromFormat('d/m/Y', $request->from_date)->format($dateFormat); | ||||
|         $to_date = Carbon::createFromFormat('d/m/Y', $request->to_date)->format($dateFormat); | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($company->id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'customers' => $customers, | ||||
|             'totalAmount' => $totalAmount, | ||||
|             'colorSettings' => $colorSettings, | ||||
|             'company' => $company, | ||||
|             'from_date' => $from_date, | ||||
|             'to_date' => $to_date | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.reports.sales-customers'); | ||||
|  | ||||
|         if ($request->has('download')) { | ||||
|             return $pdf->download(); | ||||
|         } | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function itemsSalesReport($hash, Request $request) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $items = InvoiceItem::with('item') | ||||
|             ->whereCompany($company->id) | ||||
|             ->applyInvoiceFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->itemAttributes() | ||||
|             ->get(); | ||||
|  | ||||
|         $totalAmount = 0; | ||||
|         foreach ($items as $item) { | ||||
|             $totalAmount += $item->total_amount; | ||||
|         } | ||||
|  | ||||
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id); | ||||
|         $from_date = Carbon::createFromFormat('d/m/Y', $request->from_date)->format($dateFormat); | ||||
|         $to_date = Carbon::createFromFormat('d/m/Y', $request->to_date)->format($dateFormat); | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($company->id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'items' => $items, | ||||
|             'colorSettings' => $colorSettings, | ||||
|             'totalAmount' => $totalAmount, | ||||
|             'company' => $company, | ||||
|             'from_date' => $from_date, | ||||
|             'to_date' => $to_date | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.reports.sales-items'); | ||||
|  | ||||
|         if ($request->has('download')) { | ||||
|             return $pdf->download(); | ||||
|         } | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function expensesReport($hash, Request $request) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $expenseCategories = Expense::with('category') | ||||
|             ->whereCompany($company->id) | ||||
|             ->applyFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->expensesAttributes() | ||||
|             ->get(); | ||||
|  | ||||
|         $totalAmount = 0; | ||||
|         foreach ($expenseCategories as $category) { | ||||
|             $totalAmount += $category->total_amount; | ||||
|         } | ||||
|  | ||||
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id); | ||||
|         $from_date = Carbon::createFromFormat('d/m/Y', $request->from_date)->format($dateFormat); | ||||
|         $to_date = Carbon::createFromFormat('d/m/Y', $request->to_date)->format($dateFormat); | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($company->id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'expenseCategories' => $expenseCategories, | ||||
|             'colorSettings' => $colorSettings, | ||||
|             'totalExpense' => $totalAmount, | ||||
|             'company' => $company, | ||||
|             'from_date' => $from_date, | ||||
|             'to_date' => $to_date | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.reports.expenses'); | ||||
|  | ||||
|         if ($request->has('download')) { | ||||
|             return $pdf->download(); | ||||
|         } | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function taxSummery($hash, Request $request) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $taxTypes = Tax::with('taxType', 'invoice', 'invoiceItem') | ||||
|             ->whereCompany($company->id) | ||||
|             ->whereInvoicesFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->taxAttributes() | ||||
|             ->get(); | ||||
|  | ||||
|         $totalAmount = 0; | ||||
|         foreach ($taxTypes as $taxType) { | ||||
|             $totalAmount += $taxType->total_tax_amount; | ||||
|         } | ||||
|  | ||||
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id); | ||||
|         $from_date = Carbon::createFromFormat('d/m/Y', $request->from_date)->format($dateFormat); | ||||
|         $to_date = Carbon::createFromFormat('d/m/Y', $request->to_date)->format($dateFormat); | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|  | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($company->id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'taxTypes' => $taxTypes, | ||||
|             'totalTaxAmount' => $totalAmount, | ||||
|             'colorSettings' => $colorSettings, | ||||
|             'company' => $company, | ||||
|             'from_date' => $from_date, | ||||
|             'to_date' => $to_date | ||||
|         ]); | ||||
|  | ||||
|         $pdf = PDF::loadView('app.pdf.reports.tax-summary'); | ||||
|  | ||||
|         if ($request->has('download')) { | ||||
|             return $pdf->download(); | ||||
|         } | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
|  | ||||
|     public function profitLossReport($hash, Request $request) | ||||
|     { | ||||
|         $company = Company::where('unique_hash', $hash)->first(); | ||||
|  | ||||
|         $invoicesAmount = Invoice::whereCompany($company->id) | ||||
|             ->applyFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->wherePaidStatus(Invoice::STATUS_PAID) | ||||
|             ->sum('total'); | ||||
|  | ||||
|         $expenseCategories = Expense::with('category') | ||||
|             ->whereCompany($company->id) | ||||
|             ->applyFilters($request->only(['from_date', 'to_date'])) | ||||
|             ->expensesAttributes() | ||||
|             ->get(); | ||||
|  | ||||
|         $totalAmount = 0; | ||||
|         foreach ($expenseCategories as $category) { | ||||
|             $totalAmount += $category->total_amount; | ||||
|         } | ||||
|  | ||||
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id); | ||||
|         $from_date = Carbon::createFromFormat('d/m/Y', $request->from_date)->format($dateFormat); | ||||
|         $to_date = Carbon::createFromFormat('d/m/Y', $request->to_date)->format($dateFormat); | ||||
|  | ||||
|         $colors = [ | ||||
|             'primary_text_color', | ||||
|             'heading_text_color', | ||||
|             'section_heading_text_color', | ||||
|             'border_color', | ||||
|             'body_text_color', | ||||
|             'footer_text_color', | ||||
|             'footer_total_color', | ||||
|             'footer_bg_color', | ||||
|             'date_text_color' | ||||
|         ]; | ||||
|         $colorSettings = CompanySetting::whereIn('option', $colors) | ||||
|             ->whereCompany($company->id) | ||||
|             ->get(); | ||||
|  | ||||
|         view()->share([ | ||||
|             'company' => $company, | ||||
|             'income' => $invoicesAmount, | ||||
|             'expenseCategories' => $expenseCategories, | ||||
|             'totalExpense' => $totalAmount, | ||||
|             'colorSettings' => $colorSettings, | ||||
|             'company' => $company, | ||||
|             'from_date' => $from_date, | ||||
|             'to_date' => $to_date | ||||
|         ]); | ||||
|         $pdf = PDF::loadView('app.pdf.reports.profit-loss'); | ||||
|  | ||||
|         if ($request->has('download')) { | ||||
|             return $pdf->download(); | ||||
|         } | ||||
|  | ||||
|         return $pdf->stream(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										42
									
								
								app/Http/Controllers/RequirementsController.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										42
									
								
								app/Http/Controllers/RequirementsController.php
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,42 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Laraspace\Space\RequirementsChecker; | ||||
| use Illuminate\Http\JsonResponse; | ||||
|  | ||||
| class RequirementsController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * @var RequirementsChecker | ||||
|      */ | ||||
|     protected $requirements; | ||||
|  | ||||
|     /** | ||||
|      * @param RequirementsChecker $checker | ||||
|      */ | ||||
|     public function __construct(RequirementsChecker $checker) | ||||
|     { | ||||
|         $this->requirements = $checker; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the requirements page. | ||||
|      * | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function requirements() | ||||
|     { | ||||
|         $phpSupportInfo = $this->requirements->checkPHPversion( | ||||
|             config('installer.core.minPhpVersion') | ||||
|         ); | ||||
|         $requirements = $this->requirements->check( | ||||
|             config('installer.requirements') | ||||
|         ); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'phpSupportInfo' => $phpSupportInfo, | ||||
|             'requirements' => $requirements | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								app/Http/Controllers/SettingsController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								app/Http/Controllers/SettingsController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Setting; | ||||
|  | ||||
| class SettingsController extends Controller | ||||
| { | ||||
|     public function getAppVersion(Request $request) | ||||
|     { | ||||
|         $version = Setting::getSetting('version', $request->header('company')); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'version' => $version, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										126
									
								
								app/Http/Controllers/TaxTypeController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								app/Http/Controllers/TaxTypeController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,126 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Laraspace\TaxType; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Http\Requests\TaxTypeRequest; | ||||
| use Illuminate\Http\Request; | ||||
|  | ||||
| class TaxTypeController extends Controller | ||||
| { | ||||
|     /** | ||||
|      * Display a listing of the resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function index(Request $request) | ||||
|     { | ||||
|         $taxTypes = TaxType::whereCompany($request->header('company')) | ||||
|             ->latest() | ||||
|             ->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'taxTypes' => $taxTypes | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for creating a new resource. | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function create() | ||||
|     { | ||||
|         // | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Store a newly created resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function store(TaxTypeRequest $request) | ||||
|     { | ||||
|         $taxType = new TaxType(); | ||||
|         $taxType->name = $request->name; | ||||
|         $taxType->percent = $request->percent; | ||||
|         $taxType->description = $request->description; | ||||
|         if ($request->has('compound_tax')) { | ||||
|             $taxType->compound_tax = $request->compound_tax; | ||||
|         } | ||||
|         $taxType->company_id = $request->header('company'); | ||||
|         $taxType->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'taxType' => $taxType, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Display the specified resource. | ||||
|      * | ||||
|      * @param  \Laraspace\TaxType  $taxType | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function show(TaxType $taxType) | ||||
|     { | ||||
|         // | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the form for editing the specified resource. | ||||
|      * | ||||
|      * @param  \Laraspace\TaxType  $taxType | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function edit(TaxType $taxType) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'taxType' => $taxType | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Update the specified resource in storage. | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request  $request | ||||
|      * @param  \Laraspace\TaxType  $taxType | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function update(TaxTypeRequest $request, TaxType $taxType) | ||||
|     { | ||||
|         $taxType->name = $request->name; | ||||
|         $taxType->percent = $request->percent; | ||||
|         $taxType->description = $request->description; | ||||
|         if ($request->has('collective_tax')) { | ||||
|             $taxType->collective_tax = $request->collective_tax; | ||||
|         } | ||||
|         $taxType->compound_tax = $request->compound_tax; | ||||
|         $taxType->save(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'taxType' => $taxType, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param  \Laraspace\TaxType  $taxType | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function destroy(TaxType $taxType) | ||||
|     { | ||||
|         if ($taxType->taxes() && $taxType->taxes()->count() > 0) { | ||||
|             return response()->json([ | ||||
|                 'success' => false | ||||
|             ]); | ||||
|         } | ||||
|         $taxType->delete(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'success' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										74
									
								
								app/Http/Controllers/UsersController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								app/Http/Controllers/UsersController.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,74 @@ | ||||
| <?php | ||||
| namespace Laraspace\Http\Controllers; | ||||
|  | ||||
| use Illuminate\Http\Request; | ||||
| use Laraspace\Http\Requests; | ||||
| use Laraspace\User; | ||||
| use Laraspace\Currency; | ||||
| use Laraspace\Setting; | ||||
| use Laraspace\Item; | ||||
| use Laraspace\TaxType; | ||||
| use DB; | ||||
| use Carbon\Carbon; | ||||
| use Auth; | ||||
| use Laraspace\Company; | ||||
| use Laraspace\CompanySetting; | ||||
|  | ||||
| class UsersController extends Controller | ||||
| { | ||||
|     public function getBootstrap(Request $request) | ||||
|     { | ||||
|         $user = Auth::user(); | ||||
|  | ||||
|         $company = $request->header('company') ?? 1; | ||||
|  | ||||
|         $customers = User::with('billingAddress', 'shippingAddress') | ||||
|             ->customer() | ||||
|             ->whereCompany($company) | ||||
|             ->latest() | ||||
|             ->get(); | ||||
|  | ||||
|         $currencies = Currency::latest()->get(); | ||||
|  | ||||
|         $default_language = CompanySetting::getSetting('language', $company); | ||||
|  | ||||
|         $default_currency = Currency::findOrFail( | ||||
|             CompanySetting::getSetting('currency', $company) | ||||
|         ); | ||||
|  | ||||
|         $moment_date_format = CompanySetting::getSetting( | ||||
|             'moment_date_format', | ||||
|             $request->header('company') | ||||
|         ); | ||||
|  | ||||
|         $fiscal_year = CompanySetting::getSetting( | ||||
|             'fiscal_year', | ||||
|             $request->header('company') | ||||
|         ); | ||||
|  | ||||
|         $items = Item::all(); | ||||
|  | ||||
|         $taxTypes = TaxType::latest()->get(); | ||||
|  | ||||
|         return response()->json([ | ||||
|             'user' => $user, | ||||
|             'customers' => $customers, | ||||
|             'currencies' => $currencies, | ||||
|             'default_currency' => $default_currency, | ||||
|             'default_language' => $default_language, | ||||
|             'company' => $user->company, | ||||
|             'companies' => Company::all(), | ||||
|             'items' => $items, | ||||
|             'taxTypes' => $taxTypes, | ||||
|             'moment_date_format' => $moment_date_format, | ||||
|             'fiscal_year' => $fiscal_year, | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function ping() | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'success' => 'crater-self-hosted' | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user