Skip to content

Commit

Permalink
statistic by customer and profits, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyu65 committed Dec 8, 2016
1 parent d1a8cfa commit e4a8907
Show file tree
Hide file tree
Showing 18 changed files with 921 additions and 398 deletions.
31 changes: 31 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
APP_ENV=local
APP_DEBUG=true
APP_KEY=A1GTLrmxPXVmUfdai7LhDCgyvCMYdhFt
APP_URL=http://localhost

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ccbuy_demo
DB_USERNAME=root
DB_PASSWORD=root

DB=ccbuy
DB_DEMO=ccbuy_demo

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

//use for website on the subroot
SUBROOT=Q
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
884 changes: 586 additions & 298 deletions .idea/workspace.xml

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,30 @@ public function store(Request $request)
//set profits for table carts
$cartId = $item->carts_id;
$cart = Cart::find($cartId);
$profitCurrent = $cart->profits;
$postCost = $cart->weight * $cart->postRate;
if($postCost == 0) { //the deal has not been done, post cost or post rate is gonna be 0
$profitNow = $item->itemProfit + $profitCurrent;
}else{
if($cart->profits == 0){ //the first item will be record and the post fee will be minus
$profitNow = $item->itemProfit + $profitCurrent - $postCost;
} else {
if ($cart != null) {
$profitCurrent = $cart->profits;
$postCost = $cart->weight * $cart->postRate;
if($postCost == 0) { //the deal has not been done, post cost or post rate is gonna be 0
$profitNow = $item->itemProfit + $profitCurrent;
}else{
if($cart->profits == 0){ //the first item will be record and the post fee will be minus
$profitNow = $item->itemProfit + $profitCurrent - $postCost;
} else {
$profitNow = $item->itemProfit + $profitCurrent;
}
}
DB::table('carts')->where('id', $cartId)->update(['profits' => $profitNow]);
if($dm){
return redirect('item/create/daimai')->with('status', '添加记录成功');
}else{
return redirect('item/create')->with('status', '添加记录成功');
}
}
DB::table('carts')->where('id', $cartId)->update(['profits' => $profitNow]);
if($dm){
return redirect('item/create/daimai')->with('status', '添加记录成功');
}else{
return redirect('item/create')->with('status', '添加记录成功');
if($dm){
return redirect('item/create/daimai')->with('status', '添加记录成功,但是更新利润失败');
}else{
return redirect('item/create')->with('status', '添加记录成功,但是更新利润失败,');
}
}
} else {
return redirect()->back()->withInput()->withErrors('保存失败!');
Expand Down
132 changes: 103 additions & 29 deletions app/Http/Controllers/statisticController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@

use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Mockery\CountValidator\Exception;

class statisticController extends Controller
{
protected $configFileName = 'ccbuy';
/**
* @param $year
* @param $type - profits or customer or ***
* @param string $r - refresh
* @return string
*/
public function index($year, $type, $r = '')
{
$refresh = false; //if false than use json otherwise to refresh
if($r == 'refresh')
$refresh = false; //if false than use json to show the data otherwise to refresh
if($r === 'refresh')
$refresh = true;
switch ($type) {
case 'profits':
return $this->profits($year, $refresh);
case 'customs':
break;
return $this->profits($year, $refresh, 'profit');
case 'customer':
return $this->getProfitByCustomer($year, $refresh, 'customer');
case 'items':
break;
default:
return '';
}
}

Expand All @@ -31,18 +40,18 @@ public function index($year, $type, $r = '')
* @param $refresh - if refresh page to see newest data
* @return string - data such as '1,2,3,4'
*/
private function profits($year, $refresh)
private function profits($year, $refresh, $type)
{
if ($year == '2016' || $year == '2017' || $year == 'all') {
if ($year == '2016' || $year == '2017' || $year == 'All') {
//read data from json file but the form of data is 'data,data,data'
$path = Config::get('ccbuy.statistic.profitsPath.profit' . $year);
$path = Config::get($this->configFileName.'.statistic.profitsPath.'. $type . $year);
if($refresh) { //see if the page refresh or not
$data = $this->getProfitsByYear($year);
$data = $this->getProfitsByYear($year, $type);
file_put_contents(public_path($path), $data);
}else{
$profits = file_get_contents(public_path($path));
if ($profits == '') {
$data = $this->getProfitsByYear($year);
$data = $this->getProfitsByYear($year, $type);
file_put_contents(public_path($path), $data);
}
else{
Expand All @@ -51,10 +60,10 @@ private function profits($year, $refresh)
}
//get all month x axis
if ($year == 'all') {
$month = Config::get('ccbuy.statistic.profitAll');
$month = Config::get($this->configFileName.'.statistic.profitAll');
$year = trans('statistic.allYear');
}else{
$month = Config::get('ccbuy.statistic.profitMonth');
$month = Config::get($this->configFileName.'.statistic.profitMonth');
}
$title = $year . trans('statistic.chart');
return view('statistic', ['data' => $data, 'month' => $month, 'title' => $title]);
Expand All @@ -63,40 +72,105 @@ private function profits($year, $refresh)
}
}

private function getCustomerProfits($year, $refresh)
{

}

/**
* @param $year - get all data by year
* @return array - array that show every month data(profits)
*/
private function getProfitsByYear ($year)
private function getProfitsByYear ($year, $type)
{
if ($year == 'all') {
$data = DB::table('carts')->select('profits', 'date')->get();
}else{
$profits = '';
if ($year === 'All') { //get all data by year
//$data = DB::table('carts')->select('profits', 'date')->get();
if(!array_key_exists('profitAll',Config::get($this->configFileName.'.statistic')))
return false;
$allYears = explode(',',Config::get($this->configFileName.'.statistic.profitAll'));
switch ($type) {
case 'profit':
foreach ($allYears as $year) {
$profits .= DB::table('carts')->whereRaw('year(date)=' . $year)->sum('profits') . ',';
}
break;
default:
return '';
}
$profits = rtrim($profits, ',');
}else{ //get all data by month
$data = DB::table('carts')->select('profits', 'date')->whereRaw('year(date)='.$year)->get();
}
$arr = array();
foreach ($data as $d) {
if($d->date != null) {
$date = explode('-', $d->date);
$monthNum = (int)$date[1];
if (array_key_exists($monthNum, $arr)) {
$arr[$monthNum] += $d->profits;
} else {
$arr[$monthNum] = $d->profits;
$arr = array();
foreach ($data as $d) {
if($d->date != null) {
$date = explode('-', $d->date);
$monthNum = (int)$date[1];
if (array_key_exists($monthNum, $arr)) {
$arr[$monthNum] += $d->profits;
} else {
$arr[$monthNum] = $d->profits;
}
}
}
$profits = $this->getSumByMonth($arr);
}
$profits = $this->getSumByMonth($arr);
return $profits;
}

private function getProfitByCustomer($year, $refresh, $type)
{
$profitAll = explode(',', Config::get($this->configFileName.'.statistic.profitAll'));
$path = Config::get($this->configFileName.'.statistic.profitsPath.'. $type . $year);
if(!in_array($year, $profitAll) && $year != 'All')
return '';
$profits = [];
if ($refresh) { //if refresh
$profits = $this->getProfitByCustomer_getData($year);
file_put_contents(public_path($path), serialize($profits));
}else{
$profits = unserialize(file_get_contents($path));
if(!$profits){
$profits = $this->getProfitByCustomer_getData($year);
file_put_contents(public_path($path), serialize($profits));
}
}
$data = $month = '';
foreach ($profits as $profit) { //the return data's format is array. like 'customer' => 'profits'
foreach ($profit as $customer => $sum) {
}
$data .= $sum . ',';
$month .= $customer . ',';
}
$title = $year . $type . trans('statistic.chart');
$month = rtrim($month, ',');
return view('statistic', ['data' => $data, 'month' => $month, 'title' => $title]);
}

private function getProfitByCustomer_getData($year)
{
$profits = [];
$customers = DB::table('customs')->get();
if ($year === 'All') {
foreach ($customers as $customer) {
$sum = DB::table('carts')->where('customs_id', $customer->id)->sum('profits');
array_push($profits,[$customer->customName => $sum]);
}
} else{
foreach ($customers as $customer) {
$sum = DB::table('carts')->where('customs_id', $customer->id)->whereRaw('year(date)=' . $year)->sum('profits');
array_push($profits,[$customer->customName => $sum]);
}
}
return $profits;
}
/**
* @param $arr - get all the summary of profits by month
* @return string - such as '1,2,3 ...'
*/
private function getSumByMonth($arr)
{
$monthList = explode(',', Config::get('ccbuy.statistic.profitMonth'));
$monthList = explode(',', Config::get($this->configFileName.'.statistic.profitMonth'));
$returnData = [];
foreach ($monthList as $month) {
switch ($month) {
Expand Down
25 changes: 19 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"name": "taoyu65/ccbuy",
"description": "Freelance Retail Consultant",
"keywords": ["laravel", "retail" , "php"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
"laravel/framework": "5.2.*",
"nwidart/db-exporter": "1.*",
"xethron/migrations-generator":"dev-l5",
"way/generators":"dev-feature/laravel-five-stable"

},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"symfony/css-selector": "2.8.*|3.0.*",
"symfony/dom-crawler": "2.8.*|3.0.*"
"symfony/dom-crawler": "2.8.*|3.0.*",
"xethron/migrations-generator": "dev-l5",
"way/generators": "dev-feature/laravel-five-stable"
},
"autoload": {
"classmap": [
Expand Down Expand Up @@ -47,6 +53,13 @@
]
},
"config": {
"preferred-install": "dist"
"preferred-install": "dist",
"[email protected]":"jamisonvalenta/Laravel-4-Generators.git"
},
"repositories": {
"repo-name": {
"type": "git",
"url": "[email protected]:jamisonvalenta/Laravel-4-Generators.git"
}
}
}
51 changes: 49 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e4a8907

Please sign in to comment.