InvoiceController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\CompanyModel;
  7. use App\Models\AddressBookModel;
  8. use App\Models\Invoice;
  9. use App\Models\InvoiceItems;
  10. use App\Models\ServiceList;
  11. use App\Models\Journal;
  12. use App\Models\AccountHead;
  13. use App\User;
  14. use App\Models\CompanyActivity;
  15. class InvoiceController extends Controller
  16. {
  17. public function __construct(){
  18. $this->middleware('auth');
  19. }
  20. function index()
  21. {
  22. if(\Auth::user()->utype==1 || \Auth::user()->utype==3){
  23. if(\Auth::user()->utype==3)
  24. {
  25. $uid=\Auth::user()->id;
  26. $data['invoices']=Invoice::with(['items','items.service','client','company'])->where('client_id',$uid)->orderBy('id','desc')->get();
  27. }
  28. else{
  29. $data['invoices']=Invoice::with(['items','items.service','client'])->orderBy('id','desc')->get();
  30. }
  31. $data['account_types']=AccountHead::where('type_id',1)->get();
  32. $data['title']='Invoice List';
  33. return view('admin.invoice.index',$data);
  34. }
  35. else
  36. {
  37. return "You Don't Have Access";
  38. }
  39. }
  40. function create()
  41. {
  42. $data['code']=$this->generate_inv_id();
  43. $data['companies']=User::where('utype',3)->orderBy('name','asc')->get(['id','company']);
  44. $data['client_list']=CompanyModel::where('status',2)->orderBy('name','asc')->get();
  45. $data['services']=ServiceList::orderBy('name','asc')->get(['id','name']);
  46. $data['title']='Create Invoice';
  47. return view('admin.invoice.create',$data);
  48. }
  49. function store(Request $req)
  50. {
  51. /*$this->validate($req,[
  52. 'invoice_code'=>'required|unique:invoice'
  53. ]);*/
  54. $code=$req->get('invoice_code');
  55. $file_name='';
  56. if($req->hasFile('attachment')){
  57. $allowed=['docx','doc','pdf','jpg','jpeg','png'];
  58. $file=$req->file('attachment');
  59. $ext=$file->getClientOriginalExtension();
  60. if(in_array($ext, $allowed))
  61. {
  62. $file_name=$code.'.'.$ext;
  63. }
  64. $file->move('assets/invoice/',$file_name);
  65. }
  66. $invoice=new Invoice();
  67. $invoice->invoice_code=$code;
  68. $invoice->client_id=$req->get('client');
  69. $invoice->invoice_date=$req->get('invoice_date');
  70. $invoice->due_date=$req->get('due_date');
  71. $invoice->notes=$req->get('note');
  72. $invoice->po_no=$req->get('po_no');
  73. $invoice->currency=$req->get('currency');
  74. $invoice->tax=$req->get('tax');
  75. $invoice->vat=$req->get('vat');
  76. $invoice->attachment=$file_name;
  77. $invoice->created_by=\Auth::user()->id;
  78. $invoice->status=0;
  79. $saved=$invoice->save();
  80. if($saved)
  81. {
  82. $service=$req->get('service');
  83. $payable=$req->get('payable_amount');
  84. $paid=$req->get('paid_amount');
  85. $particular=$req->get('particular');
  86. if(count($payable)>0)
  87. {
  88. $indx=0;
  89. foreach($payable as $row){
  90. $items=new InvoiceItems();
  91. $items->invoice_id=$invoice->id;
  92. $items->particular=$particular[$indx];
  93. $items->service_id=$service[$indx];
  94. $items->payable_amount=$payable[$indx];
  95. $items->paid_amount=$paid[$indx];
  96. $items->save();
  97. $indx++;
  98. }
  99. }
  100. $inv=Invoice::with(['items','items.service','client','company'])->orderBy('id','desc')->find($invoice->id);
  101. date_default_timezone_set("Asia/Dhaka");
  102. $date = date("Y-m-d h:i:s");
  103. $sav1 = new CompanyActivity;
  104. $sav1->activity = "Invoice Created";
  105. $sav1->company_id = $req->get('client');
  106. $sav1->user_id = 2;
  107. $sav1->created_at = $date;
  108. $sav1->date = $date;
  109. $sav1->save();
  110. $data['invoice']=$inv;
  111. //dd($inv);
  112. $company_id = $inv->company?$inv->company->id:'';
  113. $client_email=User::where('company',$company_id)->pluck('email')->toArray();
  114. foreach ($client_email as $value) {
  115. \Mail::send('emails.invoice_created', $data, function ($message) use($value,$file_name) {
  116. $message->from('m.revinr@gmail.com', 'Revinr CMS');
  117. //$message->sender('john@johndoe.com', 'John Doe');
  118. $message->to($value, 'Revinr CMS');
  119. $message->to('info@revinr.com', 'Revinr CMS');
  120. // $message->cc('john@johndoe.com', 'Revinr CMS');
  121. $message->bcc('shamim@revinr.com', 'Revinr CMS');
  122. //$message->replyTo('john@johndoe.com', 'John Doe');
  123. $message->subject('An invoice has been generated from revinr');
  124. // $message->priority(3);
  125. $message->attach('assets/invoice/'.$file_name);
  126. });
  127. }
  128. //});
  129. }
  130. return redirect()->back()->with('message','Invoice created successfully !');
  131. }
  132. function invoice_created_email($id)
  133. {
  134. $inv=Invoice::with(['items','items.service','client','company'])->orderBy('id','desc')->find($id);
  135. $data['invoice']=$inv;
  136. /*$client_email=$inv->client?$inv->client->email:'';*/
  137. $company_id = $inv->company?$inv->company->id:'';
  138. $client_email=User::where('company',$company_id)->pluck('email')->toArray();
  139. foreach ($client_email as $value) {
  140. \Mail::send('emails.invoice_created', $data, function ($message) use($value) {
  141. $message->from('m.revinr@gmail.com', 'Revinr CMS');
  142. //$message->sender('john@johndoe.com', 'John Doe');
  143. $message->to($value, 'Revinr CMS');
  144. $message->to('info@revinr.com', 'Revinr CMS');
  145. $message->cc('helaldu@gmail.com', 'Revinr CMS');
  146. $message->bcc('shamim@revinr.com', 'Revinr CMS');
  147. $message->subject('An invoice has been generated from revinr');
  148. });
  149. }
  150. return 'ok';
  151. }
  152. function edit($id)
  153. {
  154. $data['title']='Edit Invoice';
  155. return view('admin.invoice.edit',$data);
  156. }
  157. function update(Request $req)
  158. {
  159. }
  160. function delete($id)
  161. {
  162. }
  163. function show($id)
  164. {
  165. $data['invoice']=Invoice::with(['items','items.service','client'])->orderBy('id','desc')->find($id);
  166. $data['title']='View Invoice';
  167. return view('admin.invoice.show',$data);
  168. }
  169. function generate_inv_id()
  170. {
  171. $inv=Invoice::orderBy('id','desc')->first();
  172. $code="0000001";
  173. if($inv)
  174. {
  175. if(!empty($inv->invoice_code))
  176. {
  177. $inv_code=(int)$inv->invoice_code;
  178. $inv_code=$inv_code+1;
  179. if($inv_code<10){
  180. $code="000000".$inv_code;
  181. }
  182. elseif($inv_code<100){
  183. $code="00000".$inv_code;
  184. }
  185. elseif($inv_code<1000){
  186. $code="0000".$inv_code;
  187. }
  188. elseif($inv_code<10000){
  189. $code="000".$inv_code;
  190. }
  191. elseif($inv_code<100000){
  192. $code="00".$inv_code;
  193. }
  194. elseif($inv_code<1000000){
  195. $code="0".$inv_code;
  196. }
  197. elseif($inv_code<10000000){
  198. $code=$inv_code;
  199. }
  200. }
  201. }
  202. return $code;
  203. }
  204. function make_payment(Request $req)
  205. {
  206. $id=$req->get('id');
  207. $amount=$req->get('amount');
  208. $invoice=Invoice::find($id);
  209. $invoice->status=1;
  210. $invoice->save();
  211. $journal=new Journal();
  212. $journal->ref_id=$req->get('acc_id');
  213. $journal->debit_account=$req->get('mode_of_payment');
  214. $journal->credit_account=$req->get('acc_id');
  215. $journal->description=$req->get('description');
  216. $journal->amount=$amount;
  217. $journal->is_approaved=0;
  218. $journal->created_by=\Auth::user()->id;
  219. $journal->created_at=$req->get('date',date('Y-m-d'));
  220. $journal->client_id=$req->get('client');
  221. $journal->save();
  222. $inv=Invoice::with(['items','items.service','client','company'])->orderBy('id','desc')->find($id);
  223. $data['invoice']=$inv;
  224. /*$client_email=$inv->client?$inv->client->email:'';*/
  225. date_default_timezone_set("Asia/Dhaka");
  226. $date = date("Y-m-d h:i:s");
  227. $sav1 = new CompanyActivity;
  228. $sav1->activity = "Invoice Created";
  229. $sav1->company_id = $req->get('client');
  230. $sav1->user_id = 2;
  231. $sav1->created_at = $date;
  232. $sav1->date = $date;
  233. $sav1->save();
  234. $company_id = $inv->company?$inv->company->id:'';
  235. $client_email=User::where('company',$company_id)->pluck('email')->toArray();
  236. foreach ($client_email as $value) {
  237. \Mail::send('emails.payment_receipt', $data, function ($message) use($value) {
  238. $message->from('m.revinr@gmail.com', 'Revinr CMS');
  239. //$message->sender('john@johndoe.com', 'John Doe');
  240. $message->to($value, 'Revinr CMS');
  241. $message->to('info@revinr.com', 'Revinr CMS');
  242. $message->cc('helaldu@gmail.com', 'Revinr CMS');
  243. $message->bcc('shamim@revinr.com', 'Revinr CMS');
  244. $message->subject('Payment receipt from revinr');
  245. });
  246. }
  247. // echo "Payment done successfully !";
  248. \Session::flash('success','Payment done successfully !');
  249. return redirect('invoice');
  250. }
  251. }