Create.vue 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <script setup>
  2. import { watch, computed } from 'vue'
  3. import { Inertia } from '@inertiajs/inertia'
  4. import { optionStatus } from './config'
  5. import { cartTable } from './config'
  6. import Details from './Components/Details.vue'
  7. import Cart from './Components/Cart.vue'
  8. import { useProductCart } from './Composables/useProductCart'
  9. import { onShowDialog } from './Composables/onShowDialog'
  10. import { useForm } from '@/composables/useForm'
  11. import AppInputText from '@/components/AppInputText.vue'
  12. import AppInputNumber from '@/components/AppInputNumber.vue'
  13. import AppDropdown from '@/components/AppDropdown.vue'
  14. import AppAutoComplete from '@/components/AppAutoComplete.vue'
  15. import DashboardLayout from '@/layouts/Dashboard/DashboardLayout.vue'
  16. const props = defineProps({
  17. number: String,
  18. ppn: Number,
  19. suppliers: {
  20. type: Array,
  21. default: [],
  22. },
  23. products: {
  24. type: Array,
  25. default: [],
  26. },
  27. historyProductPurchase: Object,
  28. })
  29. const form = useForm({
  30. status: 'pending',
  31. price: null,
  32. qty: null,
  33. supplier: null,
  34. product: null,
  35. ppn: props.ppn,
  36. checkedPpn: false,
  37. })
  38. const onSubmit = () => {
  39. form
  40. .transform((data) => ({
  41. number: props.number,
  42. status: data.status,
  43. ppn: data.checkedPpn,
  44. supplier_id: data.supplier.id,
  45. products: productCart,
  46. }))
  47. .post(route('purchases.store'), {
  48. onSuccess: () => {
  49. form.reset()
  50. onClearProductCart()
  51. },
  52. })
  53. }
  54. const dropdownStatus = computed(() => {
  55. return optionStatus.filter((val) => val.value != 'success')
  56. })
  57. watch(
  58. () => form.product,
  59. () => {
  60. if (form.product.id) {
  61. Inertia.reload({
  62. data: {
  63. productNumber: form.product.number,
  64. supplierId: form.supplier.id,
  65. },
  66. only: ['historyProductPurchase'],
  67. })
  68. }
  69. }
  70. )
  71. const historyProductPrice = computed(() => {
  72. return props.historyProductPurchase?.price
  73. })
  74. const {
  75. productCart,
  76. onClearProductCart,
  77. onAddProduct,
  78. onDeleteProduct,
  79. onEditProduct,
  80. totalProductPrice,
  81. } = useProductCart(form)
  82. const { onShowCreateProduct, onShowCreateSupplier } = onShowDialog()
  83. </script>
  84. <template>
  85. <DashboardLayout title="Tambah Pembelian">
  86. <DynamicDialog />
  87. <div class="grid">
  88. <div class="col-12 md:col-8">
  89. <div class="grid">
  90. <div class="col-12">
  91. <Card>
  92. <template #title> Transaksi </template>
  93. <template #content>
  94. <div class="grid">
  95. <div class="col-12 md:col-6">
  96. <AppDropdown
  97. label="Status"
  98. placeholder="status"
  99. :options="dropdownStatus"
  100. :error="form.errors.status"
  101. v-model="form.status"
  102. />
  103. </div>
  104. <div class="col-12 md:col-6">
  105. <AppAutoComplete
  106. empty
  107. label="Supplier"
  108. placeholder="supplier"
  109. field="name"
  110. refresh-data="suppliers"
  111. v-model="form.supplier"
  112. :error="form.errors.suppliers_id"
  113. :suggestions="suppliers"
  114. >
  115. <template #item="slotProps">
  116. <template v-if="slotProps.item">
  117. <div class="flex flex-column">
  118. <span>{{ slotProps.item.name }}</span>
  119. <span>{{ slotProps.item.phone }}</span>
  120. </div>
  121. </template>
  122. </template>
  123. <template #empty>
  124. <span
  125. class="cursor-pointer"
  126. style="color: var(--primary-color)"
  127. @click="onShowCreateSupplier"
  128. >
  129. Tambah Supplier
  130. </span>
  131. </template>
  132. </AppAutoComplete>
  133. </div>
  134. </div>
  135. </template>
  136. </Card>
  137. </div>
  138. <div class="col-12">
  139. <Card>
  140. <template #title>Produk</template>
  141. <template #content>
  142. <div class="grid">
  143. <div class="col-12 md:col-6">
  144. <AppAutoComplete
  145. :disabled="!form.supplier?.id"
  146. empty
  147. label="Produk"
  148. placeholder="produk"
  149. field="name"
  150. refresh-data="products"
  151. v-model="form.product"
  152. :error="form.errors.products"
  153. :suggestions="products"
  154. >
  155. <template #item="slotProps">
  156. <template v-if="slotProps.item">
  157. <div class="flex flex-column">
  158. <span>{{ slotProps.item.number }}</span>
  159. <span>{{ slotProps.item.name }}</span>
  160. </div>
  161. </template>
  162. </template>
  163. <template #empty>
  164. <span
  165. class="cursor-pointer"
  166. style="color: var(--primary-color)"
  167. @click="onShowCreateProduct"
  168. >
  169. Tambah Produk
  170. </span>
  171. </template>
  172. </AppAutoComplete>
  173. </div>
  174. <div v-if="form.product?.number" class="col-12 md:col-6">
  175. <AppInputText
  176. disabled
  177. label="Satuan"
  178. placeholder="satuan"
  179. v-model="form.product.unit"
  180. />
  181. </div>
  182. <div class="col-12 md:col-6">
  183. <AppInputNumber
  184. disabled
  185. label="Harga Sebelumya"
  186. placeholder="harga sebelumnya"
  187. v-model="historyProductPrice"
  188. />
  189. </div>
  190. <Divider type="dashed" />
  191. <div class="col-12 md:col-6">
  192. <AppInputNumber
  193. :disabled="!form.supplier?.id"
  194. label="Harga"
  195. placeholder="harga"
  196. v-model="form.price"
  197. />
  198. </div>
  199. <div class="col-12 md:col-6">
  200. <AppInputText
  201. :disabled="!form.supplier?.id"
  202. label="Kuantitas"
  203. placeholder="kuantitas"
  204. type="number"
  205. v-model="form.qty"
  206. />
  207. </div>
  208. </div>
  209. </template>
  210. <template #footer>
  211. <div class="flex flex-column md:flex-row justify-content-end">
  212. <Button
  213. label="Tambah Produk"
  214. icon="pi pi-check"
  215. class="p-button-outlined"
  216. :disabled="
  217. !form.price || !form.qty || !form.product?.number
  218. "
  219. @click="onAddProduct"
  220. />
  221. </div>
  222. </template>
  223. </Card>
  224. </div>
  225. <div class="col-12">
  226. <Cart
  227. title="Keranjang Produk"
  228. :product-cart="productCart"
  229. :header-table="cartTable"
  230. v-model:checked-ppn="form.checkedPpn"
  231. @delete="onDeleteProduct"
  232. @edit="onEditProduct"
  233. />
  234. </div>
  235. </div>
  236. </div>
  237. <div class="col-12 md:col-4">
  238. <Details
  239. title="Detail Pembelian"
  240. message="Pastikan semua produk sudah benar"
  241. :number="number"
  242. :status="form.status"
  243. :person="form.supplier"
  244. :product="form.product"
  245. :price="totalProductPrice()"
  246. :disabled="
  247. form.processing ||
  248. !form.status ||
  249. !form.supplier?.id ||
  250. productCart.length === 0
  251. "
  252. @submit="onSubmit"
  253. />
  254. </div>
  255. </div>
  256. </DashboardLayout>
  257. </template>