@@ -1,7 +1,7 @@
'use strict' ;
let recoveryListRequest = 0 , recoveryQuoteRequest = 0 , recoveryQuoteTimer = null , recoveryDetailRequest = 0 , recoveryDetail = null , recoveryFilters = null , recoveryFilterUser = '' ;
const recoveryModeNames = { direct : '直接回收' , exchange : '置换' } ;
const recoveryDraftFields = [ 'old_source' , 'deposit_id' , 'deposit_weight' , 'mode' , 'date' , 'customer' , 'salesperson' , 'member' , 'old_name' , 'incoming_weight' , 'melted_weight' , 'purity' , 'old_pocket' , 'old_stock_count' , 'buy_rate' , 'buy_total' , 'new_name' , 'new_quantity' , 'pocket' , 'stockCount' , 'gold_price' , 'labor_mode' , 'labor_rate' , 'depreciation_weight' , 'depreciation_rate' , 'charge_name' , 'charge_amount' , 'discount' , 'rounding_mode' , 'material_source' , 'material_pool_id' , 'material_rate' , 'material_payer' , 'cost_labor_mode' , 'cost_labor_value' , 'labor_payer' , 'fee_note' , 'fee_amount' , 'fee_payer' , 'payment_method' , 'payment_recipient' , 'payout_method' , 'payer' , 'note' ] ;
const recoveryDraftFields = [ 'surplus_mode' , 'surplus_memo' , ' old_source', 'deposit_id' , 'deposit_weight' , 'mode' , 'date' , 'customer' , 'salesperson' , 'member' , 'old_name' , 'incoming_weight' , 'melted_weight' , 'purity' , 'old_pocket' , 'old_stock_count' , 'buy_rate' , 'buy_total' , 'new_name' , 'new_quantity' , 'pocket' , 'stockCount' , 'gold_price' , 'labor_mode' , 'labor_rate' , 'depreciation_weight' , 'depreciation_rate' , 'charge_name' , 'charge_amount' , 'discount' , 'rounding_mode' , 'material_source' , 'material_pool_id' , 'material_rate' , 'material_payer' , 'cost_labor_mode' , 'cost_labor_value' , 'labor_payer' , 'fee_note' , 'fee_amount' , 'fee_payer' , 'payment_method' , 'payment_recipient' , 'payout_method' , 'payer' , 'note' ] ;
const recoveryText = value => String ( value ? ? '' ) . trim ( ) ;
const recoveryDraftKey = ( ) => ` mm-recovery-draft- ${ state . user . id } ` ;
const recoveryUser = ( ) => state ? . user ? . id || '' ;
@@ -35,7 +35,7 @@ function recoveryExtraProductHTML(row,index){
}
function recoveryDefaults ( mode = 'direct' ) {
const member = currentMember ( ) ;
return { old _source : 'incoming' , deposit _id : '' , deposit _weight : '' , mode , date : state . today , customer : '' , salesperson : defaultSalesperson ( ) , member , old _name : 'Au旧料' , incoming _weight : '' , melted _weight : '' , purity : '100' , old _pocket : '回收旧料' , old _stock _count : '' , buy _rate : '' , buy _total : '' , new _name : '足金999' , new _quantity : '' , pocket : '现货' , stockCount : '' , gold _price : '' , labor _mode : 'gram' , labor _rate : '' , depreciation _weight : '' , depreciation _rate : '10' , charge _name : '其他收费' , charge _amount : '0' , discount : '0' , rounding _mode : 'yuan' , material _source : 'pool' , material _pool _id : state . today , material _rate : '' , material _payer : member , cost _labor _mode : 'gram' , cost _labor _value : '' , labor _payer : member , fee _note : '其他费用' , fee _amount : '0' , fee _payer : member , payment _method : '微信' , payment _recipient : recoveryPrefix ( member ) , payout _method : '微信' , payer : member , note : '' , buy _total _manual : false , depreciation _manual : false , melted _manual : false } ;
return { surplus _mode : 'buy' , surplus _memo : '' , old _source : 'incoming' , deposit _id : '' , deposit _weight : '' , mode , date : state . today , customer : '' , salesperson : defaultSalesperson ( ) , member , old _name : 'Au旧料' , incoming _weight : '' , melted _weight : '' , purity : '100' , old _pocket : '回收旧料' , old _stock _count : '' , buy _rate : '' , buy _total : '' , new _name : '足金999' , new _quantity : '' , pocket : '现货' , stockCount : '' , gold _price : '' , labor _mode : 'gram' , labor _rate : '' , depreciation _weight : '' , depreciation _rate : '10' , charge _name : '其他收费' , charge _amount : '0' , discount : '0' , rounding _mode : 'yuan' , material _source : 'pool' , material _pool _id : state . today , material _rate : '' , material _payer : member , cost _labor _mode : 'gram' , cost _labor _value : '' , labor _payer : member , fee _note : '其他费用' , fee _amount : '0' , fee _payer : member , payment _method : '微信' , payment _recipient : recoveryPrefix ( member ) , payout _method : '微信' , payer : member , note : '' , buy _total _manual : false , depreciation _manual : false , melted _manual : false } ;
}
function recoveryStoredDraft ( ) {
let value ; try { value = JSON . parse ( localStorage . getItem ( recoveryDraftKey ( ) ) || 'null' ) ; } catch { }
@@ -63,6 +63,7 @@ function recoveryInput(draft,{quote=false}={}){
const input = { mode : draft . mode , date : draft . date , customer : draft . customer , salesperson : draft . salesperson , member , note : draft . note , old _pocket : draft . old _pocket ? ? '回收旧料' , ... ( pocketCountsEnabled ( draft . old _pocket ) ? { old _stock _count : recoveryText ( draft . old _stock _count ) || null } : { } ) , old _lines : Array . isArray ( draft . old _lines ) ? draft . old _lines : [ { name : draft . old _name || 'Au旧料' , incoming _weight : draft . incoming _weight , melted _weight : draft . melted _weight , purity : draft . purity } ] , buy _rate : draft . buy _rate , payer : draft . payer || member , payment _account : recoveryPrefix ( draft . payer || member ) + ( draft . payout _method || '微信' ) , verified : ! quote && draft . verified === true } ;
if ( draft . mode === 'exchange' && draft . old _source === 'deposit' ) Object . assign ( input , { old _source : 'deposit' , deposit _id : draft . deposit _id , deposit _weight : draft . deposit _weight } ) ;
if ( draft . buy _total _manual && draft . buy _total !== '' ) input . buy _total = draft . buy _total ;
if ( draft . mode === 'exchange' && draft . surplus _mode === 'deposit' ) Object . assign ( input , { surplus _mode : 'deposit' , surplus _memo : draft . surplus _memo || '' , buy _rate : '0' , buy _total : '0' } ) ;
if ( draft . mode === 'exchange' ) {
const costLabor = draft . cost _labor _value === '' ? undefined : draft . cost _labor _mode === 'total' ? { mode : 'total' , amount : draft . cost _labor _value } : { mode : 'gram' , rate : draft . cost _labor _value } ;
const costMaterial = draft . material _source === 'pool' ? { mode : 'pool' , poolId : draft . material _pool _id || draft . date } : draft . material _rate === '' ? undefined : { mode : 'gram' , rate : draft . material _rate } ;
@@ -100,12 +101,13 @@ function recoveryCalculate(input){
const before = recoveryLimit ( goldAmount + laborAmount + depreciationAmount + chargesAmount ) - discount ; if ( before < 0 ) throw new Error ( '优惠金额不能大于本次置换费用。' ) ;
receivable = x . rounding _mode === 'yuan' ? Math . floor ( ( before + 50 ) / 100 ) * 100 : before ; roundingAdjustment = receivable - before ;
}
if ( surplusMg > 0 && buyRate <= 0 ) throw new Error ( '有回收旧料或余料时,请填写大于0的回收单价。' ) ;
const defaultPurchaseAmount = recoveryMul ( surplusMg , buyRate ) , purchaseAmount = input . buy _total == null ? defaultPurchaseAmount : recoveryUnits ( input . buy _total , 2 , input . mode === 'direct' ? '回收结算额' : '余料回收款 ') ;
if ( surplusMg > 0 && purchaseAmount <= 0 ) throw new Error ( '回收结算金额须大于0,请核对。 ') ;
const depositMg = input . mode === 'exchange' && input . surplus _mode === 'deposit' ? surplusMg : 0 , purchaseMg = surplusMg - depositMg ;
if ( purchaseMg > 0 && buyRate <= 0 ) throw new Error ( '有 回收旧料或余料时,请填写大于0的回收单价。 ' ) ;
const defaultPurchaseAmount = recoveryMul ( purchaseMg , buyRate ) , purchaseAmount = depositMg ? 0 : input . buy _total == null ? defaultPurchaseAmount : recoveryUnits ( input . buy _total , 2 , input . mode === 'direct' ? '回收结算额' : '余料回收款 ') ;
if ( purchaseMg > 0 && purchaseAmount <= 0 ) throw new Error ( '回收结算金额须大于0,请核对。' ) ;
if ( ! surplusMg && purchaseAmount ) throw new Error ( '本次没有余料,余料回收款应为0。' ) ;
const netSettlement = recoveryLimit ( receivable - purchaseAmount ) ;
return { mode : input . mode , old _lines : oldLines , oldMg , newMg , matchedMg , differenceMg , surplusMg , goldAmount , laborAmount , depreciationMg , depreciationAmount , chargesAmount , discount , receivable , purchaseAmount , defaultPurchaseAmount , roundingAdjustment , netSettlement , cashReceipt : Math . max ( 0 , netSettlement ) , cashPayment : Math . max ( 0 , - netSettlement ) } ;
return { mode : input . mode , old _lines : oldLines , oldMg , newMg , matchedMg , differenceMg , surplusMg , depositMg , purchaseMg , goldAmount , laborAmount , depreciationMg , depreciationAmount , chargesAmount , discount , receivable , purchaseAmount , defaultPurchaseAmount , roundingAdjustment , netSettlement , cashReceipt : Math . max ( 0 , netSettlement ) , cashPayment : Math . max ( 0 , - netSettlement ) } ;
}
function recoveryValidationOriginal ( input ) { const source = recoveryEditEntry ? . data ? . inputs , item = source ? . exchange ? . items ? . [ input . _recoveryItemIndex ] ; return item ? { ... source , ... item } : source ; }
function validateRecoveryInput ( input ) {
@@ -156,14 +158,14 @@ function recoveryFormHTML(draft){
if ( draft . mode === 'exchange' && draft . independent _products ) return recoveryItemsFormHTML ( draft ) ;
const exchange = draft . mode === 'exchange' , members = teamMemberOptions ( ) , recipients = members . map ( ( [ member , name ] ) => [ recoveryPrefix ( member ) , name ] ) ;
const oldPocket = draft . old _pocket ? ? '回收旧料' , oldOriginal = typeof recoveryEditEntry !== 'undefined' ? recoveryEditEntry ? . data ? . inputs : null ;
return ` <form id="recovery-form" data-independent-default=" ${ ! ! draft . independent _products } " data-buy_total_manual=" ${ ! ! draft . buy _total _manual } " data-depreciation_manual=" ${ ! ! draft . depreciation _manual } " data-melted_manual=" ${ ! ! draft . melted _manual } "><input name="mode" type="hidden" value=" ${ e ( draft . mode ) } "><div class="recovery-mode-switch" aria-label="业务类型"> ${ Object . entries ( recoveryModeNames ) . map ( ( [ mode , name ] ) => button ( e ( name ) , 'recovery-mode' , draft . mode === mode ? 'active' : '' , ` data-mode=" ${ mode } " aria-pressed=" ${ draft . mode === mode } " ` ) ) . join ( '' ) } ${ button ( '寄存,暂不结算' , 'customer-material-new' ) } </div><section class="panel"><div class="panel-title"><h2>客户与单据</h2><span class="small muted">保存后自动编号</span></div><div class="recovery-row recovery-identity"> ${ field ( '业务日期' , 'date' , draft . date , 'date' , ` max=" ${ e ( state . today ) } " required ` ) } ${ field ( '客户称呼/代号' , 'customer' , draft . customer , 'text' , 'maxlength="60" required placeholder="必填"' ) } ${ field ( '开单人' , 'salesperson' , draft . salesperson , 'text' , 'maxlength="30" required' ) } ${ recoveryMemberField ( draft ) } </div></section><section class="panel"><div class="panel-title"><h2>旧料称重</h2><span class="small muted">成色按百分数填写,例如99表示99%</span></div> ${ exchange ? customerMaterialSourceHTML ( draft ) : '' } <div class="recovery-row recovery-old-fields"> ${ field ( '旧料名称' , 'old_name' , draft . old _name , 'text' , 'maxlength="60" required' ) } ${ recoveryNumberField ( '来料重量(g) ' , 'incoming_weight' , draft . incoming _weight , 3 , 'required' ) } ${ recoveryNumberField ( '熔后重量(g) ' , 'melted_weight' , draft . melted _weight , 3 , 'required' ) } ${ recoveryNumberField ( '成色(%) ' , 'purity' , draft . purity , 3 , 'max="100" required' ) } ${ recoveryNumberField ( '折后重量(g,自动)' , 'folded_weight' , '' , 3 , 'readonly tabindex="-1"' ) } </div><p class="small muted recovery-formula" id="recovery-folded-formula" aria-live="polite">折后重量 = 熔后重量 × 成色 ÷ 100。</p><div class="recovery-row"> ${ pocketChoice ( '旧料入库货盘' , 'old_pocket' , oldPocket , { blank : null , historical : ! ! oldOriginal && ( oldOriginal . old _pocket ? ? '回收旧料' ) === oldPocket } )} ${ stockCountField ( oldPocket , draft . old _stock _count , { name : 'old_stock_count' , title : '旧料入库数量(对)' , help : '' , optional : ! ! oldOriginal && oldOriginal . old _stock _count == null && ( oldOriginal . old _pocket ? ? '回收旧料' ) === oldPocket } )}</div><p class="small muted">旧料按全部折后重量入所选货盘;置换新货按下方出货货盘扣减。</p></section><section class="panel recovery-exchange" data-recovery-exchange ${ exchange ? '' : 'hidden' } ><div class="panel-title"><h2>置换新货</h2><span class="small muted">新货工费按全部新货克重计算</span></div><div class="note">此草稿沿用整单共用旧料的方式。 ${ button ( '改为逐件完整置换' , 'recovery-upgrade-items' ) } <span class="small">切换后请逐件分配旧料重量并核对,保存前原账不变。</span></div><h3>新货 1</h3><datalist id="recovery-product-names"> ${ productNames . filter ( name => ! LedgerCalc . isSilver ( name ) ) . map ( name => ` <option value=" ${ e ( name ) } "></option> ` ) . join ( '' ) } </datalist> ${ recoveryProductFieldsHTML ( draft , { original : oldOriginal ? . exchange ? . products ? . [ 0 ] ? ? oldOriginal ? . exchange } )}<div class="recovery-row recovery-price-fields"> ${ recoveryNumberField ( '当天销售金价(元/g) ' , 'gold_price' , draft . gold _price , 2 , 'required' ) } ${ recoveryNumberField ( '折旧费单价(元/g) ' , 'depreciation_rate' , draft . depreciation _rate , 2 , 'required' ) } ${ recoveryNumberField ( '折旧计费克重(g,可改)' , 'depreciation_weight' , draft . depreciation _weight , 3 , 'required' ) } </div> ${ ( draft . extra _products || [ ] ) . map ( recoveryExtraProductHTML ) . join ( '' ) } <div class="form-actions"> ${ button ( '+ 添加置换商品' , 'recovery-add-product' ) } <span class="small muted">补金、折旧和余料按新货总克重结算;每件可选不同货盘或外配。</span></div><p class="small muted recovery-formula">折旧计费克重默认取折后旧重与新货克重中较小的一个。 ${ button ( '恢复默认克重' , 'recovery-reset-depreciation' , 'quiet' ) } </p><details class="recovery-optional"><summary>其他收费与优惠(选填)</summary><div class="recovery-row"> ${ field ( '其他收费名称' , 'charge_name' , draft . charge _name , 'text' , 'maxlength="60"' ) } ${ recoveryNumberField ( '其他收费(元)' , 'charge_amount' , draft . charge _amount ) } ${ recoveryNumberField ( '优惠金额(元)' , 'discount' , draft . discount ) } ${ choice ( '置换费用取整' , 'rounding_mode' , [ [ 'yuan' , '四舍五入到元' ] , [ 'cent' , '保留到分' ] ] , draft . rounding _mode ) } </div></details></section><section class="panel recovery-buy-panel"><div class="panel-title"><h2 id="recovery-buy-title"> ${ exchange ? '余料回收' : '回收结算' } </h2><span class="small muted" id="recovery-buy-help"> ${ exchange ? '仅多出的余料进入购料池' : '保存后自动记购料与' + e ( pocketLabel ( oldPocket ) ) + '库存' } </span></div><div class="recovery-row recovery-buy-fields"> ${ recoveryNumberField ( ' 回收单价 ( 元 Line truncated
return ` <form id="recovery-form" data-independent-default=" ${ ! ! draft . independent _products } " data-buy_total_manual=" ${ ! ! draft . buy _total _manual } " data-depreciation_manual=" ${ ! ! draft . depreciation _manual } " data-melted_manual=" ${ ! ! draft . melted _manual } "><input name="mode" type="hidden" value=" ${ e ( draft . mode ) } "><div class="recovery-mode-switch" aria-label="业务类型"> ${ Object . entries ( recoveryModeNames ) . map ( ( [ mode , name ] ) => button ( e ( name ) , 'recovery-mode' , draft . mode === mode ? 'active' : '' , ` data-mode=" ${ mode } " aria-pressed=" ${ draft . mode === mode } " ` ) ) . join ( '' ) } ${ button ( '寄存,暂不结算' , 'customer-material-new' ) } </div><section class="panel"><div class="panel-title"><h2>客户与单据</h2><span class="small muted">保存后自动编号</span></div><div class="recovery-row recovery-identity"> ${ field ( '业务日期' , 'date' , draft . date , 'date' , ` max=" ${ e ( state . today ) } " required ` ) } ${ field ( '客户称呼/代号' , 'customer' , draft . customer , 'text' , 'maxlength="60" required placeholder="必填"' ) } ${ field ( '开单人' , 'salesperson' , draft . salesperson , 'text' , 'maxlength="30" required' ) } ${ recoveryMemberField ( draft ) } </div></section><section class="panel"><div class="panel-title"><h2>旧料称重</h2><span class="small muted">成色按百分数填写,例如99表示99%</span></div> ${ exchange ? customerMaterialSourceHTML ( draft ) : '' } <div class="recovery-row recovery-old-fields"> ${ field ( '旧料名称' , 'old_name' , draft . old _name , 'text' , 'maxlength="60" required' ) } ${ recoveryNumberField ( '来料重量(g) ' , 'incoming_weight' , draft . incoming _weight , 3 , 'required' ) } ${ recoveryNumberField ( '熔后重量(g) ' , 'melted_weight' , draft . melted _weight , 3 , 'required' ) } ${ recoveryNumberField ( '成色(%) ' , 'purity' , draft . purity , 3 , 'max="100" required' ) } ${ recoveryNumberField ( '折后重量(g,自动)' , 'folded_weight' , '' , 3 , 'readonly tabindex="-1"' ) } </div><p class="small muted recovery-formula" id="recovery-folded-formula" aria-live="polite">折后重量 = 熔后重量 × 成色 ÷ 100。</p><div class="recovery-row"> ${ pocketChoice ( '旧料入库货盘' , 'old_pocket' , oldPocket , { blank : null , historical : ! ! oldOriginal && ( oldOriginal . old _pocket ? ? '回收旧料' ) === oldPocket } )} ${ stockCountField ( oldPocket , draft . old _stock _count , { name : 'old_stock_count' , title : '旧料入库数量(对)' , help : '' , optional : ! ! oldOriginal && oldOriginal . old _stock _count == null && ( oldOriginal . old _pocket ? ? '回收旧料' ) === oldPocket } )}</div><p class="small muted">已结算的旧料入所选货盘;暂存余料单独记入客户寄存料。置换新货按出货货盘扣减。</p></section><section class="panel recovery-exchange" data-recovery-exchange ${ exchange ? '' : 'hidden' } ><div class="panel-title"><h2>置换新货</h2><span class="small muted">新货工费按全部新货克重计算</span></div><div class="note">此草稿沿用整单共用旧料的方式。 ${ button ( '改为逐件完整置换' , 'recovery-upgrade-items' ) } <span class="small">切换后请逐件分配旧料重量并核对,保存前原账不变。</span></div><h3>新货 1</h3><datalist id="recovery-product-names"> ${ productNames . filter ( name => ! LedgerCalc . isSilver ( name ) ) . map ( name => ` <option value=" ${ e ( name ) } "></option> ` ) . join ( '' ) } </datalist> ${ recoveryProductFieldsHTML ( draft , { original : oldOriginal ? . exchange ? . products ? . [ 0 ] ? ? oldOriginal ? . exchange } )}<div class="recovery-row recovery-price-fields"> ${ recoveryNumberField ( '当天销售金价(元/g) ' , 'gold_price' , draft . gold _price , 2 , 'required' ) } ${ recoveryNumberField ( '折旧费单价(元/g) ' , 'depreciation_rate' , draft . depreciation _rate , 2 , 'required' ) } ${ recoveryNumberField ( '折旧计费克重(g,可改)' , 'depreciation_weight' , draft . depreciation _weight , 3 , 'required' ) } </div> ${ ( draft . extra _products || [ ] ) . map ( recoveryExtraProductHTML ) . join ( '' ) } <div class="form-actions"> ${ button ( '+ 添加置换商品' , 'recovery-add-product' ) } <span class="small muted">补金、折旧和余料按新货总克重结算;每件可选不同货盘或外配。</span></div><p class="small muted recovery-formula">折旧计费克重默认取折后旧重与新货克重中较小的一个。 ${ button ( '恢复默认克重' , 'recovery-reset-depreciation' , 'quiet' ) } </p><details class="recovery-optional"><summary>其他收费与优惠(选填)</summary><div class="recovery-row"> ${ field ( '其他收费名称' , 'charge_name' , draft . charge _name , 'text' , 'maxlength="60"' ) } ${ recoveryNumberField ( '其他收费(元)' , 'charge_amount' , draft . charge _amount ) } ${ recoveryNumberField ( '优惠金额(元)' , 'discount' , draft . discount ) } ${ choice ( '置换费用取整' , 'rounding_mode' , [ [ 'yuan' , '四舍五入到元' ] , [ 'cent' , '保留到分' ] ] , draft . rounding _mode ) } </div></details></section><section class="panel recovery-buy-panel"><div class="panel-title"><h2 id="recovery-buy-title"> ${ exchange ? '余料处理' : '回收结算' } </h2><span class="small muted" id="recovery-buy-help"> ${ exchange ? '余料可按价回收,也可寄存暂不结算' : '保存后自动记购料与' + e ( pocketLabel ( oldPocket ) ) + '库存' } </span></div> ${ exchange ? recoverySurplusFieldsHTML ( draft ) : '' Line truncated
}
function recoverySettlementHTML ( value , input ) {
if ( value . exchangeItems ) { const items = input ? . exchange ? . items ; return value . exchangeItems . map ( ( p , i ) => '<h3>置换商品 ' + ( i + 1 ) + ': ' + e ( p . newProduct ? . name || items ? . [ i ] ? . exchange ? . name || '' ) + '</h3>' + recoverySettlementHTML ( p , items ? { ... items [ i ] , mode : 'exchange' } : recoveryDetailInput ( p ) ) ) . join ( '' ) + recoveryItemsSummaryHTML ( value , input ) ; }
const x = input ? . exchange || value . exchange || { } , direct = ( input ? . mode || value . mode ) === 'direct' ;
const defaultAmount = value . defaultPurchaseAmount ? ? value . calculatedPurchaseAmount ? ? value . purchaseAmount , adjustment = value . roundingAdjustment ? ? value . rounding _adjustment ? ? 0 ;
const goldLine = value . differenceMg > 0 ? ` <p>补金差价:( ${ grams ( value . newMg ) } − ${ grams ( value . oldMg ) } ) g × ${ recoveryMoney ( recoveryUnits ( x . gold _price ? ? '0' , 2 , '当天销售金价' ) ) } / g = <strong>${ recoveryMoney ( value . goldAmount ) } </strong></p> ` : '<p>无需补金差价。</p>' ;
const surplusLine = value . surplusMg > 0 ? ` <p>余料回收:( ${ grams ( value . oldMg ) } − ${ grams ( value . newMg ) } ) g × ${ recoveryMoney ( recoveryUnits ( input ? . buy _rate ? ? '0' , 2 , '回收单价' ) ) } / g${ value . purchaseAmount !== defaultAmount ? ',按核实金额' : '' } = <strong> ${ recoveryMoney ( value . purchaseAmount ) } </strong></p> ` : '<p>无需余料回收。</p>' ;
const surplusLine = value . depositMg > 0 ? ` <p><strong>余料寄存: ${ grams ( value . depositMg ) } g,暂不结算。</strong>保存后加入客户寄存料,可供下次置换使用。</p> ` : value . surplusMg > 0 ? ` <p>余料回收:( ${ grams ( value . oldMg ) } − ${ grams ( value . newMg ) } ) g × ${ recoveryMoney ( recoveryUnits ( input ? . buy _rate ? ? '0' , 2 , '回收单价' ) ) } / g${ value . purchaseAmount !== defaultAmount ? ',按核实金额' : '' } = <strong> ${ recoveryMoney ( value . purchaseAmount ) } </strong></p> ` : '<p>无需余料回收。</p>' ;
const formulas = direct ? ` <p>回收金额: ${ grams ( value . oldMg ) } g × ${ recoveryMoney ( recoveryUnits ( input ? . buy _rate ? ? '0' , 2 , '回收单价' ) ) } / g = <strong>${ recoveryMoney ( defaultAmount ) } </strong></p> ${ defaultAmount !== value . purchaseAmount ? ` <p>核实后实际回收款:<strong> ${ recoveryMoney ( value . purchaseAmount ) } </strong></p> ` : '' } ` : ` ${ goldLine } <p>新货工费: ${ x . products ? '各件工费合计' : x . labor _mode === 'total' ? '按总额' : grams ( value . newMg ) + 'g × ' + recoveryMoney ( recoveryUnits ( x . labor _rate ? ? '0' , 2 , '销售工费' ) ) + '/ g' } = <strong> ${ recoveryMoney ( value . laborAmount ) } </strong></p><p>折旧费: ${ grams ( value . depreciationMg ) } g × ${ recoveryMoney ( recoveryUnits ( x . depreciation _rate ? ? '10' , 2 , '折旧费单价' ) ) } / g = <strong>${ recoveryMoney ( value . depreciationAmount ) } </strong></p> ${ value . chargesAmount ? ` <p>其他收费: ${ recoveryMoney ( value . chargesAmount ) } </p> ` : '' } ${ value . discount ? ` <p>优惠:− ${ recoveryMoney ( value . discount ) } </p> ` : '' } ${ adjustment ? ` <p>费用取整调整: ${ adjustment > 0 ? '+' : '− ' } ${ recoveryMoney ( Math . abs ( adjustment ) ) } </p> ` : '' } <p>本次置换费用:<strong> ${ recoveryMoney ( value . receivable ) } </strong></p> ${ surplusLine } ` ;
const title = value . netSettlement > 0 ? '客户补款' : value . netSettlement < 0 ? '店里付款' : '无需现金收付' ;
return ` <div class="recovery-equations"> ${ formulas } </div><div class="recovery-net ${ value . netSettlement < 0 ? 'is-payment' : '' } "><span> ${ title } </span><strong> ${ recoveryMoney ( Math . abs ( value . netSettlement ) ) } </strong></div> ${ direct ? '' : ` <p class="small muted recovery-net-formula">最终结算 = 置换费用 ${ recoveryMoney ( value . receivable ) } − 余料回收款 ${ recoveryMoney ( value . purchaseAmount ) } 。 ${ value . netSettlement === 0 ? '本次无需现金收付款。' : '' } </p> ` } ` ;
@@ -186,7 +188,7 @@ function syncRecoveryForm(form=$('#recovery-form'),{quote=true}={}){
for ( const row of form . querySelectorAll ( '[data-recovery-product]' ) ) { const index = Number ( row . dataset . recoveryProduct ) , product = draft . extra _products [ index ] ; syncStockCountField ( row , { name : 'extra_stockCount' , pocket : product . pocket , visible : exchange && pocketCountsEnabled ( product . pocket ) , optional : ! ! original ? . products ? . [ index + 1 ] && original . products [ index + 1 ] . stockCount == null } ) ; recoverySetLabel ( row , 'extra_labor_rate' , product . labor _mode === 'total' ? '销售工费(元)' : '销售工费(元/g) ' ) ; recoverySetLabel ( row , 'extra_cost_labor_value' , product . cost _labor _mode === 'total' ? '进货工费(元)' : '进货工费(元/g) ' ) ; }
const oldPocket = draft . old _pocket ? ? '回收旧料' , oldOriginal = typeof recoveryEditEntry !== 'undefined' && form . dataset . recoveryEdit === 'true' ? recoveryEditEntry ? . data ? . inputs : null ;
syncStockCountField ( form , { pocket : oldPocket , name : 'old_stock_count' , optional : ! ! oldOriginal && oldOriginal . old _stock _count == null && ( oldOriginal . old _pocket ? ? '回收旧料' ) === oldPocket } ) ;
const buyHelp = $ ( '#recovery-buy-help' ) ; if ( buyHelp ) buyHelp . textContent = exchange ? '仅多出的余料进入购料池 ' : '保存后自动记购料与' + pocketLabel ( oldPocket ) + '库存' ;
const buyHelp = $ ( '#recovery-buy-help' ) ; if ( buyHelp ) buyHelp . textContent = exchange ? '余料可按价回收,也可寄存暂不结算 ' : '保存后自动记购料与' + pocketLabel ( oldPocket ) + '库存' ;
recoverySetLabel ( form , 'labor_rate' , draft . labor _mode === 'total' ? '销售工费(元)' : '销售工费(元/g) ' ) ;
recoverySetLabel ( form , 'cost_labor_value' , draft . cost _labor _mode === 'total' ? '进货工费(元)' : '进货工费(元/g) ' ) ;
let calculation = null , input = null , error = '' , old = null , newMg = null ;
@@ -199,6 +201,7 @@ function syncRecoveryForm(form=$('#recovery-form'),{quote=true}={}){
if ( exchange && old && newMg !== null && ! draft . depreciation _manual ) { draft . depreciation _weight = grams ( Math . min ( old . oldMg , newMg ) ) ; recoveryField ( form , 'depreciation_weight' ) . value = draft . depreciation _weight ; }
const hasPurchase = ! exchange || ! ! ( old && newMg !== null && old . oldMg > newMg ) , buyPanel = form . querySelector ( '.recovery-buy-panel' ) ;
if ( buyPanel ) { buyPanel . hidden = ! hasPurchase ; for ( const control of buyPanel . querySelectorAll ( 'input,select,button' ) ) control . disabled = ! hasPurchase ; }
recoverySurplusSync ( form , draft , hasPurchase , exchange ) ;
if ( exchange && ! hasPurchase ) { draft . buy _total _manual = false ; draft . buy _total = '0' ; form . dataset . buy _total _manual = 'false' ; recoveryField ( form , 'buy_total' ) . value = '0.00' ; }
const hasDifference = exchange && ! ! ( old && newMg !== null && newMg > old . oldMg ) , goldPrice = recoveryField ( form , 'gold_price' ) , materialFields = form . querySelector ( '.recovery-material-cost .recovery-row' ) ;
if ( goldPrice ) { goldPrice . disabled = ! hasDifference ; goldPrice . required = hasDifference ; goldPrice . closest ( 'label' ) . hidden = ! hasDifference ; }
@@ -207,7 +210,7 @@ function syncRecoveryForm(form=$('#recovery-form'),{quote=true}={}){
input = recoveryInput ( draft , { quote : true } ) ; calculation = recoveryCalculate ( input ) ;
if ( ! draft . buy _total _manual ) { recoveryField ( form , 'buy_total' ) . value = ( calculation . defaultPurchaseAmount / 100 ) . toFixed ( 2 ) ; draft . buy _total = recoveryField ( form , 'buy_total' ) . value ; }
$ ( '#recovery-calculation' ) . innerHTML = recoverySettlementHTML ( calculation , input ) ;
$ ( '#recovery-buy-formula' ) . textContent = ` ${ exchange ? '余料' : '折后' } ${ grams ( calculation . surplusMg ) } g × ${ draft . buy _rate } 元/g = ${ ( calculation . defaultPurchaseAmount / 100 ) . toFixed ( 2 ) } 元 ${ draft . buy _total _manual ? ';本单采用手动核实金额。' : '。' } ` ;
$ ( '#recovery-buy-formula' ) . textContent = calculation . depositMg ? ` 余料 ${ grams ( calculation . depositMg ) } g 加入客户寄存料,暂不结算,不计回收款。 ` : ` ${ exchange ? '余料' : '折后' } ${ grams ( calculation . surplusMg ) } g × ${ draft . buy _rate } 元/g = ${ ( calculation . defaultPurchaseAmount / 100 ) . toFixed ( 2 ) } 元 ${ draft . buy _total _manual ? ';本单采用手动核实金额。' : '。' } ` ;
} catch ( err ) { error = err . message ; $ ( '#recovery-calculation' ) . innerHTML = ` <p class="muted"> ${ e ( error ) } </p> ` ; $ ( '#recovery-buy-formula' ) . textContent = '按折后重量与回收单价自动计算,实际结算有调整时可修改金额。' ; }
const net = calculation ? . netSettlement ? ? ( exchange ? 0 : - 1 ) , card = net > 0 && draft . payment _method === '信用卡' ;
for ( const [ selector , active ] of [ [ '.recovery-cash-in' , net > 0 ] , [ '.recovery-cash-out' , net < 0 ] ] ) for ( const group of form . querySelectorAll ( selector ) ) { group . hidden = ! active ; for ( const control of group . querySelectorAll ( 'input,select' ) ) control . disabled = ! active ; }
@@ -317,8 +320,8 @@ function recoveryDetailCostsHTML(value){
function recoveryDetailHTML ( value ) {
const deposits = ( value . exchangeItems ? ? [ value ] ) . filter ( p => p . customerDeposit ) . map ( p => '<p class="small">使用寄存记录 ' + e ( p . customerDeposit . number ) + ' · ' + grams ( p . customerDeposit . mg ) + 'g</p>' ) . join ( '' ) ;
const input = recoveryDetailInput ( value ) , exchange = value . mode === 'exchange' , rows = value . old _lines || [ ] , oldPocket = value . oldPocket ? ? '回收旧料' ;
const oldInventory = value . exchangeItems ? value . exchangeItems . map ( ( p , i ) => ` 商品 ${ i + 1 } 折后旧料 ${ grams ( p . oldMg ) } g 入 ${ e ( pocketLabel ( p . oldPocket ) ) } 。 ` ) . join ( '' ) : ` 折后旧料 ${ grams ( value . oldMg ) } g ${ pocketCountsEnabled ( oldPocket ) ? ' / ' + stockCountText ( value . oldStockCount ) : '' } 入 ${ e ( pocketLabel ( oldPocket ) ) } 库存。 ` ;
const inventory = value . status === 'cancelled' ? ` 本单已取消,原 ${ e ( pocketLabel ( oldPocket ) ) } 旧料入库及 ${ exchange ? '新货出库、' : '' } 关联购料已冲回。 ` : oldInventory + ( exchange ? ` 等重交换 ${ grams ( value . matchedMg ) } g 只记实物;新货 ${ grams ( value . newMg ) } g 从 ${ value . newProducts ? '各件对应货盘(外配不扣库存)' : value . newProduct ? . pocket ? e ( pocketLabel ( value . newProduct . pocket ) ) : '外配(不扣店内库存)' } 出库。 ${ value . surplusMg ? '额外回收余料 ' + grams ( value . surplusMg ) + 'g,已登记购料。' : '没有额外余料购料。' } ${ value . differenceMg ? '补金差重 ' + grams ( value . differenceMg ) + 'g 按已选成本来源核算。' : '' } ` : '已自动登记关联购料,无需另录。' ) ;
const oldInventory = value . exchangeItems ? value . exchangeItems . map ( ( p , i ) => ` 商品 ${ i + 1 } 折后旧料 ${ grams ( p . oldMg - ( p . depositMg || 0 ) ) } g 入 ${ e ( pocketLabel ( p . oldPocket ) ) } 。 ` ) . join ( '' ) : ` ${ value . depositMg ? '已结算旧料' : '折后旧料' } ${ grams ( value . oldMg - ( value . depositMg || 0 ) ) } g ${ pocketCountsEnabled ( oldPocket ) ? ' / ' + stockCountText ( value . oldStockCount ) : '' } 入 ${ e ( pocketLabel ( oldPocket ) ) } 库存。 ` ;
const inventory = value . status === 'cancelled' ? ` 本单已取消,原 ${ e ( pocketLabel ( oldPocket ) ) } 旧料入库及 ${ exchange ? '新货出库、' : '' } 关联购料已冲回。 ` : oldInventory + ( exchange ? ` 等重交换 ${ grams ( value . matchedMg ) } g 只记实物;新货 ${ grams ( value . newMg ) } g 从 ${ value . newProducts ? '各件对应货盘(外配不扣库存)' : value . newProduct ? . pocket ? e ( pocketLabel ( value . newProduct . pocket ) ) : '外配(不扣店内库存)' } 出库。 ${ value . purchaseMg ? '额外回收余料 ' + grams ( value . purchaseMg ) + 'g,已登记购料。' : '没有额外余料购料。' } ${ value . depositMg ? '客户寄存余料 ' + grams ( value . depositMg ) + 'g,暂不结算。' : '' } ${ value . differenceMg ? '补金差重 ' + grams ( value . differenceMg ) + 'g 按已选成本来源核算。' : '' } ` : '已自动登记关联购料,无需另录。' ) ;
return ` <button class="dialog-close" data-action="recovery-close" aria-label="关闭回收置换单">× </button><div class="eyebrow"> ${ exchange ? 'EXCHANGE' : 'RECOVERY' } DETAIL</div><h2 id="recovery-detail-title"> ${ e ( value . customer ) } </h2><p class="small muted"> ${ e ( recoveryModeNames [ value . mode ] ) } · ${ e ( value . number ) } · ${ e ( value . date ) } · 开单人 ${ e ( value . salesperson ) } · ${ e ( teamMemberName ( value . member ) ) } </p><div class="actions recovery-detail-actions"> ${ typeof recoveryDetailChangeActionsHTML === 'function' ? recoveryDetailChangeActionsHTML ( value ) : '' } ${ button ( '客户预览/打印' , 'recovery-print' , 'primary' , ` data-id=" ${ e ( value . id ) } " ` ) } ${ value . order _id ? button ( '查看关联销售单' , 'recovery-open-order' , '' , ` data-id=" ${ e ( value . order _id ) } " ` ) : '' } </div><section class="panel"><h3>旧料称重</h3> ${ deposits } <div class="table-wrap"><table><thead><tr><th>旧料</th><th class="number">来料(g) </th><th class="number">熔后(g) </th><th class="number">成色</th><th class="number">折后(g) </th></tr></thead><tbody> ${ rows . map ( row => ` <tr><td> ${ e ( row . name || 'Au旧料' ) } </td><td class="number"> ${ grams ( row . incomingMg ) } </td><td class="number"> ${ grams ( row . meltedMg ) } </td><td class="number"> ${ e ( row . purity ) } %</td><td class="number"> ${ grams ( row . foldedMg ) } </td></tr> ` ) . join ( '' ) } </tbody></table></div></section><section class="panel recovery-calculation"> ${ exchange ? ` ${ ( value . newProducts || [ value . newProduct ] ) . map ( ( product , index ) => ` <h3>新货 ${ index + 1 } : ${ e ( product . name ) } · ${ grams ( product . quantity ) } g ${ pocketCountsEnabled ( product . pocket ) ? ' / ' + stockCountText ( product . stockCount ) : '' } · ${ e ( pocketLabel ( product . pocket ) || '外配' ) } </h3> ${ product . laborAmount !== undefined ? '<p>销售工费:' + recoveryMoney ( product . laborAmount ) + '</p>' : '' } ` ) . join ( '' ) } ` : '' } ${ recoverySettlementHTML ( value , input ) } <p class="small"> ${ value . netSettlement > 0 ? '实际收款账户' : value . netSettlement < 0 ? '实际付款' : '无需现金收付' } ${ value . netSettlement ? ` : ${ e ( accountDisplayName ( value . payment _account ) ) } ${ value . netSettlement < 0 ? ' · ' + e ( teamMemberName ( value . payer ) ) + '付款' : '' } ` : '' } </p> ${ value . note ? ` <p class="recovery-detail-note"> ${ e ( value . note ) } </p> ` : '' } </section><div class="note"> ${ inventory } </div><section class="panel internal recovery-detail-internal"><h3>内部记账</h3><p class="small"> ${ value . purchase _id ? '已生成关联购料记录。' : '本单无额外购料。' } ${ value . order _id ? '已生成关联销售单,客户货款抵扣与实际净额分别记账。' : '' } </p> ${ recoveryDetailCostsHTML ( value ) } ${ value . order _id ? '<p class="small muted">内部成本、付款人与利润可在关联销售单核对,客户打印不显示。</p>' : '' } </section> ` ;
}
async function openRecovery ( id , saved = null ) {
@@ -405,21 +408,21 @@ document.addEventListener('submit',async event=>{
} ) ;
// 新版每件完整置换;旧版共用旧料的单据仍走原表单和算法。
const recoveryItemFields = [ 'old_source' , 'deposit_id' , 'deposit_weight' , 'old_name' , 'incoming_weight' , 'melted_weight' , 'purity' , 'old_pocket' , 'old_stock_count' , 'buy_rate' , 'buy_total' , ... recoveryProductFields , 'gold_price' , 'depreciation_weight' , 'depreciation_rate' , 'material_source' , 'material_pool_id' , 'material_rate' , 'material_payer' ] ;
const recoveryItemFields = [ 'surplus_mode' , 'surplus_memo' , ' old_source', 'deposit_id' , 'deposit_weight' , 'old_name' , 'incoming_weight' , 'melted_weight' , 'purity' , 'old_pocket' , 'old_stock_count' , 'buy_rate' , 'buy_total' , ... recoveryProductFields , 'gold_price' , 'depreciation_weight' , 'depreciation_rate' , 'material_source' , 'material_pool_id' , 'material_rate' , 'material_payer' ] ;
function recoveryItemsInput ( draft , { quote = false } = { } ) {
const common = { mode : 'exchange' , date : draft . date , customer : draft . customer , salesperson : draft . salesperson , member : draft . member , payer : draft . payer , payment _method : draft . payment _method , payment _recipient : draft . payment _recipient , payout _method : draft . payout _method , note : draft . note , verified : draft . verified } ;
const items = [ draft , ... ( draft . extra _products || [ ] ) ] . map ( row => {
const single = recoveryInput ( { ... recoveryDefaults ( 'exchange' ) , ... row , ... common , independent _products : false , extra _products : [ ] , charges : [ ] , fee _items : [ ] , charge _amount : '0' , fee _amount : '0' , discount : '0' , rounding _mode : 'cent' } , { quote } ) ;
return { ... ( single . old _source ? { old _source : single . old _source , deposit _id : single . deposit _id , deposit _weight : single . deposit _weight } : { } ) , old _lines : single . old _lines , old _pocket : single . old _pocket , ... ( single . old _stock _count == null ? { } : { old _stock _count : single . old _stock _count } ) , buy _rate : single . buy _rate , ... ( single . buy _total == null ? { } : { buy _total : single . buy _total } ) , exchange : single . exchange } ;
return { ... ( single . surplus _mode ? { surplus _mode : single . surplus _mode , surplus _memo : single . surplus _memo } : { } ) , ... ( single . old _source ? { old _source : single . old _source , deposit _id : single . deposit _id , deposit _weight : single . deposit _weight } : { } ) , old _lines : single . old _lines , old _pocket : single . old _pocket , ... ( single . old _stock _count == null ? { } : { old _stock _count : single . old _stock _count } ) , buy _rate : single . buy _rate , ... ( single . buy _total == null ? { } : { buy _total : single . buy _total } ) , exchange : single . exchange } ;
} ) ;
const first = recoveryInput ( { ... draft , independent _products : false , extra _products : [ ] , discount : '0' , rounding _mode : 'cent' } , { quote : true } ) , input = { ... first , old _lines : items . flatMap ( row => row . old _lines ) , verified : ! quote && draft . verified === true , exchange : { items , charges : first . exchange . charges , fee _items : first . exchange . fee _items , discount : draft . discount || '0' , rounding _mode : draft . rounding _mode || 'yuan' } } ;
delete input . old _source ; delete input . deposit _id ; delete input . deposit _weight ;
delete input . old _source ; delete input . deposit _id ; delete input . deposit _weight ; delete input . surplus _mode ; delete input . surplus _memo ;
const q = recoveryCalculate ( input ) ; input . payment _account = q . netSettlement > 0 ? ( draft . payment _method === '信用卡' ? '麦麦信用卡' : ( draft . payment _recipient || recoveryPrefix ( draft . member ) ) + ( draft . payment _method || '微信' ) ) : recoveryPrefix ( draft . payer || draft . member ) + ( draft . payout _method || '微信' ) ; return input ;
}
function recoveryItemsCalculate ( input ) {
const parts = input . exchange . items . map ( row => recoveryCalculate ( { ... input , ... row , buy _total : row . buy _total , buy _rate : row . buy _rate , mode : 'exchange' , exchange : row . exchange } ) ) ;
const parts = input . exchange . items . map ( row => recoveryCalculate ( { ... input , ... row , buy _total : row . buy _total , buy _rate : row . buy _rate , surplus _mode : row . surplus _mode , surplus _memo : row . surplus _memo , mode : 'exchange' , exchange : row . exchange } ) ) ;
const q = { mode : 'exchange' , old _lines : parts . flatMap ( p => p . old _lines ) , exchangeItems : parts } ;
for ( const key of [ 'oldMg' , 'newMg' , 'matchedMg' , 'differenceMg' , 'surplusMg' , 'goldAmount' , 'laborAmount' , 'depreciationMg' , 'depreciationAmount' , 'purchaseAmount' , 'defaultPurchaseAmount' ] ) q [ key ] = recoveryLimit ( parts . reduce ( ( n , p ) => n + p [ key ] , 0 ) ) ;
for ( const key of [ 'oldMg' , 'newMg' , 'matchedMg' , 'differenceMg' , 'surplusMg' , 'depositMg' , 'purchaseMg' , ' goldAmount', 'laborAmount' , 'depreciationMg' , 'depreciationAmount' , 'purchaseAmount' , 'defaultPurchaseAmount' ] ) q [ key ] = recoveryLimit ( parts . reduce ( ( n , p ) => n + p [ key ] , 0 ) ) ;
q . chargesAmount = recoveryLimit ( ( input . exchange . charges || [ ] ) . reduce ( ( n , r ) => n + recoveryUnits ( r . amount , 2 , '其他收费' ) , 0 ) ) ; q . discount = recoveryUnits ( input . exchange . discount || '0' , 2 , '整单优惠' ) ;
const before = recoveryLimit ( q . goldAmount + q . laborAmount + q . depreciationAmount + q . chargesAmount ) - q . discount ; if ( before < 0 ) throw new Error ( '优惠金额不能大于置换费用' ) ; q . receivable = input . exchange . rounding _mode === 'yuan' ? Math . floor ( ( before + 50 ) / 100 ) * 100 : before ; q . roundingAdjustment = q . receivable - before ; q . netSettlement = q . receivable - q . purchaseAmount ; q . cashReceipt = Math . max ( 0 , q . netSettlement ) ; q . cashPayment = Math . max ( 0 , - q . netSettlement ) ; return q ;
}
@@ -433,7 +436,7 @@ function recoveryItemsFormDraft(form){
function recoveryItemHTML ( row , index , date ) {
const prefix = index ? 'extra_' : '' , name = k => prefix + k , members = teamMemberOptions ( ) ;
const existing = typeof recoveryEditEntry !== 'undefined' ? recoveryEditEntry ? . data ? . inputs ? . exchange ? . items ? . [ index ] : null ;
return ` <section class="panel" data-recovery-item=" ${ index } " ${ index ? ` data-recovery-product=" ${ index - 1 } " ` : '' } data-buy_total_manual=" ${ ! ! row . buy _total _manual } " data-depreciation_manual=" ${ ! ! row . depreciation _manual } " data-melted_manual=" ${ ! ! row . melted _manual } "><div class="panel-title"><h2>置换商品 ${ index + 1 } </h2> ${ index ? button ( '移除商品' , 'recovery-remove-product' , 'quiet' , ` data-index=" ${ index - 1 } " ` ) : '' } </div> ${ customerMaterialSourceHTML ( row , prefix ) } <h3>本件旧料称重</h3><div class="recovery-row"> ${ field ( '旧料名称' , name ( 'old_name' ) , row . old _name || 'Au旧料' , 'text' , 'maxlength="60" required' ) } ${ recoveryNumberField ( '来料重量(g) ' , name ( 'incoming_weight' ) , row . incoming _weight , 3 , 'required' ) } ${ recoveryNumberField ( '熔后重量(g) ' , name ( 'melted_weight' ) , row . melted _weight , 3 , 'required' ) } ${ recoveryNumberField ( '成色(%) ' , name ( 'purity' ) , row . purity , 3 , 'max="100" required' ) } ${ pocketChoice ( '旧料入库货盘' , name ( 'old_pocket' ) , row . old _pocket ? ? '回收旧料' , { blank : null , historical : ! ! existing && existing . old _pocket === row . old _pocket } )} ${ stockCountField ( row . old _pocket , row . old _stock _count , { name : name ( 'old_stock_count' ) , title : '旧料入库数量(对)' , help : '' , optional : ! ! existing && existing . old _stock _count == null } )}</div><p class="small muted" data-item-folded></p><h3>本件换出新货</h3> ${ recoveryProductFieldsHTML ( row , { prefix , original : existing ? . exchange } )}<div class="recovery-row"> ${ recoveryNumberField ( '补金销售单价(元/g) ' , name ( 'gold_price' ) , row . gold _price , 2 , 'required' ) } ${ recoveryNumberField ( '折旧费单价(元/g) ' , name ( 'depreciation_rate' ) , row . depreciation _rate , 2 , 'required' ) } ${ recoveryNumberField ( '折旧计费克重(g,可改)' , name ( 'depreciation_weight' ) , row . depreciation _weight , 3 , 'required' ) } </div> ${ button ( '恢复本件默认折旧克重' , 'recovery-item-reset-depreciation' , 'quiet' , ` data-index=" ${ index } " ` ) } <div data-item-buy><h3>本件余料回收 </h3><div class="recovery-row"> ${ recoveryNumberField ( '回收单价(元/g) ' , name ( 'buy_rate' ) , row . buy _rate , 2 , 'required' ) } ${ recoveryNumberField ( '余料回收款(元,可改)' , name ( 'buy_total' ) , row . buy _total , 2 , 'required' ) } </div> ${ button ( '恢复本件自动回收款' , 'recovery-item-reset-buy' , 'quiet' , ` data-index=" ${ index } " ` ) } </div><div class="internal"><h3>本件内部成本</h3><div data-item-material><div class="recovery-row"> ${ choice ( '差重料费来源' , name ( 'material_source' ) , [ [ 'pool' , '每日料池' ] , [ 'manual' , '手动进货料价' ] ] , row . material _source ) } <label data-item-pool>料池日期<select name=" ${ name ( 'material_pool_id' ) } "> ${ opts ( [ [ row . material _pool _id || date , row . material _pool _id || date ] ] , row . material _pool _id || date ) } </select></label><div data-item-manual> ${ recoveryNumberField ( '差重进货料价(元/g) ' , name ( 'material_rate' ) , row . material _rate ) } ${ choice ( '料费付款人' , name ( 'material_payer' ) , members , row . material _payer ) } </div></div></div> ${ recoveryProductLaborHTML ( row , { prefix } )}<p class="small" data-item-cost></p></div><div class="recovery-calculation" data-item-formula></div></section> ` ;
return ` <section class="panel" data-recovery-item=" ${ index } " ${ index ? ` data-recovery-product=" ${ index - 1 } " ` : '' } data-buy_total_manual=" ${ ! ! row . buy _total _manual } " data-depreciation_manual=" ${ ! ! row . depreciation _manual } " data-melted_manual=" ${ ! ! row . melted _manual } "><div class="panel-title"><h2>置换商品 ${ index + 1 } </h2> ${ index ? button ( '移除商品' , 'recovery-remove-product' , 'quiet' , ` data-index=" ${ index - 1 } " ` ) : '' } </div> ${ customerMaterialSourceHTML ( row , prefix ) } <h3>本件旧料称重</h3><div class="recovery-row"> ${ field ( '旧料名称' , name ( 'old_name' ) , row . old _name || 'Au旧料' , 'text' , 'maxlength="60" required' ) } ${ recoveryNumberField ( '来料重量(g) ' , name ( 'incoming_weight' ) , row . incoming _weight , 3 , 'required' ) } ${ recoveryNumberField ( '熔后重量(g) ' , name ( 'melted_weight' ) , row . melted _weight , 3 , 'required' ) } ${ recoveryNumberField ( '成色(%) ' , name ( 'purity' ) , row . purity , 3 , 'max="100" required' ) } ${ pocketChoice ( '旧料入库货盘' , name ( 'old_pocket' ) , row . old _pocket ? ? '回收旧料' , { blank : null , historical : ! ! existing && existing . old _pocket === row . old _pocket } )} ${ stockCountField ( row . old _pocket , row . old _stock _count , { name : name ( 'old_stock_count' ) , title : '旧料入库数量(对)' , help : '' , optional : ! ! existing && existing . old _stock _count == null } )}</div><p class="small muted" data-item-folded></p><h3>本件换出新货</h3> ${ recoveryProductFieldsHTML ( row , { prefix , original : existing ? . exchange } )}<div class="recovery-row"> ${ recoveryNumberField ( '补金销售单价(元/g) ' , name ( 'gold_price' ) , row . gold _price , 2 , 'required' ) } ${ recoveryNumberField ( '折旧费单价(元/g) ' , name ( 'depreciation_rate' ) , row . depreciation _rate , 2 , 'required' ) } ${ recoveryNumberField ( '折旧计费克重(g,可改)' , name ( 'depreciation_weight' ) , row . depreciation _weight , 3 , 'required' ) } </div> ${ button ( '恢复本件默认折旧克重' , 'recovery-item-reset-depreciation' , 'quiet' , ` data-index=" ${ index } " ` ) } <div data-item-buy><h3>本件余料处理 </h3> ${ recoverySurplusFieldsHTML ( row , prefix ) } <div data-surplus-buy> <div class="recovery-row">${ recoveryNumberField ( '回收单价(元/g) ' , name ( 'buy_rate' ) , row . buy _rate , 2 , 'required' ) } ${ recoveryNumberField ( '余料回收款(元,可改)' , name ( 'buy_total' ) , row . buy _total , 2 , 'required' ) } </div> ${ button ( '恢复本件自动回收款' , 'recovery-item-reset-buy' , 'quiet' , ` data-index=" ${ index } " ` ) } </div></div>< div class="internal"><h3>本件内部成本</h3><div data-item-material><div class="recovery-row"> ${ choice ( '差重料费来源' , name ( 'material_source' ) , [ [ 'pool' , '每日料池' ] , [ 'manual' , '手动进货料价' ] ] , row . material _source ) } <label data-item-pool>料池日期<select name=" ${ name ( 'material_pool_id' ) } "> ${ opts ( [ [ row . material _pool _id || date , row . material _pool _id || date ] ] , row . material _pool _id || date ) } </select></label><div data-item-manual> ${ recoveryNumberField ( '差重进货料价(元/g) ' , name ( 'material_rate' ) , row . material _rate ) } ${ choice ( '料费付款人' , name ( 'material_payer' ) , members , row . material _payer ) } </div></div></div> ${ recoveryProductLaborHTML ( row , { prefix } )}<p class="small" data-item-cost></p></div><div class="recovery-calculation" data-item-formula></div></section> ` ;
}
function recoveryItemsFormHTML ( draft ) {
const base = recoveryFormHTML ( { ... draft , independent _products : false , extra _products : [ ] } ) , start = base . indexOf ( '<section class="panel"><div class="panel-title"><h2>旧料称重' ) , calc = base . indexOf ( '<section class="panel recovery-calculation" aria-label=' ) , internal = base . indexOf ( '<section class="panel internal recovery-internal"' ) , save = base . indexOf ( '<section class="panel recovery-save-panel">' ) ;
@@ -458,6 +461,7 @@ function syncRecoveryItems(form,{quote=true}={}){
if ( oldMg !== null && newMg !== null && ! row . depreciation _manual ) control ( 'depreciation_weight' ) . value = row . depreciation _weight = grams ( Math . min ( oldMg , newMg ) ) ;
const difference = oldMg !== null && newMg !== null && newMg > oldMg , surplus = oldMg !== null && newMg !== null && oldMg > newMg ;
toggle ( control ( 'gold_price' ) . closest ( 'label' ) , difference ) ; toggle ( section . querySelector ( '[data-item-buy]' ) , surplus ) ; toggle ( section . querySelector ( '[data-item-material]' ) , difference ) ; toggle ( section . querySelector ( '[data-item-pool]' ) , difference && row . material _source === 'pool' ) ; toggle ( section . querySelector ( '[data-item-manual]' ) , difference && row . material _source === 'manual' ) ;
recoverySurplusSync ( section , row , surplus , true , prefix ) ;
if ( ! surplus ) { control ( 'buy_total' ) . value = row . buy _total = '0' ; section . dataset . buy _total _manual = 'false' ; row . buy _total _manual = false ; }
const existing = typeof recoveryEditEntry !== 'undefined' ? recoveryEditEntry ? . data ? . inputs ? . exchange ? . items ? . [ i ] : null ;
syncStockCountField ( section , { name : prefix + 'stockCount' , pocket : row . pocket , optional : ! ! existing && existing . exchange . stockCount == null } ) ; syncStockCountField ( section , { name : prefix + 'old_stock_count' , pocket : row . old _pocket , optional : ! ! existing && existing . old _stock _count == null } ) ;
@@ -471,7 +475,7 @@ function syncRecoveryItems(form,{quote=true}={}){
$ ( '#recovery-card-owner' ) . hidden = ! card ; $ ( '#recovery-card-fee' ) . hidden = ! card ; if ( card ) $ ( '#recovery-card-fee' ) . textContent = '信用卡手续费 ' + recoveryMoney ( CreditCardFees . amount ( net , recoveryCardRate ( ) ) ) + ',麦麦付款。' ;
rememberRecoveryDraft ( form ) ; if ( quote ) { ++ recoveryQuoteRequest ; clearTimeout ( recoveryQuoteTimer ) ; $ ( '#recovery-quote-status' ) . textContent = error ? '' : '正在核对结算…' ; if ( input && ! error ) recoveryQuoteTimer = setTimeout ( ( ) => loadRecoveryQuote ( form ) , 350 ) ; } return total ;
}
function recoveryItemsSummaryHTML ( q , input ) { return ` <p>逐件补金合计 ${ grams ( q . differenceMg ) } g: ${ recoveryMoney ( q . goldAmount ) } </p><p>新货工费合计: ${ recoveryMoney ( q . laborAmount ) } ;折旧费合计: ${ recoveryMoney ( q . depreciationAmount ) } </p><p>余料回收合计 ${ grams ( q . surplusMg ) } g: ${ recoveryMoney ( q . purchaseAmount ) } </p><p>其他收费 ${ recoveryMoney ( q . chargesAmount ) } − 整单优惠 ${ recoveryMoney ( q . discount ) } ;取整调整 ${ recoveryMoney ( q . roundingAdjustment ? ? q . rounding _adjustment ? ? 0 ) } </p><p>最终结算 = 置换费用 ${ recoveryMoney ( q . receivable ) } − 余料回收款 ${ recoveryMoney ( q . purchaseAmount ) } </p><div class="recovery-net"><span> ${ q . netSettlement > 0 ? '客户补款' : q . netSettlement < 0 ? '店里付款' : '无需现金收付' } </span><strong> ${ recoveryMoney ( Math . abs ( q . netSettlement ) ) } </strong></div> ` ; }
function recoveryItemsSummaryHTML ( q , input ) { return ` <p>逐件补金合计 ${ grams ( q . differenceMg ) } g: ${ recoveryMoney ( q . goldAmount ) } </p><p>新货工费合计: ${ recoveryMoney ( q . laborAmount ) } ;折旧费合计: ${ recoveryMoney ( q . depreciationAmount ) } </p><p>余料回收合计 ${ grams ( q . surplusMg - ( q . depositMg || 0 ) ) } g: ${ recoveryMoney ( q . purchaseAmount ) } </p> ${ q . depositMg ? '<p><strong>余料寄存合计 ' + grams ( q . depositMg ) + 'g,暂不结算。</strong></p>' : '' } <p>其他收费 ${ recoveryMoney ( q . chargesAmount ) } − 整单优惠 ${ recoveryMoney ( q . discount ) } ;取整调整 ${ recoveryMoney ( q . roundingAdjustment ? ? q . rounding _adjustment ? ? 0 ) } </p><p>最终结算 = 置换费用 ${ recoveryMoney ( q . receivable ) } − 余料回收款 ${ recoveryMoney ( q . purchaseAmount ) } </p><div class="recovery-net"><span> ${ q . netSettlement > 0 ? '客户补款' : q . netSettlement < 0 ? '店里付款' : '无需现金收付' } </span><strong> ${ recoveryMoney ( Math . abs ( q . netSettlement ) ) } </strong></div> ` ; }
function recoveryItemEvent ( event , form ) {
const section = event . target . closest ( '[data-recovery-item]' ) , key = event . target . name ? . replace ( /^extra_/ , '' ) ;
if ( section ) { const prefix = section . dataset . recoveryItem === '0' ? '' : 'extra_' ; if ( key === 'incoming_weight' && section . dataset . melted _manual !== 'true' ) recoveryField ( section , prefix + 'melted_weight' ) . value = event . target . value ; for ( const [ field , flag ] of [ [ 'melted_weight' , 'melted_manual' ] , [ 'buy_total' , 'buy_total_manual' ] , [ 'depreciation_weight' , 'depreciation_manual' ] ] ) if ( key === field ) section . dataset [ flag ] = 'true' ; }
@@ -492,7 +496,7 @@ function customerMaterialSyncFields(scope,row,prefix=''){
}
function customerMaterialOldLines ( draft ) { return [ { name : '客户寄存料' , incoming _weight : draft . deposit _weight , melted _weight : draft . deposit _weight , purity : '100' } ] ; }
function customerMaterialPanelHTML ( ) { return ` <section class="panel" id="customer-material-panel"><div class="panel-title"><h2>客户寄存料</h2><div class="actions"> ${ button ( '寄存,暂不结算' , 'customer-material-new' ) } ${ button ( '刷新寄存记录' , 'customer-material-refresh' , 'quiet' ) } </div></div><p class="small muted">公共寄存台账,仅按克重和备忘记录;不计入自有库存、购料成本或可用料池。</p><div id="customer-material-list">正在读取寄存记录…</div></section> ` ; }
function customerMaterialListHTML ( data ) { return ` <div class="stats"><div><span>寄存余料</span><strong> ${ grams ( data . remainingMg ) } g</strong></div><div><span>累计寄存</span><strong> ${ grams ( data . totalMg ) } g</strong></div><div><span>已用于置换</span><strong> ${ grams ( data . usedMg ) } g</strong></div></div><div class="table-wrap"><table><thead><tr><th>日期/寄存号</th><th>备忘</th><th>寄存</th><th>已用</th><th>剩余</th><th>经办人/记录</th></tr></thead><tbody> ${ data . rows . map ( row => ` <tr><td> ${ e ( row . date ) } <div class="small muted"> ${ e ( row . number ) } </div></td><td> ${ e ( row . memo ) } </td><td> ${ grams ( row . mg ) } g</td><td> ${ grams ( row . usedMg ) } g</td><td><strong> ${ grams ( row . remainingMg ) } g</strong></td><td> ${ e ( row . actor ) } ${ row . events . length ? ` <details><summary>使用/退回记录</summary> ${ row . events . map ( event => ` <p class="small"> ${ e ( event . date ) } · ${ e ( event . memo ) } · ${ event . deltaMg > 0 ? '+' : '− ' } ${ grams ( Math . abs ( event . deltaMg ) ) } g · ${ e ( event . actor ) } </p> ` ) . join ( '' ) } </details> ` : '' } </td></tr> ` ) . join ( '' ) || '<tr><td colspan="6">暂无寄存记录</td></tr>' } </tbody></table></div> ` ; }
function customerMaterialListHTML ( data ) { return ` <div class="stats"><div><span>寄存余料</span><strong> ${ grams ( data . remainingMg ) } g</strong></div><div><span>累计寄存</span><strong> ${ grams ( data . totalMg ) } g</strong></div><div><span>已用于置换</span><strong> ${ grams ( data . usedMg ) } g</strong></div></div><div class="table-wrap"><table><thead><tr><th>日期/寄存号</th><th>备忘</th><th>寄存</th><th>已用</th><th>剩余</th><th>经办人/记录</th></tr></thead><tbody> ${ data . rows . map ( row => ` <tr><td> ${ e ( row . date ) } <div class="small muted"> ${ e ( row . number ) } </div></td><td> ${ e ( row . memo ) } ${ row . voided ? '<span class="small">(已撤回)</span>' : '' } </td><td> ${ grams ( row . mg ) } g</td><td> ${ grams ( row . usedMg ) } g</td><td><strong> ${ grams ( row . remainingMg ) } g</strong></td><td> ${ e ( row . actor ) } ${ row . events . length ? ` <details><summary>使用/退回记录</summary> ${ row . events . map ( event => ` <p class="small"> ${ e ( event . date ) } · ${ e ( event . memo ) } · ${ event . deltaMg > 0 ? '+' : '− ' } ${ grams ( Math . abs ( event . deltaMg ) ) } g · ${ e ( event . actor ) } </p> ` ) . join ( '' ) } </details> ` : '' } </td></tr> ` ) . join ( '' ) || '<tr><td colspan="6">暂无寄存记录</td></tr>' } </tbody></table></div> ` ; }
async function loadCustomerMaterials ( ) {
const user = recoveryUser ( ) , request = ++ customerMaterialRequest ; try { const data = await api ( '/api/customer-materials' ) ; if ( user !== recoveryUser ( ) || request !== customerMaterialRequest ) return ; if ( ! Array . isArray ( data . rows ) || ! Number . isSafeInteger ( data . remainingMg ) || data . rows . some ( r => ! Number . isSafeInteger ( r . remainingMg ) || r . remainingMg < 0 || ! Array . isArray ( r . events ) ) ) throw new Error ( '寄存资料未完整返回' ) ; customerMaterialCache = data ; customerMaterialCacheUser = user ; if ( $ ( '#customer-material-list' ) ) $ ( '#customer-material-list' ) . innerHTML = customerMaterialListHTML ( data ) ;
const form = $ ( '#recovery-form' ) ; if ( form ) { for ( const select of form . querySelectorAll ( '[name="deposit_id"],[name="extra_deposit_id"]' ) ) select . innerHTML = opts ( customerMaterialOptions ( select . value ) , select . value ) ; syncRecoveryForm ( form ) ; }
@@ -512,3 +516,13 @@ document.addEventListener('submit',async event=>{
const form = event . target ; if ( form . id !== 'customer-material-form' ) return ; event . preventDefault ( ) ; const user = recoveryUser ( ) ;
try { const input = { date : form . elements . date . value , grams : form . elements . grams . value , memo : form . elements . memo . value . trim ( ) , verified : form . elements . verified . checked } ; recoveryUnits ( input . grams , 3 , '寄存克重' , { zero : false } ) ; if ( ! input . memo ) throw new Error ( '请填写备忘' ) ; const result = await mutate ( '/api/customer-materials' , input ) ; if ( user !== recoveryUser ( ) ) return ; if ( ! result . id ) throw new Error ( '寄存保存结果未完整返回,请核对后再操作' ) ; await finishCustomerMaterialSave ( ) ; } catch ( err ) { if ( user === recoveryUser ( ) && $ ( '#customer-material-error' ) ) $ ( '#customer-material-error' ) . innerHTML = ` <div class="error"> ${ e ( err . message ) } </div> ` ; }
} ) ;
function recoverySurplusFieldsHTML ( row , prefix = '' ) {
return ` <div class="recovery-row"> ${ choice ( '余料处理方式' , prefix + 'surplus_mode' , [ [ 'buy' , '按回收价结算' ] , [ 'deposit' , '添加到寄存料,暂不结算' ] ] , row . surplus _mode || 'buy' ) } <div data-surplus-deposit> ${ field ( '寄存备忘(选填)' , prefix + 'surplus_memo' , row . surplus _memo || '' , 'text' , 'maxlength="160" placeholder="例如:袋号、客户代号"' ) } <p class="small muted">剩余克重自动加入客户寄存料,不折算回收款,也不加入购料池。</p></div></div> ` ;
}
function recoverySurplusSync ( scope , row , hasSurplus , exchange , prefix = '' ) {
const storing = exchange && row . surplus _mode === 'deposit' ;
for ( const [ selector , show ] of [ [ '[data-surplus-buy]' , hasSurplus && ! storing ] , [ '[data-surplus-deposit]' , hasSurplus && storing ] ] ) {
const node = scope . querySelector ( selector ) ; if ( ! node ) continue ; node . hidden = ! show ; for ( const c of node . querySelectorAll ( 'input,select,button' ) ) c . disabled = ! show ;
}
}