示例#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
						]);
	}
	public function anyListPopupRso(Request $request)
	{
		DB::enableQueryLog();
		if($request->has('input-sort')){
			$arr_sort = $request->input('input-sort');
			$arr_sort = (array)json_decode($arr_sort);
		}else{
			$arr_sort = array();
		}

		if($request->has('input-filter')){
			$arr_filter = $request->input('input-filter');
		}else{
			$arr_filter =[
						'sku' => '',
						'name' => '',
						'company_id' => '',
						'oum_id' => '',
						];
		}
		session(['sort_filter_product_rso.arr_sort' => $arr_sort]);
		session(['sort_filter_product_rso.arr_filter'=> $arr_filter]);

		$distributes = array();
		$oums = array();
		$list_sku = array();

		$distributes = Company::getDistributeList()->get()->toArray();
		$oums = Oum::orderBy('name')->get()->toArray();
		$list_all_product = Product::select('sku','name')->get()->toArray();

		$current_rso = ReturnSaleorder::find(session('current_returnsaleorder'));

		$list_product = MProduct::select('products.name',
		                                'products.sku',
		                                'm_products.id',
		                                'm_products.oum_id',
		                                'm_products.specification',
		                                'm_products.quantity',
		                                'companies.name as company_name',
		                                'm_products.module_id'
		                                )
					->with('oum')
					->leftJoin('products','products.id','=','m_products.product_id')
					->leftJoin('saleorders',function($join){
						$join->on('saleorders.id','=','m_products.module_id')
						->where('saleorders.status','=',1);
					})
					->where('m_products.module_type','=','App\\Saleorder')
					->where('saleorders.status','=',1)
					->leftJoin('companies','companies.id','=','m_products.company_id')
					->where('m_products.company_id','=',$current_rso->company_id);

		$list_product_id_so = MProduct::select('m_products.id')
					->leftJoin('saleorders',function($join){
						$join->on('saleorders.id','=','m_products.module_id')
						->where('saleorders.status','=',1);
					})
					->where('m_products.module_type','=','App\\Saleorder')
					->where('saleorders.status','=',1)
					->where('m_products.company_id','=',$current_rso->company_id)
					->lists('m_products.id');

		$list_product_rso = MProduct::select('m_products.id',
		                                'm_products.m_product_id_so',
		                                'm_products.specification',
		                                'm_products.quantity'
		                                )
					->leftJoin('return_saleorders',function($join){
						$join->on('return_saleorders.id','=','m_products.module_id')
						->where('return_saleorders.status','=',1);
					})
					->where('m_products.module_type','=','App\\ReturnSaleorder')
					->where('return_saleorders.status','=',1)
					->whereIn('m_product_id_so',$list_product_id_so)
					->where('m_products.company_id','=',$current_rso->company_id)->get();
		if(count($arr_sort)==0){
			$list_product = $list_product->orderBy('products.id','asc');
		}

			foreach ($arr_filter as $key => $value) {
				if($value!=''){
					if($arr_filter['sku']!=''){
						$list_product->where('products.sku',$arr_filter['sku']);
					}elseif($arr_filter['name']!=''){
						$list_product->where('products.name',$arr_filter['name']);
					}elseif($key == 'company_id'){
						$list_product->where('m_products.company_id',$arr_filter['company_id']);
					}elseif($key == 'oum_id'){
						$list_product->where('products.oum_id',$arr_filter['oum_id']);
					}else{
						$list_product->where($key,$value);
					}

				}
			}
		$list_product = $list_product->paginate(20);
		foreach ($list_product as $key => $value) {
			$list_product[$key]->so_le = 0;
			foreach ($list_product_rso as $key1 => $value1) {
				if($value1->m_product_id_so==$value->id){
					$so_luong_con = ($list_product[$key]->quantity * $list_product[$key]->specification) + $list_product[$key]->so_le - ($value1->quantity * $value1->specification);
					$list_product[$key]->quantity = floor($so_luong_con/$list_product[$key]->specification);
					$list_product[$key]->so_le = $so_luong_con%$list_product[$key]->specification;
				}
			}
		}
		// pr($list_product->toArray());
		// die;
		// echo $current_rso->company_id;
		// print_r(DB::getQueryLog());
		// pr($list_product->toArray());die;
		return view('popup.choose_product_rso', [
								'distributes'		=> $distributes,
								'oums'			=> $oums,
								'list_product'		=> $list_product,
								'list_all_product'		=> $list_all_product,
								'arr_sort' 		=> $arr_sort,
								'arr_filter' 		=> $arr_filter
							        ]);
	}
	public function anyExportPdf(){
		$id_template = 6;
		$arr_print = 	[
				'arr_list' =>	[
						'arr_key' => 	[
								'sku',
								'name',
								'oum_name',
								'specification',
								'quantity',
								'sell_price',
								'amount'
								],
						'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_returnsaleorder')
						]
				];	
		if (\Cache::has('list_product_rso'.\Auth::user()->id)){
			$rso = ReturnSaleorder::select('return_saleorders.*','companies.name')->where('return_saleorders.id','=',session('current_returnsaleorder'))
							->leftJoin('companies','companies.id','=','return_saleorders.company_id')->get()->first();
			$month = intval(date('m',strtotime($rso->date)));
			$year = intval(date('Y',strtotime($rso->date)));
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = $rso->date;
			$list_order = array();
			$key_order = 1;
			$arr_print['arr_data']['date'] = date('d-m-Y',strtotime($rso->date));
			$arr_print['arr_data']['company_name'] = $rso->name;
			$arr_print['arr_data']['phone'] = $rso->company_phone;
			$arr_print['arr_data']['address'] = '';
			$arr_dress = Address::select('addresses.*','provinces.name as province_name')
						->where('addresses.id','=',$rso->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','=','customer')
								->where('company_id','=',$rso->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 = Saleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rso->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_rso = ReturnSaleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rso->company_id)
					->get()->toArray();

			foreach ($list_rso 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','=',$rso->company_id)
						->where('type_paid','=','customer')
						->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_rso'.\Auth::user()->id);
			$sum_amount = 0;
			foreach ($arr_cache as $key => $value) {
				$sum_amount += $value['amount'];
			}
			if($rso->status==0){
				$arr_print['arr_data']['no_cu'] = 0;
			}
			$arr_print['arr_data']['toa_moi'] = $sum_amount;
			$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_amount]
			];
			$arr_print['arr_list']['arr_body'] = $arr_cache;
			$link = ExportsController::getCreatePrintPdf($arr_print,$id_template,'phieu_dai_ly_tra_hang_so_'.$rso->id,'potrait');
			Log::create_log(\Auth::user()->id,'App\ReturnSaleorder','In đơn hàng đại lý trả số '.session('current_returnsaleorder'));
			return redirect($link);
		}
		die;
	}
	public function postListRevenueCustomerYear(Request $request){
		DB::enableQueryLog();
		$list_order = array();
		$year = $request->has('year')?$request->input('year'):0;
		$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)));
		$key_order = 1;
		$arr_company = array();
		$list_so = Saleorder::select('saleorders.*','companies.name','companies.id')
					->where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->leftJoin('companies','companies.id','=','saleorders.company_id')
					->addSelect(DB::raw('sum(sum_amount) as sum_amount , sum(sum_invest) as sum_invest'))
					->groupBy('company_id')
					->get()->toArray();
		foreach ($list_so as $key => $value) {
			$list_order[$key_order]['id'] = $value['id'];
			$list_order[$key_order]['name'] = $value['name'];
			$list_order[$key_order]['date'] =  $value['date'];
			$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
			$list_order[$key_order]['sum_invest'] =  $value['sum_invest'];
			$list_order[$key_order]['khoang_giam'] =  0;
			$list_order[$key_order]['lai'] =  $list_order[$key_order]['sum_amount'] - $list_order[$key_order]['sum_invest'];
			$list_order[$key_order]['loi_nhuan'] = $list_order[$key_order]['lai'] - $list_order[$key_order]['khoang_giam'];
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$arr_product[$key_order] = $value['id'];
			$key_order++;
		}
		$list_rso = ReturnSaleorder::select('return_saleorders.*','companies.name','companies.id')
					->where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->leftJoin('companies','companies.id','=','return_saleorders.company_id')
					->addSelect(DB::raw('sum(sum_amount) as sum_amount , sum(sum_invest) as sum_invest'))
					->groupBy('company_id')
					->get()->toArray();
		foreach ($list_rso as $key => $value) {
			$check_in_array = array_search($value['id'], $arr_product);
			if($check_in_array){
				$list_order[$check_in_array]['sum_amount'] -=  $value['sum_amount'];
				$list_order[$check_in_array]['sum_invest'] -=  $value['sum_invest'];
				$list_order[$check_in_array]['khoang_giam'] -=  (abs($value['sum_amount']) - abs($value['sum_invest']));
				$list_order[$check_in_array]['loi_nhuan'] = $list_order[$check_in_array]['lai'] + $list_order[$check_in_array]['khoang_giam'];
			}else{
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['name'] = $value['name'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
				$list_order[$key_order]['sum_invest'] =  -$value['sum_invest'];
				$list_order[$key_order]['khoang_giam'] =  - (abs($list_order[$key_order]['sum_amount']) - abs($list_order[$key_order]['sum_invest']));
				$list_order[$key_order]['lai'] =  0;
				$list_order[$key_order]['loi_nhuan'] = $list_order[$key_order]['lai'] + $list_order[$key_order]['khoang_giam'];
				$list_order[$key_order]['updated_at'] = $value['updated_at'];
				$key_order++;
			}
		}

		$date = array();
		foreach ($list_order as $key => $value) {
			$date[$key] = $value['date'];
		}
		array_multisort($date,SORT_ASC,$list_order);
		$chi_khac = RevenueOther::where('date','>=',$begin)
					->where('date','<',$end)
					->sum('sum_amount');

		$arr_cache = [
					'year'=>$year,
					'list_order'=>$list_order,
					'chi_khac'=>$chi_khac
				];

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

		return view('revenue.list-revenue-customer-year',[
				'list_order' => $list_order,
				'chi_khac'  => $chi_khac
			]);
	}
	public function postListReceiptCustomer(Request $request)
	{
		\DB::enableQueryLog();
		$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_so = Saleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$company_id)
					->get()->toArray();
		$key_order = 1;
		foreach ($list_so 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 mua';
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$key_order++;
		}
		$list_rso = ReturnSaleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$company_id)
					->get()->toArray();
		foreach ($list_rso 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 mua';
			$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','=','customer')
					->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']));
		}
		array_multisort($date,SORT_ASC,$list_order);
		if($month!='all'){
			$receipt_month_prev = ReceiptMonth::
								where(function($query){
									$query->where('type_receipt','=','customer')
										->orWhere('type_receipt','=','no_dau_ky_customer');
								})
								->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','=','customer')
					->get()->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();
		}
		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_customer'.\Auth::user()->id, $arr_cache, 30);

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