PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Models/Tenant/PurchaseItem.php

https://gitlab.com/laher01/factu40
PHP | 269 lines | 176 code | 39 blank | 54 comment | 6 complexity | f8b0c70b9d15022ea10689e0b831f380 MD5 | raw file
  1. <?php
  2. namespace App\Models\Tenant;
  3. use App\Models\Tenant\Catalogs\AffectationIgvType;
  4. use App\Models\Tenant\Catalogs\PriceType;
  5. use App\Models\Tenant\Catalogs\SystemIscType;
  6. use Modules\Inventory\Models\Warehouse;
  7. use Modules\Item\Models\ItemLot;
  8. /**
  9. * App\Models\Tenant\PurchaseItem
  10. *
  11. * @property-read AffectationIgvType $affectation_igv_type
  12. * @property mixed $attributes
  13. * @property mixed $charges
  14. * @property mixed $discounts
  15. * @property \App\Models\Tenant\Item $item
  16. * @property-read \Illuminate\Database\Eloquent\Collection|ItemLot[] $lots
  17. * @property-read int|null $lots_count
  18. * @property-read PriceType $price_type
  19. * @property-read \App\Models\Tenant\Purchase $purchase
  20. * @property-read \App\Models\Tenant\Item $relation_item
  21. * @property-read SystemIscType $system_isc_type
  22. * @property-read Warehouse $warehouse
  23. * @method static \Illuminate\Database\Eloquent\Builder|PurchaseItem newModelQuery()
  24. * @method static \Illuminate\Database\Eloquent\Builder|PurchaseItem newQuery()
  25. * @method static \Illuminate\Database\Eloquent\Builder|PurchaseItem query()
  26. * @mixin \Eloquent
  27. */
  28. class PurchaseItem extends ModelTenant
  29. {
  30. protected $with = ['affectation_igv_type', 'system_isc_type', 'price_type', 'lots', 'warehouse'];
  31. public $timestamps = false;
  32. protected $fillable = [
  33. 'purchase_id',
  34. 'item_id',
  35. 'item',
  36. 'quantity',
  37. 'unit_value',
  38. 'affectation_igv_type_id',
  39. 'total_base_igv',
  40. 'percentage_igv',
  41. 'total_igv',
  42. 'system_isc_type_id',
  43. 'total_base_isc',
  44. 'percentage_isc',
  45. 'total_isc',
  46. 'total_base_other_taxes',
  47. 'percentage_other_taxes',
  48. 'total_other_taxes',
  49. 'total_taxes',
  50. 'price_type_id',
  51. 'unit_price',
  52. 'total_value',
  53. 'total',
  54. 'attributes',
  55. 'charges',
  56. 'lot_code',
  57. 'warehouse_id',
  58. 'discounts',
  59. 'date_of_due'
  60. ];
  61. public function getItemAttribute($value)
  62. {
  63. return (is_null($value))?null:(object) json_decode($value);
  64. }
  65. public function setItemAttribute($value)
  66. {
  67. $this->attributes['item'] = (is_null($value))?null:json_encode($value);
  68. }
  69. public function getAttributesAttribute($value)
  70. {
  71. return (is_null($value))?null:(object) json_decode($value);
  72. }
  73. public function setAttributesAttribute($value)
  74. {
  75. $this->attributes['attributes'] = (is_null($value))?null:json_encode($value);
  76. }
  77. public function getChargesAttribute($value)
  78. {
  79. return (is_null($value))?null:(object) json_decode($value);
  80. }
  81. public function setChargesAttribute($value)
  82. {
  83. $this->attributes['charges'] = (is_null($value))?null:json_encode($value);
  84. }
  85. public function getDiscountsAttribute($value)
  86. {
  87. return (is_null($value))?null:(object) json_decode($value);
  88. }
  89. public function setDiscountsAttribute($value)
  90. {
  91. $this->attributes['discounts'] = (is_null($value))?null:json_encode($value);
  92. }
  93. /**
  94. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  95. */
  96. public function affectation_igv_type()
  97. {
  98. return $this->belongsTo(AffectationIgvType::class, 'affectation_igv_type_id');
  99. }
  100. /**
  101. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  102. */
  103. public function system_isc_type()
  104. {
  105. return $this->belongsTo(SystemIscType::class, 'system_isc_type_id');
  106. }
  107. /**
  108. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  109. */
  110. public function price_type()
  111. {
  112. return $this->belongsTo(PriceType::class, 'price_type_id');
  113. }
  114. /**
  115. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  116. */
  117. public function purchase()
  118. {
  119. return $this->belongsTo(Purchase::class);
  120. }
  121. /**
  122. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  123. */
  124. public function item()
  125. {
  126. return $this->belongsTo(Item::class);
  127. }
  128. /**
  129. * @return \Illuminate\Database\Eloquent\Relations\MorphMany
  130. */
  131. public function lots()
  132. {
  133. return $this->morphMany(ItemLot::class, 'item_loteable');
  134. }
  135. /**
  136. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  137. */
  138. public function warehouse()
  139. {
  140. return $this->belongsTo(Warehouse::class);
  141. }
  142. /**
  143. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  144. */
  145. public function relation_item()
  146. {
  147. return $this->belongsTo(Item::class, 'item_id');
  148. }
  149. public function getCollectionData(Configuration $configuration = null) {
  150. if(empty($configuration)){
  151. $configuration = Configuration::first();
  152. }
  153. $purchase = $this->purchase;
  154. $purchase_collection = $purchase->getCollectionData();
  155. $item = $this->item;
  156. $wharehouse = $this->warehouse;
  157. $date_of_due = "-";
  158. if($this->date_of_due){
  159. if(is_string($this->date_of_due)){
  160. $date_of_due = $this->date_of_due;
  161. }else{
  162. $date_of_due =$this->date_of_due->format('Y-m-d');
  163. }
  164. }
  165. $data = [
  166. /*
  167. <template v-if="type == 'sale'">
  168. row.total_item_purchase
  169. row.utility_item
  170. </template>
  171. */
  172. 'id' => $this->id,
  173. 'purchase' => $purchase_collection,
  174. 'purchase_id' => $this->purchase_id,
  175. 'item_id' => $this->item_id,
  176. // 'item' => $this->item,
  177. 'item' => $item,
  178. 'quantity' => number_format($this->quantity,2,'.',''),
  179. 'unit_value' => $this->unit_value,
  180. 'affectation_igv_type_id' => $this->affectation_igv_type_id,
  181. 'total_base_igv' => $this->total_base_igv,
  182. 'percentage_igv' => $this->percentage_igv,
  183. 'total_igv' => $this->total_igv,
  184. 'system_isc_type_id' => $this->system_isc_type_id,
  185. 'total_base_isc' => $this->total_base_isc,
  186. 'percentage_isc' => $this->percentage_isc,
  187. 'total_isc' => $this->total_isc,
  188. 'total_base_other_taxes' => $this->total_base_other_taxes,
  189. 'percentage_other_taxes' => $this->percentage_other_taxes,
  190. 'total_other_taxes' => $this->total_other_taxes,
  191. 'total_taxes' => $this->total_taxes,
  192. 'price_type_id' => $this->price_type_id,
  193. 'unit_price' => $this->unit_price,
  194. 'total_value' => $this->total_value,
  195. 'total' => $this->total,
  196. 'attributes' => $this->attributes,
  197. 'charges' => $this->charges,
  198. 'lot_code' => $this->lot_code,
  199. 'warehouse_id' => $this->warehouse_id,
  200. 'warehouse' => $wharehouse,
  201. 'discounts' => $this->discounts,
  202. 'date_of_due' => $date_of_due,
  203. ];
  204. self::returnValuesToCollection($data,$purchase_collection,'customer_number');
  205. self::returnValuesToCollection($data,$purchase_collection,'customer_name');
  206. self::returnValuesToCollection($data,$purchase_collection,'date_of_issue');
  207. self::returnValuesToCollection($data,$purchase_collection,'document_type_description');
  208. self::returnValuesToCollection($data,$purchase_collection,'series');
  209. self::returnValuesToCollection($data,$purchase_collection,'alone_number');
  210. self::returnValuesToCollection($data,$purchase_collection,'internal_id');
  211. self::returnValuesToCollection($data,$purchase_collection,'brand');
  212. self::returnValuesToCollection($data,$purchase_collection,'description');
  213. self::returnValuesToCollection($data,$purchase_collection,'lot_has_sale');
  214. self::returnValuesToCollection($data,$purchase_collection,'web_platform_name');
  215. self::returnValuesToCollection($data,$purchase_collection,'unit_value');
  216. self::returnValuesToCollection($data,$purchase_collection,'currency_type_id');
  217. return $data;
  218. }
  219. protected static function returnValuesToCollection($array,$purchase,$index){
  220. if (!isset($array[$index]) && isset($purchase[$index])) {
  221. $array['index'] = $purchase[$index];
  222. } elseif (!isset($purchase[$index]) && !isset($array[$index])) {
  223. $array[$index] = null;
  224. }
  225. }
  226. /**
  227. * @return Item|Item[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|mixed|null
  228. */
  229. public function getModelItem(){ return Item::find($this->item_id);}
  230. }