index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <script>
  2. import useColumns from "./columns";
  3. import { ADD, CODE, LINKAGREEMENT } from "@/api/business/purchase/contract";
  4. import { tax, currency } from "@/components/popover-select-v2/fetch";
  5. export default {
  6. name: "AddDrawer",
  7. props: {
  8. selectData: {
  9. type: [Array],
  10. require: true,
  11. },
  12. dict: {
  13. type: Object,
  14. require: true,
  15. },
  16. addType: {
  17. type: String,
  18. default: "add",
  19. },
  20. },
  21. components: {
  22. ElSuperForm: () => import("@/components/super-form/index.vue"),
  23. ElSuperTable: () => import("@/components/super-table/index.vue"),
  24. ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  25. },
  26. data() {
  27. const {
  28. TabColumns,
  29. TableColumns,
  30. TabColumns: [
  31. {
  32. item: { key: tabName },
  33. },
  34. ],
  35. } = useColumns();
  36. const rules = this.$init.rules([...TabColumns, ...TableColumns]);
  37. const params = this.$init.params([...TabColumns, ...TableColumns]);
  38. return {
  39. width: "100%",
  40. visible: false,
  41. loading: false,
  42. rules: rules,
  43. params: params,
  44. tabName: tabName,
  45. TabColumns: TabColumns,
  46. TableColumns: TableColumns,
  47. };
  48. },
  49. computed: {
  50. title: {
  51. get() {
  52. const { addType } = this.$props;
  53. if (addType === "add") {
  54. this.params.source = "自制";
  55. return "新 增";
  56. }
  57. if (addType === "record") {
  58. this.params.source = "期初补录";
  59. return "期初补录";
  60. }
  61. },
  62. set() {},
  63. },
  64. },
  65. watch: {
  66. "params.isRebate": {
  67. handler: function (newValue) {
  68. if (newValue === "Y") {
  69. this.rules.rebatePolicy = [
  70. { required: true, message: "返利政策不能为空", trigger: "change" },
  71. ];
  72. } else {
  73. this.rules.rebatePolicy = null;
  74. }
  75. },
  76. immediate: true,
  77. },
  78. "params.isTarget": {
  79. handler: function (newValue) {
  80. if (newValue === "Y") {
  81. this.rules.contractTarget = [
  82. { required: true, message: "合同指标不能为空", trigger: "change" },
  83. ];
  84. } else {
  85. this.rules.contractTarget = null;
  86. }
  87. },
  88. immediate: true,
  89. },
  90. },
  91. methods: {
  92. //
  93. getTabTableColumnParams(prop) {
  94. const { TableColumns } = this.TabColumns.find(
  95. ({ item: { key } }) => key === prop
  96. );
  97. return this.$init.params(TableColumns);
  98. },
  99. //
  100. async changePaymentAgreement(prop) {
  101. const {
  102. selectData: { code },
  103. } = prop;
  104. const { data } = await LINKAGREEMENT(code);
  105. this.tabName = "contractAgreementList";
  106. this.params.contractAgreementList.push({
  107. ...this.getTabTableColumnParams("contractAgreementList"),
  108. ...data,
  109. });
  110. },
  111. //
  112. async changeMaterialName(prop) {
  113. const { row } = prop;
  114. const { rateCode } = row;
  115. try {
  116. // try
  117. this.loading = true;
  118. const { ntaxrate } = await tax(rateCode);
  119. row.tax = Number(ntaxrate === "0E-8" ? 0 : ntaxrate);
  120. } catch (err) {
  121. // catch
  122. console.error(err);
  123. } finally {
  124. // finally
  125. this.loading = false;
  126. }
  127. },
  128. //
  129. async open() {
  130. try {
  131. //
  132. this.visible = true;
  133. this.loading = true;
  134. //
  135. const {
  136. user: {
  137. deptId: puDept,
  138. deptName: puDeptName,
  139. name: buyer,
  140. nickName: buyerName,
  141. orgId: puOrg,
  142. orgName: puOrgName,
  143. },
  144. } = this.$store.state;
  145. this.params.puOrg = puOrg;
  146. this.params.puOrgName = puOrgName;
  147. this.params.buyer = buyer;
  148. this.params.buyerName = buyerName;
  149. this.params.puDept = puDept;
  150. this.params.puDeptName = puDeptName;
  151. //
  152. const { id, code, name } = await currency("人民币");
  153. this.params.currency = id;
  154. this.params.currencyCode = code;
  155. this.params.currencyName = name;
  156. //
  157. this.params.code = await CODE();
  158. //
  159. this.loading = false;
  160. } catch (error) {
  161. this.loading = false;
  162. this.$notify.error(error);
  163. }
  164. },
  165. //
  166. async hide() {
  167. const {
  168. TabColumns,
  169. TableColumns,
  170. TabColumns: [
  171. {
  172. item: { key: tabName },
  173. },
  174. ],
  175. } = useColumns();
  176. this.visible = false;
  177. this.tabName = tabName;
  178. this.params = this.$init.params([...TabColumns, ...TableColumns]);
  179. },
  180. //
  181. async useRowAdd(prop) {
  182. this.params[prop].push(this.getTabTableColumnParams(prop));
  183. },
  184. //
  185. async useRowRemove(prop, scope) {
  186. const { $index } = scope;
  187. this.params[prop].splice($index, 1);
  188. },
  189. //
  190. async useSubmit(prop) {
  191. this.$refs[prop].validate(async (valid) => {
  192. if (valid) {
  193. try {
  194. // try
  195. this.loading = true;
  196. const params = { ...this.params };
  197. const { msg, code } = await ADD(params);
  198. if (code === 200) {
  199. this.hide();
  200. this.$emit("success");
  201. this.$notify.success(msg);
  202. }
  203. } catch (err) {
  204. // catch
  205. console.error(err);
  206. } finally {
  207. // finally
  208. this.loading = false;
  209. }
  210. } else {
  211. return false;
  212. }
  213. });
  214. },
  215. },
  216. created() {},
  217. mounted() {},
  218. destroyed() {},
  219. };
  220. </script>
  221. <template>
  222. <el-button v-bind="$attrs" v-on="$listeners" @click="open">
  223. {{ title }}
  224. <el-drawer
  225. :size="width"
  226. :title="title"
  227. :visible.sync="visible"
  228. append-to-body
  229. destroy-on-close
  230. :show-close="false"
  231. @close="hide"
  232. >
  233. <div
  234. slot="title"
  235. style="
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. "
  240. >
  241. <h3 class="m-0">{{ title }}</h3>
  242. <div style="text-align: right">
  243. <el-button
  244. type="primary"
  245. :size="$attrs.size"
  246. :loading="loading"
  247. @click="useSubmit('superForm')"
  248. >确 认</el-button
  249. >
  250. <el-button :size="$attrs.size" :loading="loading" @click="hide"
  251. >取 消</el-button
  252. >
  253. </div>
  254. </div>
  255. <div
  256. v-loading="loading"
  257. style="height: 100%; display: flex; flex-direction: column"
  258. >
  259. <el-super-form
  260. v-model="params"
  261. :dict="dict"
  262. :rules="rules"
  263. :size="$attrs.size"
  264. :columns="TableColumns"
  265. ref="superForm"
  266. label-width="auto"
  267. label-position="right"
  268. style="padding: 18px; flex: 1; overflow-y: auto"
  269. >
  270. <template slot="paymentAgreement" slot-scope="scope">
  271. <component
  272. v-bind="scope.attr"
  273. v-model="scope.row[scope.item.key]"
  274. :size="$attrs.size"
  275. :source.sync="scope.row"
  276. @change="changePaymentAgreement({ ...scope, selectData: $event })"
  277. >
  278. </component>
  279. </template>
  280. </el-super-form>
  281. <el-tabs v-model="tabName" style="margin: 0 18px 18px">
  282. <el-tab-pane
  283. v-for="{ item, TableColumns: columns } in TabColumns"
  284. :key="item.key"
  285. :label="item.title"
  286. :name="item.key"
  287. lazy
  288. >
  289. <div style="height: 25vh; display: flex">
  290. <el-super-table
  291. v-model="params[item.key]"
  292. :dict="dict"
  293. :ref="tabName"
  294. :columns="columns"
  295. :size="$attrs.size"
  296. index
  297. >
  298. <template slot="materialName" slot-scope="scope">
  299. <component
  300. v-bind="scope.attr"
  301. v-model="scope.row[scope.item.key]"
  302. :size="$attrs.size"
  303. :source.sync="scope.row"
  304. @change="changeMaterialName(scope)"
  305. >
  306. </component>
  307. </template>
  308. <el-table-column fixed="right" label="操作" width="75">
  309. <template slot="header" slot-scope="scope">
  310. <el-button
  311. type="text"
  312. :size="$attrs.size"
  313. @click="useRowAdd(tabName)"
  314. >增行
  315. </el-button>
  316. </template>
  317. <template slot-scope="scope">
  318. <el-button
  319. type="text"
  320. :size="$attrs.size"
  321. @click.native.prevent="useRowRemove(tabName, scope)"
  322. >删行
  323. </el-button>
  324. </template>
  325. </el-table-column>
  326. </el-super-table>
  327. </div>
  328. </el-tab-pane>
  329. </el-tabs>
  330. </div>
  331. </el-drawer>
  332. </el-button>
  333. </template>