/
Eloquent.php
492 lines (426 loc) · 10.9 KB
/
Eloquent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* Copyright (C) 2013 Emay Komarudin
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Emay Komarudin
*
**/
namespace Emayk\Ics\Repo\Factory\Product;
use Emayk\Ics\Models\BaseModel;
/**
* Class Eloquent
*
* @package Emayk\Ics\Repo\Factory\Product
*/
class Eloquent extends BaseModel
{
/**
* @var array
*/
protected $guarded = [];
/**
* @var string
*/
protected $table = 'master_products';
/**
* @var array
*/
public static $rules = [];
/**
* @var array
*/
protected $appends = [
'totalstocks',
// 'color',
// 'grade',
// 'unit',
'catname', 'typename', 'widthname', 'weightname',
'totallength', 'totalroll'
];
/**
* @var array
*/
protected $hidden = ['parent_id', 'parent_type'];
// protected $with = ['category','type','unitweight','unitheight'];
/**
* @return int
*/
public function getTotallengthAttribute()
{
if (is_null($this->stock)) $this->addEmptyStock();
$count = ( is_null($this->stock) ) ? 0 : $this->stock->totalqty;
return $this->attributes[ 'totallength' ] = $count;
}
/**
* @return int
*/
public function getTotalrollAttribute()
{
if (is_null($this->stock)) $this->addEmptyStock();
$count = ( is_null($this->stock) ) ? 0 : $this->stock->totalroll;
return $this->attributes[ 'totalroll' ] = $count;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function orderItem()
{
return $this->hasMany('\Emayk\Ics\Repo\Transorderdetails\Transorderdetails', 'product_id');
}
/**
* Barang - barang yang sudah dijual
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function sale()
{
return $this->hasMany('\Emayk\Ics\Repo\Saleproduct\Saleproduct', 'product_id');
}
/**
* Mendapatkan Hpp
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hpp()
{
return $this->hasOne('\Emayk\Ics\Repo\Factory\Product\Hpp\Eloquent', 'product_id');
}
public function getHpp()
{
return new \Emayk\Ics\Repo\Factory\Product\Hpp\Eloquent();
}
/**
* Category Product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo('\Emayk\Ics\Repo\Factory\Product\Category\Eloquent', 'cat_id');
}
/**
* @return mixed
*/
public function getCatnameAttribute()
{
return $this->attributes[ 'catname' ] = $this->category()->pluck('name');
// return $this->attributes[ 'catname' ] = $this->category->name;
}
/**
* @return mixed
*/
public function getWidthnameAttribute()
{
// return $this->attributes[ 'widthname' ] = $this->unitwidth->name;//->pluck('name');
return $this->attributes[ 'widthname' ] = $this->unitwidth()->pluck('name');
}
/**
* @return mixed
*/
public function getWeightnameAttribute()
{
return $this->attributes[ 'weightname' ] = $this->unitweight()->pluck('name');
}
/**
* @return mixed
*/
public function getTypenameAttribute()
{
return $this->attributes[ 'typename' ] = $this->type()->pluck('name');
}
/**
* Type Product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function type()
{
return $this->belongsTo('Emayk\Ics\Repo\Producttype\Producttype', 'type_id');
}
/**
*
* Satuan Berat Product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function unitweight()
{
return $this->belongsTo('Emayk\Ics\Repo\Units\Units', 'unitweight_id');
}
/**
*
* Satuan Lebar Product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function unitwidth()
{
return $this->belongsTo('Emayk\Ics\Repo\Units\Units', 'unitwidth_id');
}
/**
* Create Record By User
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function createby()
{
return $this->belongsTo('Emayk\Ics\Repo\Users\Users', 'createby_id');
}
/**
*
* Update Record By User
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function updateby()
{
return $this->belongsTo('Emayk\Ics\Repo\Users\Users', 'lastupdateby_id');
}
/**
*
* Detail Product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function detail()
{
return $this->hasOne('\Emayk\Ics\Repo\Factory\Product\Detail\Eloquent', 'product_id');
}
/**
* Mendapatkan Sales Price
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function price()
{
return $this->hasOne('\Emayk\Ics\Repo\Factory\Product\SalesPrice\Eloquent', 'product_id');
}
/**
*
* Stocks Products
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function stock()
{
// return $this->hasMany('\Emayk\Ics\Repo\Factory\Product\Stock\Eloquent', 'product_id');
return $this->hasOne('\Emayk\Ics\Repo\Factory\Product\Stock\Eloquent', 'product_id');
}
public function oStock()
{
return new \Emayk\Ics\Repo\Factory\Product\Stock\Eloquent();
}
/**
*
* Suppliers Products
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function suppliers()
{
return $this->belongsToMany('\Emayk\Ics\Repo\Suppliers\Suppliers', 'master_product_suppliers', 'master_product_id', 'master_supplier_id');
}
/**
* Mendapatkan Relasi Statistik
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function stats()
{
return $this->hasMany('\Emayk\Ics\Repo\Statsproduct\Statsproduct', 'product_id');
}
/**
* Mendapatkan Image Product
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function image()
{
return $this->morphOne('Emayk\Ics\Repo\Images\Images', 'imageable');
}
/**
* Mendapatkan Jumlah Stocks
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function countStocks()
{
return $this->hasOne('Emayk\Ics\Repo\Statsproduct\Statsproduct', 'product_id');
}
/**
* @return mixed
*/
public function getTotalstocksAttribute()
{
if (is_null($this->stock)) $this->addEmptyStock();
$count = ( is_null($this->stock) ) ? 0 : $this->stock->totalqty;
return $this->attributes[ "totalstocks" ] = $count;
}
//
// protected function getTotalStock(){
// return $this->stocks()->sum('total');
// }
//
////contoh ::
// public static function boot()
// {
//// public $timestamps = false;
// parent::boot();
//
// static::creating(function ($model) {
//
//// $dt = new DateTime;
//// $model->created_at = $dt->format('m-d-y H:i:s');
// return true;
// });
////
//// static::updating(function($model) {
//// $dt = new DateTime;
//// $model->updated_at = $dt->format('m-d-y H:i:s');
//// return true;
//// });
// }
/**
* @return AbstractGenerate
*/
protected static function getFake()
{
return new AbstractGenerate();
}
/**
* @param array $record
*
* @return \Illuminate\Database\Eloquent\Model|static
*/
protected static function createRecord(array $record)
{
return static::create($record);
}
/**
* @param int $count
*
* @return string
*/
public static function generateSampleProducts($count = 10)
{
// Type
$typeIds = Producttype::getIdsOrCreateDummy();
// category
$categoryIds = Productcategory::getIdsOrCreateSampelData();
// unit berat
$unitIds = Units::getIdsOrCreateSampleUnits();
// parent_id
$parentId = 0;
$fake = static::getFake();
//
// Buat Product
$catId = $fake->getFake()->randomElement($categoryIds);
$typeId = $fake->getFake()->randomElement($typeIds);
$unitWeightId = $fake->getFake()->randomElement($unitIds);
$unitWidthId = $fake->getFake()->randomElement($unitIds);
$supplierIds = Suppliers::getRecordIdsOrCreate();
$currencyIds = Currencies::getIdsOrCreateSample();
// color
$colorIds = Colors::getIdsOrCreate();
// gradeIds
$gradeIds = Fabricgrade::getIdsOrCreate();
$productIds = array();
for ($rec = 0; $rec < $count; $rec++) {
$product = $fake->getProduct()->product(
$catId, $typeId, $unitWeightId, $unitWidthId, $parentId,
'\Emayk\Ics\Repo\Productcategory\Productcategory'
);
$record = static::createRecord($product);
$productId = $record->id;
$productIds [ ] = $productId;
// Image/ Photo Product
$imagesIds [ ] = Images::getIdsOrCreate($productId, '\Emayk\Ics\Repo\Products\Products');
// Supplier Product
// Product Supplier (Product dapat dari Supplier mana ?)
$supplierId = $fake->getFake()->randomElement($supplierIds);
$supplierProduct = Productsuppliers::create(
array('master_product_id' => $productId, 'master_supplier_id' => $supplierId));
$supplierProductId = $supplierProduct->id;
// Create Detail
// Buat Product Detail
$unitId = $fake->getFake()->randomElement($unitIds);
$colorId = $fake->getFake()->randomElement($colorIds);
$gradeId = $fake->getFake()->randomElement($gradeIds);
$currSp = $fake->getFake()->randomElement($currencyIds);
$currSpm = $fake->getFake()->randomElement($currencyIds);
$detailIds [ ] = Productdetails::getIdOrCreate(
$productId, $colorId, $unitId, $gradeId, $currSp, $currSpm
);
//Buat Stock
// Buat Stock
$stockIds[ ] = Stockproducts::createStock($productId);
}
foreach ($stockIds as $stockId) {
for ($history = 0; $history < 9; $history++) {
// Buat Stock Detail/History277
if (( $history % 2 == 0 ) || ( $history == 0 )) {
$typeHistory = 'in';
} else {
$typeHistory = 'out';
}
$firstHistory = ( $history == 0 );
// $stockHistoryIds[] =
Stockproducthistory::createHistoryStockSample(
$stockId, $typeHistory, $firstHistory);
}
}
return "Sudah Generate sebanyak " . count($productIds) . " records";
return s($productIds, $supplierProductId, $imagesIds, $detailIds);
}
/**
* Mendapatkan Model Category Product
*
* @return Category\Eloquent
*/
public function getCategory()
{
return new Category\Eloquent();
}
/**
* Mendapatkan Model Detail Product
*
* @return Detail\Eloquent
*/
public function getDetail()
{
return new Detail\Eloquent();
}
/**
* Mendapatkan SalesPrice Object
*
* @return SalesPrice\Eloquent
*/
public function getSalesPrice()
{
return new SalesPrice\Eloquent();
}
protected function addEmptyStock()
{
if (is_null($this->stock)) {
$oStock = new \Emayk\Ics\Repo\Factory\Product\Stock\Eloquent(); //
$product = $this;
$stock = $oStock->add($product, 0, 0, $this->getUid());
return $stock;
}
}
public function getModelImport()
{
return new Import\Model();
}
}