Working on the cart and other related things
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from ..lib_general import *
|
||||
from ..db_sql import sql_select
|
||||
from ..db_sql import redis_lookup_id_random, sql_select
|
||||
|
||||
from .order_model import Order_Base
|
||||
#from .person_model import Person_Base
|
||||
@@ -13,7 +13,7 @@ from .order_model import Order_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Model ### save_order_obj() ###
|
||||
def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -51,12 +51,12 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_line_obj_li_curr)
|
||||
|
||||
if repl_order_line_list: # This will remove any order line list items not sent with the new order information.
|
||||
if repl_order_line_li: # This will remove any order line list items not sent with the new order information.
|
||||
log.info('Removing any order line list items not sent with the new order information...')
|
||||
for index, order_line_obj_curr in enumerate(order_line_obj_li_curr):
|
||||
log.info(f'Current: order line ID={order_line_obj_curr.id_random} and product ID= {order_line_obj_curr.product_id_random}')
|
||||
matched_product_id = False
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
for order_line_obj_new in order_obj_new.order_line_li:
|
||||
log.debug(f'Checking new: product ID={order_line_obj_new.product_id_random}')
|
||||
|
||||
if order_line_obj_curr.product_id_random == order_line_obj_new.product_id_random:
|
||||
@@ -82,7 +82,7 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
# Loop through the new line list that was sent and compare with the current line list that was pulled from the DB
|
||||
# Only insert if a product ID does not match
|
||||
# Only update if a product ID does match
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
for order_line_obj_new in order_obj_new.order_line_li:
|
||||
log.info(f'New: order line ID={order_line_obj_new.id_random} and product ID= {order_line_obj_new.product_id_random}')
|
||||
matched_product_id = False
|
||||
for index, order_line_obj_curr in enumerate(order_line_obj_li_curr):
|
||||
@@ -105,14 +105,14 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
order_line_obj_li_curr.append(order_line_obj_new) # These will be inserted/updated below
|
||||
|
||||
# Save merged current and new list to the new order object
|
||||
order_obj_new.order_line_list = order_line_obj_li_curr
|
||||
order_obj_new.order_line_li = order_line_obj_li_curr
|
||||
log.debug(order_obj_new)
|
||||
|
||||
# Final loop through to get the new order totals
|
||||
# Calculate totals
|
||||
order_total_amount:int = 0
|
||||
order_total_quantity:int = 0
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
for order_line_obj_new in order_obj_new.order_line_li:
|
||||
order_total_amount += order_line_obj_new.quantity * order_line_obj_new.amount
|
||||
order_total_quantity += order_line_obj_new.quantity
|
||||
|
||||
@@ -120,8 +120,8 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
order_obj_new.total_quantity = order_total_quantity
|
||||
order_obj_new.balance = order_total_amount - order_obj_new.total_paid
|
||||
|
||||
log.debug(order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_list', 'cfg', 'created_on', 'updated_on'}))
|
||||
order_obj_data = order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_list', 'cfg', 'created_on', 'updated_on'})
|
||||
log.debug(order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_li', 'cfg', 'created_on', 'updated_on'}))
|
||||
order_obj_data = order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_li', 'cfg', 'created_on', 'updated_on'})
|
||||
|
||||
# SQL INSERT or UPDATE the order record
|
||||
log.info('SQL INSERT or UPDATE the order record')
|
||||
@@ -141,7 +141,7 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_list:bool=False):
|
||||
|
||||
# Loop through the order_line list to SQL INSERT or UPDATE the records
|
||||
log.info('Loop through the order_line list to SQL INSERT or UPDATE the records')
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
for order_line_obj_new in order_obj_new.order_line_li:
|
||||
log.info(f"New order_line: order_line_id_random={order_line_obj_new.id_random}; product_id_random={order_line_obj_new.product_id_random}")
|
||||
log.debug(order_line_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=False, exclude={'order_line_id_random', 'product_type_id', 'product_type', 'created_on', 'updated_on'}))
|
||||
|
||||
@@ -174,7 +174,7 @@ def load_order_obj(order_id:int|str, inc_order_line_li:bool=False, inc_order_cfg
|
||||
order_line_data['order_id'] = order_id
|
||||
if order_line_rec_li := sql_select(table_name='v_order_line', data=order_line_data, as_list=True):#, field_name='order_id', field_value=order_id):
|
||||
log.debug(order_line_rec_li)
|
||||
order_rec['order_line_list'] = order_line_rec_li
|
||||
order_rec['order_line_li'] = order_line_rec_li
|
||||
|
||||
if inc_order_cfg:
|
||||
if order_cfg_rec := sql_select(table_name='v_account_cfg_detail', field_name='account_id', field_value=order_rec.get('account_id', None)):
|
||||
|
||||
Reference in New Issue
Block a user