/** * Handle creation of new bill. * * @param CreateBillRequest $request * @return array */ public function create(CreateBillRequest $request) { // Save request data $clientName = $request->get('client'); $useCurrentCampaign = $request->get('use_current_campaign'); $campaignYear = $request->get('campaign_year'); $campaignNumber = $request->get('campaign_number'); $client = DB::table('clients')->where('name', $clientName)->where('user_id', Auth::user()->id)->first(); // Create new client if not exists if (!$client) { $client = new Client(); $client->user_id = Auth::user()->id; $client->name = $clientName; $client->save(); } // Create new bill $bill = new Bill(); $bill->client_id = $client->id; $bill->user_id = Auth::user()->id; $campaign = Campaigns::current(); // Check if current campaign should be used if (!$useCurrentCampaign) { $campaign = Campaign::where('year', $campaignYear)->where('number', $campaignNumber)->first(); } $bill->campaign_id = $campaign->id; $bill->campaign_order = Campaigns::autoDetermineOrderNumber($campaign, $client->id); $bill->save(); event(new UserCreatedNewBill(Auth::user()->id, $bill->id)); // Return response $response = new AjaxResponse(); $response->setSuccessMessage(trans('bills.bill_created')); return response($response->get()); }
/** * User create new bill with client name that is also used by another user. */ public function test_create_bill_with_client_name_used_by_another_user_using_current_campaign() { $secondUser = factory(\App\User::class)->create(); $clientOfSecondUser = factory(\App\Client::class)->create(['user_id' => $secondUser->id]); $this->postData['client'] = $clientOfSecondUser->name; $this->actingAs($this->user)->post('/bills/create', $this->postData)->seeJson(['success' => true, 'message' => trans('bills.bill_created')])->seeInDatabase('bills', ['user_id' => $this->user->id, 'client_id' => \App\Client::where('user_id', $this->user->id)->where('name', $clientOfSecondUser->name)->first()->id, 'campaign_id' => \App\Helpers\Campaigns::current()->id, 'campaign_order' => 1]); }
/** * Called before each test. */ public function setUp() { parent::setUp(); // Generate user, client and bill with current campaign $this->user = factory(App\User::class)->create(); $this->client = factory(\App\Client::class)->create(['user_id' => $this->user->id]); $this->bill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->client->id]); // Generate another client with another bill that does not use current campaign $this->secondClient = factory(\App\Client::class)->create(['user_id' => $this->user->id]); $this->secondBill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->secondClient->id]); // Set current campaign used in bills $this->campaign = \App\Helpers\Campaigns::current(); }
/** * Called before each test. */ public function setUp() { parent::setUp(); // Generate user, client and bill $this->user = factory(\App\User::class)->create(); $this->client = factory(\App\Client::class)->create(['user_id' => $this->user->id]); $this->bill = factory(\App\Bill::class)->create(['user_id' => $this->user->id, 'client_id' => $this->client->id, 'paid' => 1, 'campaign_id' => \App\Helpers\Campaigns::current()->id]); // Generate products and application products $this->product = factory(\App\Product::class)->create(['user_id' => $this->user->id]); $this->secondProduct = factory(\App\Product::class)->create(['user_id' => $this->user->id]); $this->applicationProduct = factory(\App\ApplicationProduct::class)->create(); $this->secondApplicationProduct = factory(\App\ApplicationProduct::class)->create(); // Generate bill products $this->billProduct = factory(\App\BillProduct::class)->create(['bill_id' => $this->bill->id, 'product_id' => $this->product->id]); $this->secondBillProduct = factory(\App\BillProduct::class)->create(['bill_id' => $this->bill->id, 'product_id' => $this->secondProduct->id]); // Generate bill application products $this->billApplicationProduct = factory(\App\BillApplicationProduct::class)->create(['bill_id' => $this->bill->id, 'product_id' => $this->applicationProduct->id]); $this->secondBillApplicationProduct = factory(\App\BillApplicationProduct::class)->create(['bill_id' => $this->bill->id, 'product_id' => $this->secondApplicationProduct->id]); }
/** * Seed table. */ public function run() { $currentCampaign = \App\Helpers\Campaigns::current(); factory(\App\SecuritySetting::class)->create(['current_campaign_id' => $currentCampaign->id]); }
$user = $factory->raw(App\User::class); return array_merge($user, ['role_id' => $roleHelper->getAdminRoleId()]); }); // Generate an moderator user $factory->defineAs(App\User::class, 'moderator', function ($faker) use($factory) { $roleHelper = new \App\Helpers\Roles(); $user = $factory->raw(App\User::class); return array_merge($user, ['role_id' => $roleHelper->getModeratorRoleId()]); }); // Generate a client $factory->define(App\Client::class, function ($faker) { return ['name' => $faker->name, 'phone_number' => substr(str_shuffle('01234456789'), 0, 10), 'email' => $faker->email]; }); // Generate a bill $factory->define(App\Bill::class, function ($faker) { return ['campaign_id' => \App\Helpers\Campaigns::current()->id, 'campaign_order' => rand(1, 10), 'payment_term' => date('Y-m-d'), 'other_details' => $faker->paragraph()]; }); // Generate paid bill $factory->defineAs(App\Bill::class, 'paid', function ($faker) use($factory) { $bill = $factory->raw(App\Bill::class); return array_merge($bill, ['paid' => 1]); }); // Generate product $factory->define(App\Product::class, function ($faker) { return ['name' => $faker->name, 'code' => (string) $faker->numberBetween(10000, 99999)]; }); // Generate application product $factory->define(App\ApplicationProduct::class, function () use($factory) { return $factory->raw(App\Product::class); }); // Generate bill product
/** * Return number of total products ordered by given client. * * @param int $clientId * @param bool $thisYear Indicate if should be counted only products sold in current year * @return mixed */ public static function totalNumberOfProductsOrdered($clientId, $thisYear = false) { // Count number of bill products $billProducts = DB::table('bill_products')->leftJoin('bills', 'bill_products.bill_id', '=', 'bills.id'); // Count number of bill application products $billApplicationProducts = DB::table('bill_application_products')->leftJoin('bills', 'bill_application_products.bill_id', '=', 'bills.id'); // Check if should be counted only products sold in this year if ($thisYear) { $billProducts->leftJoin('campaigns', 'bills.campaign_id', '=', 'campaigns.id')->where('campaigns.year', Campaigns::current()->year); $billApplicationProducts->leftJoin('campaigns', 'bills.campaign_id', '=', 'campaigns.id')->where('campaigns.year', Campaigns::current()->year); } // Continue bill products query $billProducts->where('bills.client_id', $clientId)->where('bills.paid', 1); // Resume bill application products query $billApplicationProducts->where('bills.client_id', $clientId)->where('bills.paid', 1); // R return $billProducts->sum('bill_products.quantity') + $billApplicationProducts->sum('bill_application_products.quantity'); }