index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <script>
  2. import useColumns from "./columns";
  3. import { EXIST } from "@/api/business/purchase/catalogue";
  4. import { ITEM, SAVE } from "@/api/business/purchase/apply";
  5. import { tax, unit, currency } from "@/components/popover-select-v2/fetch";
  6. const fetchExist = async (prop) => {
  7. try {
  8. // try
  9. const { code, data } = await EXIST(prop);
  10. if (code === 200) return data;
  11. } catch (err) {
  12. // catch
  13. console.error(err);
  14. } finally {
  15. // finally
  16. }
  17. };
  18. export default {
  19. name: "EditDrawer",
  20. props: {
  21. dict: {
  22. type: Object,
  23. },
  24. selectData: {
  25. type: [Array],
  26. require: true,
  27. },
  28. },
  29. components: {
  30. ElSuperForm: () => import("@/components/super-form/index.vue"),
  31. ElSuperTable: () => import("@/components/super-table/index.vue"),
  32. ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  33. },
  34. data() {
  35. const {
  36. TabColumns,
  37. TableColumns,
  38. TabColumns: [
  39. {
  40. item: { key: tabName },
  41. },
  42. ],
  43. } = useColumns();
  44. const rules = this.$init.rules([...TabColumns, ...TableColumns]);
  45. const params = this.$init.params([...TabColumns, ...TableColumns]);
  46. return {
  47. title: "编 辑",
  48. width: "100%",
  49. visible: false,
  50. loading: false,
  51. rules: rules,
  52. params: params,
  53. tabName: tabName,
  54. TabColumns: TabColumns,
  55. TableColumns: TableColumns,
  56. };
  57. },
  58. computed: {
  59. disabled: {
  60. get() {
  61. const {
  62. selectData,
  63. selectData: [{ status } = {}],
  64. } = this.$props;
  65. if (selectData.length !== 1) {
  66. return true;
  67. }
  68. if (selectData.length === 1 && status === "1") {
  69. return true;
  70. }
  71. if (selectData.length === 1 && status === "2") {
  72. return true;
  73. }
  74. },
  75. set() {},
  76. },
  77. priceApply: {
  78. get() {
  79. this.params.priceApplyItems = this.params.priceApplyItems.map(
  80. (item, index) => ({
  81. ...item,
  82. $index: index,
  83. })
  84. );
  85. return {
  86. priceApplyItems: this.params.priceApplyItems.filter(
  87. ({ delFlag }) => delFlag !== "2"
  88. ),
  89. };
  90. },
  91. set() {},
  92. },
  93. },
  94. watch: {},
  95. methods: {
  96. //
  97. async changeMaterialName(prop) {
  98. const { selectData } = prop;
  99. const {
  100. puOrg,
  101. customer,
  102. supplier,
  103. currency,
  104. currencyCode,
  105. currencyName,
  106. } = this.params;
  107. const { nickName: createByName } = this.$store.state.user;
  108. if (selectData.length) {
  109. this.params[this.tabName].splice(-1);
  110. }
  111. for (const item of selectData) {
  112. this.loading = true;
  113. const { tax: taxName, unitName, code: materialCode } = item;
  114. // task 1
  115. const { ntaxrate } = await tax(taxName);
  116. // task 2
  117. const {
  118. id: puUnit,
  119. code: puUnitCode,
  120. name: puUnitName,
  121. } = await unit(unitName);
  122. // task 3
  123. const {
  124. recentlyPrice = "0",
  125. isApprovalFirst = "N",
  126. isPriceAdjustment = "N",
  127. } = await fetchExist({ puOrg, customer, supplier, materialCode });
  128. this.loading = false;
  129. await this.onRowAdd(this.tabName, {
  130. ...item,
  131. currency: currency,
  132. currencyCode: currencyCode,
  133. currencyName: currencyName,
  134. unit: puUnit,
  135. unitCode: puUnitCode,
  136. unitName: puUnitName,
  137. puUnit: puUnit,
  138. puUnitCode: puUnitCode,
  139. puUnitName: puUnitName,
  140. recentlyPrice,
  141. isApprovalFirst,
  142. isPriceAdjustment,
  143. tax: Number(ntaxrate === "0E-8" ? 0 : ntaxrate).toFixed(6),
  144. createByName: createByName,
  145. updateByName: createByName,
  146. });
  147. }
  148. },
  149. //
  150. async fetchItem(prop) {
  151. try {
  152. // try
  153. this.loading = true;
  154. const { tabName, TabColumns } = this;
  155. const { TableColumns } = TabColumns.find(
  156. ({ item: { key } }) => key === tabName
  157. );
  158. const { code, data } = await ITEM(prop);
  159. if (code === 200) {
  160. this.params = data;
  161. this.params.priceApplyItems = data.priceApplyItems.map((item) => ({
  162. ...this.$init.params(TableColumns),
  163. ...item,
  164. }));
  165. return true;
  166. } else {
  167. return false;
  168. }
  169. } catch (err) {
  170. // catch
  171. console.error(err);
  172. } finally {
  173. // finally
  174. this.loading = false;
  175. }
  176. },
  177. //
  178. async onOpen() {
  179. const {
  180. selectData: [{ id }],
  181. } = this.$props;
  182. this.visible = await this.fetchItem(id);
  183. },
  184. //
  185. async onHide() {
  186. const { TabColumns, TableColumns } = useColumns();
  187. this.visible = false;
  188. this.params = this.$init.params([...TabColumns, ...TableColumns]);
  189. },
  190. //
  191. async onRowAdd(prop, pushParams = {}) {
  192. const {
  193. $notify,
  194. TabColumns,
  195. params: { puOrgName, supplierName },
  196. } = this;
  197. if (!supplierName) {
  198. return $notify.info("请选择供应商");
  199. }
  200. if (!puOrgName) {
  201. return $notify.info("请选择采购组织");
  202. }
  203. const { TableColumns } = TabColumns.find(
  204. ({ item: { key } }) => key === prop
  205. );
  206. this.params[prop].push({
  207. delFlag: "0",
  208. ...this.$init.params(TableColumns),
  209. ...pushParams,
  210. });
  211. },
  212. //
  213. async onRowRemove(prop, scope) {
  214. const {
  215. row: { $index },
  216. } = scope;
  217. this.params[prop] = this.params[prop].map((item, index) => ({
  218. ...item,
  219. delFlag: index === $index ? "2" : item.delFlag,
  220. }));
  221. },
  222. //
  223. async useSubmit(prop) {
  224. this.$refs[prop].validate(async (valid) => {
  225. console.log(this.params);
  226. if (valid) {
  227. try {
  228. this.loading = true;
  229. this.params.priceApplyItems = this.params.priceApplyItems.filter(
  230. (item) => item.materialName
  231. );
  232. const { msg, code } = await SAVE(this.params);
  233. if (code === 200) {
  234. this.onHide();
  235. this.$emit("success");
  236. this.$notify.success(msg);
  237. }
  238. } catch (err) {
  239. // catch
  240. console.error(err);
  241. } finally {
  242. // finally
  243. this.loading = false;
  244. }
  245. } else {
  246. return false;
  247. }
  248. });
  249. },
  250. },
  251. created() {},
  252. mounted() {},
  253. destroyed() {},
  254. };
  255. </script>
  256. <template>
  257. <el-button
  258. v-bind="$attrs"
  259. v-on="$listeners"
  260. :disabled="disabled"
  261. @click="onOpen"
  262. >
  263. {{ title }}
  264. <el-drawer
  265. :show-close="false"
  266. :size="width"
  267. :title="title"
  268. :visible.sync="visible"
  269. append-to-body
  270. destroy-on-close
  271. @close="onHide"
  272. >
  273. <template slot="title">
  274. <span>{{ title }}</span>
  275. <el-button
  276. type="primary"
  277. :size="$attrs.size"
  278. :loading="loading"
  279. @click="useSubmit('superForm')"
  280. >
  281. 确 认
  282. </el-button>
  283. <el-button :size="$attrs.size" :loading="loading" @click="onHide">
  284. 取 消
  285. </el-button>
  286. </template>
  287. <el-super-form
  288. v-model="params"
  289. :dict="dict"
  290. :rules="rules"
  291. :size="$attrs.size"
  292. :columns="TableColumns"
  293. ref="superForm"
  294. label-width="auto"
  295. label-position="right"
  296. style="padding: 18px"
  297. >
  298. </el-super-form>
  299. <el-tabs v-model="tabName" style="padding: 0 18px 18px">
  300. <el-tab-pane
  301. v-for="({ item, TableColumns: columns }, index) in TabColumns"
  302. :key="index"
  303. :label="item.title"
  304. :name="item.key"
  305. lazy
  306. ><div v-loading="loading" style="height: 600px; display: flex">
  307. <el-super-table
  308. v-model="priceApply[item.key]"
  309. :dict="dict"
  310. :ref="tabName"
  311. :columns="columns"
  312. :size="$attrs.size"
  313. >
  314. <template slot="materialName" slot-scope="scope">
  315. <component
  316. v-bind="scope.attr"
  317. v-model="scope.row[scope.item.key]"
  318. :size="$attrs.size"
  319. :source.sync="scope.row"
  320. @change="changeMaterialName({ ...scope, selectData: $event })"
  321. >
  322. </component>
  323. </template>
  324. <el-table-column fixed="right" label="操作" width="100">
  325. <template slot="header" slot-scope="scope">
  326. <el-button :size="$attrs.size" @click="onRowAdd(tabName)">
  327. 新增
  328. </el-button>
  329. </template>
  330. <template slot-scope="scope">
  331. <el-button
  332. :size="$attrs.size"
  333. @click.native.prevent="onRowRemove(tabName, scope)"
  334. >
  335. 删除
  336. </el-button>
  337. </template>
  338. </el-table-column>
  339. </el-super-table>
  340. </div>
  341. </el-tab-pane>
  342. </el-tabs>
  343. </el-drawer>
  344. </el-button>
  345. </template>
  346. <style scoped>
  347. ::v-deep .el-table__row.is-hidden {
  348. display: none;
  349. }
  350. </style>