Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. const express = require('express')
  2. const app = express.Router()
  3. const moment = require('moment')
  4. const transaction = require('../schema/transactionSchema')
  5. const transactionDetail = require('../schema/transactionDetailSchema')
  6. const product = require('../schema/productSchema')
  7. const purchase = require('../schema/tempPurchaseSchema')
  8. const mutation = require('../schema/mutationSchema')
  9. const {
  10. formatPrice, formatDate, toTitleCase, funcZero, funcDate, funcYear
  11. } = require('../config/formatData')
  12.  
  13.  
  14. // find all
  15. app.get('/findAll', async (req, res) => {
  16. try {
  17. let results = await transaction.all()
  18. res.status(200).json(results)
  19. }catch(e) {
  20. res.sendStatus(500)
  21. }
  22. })
  23.  
  24. // find one transaction
  25. app.get('/findOne/:id', async (req, res) => {
  26. try{
  27. let response = await transaction.id(req.params.id)
  28. res.status(200).json( response )
  29. } catch(e) {
  30. res.sendStatus(500)
  31. }
  32. })
  33. app.get('/findAll/v2/last', async (req, res) => {
  34. try{
  35. let response = await transaction.last()
  36. res.status(200).json(response)
  37. } catch(e) {
  38. console.log(e)
  39. res.sendStatus(500)
  40. }
  41. })
  42.  
  43. // insert table
  44. app.post('/added', async (req, res) => {
  45. try {
  46. let body = req.body
  47. let response = await transaction.add(
  48. body.id_transaction,
  49. body.id_user,
  50. body.id_supplier,
  51. body.trx_amount_paid,
  52. body.trx_amount_cutting,
  53. body.trx_amount_discount,
  54. body.trx_startDate_nota,
  55. body.trx_endDate_nota,
  56. body.trx_number_nota
  57. ) //inserrt data
  58.  
  59. //insert to tb_temporary stock
  60.  
  61. // get tb_temporary stock
  62.  
  63.  
  64. // // //insert into table product
  65. if (body.productList.length > 0) { //dataliost from react
  66. for(let items = body.productList.length -1; items >= 0; items--) {
  67.  
  68. let results = await product.updateStock(
  69. body.productList[items].temp_id_product,
  70. body.productList[items].temp_id_type,
  71. body.productList[items].temp_id_brand,
  72. body.productList[items].temp_prod_name,
  73. body.productList[items].temp_prod_amount, //add stock to product
  74. body.productList[items].temp_prod_purchase, //search average
  75. body.productList[items].temp_prod_selling,
  76. body.productList[items].temp_prod_cutted,
  77. body.productList[items].temp_prod_discount
  78. )
  79.  
  80. }
  81. }
  82.  
  83. //insert to tb_mutation
  84.  
  85. // //insert to tbl detail transaction
  86. // if (body.datalist.length > 0) { //dataliost from react
  87. // let response_d = 0;
  88. // for(let i = body.datalist.length -1; i >=0; i--) {
  89. // datas =
  90. // response_d = await transactionDetail.add(
  91. // body.id_transaction,
  92. // body.datalist[i].temp_id_product,
  93. // body.datalist[i].temp_prod_amount,
  94. // body.datalist[i].temp_prod_cutted,
  95. // body.datalist[i].temp_prod_discount,
  96. // )
  97. // }
  98. // }
  99. res.status(200).json(response)
  100. }catch(e) {
  101. console.log(e)
  102. res.sendStatus(500)
  103. }
  104. })
  105. // transaction updates
  106. app.put('/updated/:id', async (req, res) => {
  107. try{
  108. let id = req.params.id
  109. let x = req.body
  110. let results = await transaction.update(
  111. id,
  112. // x.id_transaction,
  113. x.id_user,
  114. x.id_supplier,
  115. x.trx_amount_paid,
  116. x.trx_amount_cutting
  117. )
  118. res.status(200).json( results )
  119. }catch(e) {
  120. console.log(e)
  121. res.sendStatus(500)
  122. }
  123. })
  124.  
  125. // transaction delete
  126. app.delete('/removed/:id', async(req, res) => {
  127. try {
  128. let results = await transaction.remove(req.params.id)
  129. res.status(200).json(results)
  130. }catch(e) {
  131. res.sendStatus(500)
  132. }
  133. })
  134.  
  135. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement