zhangqian
2023-08-11 c42cff043c049c0b12aca91bca31cd0799ea6f7a
增删改收款单时更改销售明细和服务合同应收金额
9个文件已修改
804 ■■■■■ 已修改文件
constvar/receipt.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 236 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 236 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 153 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/interface.go 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/receipt.go 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/salesDetails.go 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/serviceContract.go 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/receipt.go 58 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/receipt.go
@@ -1,4 +1,5 @@
package constvar
type ReceiptQueryClass string
const (
@@ -10,3 +11,13 @@
const (
    ReceiptKeywordCustomerName   ReceiptKeywordType = ""
)
type ReceiptSourceType int //来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
const (
    ReceiptSourceTypeSalesDetails    ReceiptSourceType = 1
    ReceiptSourceTypeServiceContract ReceiptSourceType = 2
    ReceiptSourceTypeInvoice         ReceiptSourceType = 3
    ReceiptSourceTypeCollectionPlan  ReceiptSourceType = 4
    ReceiptSourceTypeChuKuDan        ReceiptSourceType = 5
)
docs/docs.go
@@ -8341,6 +8341,169 @@
                }
            }
        },
        "/api/serviceOrderStatus/add": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "添加服务单状态",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddServiceOrderStatus"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/delete/{id}": {
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "删除服务单状态",
                "parameters": [
                    {
                        "type": "integer",
                        "description": "查询参数",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/list": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "获取服务单状态列表",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "ServiceOrderStatusKeywordCustomerName"
                        ],
                        "name": "keywordType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "ServiceOrderStatusQueryClassExpireLessThen60Days"
                        ],
                        "name": "queryClass",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/response.ListResponse"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/model.ServiceOrderStatus"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/update": {
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "更新服务单状态",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateServiceOrderStatus"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceType/add": {
            "post": {
                "produces": [
@@ -10008,6 +10171,24 @@
                "ServiceOrderQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.ServiceOrderStatusKeywordType": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "ServiceOrderStatusKeywordCustomerName"
            ]
        },
        "constvar.ServiceOrderStatusQueryClass": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "ServiceOrderStatusQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.UserType": {
            "type": "integer",
            "enum": [
@@ -11050,6 +11231,10 @@
        "model.Receipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -11660,6 +11845,9 @@
                "clientId": {
                    "type": "integer"
                },
                "contact": {
                    "$ref": "#/definitions/model.Contact"
                },
                "contactId": {
                    "type": "integer"
                },
@@ -11862,11 +12050,11 @@
                    "type": "string"
                },
                "carFare": {
                    "description": "交通费",
                    "description": "交通费                                                                 // 交通费",
                    "type": "number"
                },
                "chargeAmount": {
                    "description": "收费金额",
                    "description": "收费金额                                                       // 收费金额",
                    "type": "number"
                },
                "client": {
@@ -11959,6 +12147,9 @@
                    "description": "服务单编号",
                    "type": "string"
                },
                "serviceOrderStatus": {
                    "$ref": "#/definitions/model.ServiceOrderStatus"
                },
                "serviceType": {
                    "$ref": "#/definitions/model.ServiceType"
                },
@@ -11991,6 +12182,17 @@
                "timeSpentId": {
                    "description": "花费时间",
                    "type": "integer"
                }
            }
        },
        "model.ServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
@@ -12858,6 +13060,10 @@
        "request.AddReceipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -13601,6 +13807,17 @@
                "timeSpentId": {
                    "description": "花费时间",
                    "type": "integer"
                }
            }
        },
        "request.AddServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
@@ -15703,6 +15920,10 @@
        "request.UpdateReceipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -16698,6 +16919,17 @@
                }
            }
        },
        "request.UpdateServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "request.UpdateServiceType": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -8329,6 +8329,169 @@
                }
            }
        },
        "/api/serviceOrderStatus/add": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "添加服务单状态",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddServiceOrderStatus"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/delete/{id}": {
            "delete": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "删除服务单状态",
                "parameters": [
                    {
                        "type": "integer",
                        "description": "查询参数",
                        "name": "id",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/list": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "获取服务单状态列表",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "ServiceOrderStatusKeywordCustomerName"
                        ],
                        "name": "keywordType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "ServiceOrderStatusQueryClassExpireLessThen60Days"
                        ],
                        "name": "queryClass",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/response.ListResponse"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/model.ServiceOrderStatus"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api/serviceOrderStatus/update": {
            "put": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "服务单状态"
                ],
                "summary": "更新服务单状态",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.UpdateServiceOrderStatus"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/serviceType/add": {
            "post": {
                "produces": [
@@ -9996,6 +10159,24 @@
                "ServiceOrderQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.ServiceOrderStatusKeywordType": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "ServiceOrderStatusKeywordCustomerName"
            ]
        },
        "constvar.ServiceOrderStatusQueryClass": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "ServiceOrderStatusQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.UserType": {
            "type": "integer",
            "enum": [
@@ -11038,6 +11219,10 @@
        "model.Receipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -11648,6 +11833,9 @@
                "clientId": {
                    "type": "integer"
                },
                "contact": {
                    "$ref": "#/definitions/model.Contact"
                },
                "contactId": {
                    "type": "integer"
                },
@@ -11850,11 +12038,11 @@
                    "type": "string"
                },
                "carFare": {
                    "description": "交通费",
                    "description": "交通费                                                                 // 交通费",
                    "type": "number"
                },
                "chargeAmount": {
                    "description": "收费金额",
                    "description": "收费金额                                                       // 收费金额",
                    "type": "number"
                },
                "client": {
@@ -11947,6 +12135,9 @@
                    "description": "服务单编号",
                    "type": "string"
                },
                "serviceOrderStatus": {
                    "$ref": "#/definitions/model.ServiceOrderStatus"
                },
                "serviceType": {
                    "$ref": "#/definitions/model.ServiceType"
                },
@@ -11979,6 +12170,17 @@
                "timeSpentId": {
                    "description": "花费时间",
                    "type": "integer"
                }
            }
        },
        "model.ServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
@@ -12846,6 +13048,10 @@
        "request.AddReceipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -13589,6 +13795,17 @@
                "timeSpentId": {
                    "description": "花费时间",
                    "type": "integer"
                }
            }
        },
        "request.AddServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
@@ -15691,6 +15908,10 @@
        "request.UpdateReceipt": {
            "type": "object",
            "properties": {
                "amount": {
                    "description": "收款金额",
                    "type": "string"
                },
                "bankAccountId": {
                    "description": "账户id",
                    "type": "integer"
@@ -16686,6 +16907,17 @@
                }
            }
        },
        "request.UpdateServiceOrderStatus": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "request.UpdateServiceType": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -264,6 +264,18 @@
    type: string
    x-enum-varnames:
    - ServiceOrderQueryClassExpireLessThen60Days
  constvar.ServiceOrderStatusKeywordType:
    enum:
    - ""
    type: string
    x-enum-varnames:
    - ServiceOrderStatusKeywordCustomerName
  constvar.ServiceOrderStatusQueryClass:
    enum:
    - ""
    type: string
    x-enum-varnames:
    - ServiceOrderStatusQueryClassExpireLessThen60Days
  constvar.UserType:
    enum:
    - 1
@@ -951,6 +963,9 @@
    type: object
  model.Receipt:
    properties:
      amount:
        description: 收款金额
        type: string
      bankAccountId:
        description: 账户id
        type: integer
@@ -1360,6 +1375,8 @@
        $ref: '#/definitions/model.Client'
      clientId:
        type: integer
      contact:
        $ref: '#/definitions/model.Contact'
      contactId:
        type: integer
      endTime:
@@ -1494,10 +1511,12 @@
        description: 预约上门时间
        type: string
      carFare:
        description: 交通费
        description: 交通费                                                                 //
          交通费
        type: number
      chargeAmount:
        description: 收费金额
        description: 收费金额                                                       //
          收费金额
        type: number
      client:
        $ref: '#/definitions/model.Client'
@@ -1564,6 +1583,8 @@
      serviceNumber:
        description: 服务单编号
        type: string
      serviceOrderStatus:
        $ref: '#/definitions/model.ServiceOrderStatus'
      serviceType:
        $ref: '#/definitions/model.ServiceType'
      serviceTypeId:
@@ -1588,6 +1609,13 @@
      timeSpentId:
        description: 花费时间
        type: integer
    type: object
  model.ServiceOrderStatus:
    properties:
      id:
        type: integer
      name:
        type: string
    type: object
  model.ServiceType:
    properties:
@@ -2168,6 +2196,9 @@
    type: object
  request.AddReceipt:
    properties:
      amount:
        description: 收款金额
        type: string
      bankAccountId:
        description: 账户id
        type: integer
@@ -2678,6 +2709,13 @@
      timeSpentId:
        description: 花费时间
        type: integer
    type: object
  request.AddServiceOrderStatus:
    properties:
      id:
        type: integer
      name:
        type: string
    type: object
  request.AddServiceType:
    properties:
@@ -4108,6 +4146,9 @@
    type: object
  request.UpdateReceipt:
    properties:
      amount:
        description: 收款金额
        type: string
      bankAccountId:
        description: 账户id
        type: integer
@@ -4786,6 +4827,13 @@
      timeSpentId:
        description: 花费时间
        type: integer
    type: object
  request.UpdateServiceOrderStatus:
    properties:
      id:
        type: integer
      name:
        type: string
    type: object
  request.UpdateServiceType:
    properties:
@@ -10615,6 +10663,107 @@
      summary: 更新服务单
      tags:
      - 服务单管理
  /api/serviceOrderStatus/add:
    post:
      parameters:
      - description: 查询参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.AddServiceOrderStatus'
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            $ref: '#/definitions/contextx.Response'
      summary: 添加服务单状态
      tags:
      - 服务单状态
  /api/serviceOrderStatus/delete/{id}:
    delete:
      parameters:
      - description: 查询参数
        in: path
        name: id
        required: true
        type: integer
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            $ref: '#/definitions/contextx.Response'
      summary: 删除服务单状态
      tags:
      - 服务单状态
  /api/serviceOrderStatus/list:
    get:
      parameters:
      - in: query
        name: keyword
        type: string
      - enum:
        - ""
        in: query
        name: keywordType
        type: string
        x-enum-varnames:
        - ServiceOrderStatusKeywordCustomerName
      - description: 页码
        in: query
        name: page
        type: integer
      - description: 每页大小
        in: query
        name: pageSize
        type: integer
      - enum:
        - ""
        in: query
        name: queryClass
        type: string
        x-enum-varnames:
        - ServiceOrderStatusQueryClassExpireLessThen60Days
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            allOf:
            - $ref: '#/definitions/response.ListResponse'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/model.ServiceOrderStatus'
                  type: array
              type: object
      summary: 获取服务单状态列表
      tags:
      - 服务单状态
  /api/serviceOrderStatus/update:
    put:
      parameters:
      - description: 查询参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.UpdateServiceOrderStatus'
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            $ref: '#/definitions/contextx.Response'
      summary: 更新服务单状态
      tags:
      - 服务单状态
  /api/serviceType/add:
    post:
      parameters:
model/interface.go
@@ -6,8 +6,8 @@
)
type MoneyAdder interface {
    AmountReceivableAdd(*gorm.DB, decimal.Decimal)
    AmountReceivedAdd(*gorm.DB, decimal.Decimal)
    AmountInvoicedAdd(*gorm.DB, decimal.Decimal)
    AmountNotInvoicedAdd(*gorm.DB, decimal.Decimal)
    AmountReceivableAdd(tx *gorm.DB, id int, amount decimal.Decimal) error
    AmountReceivedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error
    AmountInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error
    AmountNotInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error
}
model/receipt.go
@@ -5,6 +5,7 @@
    "aps_crm/pkg/mysqlx"
    "errors"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
)
@@ -12,16 +13,18 @@
    // Receipt 收款单
    Receipt struct {
        Id            int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ClientId      int    `gorm:"client_id" json:"clientId"`            // 客户id
        SourceType    int    `gorm:"source_type" json:"sourceType"`        // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
        SourceId      int    `gorm:"source_id" json:"sourceId"`            // 源单id
        PrincipalId   int    `gorm:"principal_id" json:"principalId"`      // 负责人id
        ReceiptDate   string `gorm:"receipt_date" json:"receiptDate"`      // 收款日期
        MoneyType     string `gorm:"money_type" json:"moneyType"`          // 币种
        PaymentTypeId int    `gorm:"payment_type_id" json:"paymentTypeId"` // 收款方式ID
        BankAccountId int    `gorm:"bank_account_id" json:"bankAccountId"` // 账户id
        Remark        string `gorm:"remark" json:"remark"`                 // 备注
        FileId        int    `gorm:"file_id" json:"fileId"`                // 附件id
        ClientId      int                        `gorm:"column:client_id;type:int;not null;default 0;comment:客户id" json:"clientId"`                                // 客户id
        SourceType    constvar.ReceiptSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)" json:"sourceType"` // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
        SourceId      int                        `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"`                               // 源单id
        PrincipalId   int                        `gorm:"column:principal_id;type:int;not null;default 0;comment:负责人id" json:"principalId"`                         // 负责人id
        ReceiptDate   string                     `gorm:"column:receipt_date;type:varchar(255);not null;default '';comment:收款日期" json:"receiptDate"`                // 收款日期
        MoneyType     string                     `gorm:"column:money_type;type:varchar(255);not null;default '';comment:币种" json:"moneyType"`                      // 币种
        Amount        decimal.Decimal            `gorm:"column:amount;type:decimal(12,2);not null;default '0.00';comment:收款金额" json:"amount"`                      // 收款金额
        PaymentTypeId int                        `gorm:"column:payment_type_id;type:int;not null;default 0;comment:收款方式ID" json:"paymentTypeId"`                   // 收款方式ID
        BankAccountId int                        `gorm:"column:bank_account_id;type:int;not null;default 0;comment:账户id" json:"bankAccountId"`                     // 账户id
        Remark        string                     `gorm:"column:remark;type:varchar(255);not null;default '';comment:备注" json:"remark"`                             // 备注
        FileId        int                        `gorm:"column:file_id;type:int;not null;default 0;comment:附件id" json:"fileId"`                                    // 附件id
        gorm.Model    `json:"-"`
    }
    // ReceiptSearch 收款单搜索条件
model/salesDetails.go
@@ -173,3 +173,45 @@
    return nil
}
func (slf *SalesDetailsSearch) SetOrm(tx *gorm.DB) *SalesDetailsSearch {
    slf.Orm = tx
    return slf
}
func (slf *SalesDetailsSearch) AmountReceivableAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountReceivable.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_receivable": amount})
}
func (slf *SalesDetailsSearch) AmountReceivedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountReceived.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_received": amount})
}
func (slf *SalesDetailsSearch) AmountInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountInvoiced.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_invoiced": amount})
}
func (slf *SalesDetailsSearch) AmountNotInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountUnInvoiced.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_not_invoiced": amount})
}
model/serviceContract.go
@@ -216,3 +216,40 @@
    return record, nil
}
func (slf *ServiceContractSearch) AmountReceivableAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountReceivable.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_receivable": amount})
}
func (slf *ServiceContractSearch) AmountReceivedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountReceived.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_received": amount})
}
func (slf *ServiceContractSearch) AmountInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountInvoiced.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_invoiced": amount})
}
func (slf *ServiceContractSearch) AmountNotInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
    slf.Orm = tx
    record, err := slf.SetId(id).First()
    if err != nil {
        return err
    }
    amount = record.AmountUnInvoiced.Add(amount)
    return slf.UpdateByMap(map[string]interface{}{"amount_not_invoiced": amount})
}
service/receipt.go
@@ -1,9 +1,12 @@
package service
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/pkg/ecode"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
)
type ReceiptService struct{}
@@ -13,16 +16,45 @@
}
func (ReceiptService) AddReceipt(receipt *model.Receipt) int {
    err := model.NewReceiptSearch().Create(receipt)
    var moneyAdder model.MoneyAdder
    switch receipt.SourceType {
    case constvar.ReceiptSourceTypeSalesDetails:
        moneyAdder = model.NewSalesDetailsSearch()
    case constvar.ReceiptSourceTypeServiceContract:
        moneyAdder = model.NewServiceContractSearch()
    }
    err := model.WithTransaction(func(db *gorm.DB) error {
        err := model.NewReceiptSearch().SetOrm(db).Create(receipt)
        if err != nil {
            return err
        }
        return moneyAdder.AmountReceivableAdd(db, receipt.SourceId, decimal.Zero.Sub(receipt.Amount).Round(2))
    })
    if err != nil {
        return ecode.DBErr
    }
    return ecode.OK
}
func (ReceiptService) DeleteReceipt(id int) int {
    receipt, err := model.NewReceiptSearch().SetId(id).First()
    if err != nil {
        return ecode.DBErr
    }
    var moneyAdder model.MoneyAdder
    switch receipt.SourceType {
    case constvar.ReceiptSourceTypeSalesDetails:
        moneyAdder = model.NewSalesDetailsSearch()
    case constvar.ReceiptSourceTypeServiceContract:
        moneyAdder = model.NewServiceContractSearch()
    }
    err = model.WithTransaction(func(db *gorm.DB) error {
    err := model.NewReceiptSearch().SetId(id).Delete()
        if err != nil {
            return err
        }
        return moneyAdder.AmountReceivableAdd(db, receipt.SourceId, receipt.Amount.Round(2))
    })
    if err != nil {
        return ecode.DBErr
    }
@@ -46,9 +78,7 @@
            return ecode.DBErr
        }
        err = model.NewReceiptSearch().SetId(v.Id).Updates(map[string]interface{}{
        })
        err = model.NewReceiptSearch().SetId(v.Id).Updates(map[string]interface{}{})
        if err != nil {
            return ecode.DBErr
        }
@@ -58,8 +88,26 @@
}
func (ReceiptService) UpdateReceipt(receipt *model.Receipt) int {
    old, err := model.NewReceiptSearch().SetId(receipt.Id).First()
    if err != nil {
        return ecode.DBErr
    }
    var moneyAdder model.MoneyAdder
    switch receipt.SourceType {
    case constvar.ReceiptSourceTypeSalesDetails:
        moneyAdder = model.NewSalesDetailsSearch()
    case constvar.ReceiptSourceTypeServiceContract:
        moneyAdder = model.NewServiceContractSearch()
    }
    err = model.WithTransaction(func(db *gorm.DB) error {
        err := model.NewReceiptSearch().SetId(receipt.Id).Save(receipt)
        if err != nil {
            return err
        }
        diffAmount := receipt.Amount.Sub(old.Amount)
        return moneyAdder.AmountReceivableAdd(db, receipt.SourceId, diffAmount.Round(2))
    })
    if err != nil {
            return ecode.DBErr
        }
    return ecode.OK