Example #1
0
	/**
	 * Show the application dashboard to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
		$new_so = Saleorder::where('status','=','0')->count();
		$new_rso = ReturnSaleorder::where('status','=','0')->count();
		$new_po = Purchaseorder::where('status','=','0')->count();
		$new_rpo = ReturnPurchaseorder::where('status','=','0')->count();
		$tong_san_pham = Product::count();
		$begin = date('Y-m-d H:i:s',strtotime(date('d-m-Y')));
		$end = date('Y-m-d H:i:s',strtotime(date('d-m-Y'))-15*86400);

		$doanh_so = Saleorder::addSelect(\DB::raw('sum(sum_amount) as y'))
							->addSelect(\DB::raw('Date(date) as x'))
							->where('status',1)
							->groupBy('x')
							->orderBy('x','desc')
							->limit(7)
							->get()->toArray();
		$arr_x = array();
		foreach ($doanh_so as $key => $value) {
			$arr_x[] = $value['x'];
			$doanh_so[$key]['x'] = date('m-d-Y',strtotime($value['x'])); 
		}
		array_multisort($arr_x,SORT_ASC,$doanh_so);
		$doanh_so = json_encode($doanh_so);

		$this->layout->content = view('dashboard.dashboard',[
							'new_so'	=>	$new_so,
							'new_rso'	=>	$new_rso,
							'new_po'	=>	$new_po,
							'new_rpo'	=>	$new_rpo,
							'tong_san_pham'=>	$tong_san_pham, 
							'doanh_so'	=>	$doanh_so
						]);
	}
	/**
	 * Auto generated seed file
	 *
	 * @return void
	 */
	public function run()
	{
		$faker = Faker::create();
        		foreach(range(1, 30) as $index)
		{
			$arr_distribute = Company::where('is_distribute','=',1)->lists('id');
			$company_id = $faker->randomElement($array = $arr_distribute);
			$date = $faker->dateTimeBetween($startDate = '-6 months', $endDate = 'now');
			$address_id = $index+20;
			$company_phone = $faker->phoneNumber;
			$company_email = $faker->companyEmail;
			Purchaseorder::create([
							'company_id'		=> $company_id,
							'date'			=> $date,
							'address_id'		=> $address_id,
							'company_phone'	=> $company_phone,
							'company_email'	=> $company_email,
							'status'			=> 1
						]);
		}
	}
	public function getListPo(Request $request)
	{
		\DB::enableQueryLog();
		$list_order = array();
		$id_product = session('current_product') !== null ? session('current_product') : 0;
		$type = $request->has('type')? $request->input('type'): 'all';
		$company_id = $request->has('company_id')? $request->input('company_id'): 'all';
		$month_year = $request->has('month_year')? $request->input('month_year'): 'all';
		if($type=='all' || $type=='po'){
			$list_po = Purchaseorder::select('purchaseorders.date','purchaseorders.id','m_products.quantity','m_products.specification','oums.name as oum_name','companies.name as company_name','m_products.id as product_id','product_stocks.in_stock')
				->leftJoin('m_products',function($join){
					$join->on('m_products.module_id','=','purchaseorders.id')
						->where('module_type','=','App\Purchaseorder');
				})
				->leftJoin('products','m_products.product_id','=','products.id')
				->leftJoin('companies','m_products.company_id','=','companies.id')
				->leftJoin('oums','m_products.oum_id','=','oums.id')
				->leftJoin('product_stocks','product_stocks.m_product_id','=','m_products.id')
				->where('products.id','=',$id_product)
				->where('purchaseorders.status','=',1)
				->orderBy('date','desc');
			if($company_id != 'all')
				$list_po = 	$list_po->where('companies.id','=',$company_id);
			if($month_year != 'all'){
				$month = explode('-',$month_year);
				$list_po = 	$list_po->whereRaw("MONTH(`purchaseorders`.`date`)=".$month[0])
									->whereRaw("YEAR(`purchaseorders`.`date`)=".$month[1]);

			}
			$list_po =	$list_po->get()->toArray();
			// $query = \DB::getQueryLog();
			// print_r($query[count($query)-1]);
			// pr($list_po);die;

			foreach ($list_po as $key => $value) {
				$arr_tmp = array();
				$arr_tmp['company_name'] = $value['company_name'];
				$arr_tmp['oum_name'] = $value['oum_name'];
				$arr_tmp['specification'] = $value['specification'];
				$arr_tmp['quantity'] = $value['quantity'];
				$arr_tmp['id'] = $value['id'];
				$arr_tmp['product_id'] = $value['product_id'];
				$arr_tmp['date'] = $value['date'];
				$arr_tmp['in_stock'] = $value['in_stock'];
				$arr_tmp['type'] = 'po';
				$list_order[] = $arr_tmp;
			}
		}

		if($type=='all' || $type=='rpo'){
			$list_rpo = ReturnSaleorder::select('return_saleorders.date','return_saleorders.id','m_products.quantity','m_products.specification','oums.name as oum_name','companies.name as company_name','m_products.id as product_id')
				->leftJoin('m_products',function($join){
					$join->on('m_products.module_id','=','return_saleorders.id')
						->where('module_type','=','App\ReturnSaleorder');
				})
				->leftJoin('products','m_products.product_id','=','products.id')
				->leftJoin('companies','m_products.company_id','=','companies.id')
				->leftJoin('oums','m_products.oum_id','=','oums.id')
				->where('products.id','=',$id_product)
				->where('return_saleorders.status','=',1)
				->orderBy('date','desc');
			if($company_id != 'all')
				$list_rpo = $list_rpo->where('companies.id','=',$company_id);
			if($month_year != 'all'){
				$month = explode('-',$month_year);
				$list_rpo = 	$list_rpo->whereRaw("MONTH(`return_saleorders`.`date`)=".$month[0])
									->whereRaw("Year(`return_saleorders`.`date`)=".$month[1]);

			}
			$list_rpo =	$list_rpo->get()->toArray();
			foreach ($list_rpo as $key => $value) {
				$arr_tmp = array();
				$arr_tmp['company_name'] = $value['company_name'];
				$arr_tmp['oum_name'] = $value['oum_name'];
				$arr_tmp['specification'] = $value['specification'];
				$arr_tmp['quantity'] = $value['quantity'];
				$arr_tmp['id'] = $value['id'];
				$arr_tmp['date'] = $value['date'];
				$arr_tmp['in_stock'] = 0;
				$arr_tmp['type'] = 'rpo';
				$list_order[] = $arr_tmp;
			}
		}
		$product = Product::find($id_product)->toArray();

		if(($type=='all' || $type=='in_stock') && $month_year == 'all'){
			if($product['check_in_stock']){
				$list_instock = MProduct::select('m_products.*','product_stocks.in_stock')
							->with('company')->with('oum')
							->where('m_products.product_id','=',session('current_product'))
							->where('module_type','=','in_stock')
							->leftJoin('product_stocks','product_stocks.m_product_id','=','m_products.id');
				if($company_id != 'all'){
					$list_instock = $list_instock->where('m_products.company_id','=',$company_id);
				}
				$list_instock = $list_instock->get()->toArray();
				foreach ($list_instock as $key => $value) {
					$arr_tmp = array();
					$arr_tmp['company_name'] = $value['company']['name'];
					$arr_tmp['oum_name'] = $value['oum']['name'];
					$arr_tmp['specification'] = $value['specification'];
					$arr_tmp['quantity'] = $value['quantity'];
					$arr_tmp['id'] = $value['id'];
					$arr_tmp['product_id'] = $value['product_id'];
					$arr_tmp['date'] = $value['created_at'];
					$arr_tmp['in_stock'] = $value['in_stock'];
					$arr_tmp['type'] = 'in_stock';
					$list_order[] = $arr_tmp;
				}
			}
			
		}
		$arr_date = array();
		foreach ($list_order as $key => $value) {
			$arr_date[] = $value['date'];
		}
		array_multisort($arr_date,SORT_DESC,$list_order);
		return view('product.list-po',[
				'list_order'		=>	$list_order ,
				'product'		=>	$product
			]);
	}
	public function anyExportPdf(){
		$id_template = 3;
		$arr_print = 	[
				'arr_list' =>	[
						'arr_key' => 	[
								'sku',
								'name',
								'oum_name',
								'specification',
								'quantity',
								'origin_price',
								'invest'
								],
						'arr_head' => 	[
								['text'=>'Mã','class'=>''],
								['text'=>'Tên sản phẩm','class'=>''],
								['text'=>'Đơn vị','class'=>'center'],
								['text'=>'Quy cách','class'=>'center'],
								['text'=>'Số lượng','class'=>'center'],
								['text'=>'Đơn giá','class'=>'money'],
								['text'=>'Thành tiền','class'=>'money']
								],
						'arr_body'=>[],
						'arr_sum'=>[]
						],
				'arr_data'=>	[
							'id'=>session('current_returnpurchaseorder')
						]
				];	
		if (\Cache::has('list_product_rpo'.\Auth::user()->id)){
			$rpo = ReturnPurchaseorder::select('return_purchaseorders.*','companies.name')->where('return_purchaseorders.id','=',session('current_returnpurchaseorder'))
							->leftJoin('companies','companies.id','=','return_purchaseorders.company_id')->get()->first();
			$month = intval(date('m',strtotime($rpo->date)));
			$year = intval(date('Y',strtotime($rpo->date)));
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = $rpo->date;
			$list_order = array();
			$key_order = 1;
			$arr_print['arr_data']['date'] = date('d-m-Y',strtotime($rpo->date));
			$arr_print['arr_data']['company_name'] = $rpo->name;
			$arr_print['arr_data']['phone'] = $rpo->company_phone;
			$arr_print['arr_data']['address'] = '';
			$arr_dress = Address::select('addresses.*','provinces.name as province_name')
						->where('addresses.id','=',$rpo->address_id)
						->leftJoin('provinces','provinces.id','=','addresses.province_id')
						->get()->first();
			$arr_print['arr_data']['address'] .= $arr_dress->address?$arr_dress->address.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->town_city?$arr_dress->town_city.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->province_name?$arr_dress->province_name:'';

			$receipt_month_prev = ReceiptMonth::where('type_receipt','=','distribute')
								->where('company_id','=',$rpo->company_id)
								->where(function($query) use ($month,$year){
									$query->where(function($query2) use ($month,$year){
										$query2->where('month','<',$month)
											->where('year','=',$year);
									})->orWhere(function($query2) use ($month,$year){
										$query2->where('year','<',$year);
									});
								})
								->orderBy('year','desc')
								->orderBy('month','desc')
								->limit(1);
			$receipt_month_prev = $receipt_month_prev->first();

			if($receipt_month_prev){
				$arr_print['arr_data']['no_cu'] = $receipt_month_prev->con_lai;
			}else{
				$arr_print['arr_data']['no_cu'] = 0;
			}
			
			$list_po = Purchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rpo->company_id)
					->get()->toArray();
			foreach ($list_po as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
				$key_order++;
			}
			$list_rpo = ReturnPurchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rpo->company_id)
					->get()->toArray();

			foreach ($list_rpo as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
				$key_order++;
			}

			$list_paid = Paid::where('date','>=',$begin)
						->where('date','<',$end)
						->where('company_id','=',$rpo->company_id)
						->where('type_paid','=','distribute')
						->get()->toArray();
			foreach ($list_paid as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_paid'];
				$key_order++;
			}
			$date = array();
			foreach ($list_order as $key => $value) {
				$date[$key] = $value['date'];
			}
			array_multisort($date,SORT_ASC,$list_order);
			foreach ($list_order as $key => $value) {
				$arr_print['arr_data']['no_cu']+=$value['sum_amount'];
			}

			$arr_cache = \Cache::get('list_product_rpo'.\Auth::user()->id);
			$sum_invest = 0;
			foreach ($arr_cache as $key => $value) {
				$sum_invest += $value['invest'];
			}
			if($rpo->status==0){
				$arr_print['arr_data']['no_cu'] = 0;
			}
			$arr_print['arr_data']['toa_moi'] = $sum_invest;
			$arr_print['arr_data']['tong_cong'] = $arr_print['arr_data']['no_cu'] - $arr_print['arr_data']['toa_moi'];

			$arr_print['arr_data']['no_cu'] = number_format($arr_print['arr_data']['no_cu']);
			$arr_print['arr_data']['toa_moi'] = number_format(-$arr_print['arr_data']['toa_moi']);
			$arr_print['arr_data']['tong_cong'] = number_format($arr_print['arr_data']['tong_cong']);
			$arr_print['arr_list']['arr_sum'][] = [
				['value'=>'Tổng cộng:','colspan'=>'6'],
				['value'=>$sum_invest]
			];
			$arr_print['arr_list']['arr_body'] = $arr_cache;
			$link = ExportsController::getCreatePrintPdf($arr_print,$id_template,'phieu_tra_hang_ncc_so_'.$rpo->id,'potrait');
			Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','In đơn hàng trả nhà cung cấp số '.session('current_returnpurchaseorder'));
			return redirect($link);
		}
		die;
	}
	public function postListReceiptDistribute(Request $request)
	{
		$list_order = array();
		$company_id = $request->has('company_id')?$request->input('company_id'):0;
		$month = $request->has('month')?$request->input('month'):0;
		$year = $request->has('year')?$request->input('year'):0;
		if($month!='all'){
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = date('Y-m-d H:i:s',strtotime('1'.'-'.($month+1).'-'.$year));
		}else{
			$begin = date('Y-m-d H:i:s',strtotime('1-1-'.$year));
			$end = date('Y-m-d H:i:s',strtotime('1-1-'.($year+1)));
		}
		
		$list_po = Purchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$company_id)
					->get()->toArray();
		$key_order = 1;
		foreach ($list_po as $key => $value) {
			$list_order[$key_order]['id'] = $value['id'];
			$list_order[$key_order]['date'] =  $value['date'];
			$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
			$list_order[$key_order]['paid'] =  0;
			$list_order[$key_order]['no_cu'] =  0;
			$list_order[$key_order]['con_lai'] =  0;
			$list_order[$key_order]['hinh_thuc'] =  'Đơn hàng nhập';
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$key_order++;
		}
		$list_rpo = ReturnPurchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$company_id)
					->get()->toArray();
		foreach ($list_rpo as $key => $value) {
			$list_order[$key_order]['id'] = $value['id'];
			$list_order[$key_order]['date'] =  $value['date'];
			$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
			$list_order[$key_order]['paid'] =  0;
			$list_order[$key_order]['no_cu'] =  0;
			$list_order[$key_order]['con_lai'] =  0;
			$list_order[$key_order]['hinh_thuc'] =  'Trả hàng nhập';
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$key_order++;
		}

		$list_paid = Paid::where('date','>=',$begin)
					->where('date','<',$end)
					->where('company_id','=',$company_id)
					->where('type_paid','=','distribute')
					->get()->toArray();
		foreach ($list_paid as $key => $value) {
			$list_order[$key_order]['id'] = $value['id'];
			$list_order[$key_order]['date'] =  $value['date'];
			$list_order[$key_order]['sum_amount'] =  0;
			$list_order[$key_order]['paid'] =  $value['sum_paid'];
			$list_order[$key_order]['no_cu'] =  0;
			$list_order[$key_order]['con_lai'] =  0;
			$list_order[$key_order]['hinh_thuc'] =  $value['hinh_thuc'];
			$list_order[$key_order]['company_id'] =  $value['company_id'];
			$list_order[$key_order]['user_id'] =  $value['user_id'];
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$key_order++;
		}

		$date = array();
		foreach ($list_order as $key => $value) {
			$date[$key] = $value['date'];
			$list_order[$key]['date'] = date('d-m-Y',strtotime($value['date']));
		}

		$receipt_month_prev = ReceiptMonth::
							where(function($query){
								$query->where('type_receipt','=','distribute')
										->orWhere('type_receipt','=','no_dau_ky_distribute');
							})
							->where('company_id','=',$company_id)
							->where(function($query) use ($month,$year){
								$query->where(function($query2) use ($month,$year){
									$query2->where('month','<',$month)
										->where('year','=',$year);
								})->orWhere(function($query2) use ($month,$year){
									$query2->where('year','<',$year);
								});
							})
							->orderBy('year','desc')
							->orderBy('month','desc')
							->limit(1);
		$receipt_month_prev = $receipt_month_prev->first();
		if($month!='all' && $receipt_month_prev){
			$receipt_current = ReceiptMonth::where('year','=',$year)
					->where('month','=',$month)
					->where('company_id','=',$company_id)
					->where('type_receipt','=','distribute')
					->first();
			$receipt_current->no_cu = $receipt_month_prev->con_lai;
			$receipt_current->con_lai = $receipt_current->sum_amount + $receipt_current->no_cu - $receipt_current->paid;
			$receipt_current->save();
		}
		array_multisort($date,SORT_ASC,$list_order);

		foreach ($list_order as $key => $value) {
			if($key==0){
				if(isset($receipt_month_prev)){
					$receipt_month_prev= $receipt_month_prev->toArray();
					$list_order[$key]['no_cu']=$receipt_month_prev['con_lai'];
				}
			}else{
				$list_order[$key]['no_cu']=$list_order[$key-1]['con_lai'];
			}
			$list_order[$key]['con_lai']=$list_order[$key]['sum_amount'] - $list_order[$key]['paid'] + $list_order[$key]['no_cu'];
		}

		$company_name = Company::find($company_id);
		$company_name = $company_name->name;

		$arr_cache = [
					'month'=>$month,
					'year'=>$year,
					'company_name'=>$company_name,
					'list_order'=>$list_order
				];

		\Cache::put('list_receipt_distribute'.\Auth::user()->id, $arr_cache, 30);

		return view('receipt.list-receipt-distribute',[
					'list_order' =>$list_order
			]);
	}