|
@@ -34,82 +34,66 @@ class OfferingFactory extends Factory
|
34
|
34
|
'description' => $this->faker->sentence,
|
35
|
35
|
'type' => $this->faker->randomElement(OfferingType::cases()),
|
36
|
36
|
'price' => $this->faker->numberBetween(5, 1000),
|
37
|
|
- 'sellable' => $this->faker->boolean(80),
|
38
|
|
- 'purchasable' => $this->faker->boolean(80),
|
39
|
|
- 'income_account_id' => function (array $attributes) {
|
40
|
|
- return $attributes['sellable'] ? 10 : null;
|
41
|
|
- },
|
42
|
|
- 'expense_account_id' => function (array $attributes) {
|
43
|
|
- return $attributes['purchasable'] ? $this->faker->numberBetween(17, 35) : null;
|
44
|
|
- },
|
|
37
|
+ 'sellable' => false,
|
|
38
|
+ 'purchasable' => false,
|
|
39
|
+ 'income_account_id' => null,
|
|
40
|
+ 'expense_account_id' => null,
|
45
|
41
|
'created_by' => 1,
|
46
|
42
|
'updated_by' => 1,
|
47
|
43
|
];
|
48
|
44
|
}
|
49
|
45
|
|
50
|
|
- public function sellable(): self
|
|
46
|
+ public function withSalesAdjustments(): self
|
51
|
47
|
{
|
52
|
|
- $incomeAccount = Account::query()
|
53
|
|
- ->where('category', AccountCategory::Revenue)
|
54
|
|
- ->where('type', AccountType::OperatingRevenue)
|
55
|
|
- ->inRandomOrder()
|
56
|
|
- ->first();
|
|
48
|
+ return $this->afterCreating(function (Offering $offering) {
|
|
49
|
+ $incomeAccount = Account::query()
|
|
50
|
+ ->where('company_id', $offering->company_id)
|
|
51
|
+ ->where('category', AccountCategory::Revenue)
|
|
52
|
+ ->where('type', AccountType::OperatingRevenue)
|
|
53
|
+ ->inRandomOrder()
|
|
54
|
+ ->firstOrFail();
|
57
|
55
|
|
58
|
|
- return $this->state(function (array $attributes) use ($incomeAccount) {
|
59
|
|
- return [
|
|
56
|
+ $offering->updateQuietly([
|
60
|
57
|
'sellable' => true,
|
61
|
|
- 'income_account_id' => $incomeAccount?->id ?? 10,
|
62
|
|
- ];
|
63
|
|
- });
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- public function purchasable(): self
|
67
|
|
- {
|
68
|
|
- $expenseAccount = Account::query()
|
69
|
|
- ->where('category', AccountCategory::Expense)
|
70
|
|
- ->where('type', AccountType::OperatingExpense)
|
71
|
|
- ->inRandomOrder()
|
72
|
|
- ->first();
|
73
|
|
-
|
74
|
|
- return $this->state(function (array $attributes) use ($expenseAccount) {
|
75
|
|
- return [
|
76
|
|
- 'purchasable' => true,
|
77
|
|
- 'expense_account_id' => $expenseAccount?->id ?? $this->faker->numberBetween(17, 35),
|
78
|
|
- ];
|
79
|
|
- });
|
80
|
|
- }
|
|
58
|
+ 'income_account_id' => $incomeAccount->id,
|
|
59
|
+ ]);
|
81
|
60
|
|
82
|
|
- public function withSalesAdjustments(): self
|
83
|
|
- {
|
84
|
|
- return $this->afterCreating(function (Offering $offering) {
|
85
|
|
- if ($offering->sellable) {
|
86
|
|
- $adjustments = $offering->company?->adjustments()
|
87
|
|
- ->where('type', AdjustmentType::Sales)
|
88
|
|
- ->pluck('id');
|
|
61
|
+ $adjustments = $offering->company?->adjustments()
|
|
62
|
+ ->where('type', AdjustmentType::Sales)
|
|
63
|
+ ->pluck('id');
|
89
|
64
|
|
90
|
|
- $adjustmentsToAttach = $adjustments->isNotEmpty()
|
91
|
|
- ? $adjustments->random(min(2, $adjustments->count()))
|
92
|
|
- : Adjustment::factory()->salesTax()->count(2)->create()->pluck('id');
|
|
65
|
+ $adjustmentsToAttach = $adjustments->isNotEmpty()
|
|
66
|
+ ? $adjustments->random(min(2, $adjustments->count()))
|
|
67
|
+ : Adjustment::factory()->salesTax()->count(2)->create()->pluck('id');
|
93
|
68
|
|
94
|
|
- $offering->salesAdjustments()->attach($adjustmentsToAttach);
|
95
|
|
- }
|
|
69
|
+ $offering->salesAdjustments()->attach($adjustmentsToAttach);
|
96
|
70
|
});
|
97
|
71
|
}
|
98
|
72
|
|
99
|
73
|
public function withPurchaseAdjustments(): self
|
100
|
74
|
{
|
101
|
75
|
return $this->afterCreating(function (Offering $offering) {
|
102
|
|
- if ($offering->purchasable) {
|
103
|
|
- $adjustments = $offering->company?->adjustments()
|
104
|
|
- ->where('type', AdjustmentType::Purchase)
|
105
|
|
- ->pluck('id');
|
|
76
|
+ $expenseAccount = Account::query()
|
|
77
|
+ ->where('company_id', $offering->company_id)
|
|
78
|
+ ->where('category', AccountCategory::Expense)
|
|
79
|
+ ->where('type', AccountType::OperatingExpense)
|
|
80
|
+ ->inRandomOrder()
|
|
81
|
+ ->firstOrFail();
|
|
82
|
+
|
|
83
|
+ $offering->updateQuietly([
|
|
84
|
+ 'purchasable' => true,
|
|
85
|
+ 'expense_account_id' => $expenseAccount->id,
|
|
86
|
+ ]);
|
|
87
|
+
|
|
88
|
+ $adjustments = $offering->company?->adjustments()
|
|
89
|
+ ->where('type', AdjustmentType::Purchase)
|
|
90
|
+ ->pluck('id');
|
106
|
91
|
|
107
|
|
- $adjustmentsToAttach = $adjustments->isNotEmpty()
|
108
|
|
- ? $adjustments->random(min(2, $adjustments->count()))
|
109
|
|
- : Adjustment::factory()->purchaseTax()->count(2)->create()->pluck('id');
|
|
92
|
+ $adjustmentsToAttach = $adjustments->isNotEmpty()
|
|
93
|
+ ? $adjustments->random(min(2, $adjustments->count()))
|
|
94
|
+ : Adjustment::factory()->purchaseTax()->count(2)->create()->pluck('id');
|
110
|
95
|
|
111
|
|
- $offering->purchaseAdjustments()->attach($adjustmentsToAttach);
|
112
|
|
- }
|
|
96
|
+ $offering->purchaseAdjustments()->attach($adjustmentsToAttach);
|
113
|
97
|
});
|
114
|
98
|
}
|
115
|
99
|
}
|